1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-11-25 22:42:04 +02:00

make town title editable

This commit is contained in:
Laserlicht
2025-10-05 01:49:00 +02:00
parent da79b27547
commit f5d1657041
7 changed files with 120 additions and 14 deletions

View File

@@ -27,6 +27,80 @@
std::list<CFocusable *> CFocusable::focusables;
CFocusable * CFocusable::inputWithFocus;
CTextInputWithConfirm::CTextInputWithConfirm(const Rect & Pos, EFonts font, ETextAlignment alignment, std::string text, bool limitToRect, std::function<void()> confirmCallback)
: CTextInput(Pos, font, alignment, false), confirmCb(confirmCallback), limitToRect(limitToRect), initialText(text)
{
setText(text);
}
bool CTextInputWithConfirm::captureThisKey(EShortcut key)
{
return hasFocus() && (key == EShortcut::GLOBAL_ACCEPT || key == EShortcut::GLOBAL_CANCEL || key == EShortcut::GLOBAL_BACKSPACE);
}
void CTextInputWithConfirm::keyPressed(EShortcut key)
{
if(!hasFocus())
return;
if(key == EShortcut::GLOBAL_ACCEPT)
confirm();
else if(key == EShortcut::GLOBAL_CANCEL)
{
setText(initialText);
removeFocus();
}
CTextInput::keyPressed(key);
}
bool CTextInputWithConfirm::receiveEvent(const Point & position, int eventType) const
{
return eventType == AEventsReceiver::LCLICK; // capture all left clicks (not only within control)
}
void CTextInputWithConfirm::clickReleased(const Point & cursorPosition)
{
if(!pos.isInside(cursorPosition)) // clicked outside
confirm();
}
void CTextInputWithConfirm::clickPressed(const Point & cursorPosition)
{
if(pos.isInside(cursorPosition)) // clickPressed should respect control area (receiveEvent also affects this)
CTextInput::clickPressed(cursorPosition);
}
void CTextInputWithConfirm::onFocusGot()
{
initialText = getText();
CTextInput::onFocusGot();
}
void CTextInputWithConfirm::textInputted(const std::string & enteredText)
{
if(!hasFocus())
return;
CTextInput::textInputted(enteredText);
std::string visibleText = getVisibleText();
const auto & font = ENGINE->renderHandler().loadFont(label->font);
while(limitToRect && font->getStringWidth(visibleText) > pos.w)
{
TextOperations::trimRightUnicode(currentText);
visibleText = getVisibleText();
}
}
void CTextInputWithConfirm::confirm()
{
if(confirmCb && initialText != getText())
confirmCb();
removeFocus();
}
CTextInput::CTextInput(const Rect & Pos)
:originalAlignment(ETextAlignment::CENTERLEFT)
{