• 16
• 15
• 12
• 9
• 10

# GUI button clicking.

This topic is 4194 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, I am trying to make a GUI for my game. I don't know if the way im doing is is the most efficent. For example. I have made a button class. GUIButton. The button contains a Render Methold ( to draw it, called from the renderloop ) and a MouseMove(MouseEventArgs e), MouseDown(MouseEventArgs e) methold. In the form class I've 2 events.. and both look like this: void this_mouseDown(object sender, MouseEventArgs e) { gUIButton.MouseDown(e); } Are the other ways to implant this system ? ( I don't like this one :D) Thanks for (trying :'() to read this!

##### Share on other sites
I'm in the process of writing a (small) multi-platform GUI system, so that I don't have to worry about Windows to Linux conversions at uni next semester. I decided on writing a generic EventCommander pattern. It has a list of Event Handlers, which may be functions, or even member functions (bound to an instance of a class with boost::bind). What this means is that I can then derive my GUIButton from EventCommander<MouseEvent>, and have many instances of GUIButton in the code. Something like this:

void handle_A(const MouseEvent& e){}class SomethingElse{public:  void handle_B(const MouseEvent& e)  {  }};int main(){  SomethingElse se;  GUIButton g;  g.addHandler(&handle_A);  g.addHandlerMF(&se, &SomethingElse::handle_B);  for (;;)  {    // main loop  }}

And now, whenever a mouse event is sent via the GUI to that particular button (button object g), it will call both handle_A and handle_B (with the instance se), which will handle the event (first in, first served).

Your other option is to have a class like GUIButton, with lots of onClick-like virtual functions, that people then have to write overloads for. This means, however, that you have to write a whole new subclass for each different button you want. That's why I went with the above. Hope that helps.