guiMenuBarCtrl.cc 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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 "console/console.h"
  23. #include "console/consoleTypes.h"
  24. #include "graphics/dgl.h"
  25. #include "gui/guiDefaultControlRender.h"
  26. #include "gui/guiCanvas.h"
  27. #include "input/actionMap.h"
  28. #include "gui/editor/guiMenuBarCtrl.h"
  29. #include "gui/editor/guiMenuBarCtrl_ScriptBinding.h"
  30. #pragma region GuiMenuBarCtrl
  31. IMPLEMENT_CONOBJECT(GuiMenuBarCtrl);
  32. GuiMenuBarCtrl::GuiMenuBarCtrl()
  33. {
  34. mBounds.point.set(0, 0);
  35. mBounds.extent.set(400, 26);
  36. mActive = true;
  37. mUseKeyMode = false;
  38. mAcceleratorKey = "alt";
  39. setField("profile", "GuiMenuBarProfile");
  40. mMenuProfile = NULL;
  41. setField("MenuProfile", "GuiMenuProfile");
  42. mMenuContentProfile = NULL;
  43. setField("MenuContentProfile", "GuiMenuContentProfile");
  44. mMenuItemProfile = NULL;
  45. setField("MenuItemProfile", "GuiMenuItemProfile");
  46. mThumbProfile = NULL;
  47. setField("ThumbProfile", "GuiScrollThumbProfile");
  48. mArrowProfile = NULL;
  49. setField("ArrowProfile", "GuiScrollArrowProfile");
  50. mTrackProfile = NULL;
  51. setField("TrackProfile", "GuiScrollTrackProfile");
  52. mHoverTarget = NULL;
  53. mOpenMenu = NULL;
  54. mUseConstantHeightThumb = false;
  55. mScrollBarThickness = 12;
  56. mShowArrowButtons = false;
  57. mBackground = new GuiMenuBGCtrl(this);
  58. AssertFatal(mBackground, "GuiDropDownCtrl: Failed to initialize GuiMenuBGCtrl!");
  59. mBackgroundProfile = mBackground->mProfile;
  60. mBackgroundProfile->incRefCount();
  61. }
  62. void GuiMenuBarCtrl::initPersistFields()
  63. {
  64. Parent::initPersistFields();
  65. addField("MenuProfile", TypeGuiProfile, Offset(mMenuProfile, GuiMenuBarCtrl));
  66. addField("MenuContentProfile", TypeGuiProfile, Offset(mMenuContentProfile, GuiMenuBarCtrl));
  67. addField("MenuItemProfile", TypeGuiProfile, Offset(mMenuItemProfile, GuiMenuBarCtrl));
  68. addField("backgroundProfile", TypeGuiProfile, Offset(mBackgroundProfile, GuiMenuBarCtrl));
  69. addField("constantThumbHeight", TypeBool, Offset(mUseConstantHeightThumb, GuiMenuBarCtrl));
  70. addField("scrollBarThickness", TypeS32, Offset(mScrollBarThickness, GuiMenuBarCtrl));
  71. addField("showArrowButtons", TypeBool, Offset(mShowArrowButtons, GuiMenuBarCtrl));
  72. addField("thumbProfile", TypeGuiProfile, Offset(mThumbProfile, GuiMenuBarCtrl));
  73. addField("trackProfile", TypeGuiProfile, Offset(mTrackProfile, GuiMenuBarCtrl));
  74. addField("arrowProfile", TypeGuiProfile, Offset(mArrowProfile, GuiMenuBarCtrl));
  75. }
  76. void GuiMenuBarCtrl::inspectPostApply()
  77. {
  78. ApplyMenuSettings();
  79. calculateMenus();
  80. Parent::inspectPostApply();
  81. }
  82. void GuiMenuBarCtrl::childResized(GuiControl *child)
  83. {
  84. calculateMenus();
  85. }
  86. void GuiMenuBarCtrl::resize(const Point2I &newPosition, const Point2I &newExtent)
  87. {
  88. Parent::resize(Point2I(0, 0), newExtent);
  89. }
  90. void GuiMenuBarCtrl::onChildAdded(GuiControl *child)
  91. {
  92. GuiMenuItemCtrl *menu = dynamic_cast<GuiMenuItemCtrl*>(child);
  93. if (!menu)
  94. {
  95. Con::warnf("GuiMenuBarCtrl::onChildAdded - Only a GuiMenuItemCtrl can be added to a GuiMenuBarCtrl! Child will not be added.");
  96. SimObject *simObj = reinterpret_cast<SimObject*>(child);
  97. removeObject(simObj);
  98. return;
  99. }
  100. menu->mMenuBar = this;
  101. menu->setControlProfile(mMenuProfile);
  102. calculateMenus();
  103. if (size() > 1)
  104. {
  105. iterator i = end() - 2;
  106. GuiMenuItemCtrl *ctrl = static_cast<GuiMenuItemCtrl *>(*i);
  107. menu->mPrevItem = ctrl;
  108. ctrl->mNextItem = menu;
  109. }
  110. }
  111. void GuiMenuBarCtrl::onChildRemoved(SimObject *child)
  112. {
  113. calculateMenus();
  114. }
  115. void GuiMenuBarCtrl::calculateMenus()
  116. {
  117. RectI innerRect = getInnerRect();
  118. iterator i;
  119. S32 length = 0;
  120. for (i = begin(); i != end(); i++)
  121. {
  122. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  123. if (ctrl->isVisible())
  124. {
  125. S32 width = ctrl->mProfile->getFont(mFontSizeAdjust)->getStrWidth((const UTF8*)ctrl->getText());
  126. Point2I innerExtent = Point2I(width, 0);
  127. Point2I outerExtent = getOuterExtent(innerExtent, NormalState, ctrl->mProfile);
  128. ctrl->mBounds.set(Point2I(length, 0), Point2I(outerExtent.x, innerRect.extent.y));
  129. length += ctrl->getExtent().x;
  130. }
  131. }
  132. }
  133. GuiControl* GuiMenuBarCtrl::findHitControl(const Point2I &pt, S32 initialLayer)
  134. {
  135. return this;
  136. }
  137. GuiMenuItemCtrl* GuiMenuBarCtrl::findHitMenu(const Point2I &pt)
  138. {
  139. iterator i;
  140. for (i = begin(); i != end(); i++)
  141. {
  142. GuiMenuItemCtrl *ctrl = static_cast<GuiMenuItemCtrl *>(*i);
  143. if (ctrl && ctrl->isVisible() && ctrl->isActive())
  144. {
  145. if (ctrl->mBounds.pointInRect(pt - ctrl->mRenderInsetLT))
  146. {
  147. return ctrl;
  148. }
  149. }
  150. }
  151. return NULL;
  152. }
  153. void GuiMenuBarCtrl::processHover(const GuiEvent &event)
  154. {
  155. GuiMenuItemCtrl *ctrl = findHitMenu(globalToLocalCoord(event.mousePoint));
  156. mUseKeyMode = false;
  157. setHoverTarget(ctrl);
  158. }
  159. void GuiMenuBarCtrl::setHoverTarget(GuiMenuItemCtrl *ctrl)
  160. {
  161. if (mHoverTarget != ctrl)
  162. {
  163. if (mHoverTarget != NULL)
  164. {
  165. mHoverTarget->mIsHover = false;
  166. }
  167. if(ctrl != NULL)
  168. {
  169. ctrl->mIsHover = true;
  170. }
  171. mHoverTarget = ctrl;
  172. }
  173. }
  174. void GuiMenuBarCtrl::onTouchMove(const GuiEvent &event)
  175. {
  176. processHover(event);
  177. }
  178. void GuiMenuBarCtrl::onTouchEnter(const GuiEvent &event)
  179. {
  180. processHover(event);
  181. }
  182. void GuiMenuBarCtrl::onTouchLeave(const GuiEvent &event)
  183. {
  184. if (mHoverTarget != NULL) {
  185. mHoverTarget->mIsHover = false;
  186. }
  187. mHoverTarget = NULL;
  188. clearFirstResponder();
  189. }
  190. void GuiMenuBarCtrl::onTouchDown(const GuiEvent &event)
  191. {
  192. GuiMenuItemCtrl *ctrl = findHitMenu(globalToLocalCoord(event.mousePoint));
  193. if (mOpenMenu != ctrl)
  194. {
  195. if (mOpenMenu != NULL)
  196. {
  197. mOpenMenu->closeMenu();
  198. }
  199. if (ctrl != NULL)
  200. {
  201. ctrl->mIsOpen = true;
  202. }
  203. mOpenMenu = ctrl;
  204. }
  205. openMenu();
  206. }
  207. void GuiMenuBarCtrl::onRender(Point2I offset, const RectI &updateRect)
  208. {
  209. RectI clipRect = dglGetClipRect();
  210. if (clipRect.extent.x != mBounds.extent.x || mBounds.point.x != 0 || mBounds.point.y != 0)
  211. {
  212. resize(Point2I(0, 0), Point2I(clipRect.extent.x, mBounds.extent.y));
  213. }
  214. RectI ctrlRect = applyMargins(offset, mBounds.extent, NormalState, mProfile);
  215. if (!ctrlRect.isValidRect())
  216. {
  217. return;
  218. }
  219. renderUniversalRect(ctrlRect, mProfile, NormalState);
  220. RectI fillRect = applyBorders(ctrlRect.point, ctrlRect.extent, NormalState, mProfile);
  221. RectI contentRect = applyPadding(fillRect.point, fillRect.extent, NormalState, mProfile);
  222. if (contentRect.isValidRect())
  223. {
  224. //Render the childen
  225. renderChildControls(offset, contentRect, updateRect);
  226. }
  227. }
  228. void GuiMenuBarCtrl::openMenu()
  229. {
  230. if (mOpenMenu == NULL)
  231. {
  232. return;
  233. }
  234. GuiCanvas *root = getRoot();
  235. AssertFatal(root, "GuiMenuBarCtrl::openMenu: Unable to optain the Canvas!");
  236. mBackground->mBounds.extent = root->mBounds.extent;
  237. mBackground->setControlProfile(mBackgroundProfile);
  238. ApplyMenuSettings();
  239. mBackground->openMenu();
  240. root->pushDialogControl(mBackground, 99);
  241. setFirstResponder();
  242. if (isMethod("onOpen"))
  243. Con::executef(this, 1, "onOpen");
  244. }
  245. void GuiMenuBarCtrl::closeMenu()
  246. {
  247. if (!mOpenMenu)
  248. return;
  249. mOpenMenu->closeMenu();
  250. mBackground->getRoot()->popDialogControl(mBackground);
  251. mOpenMenu->mIsOpen = false;
  252. mOpenMenu->mIsHover = false;
  253. mOpenMenu = NULL;
  254. if (isMethod("onClose"))
  255. Con::executef(this, 1, "onClose");
  256. }
  257. void GuiMenuBarCtrl::ApplyMenuSettings()
  258. {
  259. iterator i;
  260. for (i = begin(); i != end(); i++)
  261. {
  262. GuiMenuItemCtrl *ctrl = static_cast<GuiMenuItemCtrl *>(*i);
  263. ctrl->ApplyMenuSettings();
  264. ctrl->setControlProfile(mMenuProfile);
  265. }
  266. setUpdate();
  267. }
  268. void GuiMenuBarCtrl::setMenuActive(const char* name, bool isActive)
  269. {
  270. iterator i;
  271. for (i = begin(); i != end(); i++)
  272. {
  273. GuiMenuItemCtrl* ctrl = static_cast<GuiMenuItemCtrl*>(*i);
  274. StringTableEntry ctrlName = StringTable->insert(ctrl->getText(), true);
  275. StringTableEntry target = StringTable->insert(name, true);
  276. if (ctrlName == target)
  277. {
  278. ctrl->setActive(isActive);
  279. }
  280. ctrl->setMenuActive(target, isActive);
  281. ctrl->ApplyMenuSettings();
  282. ctrl->setControlProfile(mMenuProfile);
  283. }
  284. }
  285. void GuiMenuBarCtrl::acceleratorKeyPress(U32 index)
  286. {
  287. if (mUseKeyMode && mOpenMenu != NULL)
  288. {
  289. mUseKeyMode = true;
  290. GuiMenuItemCtrl *ctrl = mOpenMenu;
  291. closeMenu();
  292. mHoverTarget = ctrl;
  293. mHoverTarget->mIsHover = true;
  294. }
  295. else if (mUseKeyMode)
  296. {
  297. mUseKeyMode = false;
  298. if (mHoverTarget != NULL)
  299. {
  300. mHoverTarget->mIsHover = false;
  301. }
  302. mHoverTarget = NULL;
  303. clearFirstResponder();
  304. }
  305. else
  306. {
  307. mUseKeyMode = true;
  308. GuiMenuItemCtrl *newTarget = dynamic_cast<GuiMenuItemCtrl *>(first());
  309. setHoverTarget(newTarget);
  310. setFirstResponder();
  311. }
  312. }
  313. bool GuiMenuBarCtrl::onKeyDown(const GuiEvent &event)
  314. {
  315. if (!mVisible || !mActive || !mAwake)
  316. return false;
  317. if (mHoverTarget == NULL && mOpenMenu == NULL)
  318. {
  319. clearFirstResponder();
  320. return false;
  321. }
  322. if ((event.keyCode == KEY_ALT || event.keyCode == KEY_LALT || event.keyCode == KEY_RALT) && event.modifier == 0)
  323. {
  324. acceleratorKeyPress(0);
  325. return true;
  326. }
  327. else if (mOpenMenu != NULL)
  328. {
  329. mOpenMenu->onKeyDown(event);
  330. }
  331. else if (event.keyCode == KEY_RIGHT && event.modifier == 0)
  332. {
  333. mUseKeyMode = true;
  334. if (mHoverTarget->mNextItem != NULL)
  335. {
  336. setHoverTarget(mHoverTarget->mNextItem);
  337. if (!mHoverTarget->isVisible() || !mHoverTarget->isActive() || !mHoverTarget->mHasGoodChildren)
  338. {
  339. onKeyDown(event);
  340. }
  341. }
  342. else
  343. {
  344. GuiMenuItemCtrl *newTarget = dynamic_cast<GuiMenuItemCtrl *>(first());
  345. setHoverTarget(newTarget);
  346. if (!mHoverTarget->isVisible() || !mHoverTarget->isActive() || !mHoverTarget->mHasGoodChildren)
  347. {
  348. onKeyDown(event);
  349. }
  350. }
  351. return true;
  352. }
  353. else if (event.keyCode == KEY_LEFT && event.modifier == 0)
  354. {
  355. mUseKeyMode = true;
  356. if (mHoverTarget->mPrevItem != NULL)
  357. {
  358. setHoverTarget(mHoverTarget->mPrevItem);
  359. if (!mHoverTarget->isVisible() || !mHoverTarget->isActive() || !mHoverTarget->mHasGoodChildren)
  360. {
  361. onKeyDown(event);
  362. }
  363. }
  364. else
  365. {
  366. GuiMenuItemCtrl *newTarget = dynamic_cast<GuiMenuItemCtrl *>(last());
  367. setHoverTarget(newTarget);
  368. if (!mHoverTarget->isVisible() || !mHoverTarget->isActive() || !mHoverTarget->mHasGoodChildren)
  369. {
  370. onKeyDown(event);
  371. }
  372. }
  373. return true;
  374. }
  375. else if (event.keyCode == KEY_DOWN && event.modifier == 0)
  376. {
  377. mUseKeyMode = true;
  378. if (mOpenMenu == NULL && mHoverTarget->mDisplayType == GuiMenuItemCtrl::DisplayType::Menu && mHoverTarget->isVisible() && mHoverTarget->isActive() && mHoverTarget->mHasGoodChildren)
  379. {
  380. mOpenMenu = mHoverTarget;
  381. mOpenMenu->mIsHover = false;
  382. mOpenMenu->mIsOpen = true;
  383. openMenu();
  384. return true;
  385. }
  386. }
  387. return false;
  388. }
  389. bool GuiMenuBarCtrl::onWake()
  390. {
  391. if (!Parent::onWake())
  392. return false;
  393. if (mMenuProfile != NULL)
  394. mMenuProfile->incRefCount();
  395. if (mMenuItemProfile != NULL)
  396. mMenuItemProfile->incRefCount();
  397. if (mMenuContentProfile != NULL)
  398. mMenuContentProfile->incRefCount();
  399. if (mBackgroundProfile != NULL)
  400. mBackgroundProfile->incRefCount();
  401. if (mThumbProfile != NULL)
  402. mThumbProfile->incRefCount();
  403. if (mTrackProfile != NULL)
  404. mTrackProfile->incRefCount();
  405. if (mArrowProfile != NULL)
  406. mArrowProfile->incRefCount();
  407. return true;
  408. }
  409. void GuiMenuBarCtrl::onSleep()
  410. {
  411. Parent::onSleep();
  412. if (mMenuProfile != NULL)
  413. mMenuProfile->decRefCount();
  414. if (mMenuItemProfile != NULL)
  415. mMenuItemProfile->decRefCount();
  416. if (mMenuContentProfile != NULL)
  417. mMenuContentProfile->decRefCount();
  418. if (mBackgroundProfile != NULL)
  419. mBackgroundProfile->decRefCount();
  420. if (mThumbProfile != NULL)
  421. mThumbProfile->decRefCount();
  422. if (mTrackProfile != NULL)
  423. mTrackProfile->decRefCount();
  424. if (mArrowProfile != NULL)
  425. mArrowProfile->decRefCount();
  426. }
  427. void GuiMenuBarCtrl::setControlBackgroundProfile(GuiControlProfile* prof)
  428. {
  429. AssertFatal(prof, "GuiMenuBarCtrl::setControlBackgroundProfile: invalid background profile");
  430. if (prof == mBackgroundProfile)
  431. return;
  432. if (mAwake)
  433. mBackgroundProfile->decRefCount();
  434. mBackgroundProfile = prof;
  435. if (mAwake)
  436. mBackgroundProfile->incRefCount();
  437. ApplyMenuSettings();
  438. }
  439. void GuiMenuBarCtrl::setControlMenuProfile(GuiControlProfile* prof)
  440. {
  441. AssertFatal(prof, "GuiMenuBarCtrl::setControlMenuProfile: invalid profile");
  442. if (prof == mMenuProfile)
  443. return;
  444. if (mAwake)
  445. mMenuProfile->decRefCount();
  446. mMenuProfile = prof;
  447. if (mAwake)
  448. mMenuProfile->incRefCount();
  449. ApplyMenuSettings();
  450. setUpdate();
  451. }
  452. void GuiMenuBarCtrl::setControlMenuItemProfile(GuiControlProfile* prof)
  453. {
  454. AssertFatal(prof, "GuiMenuBarCtrl::setControlMenuItemProfile: invalid profile");
  455. if (prof == mMenuItemProfile)
  456. return;
  457. if (mAwake)
  458. mMenuItemProfile->decRefCount();
  459. mMenuItemProfile = prof;
  460. if (mAwake)
  461. mMenuItemProfile->incRefCount();
  462. ApplyMenuSettings();
  463. setUpdate();
  464. }
  465. void GuiMenuBarCtrl::setControlMenuContentProfile(GuiControlProfile* prof)
  466. {
  467. AssertFatal(prof, "GuiMenuBarCtrl::setControlMenuContentProfile: invalid profile");
  468. if (prof == mMenuContentProfile)
  469. return;
  470. if (mAwake)
  471. mMenuContentProfile->decRefCount();
  472. mMenuContentProfile = prof;
  473. if (mAwake)
  474. mMenuContentProfile->incRefCount();
  475. ApplyMenuSettings();
  476. }
  477. void GuiMenuBarCtrl::setControlThumbProfile(GuiControlProfile* prof)
  478. {
  479. AssertFatal(prof, "GuiMenuBarCtrl::setControlThumbProfile: invalid thumb profile");
  480. if (prof == mThumbProfile)
  481. return;
  482. if (mAwake)
  483. mThumbProfile->decRefCount();
  484. mThumbProfile = prof;
  485. if (mAwake)
  486. mThumbProfile->incRefCount();
  487. ApplyMenuSettings();
  488. }
  489. void GuiMenuBarCtrl::setControlTrackProfile(GuiControlProfile* prof)
  490. {
  491. AssertFatal(prof, "GuiMenuBarCtrl::setControlTrackProfile: invalid track profile");
  492. if (prof == mTrackProfile)
  493. return;
  494. if (mAwake)
  495. mTrackProfile->decRefCount();
  496. mTrackProfile = prof;
  497. if (mAwake)
  498. mTrackProfile->incRefCount();
  499. ApplyMenuSettings();
  500. }
  501. void GuiMenuBarCtrl::setControlArrowProfile(GuiControlProfile* prof)
  502. {
  503. AssertFatal(prof, "GuiMenuBarCtrl::setControlArrowProfile: invalid arrow profile");
  504. if (prof == mArrowProfile)
  505. return;
  506. if (mAwake)
  507. mArrowProfile->decRefCount();
  508. mArrowProfile = prof;
  509. if (mAwake)
  510. mArrowProfile->incRefCount();
  511. ApplyMenuSettings();
  512. }
  513. #pragma endregion
  514. #pragma region GuiMenuItemCtrl
  515. IMPLEMENT_CONOBJECT(GuiMenuItemCtrl);
  516. GuiMenuItemCtrl::GuiMenuItemCtrl()
  517. {
  518. mText = "Menu";
  519. mDisplayType = TextCommand;
  520. mActive = true;
  521. mIsOpen = false;
  522. mIsHover = false;
  523. mIsOn = false;
  524. mToggle = false;
  525. mRadio = false;
  526. mHasGoodChildren = true;
  527. mPrevItem = NULL;
  528. mNextItem = NULL;
  529. mOpenSubMenu = NULL;
  530. mScroll = NULL;
  531. mList = NULL;
  532. }
  533. void GuiMenuItemCtrl::initPersistFields()
  534. {
  535. SimObject::initPersistFields();
  536. addGroup("MenuItem");
  537. addField("Variable", TypeString, Offset(mConsoleVariable, GuiMenuItemCtrl));
  538. addField("Command", TypeString, Offset(mConsoleCommand, GuiMenuItemCtrl));
  539. addField("AltCommand", TypeString, Offset(mAltConsoleCommand, GuiMenuItemCtrl));
  540. addField("Accelerator", TypeString, Offset(mAcceleratorKey, GuiMenuItemCtrl));
  541. addField("Active", TypeBool, Offset(mActive, GuiMenuItemCtrl));
  542. addField("Visible", TypeBool, Offset(mVisible, GuiMenuItemCtrl));
  543. addProtectedField("IsOn", TypeBool, Offset(mIsOn, GuiMenuItemCtrl), &defaultProtectedSetFn, &defaultProtectedGetFn, &writeIsOn, "");
  544. addProtectedField("Toggle", TypeBool, Offset(mToggle, GuiMenuItemCtrl), &setToggle, &defaultProtectedGetFn, &writeToggle, "");
  545. addProtectedField("Radio", TypeS32, Offset(mRadio, GuiMenuItemCtrl), &setRadio, &defaultProtectedGetFn, &writeRadio, "");
  546. endGroup("MenuItem");
  547. addGroup("Localization");
  548. addField("langTableMod", TypeString, Offset(mLangTableName, GuiMenuItemCtrl));
  549. endGroup("Localization");
  550. addGroup("Text");
  551. addProtectedField("text", TypeCaseString, Offset(mText, GuiMenuItemCtrl), setTextProperty, getTextProperty, "");
  552. addField("textID", TypeString, Offset(mTextID, GuiMenuItemCtrl));
  553. endGroup("Text");
  554. }
  555. void GuiMenuItemCtrl::onAction()
  556. {
  557. if (!mActive || !mVisible)
  558. return;
  559. if (mDisplayType == TextCommand)
  560. {
  561. Parent::onAction();
  562. }
  563. else if (mDisplayType == Toggle || mDisplayType == Radio)
  564. {
  565. toggleControl();
  566. }
  567. }
  568. void GuiMenuItemCtrl::inspectPostApply()
  569. {
  570. Parent::inspectPostApply();
  571. if(mDisplayType == Menu)
  572. {
  573. checkForGoodChildren();
  574. }
  575. GuiMenuItemCtrl *menu = dynamic_cast<GuiMenuItemCtrl *>(getParent());
  576. if (menu != NULL)
  577. {
  578. menu->checkForGoodChildren();
  579. }
  580. }
  581. void GuiMenuItemCtrl::onChildAdded(GuiControl *child)
  582. {
  583. GuiMenuItemCtrl *subMenu = dynamic_cast<GuiMenuItemCtrl*>(child);
  584. if (!subMenu)
  585. {
  586. Con::warnf("GuiMenuItemCtrl::onChildAdded - Only a GuiMenuItemCtrl can be added to a GuiMenuItemCtrl! Child will not be added.");
  587. SimObject *simObj = reinterpret_cast<SimObject*>(child);
  588. removeObject(simObj);
  589. return;
  590. }
  591. subMenu->mMenuBar = this->mMenuBar;
  592. subMenu->setControlProfile(mMenuBar->mMenuItemProfile);
  593. if (dStrcmp(subMenu->getText(), "-") == 0)
  594. {
  595. subMenu->mDisplayType = Spacer;
  596. subMenu->setText(StringTable->EmptyString);
  597. }
  598. else if (subMenu->mToggle)
  599. {
  600. subMenu->mDisplayType = Toggle;
  601. }
  602. else if (subMenu->mRadio)
  603. {
  604. subMenu->mDisplayType = Radio;
  605. }
  606. if (size() > 1)
  607. {
  608. iterator i = end() - 2;
  609. GuiMenuItemCtrl *ctrl = static_cast<GuiMenuItemCtrl *>(*i);
  610. subMenu->mPrevItem = ctrl;
  611. ctrl->mNextItem = subMenu;
  612. }
  613. if (mDisplayType != Menu)
  614. {
  615. mDisplayType = Menu;
  616. if(mScroll == NULL)
  617. {
  618. mScroll = new GuiScrollCtrl();
  619. mScroll->setField("horizSizing","right");
  620. mScroll->setField("vertSizing","bottom");
  621. AssertFatal(mScroll, "GuiMenuItemCtrl::onChildAdded Failed to initialize GuiScrollCtrl!");
  622. }
  623. if(mList == NULL)
  624. {
  625. mList = new GuiMenuListCtrl(this);
  626. AssertFatal(mList, "GuiMenuItemCtrl::onChildAdded Failed to initialize GuiMenuListCtrl!");
  627. mScroll->addObject(mList);
  628. }
  629. }
  630. if (mDisplayType == Menu)
  631. {
  632. mList->updateSize();
  633. }
  634. checkForGoodChildren();
  635. }
  636. void GuiMenuItemCtrl::onChildRemoved(SimObject *child)
  637. {
  638. if (size() <= 0)
  639. {
  640. if (mDisplayType == Menu)
  641. {
  642. mDisplayType = TextCommand;
  643. mList->deleteObject();
  644. mList = NULL;
  645. mScroll->deleteObject();
  646. mScroll = NULL;
  647. }
  648. }
  649. checkForGoodChildren();
  650. }
  651. void GuiMenuItemCtrl::checkForGoodChildren()
  652. {
  653. if (mDisplayType != Menu)
  654. {
  655. mHasGoodChildren = true;
  656. return;
  657. }
  658. iterator iter;
  659. for (iter = begin(); iter != end(); iter++)
  660. {
  661. GuiMenuItemCtrl *ctrl = static_cast<GuiMenuItemCtrl *>(*iter);
  662. if (ctrl->isVisible() && ctrl->isActive() && ctrl->mDisplayType != Spacer)
  663. {
  664. mHasGoodChildren = true;
  665. return;
  666. }
  667. }
  668. mHasGoodChildren = false;
  669. }
  670. void GuiMenuItemCtrl::closeMenu()
  671. {
  672. if (mOpenSubMenu != NULL)
  673. {
  674. mOpenSubMenu->closeMenu();
  675. }
  676. mOpenSubMenu = NULL;
  677. mList->mHoveredItem = NULL;
  678. mMenuBar->mBackground->removeObject(mScroll);
  679. mIsOpen = false;
  680. }
  681. void GuiMenuItemCtrl::ApplyMenuSettings()
  682. {
  683. if (mDisplayType == Menu)
  684. {
  685. mScroll->setControlProfile(mMenuBar->mMenuContentProfile);
  686. mScroll->setControlThumbProfile(mMenuBar->mThumbProfile);
  687. mScroll->setControlArrowProfile(mMenuBar->mArrowProfile);
  688. mScroll->setControlTrackProfile(mMenuBar->mTrackProfile);
  689. mScroll->setField("hScrollBar", "AlwaysOff");
  690. mScroll->setField("vScrollBar", "Dynamic");
  691. mScroll->mUseConstantHeightThumb = mMenuBar->mUseConstantHeightThumb;
  692. mScroll->mScrollBarThickness = mMenuBar->mScrollBarThickness;
  693. mScroll->mShowArrowButtons = mMenuBar->mShowArrowButtons;
  694. }
  695. iterator i;
  696. for (i = begin(); i != end(); i++)
  697. {
  698. GuiMenuItemCtrl *ctrl = static_cast<GuiMenuItemCtrl *>(*i);
  699. ctrl->ApplyMenuSettings();
  700. }
  701. setControlProfile(mMenuBar->mMenuItemProfile);
  702. }
  703. void GuiMenuItemCtrl::onRender(Point2I offset, const RectI& updateRect)
  704. {
  705. GuiControlState currentState = GuiControlState::NormalState;
  706. if (!mActive || !mHasGoodChildren)
  707. currentState = GuiControlState::DisabledState;
  708. else if (mIsOpen)
  709. currentState = GuiControlState::SelectedState;
  710. else if (mIsHover)
  711. currentState = GuiControlState::HighlightState;
  712. RectI ctrlRect = applyMargins(offset, mBounds.extent, currentState, mProfile);
  713. renderUniversalRect(ctrlRect, mProfile, currentState);
  714. //Render Text
  715. dglSetBitmapModulation(getFontColor(mProfile, currentState));
  716. RectI fillRect = applyBorders(ctrlRect.point, ctrlRect.extent, currentState, mProfile);
  717. RectI contentRect = applyPadding(fillRect.point, fillRect.extent, currentState, mProfile);
  718. renderText(contentRect.point, contentRect.extent, mText, mProfile);
  719. }
  720. void GuiMenuItemCtrl::toggleControl()
  721. {
  722. if(mDisplayType == Toggle && !mIsOn)
  723. {
  724. mIsOn = true;
  725. Parent::onAction();
  726. }
  727. else if (mDisplayType == Toggle && mIsOn)
  728. {
  729. mIsOn = false;
  730. execAltConsoleCallback();
  731. }
  732. else if(mDisplayType == Radio && !mIsOn)
  733. {
  734. mIsOn = true;
  735. Parent::onAction();
  736. //Turn off other radio controls in this block
  737. turnOffPrevRadio();
  738. turnOffNextRadio();
  739. }
  740. }
  741. void GuiMenuItemCtrl::turnOffPrevRadio()
  742. {
  743. if (mPrevItem != NULL && mPrevItem->mDisplayType == Radio)
  744. {
  745. if(mPrevItem->mIsOn)
  746. {
  747. mPrevItem->mIsOn = false;
  748. mPrevItem->execAltConsoleCallback();
  749. }
  750. mPrevItem->turnOffPrevRadio();
  751. }
  752. }
  753. void GuiMenuItemCtrl::turnOffNextRadio()
  754. {
  755. if (mNextItem != NULL && mNextItem->mDisplayType == Radio)
  756. {
  757. if (mNextItem->mIsOn)
  758. {
  759. mNextItem->mIsOn = false;
  760. mNextItem->execAltConsoleCallback();
  761. }
  762. mNextItem->turnOffNextRadio();
  763. }
  764. }
  765. const char* GuiMenuItemCtrl::getHotKeyText()
  766. {
  767. if(mAcceleratorKey == StringTable->EmptyString || mDisplayType == Menu)
  768. return StringTable->EmptyString;
  769. char key[128];
  770. dSprintf(key, 128, "%s", mAcceleratorKey);
  771. char buffer[128];
  772. buffer[0] = '\0';
  773. char *tok;
  774. tok = dStrtok((char*)key, " -");
  775. while (tok != NULL)
  776. {
  777. if(buffer[0] != '\0')
  778. dStrcat(buffer, "+");
  779. dStrcat(buffer, ActionMap::swapCtrlForCmd(tok));
  780. tok = dStrtok(NULL, " -");
  781. }
  782. return StringTable->insert(buffer);
  783. }
  784. bool GuiMenuItemCtrl::onKeyDown(const GuiEvent &event)
  785. {
  786. if (mOpenSubMenu != NULL && (mOpenSubMenu->mList->mHoveredItem != NULL || mOpenSubMenu->mOpenSubMenu != NULL))
  787. {
  788. mOpenSubMenu->onKeyDown(event);
  789. }
  790. else if (event.keyCode == KEY_DOWN && event.modifier == 0)
  791. {
  792. mMenuBar->mUseKeyMode = true;
  793. if (mList->mHoveredItem == NULL || mList->mHoveredItem->mNextItem == NULL)
  794. {
  795. mList->setHoveredItem(dynamic_cast<GuiMenuItemCtrl *>(first()));
  796. if (!mList->mHoveredItem->isVisible() || !mList->mHoveredItem->isActive() || !mList->mHoveredItem->mHasGoodChildren)
  797. {
  798. onKeyDown(event);
  799. }
  800. }
  801. else if(mList->mHoveredItem->mNextItem->mDisplayType == DisplayType::Spacer || !mList->mHoveredItem->mNextItem->isVisible() || !mList->mHoveredItem->mNextItem->isActive() || !mList->mHoveredItem->mNextItem->mHasGoodChildren)
  802. {
  803. //To skip the spacer and still do all the needed checks, we'll move the hovered item to the next item and simulate a second down button push
  804. mList->setHoveredItem(mList->mHoveredItem->mNextItem);
  805. onKeyDown(event);
  806. }
  807. else
  808. {
  809. mList->setHoveredItem(mList->mHoveredItem->mNextItem);
  810. }
  811. return true;
  812. }
  813. else if (event.keyCode == KEY_UP && event.modifier == 0)
  814. {
  815. mMenuBar->mUseKeyMode = true;
  816. if (mList->mHoveredItem == NULL || mList->mHoveredItem->mPrevItem == NULL)
  817. {
  818. mList->setHoveredItem(dynamic_cast<GuiMenuItemCtrl *>(last()));
  819. if (!mList->mHoveredItem->isVisible() || !mList->mHoveredItem->isActive() || !mList->mHoveredItem->mHasGoodChildren)
  820. {
  821. onKeyDown(event);
  822. }
  823. }
  824. else if (mList->mHoveredItem->mPrevItem->mDisplayType == DisplayType::Spacer || !mList->mHoveredItem->mPrevItem->isVisible() || !mList->mHoveredItem->mPrevItem->isActive() || !mList->mHoveredItem->mPrevItem->mHasGoodChildren)
  825. {
  826. //To skip the spacer and still do all the needed checks, we'll move the hovered item to the prev item and simulate a second up button push
  827. mList->setHoveredItem(mList->mHoveredItem->mPrevItem);
  828. onKeyDown(event);
  829. }
  830. else
  831. {
  832. mList->setHoveredItem(mList->mHoveredItem->mPrevItem);
  833. }
  834. return true;
  835. }
  836. else if (event.keyCode == KEY_RIGHT && event.modifier == 0)
  837. {
  838. mMenuBar->mUseKeyMode = true;
  839. if (mList->mHoveredItem == NULL)
  840. {
  841. //loop to find the first valid menu item
  842. GuiMenuItemCtrl *foundCtrl = NULL;
  843. iterator iter;
  844. for (iter = begin(); iter != end(); iter++)
  845. {
  846. GuiMenuItemCtrl *ctrl = static_cast<GuiMenuItemCtrl *>(*iter);
  847. if (ctrl->isVisible() && ctrl->isActive() && ctrl->mDisplayType != Spacer && ctrl->mHasGoodChildren )
  848. {
  849. foundCtrl = ctrl;
  850. break;
  851. }
  852. }
  853. if (foundCtrl != NULL)
  854. {
  855. mList->setHoveredItem(foundCtrl);
  856. }
  857. }
  858. else if (mList->mHoveredItem == mOpenSubMenu)
  859. {
  860. mOpenSubMenu->onKeyDown(event);
  861. mList->mHoveredItem = NULL;
  862. }
  863. else if(mList->mHoveredItem->mDisplayType == DisplayType::Menu && mList->mHoveredItem->isVisible() && mList->mHoveredItem->isActive() && mList->mHoveredItem->mHasGoodChildren)
  864. {
  865. mList->processMenuItem(mList->mHoveredItem);
  866. mOpenSubMenu->onKeyDown(event);
  867. mList->mHoveredItem = NULL;
  868. }
  869. return true;
  870. }
  871. else if (event.keyCode == KEY_LEFT && event.modifier == 0)
  872. {
  873. mMenuBar->mUseKeyMode = true;
  874. //Is this the open sub menu?
  875. GuiMenuItemCtrl *menu = dynamic_cast<GuiMenuItemCtrl *>(getParent());
  876. if (menu != NULL && menu->mOpenSubMenu == this)
  877. {
  878. menu->mList->mHoveredItem = menu->mOpenSubMenu;
  879. closeMenu();
  880. menu->mOpenSubMenu = NULL;
  881. }
  882. return true;
  883. }
  884. else if (event.keyCode == KEY_RETURN && event.modifier == 0)
  885. {
  886. mMenuBar->mUseKeyMode = true;
  887. if (mList->mHoveredItem == NULL)
  888. {
  889. mList->setHoveredItem(dynamic_cast<GuiMenuItemCtrl *>(first()));
  890. }
  891. else
  892. {
  893. mList->processMenuItem(mList->mHoveredItem);
  894. }
  895. return true;
  896. }
  897. }
  898. void GuiMenuItemCtrl::setMenuActive(StringTableEntry name, bool isActive)
  899. {
  900. iterator i;
  901. for (i = begin(); i != end(); i++)
  902. {
  903. GuiMenuItemCtrl* ctrl = static_cast<GuiMenuItemCtrl*>(*i);
  904. StringTableEntry ctrlName = StringTable->insert(ctrl->getText(), true);
  905. if (ctrlName == name)
  906. {
  907. ctrl->setActive(isActive);
  908. }
  909. ctrl->setMenuActive(name, isActive);
  910. }
  911. }
  912. #pragma endregion
  913. #pragma region GuiMenuBGCtrl
  914. GuiMenuBGCtrl::GuiMenuBGCtrl(GuiMenuBarCtrl *ctrl)
  915. {
  916. mMenuBarCtrl = ctrl;
  917. mBounds.point.set(0, 0);
  918. setField("profile", "GuiDefaultProfile");
  919. }
  920. void GuiMenuBGCtrl::openMenu()
  921. {
  922. GuiMenuItemCtrl *menu = mMenuBarCtrl->mOpenMenu;
  923. if (menu == NULL)
  924. {
  925. return;
  926. }
  927. positionMenu(menu, Point2I(menu->mBounds.point.x, menu->mBounds.point.y + menu->mBounds.extent.y));
  928. addObject(menu->mScroll);
  929. }
  930. void GuiMenuBGCtrl::closeMenu()
  931. {
  932. mMenuBarCtrl->closeMenu();
  933. }
  934. void GuiMenuBGCtrl::openSubMenu(GuiMenuItemCtrl *subMenu)
  935. {
  936. if (subMenu == NULL)
  937. {
  938. return;
  939. }
  940. GuiMenuItemCtrl *menu = dynamic_cast<GuiMenuItemCtrl *>(subMenu->getParent());
  941. if (subMenu == menu->mOpenSubMenu)
  942. {
  943. return;
  944. }
  945. if(menu->mOpenSubMenu != NULL)
  946. {
  947. menu->mOpenSubMenu->closeMenu();
  948. }
  949. menu->mOpenSubMenu = subMenu;
  950. subMenu->mIsOpen = true;
  951. positionMenu(subMenu, Point2I(subMenu->mBounds.point.x + subMenu->mBounds.extent.x - 4, subMenu->mBounds.point.y - 4));
  952. addObject(subMenu->mScroll);
  953. menu->mList->mArmSubMenu = false;
  954. }
  955. void GuiMenuBGCtrl::onTouchMove(const GuiEvent &event)
  956. {
  957. processHover(event);
  958. }
  959. void GuiMenuBGCtrl::onTouchDragged(const GuiEvent &event)
  960. {
  961. processHover(event);
  962. }
  963. void GuiMenuBGCtrl::onTouchDown(const GuiEvent &event)
  964. {
  965. mMenuBarCtrl->closeMenu();
  966. }
  967. void GuiMenuBGCtrl::processHover(const GuiEvent &event)
  968. {
  969. if (mMenuBarCtrl->pointInControl(event.mousePoint))
  970. {
  971. GuiMenuItemCtrl *menu = mMenuBarCtrl->findHitMenu(globalToLocalCoord(event.mousePoint));
  972. if (menu != NULL && mMenuBarCtrl->mOpenMenu != menu)
  973. {
  974. mMenuBarCtrl->mOpenMenu->closeMenu();
  975. mMenuBarCtrl->mOpenMenu = menu;
  976. mMenuBarCtrl->mOpenMenu->mIsOpen = true;
  977. openMenu();
  978. setUpdate();
  979. }
  980. }
  981. mMenuBarCtrl->mUseKeyMode = false;
  982. }
  983. void GuiMenuBGCtrl::positionMenu(const GuiMenuItemCtrl *menu, const Point2I &topLeft)
  984. {
  985. menu->mList->updateSize();
  986. Point2I menuSize = menu->mList->mBounds.extent;
  987. RectI box = RectI(topLeft, menuSize);
  988. box.extent = getOuterExtent(box.extent, NormalState, menu->mScroll->mProfile);
  989. //Is it too tall?
  990. if ((box.point.y + box.extent.y) > mBounds.extent.y)
  991. {
  992. //can we move it up?
  993. if ((mMenuBarCtrl->mBounds.point.y + mMenuBarCtrl->mBounds.extent.y) < box.point.y)
  994. {
  995. box.point.y = getMax(mMenuBarCtrl->mBounds.point.y + mMenuBarCtrl->mBounds.extent.y, mBounds.extent.y - box.extent.y);
  996. }
  997. //Is it still too tall?
  998. if ((box.point.y + box.extent.y) > mBounds.extent.y)
  999. {
  1000. box.extent.y = mBounds.extent.y - box.point.y;
  1001. box.extent.x += menu->mScroll->mScrollBarThickness;
  1002. }
  1003. }
  1004. //Is it too far to the right?
  1005. if ((box.point.x + box.extent.x) > mBounds.extent.x)
  1006. {
  1007. box.point.x = mBounds.extent.x - box.extent.x;
  1008. }
  1009. menu->mScroll->mBounds.extent = box.extent;
  1010. menu->mScroll->mBounds.point = box.point;
  1011. }
  1012. #pragma endregion
  1013. #pragma region GuiMenuListCtrl
  1014. GuiMenuListCtrl::GuiMenuListCtrl(GuiMenuItemCtrl *ctrl)
  1015. {
  1016. mMenu = ctrl;
  1017. mBounds.point.set(0,0);
  1018. mHoveredItem = NULL;
  1019. mEnterItemTime = 0;
  1020. mSubMenuStallTime = 500;
  1021. }
  1022. void GuiMenuListCtrl::onTouchMove(const GuiEvent &event)
  1023. {
  1024. Parent::onTouchMove(event);
  1025. mMenu->mMenuBar->mUseKeyMode = false;
  1026. }
  1027. GuiMenuItemCtrl* GuiMenuListCtrl::GetHitItem(const Point2I &pt)
  1028. {
  1029. iterator iter;
  1030. for (iter = mMenu->begin(); iter != mMenu->end(); iter++)
  1031. {
  1032. GuiMenuItemCtrl *ctrl = static_cast<GuiMenuItemCtrl *>(*iter);
  1033. if (!ctrl->mVisible) continue;
  1034. if (ctrl->pointInControl(pt))
  1035. {
  1036. return ctrl;
  1037. }
  1038. }
  1039. return nullptr;
  1040. }
  1041. void GuiMenuListCtrl::onPreRender()
  1042. {
  1043. U32 currentTime = Platform::getVirtualMilliseconds();
  1044. if (mArmSubMenu && mHoveredItem != NULL && mHoveredItem != mMenu->mOpenSubMenu && (currentTime - mEnterItemTime) > mSubMenuStallTime)
  1045. {
  1046. //Close an open sub menu
  1047. if (mMenu->mOpenSubMenu != NULL)
  1048. {
  1049. mMenu->mOpenSubMenu->closeMenu();
  1050. mMenu->mOpenSubMenu = NULL;
  1051. }
  1052. //Open a sub menu
  1053. if (mHoveredItem->mDisplayType == GuiMenuItemCtrl::DisplayType::Menu)
  1054. {
  1055. processMenuItem(mHoveredItem);
  1056. }
  1057. }
  1058. }
  1059. void GuiMenuListCtrl::onRender(Point2I offset, const RectI &updateRect)
  1060. {
  1061. bool foundHoveredItem = false;
  1062. iterator iter;
  1063. S32 h = 0;
  1064. for (iter = mMenu->begin(); iter != mMenu->end(); iter++)
  1065. {
  1066. GuiMenuItemCtrl *ctrl = static_cast<GuiMenuItemCtrl *>(*iter);
  1067. if (!ctrl->mVisible) continue;
  1068. S32 top = h;
  1069. S32 bottom = h;
  1070. if (ctrl->mDisplayType == GuiMenuItemCtrl::DisplayType::Spacer)
  1071. {
  1072. bottom += mSpacerSize.y;
  1073. }
  1074. else
  1075. {
  1076. bottom += mItemSize.y;
  1077. }
  1078. // Only render visible items
  1079. if (bottom + offset.y < updateRect.point.y)
  1080. continue;
  1081. // Break out once we're no longer in visible item range
  1082. if (top + offset.y >= updateRect.point.y + updateRect.extent.y)
  1083. break;
  1084. RectI itemRect;
  1085. if (ctrl->mDisplayType == GuiMenuItemCtrl::DisplayType::Spacer)
  1086. {
  1087. itemRect = RectI(offset.x, offset.y + top, mSpacerSize.x, mSpacerSize.y);
  1088. }
  1089. else
  1090. {
  1091. itemRect = RectI(offset.x, offset.y + top, mItemSize.x, mItemSize.y);
  1092. }
  1093. // Render our item
  1094. foundHoveredItem |= onRenderItem(itemRect, ctrl);
  1095. h = bottom;
  1096. }
  1097. if (!foundHoveredItem)
  1098. {
  1099. mHoveredItem = NULL;
  1100. }
  1101. }
  1102. bool GuiMenuListCtrl::onRenderItem(RectI &itemRect, GuiMenuItemCtrl *item)
  1103. {
  1104. GuiControlProfile *profile = item->mProfile;
  1105. if (!profile)
  1106. return false;
  1107. if (item->mBounds != itemRect)
  1108. {
  1109. item->mBounds = itemRect;
  1110. }
  1111. Point2I cursorPt = Point2I(0, 0);
  1112. GuiCanvas *root = getRoot();
  1113. if (root)
  1114. {
  1115. cursorPt = root->getCursorPos();
  1116. }
  1117. //is this item being hovered?
  1118. bool foundHoveredItem = false;
  1119. if (mMenu->mMenuBar->mUseKeyMode && mHoveredItem == item)
  1120. {
  1121. foundHoveredItem = true;
  1122. }
  1123. else if (!mMenu->mMenuBar->mUseKeyMode && itemRect.pointInRect(cursorPt) && (mMenu->mOpenSubMenu == NULL || !mMenu->mOpenSubMenu->mScroll->mBounds.pointInRect(cursorPt)))
  1124. {
  1125. if (mHoveredItem != item)
  1126. {
  1127. setHoveredItem(item);
  1128. }
  1129. foundHoveredItem = true;
  1130. }
  1131. //Get the current state
  1132. GuiControlState currentState = GuiControlState::NormalState;
  1133. if (!item->mActive || !item->mHasGoodChildren)
  1134. currentState = GuiControlState::DisabledState;
  1135. else if (item->mDisplayType == GuiMenuItemCtrl::DisplayType::Spacer)
  1136. currentState = GuiControlState::SelectedState;
  1137. else if (mHoveredItem == item ||
  1138. (item->mIsOpen && (mMenu->mMenuBar->mUseKeyMode || !mMenu->mScroll->mBounds.pointInRect(cursorPt) || mMenu->mOpenSubMenu->mScroll->mBounds.pointInRect(cursorPt))))
  1139. currentState = GuiControlState::HighlightState;
  1140. RectI ctrlRect = applyMargins(itemRect.point, itemRect.extent, currentState, profile);
  1141. if (!ctrlRect.isValidRect())
  1142. {
  1143. return foundHoveredItem;
  1144. }
  1145. renderUniversalRect(ctrlRect, profile, currentState);
  1146. //Render Text
  1147. dglSetBitmapModulation(profile->getFontColor(currentState));
  1148. RectI fillRect = applyBorders(ctrlRect.point, ctrlRect.extent, currentState, profile);
  1149. RectI contentRect = applyPadding(fillRect.point, fillRect.extent, currentState, profile);
  1150. if(contentRect.isValidRect())
  1151. {
  1152. //Left Icon
  1153. RectI leftIconRect = RectI(itemRect.point, Point2I(itemRect.extent.y, itemRect.extent.y));
  1154. if(item->mDisplayType == GuiMenuItemCtrl::DisplayType::Toggle)
  1155. {
  1156. ColorI itemColor = item->mIsOn ? profile->getFillColor(HighlightState) : profile->getFillColor(NormalState);
  1157. S32 size = profile->getFont(mFontSizeAdjust)->getHeight();
  1158. renderColorBullet(leftIconRect, itemColor, getMin(size, 16));
  1159. }
  1160. else if (item->mDisplayType == GuiMenuItemCtrl::DisplayType::Radio)
  1161. {
  1162. ColorI itemColor = item->mIsOn ? profile->getFillColor(HighlightState) : profile->getFillColor(NormalState);
  1163. S32 size = profile->getFont(mFontSizeAdjust)->getHeight();
  1164. renderColorBullet(leftIconRect, itemColor, getMin(size, 16), true);
  1165. }
  1166. //Right Icon
  1167. RectI rightIconRect = RectI(Point2I(itemRect.point.x + itemRect.extent.x - itemRect.extent.y, itemRect.point.y), leftIconRect.extent);
  1168. if (item->mDisplayType == GuiMenuItemCtrl::DisplayType::Menu)
  1169. {
  1170. S32 size = (profile->getFont(mFontSizeAdjust)->getHeight() / 2);
  1171. rightIconRect.inset(2, 0);
  1172. ColorI color = ColorI(profile->getFontColor(currentState));
  1173. renderTriangleIcon(rightIconRect, color, GuiDirection::Right, size);
  1174. }
  1175. //Text Space
  1176. RectI textRect = RectI(itemRect.point.x + itemRect.extent.y, itemRect.point.y, itemRect.extent.x - (2 * itemRect.extent.y), itemRect.extent.y);
  1177. //Command Description
  1178. profile->mAlignment = AlignmentType::LeftAlign;
  1179. renderText(textRect.point, textRect.extent, item->getText(), profile);
  1180. //Hot Keys!!
  1181. profile->mAlignment = AlignmentType::RightAlign;
  1182. renderText(textRect.point, textRect.extent, item->getHotKeyText(), profile);
  1183. }
  1184. return foundHoveredItem;
  1185. }
  1186. void GuiMenuListCtrl::updateSize()
  1187. {
  1188. if(mMenu->size() == 0)
  1189. {
  1190. resize(mBounds.extent, Point2I(100, 20));
  1191. return;
  1192. }
  1193. GuiMenuItemCtrl *child = dynamic_cast<GuiMenuItemCtrl *>(mMenu->first());
  1194. if (!child)
  1195. return;
  1196. GuiControlProfile *profile = child->mProfile;
  1197. if (!profile)
  1198. return;
  1199. GFont *font = profile->getFont(mFontSizeAdjust);
  1200. Point2I contentSize = Point2I(10, font->getHeight() + 2);
  1201. Point2I spacerSize = Point2I(10, 0);
  1202. // Find the maximum width cell:
  1203. mItemCount = 0;
  1204. mSpacerCount = 0;
  1205. S32 maxWidth = 1;
  1206. iterator i;
  1207. for (i = mMenu->begin(); i != mMenu->end(); i++)
  1208. {
  1209. GuiMenuItemCtrl *ctrl = static_cast<GuiMenuItemCtrl *>(*i);
  1210. if(!ctrl->mVisible) continue;
  1211. S32 width = font->getStrWidth(ctrl->getText());
  1212. if(ctrl->mDisplayType != GuiMenuItemCtrl::DisplayType::Menu && ctrl->mDisplayType != GuiMenuItemCtrl::DisplayType::Spacer)
  1213. {
  1214. StringTableEntry hotKey = ctrl->getHotKeyText();
  1215. if(hotKey != StringTable->EmptyString)
  1216. {
  1217. width += font->getStrWidth(hotKey) + 30;
  1218. }
  1219. }
  1220. if (width > maxWidth)
  1221. maxWidth = width;
  1222. if (ctrl->mDisplayType == GuiMenuItemCtrl::DisplayType::Spacer)
  1223. {
  1224. mSpacerCount++;
  1225. }
  1226. else
  1227. {
  1228. mItemCount++;
  1229. }
  1230. }
  1231. contentSize.x = maxWidth;
  1232. spacerSize.x = contentSize.x;
  1233. mItemSize = this->getOuterExtent(contentSize, NormalState, profile);
  1234. mSpacerSize = this->getOuterExtent(spacerSize, SelectedState, profile);
  1235. //Add a square of space at both ends for icons
  1236. mItemSize.x += (2 * mItemSize.y);
  1237. mSpacerSize.x += (2 * mItemSize.y);
  1238. Point2I newExtent = Point2I(mItemSize.x, (mItemSize.y * mItemCount) + (mSpacerSize.y * mSpacerCount));
  1239. resize(mBounds.point, newExtent);
  1240. }
  1241. void GuiMenuListCtrl::onTouchDown(const GuiEvent &) { }
  1242. void GuiMenuListCtrl::onTouchUp(const GuiEvent &event)
  1243. {
  1244. GuiMenuItemCtrl *ctrl = GetHitItem(event.mousePoint);
  1245. if (ctrl != nullptr )
  1246. {
  1247. processMenuItem(ctrl);
  1248. }
  1249. }
  1250. void GuiMenuListCtrl::processMenuItem(GuiMenuItemCtrl *ctrl)
  1251. {
  1252. if(!ctrl->mActive || ctrl->mDisplayType == GuiMenuItemCtrl::DisplayType::Spacer || !ctrl->mHasGoodChildren)
  1253. return;
  1254. ctrl->onAction();
  1255. GuiScrollCtrl* parent = dynamic_cast<GuiScrollCtrl *>(getParent());
  1256. if (parent)
  1257. {
  1258. GuiMenuBGCtrl* grandParent = dynamic_cast<GuiMenuBGCtrl *>(parent->getParent());
  1259. if (grandParent)
  1260. {
  1261. if (ctrl->mDisplayType != GuiMenuItemCtrl::DisplayType::Menu)
  1262. {
  1263. grandParent->closeMenu();
  1264. }
  1265. else
  1266. {
  1267. grandParent->openSubMenu(ctrl);
  1268. }
  1269. }
  1270. }
  1271. }
  1272. #pragma endregion