1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-06-15 00:05:02 +02:00

Reworked TextInput to allow text overflow support

This commit is contained in:
Ivan Savenko
2024-05-12 14:17:49 +00:00
parent 1abe9007bc
commit 93c3cf372b
11 changed files with 228 additions and 169 deletions

View File

@ -46,7 +46,7 @@ GlobalLobbyLoginWindow::GlobalLobbyLoginWindow()
labelUsernameTitle = std::make_shared<CLabel>( 10, 65, FONT_MEDIUM, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->translate("vcmi.lobby.login.username")); labelUsernameTitle = std::make_shared<CLabel>( 10, 65, FONT_MEDIUM, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->translate("vcmi.lobby.login.username"));
labelUsername = std::make_shared<CLabel>( 10, 65, FONT_MEDIUM, ETextAlignment::TOPLEFT, Colors::WHITE, loginAs.toString()); labelUsername = std::make_shared<CLabel>( 10, 65, FONT_MEDIUM, ETextAlignment::TOPLEFT, Colors::WHITE, loginAs.toString());
backgroundUsername = std::make_shared<TransparentFilledRectangle>(Rect(10, 90, 264, 20), ColorRGBA(0,0,0,128), ColorRGBA(64,64,64,64)); backgroundUsername = std::make_shared<TransparentFilledRectangle>(Rect(10, 90, 264, 20), ColorRGBA(0,0,0,128), ColorRGBA(64,64,64,64));
inputUsername = std::make_shared<CTextInput>(Rect(15, 93, 260, 16), FONT_SMALL, nullptr, ETextAlignment::TOPLEFT, true); inputUsername = std::make_shared<CTextInput>(Rect(15, 93, 260, 16), FONT_SMALL, nullptr, ETextAlignment::CENTERLEFT, true);
buttonLogin = std::make_shared<CButton>(Point(10, 180), AnimationPath::builtin("MuBchck"), CButton::tooltip(), [this](){ onLogin(); }); buttonLogin = std::make_shared<CButton>(Point(10, 180), AnimationPath::builtin("MuBchck"), CButton::tooltip(), [this](){ onLogin(); });
buttonClose = std::make_shared<CButton>(Point(210, 180), AnimationPath::builtin("MuBcanc"), CButton::tooltip(), [this](){ onClose(); }); buttonClose = std::make_shared<CButton>(Point(210, 180), AnimationPath::builtin("MuBcanc"), CButton::tooltip(), [this](){ onClose(); });
labelStatus = std::make_shared<CTextBox>( "", Rect(15, 115, 255, 60), 1, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE); labelStatus = std::make_shared<CTextBox>( "", Rect(15, 115, 255, 60), 1, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE);
@ -72,10 +72,10 @@ GlobalLobbyLoginWindow::GlobalLobbyLoginWindow()
toggleMode->setSelected(1); toggleMode->setSelected(1);
filledBackground->playerColored(PlayerColor(1)); filledBackground->playerColored(PlayerColor(1));
inputUsername->cb += [this](const std::string & text) inputUsername->setCallback([this](const std::string & text)
{ {
this->buttonLogin->block(text.empty()); this->buttonLogin->block(text.empty());
}; });
center(); center();
} }

View File

@ -20,12 +20,14 @@
#include "../CServerHandler.h" #include "../CServerHandler.h"
#include "../gui/CGuiHandler.h" #include "../gui/CGuiHandler.h"
#include "../gui/WindowHandler.h" #include "../gui/WindowHandler.h"
#include "../render/Colors.h"
#include "../widgets/Buttons.h" #include "../widgets/Buttons.h"
#include "../widgets/CTextInput.h" #include "../widgets/CTextInput.h"
#include "../widgets/GraphicalPrimitiveCanvas.h" #include "../widgets/GraphicalPrimitiveCanvas.h"
#include "../widgets/Images.h" #include "../widgets/Images.h"
#include "../widgets/MiscWidgets.h" #include "../widgets/MiscWidgets.h"
#include "../widgets/ObjectLists.h" #include "../widgets/ObjectLists.h"
#include "../widgets/TextControls.h"
#include "../../lib/CConfigHandler.h" #include "../../lib/CConfigHandler.h"
#include "../../lib/Languages.h" #include "../../lib/Languages.h"

View File

@ -611,19 +611,17 @@ std::shared_ptr<CTextInput> InterfaceObjectConfigurable::buildTextInput(const Js
auto rect = readRect(config["rect"]); auto rect = readRect(config["rect"]);
auto offset = readPosition(config["backgroundOffset"]); auto offset = readPosition(config["backgroundOffset"]);
auto bgName = ImagePath::fromJson(config["background"]); auto bgName = ImagePath::fromJson(config["background"]);
auto result = std::make_shared<CTextInput>(rect, offset, bgName, 0); auto result = std::make_shared<CTextInput>(rect, offset, bgName);
if(!config["alignment"].isNull()) if(!config["alignment"].isNull())
result->alignment = readTextAlignment(config["alignment"]); result->setAlignment(readTextAlignment(config["alignment"]));
if(!config["font"].isNull()) if(!config["font"].isNull())
result->font = readFont(config["font"]); result->setFont(readFont(config["font"]));
if(!config["color"].isNull()) if(!config["color"].isNull())
result->setColor(readColor(config["color"])); result->setColor(readColor(config["color"]));
if(!config["text"].isNull() && config["text"].isString()) if(!config["text"].isNull() && config["text"].isString())
result->setText(config["text"].String()); //for input field raw string is taken result->setText(config["text"].String()); //for input field raw string is taken
if(!config["callback"].isNull()) if(!config["callback"].isNull())
result->cb += callbacks_string.at(config["callback"].String()); result->setCallback(callbacks_string.at(config["callback"].String()));
if(!config["help"].isNull())
result->setHelpText(readText(config["help"]));
return result; return result;
} }

View File

@ -363,7 +363,7 @@ CChatBox::CChatBox(const Rect & rect)
Rect textInputArea(1, rect.h - height, rect.w - 1, height); Rect textInputArea(1, rect.h - height, rect.w - 1, height);
Rect chatHistoryArea(3, 1, rect.w - 3, rect.h - height - 1); Rect chatHistoryArea(3, 1, rect.w - 3, rect.h - height - 1);
inputBackground = std::make_shared<TransparentFilledRectangle>(textInputArea, ColorRGBA(0,0,0,192)); inputBackground = std::make_shared<TransparentFilledRectangle>(textInputArea, ColorRGBA(0,0,0,192));
inputBox = std::make_shared<CTextInput>(textInputArea, EFonts::FONT_SMALL, nullptr, ETextAlignment::TOPLEFT, true); inputBox = std::make_shared<CTextInput>(textInputArea, EFonts::FONT_SMALL, nullptr, ETextAlignment::CENTERLEFT, true);
inputBox->removeUsedEvents(KEYBOARD); inputBox->removeUsedEvents(KEYBOARD);
chatHistory = std::make_shared<CTextBox>("", chatHistoryArea, 1); chatHistory = std::make_shared<CTextBox>("", chatHistoryArea, 1);

View File

@ -181,7 +181,7 @@ OptionsTabBase::OptionsTabBase(const JsonPath & configPath)
tinfo.baseTimer = time; tinfo.baseTimer = time;
CSH->setTurnTimerInfo(tinfo); CSH->setTurnTimerInfo(tinfo);
if(auto ww = widget<CTextInput>("chessFieldBase")) if(auto ww = widget<CTextInput>("chessFieldBase"))
ww->setText(timeToString(time), false); ww->setText(timeToString(time));
} }
}); });
addCallback("parseAndSetTimer_turn", [this, parseTimerString](const std::string & str){ addCallback("parseAndSetTimer_turn", [this, parseTimerString](const std::string & str){
@ -192,7 +192,7 @@ OptionsTabBase::OptionsTabBase(const JsonPath & configPath)
tinfo.turnTimer = time; tinfo.turnTimer = time;
CSH->setTurnTimerInfo(tinfo); CSH->setTurnTimerInfo(tinfo);
if(auto ww = widget<CTextInput>("chessFieldTurn")) if(auto ww = widget<CTextInput>("chessFieldTurn"))
ww->setText(timeToString(time), false); ww->setText(timeToString(time));
} }
}); });
addCallback("parseAndSetTimer_battle", [this, parseTimerString](const std::string & str){ addCallback("parseAndSetTimer_battle", [this, parseTimerString](const std::string & str){
@ -203,7 +203,7 @@ OptionsTabBase::OptionsTabBase(const JsonPath & configPath)
tinfo.battleTimer = time; tinfo.battleTimer = time;
CSH->setTurnTimerInfo(tinfo); CSH->setTurnTimerInfo(tinfo);
if(auto ww = widget<CTextInput>("chessFieldBattle")) if(auto ww = widget<CTextInput>("chessFieldBattle"))
ww->setText(timeToString(time), false); ww->setText(timeToString(time));
} }
}); });
addCallback("parseAndSetTimer_unit", [this, parseTimerString](const std::string & str){ addCallback("parseAndSetTimer_unit", [this, parseTimerString](const std::string & str){
@ -214,7 +214,7 @@ OptionsTabBase::OptionsTabBase(const JsonPath & configPath)
tinfo.unitTimer = time; tinfo.unitTimer = time;
CSH->setTurnTimerInfo(tinfo); CSH->setTurnTimerInfo(tinfo);
if(auto ww = widget<CTextInput>("chessFieldUnit")) if(auto ww = widget<CTextInput>("chessFieldUnit"))
ww->setText(timeToString(time), false); ww->setText(timeToString(time));
} }
}); });
@ -397,13 +397,13 @@ void OptionsTabBase::recreate(bool campaign)
} }
if(auto ww = widget<CTextInput>("chessFieldBase")) if(auto ww = widget<CTextInput>("chessFieldBase"))
ww->setText(timeToString(turnTimerRemote.baseTimer), false); ww->setText(timeToString(turnTimerRemote.baseTimer));
if(auto ww = widget<CTextInput>("chessFieldTurn")) if(auto ww = widget<CTextInput>("chessFieldTurn"))
ww->setText(timeToString(turnTimerRemote.turnTimer), false); ww->setText(timeToString(turnTimerRemote.turnTimer));
if(auto ww = widget<CTextInput>("chessFieldBattle")) if(auto ww = widget<CTextInput>("chessFieldBattle"))
ww->setText(timeToString(turnTimerRemote.battleTimer), false); ww->setText(timeToString(turnTimerRemote.battleTimer));
if(auto ww = widget<CTextInput>("chessFieldUnit")) if(auto ww = widget<CTextInput>("chessFieldUnit"))
ww->setText(timeToString(turnTimerRemote.unitTimer), false); ww->setText(timeToString(turnTimerRemote.unitTimer));
if(auto w = widget<ComboBox>("timerModeSwitch")) if(auto w = widget<ComboBox>("timerModeSwitch"))
{ {

View File

@ -164,8 +164,8 @@ SelectionTab::SelectionTab(ESelectionScreen Type)
{ {
background = std::make_shared<CPicture>(ImagePath::builtin("SCSELBCK.bmp"), 0, 6); background = std::make_shared<CPicture>(ImagePath::builtin("SCSELBCK.bmp"), 0, 6);
pos = background->pos; pos = background->pos;
inputName = std::make_shared<CTextInput>(inputNameRect, Point(-32, -25), ImagePath::builtin("GSSTRIP.bmp"), 0); inputName = std::make_shared<CTextInput>(inputNameRect, Point(-32, -25), ImagePath::builtin("GSSTRIP.bmp"));
inputName->filters += CTextInput::filenameFilter; inputName->setFilterFilename();
labelMapSizes = std::make_shared<CLabel>(87, 62, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[510]); labelMapSizes = std::make_shared<CLabel>(87, 62, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[510]);
// TODO: Global constants? // TODO: Global constants?
@ -314,7 +314,7 @@ void SelectionTab::clickReleased(const Point & cursorPosition)
{ {
select(line); select(line);
} }
#ifdef VCMI_IOS #ifdef VCMI_MOBILE
// focus input field if clicked inside it // focus input field if clicked inside it
else if(inputName && inputName->isActive() && inputNameRect.isInside(cursorPosition)) else if(inputName && inputName->isActive() && inputNameRect.isInside(cursorPosition))
inputName->giveFocus(); inputName->giveFocus();

View File

@ -455,7 +455,7 @@ CMultiMode::CMultiMode(ESelectionScreen ScreenType)
statusBar = CGStatusBar::create(std::make_shared<CPicture>(background->getSurface(), Rect(7, 465, 440, 18), 7, 465)); statusBar = CGStatusBar::create(std::make_shared<CPicture>(background->getSurface(), Rect(7, 465, 440, 18), 7, 465));
playerName = std::make_shared<CTextInput>(Rect(19, 436, 334, 16), background->getSurface()); playerName = std::make_shared<CTextInput>(Rect(19, 436, 334, 16), background->getSurface());
playerName->setText(getPlayerName()); playerName->setText(getPlayerName());
playerName->cb += std::bind(&CMultiMode::onNameChange, this, _1); playerName->setCallback(std::bind(&CMultiMode::onNameChange, this, _1));
buttonHotseat = std::make_shared<CButton>(Point(373, 78 + 57 * 0), AnimationPath::builtin("MUBHOT.DEF"), CGI->generaltexth->zelp[266], std::bind(&CMultiMode::hostTCP, this)); buttonHotseat = std::make_shared<CButton>(Point(373, 78 + 57 * 0), AnimationPath::builtin("MUBHOT.DEF"), CGI->generaltexth->zelp[266], std::bind(&CMultiMode::hostTCP, this));
buttonLobby = std::make_shared<CButton>(Point(373, 78 + 57 * 1), AnimationPath::builtin("MUBONL.DEF"), CGI->generaltexth->zelp[265], std::bind(&CMultiMode::openLobby, this)); buttonLobby = std::make_shared<CButton>(Point(373, 78 + 57 * 1), AnimationPath::builtin("MUBONL.DEF"), CGI->generaltexth->zelp[265], std::bind(&CMultiMode::openLobby, this));
@ -514,15 +514,15 @@ CMultiPlayers::CMultiPlayers(const std::string & firstPlayer, ESelectionScreen S
for(int i = 0; i < inputNames.size(); i++) for(int i = 0; i < inputNames.size(); i++)
{ {
inputNames[i] = std::make_shared<CTextInput>(Rect(60, 85 + i * 30, 280, 16), background->getSurface()); inputNames[i] = std::make_shared<CTextInput>(Rect(60, 85 + i * 30, 280, 16), background->getSurface());
inputNames[i]->cb += std::bind(&CMultiPlayers::onChange, this, _1); inputNames[i]->setCallback(std::bind(&CMultiPlayers::onChange, this, _1));
} }
buttonOk = std::make_shared<CButton>(Point(95, 338), AnimationPath::builtin("MUBCHCK.DEF"), CGI->generaltexth->zelp[560], std::bind(&CMultiPlayers::enterSelectionScreen, this), EShortcut::GLOBAL_ACCEPT); buttonOk = std::make_shared<CButton>(Point(95, 338), AnimationPath::builtin("MUBCHCK.DEF"), CGI->generaltexth->zelp[560], std::bind(&CMultiPlayers::enterSelectionScreen, this), EShortcut::GLOBAL_ACCEPT);
buttonCancel = std::make_shared<CButton>(Point(205, 338), AnimationPath::builtin("MUBCANC.DEF"), CGI->generaltexth->zelp[561], [=](){ close();}, EShortcut::GLOBAL_CANCEL); buttonCancel = std::make_shared<CButton>(Point(205, 338), AnimationPath::builtin("MUBCANC.DEF"), CGI->generaltexth->zelp[561], [=](){ close();}, EShortcut::GLOBAL_CANCEL);
statusBar = CGStatusBar::create(std::make_shared<CPicture>(background->getSurface(), Rect(7, 381, 348, 18), 7, 381)); statusBar = CGStatusBar::create(std::make_shared<CPicture>(background->getSurface(), Rect(7, 381, 348, 18), 7, 381));
inputNames[0]->setText(firstPlayer, true); inputNames[0]->setText(firstPlayer);
#ifndef VCMI_IOS #ifndef VCMI_MOBILE
inputNames[0]->giveFocus(); inputNames[0]->giveFocus();
#endif #endif
} }
@ -565,13 +565,14 @@ CSimpleJoinScreen::CSimpleJoinScreen(bool host)
else else
{ {
textTitle->setText(CGI->generaltexth->translate("vcmi.mainMenu.serverAddressEnter")); textTitle->setText(CGI->generaltexth->translate("vcmi.mainMenu.serverAddressEnter"));
inputAddress->cb += std::bind(&CSimpleJoinScreen::onChange, this, _1); inputAddress->setCallback(std::bind(&CSimpleJoinScreen::onChange, this, _1));
inputPort->cb += std::bind(&CSimpleJoinScreen::onChange, this, _1); inputPort->setCallback(std::bind(&CSimpleJoinScreen::onChange, this, _1));
inputPort->filters += std::bind(&CTextInput::numberFilter, _1, _2, 0, 65535); inputPort->setFilterNumber(0, 65535);
inputAddress->giveFocus(); inputAddress->giveFocus();
} }
inputAddress->setText(host ? CSH->getLocalHostname() : CSH->getRemoteHostname(), true); inputAddress->setText(host ? CSH->getLocalHostname() : CSH->getRemoteHostname());
inputPort->setText(std::to_string(host ? CSH->getLocalPort() : CSH->getRemotePort()), true); inputPort->setText(std::to_string(host ? CSH->getLocalPort() : CSH->getRemotePort()));
buttonOk->block(inputAddress->getText().empty() || inputPort->getText().empty());
buttonCancel = std::make_shared<CButton>(Point(142, 142), AnimationPath::builtin("MUBCANC.DEF"), CGI->generaltexth->zelp[561], std::bind(&CSimpleJoinScreen::leaveScreen, this), EShortcut::GLOBAL_CANCEL); buttonCancel = std::make_shared<CButton>(Point(142, 142), AnimationPath::builtin("MUBCANC.DEF"), CGI->generaltexth->zelp[561], std::bind(&CSimpleJoinScreen::leaveScreen, this), EShortcut::GLOBAL_CANCEL);
statusBar = CGStatusBar::create(std::make_shared<CPicture>(background->getSurface(), Rect(7, 186, 218, 18), 7, 186)); statusBar = CGStatusBar::create(std::make_shared<CPicture>(background->getSurface(), Rect(7, 186, 218, 18), 7, 186));

View File

@ -11,26 +11,34 @@
#include "CTextInput.h" #include "CTextInput.h"
#include "Images.h" #include "Images.h"
#include "TextControls.h"
#include "../gui/CGuiHandler.h" #include "../gui/CGuiHandler.h"
#include "../gui/Shortcut.h" #include "../gui/Shortcut.h"
#include "../windows/InfoWindows.h" #include "../render/Graphics.h"
#include "../render/IFont.h"
#include "../../lib/TextOperations.h" #include "../../lib/TextOperations.h"
std::list<CFocusable *> CFocusable::focusables; std::list<CFocusable *> CFocusable::focusables;
CFocusable * CFocusable::inputWithFocus; CFocusable * CFocusable::inputWithFocus;
CTextInput::CTextInput(const Rect & Pos, EFonts font, const CFunctionList<void(const std::string &)> & CB, ETextAlignment alignment, bool giveFocusToInput) CTextInput::CTextInput(const Rect & Pos)
: CLabel(Pos.x, Pos.y, font, alignment), :originalAlignment(ETextAlignment::CENTERLEFT)
cb(CB)
{ {
setRedrawParent(true); pos += Pos.topLeft();
pos.h = Pos.h; pos.h = Pos.h;
pos.w = Pos.w; pos.w = Pos.w;
maxWidth = Pos.w;
background.reset(); addUsedEvents(LCLICK | KEYBOARD | TEXTINPUT);
addUsedEvents(LCLICK | SHOW_POPUP | KEYBOARD | TEXTINPUT); }
void CTextInput::createLabel(bool giveFocusToInput)
{
OBJ_CONSTRUCTION;
label = std::make_shared<CLabel>();
label->pos = pos;
label->alignment = originalAlignment;
#if !defined(VCMI_MOBILE) #if !defined(VCMI_MOBILE)
if(giveFocusToInput) if(giveFocusToInput)
@ -38,69 +46,88 @@ CTextInput::CTextInput(const Rect & Pos, EFonts font, const CFunctionList<void(c
#endif #endif
} }
CTextInput::CTextInput(const Rect & Pos, const Point & bgOffset, const ImagePath & bgName, const CFunctionList<void(const std::string &)> & CB) CTextInput::CTextInput(const Rect & Pos, EFonts font, const TextEditedCallback & onTextEdited, ETextAlignment alignment, bool giveFocusToInput)
:cb(CB) : CTextInput(Pos)
{ {
pos += Pos.topLeft(); originalAlignment = alignment;
pos.h = Pos.h; setRedrawParent(true);
pos.w = Pos.w; this->onTextEdited = onTextEdited;
maxWidth = Pos.w; createLabel(giveFocusToInput);
setFont(font);
setAlignment(alignment);
}
CTextInput::CTextInput(const Rect & Pos, const Point & bgOffset, const ImagePath & bgName)
: CTextInput(Pos)
{
OBJ_CONSTRUCTION; OBJ_CONSTRUCTION;
background = std::make_shared<CPicture>(bgName, bgOffset.x, bgOffset.y); background = std::make_shared<CPicture>(bgName, bgOffset.x, bgOffset.y);
addUsedEvents(LCLICK | SHOW_POPUP | KEYBOARD | TEXTINPUT); createLabel(true);
#if !defined(VCMI_MOBILE)
giveFocus();
#endif
} }
CTextInput::CTextInput(const Rect & Pos, std::shared_ptr<IImage> srf) CTextInput::CTextInput(const Rect & Pos, std::shared_ptr<IImage> srf)
: CTextInput(Pos)
{ {
pos += Pos.topLeft();
OBJ_CONSTRUCTION; OBJ_CONSTRUCTION;
background = std::make_shared<CPicture>(srf, Pos); background = std::make_shared<CPicture>(srf, Pos);
pos.w = background->pos.w; pos.w = background->pos.w;
pos.h = background->pos.h; pos.h = background->pos.h;
maxWidth = Pos.w;
background->pos = pos; background->pos = pos;
addUsedEvents(LCLICK | KEYBOARD | TEXTINPUT); createLabel(true);
#if !defined(VCMI_MOBILE)
giveFocus();
#endif
} }
std::atomic<int> CFocusable::usageIndex(0); void CTextInput::setFont(EFonts font)
void CFocusable::focusGot()
{ {
GH.startTextInput(pos); label->font = font;
usageIndex++;
} }
void CFocusable::focusLost() void CTextInput::setColor(const ColorRGBA & color)
{ {
if(0 == --usageIndex) label->color = color;
{
GH.stopTextInput();
}
} }
std::string CTextInput::visibleText() void CTextInput::setAlignment(ETextAlignment alignment)
{ {
return focus ? text + newText + "_" : text; originalAlignment = alignment;
label->alignment = alignment;
}
const std::string & CTextInput::getText() const
{
return currentText;
}
void CTextInput::setCallback(const TextEditedCallback & cb)
{
assert(!onTextEdited);
onTextEdited = cb;
}
void CTextInput::setFilterFilename()
{
assert(!onTextFiltering);
onTextFiltering = std::bind(&CTextInput::filenameFilter, _1, _2);
}
void CTextInput::setFilterNumber(int minValue, int maxValue)
{
onTextFiltering = std::bind(&CTextInput::numberFilter, _1, _2, minValue, maxValue);
}
std::string CTextInput::getVisibleText()
{
return hasFocus() ? currentText + composedText + "_" : currentText;
} }
void CTextInput::clickPressed(const Point & cursorPosition) void CTextInput::clickPressed(const Point & cursorPosition)
{ {
if(!focus) if(!hasFocus())
giveFocus(); giveFocus();
} }
void CTextInput::keyPressed(EShortcut key) void CTextInput::keyPressed(EShortcut key)
{ {
if(!focus) if(!hasFocus())
return; return;
if(key == EShortcut::GLOBAL_MOVE_FOCUS) if(key == EShortcut::GLOBAL_MOVE_FOCUS)
@ -114,14 +141,14 @@ void CTextInput::keyPressed(EShortcut key)
switch(key) switch(key)
{ {
case EShortcut::GLOBAL_BACKSPACE: case EShortcut::GLOBAL_BACKSPACE:
if(!newText.empty()) if(!composedText.empty())
{ {
TextOperations::trimRightUnicode(newText); TextOperations::trimRightUnicode(composedText);
redrawNeeded = true; redrawNeeded = true;
} }
else if(!text.empty()) else if(!currentText.empty())
{ {
TextOperations::trimRightUnicode(text); TextOperations::trimRightUnicode(currentText);
redrawNeeded = true; redrawNeeded = true;
} }
break; break;
@ -131,63 +158,64 @@ void CTextInput::keyPressed(EShortcut key)
if(redrawNeeded) if(redrawNeeded)
{ {
redraw(); updateLabel();
cb(text); if(onTextEdited)
onTextEdited(currentText);
} }
} }
void CTextInput::showPopupWindow(const Point & cursorPosition)
{
if(!helpBox.empty()) //there is no point to show window with nothing inside...
CRClickPopup::createAndPush(helpBox);
}
void CTextInput::setText(const std::string & nText) void CTextInput::setText(const std::string & nText)
{ {
setText(nText, false); currentText = nText;
updateLabel();
} }
void CTextInput::setText(const std::string & nText, bool callCb) void CTextInput::updateLabel()
{ {
CLabel::setText(nText); std::string visibleText = getVisibleText();
if(callCb)
cb(text); label->alignment = originalAlignment;
while (graphics->fonts[label->font]->getStringWidth(visibleText) > pos.w)
{
label->alignment = ETextAlignment::CENTERRIGHT;
visibleText = visibleText.substr(TextOperations::getUnicodeCharacterSize(visibleText[0]));
} }
void CTextInput::setHelpText(const std::string & text) label->setText(visibleText);
{
helpBox = text;
} }
void CTextInput::textInputed(const std::string & enteredText) void CTextInput::textInputed(const std::string & enteredText)
{ {
if(!focus) if(!hasFocus())
return; return;
std::string oldText = text; std::string oldText = currentText;
setText(getText() + enteredText); setText(getText() + enteredText);
filters(text, oldText); if(onTextFiltering)
if(text != oldText) onTextFiltering(currentText, oldText);
if(currentText != oldText)
{ {
redraw(); updateLabel();
cb(text); if(onTextEdited)
onTextEdited(currentText);
} }
newText.clear(); composedText.clear();
} }
void CTextInput::textEdited(const std::string & enteredText) void CTextInput::textEdited(const std::string & enteredText)
{ {
if(!focus) if(!hasFocus())
return; return;
newText = enteredText; composedText = enteredText;
redraw(); updateLabel();
cb(text + newText); //onTextEdited(currentText + composedText);
} }
void CTextInput::filenameFilter(std::string & text, const std::string &) void CTextInput::filenameFilter(std::string & text, const std::string &oldText)
{ {
static const std::string forbiddenChars = "<>:\"/\\|?*\r\n"; //if we are entering a filename, some special characters won't be allowed static const std::string forbiddenChars = "<>:\"/\\|?*\r\n"; //if we are entering a filename, some special characters won't be allowed
size_t pos; size_t pos;
@ -231,19 +259,37 @@ void CTextInput::numberFilter(std::string & text, const std::string & oldText, i
} }
} }
void CTextInput::onFocusGot()
{
updateLabel();
}
void CTextInput::onFocusLost()
{
updateLabel();
}
void CFocusable::focusGot()
{
GH.startTextInput(pos);
onFocusGot();
}
void CFocusable::focusLost()
{
GH.stopTextInput();
onFocusLost();
}
CFocusable::CFocusable() CFocusable::CFocusable()
{ {
focus = false;
focusables.push_back(this); focusables.push_back(this);
} }
CFocusable::~CFocusable() CFocusable::~CFocusable()
{ {
if(hasFocus()) if(hasFocus())
{
inputWithFocus = nullptr; inputWithFocus = nullptr;
focusLost();
}
focusables -= this; focusables -= this;
} }
@ -255,24 +301,20 @@ bool CFocusable::hasFocus() const
void CFocusable::giveFocus() void CFocusable::giveFocus()
{ {
focus = true; auto previousInput = inputWithFocus;
focusGot();
redraw();
if(inputWithFocus)
{
inputWithFocus->focus = false;
inputWithFocus->focusLost();
inputWithFocus->redraw();
}
inputWithFocus = this; inputWithFocus = this;
if(previousInput)
previousInput->focusLost();
focusGot();
} }
void CFocusable::moveFocus() void CFocusable::moveFocus()
{ {
auto i = vstd::find(focusables, this), auto i = vstd::find(focusables, this);
ourIt = i; auto ourIt = i;
for(i++; i != ourIt; i++) for(i++; i != ourIt; i++)
{ {
if(i == focusables.end()) if(i == focusables.end())
@ -293,10 +335,7 @@ void CFocusable::removeFocus()
{ {
if(this == inputWithFocus) if(this == inputWithFocus)
{ {
focus = false;
focusLost();
redraw();
inputWithFocus = nullptr; inputWithFocus = nullptr;
focusLost();
} }
} }

View File

@ -9,68 +9,54 @@
*/ */
#pragma once #pragma once
#include "TextControls.h"
#include "../gui/CIntObject.h" #include "../gui/CIntObject.h"
#include "../gui/TextAlignment.h" #include "../gui/TextAlignment.h"
#include "../render/EFont.h" #include "../render/EFont.h"
#include "../../lib/FunctionList.h"
#include "../../lib/filesystem/ResourcePath.h" #include "../../lib/filesystem/ResourcePath.h"
class CLabel; class CLabel;
class IImage; class IImage;
/// UIElement which can get input focus /// UIElement which can get input focus
class CFocusable : public virtual CIntObject class CFocusable : public CIntObject
{ {
static std::atomic<int> usageIndex; static std::atomic<int> usageIndex;
public: static std::list<CFocusable *> focusables; //all existing objs
bool focus; //only one focusable control can have focus at one moment static CFocusable * inputWithFocus; //who has focus now
void giveFocus(); //captures focus
void moveFocus(); //moves focus to next active control (may be used for tab switching)
void removeFocus(); //remove focus
bool hasFocus() const;
void focusGot(); void focusGot();
void focusLost(); void focusLost();
static std::list<CFocusable *> focusables; //all existing objs virtual void onFocusGot() = 0;
static CFocusable * inputWithFocus; //who has focus now virtual void onFocusLost() = 0;
public:
void giveFocus(); //captures focus
void moveFocus(); //moves focus to next active control (may be used for tab switching)
void removeFocus(); //remove focus
bool hasFocus() const;
CFocusable(); CFocusable();
~CFocusable(); ~CFocusable();
}; };
/// Text input box where players can enter text /// Text input box where players can enter text
class CTextInput : public CLabel, public CFocusable class CTextInput final : public CFocusable
{ {
std::string newText; using TextEditedCallback = std::function<void(const std::string &)>;
std::string helpBox; //for right-click help using TextFilterCallback = std::function<void(std::string &, const std::string &)>;
protected: private:
std::string visibleText() override; std::string currentText;
std::string composedText;
ETextAlignment originalAlignment;
public: std::shared_ptr<CPicture> background;
std::shared_ptr<CLabel> label;
CFunctionList<void(const std::string &)> cb; TextEditedCallback onTextEdited;
CFunctionList<void(std::string &, const std::string &)> filters; TextFilterCallback onTextFiltering;
void setText(const std::string & nText) override;
void setText(const std::string & nText, bool callCb);
void setHelpText(const std::string &);
CTextInput(const Rect & Pos, EFonts font, const CFunctionList<void(const std::string &)> & CB, ETextAlignment alignment, bool giveFocusToInput);
CTextInput(const Rect & Pos, const Point & bgOffset, const ImagePath & bgName, const CFunctionList<void(const std::string &)> & CB);
CTextInput(const Rect & Pos, std::shared_ptr<IImage> srf);
void clickPressed(const Point & cursorPosition) override;
void keyPressed(EShortcut key) override;
void showPopupWindow(const Point & cursorPosition) override;
//bool captureThisKey(EShortcut key) override;
void textInputed(const std::string & enteredText) override;
void textEdited(const std::string & enteredText) override;
//Filter that will block all characters not allowed in filenames //Filter that will block all characters not allowed in filenames
static void filenameFilter(std::string & text, const std::string & oldText); static void filenameFilter(std::string & text, const std::string & oldText);
@ -78,5 +64,39 @@ public:
//min-max should be set via something like std::bind //min-max should be set via something like std::bind
static void numberFilter(std::string & text, const std::string & oldText, int minValue, int maxValue); static void numberFilter(std::string & text, const std::string & oldText, int minValue, int maxValue);
friend class CKeyboardFocusListener; std::string getVisibleText();
void createLabel(bool giveFocusToInput);
void updateLabel();
void clickPressed(const Point & cursorPosition) final;
void textInputed(const std::string & enteredText) final;
void textEdited(const std::string & enteredText) final;
void onFocusGot() final;
void onFocusLost() final;
CTextInput(const Rect & Pos);
public:
CTextInput(const Rect & Pos, EFonts font, const TextEditedCallback& onTextEdited, ETextAlignment alignment, bool giveFocusToInput);
CTextInput(const Rect & Pos, const Point & bgOffset, const ImagePath & bgName);
CTextInput(const Rect & Pos, std::shared_ptr<IImage> srf);
/// Returns currently entered text. May not match visible text
const std::string & getText() const;
void setText(const std::string & nText);
/// Set callback that will be called whenever player enters new text
void setCallback(const TextEditedCallback & cb);
/// Enables filtering entered text that ensures that text is valid filename (existing or not)
void setFilterFilename();
/// Enable filtering entered text that ensures that text is valid number in provided range [min, max]
void setFilterNumber(int minValue, int maxValue);
void setFont(EFonts Font);
void setColor(const ColorRGBA & Color);
void setAlignment(ETextAlignment alignment);
// CIntObject interface impl
void keyPressed(EShortcut key) final;
}; };

View File

@ -333,11 +333,11 @@ CSplitWindow::CSplitWindow(const CCreature * creature, std::function<void(int, i
rightInput = std::make_shared<CTextInput>(Rect(176, 218, 100, 36), FONT_BIG, std::bind(&CSplitWindow::setAmountText, this, _1, false), ETextAlignment::CENTER, true); rightInput = std::make_shared<CTextInput>(Rect(176, 218, 100, 36), FONT_BIG, std::bind(&CSplitWindow::setAmountText, this, _1, false), ETextAlignment::CENTER, true);
//add filters to allow only number input //add filters to allow only number input
leftInput->filters += std::bind(&CTextInput::numberFilter, _1, _2, leftMin, leftMax); leftInput->setFilterNumber(leftMin, leftMax);
rightInput->filters += std::bind(&CTextInput::numberFilter, _1, _2, rightMin, rightMax); rightInput->setFilterNumber(rightMin, rightMax);
leftInput->setText(std::to_string(leftAmount), false); leftInput->setText(std::to_string(leftAmount));
rightInput->setText(std::to_string(rightAmount), false); rightInput->setText(std::to_string(rightAmount));
animLeft = std::make_shared<CCreaturePic>(20, 54, creature, true, false); animLeft = std::make_shared<CCreaturePic>(20, 54, creature, true, false);
animRight = std::make_shared<CCreaturePic>(177, 54,creature, true, false); animRight = std::make_shared<CCreaturePic>(177, 54,creature, true, false);

View File

@ -130,7 +130,6 @@
{ {
"name" : "messageInput", "name" : "messageInput",
"type": "textInput", "type": "textInput",
"alignment" : "left",
"rect": {"x": 440, "y": 568, "w": 377, "h": 20} "rect": {"x": 440, "y": 568, "w": 377, "h": 20}
}, },