popupMenu.cpp 15 KB

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