@@ -15,6 +15,7 @@ namespace chesspp
1515 class Button : public sf ::Drawable
1616 {
1717 sf::Text button;
18+ sf::Vector2f center;
1819 /* *
1920 * \brief
2021 * Overriden sf::Drawable::draw method to provide to custom rendering behavior
@@ -24,6 +25,11 @@ namespace chesspp
2425 */
2526 virtual void draw (sf::RenderTarget& target, sf::RenderStates states) const override ;
2627
28+ void recenter () noexcept
29+ {
30+ button.setPosition (center.x - (button.getLocalBounds ().width /2 ), center.y - (button.getLocalBounds ().height /2 ));
31+ }
32+
2733 public:
2834 /* *
2935 * \brief
@@ -54,7 +60,8 @@ namespace chesspp
5460 */
5561 void setPosition (int x, int y)
5662 {
57- button.setPosition (x,y);
63+ center = sf::Vector2f{sf::Vector2i{x, y}};
64+ recenter ();
5865 }
5966 /* *
6067 * \brief
@@ -64,7 +71,8 @@ namespace chesspp
6471 */
6572 void setPosition (sf::Vector2f &pos)
6673 {
67- button.setPosition (pos);
74+ center = pos;
75+ recenter ();
6876 }
6977 /* *
7078 * \brief
@@ -75,6 +83,7 @@ namespace chesspp
7583 void setText (const std::string &text)
7684 {
7785 button.setString (text);
86+ recenter ();
7887 }
7988 /* *
8089 * \brief
@@ -95,6 +104,7 @@ namespace chesspp
95104 void setFont (const sf::Font &font)
96105 {
97106 button.setFont (font);
107+ recenter ();
98108 }
99109 /* *
100110 * \brief
@@ -115,7 +125,7 @@ namespace chesspp
115125 */
116126 sf::Vector2f getPosition ()
117127 {
118- return button. getPosition () ;
128+ return center ;
119129 }
120130 /* *
121131 * \brief
@@ -135,6 +145,7 @@ namespace chesspp
135145 */
136146 bool contains (int x, int y)
137147 {
148+ recenter ();
138149 return button.getGlobalBounds ().contains (x,y);
139150 }
140151
0 commit comments