Need advice for building a window/button class.

Started by
0 comments, last by small_duck 19 years, 2 months ago
I am in the process of building a window and button class. Should I just make one button class and have the RightClick() and LeftClick() process a function pointer data member, or should I have all of my buttons I plan to use derived off of the basic button class and use virtual functions? I’d appreciate any advice on designing these classes.
-----------------------------Download my real time 3D RPG.
Advertisement
Well, I assume you're trying to build a GUI system yourself, and that you need a button class.

I would not go for any of the solutions you're giving. The first one is a bit messy, and you'll be quickly lost in your function calls. The second may look nicer, but you would need to create as many classes as you have buttons, which is too much.

So, just copy what exists and work well!

Windows uses an event system. This means that when you click on the button, an event is generated, which is added to an event queue. These events are then dispatched, and can call functions.

If your buttons are all in the same screen (like a configuration screen, for example), you can have a general event queue at the screen level. When you create a button, you give them an ID, and a reference to the event queue. When the user clicks on the button, the button class adds the event "clicked" to the queue. Your screen class will have a method update, that you call from your general main loop. You extract the events, and make actions depending on it, for example changing a number on your Configuration screen.

The nice thing about events is that you can very easely add controls: just create a new control class, add the corresponding event codes, and process them into your main screen.

This topic is closed to new replies.

Advertisement