guiMenuBarCtrl.cc 36 KB

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