popupMenu.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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.empty() || 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.empty() || 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.empty() || 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.empty() || 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. void PopupMenu::clearItems()
  210. {
  211. mMenuItems.clear();
  212. }
  213. //////////////////////////////////////////////////////////////////////////
  214. bool PopupMenu::canHandleID(U32 id)
  215. {
  216. return true;
  217. }
  218. bool PopupMenu::handleSelect(U32 command, const char *text /* = NULL */)
  219. {
  220. return dAtob(Con::executef(this, "onSelectItem", Con::getIntArg(command), text ? text : ""));
  221. }
  222. //////////////////////////////////////////////////////////////////////////
  223. void PopupMenu::showPopup(GuiCanvas *owner, S32 x /* = -1 */, S32 y /* = -1 */)
  224. {
  225. if (owner == NULL)
  226. return;
  227. GuiControl* editorGui;
  228. Sim::findObject("EditorGui", editorGui);
  229. if (editorGui)
  230. {
  231. GuiPopupMenuBackgroundCtrl* backgroundCtrl;
  232. Sim::findObject("PopUpMenuControl", backgroundCtrl);
  233. GuiControlProfile* profile;
  234. Sim::findObject("GuiMenubarProfile", profile);
  235. if (!profile)
  236. return;
  237. if (mTextList == nullptr)
  238. {
  239. mTextList = new GuiPopupMenuTextListCtrl();
  240. mTextList->registerObject();
  241. mTextList->setControlProfile(profile);
  242. mTextList->mPopup = this;
  243. mTextList->mMenuBar = getMenuBarCtrl();
  244. }
  245. if (!backgroundCtrl)
  246. {
  247. backgroundCtrl = new GuiPopupMenuBackgroundCtrl();
  248. backgroundCtrl->registerObject("PopUpMenuControl");
  249. }
  250. if (!backgroundCtrl || !mTextList)
  251. return;
  252. if (!isSubmenu)
  253. {
  254. //if we're a 'parent' menu, then tell the background to clear out all existing other popups
  255. backgroundCtrl->clearPopups();
  256. }
  257. //find out if we're doing a first-time add
  258. S32 popupIndex = backgroundCtrl->findPopupMenu(this);
  259. if (popupIndex == -1)
  260. {
  261. backgroundCtrl->addObject(mTextList);
  262. backgroundCtrl->mPopups.push_back(this);
  263. }
  264. mTextList->mBackground = backgroundCtrl;
  265. owner->pushDialogControl(backgroundCtrl, 10);
  266. //Set the background control's menubar, if any, and if it's not already set
  267. if(backgroundCtrl->mMenuBarCtrl == nullptr)
  268. backgroundCtrl->mMenuBarCtrl = getMenuBarCtrl();
  269. backgroundCtrl->setExtent(editorGui->getExtent());
  270. mTextList->clear();
  271. S32 textWidth = 0, width = 0;
  272. S32 acceleratorWidth = 0;
  273. GFont *font = profile->mFont;
  274. Point2I maxBitmapSize = Point2I(0, 0);
  275. S32 numBitmaps = profile->mBitmapArrayRects.size();
  276. if (numBitmaps)
  277. {
  278. RectI *bitmapBounds = profile->mBitmapArrayRects.address();
  279. for (S32 i = 0; i < numBitmaps; i++)
  280. {
  281. if (bitmapBounds[i].extent.x > maxBitmapSize.x)
  282. maxBitmapSize.x = bitmapBounds[i].extent.x;
  283. if (bitmapBounds[i].extent.y > maxBitmapSize.y)
  284. maxBitmapSize.y = bitmapBounds[i].extent.y;
  285. }
  286. }
  287. for (U32 i = 0; i < mMenuItems.size(); i++)
  288. {
  289. if (!mMenuItems[i].visible)
  290. continue;
  291. S32 iTextWidth = font->getStrWidth(mMenuItems[i].text.c_str());
  292. S32 iAcceleratorWidth = mMenuItems[i].accelerator ? font->getStrWidth(mMenuItems[i].accelerator) : 0;
  293. if (iTextWidth > textWidth)
  294. textWidth = iTextWidth;
  295. if (iAcceleratorWidth > acceleratorWidth)
  296. acceleratorWidth = iAcceleratorWidth;
  297. }
  298. width = textWidth + acceleratorWidth + maxBitmapSize.x * 2 + 2 + 4;
  299. mTextList->setCellSize(Point2I(width, font->getHeight() + 2));
  300. mTextList->clearColumnOffsets();
  301. mTextList->addColumnOffset(-1); // add an empty column in for the bitmap index.
  302. mTextList->addColumnOffset(maxBitmapSize.x + 1);
  303. mTextList->addColumnOffset(maxBitmapSize.x + 1 + textWidth + 4);
  304. U32 entryCount = 0;
  305. for (U32 i = 0; i < mMenuItems.size(); i++)
  306. {
  307. if (!mMenuItems[i].visible)
  308. continue;
  309. char buf[512];
  310. // If this menu item is a submenu, then set the isSubmenu to 2 to indicate
  311. // an arrow should be drawn. Otherwise set the isSubmenu normally.
  312. char isSubmenu = 1;
  313. if (mMenuItems[i].isSubmenu)
  314. isSubmenu = 2;
  315. char bitmapIndex = 1;
  316. if (mMenuItems[i].bitmapIndex >= 0 && (mMenuItems[i].bitmapIndex * 3 <= profile->mBitmapArrayRects.size()))
  317. bitmapIndex = mMenuItems[i].bitmapIndex + 2;
  318. dSprintf(buf, sizeof(buf), "%c%c\t%s\t%s", bitmapIndex, isSubmenu, mMenuItems[i].text.c_str(), mMenuItems[i].accelerator ? mMenuItems[i].accelerator : "");
  319. mTextList->addEntry(entryCount, buf);
  320. if (!mMenuItems[i].enabled)
  321. mTextList->setEntryActive(entryCount, false);
  322. entryCount++;
  323. }
  324. Point2I pos = Point2I::Zero;
  325. if (x == -1 && y == -1)
  326. pos = owner->getCursorPos();
  327. else
  328. pos = Point2I(x, y);
  329. mTextList->setPosition(pos);
  330. //nudge in if we'd overshoot the screen
  331. S32 widthDiff = (mTextList->getPosition().x + mTextList->getExtent().x) - backgroundCtrl->getWidth();
  332. if (widthDiff > 0)
  333. {
  334. Point2I popupPos = mTextList->getPosition();
  335. mTextList->setPosition(popupPos.x - widthDiff, popupPos.y);
  336. }
  337. mTextList->setHidden(false);
  338. }
  339. }
  340. void PopupMenu::hidePopup()
  341. {
  342. if (mTextList)
  343. {
  344. mTextList->setHidden(true);
  345. }
  346. hidePopupSubmenus();
  347. }
  348. void PopupMenu::hidePopupSubmenus()
  349. {
  350. for (U32 i = 0; i < mMenuItems.size(); i++)
  351. {
  352. if (mMenuItems[i].subMenu != nullptr)
  353. mMenuItems[i].subMenu->hidePopup();
  354. }
  355. }
  356. //-----------------------------------------------------------------------------
  357. // Console Methods
  358. //-----------------------------------------------------------------------------
  359. DefineConsoleMethod(PopupMenu, insertItem, S32, (S32 pos, const char * title, const char * accelerator, const char* cmd), ("", "", ""), "(pos[, title][, accelerator][, cmd])")
  360. {
  361. return object->insertItem(pos, title, accelerator, cmd);
  362. }
  363. DefineConsoleMethod(PopupMenu, removeItem, void, (S32 pos), , "(pos)")
  364. {
  365. object->removeItem(pos);
  366. }
  367. DefineConsoleMethod(PopupMenu, insertSubMenu, S32, (S32 pos, String title, String subMenu), , "(pos, title, subMenu)")
  368. {
  369. PopupMenu *mnu = dynamic_cast<PopupMenu *>(Sim::findObject(subMenu));
  370. if(mnu == NULL)
  371. {
  372. Con::errorf("PopupMenu::insertSubMenu - Invalid PopupMenu object specified for submenu");
  373. return -1;
  374. }
  375. return object->insertSubMenu(pos, title, mnu);
  376. }
  377. DefineConsoleMethod(PopupMenu, setItem, bool, (S32 pos, const char * title, const char * accelerator, const char *cmd), (""), "(pos, title[, accelerator][, cmd])")
  378. {
  379. return object->setItem(pos, title, accelerator, cmd);
  380. }
  381. //-----------------------------------------------------------------------------
  382. DefineConsoleMethod(PopupMenu, enableItem, void, (S32 pos, bool enabled), , "(pos, enabled)")
  383. {
  384. object->enableItem(pos, enabled);
  385. }
  386. DefineConsoleMethod(PopupMenu, checkItem, void, (S32 pos, bool checked), , "(pos, checked)")
  387. {
  388. object->checkItem(pos, checked);
  389. }
  390. DefineConsoleMethod(PopupMenu, checkRadioItem, void, (S32 firstPos, S32 lastPos, S32 checkPos), , "(firstPos, lastPos, checkPos)")
  391. {
  392. object->checkRadioItem(firstPos, lastPos, checkPos);
  393. }
  394. DefineConsoleMethod(PopupMenu, isItemChecked, bool, (S32 pos), , "(pos)")
  395. {
  396. return object->isItemChecked(pos);
  397. }
  398. DefineConsoleMethod(PopupMenu, getItemCount, S32, (), , "()")
  399. {
  400. return object->getItemCount();
  401. }
  402. DefineConsoleMethod(PopupMenu, clearItems, void, (), , "()")
  403. {
  404. return object->clearItems();
  405. }
  406. //-----------------------------------------------------------------------------
  407. DefineConsoleMethod(PopupMenu, showPopup, void, (const char * canvasName, S32 x, S32 y), ( -1, -1), "(Canvas,[x, y])")
  408. {
  409. GuiCanvas *pCanvas = dynamic_cast<GuiCanvas*>(Sim::findObject(canvasName));
  410. object->showPopup(pCanvas, x, y);
  411. }