popupMenu.cpp 16 KB

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