1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-11-06 09:09:40 +02:00

Merge branch 'master' into 'develop'

This commit is contained in:
Ivan Savenko
2024-01-25 16:23:13 +02:00
26 changed files with 152 additions and 155 deletions

View File

@@ -553,8 +553,7 @@ Point CGStatusBar::getBorderSize()
CTextInput::CTextInput(const Rect & Pos, EFonts font, const CFunctionList<void(const std::string &)> & CB, bool giveFocusToInput)
: CLabel(Pos.x, Pos.y, font, ETextAlignment::CENTER),
cb(CB),
CFocusable(std::make_shared<CKeyboardFocusListener>(this))
cb(CB)
{
setRedrawParent(true);
pos.h = Pos.h;
@@ -570,7 +569,7 @@ CTextInput::CTextInput(const Rect & Pos, EFonts font, const CFunctionList<void(c
}
CTextInput::CTextInput(const Rect & Pos, const Point & bgOffset, const ImagePath & bgName, const CFunctionList<void(const std::string &)> & CB)
:cb(CB), CFocusable(std::make_shared<CKeyboardFocusListener>(this))
:cb(CB)
{
pos += Pos.topLeft();
pos.h = Pos.h;
@@ -587,7 +586,6 @@ CTextInput::CTextInput(const Rect & Pos, const Point & bgOffset, const ImagePath
}
CTextInput::CTextInput(const Rect & Pos, std::shared_ptr<IImage> srf)
:CFocusable(std::make_shared<CKeyboardFocusListener>(this))
{
pos += Pos.topLeft();
OBJ_CONSTRUCTION;
@@ -603,20 +601,15 @@ CTextInput::CTextInput(const Rect & Pos, std::shared_ptr<IImage> srf)
#endif
}
std::atomic<int> CKeyboardFocusListener::usageIndex(0);
std::atomic<int> CFocusable::usageIndex(0);
CKeyboardFocusListener::CKeyboardFocusListener(CTextInput * textInput)
:textInput(textInput)
void CFocusable::focusGot()
{
}
void CKeyboardFocusListener::focusGot()
{
GH.startTextInput(textInput->pos);
GH.startTextInput(pos);
usageIndex++;
}
void CKeyboardFocusListener::focusLost()
void CFocusable::focusLost()
{
if(0 == --usageIndex)
{
@@ -769,12 +762,6 @@ void CTextInput::numberFilter(std::string & text, const std::string & oldText, i
}
CFocusable::CFocusable()
:CFocusable(std::make_shared<IFocusListener>())
{
}
CFocusable::CFocusable(std::shared_ptr<IFocusListener> focusListener)
: focusListener(focusListener)
{
focus = false;
focusables.push_back(this);
@@ -785,7 +772,7 @@ CFocusable::~CFocusable()
if(hasFocus())
{
inputWithFocus = nullptr;
focusListener->focusLost();
focusLost();
}
focusables -= this;
@@ -799,13 +786,13 @@ bool CFocusable::hasFocus() const
void CFocusable::giveFocus()
{
focus = true;
focusListener->focusGot();
focusGot();
redraw();
if(inputWithFocus)
{
inputWithFocus->focus = false;
inputWithFocus->focusListener->focusLost();
inputWithFocus->focusLost();
inputWithFocus->redraw();
}
@@ -837,7 +824,7 @@ void CFocusable::removeFocus()
if(this == inputWithFocus)
{
focus = false;
focusListener->focusLost();
focusLost();
redraw();
inputWithFocus = nullptr;

View File

@@ -163,25 +163,12 @@ public:
void clear() override;
void setEnteringMode(bool on) override;
void setEnteredText(const std::string & text) override;
};
class CFocusable;
class IFocusListener
{
public:
virtual void focusGot() {};
virtual void focusLost() {};
virtual ~IFocusListener() = default;
};
/// UIElement which can get input focus
class CFocusable : public virtual CIntObject
{
private:
std::shared_ptr<IFocusListener> focusListener;
static std::atomic<int> usageIndex;
public:
bool focus; //only one focusable control can have focus at one moment
@@ -190,38 +177,27 @@ public:
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(std::shared_ptr<IFocusListener> focusListener);
~CFocusable();
};
class CTextInput;
class CKeyboardFocusListener : public IFocusListener
{
private:
static std::atomic<int> usageIndex;
CTextInput * textInput;
public:
CKeyboardFocusListener(CTextInput * textInput);
void focusGot() override;
void focusLost() override;
};
/// 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;