2
0

popupMenu.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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/popupMenu.h"
  23. #include "console/consoleTypes.h"
  24. #include "console/engineAPI.h"
  25. #include "gui/core/guiCanvas.h"
  26. #include "core/util/safeDelete.h"
  27. #include "gui/editor/guiPopupMenuCtrl.h"
  28. #include "gui/editor/guiMenuBar.h"
  29. static U32 sMaxPopupGUID = 0;
  30. PopupMenuEvent PopupMenu::smPopupMenuEvent;
  31. bool PopupMenu::smSelectionEventHandled = false;
  32. /// Event class used to remove popup menus from the event notification in a safe way
  33. class PopUpNotifyRemoveEvent : public SimEvent
  34. {
  35. public:
  36. void process(SimObject *object)
  37. {
  38. PopupMenu::smPopupMenuEvent.remove((PopupMenu *)object, &PopupMenu::handleSelectEvent);
  39. }
  40. };
  41. //-----------------------------------------------------------------------------
  42. // Constructor/Destructor
  43. //-----------------------------------------------------------------------------
  44. PopupMenu::PopupMenu()
  45. {
  46. bitmapIndex = -1;
  47. barTitle = StringTable->EmptyString();
  48. mMenuBarCtrl = nullptr;
  49. mTextList = nullptr;
  50. isSubmenu = false;
  51. }
  52. PopupMenu::~PopupMenu()
  53. {
  54. PopupMenu::smPopupMenuEvent.remove(this, &PopupMenu::handleSelectEvent);
  55. }
  56. IMPLEMENT_CONOBJECT(PopupMenu);
  57. ConsoleDocClass( PopupMenu,
  58. "@brief PopupMenu represents a system menu.\n\n"
  59. "You can add menu items to the menu, but there is no torque object associated "
  60. "with these menu items, they exist only in a platform specific manner.\n\n"
  61. "@note Internal use only\n\n"
  62. "@internal"
  63. );
  64. //-----------------------------------------------------------------------------
  65. void PopupMenu::initPersistFields()
  66. {
  67. Parent::initPersistFields();
  68. addField("barTitle", TypeCaseString, Offset(barTitle, PopupMenu), "");
  69. }
  70. //-----------------------------------------------------------------------------
  71. bool PopupMenu::onAdd()
  72. {
  73. if(! Parent::onAdd())
  74. return false;
  75. Con::executef(this, "onAdd");
  76. return true;
  77. }
  78. void PopupMenu::onRemove()
  79. {
  80. Con::executef(this, "onRemove");
  81. Parent::onRemove();
  82. }
  83. //-----------------------------------------------------------------------------
  84. void PopupMenu::onMenuSelect()
  85. {
  86. Con::executef(this, "onMenuSelect");
  87. }
  88. //-----------------------------------------------------------------------------
  89. void PopupMenu::handleSelectEvent(U32 popID, U32 command)
  90. {
  91. }
  92. //-----------------------------------------------------------------------------
  93. bool PopupMenu::onMessageReceived(StringTableEntry queue, const char* event, const char* data)
  94. {
  95. return Con::executef(this, "onMessageReceived", queue, event, data);
  96. }
  97. bool PopupMenu::onMessageObjectReceived(StringTableEntry queue, Message *msg )
  98. {
  99. return Con::executef(this, "onMessageReceived", queue, Con::getIntArg(msg->getId()));
  100. }
  101. //////////////////////////////////////////////////////////////////////////
  102. // Platform Menu Data
  103. //////////////////////////////////////////////////////////////////////////
  104. GuiMenuBar* PopupMenu::getMenuBarCtrl()
  105. {
  106. return mMenuBarCtrl;
  107. }
  108. //////////////////////////////////////////////////////////////////////////
  109. // Public Methods
  110. //////////////////////////////////////////////////////////////////////////
  111. S32 PopupMenu::insertItem(S32 pos, const char *title, const char* accelerator, const char* cmd)
  112. {
  113. String titleString = title;
  114. MenuItem newItem;
  115. newItem.id = pos;
  116. newItem.text = titleString;
  117. newItem.cmd = cmd;
  118. if (titleString.isEmpty() || titleString == String("-"))
  119. newItem.isSpacer = true;
  120. else
  121. newItem.isSpacer = false;
  122. if (accelerator[0])
  123. newItem.accelerator = dStrdup(accelerator);
  124. else
  125. newItem.accelerator = NULL;
  126. newItem.visible = true;
  127. newItem.isChecked = false;
  128. newItem.acceleratorIndex = 0;
  129. newItem.enabled = !newItem.isSpacer;
  130. newItem.isSubmenu = false;
  131. newItem.subMenu = nullptr;
  132. newItem.subMenuParentMenu = nullptr;
  133. mMenuItems.push_back(newItem);
  134. return pos;
  135. }
  136. S32 PopupMenu::insertSubMenu(S32 pos, const char *title, PopupMenu *submenu)
  137. {
  138. S32 itemPos = insertItem(pos, title, "", "");
  139. mMenuItems[itemPos].isSubmenu = true;
  140. mMenuItems[itemPos].subMenu = submenu;
  141. mMenuItems[itemPos].subMenuParentMenu = this;
  142. submenu->isSubmenu = true;
  143. return itemPos;
  144. }
  145. bool PopupMenu::setItem(S32 pos, const char *title, const char* accelerator, const char* cmd)
  146. {
  147. String titleString = title;
  148. for (U32 i = 0; i < mMenuItems.size(); i++)
  149. {
  150. if (mMenuItems[i].text == titleString)
  151. {
  152. mMenuItems[i].id = pos;
  153. mMenuItems[i].cmd = cmd;
  154. if (accelerator && accelerator[0])
  155. mMenuItems[i].accelerator = dStrdup(accelerator);
  156. else
  157. mMenuItems[i].accelerator = NULL;
  158. return true;
  159. }
  160. }
  161. return false;
  162. }
  163. void PopupMenu::removeItem(S32 itemPos)
  164. {
  165. if (mMenuItems.size() < itemPos || itemPos < 0)
  166. return;
  167. mMenuItems.erase(itemPos);
  168. }
  169. //////////////////////////////////////////////////////////////////////////
  170. void PopupMenu::enableItem(S32 pos, bool enable)
  171. {
  172. if (mMenuItems.size() < pos || pos < 0)
  173. return;
  174. mMenuItems[pos].enabled = enable;
  175. }
  176. void PopupMenu::checkItem(S32 pos, bool checked)
  177. {
  178. if (mMenuItems.size() < pos || pos < 0)
  179. return;
  180. if (checked && mMenuItems[pos].checkGroup != -1)
  181. {
  182. // first, uncheck everything in the group:
  183. for (U32 i = 0; i < mMenuItems.size(); i++)
  184. if (mMenuItems[i].checkGroup == mMenuItems[pos].checkGroup && mMenuItems[i].isChecked)
  185. mMenuItems[i].isChecked = false;
  186. }
  187. mMenuItems[pos].isChecked;
  188. }
  189. void PopupMenu::checkRadioItem(S32 firstPos, S32 lastPos, S32 checkPos)
  190. {
  191. for (U32 i = 0; i < mMenuItems.size(); i++)
  192. {
  193. if (mMenuItems[i].id >= firstPos && mMenuItems[i].id <= lastPos)
  194. {
  195. mMenuItems[i].isChecked = false;
  196. }
  197. }
  198. }
  199. bool PopupMenu::isItemChecked(S32 pos)
  200. {
  201. if (mMenuItems.size() < pos || pos < 0)
  202. return false;
  203. return mMenuItems[pos].isChecked;
  204. }
  205. U32 PopupMenu::getItemCount()
  206. {
  207. return mMenuItems.size();
  208. }
  209. //////////////////////////////////////////////////////////////////////////
  210. bool PopupMenu::canHandleID(U32 id)
  211. {
  212. return true;
  213. }
  214. bool PopupMenu::handleSelect(U32 command, const char *text /* = NULL */)
  215. {
  216. return dAtob(Con::executef(this, "onSelectItem", Con::getIntArg(command), text ? text : ""));
  217. }
  218. //////////////////////////////////////////////////////////////////////////
  219. void PopupMenu::showPopup(GuiCanvas *owner, S32 x /* = -1 */, S32 y /* = -1 */)
  220. {
  221. if (owner == NULL)
  222. return;
  223. GuiControl* editorGui;
  224. Sim::findObject("EditorGui", editorGui);
  225. if (editorGui)
  226. {
  227. GuiPopupMenuBackgroundCtrl* backgroundCtrl;
  228. Sim::findObject("PopUpMenuControl", backgroundCtrl);
  229. GuiControlProfile* profile;
  230. Sim::findObject("GuiMenubarProfile", profile);
  231. if (!profile)
  232. return;
  233. if (mTextList == nullptr)
  234. {
  235. mTextList = new GuiPopupMenuTextListCtrl();
  236. mTextList->registerObject();
  237. mTextList->setControlProfile(profile);
  238. mTextList->mPopup = this;
  239. mTextList->mMenuBar = getMenuBarCtrl();
  240. }
  241. if (!backgroundCtrl)
  242. {
  243. backgroundCtrl = new GuiPopupMenuBackgroundCtrl();
  244. backgroundCtrl->registerObject("PopUpMenuControl");
  245. }
  246. if (!backgroundCtrl || !mTextList)
  247. return;
  248. if (!isSubmenu)
  249. {
  250. //if we're a 'parent' menu, then tell the background to clear out all existing other popups
  251. backgroundCtrl->clearPopups();
  252. }
  253. //find out if we're doing a first-time add
  254. S32 popupIndex = backgroundCtrl->findPopupMenu(this);
  255. if (popupIndex == -1)
  256. {
  257. backgroundCtrl->addObject(mTextList);
  258. backgroundCtrl->mPopups.push_back(this);
  259. }
  260. mTextList->mBackground = backgroundCtrl;
  261. owner->pushDialogControl(backgroundCtrl, 10);
  262. //Set the background control's menubar, if any, and if it's not already set
  263. if(backgroundCtrl->mMenuBarCtrl == nullptr)
  264. backgroundCtrl->mMenuBarCtrl = getMenuBarCtrl();
  265. backgroundCtrl->setExtent(editorGui->getExtent());
  266. mTextList->clear();
  267. S32 textWidth = 0, width = 0;
  268. S32 acceleratorWidth = 0;
  269. GFont *font = profile->mFont;
  270. Point2I maxBitmapSize = Point2I(0, 0);
  271. S32 numBitmaps = profile->mBitmapArrayRects.size();
  272. if (numBitmaps)
  273. {
  274. RectI *bitmapBounds = profile->mBitmapArrayRects.address();
  275. for (S32 i = 0; i < numBitmaps; i++)
  276. {
  277. if (bitmapBounds[i].extent.x > maxBitmapSize.x)
  278. maxBitmapSize.x = bitmapBounds[i].extent.x;
  279. if (bitmapBounds[i].extent.y > maxBitmapSize.y)
  280. maxBitmapSize.y = bitmapBounds[i].extent.y;
  281. }
  282. }
  283. for (U32 i = 0; i < mMenuItems.size(); i++)
  284. {
  285. if (!mMenuItems[i].visible)
  286. continue;
  287. S32 iTextWidth = font->getStrWidth(mMenuItems[i].text.c_str());
  288. S32 iAcceleratorWidth = mMenuItems[i].accelerator ? font->getStrWidth(mMenuItems[i].accelerator) : 0;
  289. if (iTextWidth > textWidth)
  290. textWidth = iTextWidth;
  291. if (iAcceleratorWidth > acceleratorWidth)
  292. acceleratorWidth = iAcceleratorWidth;
  293. }
  294. width = textWidth + acceleratorWidth + maxBitmapSize.x * 2 + 2 + 4;
  295. mTextList->setCellSize(Point2I(width, font->getHeight() + 2));
  296. mTextList->clearColumnOffsets();
  297. mTextList->addColumnOffset(-1); // add an empty column in for the bitmap index.
  298. mTextList->addColumnOffset(maxBitmapSize.x + 1);
  299. mTextList->addColumnOffset(maxBitmapSize.x + 1 + textWidth + 4);
  300. U32 entryCount = 0;
  301. for (U32 i = 0; i < mMenuItems.size(); i++)
  302. {
  303. if (!mMenuItems[i].visible)
  304. continue;
  305. char buf[512];
  306. // If this menu item is a submenu, then set the isSubmenu to 2 to indicate
  307. // an arrow should be drawn. Otherwise set the isSubmenu normally.
  308. char isSubmenu = 1;
  309. if (mMenuItems[i].isSubmenu)
  310. isSubmenu = 2;
  311. char bitmapIndex = 1;
  312. if (mMenuItems[i].bitmapIndex >= 0 && (mMenuItems[i].bitmapIndex * 3 <= profile->mBitmapArrayRects.size()))
  313. bitmapIndex = mMenuItems[i].bitmapIndex + 2;
  314. dSprintf(buf, sizeof(buf), "%c%c\t%s\t%s", bitmapIndex, isSubmenu, mMenuItems[i].text.c_str(), mMenuItems[i].accelerator ? mMenuItems[i].accelerator : "");
  315. mTextList->addEntry(entryCount, buf);
  316. if (!mMenuItems[i].enabled)
  317. mTextList->setEntryActive(entryCount, false);
  318. entryCount++;
  319. }
  320. Point2I pos = Point2I::Zero;
  321. if (x == -1 && y == -1)
  322. pos = owner->getCursorPos();
  323. else
  324. pos = Point2I(x, y);
  325. mTextList->setPosition(pos);
  326. //nudge in if we'd overshoot the screen
  327. S32 widthDiff = (mTextList->getPosition().x + mTextList->getExtent().x) - backgroundCtrl->getWidth();
  328. if (widthDiff > 0)
  329. {
  330. Point2I popupPos = mTextList->getPosition();
  331. mTextList->setPosition(popupPos.x - widthDiff, popupPos.y);
  332. }
  333. mTextList->setHidden(false);
  334. }
  335. }
  336. void PopupMenu::hidePopup()
  337. {
  338. if (mTextList)
  339. {
  340. mTextList->setHidden(true);
  341. }
  342. hidePopupSubmenus();
  343. }
  344. void PopupMenu::hidePopupSubmenus()
  345. {
  346. for (U32 i = 0; i < mMenuItems.size(); i++)
  347. {
  348. if (mMenuItems[i].subMenu != nullptr)
  349. mMenuItems[i].subMenu->hidePopup();
  350. }
  351. }
  352. //-----------------------------------------------------------------------------
  353. // Console Methods
  354. //-----------------------------------------------------------------------------
  355. DefineConsoleMethod(PopupMenu, insertItem, S32, (S32 pos, const char * title, const char * accelerator, const char* cmd), ("", "", ""), "(pos[, title][, accelerator][, cmd])")
  356. {
  357. return object->insertItem(pos, title, accelerator, cmd);
  358. }
  359. DefineConsoleMethod(PopupMenu, removeItem, void, (S32 pos), , "(pos)")
  360. {
  361. object->removeItem(pos);
  362. }
  363. DefineConsoleMethod(PopupMenu, insertSubMenu, S32, (S32 pos, String title, String subMenu), , "(pos, title, subMenu)")
  364. {
  365. PopupMenu *mnu = dynamic_cast<PopupMenu *>(Sim::findObject(subMenu));
  366. if(mnu == NULL)
  367. {
  368. Con::errorf("PopupMenu::insertSubMenu - Invalid PopupMenu object specified for submenu");
  369. return -1;
  370. }
  371. return object->insertSubMenu(pos, title, mnu);
  372. }
  373. DefineConsoleMethod(PopupMenu, setItem, bool, (S32 pos, const char * title, const char * accelerator, const char *cmd), (""), "(pos, title[, accelerator][, cmd])")
  374. {
  375. return object->setItem(pos, title, accelerator, cmd);
  376. }
  377. //-----------------------------------------------------------------------------
  378. DefineConsoleMethod(PopupMenu, enableItem, void, (S32 pos, bool enabled), , "(pos, enabled)")
  379. {
  380. object->enableItem(pos, enabled);
  381. }
  382. DefineConsoleMethod(PopupMenu, checkItem, void, (S32 pos, bool checked), , "(pos, checked)")
  383. {
  384. object->checkItem(pos, checked);
  385. }
  386. DefineConsoleMethod(PopupMenu, checkRadioItem, void, (S32 firstPos, S32 lastPos, S32 checkPos), , "(firstPos, lastPos, checkPos)")
  387. {
  388. object->checkRadioItem(firstPos, lastPos, checkPos);
  389. }
  390. DefineConsoleMethod(PopupMenu, isItemChecked, bool, (S32 pos), , "(pos)")
  391. {
  392. return object->isItemChecked(pos);
  393. }
  394. DefineConsoleMethod(PopupMenu, getItemCount, S32, (), , "()")
  395. {
  396. return object->getItemCount();
  397. }
  398. //-----------------------------------------------------------------------------
  399. DefineConsoleMethod(PopupMenu, showPopup, void, (const char * canvasName, S32 x, S32 y), ( -1, -1), "(Canvas,[x, y])")
  400. {
  401. GuiCanvas *pCanvas = dynamic_cast<GuiCanvas*>(Sim::findObject(canvasName));
  402. object->showPopup(pCanvas, x, y);
  403. }