8000 EditBox: Add onCaretPositionChange Signal and Fix Bugs in EditBox::selectText() by CasualYT31 · Pull Request #206 · texus/TGUI · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

EditBox: Add onCaretPositionChange Signal and Fix Bugs in EditBox::selectText() #206

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions include/TGUI/Widgets/EditBox.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,13 @@ TGUI_MODULE_EXPORT namespace tgui
TGUI_NODISCARD Widget::Ptr clone() const override;


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Updates m_selEnd with a new value and emits the onCaretPositionChange signal
// @param newValue the value to assign to m_selEnd.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void updateSelEnd(const std::size_t newValue);


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
private:

Expand Down Expand Up @@ -595,9 +602,10 @@ TGUI_MODULE_EXPORT namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public:

SignalString //!< The text was changed. Optional parameter: new text
SignalString //!< The return key was pressed. Optional parameter: text in the edit box
SignalString //!< The return key was pressed or the edit box was unfocused. Optional parameter: text in the edit box
SignalString //!< The text was changed. Optional parameter: new text
SignalString //!< The return key was pressed. Optional parameter: text in the edit box
SignalString //!< The return key was pressed or the edit box was unfocused. Optional parameter: text in the edit box
SignalTyped<std::size_t> //!< The caret's position was changed. Optional parameter: new caret position


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
44 changes: 28 additions & 16 deletions src/Widgets/EditBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ namespace tgui

void EditBox::selectText(std::size_t start, std::size_t length)
{
m_selStart = start;
m_selEnd = std::min(m_text.length(), start + length);
m_selStart = std::min(m_text.length(), start);
updateSelEnd(length == String::npos ? m_text.length() : std::min(m_text.length(), start + length));
updateSelection();
}

Expand Down Expand Up @@ -334,7 +334,7 @@ namespace tgui

// Set the caret to the correct position
m_selStart = charactersBeforeCaret;
m_selEnd = charactersBeforeCaret;
updateSelEnd(charactersBeforeCaret);
updateSelection();
}

Expand Down Expand Up @@ -460,7 +460,7 @@ namespace tgui

// Select the whole text
m_selStart = 0;
m_selEnd = m_text.length();
updateSelEnd(m_text.length());
updateSelection();
}
else // No double clicking
Expand Down Expand Up @@ -501,7 +501,7 @@ namespace tgui
if (m_limitTextWidth)
{
// Find out between which characters the mouse is standing
m_selEnd = findCaretPosition(pos.x - m_bordersCached.getLeft() - m_paddingCached.getLeft());
updateSelEnd(findCaretPosition(pos.x - m_bordersCached.getLeft() - m_paddingCached.getLeft()));
}
else // Scrolling is enabled
{
Expand Down Expand Up @@ -545,7 +545,7 @@ namespace tgui
}

// Find out between which characters the mouse is standing
m_selEnd = findCaretPosition(pos.x - m_bordersCached.getLeft() - m_paddingCached.getLeft());
updateSelEnd(findCaretPosition(pos.x - m_bordersCached.getLeft() - m_paddingCached.getLeft()));
}

if (m_selEnd != oldSelEnd)
Expand Down Expand Up @@ -587,10 +587,10 @@ namespace tgui
moveCaretWordEnd();
else if (keyboard::isKeyPressMoveCaretLineStart(event) || keyboard::isKeyPressMoveCaretUp(event)
|| keyboard::isKeyPressMoveCaretDocumentBegin(event) || (event.code == Event::KeyboardKey::PageUp))
m_selEnd = 0;
updateSelEnd(0);
else if (keyboard::isKeyPressMoveCaretLineEnd(event) || keyboard::isKeyPressMoveCaretDown(event)
|| keyboard::isKeyPressMoveCaretDocumentEnd(event) || (event.code == Event::KeyboardKey::PageDown))
m_selEnd = m_text.length();
updateSelEnd(m_text.length());
else
caretMoved = false;

Expand Down Expand Up @@ -686,6 +686,8 @@ namespace tgui
return onReturnKeyPress;
else if (signalName == onReturnOrUnfocus.getName())
return onReturnOrUnfocus;
else if (signalName == onCaretPositionChange.getName())
return onCaretPositionChange;
else
return ClickableWidget::getSignal(std::move(signalName));
}
Expand Down Expand Up @@ -1421,9 +1423,9 @@ namespace tgui
{
// If text is selected then move to the cursor to the left side of the selected text
if ((m_selChars > 0) && !shiftPressed)
m_selEnd = std::min(m_selStart, m_selEnd);
updateSelEnd(std::min(m_selStart, m_selEnd));
else if (m_selEnd > 0)
m_selEnd--;
updateSelEnd(m_selEnd - 1);
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -1432,9 +1434,9 @@ namespace tgui
{
// If text is selected then move to the cursor to the right side of the selected text
if ((m_selChars > 0) && !shiftPressed)
m_selEnd = std::max(m_selStart, m_selEnd);
updateSelEnd(std::max(m_selStart, m_selEnd));
else if (m_selEnd < m_text.length())
m_selEnd++;
updateSelEnd(m_selEnd + 1);
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -1450,7 +1452,7 @@ namespace tgui
{
if (isWhitespace(m_text[i-1]))
{
m_selEnd = i;
updateSelEnd(i);
done = true;
break;
}
Expand All @@ -1463,7 +1465,7 @@ namespace tgui
}

if (!done && skippedWhitespace)
m_selEnd = 0;
updateSelEnd(0);
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -1479,7 +1481,7 @@ namespace tgui
{
if (isWhitespace(m_text[i]))
{
m_selEnd = i;
updateSelEnd(i);
done = true;
break;
}
Expand All @@ -1492,7 +1494,17 @@ namespace tgui
}

if (!done && skippedWhitespace)
m_selEnd = m_text.length();
updateSelEnd(m_text.length());
5C98 }

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void EditBox::updateSelEnd(const std::size_t newValue)
{
const bool emit = newValue != m_selEnd;
m_selEnd = newValue;
if (emit)
onCaretPositionChange.emit(this, m_selEnd);
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
0