also, i think many return types for the onClick() function would be possible. it all depends on what a specific button should do. so i'm really curious about this.
thanks in advance :)
you mean i should create many derived classes, each one with a specific function? wouldn't that make me have more classes than objects in most cases (except when a gui uses all possible buttons)?
also, i did read something about std::function, but didn't really understood...
you're right, i've never thought about that :P
i tried to use std::function anyway, just out of curiosity, but wasn't able to. is it possible to make an object that can change a string and another that can change an int if both are from the same class?
Shouldn't your class have a bounding box of the button (left, top, width, height)? Otherwise that is going to be very hard to click on that one tiny pixel.
They also let you get the position of them. So why have part of it in the class? Anyways the actual image(sprite) of the button isn't even in the class so I am not sure how they would get the width/height of it. Well, I guess in this case it is text but still where is the actual "label" object to get the dimensions from? Is it created and stored in the constructor?
For a basic button class I would do something like
class Button
{
private:
struct Rect
{
float x;
float y;
float width;
float height;
} rect;
std::string label;
bool isHovering(); //if it is hovering it can't enter since it already has entered,
//if it is not hovering it can't exit since it is not already hovering
bool wasPressed(); //if mouse was down pressed over the button then
//it can't be pressed and released over the button
sprite button; //the thing to be drawn
public:
Button();
~Button();
void update(); //update and draw the button
bool onMouseDown(); //mouse was clicked over button
bool onMouseUp(); //mouse was released over button
bool onMouseDownAndUp(); //mouse was clicked and released over button
bool onMouseEnter(); //mouse entered the button
bool onMouseOver(); //mouse is hovering over button
bool onMouseExit(); //mouse left the button
};
Though, most graphics libraries already have a rect class.
I suppose you could add the "add action" or w.e delegate kind of thing to the buttons aswell.
the real class does have a bounding box. it is made in SFML, with a sf::Sprite, a texture reference, etc :P
i removed most of things just to make the class more readable in this forum.
anyway, i'll dig up a little more on functors, which seemed interesting . thanks everybody for the help :)