mirror of
https://github.com/vcmi/vcmi.git
synced 2024-12-24 22:14:36 +02:00
Reworked TextInput to allow text overflow support
This commit is contained in:
parent
1abe9007bc
commit
93c3cf372b
@ -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"));
|
||||
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, nullptr, 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);
|
||||
@ -72,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();
|
||||
}
|
||||
|
@ -20,12 +20,14 @@
|
||||
#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"
|
||||
#include "../widgets/ObjectLists.h"
|
||||
#include "../widgets/TextControls.h"
|
||||
|
||||
#include "../../lib/CConfigHandler.h"
|
||||
#include "../../lib/Languages.h"
|
||||
|
@ -611,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;
|
||||
}
|
||||
|
||||
|
@ -363,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, nullptr, ETextAlignment::CENTERLEFT, true);
|
||||
inputBox->removeUsedEvents(KEYBOARD);
|
||||
chatHistory = std::make_shared<CTextBox>("", chatHistoryArea, 1);
|
||||
|
||||
|
@ -181,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){
|
||||
@ -192,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){
|
||||
@ -203,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){
|
||||
@ -214,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));
|
||||
}
|
||||
});
|
||||
|
||||
@ -397,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"))
|
||||
{
|
||||
|
@ -164,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?
|
||||
@ -314,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();
|
||||
|
@ -455,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));
|
||||
@ -514,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
|
||||
}
|
||||
@ -565,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));
|
||||
|
@ -11,26 +11,34 @@
|
||||
#include "CTextInput.h"
|
||||
|
||||
#include "Images.h"
|
||||
#include "TextControls.h"
|
||||
|
||||
#include "../gui/CGuiHandler.h"
|
||||
#include "../gui/Shortcut.h"
|
||||
#include "../windows/InfoWindows.h"
|
||||
#include "../render/Graphics.h"
|
||||
#include "../render/IFont.h"
|
||||
|
||||
#include "../../lib/TextOperations.h"
|
||||
|
||||
std::list<CFocusable*> CFocusable::focusables;
|
||||
std::list<CFocusable *> CFocusable::focusables;
|
||||
CFocusable * CFocusable::inputWithFocus;
|
||||
|
||||
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)
|
||||
CTextInput::CTextInput(const Rect & Pos)
|
||||
:originalAlignment(ETextAlignment::CENTERLEFT)
|
||||
{
|
||||
setRedrawParent(true);
|
||||
pos += Pos.topLeft();
|
||||
pos.h = Pos.h;
|
||||
pos.w = Pos.w;
|
||||
maxWidth = Pos.w;
|
||||
background.reset();
|
||||
addUsedEvents(LCLICK | SHOW_POPUP | KEYBOARD | TEXTINPUT);
|
||||
|
||||
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)
|
||||
@ -38,69 +46,88 @@ CTextInput::CTextInput(const Rect & Pos, EFonts font, const CFunctionList<void(c
|
||||
#endif
|
||||
}
|
||||
|
||||
CTextInput::CTextInput(const Rect & Pos, const Point & bgOffset, const ImagePath & bgName, const CFunctionList<void(const std::string &)> & CB)
|
||||
:cb(CB)
|
||||
CTextInput::CTextInput(const Rect & Pos, EFonts font, const TextEditedCallback & onTextEdited, ETextAlignment alignment, bool giveFocusToInput)
|
||||
: CTextInput(Pos)
|
||||
{
|
||||
pos += Pos.topLeft();
|
||||
pos.h = Pos.h;
|
||||
pos.w = Pos.w;
|
||||
maxWidth = Pos.w;
|
||||
originalAlignment = alignment;
|
||||
setRedrawParent(true);
|
||||
this->onTextEdited = onTextEdited;
|
||||
createLabel(giveFocusToInput);
|
||||
setFont(font);
|
||||
setAlignment(alignment);
|
||||
}
|
||||
|
||||
CTextInput::CTextInput(const Rect & Pos, const Point & bgOffset, const ImagePath & bgName)
|
||||
: CTextInput(Pos)
|
||||
{
|
||||
OBJ_CONSTRUCTION;
|
||||
background = std::make_shared<CPicture>(bgName, bgOffset.x, bgOffset.y);
|
||||
addUsedEvents(LCLICK | SHOW_POPUP | KEYBOARD | TEXTINPUT);
|
||||
|
||||
#if !defined(VCMI_MOBILE)
|
||||
giveFocus();
|
||||
#endif
|
||||
createLabel(true);
|
||||
}
|
||||
|
||||
CTextInput::CTextInput(const Rect & Pos, std::shared_ptr<IImage> srf)
|
||||
: CTextInput(Pos)
|
||||
{
|
||||
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
|
||||
createLabel(true);
|
||||
}
|
||||
|
||||
std::atomic<int> CFocusable::usageIndex(0);
|
||||
|
||||
void CFocusable::focusGot()
|
||||
void CTextInput::setFont(EFonts font)
|
||||
{
|
||||
GH.startTextInput(pos);
|
||||
usageIndex++;
|
||||
label->font = font;
|
||||
}
|
||||
|
||||
void CFocusable::focusLost()
|
||||
void CTextInput::setColor(const ColorRGBA & color)
|
||||
{
|
||||
if(0 == --usageIndex)
|
||||
{
|
||||
GH.stopTextInput();
|
||||
}
|
||||
label->color = color;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
if(!focus)
|
||||
if(!hasFocus())
|
||||
giveFocus();
|
||||
}
|
||||
|
||||
void CTextInput::keyPressed(EShortcut key)
|
||||
{
|
||||
if(!focus)
|
||||
if(!hasFocus())
|
||||
return;
|
||||
|
||||
if(key == EShortcut::GLOBAL_MOVE_FOCUS)
|
||||
@ -114,14 +141,14 @@ void CTextInput::keyPressed(EShortcut key)
|
||||
switch(key)
|
||||
{
|
||||
case EShortcut::GLOBAL_BACKSPACE:
|
||||
if(!newText.empty())
|
||||
if(!composedText.empty())
|
||||
{
|
||||
TextOperations::trimRightUnicode(newText);
|
||||
TextOperations::trimRightUnicode(composedText);
|
||||
redrawNeeded = true;
|
||||
}
|
||||
else if(!text.empty())
|
||||
else if(!currentText.empty())
|
||||
{
|
||||
TextOperations::trimRightUnicode(text);
|
||||
TextOperations::trimRightUnicode(currentText);
|
||||
redrawNeeded = true;
|
||||
}
|
||||
break;
|
||||
@ -131,63 +158,64 @@ void CTextInput::keyPressed(EShortcut key)
|
||||
|
||||
if(redrawNeeded)
|
||||
{
|
||||
redraw();
|
||||
cb(text);
|
||||
updateLabel();
|
||||
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)
|
||||
{
|
||||
setText(nText, false);
|
||||
currentText = nText;
|
||||
updateLabel();
|
||||
}
|
||||
|
||||
void CTextInput::setText(const std::string & nText, bool callCb)
|
||||
void CTextInput::updateLabel()
|
||||
{
|
||||
CLabel::setText(nText);
|
||||
if(callCb)
|
||||
cb(text);
|
||||
}
|
||||
std::string visibleText = getVisibleText();
|
||||
|
||||
void CTextInput::setHelpText(const std::string & text)
|
||||
{
|
||||
helpBox = text;
|
||||
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(!focus)
|
||||
if(!hasFocus())
|
||||
return;
|
||||
std::string oldText = text;
|
||||
std::string oldText = currentText;
|
||||
|
||||
setText(getText() + enteredText);
|
||||
|
||||
filters(text, oldText);
|
||||
if(text != oldText)
|
||||
if(onTextFiltering)
|
||||
onTextFiltering(currentText, oldText);
|
||||
|
||||
if(currentText != oldText)
|
||||
{
|
||||
redraw();
|
||||
cb(text);
|
||||
updateLabel();
|
||||
if(onTextEdited)
|
||||
onTextEdited(currentText);
|
||||
}
|
||||
newText.clear();
|
||||
composedText.clear();
|
||||
}
|
||||
|
||||
void CTextInput::textEdited(const std::string & enteredText)
|
||||
{
|
||||
if(!focus)
|
||||
if(!hasFocus())
|
||||
return;
|
||||
|
||||
newText = enteredText;
|
||||
redraw();
|
||||
cb(text + newText);
|
||||
composedText = enteredText;
|
||||
updateLabel();
|
||||
//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
|
||||
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()
|
||||
{
|
||||
focus = false;
|
||||
focusables.push_back(this);
|
||||
}
|
||||
|
||||
CFocusable::~CFocusable()
|
||||
{
|
||||
if(hasFocus())
|
||||
{
|
||||
inputWithFocus = nullptr;
|
||||
focusLost();
|
||||
}
|
||||
|
||||
focusables -= this;
|
||||
}
|
||||
@ -255,30 +301,26 @@ bool CFocusable::hasFocus() const
|
||||
|
||||
void CFocusable::giveFocus()
|
||||
{
|
||||
focus = true;
|
||||
focusGot();
|
||||
redraw();
|
||||
|
||||
if(inputWithFocus)
|
||||
{
|
||||
inputWithFocus->focus = false;
|
||||
inputWithFocus->focusLost();
|
||||
inputWithFocus->redraw();
|
||||
}
|
||||
|
||||
auto previousInput = inputWithFocus;
|
||||
inputWithFocus = this;
|
||||
|
||||
if(previousInput)
|
||||
previousInput->focusLost();
|
||||
|
||||
focusGot();
|
||||
}
|
||||
|
||||
void CFocusable::moveFocus()
|
||||
{
|
||||
auto i = vstd::find(focusables, this),
|
||||
ourIt = i;
|
||||
auto i = vstd::find(focusables, this);
|
||||
auto ourIt = i;
|
||||
|
||||
for(i++; i != ourIt; i++)
|
||||
{
|
||||
if(i == focusables.end())
|
||||
i = focusables.begin();
|
||||
|
||||
if (*i == this)
|
||||
if(*i == this)
|
||||
return;
|
||||
|
||||
if((*i)->isActive())
|
||||
@ -293,10 +335,7 @@ void CFocusable::removeFocus()
|
||||
{
|
||||
if(this == inputWithFocus)
|
||||
{
|
||||
focus = false;
|
||||
focusLost();
|
||||
redraw();
|
||||
|
||||
inputWithFocus = nullptr;
|
||||
focusLost();
|
||||
}
|
||||
}
|
||||
|
@ -9,68 +9,54 @@
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include "TextControls.h"
|
||||
#include "../gui/CIntObject.h"
|
||||
#include "../gui/TextAlignment.h"
|
||||
#include "../render/EFont.h"
|
||||
|
||||
#include "../../lib/FunctionList.h"
|
||||
#include "../../lib/filesystem/ResourcePath.h"
|
||||
|
||||
class CLabel;
|
||||
class IImage;
|
||||
|
||||
/// UIElement which can get input focus
|
||||
class CFocusable : public virtual CIntObject
|
||||
class CFocusable : public 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;
|
||||
static std::list<CFocusable *> focusables; //all existing objs
|
||||
static CFocusable * inputWithFocus; //who has focus now
|
||||
|
||||
void focusGot();
|
||||
void focusLost();
|
||||
|
||||
static std::list<CFocusable *> focusables; //all existing objs
|
||||
static CFocusable * inputWithFocus; //who has focus now
|
||||
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 : public CLabel, public CFocusable
|
||||
class CTextInput final : public CFocusable
|
||||
{
|
||||
std::string newText;
|
||||
std::string helpBox; //for right-click help
|
||||
using TextEditedCallback = std::function<void(const std::string &)>;
|
||||
using TextFilterCallback = std::function<void(std::string &, const std::string &)>;
|
||||
|
||||
protected:
|
||||
std::string visibleText() override;
|
||||
private:
|
||||
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;
|
||||
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;
|
||||
TextEditedCallback onTextEdited;
|
||||
TextFilterCallback onTextFiltering;
|
||||
|
||||
//Filter that will block all characters not allowed in filenames
|
||||
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
|
||||
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;
|
||||
};
|
||||
|
@ -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);
|
||||
|
||||
//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);
|
||||
|
@ -130,7 +130,6 @@
|
||||
{
|
||||
"name" : "messageInput",
|
||||
"type": "textInput",
|
||||
"alignment" : "left",
|
||||
"rect": {"x": 440, "y": 568, "w": 377, "h": 20}
|
||||
},
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user