guiMenuBar.h

Engine/source/gui/editor/guiMenuBar.h

More...

Classes:

Detailed Description

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#ifndef _GUIMENUBAR_H_
 25#define _GUIMENUBAR_H_
 26
 27#ifndef _GUITEXTLISTCTRL_H_
 28#include "gui/controls/guiTextListCtrl.h"
 29#endif
 30#ifndef _GUITICKCTRL_H_
 31#include "gui/shiny/guiTickCtrl.h"
 32#endif
 33
 34class GuiMenuBar;
 35class GuiMenuTextListCtrl;
 36class WindowInputGenerator;
 37
 38class GuiMenuBackgroundCtrl : public GuiControl
 39{
 40   typedef GuiControl Parent;
 41
 42protected:
 43   GuiMenuBar *mMenuBarCtrl;
 44   GuiMenuTextListCtrl *mTextList; 
 45public:
 46   GuiMenuBackgroundCtrl(GuiMenuBar *ctrl, GuiMenuTextListCtrl* textList);
 47   void onMouseDown(const GuiEvent &event);
 48   void onMouseMove(const GuiEvent &event);
 49   void onMouseDragged(const GuiEvent &event);
 50};
 51
 52class GuiSubmenuBackgroundCtrl : public GuiMenuBackgroundCtrl
 53{
 54   typedef GuiMenuBackgroundCtrl Parent;
 55
 56public:
 57   GuiSubmenuBackgroundCtrl(GuiMenuBar *ctrl, GuiMenuTextListCtrl* textList);
 58   bool pointInControl(const Point2I & parentCoordPoint);
 59   void onMouseDown(const GuiEvent &event);
 60};
 61
 62//------------------------------------------------------------------------------
 63
 64class GuiMenuTextListCtrl : public GuiTextListCtrl
 65{
 66   private:
 67      typedef GuiTextListCtrl Parent;
 68
 69   protected:
 70      GuiMenuBar *mMenuBarCtrl;
 71
 72   public:
 73     bool isSubMenu; //  Indicates that this text list is in a submenu
 74
 75      GuiMenuTextListCtrl(); // for inheritance
 76      GuiMenuTextListCtrl(GuiMenuBar *ctrl);
 77
 78      // GuiControl overloads:
 79      bool onKeyDown(const GuiEvent &event);
 80      void onMouseDown(const GuiEvent &event);
 81      void onMouseUp(const GuiEvent &event);
 82      void onRenderCell(Point2I offset, Point2I cell, bool selected, bool mouseOver);
 83
 84      virtual void onCellHighlighted(Point2I cell); //  Added
 85};
 86
 87//------------------------------------------------------------------------------
 88
 89class GuiMenuBar : public GuiTickCtrl //  Was: GuiControl
 90{
 91   typedef GuiTickCtrl Parent; //  Was: GuiControl Parent;
 92public:
 93
 94   struct Menu;
 95
 96   struct MenuItem   // an individual item in a pull-down menu
 97   {
 98      char *text;    // the text of the menu item
 99      U32 id;        // a script-assigned identifier
100      char *accelerator; // the keyboard accelerator shortcut for the menu item
101      U32 acceleratorIndex; // index of this accelerator
102      bool enabled;        // true if the menu item is selectable
103      bool visible;        // true if the menu item is visible
104      S32 bitmapIndex;     // index of the bitmap in the bitmap array
105      S32 checkGroup;      // the group index of the item visa vi check marks - 
106                           // only one item in the group can be checked.
107      MenuItem *nextMenuItem; // next menu item in the linked list
108
109      bool isSubmenu;            //  This menu item has a submenu that will be displayed
110
111      Menu* submenuParentMenu; //  For a submenu, this is the parent menu
112      Menu* submenu;
113      String cmd;
114   };
115
116   struct Menu
117   {
118      char *text;
119      U32 id;
120      RectI bounds;
121      bool visible;
122
123      S32 bitmapIndex;     // Index of the bitmap in the bitmap array (-1 = no bitmap)
124      bool drawBitmapOnly; // Draw only the bitmap and not the text
125      bool drawBorder;     // Should a border be drawn around this menu (usually if we only have a bitmap, we don't want a border)
126
127      Menu *nextMenu;
128      MenuItem *firstMenuItem;
129   };
130   
131   GuiMenuBackgroundCtrl *mBackground;
132   GuiMenuTextListCtrl *mTextList;
133   
134   GuiSubmenuBackgroundCtrl *mSubmenuBackground; //  Background for a submenu
135   GuiMenuTextListCtrl *mSubmenuTextList;     //  Text list for a submenu
136
137   Vector<Menu*> mMenuList;
138   Menu *mouseDownMenu;
139   Menu *mouseOverMenu;
140
141   MenuItem* mouseDownSubmenu; //  Stores the menu item that is a submenu that has been selected
142   MenuItem* mouseOverSubmenu; //  Stores the menu item that is a submenu that has been highlighted
143
144   bool menuBarDirty;
145   U32 mCurAcceleratorIndex;
146   Point2I maxBitmapSize;
147
148   S32 mCheckmarkBitmapIndex; // Index in the bitmap array to use for the check mark image
149
150   S32 mPadding;
151   S32 mHorizontalMargin; // Left and right margin around the text of each menu
152   S32 mVerticalMargin;   // Top and bottom margin around the text of each menu
153   S32 mBitmapMargin;     // Margin between a menu's bitmap and text
154
155   //  Used to keep track of the amount of ticks that the mouse is hovering
156   // over a menu.
157   S32 mMouseOverCounter;
158   bool mCountMouseOver;
159   S32 mMouseHoverAmount;
160   
161   GuiMenuBar();
162   bool onWake();
163   void onSleep();
164
165   // internal menu handling functions
166   // these are used by the script manipulation functions to add/remove/change menu items
167   static Menu* sCreateMenu(const char *menuText, U32 menuId);
168   void addMenu(Menu *menu, S32 pos = -1);
169   void addMenu(const char *menuText, U32 menuId);
170   Menu *findMenu(const char *menu);  // takes either a menu text or a string id
171   static MenuItem *findMenuItem(Menu *menu, const char *menuItem); // takes either a menu text or a string id
172   void removeMenu(Menu *menu);
173   static void removeMenuItem(Menu *menu, MenuItem *menuItem);
174   static MenuItem* addMenuItem(Menu *menu, const char *text, U32 id, const char *accelerator, S32 checkGroup, const char *cmd);
175   static MenuItem* addMenuItem(Menu *menu, MenuItem *menuItem);
176   static void clearMenuItems(Menu *menu);
177   void clearMenus();
178
179   void attachToMenuBar(Menu* menu, S32 pos = -1);
180   void removeFromMenuBar(Menu* menu);
181
182   //  Methods to deal with submenus
183   static MenuItem* findSubmenuItem(Menu *menu, const char *menuItem, const char *submenuItem);
184   static MenuItem* findSubmenuItem(MenuItem *menuItem, const char *submenuItem);
185   static void addSubmenuItem(Menu *menu, MenuItem *submenu, const char *text, U32 id, const char *accelerator, S32 checkGroup);
186   static void addSubmenuItem(Menu *menu, MenuItem *submenu, MenuItem *newMenuItem );
187   static void removeSubmenuItem(MenuItem *menuItem, MenuItem *submenuItem);
188   static void clearSubmenuItems(MenuItem *menuitem);
189   void onSubmenuAction(S32 selectionIndex, const RectI& bounds, Point2I cellSize);
190   void closeSubmenu();
191   void checkSubmenuMouseMove(const GuiEvent &event);
192   MenuItem *findHitMenuItem(Point2I mousePoint);
193
194   void highlightedMenuItem(S32 selectionIndex, const RectI& bounds, Point2I cellSize); //  Called whenever a menu item is highlighted by the mouse
195
196   // display/mouse functions
197
198   Menu *findHitMenu(Point2I mousePoint);
199
200   //  Called when the GUI theme changes and a bitmap arrary may need updating
201  // void onThemeChange();
202
203   void onPreRender();
204   void onRender(Point2I offset, const RectI &updateRect);
205
206   void checkMenuMouseMove(const GuiEvent &event);
207   void onMouseMove(const GuiEvent &event);
208   void onMouseLeave(const GuiEvent &event);
209   void onMouseDown(const GuiEvent &event);
210   void onMouseDragged(const GuiEvent &event);
211   void onMouseUp(const GuiEvent &event);
212   
213   void onAction();
214   void closeMenu();
215   void buildWindowAcceleratorMap( WindowInputGenerator &inputGenerator );
216   void removeWindowAcceleratorMap( WindowInputGenerator &inputGenerator );
217   void acceleratorKeyPress(U32 index);
218
219   virtual void menuItemSelected(Menu *menu, MenuItem *item);
220
221   //  Added to support 'ticks'
222   void processTick();
223
224   static void initPersistFields();
225
226   DECLARE_CONOBJECT(GuiMenuBar);
227   DECLARE_CALLBACK( void, onMouseInMenu, ( bool hasLeftMenu ));
228   DECLARE_CALLBACK( void, onMenuSelect, ( S32 menuId, const char* menuText ));
229   DECLARE_CALLBACK( void, onMenuItemSelect, ( S32 menuId, const char* menuText, S32 menuItemId, const char* menuItemText  ));
230   DECLARE_CALLBACK( void, onSubmenuSelect, ( S32 submenuId, const char* submenuText ));
231};
232
233#endif
234