<p>I've made many different seperate parts of a GUI system for the Nintendo DS, like buttons and textboxes and select boxes, but I need a way of containing these classes in one Gui class, so that I can draw everything to the screen all at once, and check all the buttons at once to check if any are being pressed. My question is what is the best way organize all the classes (such as buttons and textboxes) into one GUI class?</p>

<p>Here's one way I thought of but it doesn't seem right:</p>

<p><strong>Edit:</strong> I'm using C++.</p>

<pre><code>class Gui {
    public:
        void update_all();
        void draw_all() const;
        int add_button(Button *button); // Returns button id
        void remove_button(int button_id);
    private:
        Button *buttons[10];
        int num_buttons;
}
</code></pre>

<p>This code has a few problems, but I just wanted to give you an idea of what I want.</p>

[{'answer_id': 7506, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>One useful strategy to keep in mind might be the <a href="http://en.wikipedia.org/wiki/Composite_pattern" rel="nofollow noreferrer">composite pattern</a>. At a low level, it might allow you to treat all GUI objects (and collections of objects) more easily once built. But I have no idea what\'s involved in GUI framework design, so one place to find general inspiration is in the source code of an existing project. <a href="http://www.wxwidgets.org/downloads/" rel="nofollow noreferrer">WxWidgets</a> is a cross-platform GUI framework with source available. Good luck with your project!</p>\n'}, {'answer_id': 7511, 'author': 'Bernard', 'author_id': 61, 'author_profile': 'https://Stackoverflow.com/users/61', 'pm_score': 0, 'selected': False, 'text': '<p>I think looking at the way other GUI toolkits have done it would be an excellent place to start. For C++ examples, I hear lots of good things about <a href="http://trolltech.com/products/qt/" rel="nofollow noreferrer">Qt</a>. I haven\'t used it personally though. And of course WxWidgets as Nick mentioned.</p>\n'}, {'answer_id': 13384, 'author': 'thing2k', 'author_id': 3180, 'author_profile': 'https://Stackoverflow.com/users/3180', 'pm_score': 3, 'selected': True, 'text': '<p>This question is very similar to one I was going to post, only mine is for Sony PSP programming.</p>\n\n<p>I\'ve been toying with something for a while, I\'ve consulted some books and <a href="http://www.3dbuzz.com/xcart/product.php?productid=30&amp;cat=12&amp;page=1" rel="nofollow noreferrer">VTMs</a>, and so far this is a rough idea of a simple ui systems.</p>\n\n<pre><code>class uiElement()\n{\n    ...\n    virtual void Update() = 0;\n    virtual void Draw() = 0;\n    ...\n}\n\nclass uiButton() public : uiElement\n{\n    ...\n    virtual void Update();\n    virtual void Draw();\n    ...\n}\n\nclass uiTextbox() public : uiElement\n{\n    ...\n    virtual void Update();\n    virtual void Draw();\n    ...\n}\n\n... // Other ui Elements\n\nclass uiWindow()\n{\n    ...\n    void Update();\n    void Draw();\n\n    void AddElement(uiElement *Element);\n    void RemoveElement(uiElement *Element);\n\n    std::list &lt;uiElement*&gt; Elements;\n\n    ...\n}\n\nvoid uiWindow::Update()\n{\n    ...\n    for (list &lt;uiElement*&gt;::iterator it = Elements.begin(); it != Elements.end(); it++ )\n        it-&gt;Update();\n    ...\n}\n\nvoid uiWindow::Draw()\n{\n    ...\n    for (list &lt;uiElement*&gt;::iterator it = Elements.begin(); it != Elements.end(); it++ )\n        it-&gt;Draw();\n    ...\n}\n</code></pre>\n\n<p>The princple is to create a window and attact ui Elements to it, and call the draw and update methods from the respective main functions.</p>\n\n<p>I don\'t have anything working yet, as I have issues with drawing code. With different APIs on the PC and PSP, I\'m looking at some wrapper code for OpenGL and psp gu. </p>\n\n<p>Hope this helps.</p>\n\n<p>thing2k</p>\n'}, {'answer_id': 78385, 'author': 'KPexEA', 'author_id': 13676, 'author_profile': 'https://Stackoverflow.com/users/13676', 'pm_score': 0, 'selected': False, 'text': '<p>I\'ve written a very simple GUI just like you propose. I have it running on Windows, Linux and Macintosh. It should port relatively easily to any system like the PSP or DS too.</p>\n\n<p>It\'s open-source, LGPL and is here:</p>\n\n<p><a href="http://code.google.com/p/kgui/" rel="nofollow noreferrer">http://code.google.com/p/kgui/</a></p>\n'}, {'answer_id': 192409, 'author': 'Ant', 'author_id': 2289, 'author_profile': 'https://Stackoverflow.com/users/2289', 'pm_score': 2, 'selected': False, 'text': '<p>For anyone who\'s interested, here\'s my open source, BSD-licenced GUI toolkit for the DS:</p>\n\n<p><a href="http://www.sourceforge.net/projects/woopsi" rel="nofollow noreferrer">http://www.sourceforge.net/projects/woopsi</a></p>\n\n<p>thing2k\'s answer is pretty good, but I\'d seriously recommend having code to contain child UI elements in the base uiElement class.  This is the pattern I\'ve followed in Woopsi.</p>\n\n<p>If you <em>don\'t</em> support this in the base class, you\'ll run into major problems when you try to implement anything more complex than a textbox and a button.  For example:</p>\n\n<ul>\n<li>Tab bars can be modelled as multiple buttons grouped together into a single parent UI element that enforces mutual exclusiveness of selection;</li>\n<li>Radio button groups (ditto);</li>\n<li>Scroll bars can be represented as a slider/gutter element and up/down buttons;</li>\n<li>Scrolling lists can be represented as a container and multiple option UI elements.</li>\n</ul>\n\n<p>Also, it\'s worth remembering that the DS has a 66MHz CPU and 4MB of RAM, which is used both to store your program and execute it (DS ROMs are loaded into RAM before they are run).  You should really be treating it as an embedded system, which means the STL is out.  I removed the STL from Woopsi and managed to save 0.5MB.  Not a lot by desktop standards, but that\'s 1/8th of the DS\' total available memory consumed by STL junk.</p>\n\n<p>I\'ve detailed the entire process of writing the UI on my blog:</p>\n\n<p><a href="http://ant.simianzombie.com/blog" rel="nofollow noreferrer">http://ant.simianzombie.com/blog</a></p>\n\n<p>It includes descriptions of the two algorithms I came up with for redrawing the screen, which is the trickiest part of creating a GUI (one just splits rectangles up and remembers visible regions; the other uses BSP trees, which is much more efficient and easier to understand), tips for optimisation, etc.</p>\n'}]
['https://Stackoverflow.com/questions/7489', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/813/']