1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-01-12 02:28:11 +02:00

Merge pull request #3955 from IvanSavenko/text_input_refactor

Fix message text length limit in lobby chat
This commit is contained in:
Ivan Savenko 2024-05-15 16:46:44 +03:00 committed by GitHub
commit e00046dd70
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
21 changed files with 549 additions and 397 deletions

View File

@ -9,14 +9,10 @@
*/
#include "StdInc.h"
#include "CFocusableHelper.h"
#include "../Global.h"
#include "widgets/TextControls.h"
#include "widgets/CTextInput.h"
void removeFocusFromActiveInput()
{
if(CFocusable::inputWithFocus == nullptr)
return;
CFocusable::inputWithFocus->focus = false;
CFocusable::inputWithFocus->redraw();
CFocusable::inputWithFocus = nullptr;
if(CFocusable::inputWithFocus != nullptr)
CFocusable::inputWithFocus->removeFocus();
}

View File

@ -111,6 +111,7 @@ set(client_SRCS
widgets/CGarrisonInt.cpp
widgets/CreatureCostBox.cpp
widgets/ComboBox.cpp
widgets/CTextInput.cpp
widgets/GraphicalPrimitiveCanvas.cpp
widgets/Images.cpp
widgets/MiscWidgets.cpp
@ -304,6 +305,7 @@ set(client_HEADERS
widgets/CGarrisonInt.h
widgets/CreatureCostBox.h
widgets/ComboBox.h
widgets/CTextInput.h
widgets/GraphicalPrimitiveCanvas.h
widgets/Images.h
widgets/MiscWidgets.h

View File

@ -266,7 +266,13 @@ void CInGameConsole::startEnteringText()
return;
if(isEnteringText())
{
// force-reset text input to re-show on-screen keyboard
GH.statusbar()->setEnteringMode(false);
GH.statusbar()->setEnteringMode(true);
GH.statusbar()->setEnteredText(enteredText);
return;
}
assert(currentStatusBar.expired());//effectively, nullptr check

View File

@ -19,6 +19,7 @@
#include "../gui/CGuiHandler.h"
#include "../gui/WindowHandler.h"
#include "../widgets/Buttons.h"
#include "../widgets/CTextInput.h"
#include "../widgets/Images.h"
#include "../widgets/GraphicalPrimitiveCanvas.h"
#include "../widgets/MiscWidgets.h"
@ -45,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"));
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));
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, ETextAlignment::CENTERLEFT, true);
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(); });
labelStatus = std::make_shared<CTextBox>( "", Rect(15, 115, 255, 60), 1, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE);
@ -71,10 +72,10 @@ GlobalLobbyLoginWindow::GlobalLobbyLoginWindow()
toggleMode->setSelected(1);
filledBackground->playerColored(PlayerColor(1));
inputUsername->cb += [this](const std::string & text)
inputUsername->setCallback([this](const std::string & text)
{
this->buttonLogin->block(text.empty());
};
});
center();
}

View File

@ -20,7 +20,9 @@
#include "../CServerHandler.h"
#include "../gui/CGuiHandler.h"
#include "../gui/WindowHandler.h"
#include "../render/Colors.h"
#include "../widgets/Buttons.h"
#include "../widgets/CTextInput.h"
#include "../widgets/GraphicalPrimitiveCanvas.h"
#include "../widgets/Images.h"
#include "../widgets/MiscWidgets.h"

View File

@ -18,9 +18,10 @@
#include "../CServerHandler.h"
#include "../gui/CGuiHandler.h"
#include "../gui/WindowHandler.h"
#include "../widgets/TextControls.h"
#include "../widgets/CTextInput.h"
#include "../widgets/Slider.h"
#include "../widgets/ObjectLists.h"
#include "../widgets/TextControls.h"
#include "../../lib/Languages.h"
#include "../../lib/MetaString.h"

View File

@ -22,6 +22,7 @@
#include "../widgets/CComponent.h"
#include "../widgets/ComboBox.h"
#include "../widgets/Buttons.h"
#include "../widgets/CTextInput.h"
#include "../widgets/GraphicalPrimitiveCanvas.h"
#include "../widgets/ObjectLists.h"
#include "../widgets/Slider.h"
@ -610,19 +611,17 @@ std::shared_ptr<CTextInput> InterfaceObjectConfigurable::buildTextInput(const Js
auto rect = readRect(config["rect"]);
auto offset = readPosition(config["backgroundOffset"]);
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())
result->alignment = readTextAlignment(config["alignment"]);
result->setAlignment(readTextAlignment(config["alignment"]));
if(!config["font"].isNull())
result->font = readFont(config["font"]);
result->setFont(readFont(config["font"]));
if(!config["color"].isNull())
result->setColor(readColor(config["color"]));
if(!config["text"].isNull() && config["text"].isString())
result->setText(config["text"].String()); //for input field raw string is taken
if(!config["callback"].isNull())
result->cb += callbacks_string.at(config["callback"].String());
if(!config["help"].isNull())
result->setHelpText(readText(config["help"]));
result->setCallback(callbacks_string.at(config["callback"].String()));
return result;
}

View File

@ -17,7 +17,7 @@
#include "../gui/CGuiHandler.h"
#include "../gui/Shortcut.h"
#include "../widgets/Buttons.h"
#include "../widgets/TextControls.h"
#include "../widgets/CTextInput.h"
#include "../../CCallback.h"
#include "../../lib/CConfigHandler.h"

View File

@ -30,6 +30,7 @@
#include "../mainmenu/CMainMenu.h"
#include "../widgets/Buttons.h"
#include "../widgets/CComponent.h"
#include "../widgets/CTextInput.h"
#include "../widgets/GraphicalPrimitiveCanvas.h"
#include "../widgets/Images.h"
#include "../widgets/ObjectLists.h"
@ -362,7 +363,7 @@ CChatBox::CChatBox(const Rect & rect)
Rect textInputArea(1, rect.h - height, rect.w - 1, height);
Rect chatHistoryArea(3, 1, rect.w - 3, rect.h - height - 1);
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, ETextAlignment::CENTERLEFT, true);
inputBox->removeUsedEvents(KEYBOARD);
chatHistory = std::make_shared<CTextBox>("", chatHistoryArea, 1);

View File

@ -22,6 +22,7 @@
#include "../render/IFont.h"
#include "../widgets/CComponent.h"
#include "../widgets/ComboBox.h"
#include "../widgets/CTextInput.h"
#include "../widgets/Buttons.h"
#include "../widgets/Images.h"
#include "../widgets/MiscWidgets.h"
@ -892,7 +893,7 @@ OptionsTab::PlayerOptionsEntry::PlayerOptionsEntry(const PlayerSettings & S, con
labelPlayerName = std::make_shared<CLabel>(55, 10, EFonts::FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, name, 95);
else
{
labelPlayerNameEdit = std::make_shared<CTextInput>(Rect(6, 3, 95, 15), EFonts::FONT_SMALL, nullptr, ETextAlignment::CENTER, false);
labelPlayerNameEdit = std::make_shared<CTextInput>(Rect(6, 3, 95, 15), EFonts::FONT_SMALL, ETextAlignment::CENTER, false);
labelPlayerNameEdit->setText(name);
}
labelWhoCanPlay = std::make_shared<CMultiLineLabel>(Rect(6, 23, 45, (int)graphics->fonts[EFonts::FONT_TINY]->getLineHeight()*2), EFonts::FONT_TINY, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->arraytxt[206 + whoCanPlay]);

View File

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

View File

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

View File

@ -14,11 +14,12 @@
#include "../gui/CGuiHandler.h"
#include "../gui/WindowHandler.h"
#include "../gui/Shortcut.h"
#include "../widgets/TextControls.h"
#include "../widgets/Buttons.h"
#include "../widgets/CTextInput.h"
#include "../widgets/Images.h"
#include "../widgets/GraphicalPrimitiveCanvas.h"
#include "../windows/InfoWindows.h"
#include "../widgets/TextControls.h"
#include "../render/Canvas.h"
#include "../CGameInfo.h"
@ -372,7 +373,7 @@ CHighScoreInput::CHighScoreInput(std::string playerName, std::function<void(std:
buttonOk = std::make_shared<CButton>(Point(26, 142), AnimationPath::builtin("MUBCHCK.DEF"), CGI->generaltexth->zelp[560], std::bind(&CHighScoreInput::okay, this), EShortcut::GLOBAL_ACCEPT);
buttonCancel = std::make_shared<CButton>(Point(142, 142), AnimationPath::builtin("MUBCANC.DEF"), CGI->generaltexth->zelp[561], std::bind(&CHighScoreInput::abort, this), EShortcut::GLOBAL_CANCEL);
statusBar = CGStatusBar::create(std::make_shared<CPicture>(background->getSurface(), Rect(7, 186, 218, 18), 7, 186));
textInput = std::make_shared<CTextInput>(Rect(18, 104, 200, 25), FONT_SMALL, nullptr, ETextAlignment::CENTER, true);
textInput = std::make_shared<CTextInput>(Rect(18, 104, 200, 25), FONT_SMALL, ETextAlignment::CENTER, true);
textInput->setText(playerName);
}

View File

@ -29,6 +29,7 @@
#include "../globalLobby/GlobalLobbyWindow.h"
#include "../widgets/CComponent.h"
#include "../widgets/Buttons.h"
#include "../widgets/CTextInput.h"
#include "../widgets/MiscWidgets.h"
#include "../widgets/ObjectLists.h"
#include "../widgets/TextControls.h"
@ -454,7 +455,7 @@ CMultiMode::CMultiMode(ESelectionScreen ScreenType)
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->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));
buttonLobby = std::make_shared<CButton>(Point(373, 78 + 57 * 1), AnimationPath::builtin("MUBONL.DEF"), CGI->generaltexth->zelp[265], std::bind(&CMultiMode::openLobby, this));
@ -513,15 +514,15 @@ CMultiPlayers::CMultiPlayers(const std::string & firstPlayer, ESelectionScreen S
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]->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);
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));
inputNames[0]->setText(firstPlayer, true);
#ifndef VCMI_IOS
inputNames[0]->setText(firstPlayer);
#ifndef VCMI_MOBILE
inputNames[0]->giveFocus();
#endif
}
@ -564,13 +565,14 @@ CSimpleJoinScreen::CSimpleJoinScreen(bool host)
else
{
textTitle->setText(CGI->generaltexth->translate("vcmi.mainMenu.serverAddressEnter"));
inputAddress->cb += std::bind(&CSimpleJoinScreen::onChange, this, _1);
inputPort->cb += std::bind(&CSimpleJoinScreen::onChange, this, _1);
inputPort->filters += std::bind(&CTextInput::numberFilter, _1, _2, 0, 65535);
inputAddress->setCallback(std::bind(&CSimpleJoinScreen::onChange, this, _1));
inputPort->setCallback(std::bind(&CSimpleJoinScreen::onChange, this, _1));
inputPort->setFilterNumber(0, 65535);
inputAddress->giveFocus();
}
inputAddress->setText(host ? CSH->getLocalHostname() : CSH->getRemoteHostname(), true);
inputPort->setText(std::to_string(host ? CSH->getLocalPort() : CSH->getRemotePort()), true);
inputAddress->setText(host ? CSH->getLocalHostname() : CSH->getRemoteHostname());
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);
statusBar = CGStatusBar::create(std::make_shared<CPicture>(background->getSurface(), Rect(7, 186, 218, 18), 7, 186));

View File

@ -0,0 +1,373 @@
/*
* CTextInput.cpp, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#include "StdInc.h"
#include "CTextInput.h"
#include "Images.h"
#include "TextControls.h"
#include "../gui/CGuiHandler.h"
#include "../gui/Shortcut.h"
#include "../render/Graphics.h"
#include "../render/IFont.h"
#include "../../lib/TextOperations.h"
std::list<CFocusable *> CFocusable::focusables;
CFocusable * CFocusable::inputWithFocus;
CTextInput::CTextInput(const Rect & Pos)
:originalAlignment(ETextAlignment::CENTERLEFT)
{
pos += Pos.topLeft();
pos.h = Pos.h;
pos.w = Pos.w;
addUsedEvents(LCLICK | 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(giveFocusToInput)
giveFocus();
#endif
}
CTextInput::CTextInput(const Rect & Pos, EFonts font, ETextAlignment alignment, bool giveFocusToInput)
: CTextInput(Pos)
{
originalAlignment = alignment;
setRedrawParent(true);
createLabel(giveFocusToInput);
setFont(font);
setAlignment(alignment);
}
CTextInput::CTextInput(const Rect & Pos, const Point & bgOffset, const ImagePath & bgName)
: CTextInput(Pos)
{
OBJ_CONSTRUCTION;
if (!bgName.empty())
background = std::make_shared<CPicture>(bgName, bgOffset.x, bgOffset.y);
else
setRedrawParent(true);
createLabel(true);
}
CTextInput::CTextInput(const Rect & Pos, std::shared_ptr<IImage> srf)
: CTextInput(Pos)
{
OBJ_CONSTRUCTION;
background = std::make_shared<CPicture>(srf, Pos);
pos.w = background->pos.w;
pos.h = background->pos.h;
background->pos = pos;
createLabel(true);
}
void CTextInput::setFont(EFonts font)
{
label->font = font;
}
void CTextInput::setColor(const ColorRGBA & color)
{
label->color = color;
}
void CTextInput::setAlignment(ETextAlignment alignment)
{
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)
{
// attempt to give focus unconditionally, even if we already have it
// this forces on-screen keyboard to show up again, even if player have closed it before
giveFocus();
}
void CTextInput::keyPressed(EShortcut key)
{
if(!hasFocus())
return;
if(key == EShortcut::GLOBAL_MOVE_FOCUS)
{
moveFocus();
return;
}
bool redrawNeeded = false;
switch(key)
{
case EShortcut::GLOBAL_BACKSPACE:
if(!composedText.empty())
{
TextOperations::trimRightUnicode(composedText);
redrawNeeded = true;
}
else if(!currentText.empty())
{
TextOperations::trimRightUnicode(currentText);
redrawNeeded = true;
}
break;
default:
break;
}
if(redrawNeeded)
{
updateLabel();
if(onTextEdited)
onTextEdited(currentText);
}
}
void CTextInput::setText(const std::string & nText)
{
currentText = nText;
updateLabel();
}
void CTextInput::updateLabel()
{
std::string visibleText = getVisibleText();
label->alignment = originalAlignment;
while (graphics->fonts[label->font]->getStringWidth(visibleText) > pos.w)
{
label->alignment = ETextAlignment::CENTERRIGHT;
visibleText = visibleText.substr(TextOperations::getUnicodeCharacterSize(visibleText[0]));
}
label->setText(visibleText);
}
void CTextInput::textInputed(const std::string & enteredText)
{
if(!hasFocus())
return;
std::string oldText = currentText;
setText(getText() + enteredText);
if(onTextFiltering)
onTextFiltering(currentText, oldText);
if(currentText != oldText)
{
updateLabel();
if(onTextEdited)
onTextEdited(currentText);
}
composedText.clear();
}
void CTextInput::textEdited(const std::string & enteredText)
{
if(!hasFocus())
return;
composedText = enteredText;
updateLabel();
//onTextEdited(currentText + composedText);
}
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
size_t pos;
while((pos = text.find_first_of(forbiddenChars)) != std::string::npos)
text.erase(pos, 1);
}
void CTextInput::numberFilter(std::string & text, const std::string & oldText, int minValue, int maxValue)
{
assert(minValue < maxValue);
if(text.empty())
text = "0";
size_t pos = 0;
if(text[0] == '-') //allow '-' sign as first symbol only
pos++;
while(pos < text.size())
{
if(text[pos] < '0' || text[pos] > '9')
{
text = oldText;
return; //new text is not number.
}
pos++;
}
try
{
int value = boost::lexical_cast<int>(text);
if(value < minValue)
text = std::to_string(minValue);
else if(value > maxValue)
text = std::to_string(maxValue);
}
catch(boost::bad_lexical_cast &)
{
//Should never happen. Unless I missed some cases
logGlobal->warn("Warning: failed to convert %s to number!", text);
text = oldText;
}
}
void CTextInput::activate()
{
CFocusable::activate();
if (hasFocus())
{
#if defined(VCMI_MOBILE)
//giveFocus();
#else
GH.startTextInput(pos);
#endif
}
}
void CTextInput::deactivate()
{
CFocusable::deactivate();
if (hasFocus())
{
#if defined(VCMI_MOBILE)
removeFocus();
#else
GH.stopTextInput();
#endif
}
}
void CTextInput::onFocusGot()
{
updateLabel();
}
void CTextInput::onFocusLost()
{
updateLabel();
}
void CFocusable::focusGot()
{
if (isActive())
GH.startTextInput(pos);
onFocusGot();
}
void CFocusable::focusLost()
{
if (isActive())
GH.stopTextInput();
onFocusLost();
}
CFocusable::CFocusable()
{
focusables.push_back(this);
}
CFocusable::~CFocusable()
{
if(hasFocus())
inputWithFocus = nullptr;
focusables -= this;
}
bool CFocusable::hasFocus() const
{
return inputWithFocus == this;
}
void CFocusable::giveFocus()
{
auto previousInput = inputWithFocus;
inputWithFocus = this;
if(previousInput)
previousInput->focusLost();
focusGot();
}
void CFocusable::moveFocus()
{
auto i = vstd::find(focusables, this);
auto ourIt = i;
for(i++; i != ourIt; i++)
{
if(i == focusables.end())
i = focusables.begin();
if(*i == this)
return;
if((*i)->isActive())
{
(*i)->giveFocus();
break;
}
}
}
void CFocusable::removeFocus()
{
if(this == inputWithFocus)
{
inputWithFocus = nullptr;
focusLost();
}
}

106
client/widgets/CTextInput.h Normal file
View File

@ -0,0 +1,106 @@
/*
* CTextInput.h, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#pragma once
#include "../gui/CIntObject.h"
#include "../gui/TextAlignment.h"
#include "../render/EFont.h"
#include "../../lib/filesystem/ResourcePath.h"
class CLabel;
class IImage;
/// UIElement which can get input focus
class CFocusable : public CIntObject
{
friend void removeFocusFromActiveInput();
static std::atomic<int> usageIndex;
static std::list<CFocusable *> focusables; //all existing objs
static CFocusable * inputWithFocus; //who has focus now
void focusGot();
void focusLost();
virtual void onFocusGot() = 0;
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();
};
/// Text input box where players can enter text
class CTextInput final : public CFocusable
{
using TextEditedCallback = std::function<void(const std::string &)>;
using TextFilterCallback = std::function<void(std::string &, const std::string &)>;
private:
std::string currentText;
std::string composedText;
ETextAlignment originalAlignment;
std::shared_ptr<CPicture> background;
std::shared_ptr<CLabel> label;
TextEditedCallback onTextEdited;
TextFilterCallback onTextFiltering;
//Filter that will block all characters not allowed in filenames
static void filenameFilter(std::string & text, const std::string & oldText);
//Filter that will allow only input of numbers in range min-max (min-max are allowed)
//min-max should be set via something like std::bind
static void numberFilter(std::string & text, const std::string & oldText, int minValue, int maxValue);
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, 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;
void activate() final;
void deactivate() final;
};

View File

@ -15,7 +15,6 @@
#include "../CPlayerInterface.h"
#include "../gui/CGuiHandler.h"
#include "../gui/Shortcut.h"
#include "../windows/CMessage.h"
#include "../windows/InfoWindows.h"
#include "../adventureMap/CInGameConsole.h"
@ -30,9 +29,6 @@
#include "lib/CAndroidVMHelper.h"
#endif
std::list<CFocusable*> CFocusable::focusables;
CFocusable * CFocusable::inputWithFocus;
std::string CLabel::visibleText()
{
return text;
@ -572,283 +568,3 @@ Point CGStatusBar::getBorderSize()
assert(0);
return Point();
}
CTextInput::CTextInput(const Rect & Pos, EFonts font, const CFunctionList<void(const std::string &)> & CB, ETextAlignment alignment, bool giveFocusToInput)
: CLabel(Pos.x, Pos.y, font, alignment),
cb(CB)
{
setRedrawParent(true);
pos.h = Pos.h;
pos.w = Pos.w;
maxWidth = Pos.w;
background.reset();
addUsedEvents(LCLICK | SHOW_POPUP | KEYBOARD | TEXTINPUT);
#if !defined(VCMI_MOBILE)
if(giveFocusToInput)
giveFocus();
#endif
}
CTextInput::CTextInput(const Rect & Pos, const Point & bgOffset, const ImagePath & bgName, const CFunctionList<void(const std::string &)> & CB)
:cb(CB)
{
pos += Pos.topLeft();
pos.h = Pos.h;
pos.w = Pos.w;
maxWidth = Pos.w;
OBJ_CONSTRUCTION;
background = std::make_shared<CPicture>(bgName, bgOffset.x, bgOffset.y);
addUsedEvents(LCLICK | SHOW_POPUP | KEYBOARD | TEXTINPUT);
#if !defined(VCMI_MOBILE)
giveFocus();
#endif
}
CTextInput::CTextInput(const Rect & Pos, std::shared_ptr<IImage> srf)
{
pos += Pos.topLeft();
OBJ_CONSTRUCTION;
background = std::make_shared<CPicture>(srf, Pos);
pos.w = background->pos.w;
pos.h = background->pos.h;
maxWidth = Pos.w;
background->pos = pos;
addUsedEvents(LCLICK | KEYBOARD | TEXTINPUT);
#if !defined(VCMI_MOBILE)
giveFocus();
#endif
}
std::atomic<int> CFocusable::usageIndex(0);
void CFocusable::focusGot()
{
GH.startTextInput(pos);
usageIndex++;
}
void CFocusable::focusLost()
{
if(0 == --usageIndex)
{
GH.stopTextInput();
}
}
std::string CTextInput::visibleText()
{
return focus ? text + newText + "_" : text;
}
void CTextInput::clickPressed(const Point & cursorPosition)
{
if(!focus)
giveFocus();
}
void CTextInput::keyPressed(EShortcut key)
{
if(!focus)
return;
if(key == EShortcut::GLOBAL_MOVE_FOCUS)
{
moveFocus();
return;
}
bool redrawNeeded = false;
switch(key)
{
case EShortcut::GLOBAL_BACKSPACE:
if(!newText.empty())
{
TextOperations::trimRightUnicode(newText);
redrawNeeded = true;
}
else if(!text.empty())
{
TextOperations::trimRightUnicode(text);
redrawNeeded = true;
}
break;
default:
break;
}
if(redrawNeeded)
{
redraw();
cb(text);
}
}
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)
{
setText(nText, false);
}
void CTextInput::setText(const std::string & nText, bool callCb)
{
CLabel::setText(nText);
if(callCb)
cb(text);
}
void CTextInput::setHelpText(const std::string & text)
{
helpBox = text;
}
void CTextInput::textInputed(const std::string & enteredText)
{
if(!focus)
return;
std::string oldText = text;
setText(getText() + enteredText);
filters(text, oldText);
if(text != oldText)
{
redraw();
cb(text);
}
newText.clear();
}
void CTextInput::textEdited(const std::string & enteredText)
{
if(!focus)
return;
newText = enteredText;
redraw();
cb(text + newText);
}
void CTextInput::filenameFilter(std::string & text, const std::string &)
{
static const std::string forbiddenChars = "<>:\"/\\|?*\r\n"; //if we are entering a filename, some special characters won't be allowed
size_t pos;
while((pos = text.find_first_of(forbiddenChars)) != std::string::npos)
text.erase(pos, 1);
}
void CTextInput::numberFilter(std::string & text, const std::string & oldText, int minValue, int maxValue)
{
assert(minValue < maxValue);
if(text.empty())
text = "0";
size_t pos = 0;
if(text[0] == '-') //allow '-' sign as first symbol only
pos++;
while(pos < text.size())
{
if(text[pos] < '0' || text[pos] > '9')
{
text = oldText;
return; //new text is not number.
}
pos++;
}
try
{
int value = boost::lexical_cast<int>(text);
if(value < minValue)
text = std::to_string(minValue);
else if(value > maxValue)
text = std::to_string(maxValue);
}
catch(boost::bad_lexical_cast &)
{
//Should never happen. Unless I missed some cases
logGlobal->warn("Warning: failed to convert %s to number!", text);
text = oldText;
}
}
CFocusable::CFocusable()
{
focus = false;
focusables.push_back(this);
}
CFocusable::~CFocusable()
{
if(hasFocus())
{
inputWithFocus = nullptr;
focusLost();
}
focusables -= this;
}
bool CFocusable::hasFocus() const
{
return inputWithFocus == this;
}
void CFocusable::giveFocus()
{
focus = true;
focusGot();
redraw();
if(inputWithFocus)
{
inputWithFocus->focus = false;
inputWithFocus->focusLost();
inputWithFocus->redraw();
}
inputWithFocus = this;
}
void CFocusable::moveFocus()
{
auto i = vstd::find(focusables, this),
ourIt = i;
for(i++; i != ourIt; i++)
{
if(i == focusables.end())
i = focusables.begin();
if (*i == this)
return;
if((*i)->isActive())
{
(*i)->giveFocus();
break;
}
}
}
void CFocusable::removeFocus()
{
if(this == inputWithFocus)
{
focus = false;
focusLost();
redraw();
inputWithFocus = nullptr;
}
}

View File

@ -13,7 +13,6 @@
#include "../gui/TextAlignment.h"
#include "../render/Colors.h"
#include "../render/EFont.h"
#include "../../lib/FunctionList.h"
#include "../../lib/filesystem/ResourcePath.h"
class IImage;
@ -168,64 +167,3 @@ public:
void setEnteringMode(bool on) override;
void setEnteredText(const std::string & text) override;
};
/// UIElement which can get input focus
class CFocusable : public virtual CIntObject
{
static std::atomic<int> usageIndex;
public:
bool focus; //only one focusable control can have focus at one moment
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 focusLost();
static std::list<CFocusable *> focusables; //all existing objs
static CFocusable * inputWithFocus; //who has focus now
CFocusable();
~CFocusable();
};
/// Text input box where players can enter text
class CTextInput : public CLabel, public CFocusable
{
std::string newText;
std::string helpBox; //for right-click help
protected:
std::string visibleText() override;
public:
CFunctionList<void(const std::string &)> cb;
CFunctionList<void(std::string &, const std::string &)> filters;
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
static void filenameFilter(std::string & text, const std::string & oldText);
//Filter that will allow only input of numbers in range min-max (min-max are allowed)
//min-max should be set via something like std::bind
static void numberFilter(std::string & text, const std::string & oldText, int minValue, int maxValue);
friend class CKeyboardFocusListener;
};

View File

@ -27,6 +27,7 @@
#include "../gui/WindowHandler.h"
#include "../widgets/GraphicalPrimitiveCanvas.h"
#include "../widgets/CComponent.h"
#include "../widgets/CTextInput.h"
#include "../widgets/TextControls.h"
#include "../adventureMap/AdventureMapInterface.h"
#include "../render/CAnimation.h"
@ -137,7 +138,8 @@ CSpellWindow::CSpellWindow(const CGHeroInstance * _myHero, CPlayerInterface * _m
searchBoxRectangle = std::make_shared<TransparentFilledRectangle>(r.resize(1), rectangleColor, borderColor);
searchBoxDescription = std::make_shared<CLabel>(r.center().x, r.center().y, FONT_SMALL, ETextAlignment::CENTER, grayedColor, CGI->generaltexth->translate("vcmi.spellBook.search"));
searchBox = std::make_shared<CTextInput>(r, FONT_SMALL, std::bind(&CSpellWindow::searchInput, this), ETextAlignment::CENTER, true);
searchBox = std::make_shared<CTextInput>(r, FONT_SMALL, ETextAlignment::CENTER, true);
searchBox->setCallback(std::bind(&CSpellWindow::searchInput, this));
}
processSpells();
@ -160,7 +162,7 @@ CSpellWindow::CSpellWindow(const CGHeroInstance * _myHero, CPlayerInterface * _m
for(auto item : schoolBorders)
item->preload();
mana = std::make_shared<CLabel>(435 + (isBigSpellbook ? 159 : 0), 426 + offB, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, std::to_string(myHero->mana));
if(isBigSpellbook)
statusBar = CGStatusBar::create(400, 587);
else

View File

@ -31,6 +31,7 @@
#include "../widgets/CComponent.h"
#include "../widgets/CGarrisonInt.h"
#include "../widgets/CreatureCostBox.h"
#include "../widgets/CTextInput.h"
#include "../widgets/Buttons.h"
#include "../widgets/Slider.h"
#include "../widgets/TextControls.h"
@ -328,15 +329,18 @@ CSplitWindow::CSplitWindow(const CCreature * creature, std::function<void(int, i
int sliderPosition = total - leftMin - rightMin;
leftInput = std::make_shared<CTextInput>(Rect(20, 218, 100, 36), FONT_BIG, std::bind(&CSplitWindow::setAmountText, this, _1, true), 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);
leftInput = std::make_shared<CTextInput>(Rect(20, 218, 100, 36), FONT_BIG, ETextAlignment::CENTER, true);
rightInput = std::make_shared<CTextInput>(Rect(176, 218, 100, 36), FONT_BIG, ETextAlignment::CENTER, true);
leftInput->setCallback(std::bind(&CSplitWindow::setAmountText, this, _1, true));
rightInput->setCallback(std::bind(&CSplitWindow::setAmountText, this, _1, false));
//add filters to allow only number input
leftInput->filters += std::bind(&CTextInput::numberFilter, _1, _2, leftMin, leftMax);
rightInput->filters += std::bind(&CTextInput::numberFilter, _1, _2, rightMin, rightMax);
leftInput->setFilterNumber(leftMin, leftMax);
rightInput->setFilterNumber(rightMin, rightMax);
leftInput->setText(std::to_string(leftAmount), false);
rightInput->setText(std::to_string(rightAmount), false);
leftInput->setText(std::to_string(leftAmount));
rightInput->setText(std::to_string(rightAmount));
animLeft = std::make_shared<CCreaturePic>(20, 54, creature, true, false);
animRight = std::make_shared<CCreaturePic>(177, 54,creature, true, false);

View File

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