Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your Model <code>class menu</code> needs a <code>add_view(menuview *v)</code> and <code>update()</code> method.</p> <p>Then you can delegate the update of your widget to the derived View (<code>menu_renderer</code> or a <code>cli_menu_renderer</code>). </p> <p>The Controller needs to know the Model (as member), when the Controller runs (or executes a Command) and has to update the Model with a Setter (like <code>m_menu_model-&gt;set_selected(item, state)</code>) and the Model calls <code>update()</code> on Setters. </p> <p>Your Controller <code>menu_controller</code> has a update method, there you could also ask for Input, like <code>if (menuview-&gt;toggle_select()) m_menu_model-&gt;toggle_selected();</code> (which all menuviews have to implement) and invoke the setter, but thats a inflexible coupling of View and Controller (you could check MVC with the Command Pattern for a more advanced combination). </p> <p>For the Position you can set member vars like int m_x, m_y, m_w, m_h. But these members are specific to a View with GUI, so only the derived View needs them.</p> <p>Then you could use these values to compare against mouse Positions and use a MouseOver Detection Method like this: </p> <pre><code>// View menu_item bool menu_item::over() { if (::mouse_x &gt; m_x &amp;&amp; ::mouse_x &lt; m_x + m_w &amp;&amp; ::mouse_y &gt; m_y &amp;&amp; ::mouse_y &lt; m_y + m_h) { return true; } return false; } // update on gui menu item bool menu_item::update() { if (over()) { m_over = true; } else { m_over = false; } // onclick for the idea if ((::mouse_b &amp; 1) &amp;&amp; m_over) { // here you could invoke a callback or fire event m_selected = 1; } else { m_selected = 0; } return m_selected; } // update the command line interface menu item bool cli_menu_item::update() { if ((::enterKeyPressed &amp; 1) &amp;&amp; m_selected) { // here you could invoke a callback or fire event m_selected = 1; } else { m_selected = 0; } return m_selected; } void menu_item_renderer::render() { // update widgets _item-&gt;update(); // ... } // Model void menu::add_view(menuview *v) { m_view=v; } void menu::update() { if (m_view) m_view-&gt;update(); } bool menu::set_selected(int item, int state) { m_item[index]=state; update(); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload