Sign in to follow this  

tool mode design

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

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