Hey everyone, I haven't written any C++ code in a long time, but I recently started a new project and I'm using SFML. Years ago when I was in highschool, I actually wrote several projects in SFML, and I figured I would reuse some of my code. But this question isn't about SFML, it's about software design.
The code that I'm about to show you is just a header file, because I think that's all that's needed for this discussion, but what immediately jumped out at me when I looked at this header file is that the code seems over-engineered and like it has a lot of redundancies, but it also does look clean and nice, and it's easy to read.
I was hoping that someone here might have a bit more experience than me with software design, and maybe I could get some ideas for how I should refactor it (or perhaps this is good, and I shouldn't refactor it). Anyways, here goes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
class TextButton{
private:
sf::Text button;
sf::FloatRect collision;
sf::Color defaultColor;
sf::Color hoverColor;
bool mouseClicked;
public:
TextButton(const sf::Vector2f& position,
const sf::Color& defaultClr,
const sf::Color& hoverClr,
const std::string& text,
const int& fontSize);
bool pollClicked();
bool isHovering();
void draw();
//setters:
void setPosition(const float& x, const float& y);
void setHoverColor(const sf::Color& color);
void setPosition(const sf::Vector2f& pos);
void setColor(const sf::Color& color);
void setText(const std::string& text);
void setFont(const sf::Font& font);
void setWasClicked(bool c);
//getters:
sf::FloatRect getCollision();
sf::Color getDefaultColor();
sf::Vector2f getPosition();
sf::Color getHoverColor();
const sf::Font* getFont();
std::string getText();
bool getWasClicked();
};
|
Alright, so basically, the way this works is that the sf::Text object at the very top of the class is an object from SFML, and it's the actual text that's being drawn to the screen. All of that is handled by SFML. I use this object to emulate a text button, and basically the functions within the class will check to see where the mouse is in relation to the sf::Text object in the render window, and if it's hovering over the sf::Text object, the color will change to the "hover color" and if clicked, then pollClicked() returns true. That's all fine and dandy, and exactly what I need it to do. The three functions below the constructor handle all of that functionality.
But the problem is all of the setters and getters. I think that I could actually get rid of almost all of those, and just make the sf::Text object public, because most of those setters and getters are just one-liners in the actual function's source code, and it literally just calls the corresponding functions for the sf::Text object. It is possible that if I did that, you could use this class in a way that isn't intended, such as changing the color of the sf::Text object to something other than default or hover, but it would just get changed back whenever something triggers it to change to the default/hover color, and it shouldn't break anything; therefore I don't think it would be such a bad idea.
I want some opinions on this class though, like let's say this was part of some big project that I'm working on with other people, like for a job or something, which design should i go with for this?