1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00

Moved CTextInput to a separate file. No changes in logic

This commit is contained in:
Ivan Savenko 2024-05-12 10:24:13 +00:00
parent 1b1f724de1
commit 1abe9007bc
18 changed files with 401 additions and 350 deletions

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

@ -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"

View File

@ -21,11 +21,11 @@
#include "../gui/CGuiHandler.h"
#include "../gui/WindowHandler.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"

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"

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"

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"

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"

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"

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"

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"

View File

@ -0,0 +1,302 @@
/*
* 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 "../gui/CGuiHandler.h"
#include "../gui/Shortcut.h"
#include "../windows/InfoWindows.h"
#include "../../lib/TextOperations.h"
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)
{
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

@ -0,0 +1,82 @@
/*
* 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 "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
{
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

@ -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"

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"