Sign in to follow this  
pauliuw

How does Gui buttons work?

Recommended Posts

Hi, im programming in c++ SDL. Im creating my own gui and currently stuck on buttons. I don't understand how to do button actions( when you click button it have to do something ). Do i have to make my own script or at button class constructor add a function argument( ex. Button( void ( *function )( void ) ), or there is another way?

Share this post


Link to post
Share on other sites
This is often a can of worms. There are lots of methods of doing it, and they range from the simple to the horrendously complex.

Since you are a beginner, I recommend you use the "listener" pattern. Each button has a list of "listener" objects. Like this:


class ButtonListener
{
public:
virtual ~ButtonListener(){}
virtual void onButtonClick(Button * b, int mouse_button, float x, float y){}
};

class LolButtonListener : public ButtonListener
{
public:
virtual void onButtonClick(Button * b, int mouse_button, float x, float y)
{
std::cout<<"lol"<<std::endl;
}
};
LolButtonListener l;
myButton->addListener(&l);




When the button detects that it has been clicked, it loops through all its listeners and sends listener->hearClick(this, which_button, clickXPos, clickYPos);

Although I dont want to start the usual flame war, I would like to point out that c++ is rather difficult. I hope you are prepared for years of fun and pain.

Share this post


Link to post
Share on other sites
Wow, GUI programming has its own flamewar too?
I'm right in the middle of windowed GUI programming, but in C, and totally from starch, without any readings etc, so I won't dare to give advices.
(But check out the GUI in the signature, widgets have a function pointer, just like it's mentioned in the OP)

Share this post


Link to post
Share on other sites
My solution for Gui that has worked well for me and proven viable

i have two base objects one is my gui_interface where i post all my events to, generate new controls etc..., the other is the gui_object

gui_interface has a list of gui_object, and event trigger functions (on_click, on_keypress, on_move) also handles timebased and contextual information like which object has focus which object is dragging begining location of an object being dragged, etc...

gui_object has a virtual function for each event and a function pointer to for each event type, type as well as a list of child gui_objects for (windows, listboxs, controls, ...) a pointer to the objects parent if it has one, and a list of properties that describe the object, and a virtual function for drawing the object so each object knows how to draw it self

the function pointer lets me dynamically change what a its behavior is

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this