Fixed cursor positioning issues with new ImGui mentioned in: https://github.com/BalazsJako/ImGuiColorTextEdit/issues/71
diff --git a/TextEditor.cpp b/TextEditor.cpp index 2093ce1..cddb6ca 100644 --- a/TextEditor.cpp +++ b/TextEditor.cpp
@@ -266,7 +266,7 @@ { cumulatedStringWidth[1] = cumulatedStringWidth[0]; cumulatedString += line[columnCoord].mChar; - cumulatedStringWidth[0] = ImGui::CalcTextSize(cumulatedString.c_str()).x; + cumulatedStringWidth[0] = ImGui::GetFont()->CalcTextSizeA(ImGui::GetFontSize(), FLT_MAX, -1.0f, cumulatedString.c_str(), nullptr, nullptr).x; columnWidth = (cumulatedStringWidth[0] - cumulatedStringWidth[1]); columnCoord++; } @@ -615,7 +615,7 @@ void TextEditor::Render() { /* Compute mCharAdvance regarding to scaled font size (Ctrl + mouse wheel)*/ - const float fontSize = ImGui::CalcTextSize("#").x; + const float fontSize = ImGui::GetFont()->CalcTextSizeA(ImGui::GetFontSize(), FLT_MAX, -1.0f, "#", nullptr, nullptr).x; mCharAdvance = ImVec2(fontSize, ImGui::GetTextLineHeightWithSpacing() * mLineSpacing); /* Update palette with the current alpha from style */ @@ -650,12 +650,12 @@ // Deduce mTextStart by evaluating mLines size (global lineMax) plus two spaces as text width char buf[16]; snprintf(buf, 16, " %d ", globalLineMax); - mTextStart = ImGui::CalcTextSize(buf).x + mLeftMargin; + mTextStart = ImGui::GetFont()->CalcTextSizeA(ImGui::GetFontSize(), FLT_MAX, -1.0f, buf, nullptr, nullptr).x + mLeftMargin; if (!mLines.empty()) { auto fontScale = ImGui::GetFontSize() / ImGui::GetFont()->FontSize; - float spaceSize = ImGui::CalcTextSize(" ").x + 1.0f * fontScale; + float spaceSize = ImGui::GetFont()->CalcTextSizeA(ImGui::GetFontSize(), FLT_MAX, -1.0f, " ", nullptr, nullptr).x; while (lineNo <= lineMax) { @@ -720,7 +720,8 @@ // Draw line number (right aligned) snprintf(buf, 16, "%d ", lineNo + 1); - auto lineNoWidth = ImGui::CalcTextSize(buf).x; + + auto lineNoWidth = ImGui::GetFont()->CalcTextSizeA(ImGui::GetFontSize(), FLT_MAX, -1.0f, buf, nullptr, nullptr).x; drawList->AddText(ImVec2(lineStartScreenPos.x + mTextStart - lineNoWidth, lineStartScreenPos.y), mPalette[(int)PaletteIndex::LineNumber], buf); // Highlight the current line (where the cursor is) @@ -766,8 +767,8 @@ { const ImVec2 newOffset(textScreenPos.x + bufferOffset.x, textScreenPos.y + bufferOffset.y); drawList->AddText(newOffset, prevColor, buffer.c_str()); - auto textSize = ImGui::CalcTextSize(buffer.c_str()); - bufferOffset.x += textSize.x + 1.0f * fontScale; + auto textSize = ImGui::GetFont()->CalcTextSizeA(ImGui::GetFontSize(), FLT_MAX, -1.0f, buffer.c_str(), nullptr, nullptr); + bufferOffset.x += textSize.x; buffer.clear(); } prevColor = color; @@ -1972,7 +1973,7 @@ auto& line = mLines[aFrom.mLine]; float distance = 0.0f; auto fontScale = ImGui::GetFontSize() / ImGui::GetFont()->FontSize; - float spaceSize = ImGui::CalcTextSize(" ").x + 1.0f * fontScale; + float spaceSize = ImGui::GetFont()->CalcTextSizeA(ImGui::GetFontSize(), FLT_MAX, -1.0f, " ", nullptr, nullptr).x; for (size_t it = 0u; it < line.size() && it < (unsigned)aFrom.mColumn; ++it) { if (line[it].mChar == '\t') @@ -1984,7 +1985,7 @@ char tempCString[2]; tempCString[0] = line[it].mChar; tempCString[1] = '\0'; - distance += ImGui::CalcTextSize(tempCString).x + 1.0f * fontScale; + distance += ImGui::GetFont()->CalcTextSizeA(ImGui::GetFontSize(), FLT_MAX, -1.0f, tempCString, nullptr, nullptr).x; } }