popupMenu.cpp 16 KB

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