Jump to content
  • Advertisement
Sign in to follow this  
soconne

GUI controls & adding behaviors such as scrolling

This topic is 3752 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'm currently designing a GUI library, that I've rewritten from scratch now 3 times, and I'm currently on the 3rd attempt trying to figure out what the best way is to implement new Control behaviors. For instance, allowing a Control to be scrollable. Meaning, when the inner contents of a Control go outside of its boundaries, scrollbars appear and allow the contents to be scrolled. My first attempt was the following.
class Control {
public:
    virtual void onMouseDown();
    virtual void onMouseMove();
    ect...
};


class Scrollable {
public:
    Scrollable(Control* control);

    virtual void onMouseDown();
    virtual void onMouseMove();
    ect...

private:
    Control* m_control;
};

class Panel : public Control, Scrollable {
public:
    Panel() : Scrollable(this) { }

    void onMouseDown() {
        // do stuff

        Scrollable::onMouseDown();
    }

    void onMouseMove() {
        // do stuff
 
        Scrollable::onMouseMove();
    }
};


The thing I liked about this approach was that Scrollable wasn't a Control, it was a class that added functionality to an existing Control class. But the problem is that the class inheriting from Scrollable had to remember to call the Scrollable events, such as Scrollable::onMouseDown. My 2nd attempt, right now, is to use the decorator pattern.
class Control {
public:
    virtual void onMouseDown();
    virtual void onMouseMove();
    ect...
};

class Panel : public Control {
public:`
    virtual void onMouseDown();
    virtual void onMouseMove();
    ect...
};

class Scrollable : public Control {
public:
    Scrollable(Control* control);

    virtual void onMouseDown() {
        // handle Scrollable code

        if (event not consumed by Scrollable)
            this->m_control->onMouseDown();
    }

    virtual void onMouseMove() {
        // same as onMouseDown
    }

private:
    Control* m_control;
};


Now this is somewhat better since the Panel class has no idea it is also scrollable and does not have to add any special code to handle it. But on the flipside, creating a scrollable panel is kind of ugly, at least in my opinion. Panel* panel = new Panel(); this.AddControl(new Scrollable(panel)); Does anybody have any suggestions on which is the preferred way to implement behaviors such as this?

Share this post


Link to post
Share on other sites
Advertisement
Java's Swing library prefers things using the decorator pattern, and Windows Forms is made to work that way. I don't know about most others, but it is a decent and well-known pattern.

Just be sure to deal with resizability very early on in your design; Swing may have it down pat, but they haven't bothered to document it very well, yet. You'll find things such as how excrutiatingly difficult it is to create a control of automatic size in one dimension, but fixed size in another.

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!