guiPopupMenuCtrl.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. #include "gui/editor/guiPopupMenuCtrl.h"
  23. #include "gfx/gfxDrawUtil.h"
  24. #include "gfx/primBuilder.h"
  25. #include "gui/core/guiCanvas.h"
  26. GuiPopupMenuBackgroundCtrl::GuiPopupMenuBackgroundCtrl()
  27. {
  28. mMenuBarCtrl = nullptr;
  29. }
  30. void GuiPopupMenuBackgroundCtrl::onMouseDown(const GuiEvent &event)
  31. {
  32. }
  33. void GuiPopupMenuBackgroundCtrl::onMouseUp(const GuiEvent &event)
  34. {
  35. clearPopups();
  36. //Pass along the event just in case we clicked over a menu item. We don't want to eat the input for it.
  37. if (mMenuBarCtrl)
  38. mMenuBarCtrl->onMouseUp(event);
  39. close();
  40. }
  41. void GuiPopupMenuBackgroundCtrl::onMouseMove(const GuiEvent &event)
  42. {
  43. //It's possible we're trying to pan through a menubar while a popup is displayed. Pass along our event to the menubar for good measure
  44. if (mMenuBarCtrl)
  45. mMenuBarCtrl->onMouseMove(event);
  46. }
  47. void GuiPopupMenuBackgroundCtrl::onMouseDragged(const GuiEvent &event)
  48. {
  49. }
  50. void GuiPopupMenuBackgroundCtrl::close()
  51. {
  52. getRoot()->removeObject(this);
  53. mMenuBarCtrl = nullptr;
  54. }
  55. S32 GuiPopupMenuBackgroundCtrl::findPopupMenu(PopupMenu* menu)
  56. {
  57. S32 menuId = -1;
  58. for (U32 i = 0; i < mPopups.size(); i++)
  59. {
  60. if (mPopups[i]->getId() == menu->getId())
  61. return i;
  62. }
  63. return menuId;
  64. }
  65. void GuiPopupMenuBackgroundCtrl::clearPopups()
  66. {
  67. for (U32 i = 0; i < mPopups.size(); i++)
  68. {
  69. mPopups[i]->mTextList->setSelectedCell(Point2I(-1, -1));
  70. mPopups[i]->mTextList->mPopup->hidePopup();
  71. }
  72. }
  73. GuiPopupMenuTextListCtrl::GuiPopupMenuTextListCtrl()
  74. {
  75. isSubMenu = false; // Added
  76. mMenuBar = nullptr;
  77. mPopup = nullptr;
  78. mLastHighlightedMenuIdx = -1;
  79. }
  80. void GuiPopupMenuTextListCtrl::onRenderCell(Point2I offset, Point2I cell, bool selected, bool mouseOver)
  81. {
  82. //check if we're a real entry, or if it's a divider
  83. if (mPopup->mMenuItems[cell.y].isSpacer)
  84. {
  85. S32 yp = offset.y + mCellSize.y / 2;
  86. GFX->getDrawUtil()->drawLine(offset.x + 5, yp, offset.x + mCellSize.x - 5, yp, ColorI(128, 128, 128));
  87. }
  88. else
  89. {
  90. if (dStrcmp(mList[cell.y].text + 3, "-\t")) // Was: dStrcmp(mList[cell.y].text + 2, "-\t")) but has been changed to take into account the submenu flag
  91. {
  92. Parent::onRenderCell(offset, cell, selected, mouseOver);
  93. }
  94. else
  95. {
  96. S32 yp = offset.y + mCellSize.y / 2;
  97. GFX->getDrawUtil()->drawLine(offset.x, yp, offset.x + mCellSize.x, yp, ColorI(128, 128, 128));
  98. GFX->getDrawUtil()->drawLine(offset.x, yp + 1, offset.x + mCellSize.x, yp + 1, ColorI(255, 255, 255));
  99. }
  100. }
  101. // now see if there's a bitmap...
  102. U8 idx = mList[cell.y].text[0];
  103. if (idx != 1)
  104. {
  105. // there's a bitmap...
  106. U32 index = U32(idx - 2) * 3;
  107. if (!mList[cell.y].active)
  108. index += 2;
  109. else if (selected || mouseOver)
  110. index++;
  111. if (mProfile->mBitmapArrayRects.size() > index)
  112. {
  113. RectI rect = mProfile->mBitmapArrayRects[index];
  114. Point2I off = maxBitmapSize - rect.extent;
  115. off /= 2;
  116. GFX->getDrawUtil()->clearBitmapModulation();
  117. GFX->getDrawUtil()->drawBitmapSR(mProfile->mTextureObject, offset + off, rect);
  118. }
  119. }
  120. // Check if this is a submenu
  121. idx = mList[cell.y].text[1];
  122. if (idx != 1)
  123. {
  124. // This is a submenu, so draw an arrow
  125. S32 left = offset.x + mCellSize.x - 12;
  126. S32 right = left + 8;
  127. S32 top = mCellSize.y / 2 + offset.y - 4;
  128. S32 bottom = top + 8;
  129. S32 middle = top + 4;
  130. PrimBuild::begin(GFXTriangleList, 3);
  131. if (selected || mouseOver)
  132. PrimBuild::color(mProfile->mFontColorHL);
  133. else
  134. PrimBuild::color(mProfile->mFontColor);
  135. PrimBuild::vertex2i(left, top);
  136. PrimBuild::vertex2i(right, middle);
  137. PrimBuild::vertex2i(left, bottom);
  138. PrimBuild::end();
  139. }
  140. }
  141. bool GuiPopupMenuTextListCtrl::onKeyDown(const GuiEvent &event)
  142. {
  143. //if the control is a dead end, don't process the input:
  144. if (!mVisible || !mActive || !mAwake)
  145. return false;
  146. //see if the key down is a <return> or not
  147. if (event.modifier == 0)
  148. {
  149. if (event.keyCode == KEY_RETURN)
  150. {
  151. mBackground->close();
  152. return true;
  153. }
  154. else if (event.keyCode == KEY_ESCAPE)
  155. {
  156. mSelectedCell.set(-1, -1);
  157. mBackground->close();
  158. return true;
  159. }
  160. }
  161. //otherwise, pass the event to it's parent
  162. return Parent::onKeyDown(event);
  163. }
  164. void GuiPopupMenuTextListCtrl::onMouseDown(const GuiEvent &event)
  165. {
  166. Parent::onMouseDown(event);
  167. }
  168. void GuiPopupMenuTextListCtrl::onMouseUp(const GuiEvent &event)
  169. {
  170. Parent::onMouseUp(event);
  171. S32 selectionIndex = getSelectedCell().y;
  172. if (selectionIndex != -1)
  173. {
  174. MenuItem *item = &mPopup->mMenuItems[selectionIndex];
  175. if (item)
  176. {
  177. if (item->enabled)
  178. dAtob(Con::executef(mPopup, "onSelectItem", Con::getIntArg(getSelectedCell().y), item->text.isNotEmpty() ? item->text : ""));
  179. }
  180. }
  181. mSelectedCell.set(-1, -1);
  182. mBackground->close();
  183. }
  184. void GuiPopupMenuTextListCtrl::onCellHighlighted(Point2I cell)
  185. {
  186. // If this text list control is part of a submenu, then don't worry about
  187. // passing this along
  188. if (!isSubMenu)
  189. {
  190. RectI globalbounds(getBounds());
  191. Point2I globalpoint = localToGlobalCoord(globalbounds.point);
  192. globalbounds.point = globalpoint;
  193. }
  194. S32 selectionIndex = cell.y;
  195. if (selectionIndex != -1 && mLastHighlightedMenuIdx != selectionIndex)
  196. {
  197. mLastHighlightedMenuIdx = selectionIndex;
  198. mPopup->hidePopupSubmenus();
  199. }
  200. if (selectionIndex != -1)
  201. {
  202. MenuItem *list = &mPopup->mMenuItems[selectionIndex];
  203. if (list->isSubmenu && list->subMenu != nullptr)
  204. {
  205. list->subMenu->showPopup(getRoot(), getPosition().x + mCellSize.x, getPosition().y + (selectionIndex * mCellSize.y));
  206. }
  207. }
  208. }