guiDropDownCtrl.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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 "gui/buttons/guiDropDownCtrl.h"
  23. #include "gui/guiCanvas.h"
  24. #include "guiDropDownCtrl_ScriptBinding.h"
  25. #pragma region GuiDropDownBGCtrl
  26. GuiDropDownBGCtrl::GuiDropDownBGCtrl(GuiDropDownCtrl *ctrl)
  27. {
  28. mDropDownCtrl = ctrl;
  29. mBounds.point.set(0, 0);
  30. setField("profile", "GuiDefaultProfile");
  31. }
  32. void GuiDropDownBGCtrl::onTouchUp(const GuiEvent &event)
  33. {
  34. mDropDownCtrl->closeDropDown();
  35. }
  36. #pragma endregion
  37. #pragma region GuiDropDownListBoxCtrl
  38. GuiDropDownListBoxCtrl::GuiDropDownListBoxCtrl(GuiDropDownCtrl *ctrl)
  39. {
  40. mDropDownCtrl = ctrl;
  41. mBounds.point.set(0, 0);
  42. caller = ctrl;
  43. }
  44. void GuiDropDownListBoxCtrl::addSelection(LBItem *item, S32 index)
  45. {
  46. Parent::addSelection(item, index);
  47. mDropDownCtrl->itemSelected();
  48. }
  49. void GuiDropDownListBoxCtrl::setCurSel(S32 index)
  50. {
  51. Parent::setCurSel(index);
  52. }
  53. void GuiDropDownListBoxCtrl::onTouchUp(const GuiEvent &event)
  54. {
  55. Parent::onTouchUp(event);
  56. mDropDownCtrl->closeDropDown();
  57. }
  58. #pragma endregion
  59. IMPLEMENT_CONOBJECT(GuiDropDownCtrl);
  60. GuiDropDownCtrl::GuiDropDownCtrl()
  61. {
  62. mMaxHeight = DEFAULT_MAX_HEIGHT;
  63. mBounds.extent.set(140, 24);
  64. mIsOpen = false;
  65. mActive = true;
  66. mText = StringTable->insert("none");
  67. mRendersChildren = false;
  68. mIsContainer = false;
  69. setField("profile", "GuiDropDownProfile");
  70. mBackground = new GuiDropDownBGCtrl(this);
  71. AssertFatal(mBackground, "GuiDropDownCtrl: Failed to initialize GuiDropDownBGCtrl!");
  72. mBackgroundProfile = mBackground->mProfile;
  73. mBackgroundProfile->incRefCount();
  74. mListBox = new GuiDropDownListBoxCtrl(this);
  75. AssertFatal(mListBox, "GuiDropDownCtrl: Failed to initialize GuiDropDownListBoxCtrl!");
  76. mListBox->setField("profile", "GuiListBoxProfile");
  77. mListBoxProfile = mListBox->mProfile;
  78. mListBoxProfile->incRefCount();
  79. mListBox->setField("FitParentWidth", "1");
  80. mListBox->setField("AllowMultipleSelections", "0");
  81. mScroll = new GuiScrollCtrl();
  82. AssertFatal(mScroll, "GuiDropDownCtrl: Failed to initialize GuiScrollCtrl!");
  83. mScroll->setField("profile", "GuiScrollProfile");
  84. mScroll->setField("horizSizing","right");
  85. mScroll->setField("vertSizing","bottom");
  86. mScrollProfile = mScroll->mProfile;
  87. mScrollProfile->incRefCount();
  88. mScroll->setField("thumbProfile", "GuiScrollThumbProfile");
  89. mThumbProfile = mScroll->mThumbProfile;
  90. mThumbProfile->incRefCount();
  91. mScroll->setField("arrowProfile", "GuiScrollArrowProfile");
  92. mArrowProfile = mScroll->mArrowProfile;
  93. mArrowProfile->incRefCount();
  94. mScroll->setField("trackProfile", "GuiScrollTrackProfile");
  95. mTrackProfile = mScroll->mTrackProfile;
  96. mTrackProfile->incRefCount();
  97. mScroll->setField("hScrollBar", "AlwaysOff");
  98. mScroll->setField("vScrollBar", "Dynamic");
  99. mScroll->setField("constantThumbHeight", "0");
  100. mUseConstantHeightThumb = false;
  101. mScroll->setField("scrollBarThickness", "12");
  102. mScrollBarThickness = DEFAULT_THICKNESS;
  103. mScroll->setField("showArrowButtons", "0");
  104. mShowArrowButtons = false;
  105. mScroll->addObject(mListBox);
  106. mBackground->addObject(mScroll);
  107. }
  108. void GuiDropDownCtrl::initPersistFields()
  109. {
  110. Parent::initPersistFields();
  111. addGroup("Drop Down");
  112. addField("maxHeight", TypeS32, Offset(mMaxHeight, GuiDropDownCtrl), &writeMaxHeightFn);
  113. addField("scrollProfile", TypeGuiProfile, Offset(mScrollProfile, GuiDropDownCtrl));
  114. addField("thumbProfile", TypeGuiProfile, Offset(mThumbProfile, GuiDropDownCtrl));
  115. addField("arrowProfile", TypeGuiProfile, Offset(mArrowProfile, GuiDropDownCtrl));
  116. addField("trackProfile", TypeGuiProfile, Offset(mTrackProfile, GuiDropDownCtrl));
  117. addField("listBoxProfile", TypeGuiProfile, Offset(mListBoxProfile, GuiDropDownCtrl));
  118. addField("backgroundProfile", TypeGuiProfile, Offset(mBackgroundProfile, GuiDropDownCtrl));
  119. addField("constantThumbHeight", TypeBool, Offset(mUseConstantHeightThumb, GuiDropDownCtrl), &writeConstantThumbHeightFn);
  120. addField("showArrowButtons", TypeBool, Offset(mShowArrowButtons, GuiDropDownCtrl), &writeShowArrowButtonsFn);
  121. addField("scrollBarThickness", TypeS32, Offset(mScrollBarThickness, GuiDropDownCtrl), &writeScrollBarThicknessFn);
  122. endGroup("Drop Down");
  123. }
  124. void GuiDropDownCtrl::onTouchUp(const GuiEvent &event)
  125. {
  126. if (!mActive)
  127. return;
  128. Parent::onTouchUp(event);
  129. mouseUnlock();
  130. if (!mIsOpen)
  131. {
  132. openDropDown();
  133. }
  134. else
  135. {
  136. closeDropDown();
  137. }
  138. }
  139. GuiControlState GuiDropDownCtrl::getCurrentState()
  140. {
  141. if (!mActive)
  142. return GuiControlState::DisabledState;
  143. else if (mDepressed || mIsOpen)
  144. return GuiControlState::SelectedState;
  145. else if (mMouseOver || isFirstResponder())
  146. return GuiControlState::HighlightState;
  147. else
  148. return GuiControlState::NormalState;
  149. }
  150. void GuiDropDownCtrl::onRender(Point2I offset, const RectI& updateRect)
  151. {
  152. GuiControlState currentState = getCurrentState();
  153. RectI ctrlRect = applyMargins(offset, mBounds.extent, currentState, mProfile);
  154. renderUniversalRect(ctrlRect, mProfile, currentState, getFillColor(currentState), true);
  155. //Get the content area
  156. dglSetBitmapModulation(getFontColor(mProfile, currentState));
  157. RectI fillRect = applyBorders(ctrlRect.point, ctrlRect.extent, currentState, mProfile);
  158. RectI contentRect = applyPadding(fillRect.point, fillRect.extent, currentState, mProfile);
  159. //Render the triangle
  160. RectI drawArea = RectI(contentRect.point.x + contentRect.extent.x - contentRect.extent.y, contentRect.point.y, contentRect.extent.y, contentRect.extent.y);
  161. ColorI color = ColorI(getFontColor(mProfile, currentState));
  162. renderTriangleIcon(drawArea, color, GuiDirection::Down, 8);
  163. contentRect.extent.x -= contentRect.extent.y;
  164. //Render the text
  165. S32 index = mListBox->getSelectedItem();
  166. if (index == -1)
  167. {
  168. renderText(contentRect.point, contentRect.extent, mText, mProfile);
  169. }
  170. else
  171. {
  172. if (mListBox->getItemHasColor(index))
  173. {
  174. //Draw the bullet
  175. RectI drawArea = RectI(contentRect.point.x, contentRect.point.y, contentRect.extent.y, contentRect.extent.y);
  176. ColorI color = ColorI(mListBox->getItemColor(index));
  177. renderColorBullet(drawArea, color, 5);
  178. contentRect.point.x += contentRect.extent.y;
  179. contentRect.extent.x -= contentRect.extent.y;
  180. }
  181. renderText(contentRect.point, contentRect.extent, mListBox->getItemText(index), mProfile);
  182. }
  183. if (isFirstResponder())
  184. {
  185. dglDrawRect(ctrlRect, mProfile->mCursorColor);
  186. }
  187. }
  188. bool GuiDropDownCtrl::onKeyDown(const GuiEvent &event)
  189. {
  190. //if the control is a dead end, don't process the input:
  191. if (!mVisible || !mActive || !mAwake)
  192. return false;
  193. //see if the key down is a <return> or not
  194. if (event.keyCode == KEY_RETURN && event.modifier == 0)
  195. {
  196. if(!mIsOpen)
  197. {
  198. openDropDown();
  199. }
  200. else
  201. {
  202. closeDropDown();
  203. }
  204. return true;
  205. }
  206. else if (mIsOpen)
  207. {
  208. return mListBox->onKeyDown(event);
  209. }
  210. return false;
  211. }
  212. void GuiDropDownCtrl::onAction() //called when the button is clicked.
  213. {
  214. if (!mActive)
  215. return;
  216. setUpdate();
  217. }
  218. void GuiDropDownCtrl::itemSelected()
  219. {
  220. if (mConsoleCommand[0])
  221. Con::evaluate(mConsoleCommand, false);
  222. }
  223. void GuiDropDownCtrl::openDropDown()
  224. {
  225. if (mIsOpen)
  226. return;
  227. GuiCanvas *root = getRoot();
  228. AssertFatal(root, "GuiDropDownCtrl::openDropDown: Unable to optain the Canvas!");
  229. mBackground->mBounds.extent = root->mBounds.extent;
  230. //Update all pass through values
  231. mBackground->setControlProfile(mBackgroundProfile);
  232. mListBox->setControlProfile(mListBoxProfile);
  233. mScroll->setControlProfile(mScrollProfile);
  234. mScroll->setControlThumbProfile(mThumbProfile);
  235. mScroll->setControlArrowProfile(mArrowProfile);
  236. mScroll->setControlTrackProfile(mTrackProfile);
  237. mScroll->mUseConstantHeightThumb = mUseConstantHeightThumb;
  238. mScroll->mScrollBarThickness = mScrollBarThickness;
  239. mScroll->mShowArrowButtons = mShowArrowButtons;
  240. //Set the size of the scroll control.
  241. mListBox->updateSize();
  242. S32 width = mClamp(mListBox->mBounds.extent.x, mBounds.extent.x, mBounds.extent.x * 2);
  243. S32 height = mClamp(mListBox->mBounds.extent.y, 10, mMaxHeight);
  244. Point2I pos = localToGlobalCoord(Point2I(0,0));
  245. //Is there enough space below?
  246. if ((height + pos.y + mBounds.extent.y) <= root->mBounds.extent.y)
  247. {
  248. pos.y += mBounds.extent.y;
  249. }
  250. else if (height <= pos.y) //Is there enough space above?
  251. {
  252. pos.y -= height;
  253. }
  254. else if (pos.y < (root->mBounds.extent.y - (pos.y + mBounds.extent.y))) //Is there more space below?
  255. {
  256. pos.y += mBounds.extent.y;
  257. height = root->mBounds.extent.y - pos.y;
  258. }
  259. else //There must be more space above
  260. {
  261. height = pos.y;
  262. pos.y = 0;
  263. }
  264. mScroll->resize(pos, Point2I(width, height));
  265. root->pushDialogControl(mBackground, 99);
  266. mListBox->ScrollToIndex(mListBox->getSelectedItem());
  267. mIsOpen = true;
  268. setFirstResponder();
  269. if (isMethod("onOpen"))
  270. Con::executef(this, 1, "onOpen");
  271. }
  272. void GuiDropDownCtrl::closeDropDown()
  273. {
  274. if(!mIsOpen)
  275. return;
  276. GuiCanvas *root = mBackground->getRoot();
  277. if (!root)
  278. {
  279. return;
  280. }
  281. root->popDialogControl(mBackground);
  282. mIsOpen = false;
  283. if (isMethod("onClose"))
  284. Con::executef(this, 1, "onClose");
  285. }
  286. bool GuiDropDownCtrl::onWake()
  287. {
  288. if (!Parent::onWake())
  289. return false;
  290. if (mBackgroundProfile != NULL)
  291. mBackgroundProfile->incRefCount();
  292. if (mListBoxProfile != NULL)
  293. mListBoxProfile->incRefCount();
  294. if (mScrollProfile != NULL)
  295. mScrollProfile->incRefCount();
  296. if (mThumbProfile != NULL)
  297. mThumbProfile->incRefCount();
  298. if (mTrackProfile != NULL)
  299. mTrackProfile->incRefCount();
  300. if (mArrowProfile != NULL)
  301. mArrowProfile->incRefCount();
  302. return true;
  303. }
  304. void GuiDropDownCtrl::onSleep()
  305. {
  306. Parent::onSleep();
  307. if (mBackgroundProfile != NULL)
  308. mBackgroundProfile->decRefCount();
  309. if (mListBoxProfile != NULL)
  310. mListBoxProfile->decRefCount();
  311. if (mScrollProfile != NULL)
  312. mScrollProfile->decRefCount();
  313. if (mThumbProfile != NULL)
  314. mThumbProfile->decRefCount();
  315. if (mTrackProfile != NULL)
  316. mTrackProfile->decRefCount();
  317. if (mArrowProfile != NULL)
  318. mArrowProfile->decRefCount();
  319. }
  320. void GuiDropDownCtrl::setControlBackgroundProfile(GuiControlProfile* prof)
  321. {
  322. AssertFatal(prof, "GuiDropDownCtrl::setControlBackgroundProfile: invalid background profile");
  323. if (prof == mBackgroundProfile)
  324. return;
  325. if (mAwake)
  326. mBackgroundProfile->decRefCount();
  327. mBackgroundProfile = prof;
  328. if (mAwake)
  329. mBackgroundProfile->incRefCount();
  330. }
  331. void GuiDropDownCtrl::setControlListBoxProfile(GuiControlProfile* prof)
  332. {
  333. AssertFatal(prof, "GuiDropDownCtrl::setControlListBoxProfile: invalid list box profile");
  334. if (prof == mListBoxProfile)
  335. return;
  336. if (mAwake)
  337. mListBoxProfile->decRefCount();
  338. mListBoxProfile = prof;
  339. if (mAwake)
  340. mListBoxProfile->incRefCount();
  341. }
  342. void GuiDropDownCtrl::setControlScrollProfile(GuiControlProfile* prof)
  343. {
  344. AssertFatal(prof, "GuiDropDownCtrl::setControlScrollProfile: invalid scroll profile");
  345. if (prof == mScrollProfile)
  346. return;
  347. if (mAwake)
  348. mScrollProfile->decRefCount();
  349. mScrollProfile = prof;
  350. if (mAwake)
  351. mScrollProfile->incRefCount();
  352. }
  353. void GuiDropDownCtrl::setControlThumbProfile(GuiControlProfile* prof)
  354. {
  355. AssertFatal(prof, "GuiDropDownCtrl::setControlThumbProfile: invalid thumb profile");
  356. if (prof == mThumbProfile)
  357. return;
  358. if (mAwake)
  359. mThumbProfile->decRefCount();
  360. mThumbProfile = prof;
  361. if (mAwake)
  362. mThumbProfile->incRefCount();
  363. }
  364. void GuiDropDownCtrl::setControlTrackProfile(GuiControlProfile* prof)
  365. {
  366. AssertFatal(prof, "GuiDropDownCtrl::setControlTrackProfile: invalid track profile");
  367. if (prof == mTrackProfile)
  368. return;
  369. if (mAwake)
  370. mTrackProfile->decRefCount();
  371. mTrackProfile = prof;
  372. if (mAwake)
  373. mTrackProfile->incRefCount();
  374. }
  375. void GuiDropDownCtrl::setControlArrowProfile(GuiControlProfile* prof)
  376. {
  377. AssertFatal(prof, "GuiDropDownCtrl::setControlArrowProfile: invalid arrow profile");
  378. if (prof == mArrowProfile)
  379. return;
  380. if (mAwake)
  381. mArrowProfile->decRefCount();
  382. mArrowProfile = prof;
  383. if (mAwake)
  384. mArrowProfile->incRefCount();
  385. }