Jump to content
  • Advertisement
Sign in to follow this  
Quat

tool mode design

This topic is 3882 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

I am working on a program that has a toolbar similar to Photoshop's. Basically, the user will select a tool, and based on the tool mode, the OnButtonDown, OnMouseMove, OnButtonUp function do different things. Right now I am using if statements (see below for pseudo code). I am finding this strategy annoying. What would be a better design? I am thinking a state machine--so I would have a State class for each tool that encapsulates the work the tool needs to do. Is this the way to go? Any other ideas?
void MyControl::OnMouseDown(MouseEventArgs^ e)
{
    switch(mode)
    {
        case A:
            ...
        case B:
            ...
        case C:
            ...
    }
}

void MyControl::OnMouseMove(MouseEventArgs^ e)
{
    switch(mode)
    {
        case A:
            ...
        case B:
            ...
        case C:
            ...
    }
}

Share this post


Link to post
Share on other sites
Advertisement
Create a common base class for all your different tools with virtual functions for everything you need, and call those.

For example:
class ToolbarBase {
virtual image GetToolbarImage();
virtual void MouseDown( MouseEventArgs^ e );
virtual void MouseUp ( MouseEventArgs^ e );
virtual void MouseMove( MouseEventArgs^ e );
...
}


Next, you have a collection of your toolbar items. When the toolbar item is selected you point to that instance as your current element. When you receive a command, you call the corresponding virtual function.

Now your control doesn't need to know anything about what tool is active, and just forwards the message to the appropriate control handler.

So it becomes:
void MyControl::OnMouseDown(MouseEventArgs^ e)
{
if( mCurrentTool )
{
mCurrentTool->MouseDown(e);
}
else
{
/* We don't have a currently selected tool. Now what? */
}
}

Then you can keep all your logic inside the individual tools, where it belongs.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!