This is a screenshot of the first prototype i’ve done for a menu system. I’ve been thinking about ways of implementing a decent menu system in TrueVision itself (Graphically drawn by TrueVision) rather then Windows Forms. I initially thought I was going to perform Collision detection on rectangles with set bounds but when I started coding it wasn’t as easy as I initially thought. So, I went with something else.Beolow you can see an example of how the Menu class renders itself. Rather then checking object collisions I get the absolute mouse co-ordinates and pass that in, in a render cycle for a Button to compute whether it is being hovered over and clicked.

//Menu class
public void Render() {
  _screenImmediate.ACTION_Begin2D();
  _screenImmediate.DRAW_FilledBox(_x1, _y1, _x2, _y2,
     Global.Globals.RGBA(0.2f, 0.2f, 0.2f, 0.3f),
     Global.Globals.RGBA(0.2f, 0.2f, 0.2f, 0.3f),
     Global.Globals.RGBA(0.2f, 0.2f, 0.2f, 0.3f),
     Global.Globals.RGBA(0.2f, 0.2f, 0.2f, 0.3f));
  _screenImmediate.ACTION_End2D();
 
  TVInputEngine input = Global.Input.InputEngine;
  int mx = 0, my = 0;
  short b1 = 0, b2 = 0, b3 = 0;
  input.GetAbsMouseState(ref mx, ref my, ref b1,ref b2, ref b3);
 
  foreach (Button button in _buttonList) {
      button.Render(mx, my, b1);
  }
  foreach (Menu aMenu in _subMenu) {
      aMenu.Render(); // render subMenu
  }
}

The Menu class can accept other Menu’s as sub menus. So This calls the render method of all the buttons the Menu has, then it calls render on subMenus. I’ve basically found the Menu system of attaching submenus and what not to be a bit clunky, so I’ll come up with another design. But I think the deviation from collision checking to absolute mouse co-ordinates and having a button calc this itself will work out ok.

//Button Class
public void Render(int mx, int my, short b1) {
// Left box.
_context.DRAW_FilledBox(_x1, _y1, _x2, _y2,
  Global.Globals.RGBA(1f, 0.0f, 0.0f, 0.7f),
  Global.Globals.RGBA(0.8f, 0.0f, 0.0f, 0.7f),
  Global.Globals.RGBA(0.2f, 0.0f, 0.0f, 0.7f),
  Global.Globals.RGBA(0.8f, 0.0f, 0.0f, 0.7f));
  _text.NormalFont_DrawText(_name, _x1+5, _y2-20,
      Global.Globals.RGBA(0f, 0f, 0f, 0.9f),"default");
  // Mouse is hovering over me
  if (_x1 = mx&& _y1 = my) {
    if (b1 != 0 && _menu != null) {
        _menu.Clear();
        foreach (Button button in _buttons)
           _menu.AddButton2(button);
    }
    Debug.PrintLine("Hovering over: " + _name);
  }
}

You can see the button checks the absolute mouse co-ordinates that were passed in when the last frame was rendered. So, during that render cycle if it is being hovered over and is clicked, it does a simple button update of the subMenu attached to the menu (to which this button is attached). Ultimately, the button would have a proper action method that gets executed. But for now, this is all it is doing.The rest of the code was a bit ugly, so i’ll explore other ideas.Tech Tags: