guiScrollCtrl.cc 33 KB

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