popupMenu.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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, S32 bitmapIndex)
  120. {
  121. String titleString = title;
  122. MenuItem newItem;
  123. newItem.mID = pos;
  124. newItem.mText = titleString;
  125. newItem.mCMD = cmd;
  126. newItem.mBitmapIndex = bitmapIndex;
  127. if (titleString.isEmpty() || titleString == String("-"))
  128. newItem.mIsSpacer = true;
  129. else
  130. newItem.mIsSpacer = false;
  131. if (accelerator[0])
  132. newItem.mAccelerator = dStrdup(accelerator);
  133. else
  134. newItem.mAccelerator = NULL;
  135. newItem.mVisible = true;
  136. newItem.mIsChecked = false;
  137. newItem.mAcceleratorIndex = 0;
  138. newItem.mEnabled = !newItem.mIsSpacer;
  139. newItem.mIsSubmenu = false;
  140. newItem.mSubMenu = nullptr;
  141. newItem.mSubMenuParentMenu = nullptr;
  142. mMenuItems.push_back(newItem);
  143. return pos;
  144. }
  145. S32 PopupMenu::insertSubMenu(S32 pos, const char *title, PopupMenu *submenu)
  146. {
  147. S32 itemPos = insertItem(pos, title, "", "");
  148. mMenuItems[itemPos].mIsSubmenu = true;
  149. mMenuItems[itemPos].mSubMenu = submenu;
  150. mMenuItems[itemPos].mSubMenuParentMenu = this;
  151. submenu->mIsSubmenu = true;
  152. return itemPos;
  153. }
  154. bool PopupMenu::setItem(S32 pos, const char *title, const char* accelerator, const char* cmd)
  155. {
  156. String titleString = title;
  157. for (U32 i = 0; i < mMenuItems.size(); i++)
  158. {
  159. if (mMenuItems[i].mText == titleString)
  160. {
  161. mMenuItems[i].mID = pos;
  162. mMenuItems[i].mCMD = cmd;
  163. if (accelerator && accelerator[0])
  164. mMenuItems[i].mAccelerator = dStrdup(accelerator);
  165. else
  166. mMenuItems[i].mAccelerator = NULL;
  167. return true;
  168. }
  169. }
  170. return false;
  171. }
  172. void PopupMenu::removeItem(S32 itemPos)
  173. {
  174. if (mMenuItems.empty() || mMenuItems.size() < itemPos || itemPos < 0)
  175. return;
  176. mMenuItems.erase(itemPos);
  177. }
  178. //////////////////////////////////////////////////////////////////////////
  179. void PopupMenu::enableItem(S32 pos, bool enable)
  180. {
  181. if (mMenuItems.empty() || mMenuItems.size() < pos || pos < 0)
  182. return;
  183. mMenuItems[pos].mEnabled = enable;
  184. }
  185. void PopupMenu::checkItem(S32 pos, bool checked)
  186. {
  187. if (mMenuItems.empty() || mMenuItems.size() < pos || pos < 0)
  188. return;
  189. if (checked && mMenuItems[pos].mCheckGroup != -1 && mRadioSelection)
  190. {
  191. // first, uncheck everything in the group:
  192. for (U32 i = 0; i < mMenuItems.size(); i++)
  193. if (mMenuItems[i].mCheckGroup == mMenuItems[pos].mCheckGroup && mMenuItems[i].mIsChecked)
  194. mMenuItems[i].mIsChecked = false;
  195. }
  196. mMenuItems[pos].mIsChecked = checked;
  197. }
  198. void PopupMenu::checkRadioItem(S32 firstPos, S32 lastPos, S32 checkPos)
  199. {
  200. for (U32 i = 0; i < mMenuItems.size(); i++)
  201. {
  202. if (mMenuItems[i].mID >= firstPos && mMenuItems[i].mID <= lastPos)
  203. {
  204. mMenuItems[i].mIsChecked = false;
  205. }
  206. }
  207. }
  208. bool PopupMenu::isItemChecked(S32 pos)
  209. {
  210. if (mMenuItems.empty() || mMenuItems.size() < pos || pos < 0)
  211. return false;
  212. return mMenuItems[pos].mIsChecked;
  213. }
  214. U32 PopupMenu::getItemCount()
  215. {
  216. return mMenuItems.size();
  217. }
  218. void PopupMenu::clearItems()
  219. {
  220. mMenuItems.clear();
  221. }
  222. String PopupMenu::getItemText(S32 pos)
  223. {
  224. if (mMenuItems.empty() || mMenuItems.size() < pos || pos < 0)
  225. return String::EmptyString;
  226. return mMenuItems[pos].mText;
  227. }
  228. //////////////////////////////////////////////////////////////////////////
  229. bool PopupMenu::canHandleID(U32 id)
  230. {
  231. return true;
  232. }
  233. bool PopupMenu::handleSelect(U32 command, const char *text /* = NULL */)
  234. {
  235. return dAtob(Con::executef(this, "onSelectItem", Con::getIntArg(command), text ? text : ""));
  236. }
  237. //////////////////////////////////////////////////////////////////////////
  238. void PopupMenu::showPopup(GuiCanvas *owner, S32 x /* = -1 */, S32 y /* = -1 */)
  239. {
  240. if (owner == NULL)
  241. return;
  242. GuiPopupMenuBackgroundCtrl* backgroundCtrl;
  243. Sim::findObject("PopUpMenuControl", backgroundCtrl);
  244. GuiControlProfile* profile;
  245. Sim::findObject("ToolsGuiMenuBarProfile", profile);
  246. if (!profile)
  247. return;
  248. if (mTextList == nullptr)
  249. {
  250. mTextList = new GuiPopupMenuTextListCtrl();
  251. mTextList->registerObject();
  252. mTextList->setControlProfile(profile);
  253. mTextList->mRowHeightPadding = 5;
  254. mTextList->mPopup = this;
  255. mTextList->mMenuBar = getMenuBarCtrl();
  256. }
  257. if (!backgroundCtrl)
  258. {
  259. backgroundCtrl = new GuiPopupMenuBackgroundCtrl();
  260. backgroundCtrl->registerObject("PopUpMenuControl");
  261. }
  262. if (!backgroundCtrl || !mTextList)
  263. return;
  264. if (!mIsSubmenu)
  265. {
  266. //if we're a 'parent' menu, then tell the background to clear out all existing other popups
  267. backgroundCtrl->clearPopups();
  268. }
  269. //find out if we're doing a first-time add
  270. S32 popupIndex = backgroundCtrl->findPopupMenu(this);
  271. if (popupIndex == -1)
  272. {
  273. backgroundCtrl->addObject(mTextList);
  274. backgroundCtrl->mPopups.push_back(this);
  275. }
  276. mTextList->mBackground = backgroundCtrl;
  277. owner->pushDialogControl(backgroundCtrl, 10);
  278. //Set the background control's menubar, if any, and if it's not already set
  279. if(backgroundCtrl->mMenuBarCtrl == nullptr)
  280. backgroundCtrl->mMenuBarCtrl = getMenuBarCtrl();
  281. backgroundCtrl->setExtent(owner->getExtent());
  282. mTextList->clear();
  283. S32 textWidth = 0, width = 0;
  284. S32 acceleratorWidth = 0;
  285. GFont *font = profile->mFont;
  286. Point2I maxBitmapSize = Point2I(0, 0);
  287. S32 numBitmaps = profile->mBitmapArrayRects.size();
  288. if (numBitmaps)
  289. {
  290. RectI *bitmapBounds = profile->mBitmapArrayRects.address();
  291. for (S32 i = 0; i < numBitmaps; i++)
  292. {
  293. if (bitmapBounds[i].extent.x > maxBitmapSize.x)
  294. maxBitmapSize.x = bitmapBounds[i].extent.x;
  295. if (bitmapBounds[i].extent.y > maxBitmapSize.y)
  296. maxBitmapSize.y = bitmapBounds[i].extent.y;
  297. }
  298. }
  299. for (U32 i = 0; i < mMenuItems.size(); i++)
  300. {
  301. if (!mMenuItems[i].mVisible)
  302. continue;
  303. S32 iTextWidth = font->getStrWidth(mMenuItems[i].mText.c_str());
  304. S32 iAcceleratorWidth = mMenuItems[i].mAccelerator ? font->getStrWidth(mMenuItems[i].mAccelerator) : 0;
  305. if (iTextWidth > textWidth)
  306. textWidth = iTextWidth;
  307. if (iAcceleratorWidth > acceleratorWidth)
  308. acceleratorWidth = iAcceleratorWidth;
  309. }
  310. width = textWidth + acceleratorWidth + maxBitmapSize.x * 2 + 2 + 4;
  311. mTextList->setCellSize(Point2I(width, font->getHeight() + 2));
  312. mTextList->clearColumnOffsets();
  313. mTextList->addColumnOffset(-1); // add an empty column in for the bitmap index.
  314. mTextList->addColumnOffset(maxBitmapSize.x + 1);
  315. mTextList->addColumnOffset(maxBitmapSize.x + 1 + textWidth + 4);
  316. U32 entryCount = 0;
  317. for (U32 i = 0; i < mMenuItems.size(); i++)
  318. {
  319. if (!mMenuItems[i].mVisible)
  320. continue;
  321. char buf[512];
  322. // If this menu item is a submenu, then set the isSubmenu to 2 to indicate
  323. // an arrow should be drawn. Otherwise set the isSubmenu normally.
  324. char isSubmenu = 1;
  325. if (mMenuItems[i].mIsSubmenu)
  326. isSubmenu = 2;
  327. char bitmapIndex = 1;
  328. if (mMenuItems[i].mBitmapIndex >= 0 && (mMenuItems[i].mBitmapIndex * 3 <= profile->mBitmapArrayRects.size()))
  329. bitmapIndex = mMenuItems[i].mBitmapIndex + 2;
  330. dSprintf(buf, sizeof(buf), "%c%c\t%s\t%s", bitmapIndex, isSubmenu, mMenuItems[i].mText.c_str(), mMenuItems[i].mAccelerator ? mMenuItems[i].mAccelerator : "");
  331. mTextList->addEntry(entryCount, buf);
  332. if (!mMenuItems[i].mEnabled)
  333. mTextList->setEntryActive(entryCount, false);
  334. entryCount++;
  335. }
  336. Point2I pos = Point2I::Zero;
  337. if (x == -1 && y == -1)
  338. pos = owner->getCursorPos();
  339. else
  340. pos = Point2I(x, y);
  341. mTextList->setPosition(pos);
  342. //nudge in if we'd overshoot the screen
  343. S32 widthDiff = (mTextList->getPosition().x + mTextList->getExtent().x) - backgroundCtrl->getWidth();
  344. if (widthDiff > 0)
  345. {
  346. Point2I popupPos = mTextList->getPosition();
  347. mTextList->setPosition(popupPos.x - widthDiff, popupPos.y);
  348. }
  349. //If we'd overshoot the screen vertically, just mirror the axis so we're above the mouse
  350. S32 heightDiff = (mTextList->getPosition().y + mTextList->getExtent().y) - backgroundCtrl->getHeight();
  351. if (heightDiff > 0)
  352. {
  353. Point2I popupPos = mTextList->getPosition();
  354. mTextList->setPosition(popupPos.x, popupPos.y - mTextList->getExtent().y);
  355. }
  356. mTextList->setHidden(false);
  357. mVisible = true;
  358. }
  359. void PopupMenu::hidePopup()
  360. {
  361. if (mTextList)
  362. {
  363. mTextList->setHidden(true);
  364. }
  365. hidePopupSubmenus();
  366. mVisible = false;
  367. }
  368. void PopupMenu::hidePopupSubmenus()
  369. {
  370. for (U32 i = 0; i < mMenuItems.size(); i++)
  371. {
  372. if (mMenuItems[i].mSubMenu != nullptr)
  373. mMenuItems[i].mSubMenu->hidePopup();
  374. }
  375. }
  376. //-----------------------------------------------------------------------------
  377. // Console Methods
  378. //-----------------------------------------------------------------------------
  379. DefineEngineMethod(PopupMenu, insertItem, S32, (S32 pos, const char * title, const char * accelerator, const char* cmd, S32 bitmapIndex), ("", "", "", -1), "(pos[, title][, accelerator][, cmd][, bitmapIndex])")
  380. {
  381. return object->insertItem(pos, title, accelerator, cmd, bitmapIndex);
  382. }
  383. DefineEngineMethod(PopupMenu, removeItem, void, (S32 pos), , "(pos)")
  384. {
  385. object->removeItem(pos);
  386. }
  387. DefineEngineMethod(PopupMenu, insertSubMenu, S32, (S32 pos, String title, String subMenu), , "(pos, title, subMenu)")
  388. {
  389. PopupMenu *mnu = dynamic_cast<PopupMenu *>(Sim::findObject(subMenu));
  390. if(mnu == NULL)
  391. {
  392. Con::errorf("PopupMenu::insertSubMenu - Invalid PopupMenu object specified for submenu");
  393. return -1;
  394. }
  395. return object->insertSubMenu(pos, title, mnu);
  396. }
  397. DefineEngineMethod(PopupMenu, setItem, bool, (S32 pos, const char * title, const char * accelerator, const char *cmd), (""), "(pos, title[, accelerator][, cmd])")
  398. {
  399. return object->setItem(pos, title, accelerator, cmd);
  400. }
  401. //-----------------------------------------------------------------------------
  402. DefineEngineMethod(PopupMenu, enableItem, void, (S32 pos, bool enabled), , "(pos, enabled)")
  403. {
  404. object->enableItem(pos, enabled);
  405. }
  406. DefineEngineMethod(PopupMenu, checkItem, void, (S32 pos, bool checked), , "(pos, checked)")
  407. {
  408. object->checkItem(pos, checked);
  409. }
  410. DefineEngineMethod(PopupMenu, getItemText, const char*, (S32 pos), , "(pos)")
  411. {
  412. return object->getItemText(pos).c_str();
  413. }
  414. DefineEngineMethod(PopupMenu, checkRadioItem, void, (S32 firstPos, S32 lastPos, S32 checkPos), , "(firstPos, lastPos, checkPos)")
  415. {
  416. object->checkRadioItem(firstPos, lastPos, checkPos);
  417. }
  418. DefineEngineMethod(PopupMenu, isItemChecked, bool, (S32 pos), , "(pos)")
  419. {
  420. return object->isItemChecked(pos);
  421. }
  422. DefineEngineMethod(PopupMenu, getItemCount, S32, (), , "()")
  423. {
  424. return object->getItemCount();
  425. }
  426. DefineEngineMethod(PopupMenu, clearItems, void, (), , "()")
  427. {
  428. return object->clearItems();
  429. }
  430. //-----------------------------------------------------------------------------
  431. DefineEngineMethod(PopupMenu, showPopup, void, (const char * canvasName, S32 x, S32 y), ( -1, -1), "(Canvas,[x, y])")
  432. {
  433. GuiCanvas *pCanvas = dynamic_cast<GuiCanvas*>(Sim::findObject(canvasName));
  434. object->showPopup(pCanvas, x, y);
  435. }