2
0

popupMenu.cpp 15 KB

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