guiScrollCtrl.cc 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158
  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/consoleTypes.h"
  23. #include "console/console.h"
  24. #include "graphics/gBitmap.h"
  25. #include "graphics/TextureManager.h"
  26. #include "io/resource/resourceManager.h"
  27. #include "platform/event.h"
  28. #include "graphics/dgl.h"
  29. #include "gui/guiArrayCtrl.h"
  30. #include "gui/containers/guiScrollCtrl.h"
  31. #include "gui/guiDefaultControlRender.h"
  32. #include "guiScrollCtrl_ScriptBinding.h"
  33. static EnumTable::Enums scrollBarEnums[] =
  34. {
  35. { GuiScrollCtrl::ScrollBarAlwaysOn, "alwaysOn" },
  36. { GuiScrollCtrl::ScrollBarAlwaysOff, "alwaysOff" },
  37. { GuiScrollCtrl::ScrollBarDynamic, "dynamic" },
  38. };
  39. static EnumTable gScrollBarTable(3, &scrollBarEnums[0]);
  40. IMPLEMENT_CONOBJECT(GuiScrollCtrl);
  41. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //
  42. GuiScrollCtrl::GuiScrollCtrl()
  43. {
  44. mBounds.extent.set(200,200);
  45. mScrollBarThickness = 14;
  46. mScrollBarDragTolerance = 130;
  47. mDepressed = false;
  48. curHitRegion = Content;
  49. mActive = true;
  50. mShowArrowButtons = true;
  51. mBaseThumbSize = (mScrollBarThickness * 2);
  52. mUseConstantHeightThumb = false;
  53. mForceVScrollBar = ScrollBarAlwaysOn;
  54. mForceHScrollBar = ScrollBarAlwaysOn;
  55. mThumbProfile = NULL;
  56. mTrackProfile = NULL;
  57. mArrowProfile = NULL;
  58. mScrollOffset.set(0, 0);
  59. mContentExt.set(200,200);
  60. mHorizSizing = horizResizeFill;
  61. mVertSizing = vertResizeFill;
  62. setField("thumbProfile", "GuiScrollThumbProfile");
  63. setField("arrowProfile", "GuiScrollArrowProfile");
  64. setField("trackProfile", "GuiScrollTrackProfile");
  65. setField("profile", "GuiScrollProfile");
  66. mEventBubbled = false;
  67. mCalcGuard = false;
  68. mResizeGuard = false;
  69. }
  70. void GuiScrollCtrl::initPersistFields()
  71. {
  72. Parent::initPersistFields();
  73. addGroup("GuiScrollCtrl");
  74. addField("hScrollBar", TypeEnum, Offset(mForceHScrollBar, GuiScrollCtrl), 1, &gScrollBarTable);
  75. addField("vScrollBar", TypeEnum, Offset(mForceVScrollBar, GuiScrollCtrl), 1, &gScrollBarTable);
  76. addField("constantThumbHeight", TypeBool, Offset(mUseConstantHeightThumb, GuiScrollCtrl));
  77. addField("scrollBarThickness", TypeS32, Offset(mScrollBarThickness, GuiScrollCtrl));
  78. addField("showArrowButtons", TypeBool, Offset(mShowArrowButtons, GuiScrollCtrl));
  79. addField("thumbProfile", TypeGuiProfile, Offset(mThumbProfile, GuiScrollCtrl));
  80. addField("trackProfile", TypeGuiProfile, Offset(mTrackProfile, GuiScrollCtrl));
  81. addField("arrowProfile", TypeGuiProfile, Offset(mArrowProfile, GuiScrollCtrl));
  82. endGroup("GuiScrollCtrl");
  83. }
  84. void GuiScrollCtrl::resize(const Point2I &newPos, const Point2I &newExt)
  85. {
  86. if(!mResizeGuard)
  87. {
  88. mResizeGuard = true;
  89. bool hasH = mHasHScrollBar;
  90. bool hasV = mHasVScrollBar;
  91. mCalcGuard = true;
  92. Parent::resize(newPos, newExt);
  93. mCalcGuard = false;
  94. computeSizes();
  95. if (hasH != mHasHScrollBar || hasV != mHasVScrollBar)
  96. {
  97. S32 deltaY = hasH != mHasHScrollBar ? (mHasHScrollBar ? mScrollBarThickness : -mScrollBarThickness) : 0;
  98. S32 deltaX = hasV != mHasVScrollBar ? (mHasVScrollBar ? mScrollBarThickness : -mScrollBarThickness) : 0;
  99. iterator i;
  100. for (i = begin(); i != end(); i++)
  101. {
  102. GuiControl* ctrl = static_cast<GuiControl*>(*i);
  103. ctrl->mRenderInsetRB = Point2I(ctrl->mRenderInsetRB.x + deltaX, ctrl->mRenderInsetRB.y + deltaY);
  104. ctrl->preventResizeModeFill();
  105. ctrl->preventResizeModeCenter();
  106. ctrl->parentResized(mBounds.extent - (ctrl->mRenderInsetLT + ctrl->mRenderInsetRB), mBounds.extent - (ctrl->mRenderInsetLT + ctrl->mRenderInsetRB));
  107. }
  108. mCalcGuard = true;
  109. Parent::resize(newPos, newExt);
  110. mCalcGuard = false;
  111. computeSizes();
  112. }
  113. mResizeGuard = false;
  114. }
  115. }
  116. void GuiScrollCtrl::childResized(GuiControl *child)
  117. {
  118. Parent::childResized(child);
  119. computeSizes();
  120. }
  121. void GuiScrollCtrl::addObject(SimObject* object)
  122. {
  123. //Fill is not supported inside a scroll control
  124. GuiControl* child = dynamic_cast<GuiControl*>(object);
  125. if (child)
  126. {
  127. child->preventResizeModeFill();
  128. child->preventResizeModeCenter();
  129. }
  130. Parent::addObject(object);
  131. computeSizes();
  132. }
  133. bool GuiScrollCtrl::onWake()
  134. {
  135. if (! Parent::onWake())
  136. return false;
  137. if (mThumbProfile != NULL)
  138. mThumbProfile->incRefCount();
  139. if (mTrackProfile != NULL)
  140. mTrackProfile->incRefCount();
  141. if (mArrowProfile != NULL)
  142. mArrowProfile->incRefCount();
  143. return true;
  144. }
  145. void GuiScrollCtrl::onSleep()
  146. {
  147. Parent::onSleep();
  148. if (mThumbProfile != NULL)
  149. mThumbProfile->decRefCount();
  150. if (mTrackProfile != NULL)
  151. mTrackProfile->decRefCount();
  152. if (mArrowProfile != NULL)
  153. mArrowProfile->decRefCount();
  154. }
  155. void GuiScrollCtrl::inspectPostApply()
  156. {
  157. Parent::inspectPostApply();
  158. computeSizes();
  159. }
  160. void GuiScrollCtrl::setControlThumbProfile(GuiControlProfile* prof)
  161. {
  162. AssertFatal(prof, "GuiScrollCtrl::setControlThumbProfile: invalid thumb profile");
  163. if (prof == mThumbProfile)
  164. return;
  165. if (mAwake)
  166. mThumbProfile->decRefCount();
  167. mThumbProfile = prof;
  168. if (mAwake)
  169. mThumbProfile->incRefCount();
  170. }
  171. void GuiScrollCtrl::setControlTrackProfile(GuiControlProfile* prof)
  172. {
  173. AssertFatal(prof, "GuiScrollCtrl::setControlTrackProfile: invalid track profile");
  174. if (prof == mTrackProfile)
  175. return;
  176. if (mAwake)
  177. mTrackProfile->decRefCount();
  178. mTrackProfile = prof;
  179. if (mAwake)
  180. mTrackProfile->incRefCount();
  181. }
  182. void GuiScrollCtrl::setControlArrowProfile(GuiControlProfile* prof)
  183. {
  184. AssertFatal(prof, "GuiScrollCtrl::setControlArrowProfile: invalid Arrow profile");
  185. if (prof == mArrowProfile)
  186. return;
  187. if (mAwake)
  188. mArrowProfile->decRefCount();
  189. mArrowProfile = prof;
  190. if (mAwake)
  191. mArrowProfile->incRefCount();
  192. }
  193. GuiControl* GuiScrollCtrl::findHitControl(const Point2I& pt, S32 initialLayer)
  194. {
  195. Point2I localPt = localToGlobalCoord(pt);
  196. if (mChildArea.pointInRect(localPt))
  197. {
  198. iterator i = end(); // find in z order (last to first)
  199. while (i != begin())
  200. {
  201. i--;
  202. GuiControl* ctrl = static_cast<GuiControl*>(*i);
  203. if (initialLayer >= 0 && ctrl->mLayer > initialLayer)
  204. {
  205. continue;
  206. }
  207. else if (ctrl->mVisible && ctrl->pointInControl(pt - ctrl->mRenderInsetLT) && ctrl->mUseInput)
  208. {
  209. Point2I ptemp = pt - (ctrl->mBounds.point + ctrl->mRenderInsetLT);
  210. GuiControl* hitCtrl = ctrl->findHitControl(ptemp);
  211. if (hitCtrl->mUseInput)
  212. return hitCtrl;
  213. }
  214. }
  215. }
  216. return this;
  217. }
  218. GuiScrollCtrl::Region GuiScrollCtrl::findHitRegion(const Point2I& pt)
  219. {
  220. if (mVBarEnabled && mHasVScrollBar)
  221. {
  222. if (mShowArrowButtons && mUpArrowRect.pointInRect(pt))
  223. return UpArrow;
  224. else if (mShowArrowButtons && mDownArrowRect.pointInRect(pt))
  225. return DownArrow;
  226. else if (mVTrackRect.pointInRect(pt))
  227. {
  228. S32 y = pt.y - mVTrackRect.point.y;
  229. if (y < mVThumbPos)
  230. return UpPage;
  231. else if (y < mVThumbPos + mVThumbSize)
  232. return VertThumb;
  233. else
  234. return DownPage;
  235. }
  236. }
  237. if (mHBarEnabled && mHasHScrollBar)
  238. {
  239. if (mShowArrowButtons && mLeftArrowRect.pointInRect(pt))
  240. return LeftArrow;
  241. else if (mShowArrowButtons && mRightArrowRect.pointInRect(pt))
  242. return RightArrow;
  243. else if (mHTrackRect.pointInRect(pt))
  244. {
  245. S32 x = pt.x - mHTrackRect.point.x;
  246. if (x < mHThumbPos)
  247. return LeftPage;
  248. else if (x < mHThumbPos + mHThumbSize)
  249. return HorizThumb;
  250. else
  251. return RightPage;
  252. }
  253. }
  254. return Content;
  255. }
  256. #pragma region CalculationFunctions
  257. void GuiScrollCtrl::computeSizes()
  258. {
  259. if (!mCalcGuard)//Prevent needless calcuations
  260. {
  261. calcContentExtents();
  262. mHBarEnabled = false;
  263. mVBarEnabled = false;
  264. mHasVScrollBar = (mForceVScrollBar == ScrollBarAlwaysOn);
  265. mHasHScrollBar = (mForceHScrollBar == ScrollBarAlwaysOn);
  266. setUpdate();
  267. if (calcChildExtents())
  268. {
  269. if (mChildExt.x > mContentExt.x && (mForceHScrollBar == ScrollBarDynamic))
  270. {
  271. mHasHScrollBar = true;
  272. }
  273. if (mChildExt.y > mContentExt.y && (mForceVScrollBar == ScrollBarDynamic))
  274. {
  275. mHasVScrollBar = true;
  276. // If Extent X Changed, check Horiz Scrollbar.
  277. if (mChildExt.x > mContentExt.x && !mHasHScrollBar && (mForceHScrollBar == ScrollBarDynamic))
  278. {
  279. mHasHScrollBar = true;
  280. }
  281. }
  282. if (mHasVScrollBar)
  283. mContentExt.x -= mScrollBarThickness;
  284. if (mHasHScrollBar)
  285. mContentExt.y -= mScrollBarThickness;
  286. // enable needed scroll bars
  287. if (mChildExt.x > mContentExt.x)
  288. mHBarEnabled = true;
  289. if (mChildExt.y > mContentExt.y)
  290. mVBarEnabled = true;
  291. //Are we now over-scrolled?
  292. calcScrollOffset();
  293. }
  294. // build all the rectangles and such...
  295. Point2I zero = mBounds.point.Zero;
  296. RectI ctrlRect = applyMargins(zero, mBounds.extent, NormalState, mProfile);
  297. RectI fillRect = applyBorders(ctrlRect.point, ctrlRect.extent, NormalState, mProfile);
  298. calcScrollRects(fillRect);
  299. calcThumbs();
  300. }
  301. }
  302. void GuiScrollCtrl::calcContentExtents()
  303. {
  304. RectI ctrlRect = applyMargins(mBounds.point, mBounds.extent, NormalState, mProfile);
  305. RectI fillRect = applyBorders(ctrlRect.point, ctrlRect.extent, NormalState, mProfile);
  306. RectI contentRect = applyPadding(fillRect.point, fillRect.extent, NormalState, mProfile);
  307. mContentExt = contentRect.extent;
  308. }
  309. bool GuiScrollCtrl::calcChildExtents()
  310. {
  311. if (!size())
  312. return false;
  313. mChildExt = Point2I(0,0);
  314. for (iterator itr = begin(); itr != end(); ++itr)
  315. {
  316. GuiControl* child = dynamic_cast<GuiControl*>(*itr);
  317. mChildExt.setMax(child->getExtent() + child->getPosition());
  318. }
  319. return true;
  320. }
  321. void GuiScrollCtrl::calcScrollOffset()
  322. {
  323. if ((mScrollOffset.x + mContentExt.x) > mChildExt.x)
  324. mScrollOffset.x = getMax(mChildExt.x - mContentExt.x, 0);
  325. if ((mScrollOffset.y + mContentExt.y) > mChildExt.y)
  326. mScrollOffset.y = getMax(mChildExt.y - mContentExt.y, 0);
  327. }
  328. void GuiScrollCtrl::calcScrollRects(RectI &fillRect)
  329. {
  330. if (mHasVScrollBar)
  331. {
  332. RectI vScrollRect = RectI(fillRect.point.x + fillRect.extent.x - mScrollBarThickness, fillRect.point.y, mScrollBarThickness, fillRect.extent.y);
  333. if (mHasHScrollBar)
  334. {
  335. vScrollRect.extent.y -= mScrollBarThickness;
  336. }
  337. mVTrackRect = RectI(vScrollRect);
  338. if (mShowArrowButtons)
  339. {
  340. mUpArrowRect = RectI(vScrollRect.point.x, vScrollRect.point.y, vScrollRect.extent.x, mScrollBarThickness);
  341. mDownArrowRect = RectI(vScrollRect.point.x, vScrollRect.point.y + vScrollRect.extent.y - mScrollBarThickness, vScrollRect.extent.x, mScrollBarThickness);
  342. mVTrackRect = RectI(vScrollRect.point.x, vScrollRect.point.y + mScrollBarThickness, vScrollRect.extent.x, vScrollRect.extent.y - (2 * mScrollBarThickness));
  343. }
  344. }
  345. if (mHasHScrollBar)
  346. {
  347. RectI hScrollRect = RectI(fillRect.point.x, fillRect.point.y + fillRect.extent.y - mScrollBarThickness, fillRect.extent.x, mScrollBarThickness);
  348. if (mHasVScrollBar)
  349. {
  350. hScrollRect.extent.x -= mScrollBarThickness;
  351. }
  352. mHTrackRect = RectI(hScrollRect);
  353. if (mShowArrowButtons)
  354. {
  355. mLeftArrowRect = RectI(hScrollRect.point.x, hScrollRect.point.y, mScrollBarThickness, hScrollRect.extent.y);
  356. mRightArrowRect = RectI(hScrollRect.point.x + hScrollRect.extent.x - mScrollBarThickness, hScrollRect.point.y, mScrollBarThickness, hScrollRect.extent.y);
  357. mHTrackRect = RectI(hScrollRect.point.x + mScrollBarThickness, hScrollRect.point.y, hScrollRect.extent.x - (2 * mScrollBarThickness), hScrollRect.extent.y);
  358. }
  359. }
  360. }
  361. void GuiScrollCtrl::calcThumbs()
  362. {
  363. if (mHBarEnabled)
  364. {
  365. S32 totalArea = mChildExt.x - mContentExt.x;
  366. if (totalArea <= 0)
  367. {
  368. mHBarEnabled = false;
  369. mHThumbSize = mBaseThumbSize;
  370. mHThumbPos = 0;
  371. }
  372. else
  373. {
  374. U32 trackSize = mHTrackRect.len_x();
  375. if (mUseConstantHeightThumb)
  376. mHThumbSize = mBaseThumbSize;
  377. else if(mChildExt.x > 0)
  378. mHThumbSize = getMax(mBaseThumbSize, S32((mContentExt.x * trackSize) / mChildExt.x));
  379. else
  380. mHThumbSize = mBaseThumbSize;
  381. F32 fraction = (F32)mScrollOffset.x / (F32)totalArea;
  382. mHThumbPos = roundf((trackSize - mHThumbSize) * fraction);
  383. }
  384. }
  385. if (mVBarEnabled)
  386. {
  387. S32 totalArea = mChildExt.y - mContentExt.y;
  388. if (totalArea <= 0)
  389. {
  390. mVBarEnabled = false;
  391. mVThumbSize = mBaseThumbSize;
  392. mVThumbPos = 0;
  393. }
  394. else
  395. {
  396. U32 trackSize = mVTrackRect.len_y();
  397. if (mUseConstantHeightThumb)
  398. mVThumbSize = mBaseThumbSize;
  399. else if(mChildExt.y > 0)
  400. mVThumbSize = getMax(mBaseThumbSize, S32((mContentExt.y * trackSize) / mChildExt.y));
  401. else
  402. mVThumbSize = mBaseThumbSize;
  403. F32 fraction = (F32)mScrollOffset.y / (F32)totalArea;
  404. mVThumbPos = roundf((trackSize - mVThumbSize) * fraction);
  405. }
  406. }
  407. }
  408. #pragma endregion
  409. #pragma region ScrollingFunctions
  410. void GuiScrollCtrl::scrollDelta(S32 deltaX, S32 deltaY)
  411. {
  412. mScrollOffset.x += deltaX;
  413. mScrollOffset.y += deltaY;
  414. mScrollOffset.setMin(mChildExt - mContentExt);
  415. mScrollOffset.setMax(mScrollOffset.Zero);
  416. calcThumbs();
  417. }
  418. void GuiScrollCtrl::scrollTo(S32 x, S32 y)
  419. {
  420. if(!size())
  421. return;
  422. setUpdate();
  423. if (x > mChildExt.x - mContentExt.x)
  424. x = mChildExt.x - mContentExt.x;
  425. if (x < 0)
  426. x = 0;
  427. if (y > mChildExt.y - mContentExt.y)
  428. y = mChildExt.y - mContentExt.y;
  429. if (y < 0)
  430. y = 0;
  431. mScrollOffset.set(x, y);
  432. calcThumbs();
  433. }
  434. void GuiScrollCtrl::scrollByRegion(Region reg)
  435. {
  436. setUpdate();
  437. if (!size())
  438. return;
  439. GuiControl* content = (GuiControl*)front();
  440. U32 rowHeight, columnWidth;
  441. U32 pageHeight, pageWidth;
  442. content->getScrollLineSizes(&rowHeight, &columnWidth);
  443. if (rowHeight >= (U32)mContentExt.y)
  444. pageHeight = 1;
  445. else
  446. pageHeight = mContentExt.y - rowHeight;
  447. if (columnWidth >= (U32)mContentExt.x)
  448. pageWidth = 1;
  449. else
  450. pageWidth = mContentExt.x - columnWidth;
  451. if (mVBarEnabled)
  452. {
  453. switch (reg)
  454. {
  455. case UpPage:
  456. scrollDelta(0, -(S32)pageHeight);
  457. break;
  458. case DownPage:
  459. scrollDelta(0, pageHeight);
  460. break;
  461. case UpArrow:
  462. scrollDelta(0, -(S32)rowHeight);
  463. break;
  464. case DownArrow:
  465. scrollDelta(0, rowHeight);
  466. break;
  467. case LeftArrow:
  468. case RightArrow:
  469. case LeftPage:
  470. case RightPage:
  471. case VertThumb:
  472. case HorizThumb:
  473. case Content:
  474. //Con::errorf("Unhandled case in GuiScrollCtrl::scrollByRegion");
  475. break;
  476. }
  477. }
  478. if (mHBarEnabled)
  479. {
  480. switch (reg)
  481. {
  482. case LeftPage:
  483. scrollDelta(-(S32)pageWidth, 0);
  484. break;
  485. case RightPage:
  486. scrollDelta(pageWidth, 0);
  487. break;
  488. case LeftArrow:
  489. scrollDelta(-(S32)columnWidth, 0);
  490. break;
  491. case RightArrow:
  492. scrollDelta(columnWidth, 0);
  493. break;
  494. case UpArrow:
  495. case DownArrow:
  496. case UpPage:
  497. case DownPage:
  498. case VertThumb:
  499. case HorizThumb:
  500. case Content:
  501. //Con::errorf("Unhandled case in GuiScrollCtrl::scrollByRegion");
  502. break;
  503. }
  504. }
  505. }
  506. void GuiScrollCtrl::scrollRectVisible(RectI rect)
  507. {
  508. // rect is passed in virtual client space
  509. if (rect.extent.x > mContentExt.x)
  510. rect.extent.x = mContentExt.x;
  511. if (rect.extent.y > mContentExt.y)
  512. rect.extent.y = mContentExt.y;
  513. // Determine the points bounding the requested rectangle
  514. Point2I rectUpperLeft = rect.point;
  515. Point2I rectLowerRight = rect.point + rect.extent;
  516. // Determine the points bounding the actual visible area...
  517. Point2I visUpperLeft = mScrollOffset;
  518. Point2I visLowerRight = mContentExt + mScrollOffset;
  519. Point2I delta(0, 0);
  520. // We basically try to make sure that first the top left of the given
  521. // rect is visible, and if it is, then that the bottom right is visible.
  522. // Make sure the rectangle is visible along the X axis...
  523. if (rectUpperLeft.x < visUpperLeft.x)
  524. delta.x = rectUpperLeft.x - visUpperLeft.x;
  525. else if (rectLowerRight.x > visLowerRight.x)
  526. delta.x = rectLowerRight.x - visLowerRight.x;
  527. // Make sure the rectangle is visible along the Y axis...
  528. if (rectUpperLeft.y < visUpperLeft.y)
  529. delta.y = rectUpperLeft.y - visUpperLeft.y;
  530. else if (rectLowerRight.y > visLowerRight.y)
  531. delta.y = rectLowerRight.y - visLowerRight.y;
  532. // If we had any changes, scroll, otherwise don't.
  533. if (delta.x || delta.y)
  534. scrollDelta(delta.x, delta.y);
  535. }
  536. #pragma endregion
  537. #pragma region Event_Processing
  538. void GuiScrollCtrl::onTouchMove(const GuiEvent& event)
  539. {
  540. curHitRegion = findHitRegion(globalToLocalCoord(event.mousePoint));
  541. GuiControl* parent = getParent();
  542. if (parent)
  543. parent->onTouchMove(event);
  544. }
  545. void GuiScrollCtrl::onTouchLeave(const GuiEvent &event)
  546. {
  547. if (!mDepressed)
  548. {
  549. curHitRegion = Content;
  550. }
  551. }
  552. bool GuiScrollCtrl::onKeyDown(const GuiEvent &event)
  553. {
  554. switch (event.keyCode)
  555. {
  556. case KEY_RIGHT:
  557. scrollByRegion(RightArrow);
  558. return true;
  559. case KEY_LEFT:
  560. scrollByRegion(LeftArrow);
  561. return true;
  562. case KEY_DOWN:
  563. scrollByRegion(DownArrow);
  564. return true;
  565. case KEY_UP:
  566. scrollByRegion(UpArrow);
  567. return true;
  568. case KEY_PAGE_UP:
  569. scrollByRegion(UpPage);
  570. return true;
  571. case KEY_PAGE_DOWN:
  572. scrollByRegion(DownPage);
  573. return true;
  574. }
  575. return Parent::onKeyDown(event);
  576. }
  577. void GuiScrollCtrl::onTouchDown(const GuiEvent &event)
  578. {
  579. mouseLock();
  580. setUpdate();
  581. Point2I curMousePos = globalToLocalCoord(event.mousePoint);
  582. curHitRegion = findHitRegion(curMousePos);
  583. mDepressed = true;
  584. mEventBubbled = false;
  585. // Set a 0.5 second delay before we start scrolling
  586. mLastUpdated = Platform::getVirtualMilliseconds() + 500;
  587. scrollByRegion(curHitRegion);
  588. if (curHitRegion == VertThumb)
  589. {
  590. mScrollOffsetAnchor = mScrollOffset;
  591. mThumbMouseDelta = curMousePos.y - mVThumbPos;
  592. }
  593. else if (curHitRegion == HorizThumb)
  594. {
  595. mScrollOffsetAnchor = mScrollOffset;
  596. mThumbMouseDelta = curMousePos.x - mHThumbPos;
  597. }
  598. else if (curHitRegion == Content)
  599. {
  600. GuiControl* parent = getParent();
  601. if (parent)
  602. {
  603. parent->onTouchDown(event);
  604. mEventBubbled = true;
  605. }
  606. }
  607. }
  608. void GuiScrollCtrl::onTouchUp(const GuiEvent &event)
  609. {
  610. mouseUnlock();
  611. setUpdate();
  612. if (mEventBubbled)
  613. {
  614. GuiControl* parent = getParent();
  615. if (parent)
  616. {
  617. parent->onTouchUp(event);
  618. }
  619. mEventBubbled = false;
  620. }
  621. curHitRegion = Content;
  622. mDepressed = false;
  623. }
  624. void GuiScrollCtrl::onTouchDragged(const GuiEvent &event)
  625. {
  626. Point2I curMousePos = globalToLocalCoord(event.mousePoint);
  627. setUpdate();
  628. if (mEventBubbled)
  629. {
  630. GuiControl* parent = getParent();
  631. if (parent)
  632. {
  633. parent->onTouchDragged(event);
  634. return;
  635. }
  636. }
  637. if ( (curHitRegion != VertThumb) && (curHitRegion != HorizThumb) )
  638. {
  639. Region hit = findHitRegion(curMousePos);
  640. if (hit != curHitRegion)
  641. mDepressed = false;
  642. else
  643. mDepressed = true;
  644. return;
  645. }
  646. // ok... if the mouse is 'near' the scroll bar, scroll with it
  647. // otherwise, snap back to the previous position.
  648. if (curHitRegion == VertThumb)
  649. {
  650. if (curMousePos.x >= mVTrackRect.point.x - mScrollBarDragTolerance &&
  651. curMousePos.x <= mVTrackRect.point.x + mVTrackRect.extent.x - 1 + mScrollBarDragTolerance &&
  652. curMousePos.y >= mVTrackRect.point.y - mScrollBarDragTolerance &&
  653. curMousePos.y <= mVTrackRect.point.y + mVTrackRect.extent.y - 1 + mScrollBarDragTolerance)
  654. {
  655. S32 newVThumbPos = curMousePos.y - mThumbMouseDelta;
  656. if(newVThumbPos != mVThumbPos)
  657. {
  658. S32 newVPos = (newVThumbPos) *
  659. (mChildExt.y - mContentExt.y) /
  660. (mVTrackRect.extent.y - mVThumbSize);
  661. scrollTo(mScrollOffset.x, newVPos);
  662. }
  663. }
  664. else
  665. scrollTo(mScrollOffset.x, mScrollOffsetAnchor.y);
  666. }
  667. else if (curHitRegion == HorizThumb)
  668. {
  669. if (curMousePos.x >= mHTrackRect.point.x - mScrollBarDragTolerance &&
  670. curMousePos.x <= mHTrackRect.point.x + mHTrackRect.extent.x - 1 + mScrollBarDragTolerance &&
  671. curMousePos.y >= mHTrackRect.point.y - mScrollBarDragTolerance &&
  672. curMousePos.y <= mHTrackRect.point.y + mHTrackRect.extent.y - 1 + mScrollBarDragTolerance)
  673. {
  674. S32 newHThumbPos = curMousePos.x - mThumbMouseDelta;
  675. if(newHThumbPos != mHThumbPos)
  676. {
  677. S32 newHPos = (newHThumbPos) *
  678. (mChildExt.x - mContentExt.x) /
  679. (mHTrackRect.extent.x - mHThumbSize);
  680. scrollTo(newHPos, mScrollOffset.y);
  681. }
  682. }
  683. else
  684. scrollTo(mScrollOffsetAnchor.x, mScrollOffset.y);
  685. }
  686. }
  687. void GuiScrollCtrl::onMouseWheelUp(const GuiEvent &event)
  688. {
  689. if ( !mAwake || !mVisible )
  690. return;
  691. Point2I previousPos = mScrollOffset;
  692. scrollByRegion((event.modifier & SI_CTRL) ? UpPage : UpArrow);
  693. // Tell the kids that the mouse moved (relatively):
  694. iterator itr;
  695. for ( itr = begin(); itr != end(); itr++ )
  696. {
  697. GuiControl* grandKid = static_cast<GuiControl*>( *itr );
  698. grandKid->onTouchMove( event );
  699. }
  700. // If no scrolling happened (already at the top), pass it on to the parent.
  701. GuiControl* parent = getParent();
  702. if (parent && (previousPos == mScrollOffset))
  703. parent->onMouseWheelUp(event);
  704. }
  705. void GuiScrollCtrl::onMouseWheelDown(const GuiEvent &event)
  706. {
  707. if ( !mAwake || !mVisible )
  708. return;
  709. Point2I previousPos = mScrollOffset;
  710. scrollByRegion((event.modifier & SI_CTRL) ? DownPage : DownArrow);
  711. // Tell the kids that the mouse moved (relatively):
  712. iterator itr;
  713. for ( itr = begin(); itr != end(); itr++ )
  714. {
  715. GuiControl* grandKid = static_cast<GuiControl *>( *itr );
  716. grandKid->onTouchMove( event );
  717. }
  718. // If no scrolling happened (already at the bottom), pass it on to the parent.
  719. GuiControl* parent = getParent();
  720. if (parent && (previousPos == mScrollOffset))
  721. parent->onMouseWheelDown(event);
  722. }
  723. bool GuiScrollCtrl::onMouseDownEditor(const GuiEvent& event, const Point2I& offset)
  724. {
  725. Point2I curMousePos = globalToLocalCoord(event.mousePoint);
  726. Region hitRegion = findHitRegion(curMousePos);
  727. if (hitRegion != Region::Content)
  728. {
  729. onTouchDown(event);
  730. return true;
  731. }
  732. return false;
  733. }
  734. bool GuiScrollCtrl::onMouseUpEditor(const GuiEvent& event, const Point2I& offset)
  735. {
  736. Point2I curMousePos = globalToLocalCoord(event.mousePoint);
  737. Region hitRegion = findHitRegion(curMousePos);
  738. if (hitRegion != Region::Content || mDepressed)
  739. {
  740. onTouchUp(event);
  741. return true;
  742. }
  743. return false;
  744. }
  745. bool GuiScrollCtrl::onMouseDraggedEditor(const GuiEvent& event, const Point2I& offset)
  746. {
  747. Point2I curMousePos = globalToLocalCoord(event.mousePoint);
  748. Region hitRegion = findHitRegion(curMousePos);
  749. if (hitRegion != Region::Content || mDepressed)
  750. {
  751. onTouchDragged(event);
  752. return true;
  753. }
  754. return false;
  755. }
  756. #pragma endregion
  757. #pragma region rendering
  758. void GuiScrollCtrl::onPreRender()
  759. {
  760. Parent::onPreRender();
  761. // Short circuit if not depressed to save cycles
  762. if( mDepressed != true )
  763. return;
  764. //default to one second, though it shouldn't be necessary
  765. U32 timeThreshold = 1000;
  766. // We don't want to scroll by pages at an interval the same as when we're scrolling
  767. // using the arrow buttons, so adjust accordingly.
  768. switch( curHitRegion )
  769. {
  770. case UpPage:
  771. case DownPage:
  772. case LeftPage:
  773. case RightPage:
  774. timeThreshold = 200;
  775. break;
  776. case UpArrow:
  777. case DownArrow:
  778. case LeftArrow:
  779. case RightArrow:
  780. timeThreshold = 20;
  781. break;
  782. default:
  783. // Neither a button or a page, don't scroll (shouldn't get here)
  784. return;
  785. break;
  786. };
  787. S32 timeElapsed = Platform::getVirtualMilliseconds() - mLastUpdated;
  788. if ( ( timeElapsed > 0 ) && ( timeElapsed > (S32)timeThreshold ) )
  789. {
  790. mLastUpdated = Platform::getVirtualMilliseconds();
  791. scrollByRegion(curHitRegion);
  792. }
  793. }
  794. void GuiScrollCtrl::onRender(Point2I offset, const RectI &updateRect)
  795. {
  796. RectI ctrlRect = applyMargins(offset, mBounds.extent, NormalState, mProfile);
  797. if (!ctrlRect.isValidRect())
  798. {
  799. return;
  800. }
  801. renderUniversalRect(ctrlRect, mProfile, NormalState);
  802. RectI fillRect = applyBorders(ctrlRect.point, ctrlRect.extent, NormalState, mProfile);
  803. RectI contentRect = applyScrollBarSpacing(fillRect.point, fillRect.extent);
  804. mChildArea.set(contentRect.point, contentRect.extent);
  805. renderVScrollBar(offset);
  806. renderHScrollBar(offset);
  807. if (contentRect.isValidRect())
  808. {
  809. renderChildControls(offset, contentRect, updateRect);
  810. }
  811. }
  812. RectI GuiScrollCtrl::applyScrollBarSpacing(Point2I offset, Point2I extent)
  813. {
  814. RectI contentRect = RectI(offset, extent);
  815. if (mHasVScrollBar && mHasHScrollBar)
  816. {
  817. contentRect.extent.x -= mScrollBarThickness;
  818. contentRect.extent.y -= mScrollBarThickness;
  819. }
  820. else if (mHasVScrollBar)
  821. {
  822. contentRect.extent.x -= mScrollBarThickness;
  823. }
  824. else if (mHasHScrollBar)
  825. {
  826. contentRect.extent.y -= mScrollBarThickness;
  827. }
  828. return contentRect;
  829. }
  830. GuiControlState GuiScrollCtrl::getRegionCurrentState(GuiScrollCtrl::Region region)
  831. {
  832. GuiControlState currentState = GuiControlState::NormalState;
  833. if (!mActive)
  834. {
  835. currentState = GuiControlState::DisabledState;
  836. }
  837. else if (curHitRegion == region && mDepressed)
  838. {
  839. currentState = GuiControlState::SelectedState;
  840. }
  841. else if (curHitRegion == region)
  842. {
  843. currentState = GuiControlState::HighlightState;
  844. }
  845. return currentState;
  846. }
  847. void GuiScrollCtrl::renderBorderedRectWithArrow(RectI& bounds, GuiControlProfile* profile, GuiControlState state, GuiDirection direction)
  848. {
  849. if (!profile)
  850. {
  851. return;
  852. }
  853. renderUniversalRect(bounds, profile, state);
  854. RectI ctrlRect = applyMargins(bounds.point, bounds.extent, state, profile);
  855. RectI fillRect = applyBorders(ctrlRect.point, ctrlRect.extent, state, profile);
  856. RectI contentRect = applyPadding(fillRect.point, fillRect.extent, state, profile);
  857. if (contentRect.isValidRect())
  858. {
  859. Point2I p1, p2, p3;
  860. switch (direction)
  861. {
  862. case GuiDirection::Up:
  863. p1 = Point2I(contentRect.point.x + (contentRect.extent.x / 2), contentRect.point.y);
  864. p2 = Point2I(contentRect.point.x, contentRect.point.y + contentRect.extent.y);
  865. p3 = Point2I(contentRect.point.x + contentRect.extent.x, contentRect.point.y + contentRect.extent.y);
  866. break;
  867. case GuiDirection::Down:
  868. p1 = Point2I(contentRect.point.x, contentRect.point.y);
  869. p2 = Point2I(contentRect.point.x + (contentRect.extent.x / 2), contentRect.point.y + contentRect.extent.y);
  870. p3 = Point2I(contentRect.point.x + contentRect.extent.x, contentRect.point.y);
  871. break;
  872. case GuiDirection::Left:
  873. p1 = Point2I(contentRect.point.x + contentRect.extent.x, contentRect.point.y);
  874. p2 = Point2I(contentRect.point.x, contentRect.point.y + (contentRect.extent.y/2));
  875. p3 = Point2I(contentRect.point.x + contentRect.extent.x, contentRect.point.y + contentRect.extent.y);
  876. break;
  877. case GuiDirection::Right:
  878. p1 = Point2I(contentRect.point.x, contentRect.point.y);
  879. p2 = Point2I(contentRect.point.x, contentRect.point.y + contentRect.extent.y);
  880. p3 = Point2I(contentRect.point.x + contentRect.extent.x, contentRect.point.y + (contentRect.extent.y/2));
  881. break;
  882. }
  883. dglDrawTriangleFill(p1, p2, p3, profile->getFontColor(state));
  884. }
  885. }
  886. void GuiScrollCtrl::renderVScrollBar(const Point2I& offset)
  887. {
  888. if(mHasVScrollBar && mTrackProfile && mThumbProfile)
  889. {
  890. if(mVBarEnabled)
  891. {
  892. if (mShowArrowButtons && mArrowProfile)
  893. {
  894. RectI upArrowExtent = RectI(mUpArrowRect.point + offset, mUpArrowRect.extent);
  895. renderBorderedRectWithArrow(upArrowExtent, mArrowProfile, getRegionCurrentState(Region::UpArrow), GuiDirection::Up);
  896. RectI downArrowExtent = RectI(mDownArrowRect.point + offset, mDownArrowRect.extent);
  897. renderBorderedRectWithArrow(downArrowExtent, mArrowProfile, getRegionCurrentState(Region::DownArrow), GuiDirection::Down);
  898. }
  899. RectI mVTrackExtent = RectI(mVTrackRect.point + offset, mVTrackRect.extent);
  900. renderUniversalRect(mVTrackExtent, mTrackProfile, GuiControlState::NormalState);
  901. //The Thumb
  902. GuiControlState thumbState = getRegionCurrentState(Region::VertThumb);
  903. RectI vThumb = RectI(mVTrackRect.point.x + offset.x, mVTrackRect.point.y + mVThumbPos + offset.y, mScrollBarThickness, mVThumbSize);
  904. RectI vThumbWithMargins = applyMargins(vThumb.point, vThumb.extent, thumbState, mThumbProfile);
  905. renderUniversalRect(vThumbWithMargins, mThumbProfile, thumbState);
  906. }
  907. else
  908. {
  909. if (mShowArrowButtons && mArrowProfile)
  910. {
  911. RectI upArrowExtent = RectI(mUpArrowRect.point + offset, mUpArrowRect.extent);
  912. renderBorderedRectWithArrow(upArrowExtent, mArrowProfile, GuiControlState::DisabledState, GuiDirection::Up);
  913. RectI downArrowExtent = RectI(mDownArrowRect.point + offset, mDownArrowRect.extent);
  914. renderBorderedRectWithArrow(downArrowExtent, mArrowProfile, GuiControlState::DisabledState, GuiDirection::Down);
  915. }
  916. RectI mVTrackExtent = RectI(mVTrackRect.point + offset, mVTrackRect.extent);
  917. renderUniversalRect(mVTrackExtent, mTrackProfile, GuiControlState::DisabledState);
  918. }
  919. }
  920. }
  921. void GuiScrollCtrl::renderHScrollBar(const Point2I& offset)
  922. {
  923. if(mHasHScrollBar && mTrackProfile && mThumbProfile)
  924. {
  925. if (mHBarEnabled)
  926. {
  927. if (mShowArrowButtons && mArrowProfile)
  928. {
  929. RectI leftArrowBounds = RectI(mLeftArrowRect.point + offset, mLeftArrowRect.extent);
  930. renderBorderedRectWithArrow(leftArrowBounds, mArrowProfile, getRegionCurrentState(Region::LeftArrow), GuiDirection::Left);
  931. RectI rightArrowBounds = RectI(mRightArrowRect.point + offset, mRightArrowRect.extent);
  932. renderBorderedRectWithArrow(rightArrowBounds, mArrowProfile, getRegionCurrentState(Region::RightArrow), GuiDirection::Right);
  933. }
  934. RectI hTrackBounds = RectI(mHTrackRect.point + offset, mHTrackRect.extent);
  935. renderUniversalRect(hTrackBounds, mTrackProfile, GuiControlState::NormalState);
  936. //The Thumb
  937. GuiControlState thumbState = getRegionCurrentState(Region::HorizThumb);
  938. RectI hThumb = RectI(mHTrackRect.point.x + mHThumbPos + offset.x, mHTrackRect.point.y + offset.y, mHThumbSize, mScrollBarThickness);
  939. RectI hThumbWithMargins = applyMargins(hThumb.point, hThumb.extent, thumbState, mThumbProfile);
  940. renderUniversalRect(hThumbWithMargins, mThumbProfile, thumbState);
  941. }
  942. else
  943. {
  944. if (mShowArrowButtons && mArrowProfile)
  945. {
  946. RectI leftArrowBounds = RectI(mLeftArrowRect.point + offset, mLeftArrowRect.extent);
  947. renderBorderedRectWithArrow(leftArrowBounds, mArrowProfile, GuiControlState::DisabledState, GuiDirection::Left);
  948. RectI rightArrowBounds = RectI(mRightArrowRect.point + offset, mRightArrowRect.extent);
  949. renderBorderedRectWithArrow(rightArrowBounds, mArrowProfile, GuiControlState::DisabledState, GuiDirection::Right);
  950. }
  951. RectI hTrackBounds = RectI(mHTrackRect.point + offset, mHTrackRect.extent);
  952. renderUniversalRect(hTrackBounds, mTrackProfile, GuiControlState::DisabledState);
  953. }
  954. }
  955. }
  956. void GuiScrollCtrl::renderChildControls(const Point2I& offset, const RectI& content, const RectI& updateRect)
  957. {
  958. // offset is the upper-left corner of this control in screen coordinates. It should almost always be the same offset passed into the onRender method.
  959. // updateRect is the area that this control was allowed to draw in. It should almost always be the same as the value in onRender.
  960. // content is the area that child controls are allowed to draw in.
  961. RectI clipRect = content;
  962. if (clipRect.intersect(dglGetClipRect()))
  963. {
  964. S32 size = objectList.size();
  965. S32 size_cpy = size;
  966. //Get the border profiles - padding is actually applied here...
  967. GuiBorderProfile* leftProfile = mProfile->getLeftBorder();
  968. GuiBorderProfile* topProfile = mProfile->getTopBorder();
  969. S32 leftSize = (leftProfile) ? leftProfile->getPadding(NormalState) : 0;
  970. S32 topSize = (topProfile) ? topProfile->getPadding(NormalState) : 0;
  971. Point2I ltPadding = Point2I(leftSize, topSize);
  972. //-Mat look through our vector all normal-like, trying to use an iterator sometimes gives us
  973. //bad cast on good objects
  974. for (S32 count = 0; count < objectList.size(); count++)
  975. {
  976. GuiControl* ctrl = (GuiControl*)objectList[count];
  977. if (ctrl == NULL) {
  978. Con::errorf("GuiControl::renderChildControls() object %i is NULL", count);
  979. continue;
  980. }
  981. if (ctrl->mVisible)
  982. {
  983. ctrl->mRenderInsetLT = (ltPadding + content.point - offset) - mScrollOffset;
  984. ctrl->mRenderInsetRB = mBounds.extent - (ctrl->mRenderInsetLT + content.extent);
  985. Point2I childPosition = ltPadding + content.point + ctrl->getPosition() - mScrollOffset;
  986. RectI childClip(childPosition, ctrl->getExtent());
  987. if (childClip.intersect(clipRect))
  988. {
  989. RectI old = dglGetClipRect();
  990. dglSetClipRect(clipRect);
  991. glDisable(GL_CULL_FACE);
  992. ctrl->onRender(childPosition, RectI(childPosition, ctrl->getExtent()));
  993. dglSetClipRect(old);
  994. }
  995. }
  996. size_cpy = objectList.size(); // CHRIS: i know its wierd but the size of the list changes sometimes during execution of this loop
  997. if (size != size_cpy)
  998. {
  999. size = size_cpy;
  1000. count--; // CHRIS: just to make sure one wasnt skipped.
  1001. }
  1002. }
  1003. }
  1004. }
  1005. #pragma endregion