Skip to main content
GameDev.net gamedev.net
🔒 Locked

GUI Button - Handling Each Event

Started by DarkZlayer Apr 24, 2009 at 7:42 AM 3 replies 1.1k views
Original Post
DarkZlayer
DarkZlayer
I'm just fooling around on making a GUI (that I will probably use in games later) and I'm wondering what type of approach is "best" for my situation. I'm going to have a Window and\or Panel object which will contain a collection of buttons and textboxs. It will then cycle through the collections to see if any has been clicked and then take the appropriate action. It's easy to handle whether or not if the button has been clicked but I cannot think of a good approach to "define" what each button does without having a whole bunch of: if(button1.isClicked()){ Do Action }else if(button2.isClicked()){ Do a different action } Ect. for every button. Perhaps this is one of the better\only ways to go about it or maybe I will have to redesign the Button class. Nevertheless, any help would definitely be appreciated.
Black Knight
Black Knight
In my gui,every guielement like buttons panels text boxes etc have three boost::function members;onClicked,onPressed and onMouseOver.In the client application I bind functions to these and when an event happens I call these methods.

When the user presses the left mouse button this window message is given to the user interface and the user interface checks all elements if they are clicked.Also some gui elements can have childs for example panels.The panels also check if their childs are clicked.

for example:
panel->onLeftButtonDown(cursorPos); // will call onLeftButtonDown on all its childs and fire events if necessary
DarkZlayer
DarkZlayer
I'm sorry, I must have not made my question clear enough. I meant that, I'm not sure how to go about each event after it is clicked. I'll make a quick example:
Button button1 = new Button();Button button2 = new Button();if(button1.isClicked()){    //Turn background red}else if(button2.isClicked()){    //Turn background blue}

So, I was wondering if there is any way to hold what each button does from within the method. If I hold all the buttons for the window in an a collection like a list it'd be nice to just do something like:

for(int ndX = 0; ndX < buttonList.length; ndX++){    if(buttonList[ndX].isClicked())        buttonList[ndX].doEvent();}



I'd prefer not to have a bunch of if statements like above for each and every button\textbox. The only problem is I cannot think of any other way (maybe because it's not possible?) to go about doing it than a bunch of if else statements.
Black Knight
Black Knight
That's something like what I do.The doEvent method is the boost::function member in this case.

Here is some pseudo-code :

void initGUI(){vector<GUIElement> guiElements;ButtonPtr b1(new Button());b1->onClick = bind(&b1OnClickedMethod);guiElements.pushBack(b1);ButtonPtr b2(new Button());b2->onClick = bind(&b2OnClickedMethod);guiElements.pushBack(b2);}void onLeftButtonDown(){ for(unsigned int i=0; i<guiElements.size(); ++i) {   if(guiElements->isClicked(cursorPos))   guiElements->onClick();  }}void b1OnClickMethod(){ //change background to green}void b2OnClickMethod(){ //change background to blue}


This won't compile though you have to look into syntax [smile]
Chadwell
Chadwell
Quote:
Original post by Black Knight
In my gui,every guielement like buttons panels text boxes etc have three boost::function members;onClicked,onPressed and onMouseOver.In the client application I bind functions to these and when an event happens I call these methods.

When the user presses the left mouse button this window message is given to the user interface and the user interface checks all elements if they are clicked.Also some gui elements can have childs for example panels.The panels also check if their childs are clicked.

for example:
*** Source Snippet Removed ***


I'm in a similar situation, and while I have been searching all over, I can't seem to find a good place to show how exactly boost::function can be used in this way(This will be my first time using boost). Is there a way to use boost::function to bind a member function to a particular action? I'm basically thinking of wxWidgets and the way you are able to bind your class's member functions to specific widgets, but it uses Macro tables to do it and I'm trying to find a way to implement something similar.

Thanks for any help.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.