guiPopupMenuCtrl.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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(GuiPopupMenuTextListCtrl *textList)
  27. {
  28. mTextList = textList;
  29. mTextList->mBackground = this;
  30. }
  31. void GuiPopupMenuBackgroundCtrl::onMouseDown(const GuiEvent &event)
  32. {
  33. mTextList->setSelectedCell(Point2I(-1, -1));
  34. close();
  35. }
  36. void GuiPopupMenuBackgroundCtrl::onMouseMove(const GuiEvent &event)
  37. {
  38. }
  39. void GuiPopupMenuBackgroundCtrl::onMouseDragged(const GuiEvent &event)
  40. {
  41. }
  42. void GuiPopupMenuBackgroundCtrl::close()
  43. {
  44. getRoot()->removeObject(this);
  45. }
  46. GuiPopupMenuTextListCtrl::GuiPopupMenuTextListCtrl()
  47. {
  48. isSubMenu = false; // Added
  49. mMenu = NULL;
  50. mMenuBar = NULL;
  51. mPopup = NULL;
  52. }
  53. void GuiPopupMenuTextListCtrl::onRenderCell(Point2I offset, Point2I cell, bool selected, bool mouseOver)
  54. {
  55. 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
  56. Parent::onRenderCell(offset, cell, selected, mouseOver);
  57. else
  58. {
  59. S32 yp = offset.y + mCellSize.y / 2;
  60. GFX->getDrawUtil()->drawLine(offset.x, yp, offset.x + mCellSize.x, yp, ColorI(128, 128, 128));
  61. GFX->getDrawUtil()->drawLine(offset.x, yp + 1, offset.x + mCellSize.x, yp + 1, ColorI(255, 255, 255));
  62. }
  63. // now see if there's a bitmap...
  64. U8 idx = mList[cell.y].text[0];
  65. if (idx != 1)
  66. {
  67. // there's a bitmap...
  68. U32 index = U32(idx - 2) * 3;
  69. if (!mList[cell.y].active)
  70. index += 2;
  71. else if (selected || mouseOver)
  72. index++;
  73. if (mProfile->mBitmapArrayRects.size() > index)
  74. {
  75. RectI rect = mProfile->mBitmapArrayRects[index];
  76. Point2I off = maxBitmapSize - rect.extent;
  77. off /= 2;
  78. GFX->getDrawUtil()->clearBitmapModulation();
  79. GFX->getDrawUtil()->drawBitmapSR(mProfile->mTextureObject, offset + off, rect);
  80. }
  81. }
  82. // Check if this is a submenu
  83. idx = mList[cell.y].text[1];
  84. if (idx != 1)
  85. {
  86. // This is a submenu, so draw an arrow
  87. S32 left = offset.x + mCellSize.x - 12;
  88. S32 right = left + 8;
  89. S32 top = mCellSize.y / 2 + offset.y - 4;
  90. S32 bottom = top + 8;
  91. S32 middle = top + 4;
  92. PrimBuild::begin(GFXTriangleList, 3);
  93. if (selected || mouseOver)
  94. PrimBuild::color(mProfile->mFontColorHL);
  95. else
  96. PrimBuild::color(mProfile->mFontColor);
  97. PrimBuild::vertex2i(left, top);
  98. PrimBuild::vertex2i(right, middle);
  99. PrimBuild::vertex2i(left, bottom);
  100. PrimBuild::end();
  101. }
  102. }
  103. bool GuiPopupMenuTextListCtrl::onKeyDown(const GuiEvent &event)
  104. {
  105. //if the control is a dead end, don't process the input:
  106. if (!mVisible || !mActive || !mAwake)
  107. return false;
  108. //see if the key down is a <return> or not
  109. if (event.modifier == 0)
  110. {
  111. if (event.keyCode == KEY_RETURN)
  112. {
  113. mBackground->close();
  114. return true;
  115. }
  116. else if (event.keyCode == KEY_ESCAPE)
  117. {
  118. mSelectedCell.set(-1, -1);
  119. mBackground->close();
  120. return true;
  121. }
  122. }
  123. //otherwise, pass the event to it's parent
  124. return Parent::onKeyDown(event);
  125. }
  126. void GuiPopupMenuTextListCtrl::onMouseDown(const GuiEvent &event)
  127. {
  128. Parent::onMouseDown(event);
  129. }
  130. void GuiPopupMenuTextListCtrl::onMouseUp(const GuiEvent &event)
  131. {
  132. Parent::onMouseUp(event);
  133. S32 selectionIndex = getSelectedCell().y;
  134. if (selectionIndex != -1)
  135. {
  136. GuiMenuBar::MenuItem *list = mMenu->firstMenuItem;
  137. while (selectionIndex && list)
  138. {
  139. list = list->nextMenuItem;
  140. selectionIndex--;
  141. }
  142. if (list)
  143. {
  144. if (list->enabled)
  145. dAtob(Con::executef(mPopup, "onSelectItem", Con::getIntArg(getSelectedCell().y), list->text ? list->text : ""));
  146. }
  147. }
  148. mSelectedCell.set(-1, -1);
  149. mBackground->close();
  150. }
  151. void GuiPopupMenuTextListCtrl::onCellHighlighted(Point2I cell)
  152. {
  153. // If this text list control is part of a submenu, then don't worry about
  154. // passing this along
  155. if (!isSubMenu)
  156. {
  157. RectI globalbounds(getBounds());
  158. Point2I globalpoint = localToGlobalCoord(globalbounds.point);
  159. globalbounds.point = globalpoint;
  160. }
  161. }