guiMenuBar.h 7.9 KB

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