guiMenuBarCtrl.cc 37 KB

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