Ver Fonte

Merge CKeyboardFocusListener and IFocusListener into CFocusable

Ivan Savenko há 1 ano atrás
pai
commit
6a42494b12
2 ficheiros alterados com 16 adições e 53 exclusões
  1. 10 23
      client/widgets/TextControls.cpp
  2. 6 30
      client/widgets/TextControls.h

+ 10 - 23
client/widgets/TextControls.cpp

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

+ 6 - 30
client/widgets/TextControls.h

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