Skip to content

Commit

Permalink
Fix for #86: Invalid cursor position after mouse click on line with t…
Browse files Browse the repository at this point in the history
…abulations

Also removed some unnecessary SanitizeCooridates calls.
  • Loading branch information
BalazsJako committed Jun 14, 2019
1 parent e2a2520 commit 9f8a336
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions TextEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,20 +335,23 @@ TextEditor::Coordinates TextEditor::ScreenPosToCoordinates(const ImVec2& aPositi
auto& line = mLines.at(lineNo);

int columnIndex = 0;
std::string cumulatedString = "";
float columnWidth = 0.0f;
float columnX = 0.0f;

// First we find the hovered column coord.
while (mTextStart + columnX < local.x && (size_t)columnIndex < line.size())
while ((size_t)columnIndex < line.size())
{
float columnWidth = 0.0f;

if (line[columnIndex].mChar == '\t')
{
float spaceSize = ImGui::GetFont()->CalcTextSizeA(ImGui::GetFontSize(), FLT_MAX, -1.0f, " ").x;
float oldX = columnX;
columnX = (1.0f + std::floor((1.0f + columnX) / (float(mTabSize) * spaceSize))) * (float(mTabSize) * spaceSize);
columnWidth = columnX - oldX;
columnCoord++;
float newColumnX = (1.0f + std::floor((1.0f + columnX) / (float(mTabSize) * spaceSize))) * (float(mTabSize) * spaceSize);
columnWidth = newColumnX - oldX;
if (mTextStart + columnX + columnWidth * 0.5f > local.x)
break;
columnX = newColumnX;
columnCoord = (columnCoord / mTabSize) * mTabSize + mTabSize;
columnIndex++;
}
else
{
Expand All @@ -359,14 +362,12 @@ TextEditor::Coordinates TextEditor::ScreenPosToCoordinates(const ImVec2& aPositi
buf[i++] = line[columnIndex++].mChar;
buf[i] = '\0';
columnWidth = ImGui::GetFont()->CalcTextSizeA(ImGui::GetFontSize(), FLT_MAX, -1.0f, buf).x;
if (mTextStart + columnX + columnWidth * 0.5f > local.x)
break;
columnX += columnWidth;
columnCoord++;
}
}

// Then we reduce by 1 column coord if cursor is on the left side of the hovered column.
if (mTextStart + columnX - columnWidth / 2.0f > local.x)
columnIndex = std::max(0, columnIndex - 1);
}

return SanitizeCoordinates(Coordinates(lineNo, columnCoord));
Expand Down Expand Up @@ -760,7 +761,8 @@ void TextEditor::HandleKeyboardInputs()
EnterCharacter('\n', false);
else if (!IsReadOnly() && !ctrl && !alt && ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_Tab)))
EnterCharacter('\t', shift);
if (!IsReadOnly() && !io.InputQueueCharacters.empty() && !ctrl)

if (!IsReadOnly() && !io.InputQueueCharacters.empty())
{
for (int i = 0; i < io.InputQueueCharacters.Size; i++)
{
Expand Down Expand Up @@ -797,7 +799,7 @@ void TextEditor::HandleMouseInputs()
{
if (!ctrl)
{
mState.mCursorPosition = mInteractiveStart = mInteractiveEnd = SanitizeCoordinates(ScreenPosToCoordinates(ImGui::GetMousePos()));
mState.mCursorPosition = mInteractiveStart = mInteractiveEnd = ScreenPosToCoordinates(ImGui::GetMousePos());
mSelectionMode = SelectionMode::Line;
SetSelection(mInteractiveStart, mInteractiveEnd, mSelectionMode);
}
Expand All @@ -813,7 +815,7 @@ void TextEditor::HandleMouseInputs()
{
if (!ctrl)
{
mState.mCursorPosition = mInteractiveStart = mInteractiveEnd = SanitizeCoordinates(ScreenPosToCoordinates(ImGui::GetMousePos()));
mState.mCursorPosition = mInteractiveStart = mInteractiveEnd = ScreenPosToCoordinates(ImGui::GetMousePos());
if (mSelectionMode == SelectionMode::Line)
mSelectionMode = SelectionMode::Normal;
else
Expand All @@ -829,7 +831,7 @@ void TextEditor::HandleMouseInputs()
*/
else if (click)
{
mState.mCursorPosition = mInteractiveStart = mInteractiveEnd = SanitizeCoordinates(ScreenPosToCoordinates(ImGui::GetMousePos()));
mState.mCursorPosition = mInteractiveStart = mInteractiveEnd = ScreenPosToCoordinates(ImGui::GetMousePos());
if (ctrl)
mSelectionMode = SelectionMode::Word;
else
Expand All @@ -842,7 +844,7 @@ void TextEditor::HandleMouseInputs()
else if (ImGui::IsMouseDragging(0) && ImGui::IsMouseDown(0))
{
io.WantCaptureMouse = true;
mState.mCursorPosition = mInteractiveEnd = SanitizeCoordinates(ScreenPosToCoordinates(ImGui::GetMousePos()));
mState.mCursorPosition = mInteractiveEnd = ScreenPosToCoordinates(ImGui::GetMousePos());
SetSelection(mInteractiveStart, mInteractiveEnd, mSelectionMode);
}
}
Expand Down Expand Up @@ -1122,7 +1124,7 @@ void TextEditor::Render(const char* aTitle, const ImVec2& aSize, bool aBorder)
mTextChanged = false;
mCursorPositionChanged = false;

ImGui::PushStyleColor(ImGuiCol_ChildBg, ImGui::ColorConvertU32ToFloat4(mPalette[(int)PaletteIndex::Background]));
ImGui::PushStyleColor(ImGuiCol_ChildWindowBg, ImGui::ColorConvertU32ToFloat4(mPalette[(int)PaletteIndex::Background]));
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0.0f, 0.0f));
if (!mIgnoreImGuiChild)
ImGui::BeginChild(aTitle, aSize, aBorder, ImGuiWindowFlags_HorizontalScrollbar | ImGuiWindowFlags_AlwaysHorizontalScrollbar | ImGuiWindowFlags_NoMove);
Expand Down

0 comments on commit 9f8a336

Please sign in to comment.