Archived

This topic is now archived and is closed to further replies.

BradDaBug

GUI help

Recommended Posts

BradDaBug    913
I''m workin on my GUI, and its working great, but now I have a problem. Each type of control is a class derived from a base class. All the things like LeftMouseButtonDown() and junk like that (messages, i guess you could say) are virtual. Now, how do I make it so that each button can do something different? Like, when I click on a QUIT button it quits, but a different button would do something different? Would I have some other .cpp file containing all the different functions each button would have, then have some kind of function pointer telling each button which function to call once its pressed or whatever? How would I keep track of all those function pointers?

Share this post


Link to post
Share on other sites
Colin Jeanne    1114
I''m not sure exactly what you''re trying to do, but you could use a function pointer that you pass to the button object during its creation (or some other time) and then your GUI''s user could define the button''s action.

Invader X
Invader''s Realm

Share this post


Link to post
Share on other sites
Zaei    122
I do what Invader X has already said, using a function pointer to handle messages. Just like Windows... you can pass in the ID of the event, and use a switch to handle it.

Z.

Share this post


Link to post
Share on other sites
BradDaBug    913
But wouldn''t that involve creating a message queu? I don''t wanna do that. What I have now is it calls the function, like wm_click() and wm_mousemove() and stuff like that.

Would I have to give it a function pointer for each function if I do it like that?

I''m loosly following Mason McCuskey''s "Creating a GUI using C++ and DirectX" tutorial.

Share this post


Link to post
Share on other sites
lshadow    123
Well, since all your base class methods are virtual, all you have to do is create the same methods in your derived class and they will be used instead of the base class. ie:

    
class GUI
{
virtual void LeftMouseButtonDown();
..
};

class Button : public GUI
{
void LeftMouseButtonDown();
..
};

// then in .cpp

void GUI::LeftMouseButtonDown()
{
// Base class implementation

}

void Button::LeftMouseButtonDown()
{
// Derived class implementation

}

// then later in main() or WinMain() or whatever

GUI* b;
b = new Button();
b->LeftMouseButtonDown(); // this will call the Button method



And that's all you have to do!

Edited by - lshadow on December 18, 2001 11:32:30 AM

Share this post


Link to post
Share on other sites
BradDaBug    913
Thanks for all the replies, but I''m still not getting my answer (or maybe I''m just missing the obvious.)

I know how to derive a button class from a base class, and get buttons to do diffferent stuff than progress bars and whatever by using polymorphism, but this is what I''m trying to do...

I have two buttons, Button A and Button B. Both are instances of class CButton. I want Button A to do this and this and this, and I want Button B to do that and that and that. Two totally different actions for two totally different buttons that are instances of the same class. How?

Share this post


Link to post
Share on other sites
ChrisKoz    122
Maybe if you derive yet another class for each type of button and then they can have specialized functions for what you want them to do. Or, you could also just define ALL the functions that you would need a button to perform in your CButton class (I think that is less flexible). One more thing too is that you could have an action variable like mybutton.type = CB_QUIT so that your class could decide what action to perform.

My best guess

Koz

Share this post


Link to post
Share on other sites
ChrisKoz    122
Maybe if you derive yet another class for each type of button and then they can have specialized functions for what you want them to do. Or, you could also just define ALL the functions that you would need a button to perform in your CButton class (I think that is less flexible). One more thing too is that you could have an action variable like mybutton.type = CB_QUIT so that your class could decide what action to perform.

My best guess

Koz

Share this post


Link to post
Share on other sites
smokes    122
BradDaBug: your question have already been answered by IShadow..
You said yourself that u got messages ala wm_leftmousebutton() (methods in ya baseclass) if these functions are virtual you just create a new button by deriving a new button class from you baseclass and make a Wm_leftmousebutton() method for that class.. like this:

class BaseButtonClass
{
virtual wm_leftmousebutton()
};

BaseButtonClass::wm_leftmousebutton()
{
// default handler code here
}

your button:

class MyButton : BaseButtonClass
{
virtual wm_leftmousebutton() // this method will override the one in the base class
};

MyButton::wm_leftmousebutton()
{
// this code is executed when button is clicked
}


the really smart thing is that a BaseButtonClass pointer can also refer to a MyButton object and the compiler figures out which method to call.. long live polymorphism :-)

Share this post


Link to post
Share on other sites
Zaei    122
Deriving a new class for every object that does a different thing is a pain in the arse. Suddenly, youve got 35 button classes that do almost the same thing. This is horribly inefficient. The function pointer/message queue thing is one of the cleaner methods. You could also implement the State pattern, and have a base UIState, with several members, and switch states for each action. Just derive a new state class for each thing (this still leaves you with a ton of classes that all do similar things).

Z.

Share this post


Link to post
Share on other sites
VolkerG    151
I''ve been using the method described by lshadow, ChrisKoz and smokes. It works very well and it is much clearer when each button class only contains the stuff it really needs. I can''t see any advantage of the function pointer method.

Share this post


Link to post
Share on other sites