Sign in to follow this  

GUI Callbacks

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

So I'm at the point where my programs are complex enough that a GUI system seems the logical next step. Read the articles, hacked together something towards my liking, but have come to an issue. How to get things to happen? My thoughts so far are that each control would hold member variables that point to certain functions needed by the control that could be created on a per project basis:


class button {
friend class GUI;
public:
void OnClick() { (*m_OnClick_callback)(); }
// ... OnMouseOver, OnMouseOut, etc...
private:
void SetOnClick(void(*pfunc)()) { m_OnClick_callback = pfunc; }
void (*m_OnClick_callback)();

}



At first I could not see a way to have the button handle things like setting it's image in the onclick_callback but now realize that this functionality would be part of all buttons so could reside in OnClick() and a few member variables(m_hovertexture, m_downtexture).

Unfortunately this oversight makes me unsure if I have a clear enough picture in my mind to really proceed full tilt with it.

Have I got the right idea here?
Is there a better way?

Share this post


Link to post
Share on other sites
I find the following concept a better idea. I too wrote GUI code, and found signals and slots to be the best method.

Signals and slots makes it possible to connect multiple slots (functions called) to a signal (event ID that gets triggered). Each GUI widget contains a Signals class which holds a std::map of Signal instances. Whenever one is getting used, it will thus be allocated.

Each Signal has a std::vector of Slot instances, which is a base class for few specific slots: functions, member functions, const member functions and other signals (I added this myself, very convenient, it's not part of the signal-slot concept).

Each Slot has the parameters it needs to pass to the function / member function / signal stored as (private) data members. I allow up to 9 parameters (and only void for return type), so each slot kinds I described above has 9 classes (I do this with macro trickery, it creates classes for each version with N number of arguments).

Then I can send a signal (I call emit() on a Signal instance), it will call each of it's slots and make them call the function / member function / signal they hold. Emit does not take parameters, as the parameters are already saved in the slots.

Some implementations allow to pass parameters to emit(). This is (practically) impossible if you allow slots to point to other signals.

Example:
void f(const std::string &string)
{
std::cout << string << '\n';
}

button.signals[Events::OnClick].connect(&f, "some text");
button.signals[Events::OnClick].emit(); // prints "some text"

Share this post


Link to post
Share on other sites
I've heard of signals and slots, via boost, but never really looked into it. In fact i'm still working on removing all the char* and printf stuffs from my "c++" code. I've never used map before, but would love some more practice with stdlib.

Share this post


Link to post
Share on other sites
Quote:
Original post by Burnt_Fyr
I've heard of signals and slots, via boost, but never really looked into it. In fact i'm still working on removing all the char* and printf stuffs from my "c++" code. I've never used map before, but would love some more practice with stdlib.


If I may recommend, don't use the Boost implementation, unless performance is of no concern but portability is. SigC++ and some others are about a magnitude more efficient. My own implementation was about as fast as it could get (yeah, as fast as FastDelegate, on my particular compiler + environment).

If you're interested in my code (ie. exactly how I described it above), I will sure give it to you. You can tweak it etc. I might even consider releasing it as a separate library now that I think of it.

You can of course also go with a better maintained / tested library. I doubt I will use mine much, as I am worked to use D as my main language now.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

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