guiMenuBarCtrl.cc 36 KB

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