guiCanvas.cc 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423
  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 "torqueConfig.h"
  23. #include "console/consoleInternal.h"
  24. #include "debug/profiler.h"
  25. #include "graphics/dgl.h"
  26. #include "platform/event.h"
  27. #include "platform/platform.h"
  28. #include "platform/platformVideo.h"
  29. #include "gui/guiTypes.h"
  30. #include "gui/guiControl.h"
  31. #include "gui/guiCanvas.h"
  32. #include "game/gameInterface.h"
  33. #include "guiCanvas_ScriptBinding.h"
  34. extern int _AndroidGetScreenWidth();
  35. extern int _AndroidGetScreenHeight();
  36. IMPLEMENT_CONOBJECT(GuiCanvas);
  37. GuiCanvas *Canvas = NULL;
  38. GuiCanvas::GuiCanvas()
  39. {
  40. #ifdef TORQUE_OS_IOS
  41. mBounds.set(0, 0, IOS_DEFAULT_RESOLUTION_X, IOS_DEFAULT_RESOLUTION_Y);
  42. #elif TORQUE_OS_ANDROID
  43. mBounds.set(0, 0, _AndroidGetScreenWidth(), _AndroidGetScreenHeight());
  44. #else
  45. mBounds.set(0, 0, MIN_RESOLUTION_X, MIN_RESOLUTION_Y);
  46. #endif
  47. mAwake = true;
  48. mPixelsPerMickey = 1.0f;
  49. cursorON = true;
  50. mShowCursor = false;
  51. lastCursorON = false;
  52. rLastFrameTime = 0.0f;
  53. mMouseCapturedControl = NULL;
  54. mMouseControl = NULL;
  55. mMouseControlClicked = false;
  56. mMouseButtonDown = false;
  57. mMouseRightButtonDown = false;
  58. mMouseMiddleButtonDown = false;
  59. lastCursor = NULL;
  60. lastCursorPt.set(0,0);
  61. cursorPt.set(0,0);
  62. mLastMouseClickCount = 0;
  63. mLastMouseDownTime = 0;
  64. mPrevMouseTime = 0;
  65. defaultCursor = NULL;
  66. mRenderFront = false;
  67. hoverControlStart = Platform::getRealMilliseconds();
  68. hoverControl = NULL;
  69. hoverPosition = getCursorPos();
  70. hoverPositionSet = false;
  71. hoverLeftControlTime = 0;
  72. mLeftMouseLast = false;
  73. mMiddleMouseLast = false;
  74. mRightMouseLast = false;
  75. mDoubleClickWidth = Input::getDoubleClickWidth();
  76. mDoubleClickHeight = Input::getDoubleClickHeight();
  77. mDoubleClickTime = Input::getDoubleClickTime();
  78. /// Background color.
  79. mBackgroundColor.set( 0.0f, 0.0f, 0.0f, 0.0f );
  80. mUseBackgroundColor = true;
  81. }
  82. GuiCanvas::~GuiCanvas()
  83. {
  84. if(Canvas == this)
  85. Canvas = 0;
  86. }
  87. //-----------------------------------------------------------------------------
  88. void GuiCanvas::initPersistFields()
  89. {
  90. // Call Parent.
  91. Parent::initPersistFields();
  92. // Physics.
  93. addField("UseBackgroundColor", TypeBool, Offset(mUseBackgroundColor, GuiCanvas), "" );
  94. addField("BackgroundColor", TypeColorF, Offset(mBackgroundColor, GuiCanvas), "" );
  95. }
  96. //------------------------------------------------------------------------------
  97. void GuiCanvas::setCursor(GuiCursor *curs)
  98. {
  99. defaultCursor = curs;
  100. if(mShowCursor)
  101. {
  102. Input::setCursorState(false);
  103. }
  104. }
  105. void GuiCanvas::setCursorON(bool onOff)
  106. {
  107. cursorON = onOff;
  108. if(!cursorON)
  109. mMouseControl = NULL;
  110. }
  111. void GuiCanvas::setCursorPos(const Point2I &pt)
  112. {
  113. cursorPt.x = F32(pt.x);
  114. cursorPt.y = F32(pt.y);
  115. Input::setCursorPos( pt.x, pt.y );
  116. }
  117. void GuiCanvas::addAcceleratorKey(GuiControl *ctrl, U32 index, U32 keyCode, U32 modifier)
  118. {
  119. if (keyCode > 0 && ctrl)
  120. {
  121. AccKeyMap newMap;
  122. newMap.ctrl = ctrl;
  123. newMap.index = index;
  124. newMap.keyCode = keyCode;
  125. newMap.modifier = modifier;
  126. mAcceleratorMap.push_back(newMap);
  127. }
  128. }
  129. bool GuiCanvas::tabNext(void)
  130. {
  131. GuiControl *ctrl = static_cast<GuiControl *>(last());
  132. if (ctrl)
  133. {
  134. //save the old
  135. GuiControl *oldResponder = mFirstResponder;
  136. GuiControl* newResponder = ctrl->findNextTabable(mFirstResponder);
  137. if ( !newResponder )
  138. newResponder = ctrl->findFirstTabable();
  139. if ( newResponder && newResponder != oldResponder )
  140. {
  141. newResponder->setFirstResponder();
  142. if ( oldResponder )
  143. oldResponder->onLoseFirstResponder();
  144. return true;
  145. }
  146. }
  147. return false;
  148. }
  149. bool GuiCanvas::tabPrev(void)
  150. {
  151. GuiControl *ctrl = static_cast<GuiControl *>(last());
  152. if (ctrl)
  153. {
  154. //save the old
  155. GuiControl *oldResponder = mFirstResponder;
  156. GuiControl* newResponder = ctrl->findPrevTabable(mFirstResponder);
  157. if ( !newResponder )
  158. newResponder = ctrl->findLastTabable();
  159. if ( newResponder && newResponder != oldResponder )
  160. {
  161. newResponder->setFirstResponder();
  162. if ( oldResponder )
  163. oldResponder->onLoseFirstResponder();
  164. return true;
  165. }
  166. }
  167. return false;
  168. }
  169. void GuiCanvas::processScreenTouchEvent(const ScreenTouchEvent *event)
  170. {
  171. //copy the cursor point into the event
  172. mLastEvent.mousePoint.x = S32(event->xPos);
  173. mLastEvent.mousePoint.y = S32(event->yPos);
  174. mLastEvent.eventID = event->touchID;
  175. mLastEvent.mouseClickCount = event->numTouches;
  176. //see if button was pressed
  177. if (event->action == SI_MAKE)
  178. {
  179. U32 curTime = Platform::getVirtualMilliseconds();
  180. mNextMouseTime = curTime + mInitialMouseDelay;
  181. mLastMouseDownTime = curTime;
  182. // mLastEvent.mouseClickCount = mLastMouseClickCount;
  183. rootScreenTouchDown(mLastEvent);
  184. }
  185. else if(event->action == SI_MOVE)
  186. {
  187. rootScreenTouchMove(mLastEvent);
  188. }
  189. //else button was released
  190. else if(event->action == SI_BREAK)
  191. {
  192. mNextMouseTime = 0xFFFFFFFF;
  193. rootScreenTouchUp(mLastEvent);
  194. }
  195. }
  196. void GuiCanvas::processMouseMoveEvent(const MouseMoveEvent *event)
  197. {
  198. if( cursorON )
  199. {
  200. //copy the modifier into the new event
  201. mLastEvent.modifier = event->modifier;
  202. cursorPt.x += ( F32(event->xPos - cursorPt.x) * mPixelsPerMickey);
  203. cursorPt.y += ( F32(event->yPos - cursorPt.y) * mPixelsPerMickey);
  204. // clamp the cursor to the window, or not
  205. if( ! Con::getBoolVariable( "$pref::Gui::noClampTorqueCursorToWindow", true ))
  206. {
  207. cursorPt.x =(F32) getMax(0, getMin((S32)cursorPt.x, mBounds.extent.x - 1));
  208. cursorPt.y = (F32)getMax(0, getMin((S32)cursorPt.y, mBounds.extent.y - 1));
  209. }
  210. mLastEvent.mousePoint.x = S32(cursorPt.x);
  211. mLastEvent.mousePoint.y = S32(cursorPt.y);
  212. mLastEvent.eventID = 0;
  213. Point2F movement = mMouseDownPoint - cursorPt;
  214. if ((mAbs((S32)movement.x) > mDoubleClickWidth) || (mAbs((S32)movement.y) > mDoubleClickHeight))
  215. {
  216. mLeftMouseLast = false;
  217. mMiddleMouseLast = false;
  218. mRightMouseLast = false;
  219. }
  220. if (mMouseButtonDown)
  221. rootMouseDragged(mLastEvent);
  222. else if (mMouseRightButtonDown)
  223. rootRightMouseDragged(mLastEvent);
  224. else if(mMouseMiddleButtonDown)
  225. rootMiddleMouseDragged(mLastEvent);
  226. else
  227. rootMouseMove(mLastEvent);
  228. }
  229. }
  230. bool GuiCanvas::processInputEvent(const InputEvent *event)
  231. {
  232. // First call the general input handler (on the extremely off-chance that it will be handled):
  233. if ( mFirstResponder )
  234. {
  235. if ( mFirstResponder->onInputEvent( *event ) )
  236. return( true );
  237. }
  238. if(event->deviceType == KeyboardDeviceType)
  239. {
  240. mLastEvent.ascii = event->ascii;
  241. mLastEvent.modifier = event->modifier;
  242. mLastEvent.keyCode = (U8)event->objInst;
  243. U32 eventModifier = event->modifier;
  244. if(eventModifier & SI_SHIFT)
  245. eventModifier |= SI_SHIFT;
  246. if(eventModifier & SI_CTRL)
  247. eventModifier |= SI_CTRL;
  248. if(eventModifier & SI_ALT)
  249. eventModifier |= SI_ALT;
  250. if (event->action == SI_MAKE)
  251. {
  252. //see if we should tab next/prev
  253. //see if we should now pass the event to the first responder
  254. if (mFirstResponder)
  255. {
  256. if(mFirstResponder->onKeyDown(mLastEvent))
  257. return true;
  258. }
  259. if ( isCursorON() && ( event->objInst == KEY_TAB ) )
  260. {
  261. if (size() > 0)
  262. {
  263. if (event->modifier & SI_SHIFT)
  264. {
  265. if(tabPrev())
  266. return true;
  267. }
  268. else if (event->modifier == 0)
  269. {
  270. if(tabNext())
  271. return true;
  272. }
  273. }
  274. }
  275. //if not handled, search for an accelerator
  276. for (U32 i = 0; i < (U32)mAcceleratorMap.size(); i++)
  277. {
  278. if ((U32)mAcceleratorMap[i].keyCode == (U32)event->objInst && (U32)mAcceleratorMap[i].modifier == eventModifier)
  279. {
  280. mAcceleratorMap[i].ctrl->acceleratorKeyPress(mAcceleratorMap[i].index);
  281. return true;
  282. }
  283. }
  284. }
  285. else if(event->action == SI_BREAK)
  286. {
  287. if(mFirstResponder)
  288. if(mFirstResponder->onKeyUp(mLastEvent))
  289. return true;
  290. //see if there's an accelerator
  291. for (U32 i = 0; i < (U32)mAcceleratorMap.size(); i++)
  292. {
  293. if ((U32)mAcceleratorMap[i].keyCode == (U32)event->objInst && (U32)mAcceleratorMap[i].modifier == eventModifier)
  294. {
  295. mAcceleratorMap[i].ctrl->acceleratorKeyRelease(mAcceleratorMap[i].index);
  296. return true;
  297. }
  298. }
  299. }
  300. else if(event->action == SI_REPEAT)
  301. {
  302. //if not handled, search for an accelerator
  303. for (U32 i = 0; i < (U32)mAcceleratorMap.size(); i++)
  304. {
  305. if ((U32)mAcceleratorMap[i].keyCode == (U32)event->objInst && (U32)mAcceleratorMap[i].modifier == eventModifier)
  306. {
  307. mAcceleratorMap[i].ctrl->acceleratorKeyPress(mAcceleratorMap[i].index);
  308. return true;
  309. }
  310. }
  311. if(mFirstResponder)
  312. mFirstResponder->onKeyRepeat(mLastEvent);
  313. return true;
  314. }
  315. }
  316. else if(event->deviceType == MouseDeviceType && cursorON)
  317. {
  318. //copy the modifier into the new event
  319. mLastEvent.modifier = event->modifier;
  320. if(event->objType == SI_XAXIS || event->objType == SI_YAXIS)
  321. {
  322. bool moved = false;
  323. Point2I oldpt((S32)cursorPt.x, (S32)cursorPt.y);
  324. Point2F pt(cursorPt.x, cursorPt.y);
  325. if (event->objType == SI_XAXIS)
  326. {
  327. pt.x += (event->fValues[0] * mPixelsPerMickey);
  328. cursorPt.x = (F32)getMax(0, getMin((S32)pt.x, mBounds.extent.x - 1));
  329. if (oldpt.x != S32(cursorPt.x))
  330. moved = true;
  331. }
  332. else
  333. {
  334. pt.y += (event->fValues[0] * mPixelsPerMickey);
  335. cursorPt.y = (F32)getMax(0, getMin((S32)pt.y, mBounds.extent.y - 1));
  336. if (oldpt.y != S32(cursorPt.y))
  337. moved = true;
  338. }
  339. if (moved)
  340. {
  341. mLastEvent.mousePoint.x = S32(cursorPt.x);
  342. mLastEvent.mousePoint.y = S32(cursorPt.y);
  343. mLastEvent.eventID = 0;
  344. #ifdef TORQUE_ALLOW_JOURNALING
  345. // [tom, 9/8/2006] If we're journaling, we need to update the plat cursor
  346. if(Game->isJournalReading())
  347. Input::setCursorPos((S32)cursorPt.x, (S32)cursorPt.y);
  348. #endif //TORQUE_ALLOW_JOURNALING
  349. if (mMouseButtonDown)
  350. rootMouseDragged(mLastEvent);
  351. else if (mMouseRightButtonDown)
  352. rootRightMouseDragged(mLastEvent);
  353. else if(mMouseMiddleButtonDown)
  354. rootMiddleMouseDragged(mLastEvent);
  355. else
  356. rootMouseMove(mLastEvent);
  357. }
  358. return true;
  359. }
  360. else if ( event->objType == SI_ZAXIS )
  361. {
  362. mLastEvent.mousePoint.x = S32( cursorPt.x );
  363. mLastEvent.mousePoint.y = S32( cursorPt.y );
  364. mLastEvent.eventID = 0;
  365. if ( event->fValues[0] < 0.0f )
  366. rootMouseWheelDown( mLastEvent );
  367. else
  368. rootMouseWheelUp( mLastEvent );
  369. }
  370. else if(event->objType == SI_BUTTON)
  371. {
  372. //copy the cursor point into the event
  373. mLastEvent.mousePoint.x = S32(cursorPt.x);
  374. mLastEvent.mousePoint.y = S32(cursorPt.y);
  375. mLastEvent.eventID = 0;
  376. mMouseDownPoint = cursorPt;
  377. if(event->objInst == KEY_BUTTON0) // left button
  378. {
  379. //see if button was pressed
  380. if (event->action == SI_MAKE)
  381. {
  382. U32 curTime = Platform::getVirtualMilliseconds();
  383. mNextMouseTime = curTime + mInitialMouseDelay;
  384. //if the last button pressed was the left...
  385. if (mLeftMouseLast)
  386. {
  387. //if it was within the double click time count the clicks
  388. if ((S32)curTime - mLastMouseDownTime <= mDoubleClickTime)
  389. mLastMouseClickCount++;
  390. else
  391. mLastMouseClickCount = 1;
  392. }
  393. else
  394. {
  395. mLeftMouseLast = true;
  396. mLastMouseClickCount = 1;
  397. }
  398. mLastMouseDownTime = curTime;
  399. mLastEvent.mouseClickCount = mLastMouseClickCount;
  400. rootMouseDown(mLastEvent);
  401. }
  402. //else button was released
  403. else
  404. {
  405. mNextMouseTime = 0xFFFFFFFF;
  406. rootMouseUp(mLastEvent);
  407. }
  408. return true;
  409. }
  410. else if(event->objInst == KEY_BUTTON1) // right button
  411. {
  412. if(event->action == SI_MAKE)
  413. {
  414. U32 curTime = Platform::getVirtualMilliseconds();
  415. //if the last button pressed was the right...
  416. if (mRightMouseLast)
  417. {
  418. //if it was within the double click time count the clicks
  419. if ((S32)curTime - mLastMouseDownTime <= mDoubleClickTime)
  420. mLastMouseClickCount++;
  421. else
  422. mLastMouseClickCount = 1;
  423. }
  424. else
  425. {
  426. mRightMouseLast = true;
  427. mLastMouseClickCount = 1;
  428. }
  429. mLastMouseDownTime = curTime;
  430. mLastEvent.mouseClickCount = mLastMouseClickCount;
  431. rootRightMouseDown(mLastEvent);
  432. }
  433. else // it was a mouse up
  434. rootRightMouseUp(mLastEvent);
  435. return true;
  436. }
  437. else if(event->objInst == KEY_BUTTON2) // middle button
  438. {
  439. if(event->action == SI_MAKE)
  440. {
  441. U32 curTime = Platform::getVirtualMilliseconds();
  442. //if the last button pressed was the right...
  443. if (mMiddleMouseLast)
  444. {
  445. //if it was within the double click time count the clicks
  446. if ((S32)curTime - mLastMouseDownTime <= mDoubleClickTime)
  447. mLastMouseClickCount++;
  448. else
  449. mLastMouseClickCount = 1;
  450. }
  451. else
  452. {
  453. mMiddleMouseLast = true;
  454. mLastMouseClickCount = 1;
  455. }
  456. mLastMouseDownTime = curTime;
  457. mLastEvent.mouseClickCount = mLastMouseClickCount;
  458. rootMiddleMouseDown(mLastEvent);
  459. }
  460. else // it was a mouse up
  461. rootMiddleMouseUp(mLastEvent);
  462. return true;
  463. }
  464. }
  465. }
  466. return false;
  467. }
  468. void GuiCanvas::rootMouseDown(const GuiEvent &event)
  469. {
  470. mPrevMouseTime = Platform::getVirtualMilliseconds();
  471. mMouseButtonDown = true;
  472. //pass the event to the mouse locked control
  473. if (bool(mMouseCapturedControl))
  474. mMouseCapturedControl->onMouseDown(event);
  475. //else pass it to whoever is underneath the cursor
  476. else
  477. {
  478. iterator i;
  479. i = end();
  480. while (i != begin())
  481. {
  482. i--;
  483. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  484. GuiControl *controlHit = ctrl->findHitControl(event.mousePoint);
  485. //see if the controlHit is a modeless dialog...
  486. if ((! controlHit->mActive) && (! controlHit->mProfile->mModal))
  487. continue;
  488. else
  489. {
  490. controlHit->onMouseDown(event);
  491. break;
  492. }
  493. }
  494. }
  495. if (bool(mMouseControl))
  496. mMouseControlClicked = true;
  497. }
  498. void GuiCanvas::findMouseControl(const GuiEvent &event)
  499. {
  500. if(size() == 0)
  501. {
  502. mMouseControl = NULL;
  503. return;
  504. }
  505. GuiControl *controlHit = findHitControl(event.mousePoint);
  506. if(controlHit != static_cast<GuiControl*>(mMouseControl))
  507. {
  508. if(bool(mMouseControl))
  509. mMouseControl->onMouseLeave(event);
  510. mMouseControl = controlHit;
  511. mMouseControl->onMouseEnter(event);
  512. }
  513. }
  514. //Luma: Some fixes from the forums, Dave Calabrese
  515. //http://www.garagegames.com/community/forums/viewthread/93467/1#comment-669559
  516. void GuiCanvas::rootScreenTouchDown(const GuiEvent &event)
  517. {
  518. mPrevMouseTime = Platform::getVirtualMilliseconds();
  519. mMouseButtonDown = true;
  520. iterator i;
  521. i = end();
  522. while (i != begin())
  523. {
  524. i--;
  525. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  526. GuiControl *controlHit = ctrl->findHitControl(event.mousePoint);
  527. //If the control we hit is not the same one that is locked,
  528. // then unlock the existing control.
  529. if (bool(mMouseCapturedControl))
  530. {
  531. if(mMouseCapturedControl->isMouseLocked())
  532. {
  533. if(mMouseCapturedControl != controlHit)
  534. {
  535. mMouseCapturedControl->onMouseLeave(event);
  536. }
  537. }
  538. }
  539. //see if the controlHit is a modeless dialog...
  540. if ((! controlHit->mActive) && (! controlHit->mProfile->mModal))
  541. continue;
  542. else
  543. {
  544. controlHit->onMouseDown(event);
  545. break;
  546. }
  547. }
  548. if (bool(mMouseControl))
  549. mMouseControlClicked = true;
  550. }
  551. void GuiCanvas::rootScreenTouchUp(const GuiEvent &event)
  552. {
  553. mPrevMouseTime = Platform::getVirtualMilliseconds();
  554. mMouseButtonDown = false;
  555. iterator i;
  556. i = end();
  557. while (i != begin())
  558. {
  559. i--;
  560. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  561. GuiControl *controlHit = ctrl->findHitControl(event.mousePoint);
  562. //see if the controlHit is a modeless dialog...
  563. if ((! controlHit->mActive) && (! controlHit->mProfile->mModal))
  564. continue;
  565. else
  566. {
  567. controlHit->onMouseUp(event);
  568. break;
  569. }
  570. }
  571. }
  572. void GuiCanvas::rootScreenTouchMove(const GuiEvent &event)
  573. {
  574. //pass the event to the mouse locked control
  575. if (bool(mMouseCapturedControl))
  576. {
  577. checkLockMouseMove(event);
  578. if(!mMouseCapturedControl.isNull())
  579. mMouseCapturedControl->onMouseDragged(event);
  580. }
  581. else
  582. {
  583. findMouseControl(event);
  584. if(bool(mMouseControl))
  585. {
  586. mMouseControl->onMouseDragged(event);
  587. }
  588. }
  589. }
  590. void GuiCanvas::refreshMouseControl()
  591. {
  592. GuiEvent evt;
  593. evt.mousePoint.x = S32(cursorPt.x);
  594. evt.mousePoint.y = S32(cursorPt.y);
  595. findMouseControl(evt);
  596. }
  597. void GuiCanvas::rootMouseUp(const GuiEvent &event)
  598. {
  599. mPrevMouseTime = Platform::getVirtualMilliseconds();
  600. mMouseButtonDown = false;
  601. //pass the event to the mouse locked control
  602. if (bool(mMouseCapturedControl))
  603. mMouseCapturedControl->onMouseUp(event);
  604. else
  605. {
  606. findMouseControl(event);
  607. if(bool(mMouseControl))
  608. mMouseControl->onMouseUp(event);
  609. }
  610. }
  611. void GuiCanvas::checkLockMouseMove(const GuiEvent &event)
  612. {
  613. GuiControl *controlHit = findHitControl(event.mousePoint);
  614. if(controlHit != mMouseControl)
  615. {
  616. if(mMouseControl == mMouseCapturedControl)
  617. mMouseCapturedControl->onMouseLeave(event);
  618. else if(controlHit == mMouseCapturedControl)
  619. mMouseCapturedControl->onMouseEnter(event);
  620. mMouseControl = controlHit;
  621. }
  622. }
  623. void GuiCanvas::rootMouseDragged(const GuiEvent &event)
  624. {
  625. //pass the event to the mouse locked control
  626. if (bool(mMouseCapturedControl))
  627. {
  628. checkLockMouseMove(event);
  629. if(!mMouseCapturedControl.isNull())
  630. mMouseCapturedControl->onMouseDragged(event);
  631. //Luma: Mouse dragged calls mouse Moved on iPhone
  632. #if defined(TORQUE_OS_IOS) || defined(TORQUE_OS_ANDROID)
  633. mMouseCapturedControl->onMouseMove(event);
  634. #endif //TORQUE_OS_IOS
  635. }
  636. else
  637. {
  638. findMouseControl(event);
  639. if(bool(mMouseControl))
  640. {
  641. mMouseControl->onMouseDragged(event);
  642. #if defined(TORQUE_OS_IOS) || defined(TORQUE_OS_ANDROID)
  643. mMouseControl->onMouseMove(event);
  644. #endif //TORQUE_OS_IOS
  645. }
  646. }
  647. }
  648. void GuiCanvas::rootMouseMove(const GuiEvent &event)
  649. {
  650. if(mMouseCapturedControl != NULL)
  651. {
  652. checkLockMouseMove(event);
  653. if(mMouseCapturedControl != NULL)
  654. mMouseCapturedControl->onMouseMove(event);
  655. }
  656. else
  657. {
  658. findMouseControl(event);
  659. if(bool(mMouseControl))
  660. mMouseControl->onMouseMove(event);
  661. }
  662. }
  663. void GuiCanvas::rootRightMouseDown(const GuiEvent &event)
  664. {
  665. mPrevMouseTime = Platform::getVirtualMilliseconds();
  666. mMouseRightButtonDown = true;
  667. if (bool(mMouseCapturedControl))
  668. mMouseCapturedControl->onRightMouseDown(event);
  669. else
  670. {
  671. findMouseControl(event);
  672. if(bool(mMouseControl))
  673. {
  674. mMouseControl->onRightMouseDown(event);
  675. }
  676. }
  677. }
  678. void GuiCanvas::rootRightMouseUp(const GuiEvent &event)
  679. {
  680. mPrevMouseTime = Platform::getVirtualMilliseconds();
  681. mMouseRightButtonDown = false;
  682. if (bool(mMouseCapturedControl))
  683. mMouseCapturedControl->onRightMouseUp(event);
  684. else
  685. {
  686. findMouseControl(event);
  687. if(bool(mMouseControl))
  688. mMouseControl->onRightMouseUp(event);
  689. }
  690. }
  691. void GuiCanvas::rootRightMouseDragged(const GuiEvent &event)
  692. {
  693. mPrevMouseTime = Platform::getVirtualMilliseconds();
  694. if (bool(mMouseCapturedControl))
  695. {
  696. checkLockMouseMove(event);
  697. mMouseCapturedControl->onRightMouseDragged(event);
  698. }
  699. else
  700. {
  701. findMouseControl(event);
  702. if(bool(mMouseControl))
  703. mMouseControl->onRightMouseDragged(event);
  704. }
  705. }
  706. void GuiCanvas::rootMiddleMouseDown(const GuiEvent &event)
  707. {
  708. mPrevMouseTime = Platform::getVirtualMilliseconds();
  709. mMouseMiddleButtonDown = true;
  710. if (bool(mMouseCapturedControl))
  711. mMouseCapturedControl->onMiddleMouseDown(event);
  712. else
  713. {
  714. findMouseControl(event);
  715. if(bool(mMouseControl))
  716. {
  717. mMouseControl->onMiddleMouseDown(event);
  718. }
  719. }
  720. }
  721. void GuiCanvas::rootMiddleMouseUp(const GuiEvent &event)
  722. {
  723. mPrevMouseTime = Platform::getVirtualMilliseconds();
  724. mMouseMiddleButtonDown = false;
  725. if (bool(mMouseCapturedControl))
  726. mMouseCapturedControl->onMiddleMouseUp(event);
  727. else
  728. {
  729. findMouseControl(event);
  730. if(bool(mMouseControl))
  731. mMouseControl->onMiddleMouseUp(event);
  732. }
  733. }
  734. void GuiCanvas::rootMiddleMouseDragged(const GuiEvent &event)
  735. {
  736. mPrevMouseTime = Platform::getVirtualMilliseconds();
  737. if (bool(mMouseCapturedControl))
  738. {
  739. checkLockMouseMove(event);
  740. mMouseCapturedControl->onMiddleMouseDragged(event);
  741. }
  742. else
  743. {
  744. findMouseControl(event);
  745. if(bool(mMouseControl))
  746. mMouseControl->onMiddleMouseDragged(event);
  747. }
  748. }
  749. void GuiCanvas::rootMouseWheelUp(const GuiEvent &event)
  750. {
  751. if (bool(mMouseCapturedControl))
  752. mMouseCapturedControl->onMouseWheelUp(event);
  753. else
  754. {
  755. findMouseControl(event);
  756. if (bool(mMouseControl))
  757. mMouseControl->onMouseWheelUp(event);
  758. }
  759. }
  760. void GuiCanvas::rootMouseWheelDown(const GuiEvent &event)
  761. {
  762. if (bool(mMouseCapturedControl))
  763. mMouseCapturedControl->onMouseWheelDown(event);
  764. else
  765. {
  766. findMouseControl(event);
  767. if (bool(mMouseControl))
  768. mMouseControl->onMouseWheelDown(event);
  769. }
  770. }
  771. void GuiCanvas::setContentControl(GuiControl *gui)
  772. {
  773. if(!gui)
  774. return;
  775. // If we're setting the same content, don't do anything
  776. if( gui == at(0) )
  777. return;
  778. //remove all dialogs on layer 0
  779. U32 index = 0;
  780. while ((U32)size() > index)
  781. {
  782. GuiControl *ctrl = static_cast<GuiControl*>((*this)[index]);
  783. if (ctrl == gui || ctrl->mLayer != 0)
  784. index++;
  785. removeObject(ctrl);
  786. Sim::getGuiGroup()->addObject(ctrl);
  787. }
  788. // lose the first responder from the old GUI
  789. GuiControl* responder = gui->findFirstTabable();
  790. if(responder)
  791. responder->setFirstResponder();
  792. //add the gui to the front
  793. if(!size() || gui != (*this)[0])
  794. {
  795. // automatically wakes objects in GuiControl::onWake
  796. addObject(gui);
  797. if (size() >= 2)
  798. reOrder(gui, *begin());
  799. }
  800. //refresh the entire gui
  801. resetUpdateRegions();
  802. //rebuild the accelerator map
  803. mAcceleratorMap.clear();
  804. for(iterator i = end(); i != begin() ; )
  805. {
  806. i--;
  807. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  808. ctrl->buildAcceleratorMap();
  809. if (ctrl->mProfile->mModal)
  810. break;
  811. }
  812. refreshMouseControl();
  813. // Force the canvas to update the sizing of the new content control
  814. maintainSizing();
  815. }
  816. GuiControl *GuiCanvas::getContentControl()
  817. {
  818. if(size() > 0)
  819. return (GuiControl *) first();
  820. return NULL;
  821. }
  822. void GuiCanvas::pushDialogControl(GuiControl *gui, S32 layer)
  823. {
  824. //add the gui
  825. gui->mLayer = layer;
  826. // GuiControl::addObject wakes the object
  827. addObject(gui);
  828. //reorder it to the correct layer
  829. iterator i;
  830. for (i = begin(); i != end(); i++)
  831. {
  832. GuiControl *ctrl = static_cast<GuiControl*>(*i);
  833. if (ctrl->mLayer > gui->mLayer)
  834. {
  835. reOrder(gui, ctrl);
  836. break;
  837. }
  838. }
  839. //call the dialog push method
  840. gui->onDialogPush();
  841. //find the top most dialog
  842. //find the first responder
  843. GuiControl* responder = gui->findFirstTabable();
  844. if(responder)
  845. responder->setFirstResponder();
  846. // call the 'onWake' method?
  847. //if(wakedGui)
  848. // Con::executef(gui, 1, "onWake");
  849. //refresh the entire gui
  850. resetUpdateRegions();
  851. //rebuild the accelerator map
  852. mAcceleratorMap.clear();
  853. if (size() > 0)
  854. {
  855. GuiControl *ctrl = static_cast<GuiControl*>(last());
  856. ctrl->buildAcceleratorMap();
  857. }
  858. refreshMouseControl();
  859. if(gui->mProfile && gui->mProfile->mModal)
  860. {
  861. Input::pushCursor(CursorManager::curArrow);
  862. }
  863. }
  864. void GuiCanvas::popDialogControl(GuiControl *gui)
  865. {
  866. if (size() < 1)
  867. return;
  868. //first, find the dialog, and call the "onDialogPop()" method
  869. GuiControl *ctrl = NULL;
  870. if (gui)
  871. {
  872. //*** DAW: For modal dialogs, reset the mouse cursor and enable the appropriate platform menus
  873. if(gui->mProfile && gui->mProfile->mModal)
  874. {
  875. Input::popCursor();
  876. }
  877. //make sure the gui really exists on the stack
  878. iterator i;
  879. bool found = false;
  880. for(i = begin(); i != end(); i++)
  881. {
  882. GuiControl *check = static_cast<GuiControl *>(*i);
  883. if (check == gui)
  884. {
  885. ctrl = check;
  886. found = true;
  887. }
  888. }
  889. if (! found)
  890. return;
  891. }
  892. else
  893. ctrl = static_cast<GuiControl*>(last());
  894. //call the "on pop" function
  895. ctrl->onDialogPop();
  896. // sleep the object
  897. //now pop the last child (will sleep if awake)
  898. removeObject(ctrl);
  899. // Save the old responder:
  900. Sim::getGuiGroup()->addObject(ctrl);
  901. if (size() > 0)
  902. {
  903. GuiControl *ctrl = static_cast<GuiControl *>(last());
  904. if(ctrl->mFirstResponder)
  905. ctrl->mFirstResponder->setFirstResponder();
  906. }
  907. else
  908. {
  909. setFirstResponder(NULL);
  910. }
  911. //refresh the entire gui
  912. resetUpdateRegions();
  913. //rebuild the accelerator map
  914. mAcceleratorMap.clear();
  915. if (size() > 0)
  916. {
  917. GuiControl *ctrl = static_cast<GuiControl*>(last());
  918. ctrl->buildAcceleratorMap();
  919. }
  920. refreshMouseControl();
  921. }
  922. void GuiCanvas::popDialogControl(S32 layer)
  923. {
  924. if (size() < 1)
  925. return;
  926. GuiControl *ctrl = NULL;
  927. iterator i = end(); // find in z order (last to first)
  928. while (i != begin())
  929. {
  930. i--;
  931. ctrl = static_cast<GuiControl*>(*i);
  932. if (ctrl->mLayer == layer)
  933. break;
  934. }
  935. if (ctrl)
  936. popDialogControl(ctrl);
  937. }
  938. void GuiCanvas::mouseLock(GuiControl *lockingControl)
  939. {
  940. if (bool(mMouseCapturedControl))
  941. return;
  942. mMouseCapturedControl = lockingControl;
  943. if(mMouseControl && mMouseControl != mMouseCapturedControl)
  944. {
  945. GuiEvent evt;
  946. evt.mousePoint.x = S32(cursorPt.x);
  947. evt.mousePoint.y = S32(cursorPt.y);
  948. mMouseControl->onMouseLeave(evt);
  949. }
  950. }
  951. void GuiCanvas::mouseUnlock(GuiControl *lockingControl)
  952. {
  953. if (static_cast<GuiControl*>(mMouseCapturedControl) != lockingControl)
  954. return;
  955. GuiEvent evt;
  956. evt.mousePoint.x = S32(cursorPt.x);
  957. evt.mousePoint.y = S32(cursorPt.y);
  958. GuiControl * controlHit = findHitControl(evt.mousePoint);
  959. if(controlHit != mMouseCapturedControl)
  960. {
  961. mMouseControl = controlHit;
  962. mMouseControlClicked = false;
  963. if(bool(mMouseControl))
  964. mMouseControl->onMouseEnter(evt);
  965. }
  966. mMouseCapturedControl = NULL;
  967. }
  968. void GuiCanvas::paint()
  969. {
  970. resetUpdateRegions();
  971. // inhibit explicit refreshes in the case we're swapped out
  972. if (TextureManager::mDGLRender)
  973. renderFrame(false);
  974. }
  975. void GuiCanvas::maintainSizing()
  976. {
  977. Point2I size = Platform::getWindowSize();
  978. if(size.x == 0 || size.y == 0)
  979. return;
  980. RectI screenRect(0, 0, size.x, size.y);
  981. mBounds = screenRect;
  982. //all bottom level controls should be the same dimensions as the canvas
  983. //this is necessary for passing mouse events accurately
  984. iterator i;
  985. for (i = begin(); i != end(); i++)
  986. {
  987. AssertFatal(static_cast<GuiControl*>((*i))->isAwake(), "GuiCanvas::renderFrame: ctrl is not awake");
  988. GuiControl *ctrl = static_cast<GuiControl*>(*i);
  989. Point2I ext = ctrl->getExtent();
  990. Point2I pos = ctrl->getPosition();
  991. if(pos != screenRect.point || ext != screenRect.extent)
  992. {
  993. ctrl->resize(screenRect.point, screenRect.extent);
  994. resetUpdateRegions();
  995. }
  996. }
  997. }
  998. void GuiCanvas::renderFrame(bool preRenderOnly, bool bufferSwap /* = true */)
  999. {
  1000. PROFILE_START(CanvasPreRender);
  1001. #if !defined TORQUE_OS_IOS && !defined TORQUE_OS_ANDROID && !defined TORQUE_OS_EMSCRIPTEN
  1002. if(mRenderFront)
  1003. glDrawBuffer(GL_FRONT);
  1004. else
  1005. glDrawBuffer(GL_BACK);
  1006. #endif
  1007. // Make sure the root control is the size of the canvas.
  1008. Point2I size = Platform::getWindowSize();
  1009. if(size.x == 0 || size.y == 0)
  1010. {
  1011. //Luma: Fixed missing PROFILE_END()
  1012. PROFILE_END();
  1013. return;
  1014. }
  1015. RectI screenRect(0, 0, size.x, size.y);
  1016. maintainSizing();
  1017. //preRender (recursive) all controls
  1018. preRender();
  1019. PROFILE_END();
  1020. if(preRenderOnly)
  1021. return;
  1022. // for now, just always reset the update regions - this is a
  1023. // fix for FSAA on ATI cards
  1024. resetUpdateRegions();
  1025. // Moved this below object integration for performance reasons. -JDD
  1026. // // finish the gl render so we don't get too far ahead of ourselves
  1027. //#if defined(TORQUE_OS_WIN32)
  1028. // PROFILE_START(glFinish);
  1029. // glFinish();
  1030. // PROFILE_END();
  1031. //#endif
  1032. //draw the mouse, but not using tags...
  1033. PROFILE_START(CanvasRenderControls);
  1034. GuiCursor *mouseCursor = NULL;
  1035. bool cursorVisible = true;
  1036. if(bool(mMouseCapturedControl))
  1037. mMouseCapturedControl->getCursor(mouseCursor, cursorVisible, mLastEvent);
  1038. else if(bool(mMouseControl))
  1039. mMouseControl->getCursor(mouseCursor, cursorVisible, mLastEvent);
  1040. Point2I cursorPos((S32)cursorPt.x, (S32)cursorPt.y);
  1041. if(!mouseCursor)
  1042. mouseCursor = defaultCursor;
  1043. if(lastCursorON && lastCursor)
  1044. {
  1045. Point2I spot = lastCursor->getHotSpot();
  1046. Point2I cext = lastCursor->getExtent();
  1047. Point2I pos = lastCursorPt - spot;
  1048. addUpdateRegion(pos - Point2I(2, 2), Point2I(cext.x + 4, cext.y + 4));
  1049. }
  1050. if(cursorVisible && mouseCursor)
  1051. {
  1052. Point2I spot = mouseCursor->getHotSpot();
  1053. Point2I cext = mouseCursor->getExtent();
  1054. Point2I pos = cursorPos - spot;
  1055. addUpdateRegion(pos - Point2I(2, 2), Point2I(cext.x + 4, cext.y + 4));
  1056. }
  1057. lastCursorON = cursorVisible;
  1058. lastCursor = mouseCursor;
  1059. lastCursorPt = cursorPos;
  1060. RectI updateUnion;
  1061. buildUpdateUnion(&updateUnion);
  1062. if (updateUnion.intersect(screenRect))
  1063. {
  1064. // Clear the background color if requested.
  1065. if ( mUseBackgroundColor )
  1066. {
  1067. glClearColor( mBackgroundColor.red, mBackgroundColor.green, mBackgroundColor.blue, mBackgroundColor.alpha );
  1068. glClear(GL_COLOR_BUFFER_BIT);
  1069. }
  1070. //render the dialogs
  1071. iterator i;
  1072. for(i = begin(); i != end(); i++)
  1073. {
  1074. GuiControl *contentCtrl = static_cast<GuiControl*>(*i);
  1075. dglSetClipRect(updateUnion);
  1076. glDisable( GL_CULL_FACE );
  1077. contentCtrl->onRender(contentCtrl->getPosition(), updateUnion);
  1078. }
  1079. // Tooltip resource
  1080. if(bool(mMouseControl))
  1081. {
  1082. U32 curTime = Platform::getRealMilliseconds();
  1083. if(hoverControl == mMouseControl)
  1084. {
  1085. if(hoverPositionSet || (curTime - hoverControlStart) >= (U32)hoverControl->mTipHoverTime || (curTime - hoverLeftControlTime) <= (U32)hoverControl->mTipHoverTime)
  1086. {
  1087. // MM: Controls whether the tooltip tracks the mouse cursor or not.
  1088. #if 0
  1089. if(!hoverPositionSet)
  1090. {
  1091. hoverPosition = cursorPos;
  1092. }
  1093. #else
  1094. hoverPosition = cursorPos;
  1095. #endif
  1096. hoverPositionSet = mMouseControl->renderTooltip(hoverPosition);
  1097. }
  1098. } else
  1099. {
  1100. if(hoverPositionSet)
  1101. {
  1102. hoverLeftControlTime = curTime;
  1103. hoverPositionSet = false;
  1104. }
  1105. hoverControl = mMouseControl;
  1106. hoverControlStart = curTime;
  1107. }
  1108. }
  1109. //end tooltip
  1110. dglSetClipRect(updateUnion);
  1111. //temp draw the mouse
  1112. if (cursorON && mShowCursor && !mouseCursor)
  1113. {
  1114. #if defined(TORQUE_OS_IOS) || defined(TORQUE_OS_ANDROID) || defined(TORQUE_OS_EMSCRIPTEN)
  1115. glColor4ub(255, 0, 0, 255);
  1116. GLfloat vertices[] = {
  1117. (GLfloat)(cursorPt.x),(GLfloat)(cursorPt.y),
  1118. (GLfloat)(cursorPt.x + 2),(GLfloat)(cursorPt.y),
  1119. (GLfloat)(cursorPt.x + 2),(GLfloat)(cursorPt.y + 2),
  1120. (GLfloat)(cursorPt.x),(GLfloat)(cursorPt.y + 2),
  1121. };
  1122. glEnableClientState(GL_VERTEX_ARRAY);
  1123. glVertexPointer(2, GL_FLOAT, 0, vertices);
  1124. glDrawArrays(GL_LINE_LOOP, 0, 4);
  1125. #else
  1126. glColor4ub(255, 0, 0, 255);
  1127. glRecti((S32)cursorPt.x, (S32)cursorPt.y, (S32)(cursorPt.x + 2), (S32)(cursorPt.y + 2));
  1128. #endif
  1129. }
  1130. //DEBUG
  1131. //draw the help ctrl
  1132. //if (helpCtrl)
  1133. //{
  1134. // helpCtrl->render(srf);
  1135. //}
  1136. if (cursorON && mouseCursor && mShowCursor)
  1137. {
  1138. Point2I pos((S32)cursorPt.x, (S32)cursorPt.y);
  1139. Point2I spot = mouseCursor->getHotSpot();
  1140. pos -= spot;
  1141. mouseCursor->render(pos);
  1142. }
  1143. }
  1144. PROFILE_END();
  1145. if( bufferSwap )
  1146. swapBuffers();
  1147. //#if defined(TORQUE_OS_WIN32)
  1148. // PROFILE_START(glFinish);
  1149. // glFinish(); // This was changed to work with the D3D layer -pw
  1150. // PROFILE_END();
  1151. //#endif
  1152. }
  1153. void GuiCanvas::swapBuffers()
  1154. {
  1155. PROFILE_START(SwapBuffers);
  1156. //flip the surface
  1157. if(!mRenderFront)
  1158. Video::swapBuffers();
  1159. PROFILE_END();
  1160. }
  1161. void GuiCanvas::buildUpdateUnion(RectI *updateUnion)
  1162. {
  1163. *updateUnion = mOldUpdateRects[0];
  1164. //the update region should encompass the oldUpdateRects, and the curUpdateRect
  1165. Point2I upperL;
  1166. Point2I lowerR;
  1167. upperL.x = getMin(mOldUpdateRects[0].point.x, mOldUpdateRects[1].point.x);
  1168. upperL.x = getMin(upperL.x, mCurUpdateRect.point.x);
  1169. upperL.y = getMin(mOldUpdateRects[0].point.y, mOldUpdateRects[1].point.y);
  1170. upperL.y = getMin(upperL.y, mCurUpdateRect.point.y);
  1171. lowerR.x = getMax(mOldUpdateRects[0].point.x + mOldUpdateRects[0].extent.x, mOldUpdateRects[1].point.x + mOldUpdateRects[1].extent.x);
  1172. lowerR.x = getMax(lowerR.x, mCurUpdateRect.point.x + mCurUpdateRect.extent.x);
  1173. lowerR.y = getMax(mOldUpdateRects[0].point.y + mOldUpdateRects[0].extent.y, mOldUpdateRects[1].point.y + mOldUpdateRects[1].extent.y);
  1174. lowerR.y = getMax(lowerR.y, mCurUpdateRect.point.y + mCurUpdateRect.extent.y);
  1175. updateUnion->point = upperL;
  1176. updateUnion->extent = lowerR - upperL;
  1177. //shift the oldUpdateRects
  1178. mOldUpdateRects[0] = mOldUpdateRects[1];
  1179. mOldUpdateRects[1] = mCurUpdateRect;
  1180. mCurUpdateRect.point.set(0,0);
  1181. mCurUpdateRect.extent.set(0,0);
  1182. }
  1183. void GuiCanvas::addUpdateRegion(Point2I pos, Point2I ext)
  1184. {
  1185. if(mCurUpdateRect.extent.x == 0)
  1186. {
  1187. mCurUpdateRect.point = pos;
  1188. mCurUpdateRect.extent = ext;
  1189. }
  1190. else
  1191. {
  1192. Point2I upperL;
  1193. upperL.x = getMin(mCurUpdateRect.point.x, pos.x);
  1194. upperL.y = getMin(mCurUpdateRect.point.y, pos.y);
  1195. Point2I lowerR;
  1196. lowerR.x = getMax(mCurUpdateRect.point.x + mCurUpdateRect.extent.x, pos.x + ext.x);
  1197. lowerR.y = getMax(mCurUpdateRect.point.y + mCurUpdateRect.extent.y, pos.y + ext.y);
  1198. mCurUpdateRect.point = upperL;
  1199. mCurUpdateRect.extent = lowerR - upperL;
  1200. }
  1201. }
  1202. void GuiCanvas::resetUpdateRegions()
  1203. {
  1204. //DEBUG - get surface width and height
  1205. mOldUpdateRects[0].set(mBounds.point, mBounds.extent);
  1206. mOldUpdateRects[1] = mOldUpdateRects[0];
  1207. mCurUpdateRect = mOldUpdateRects[0];
  1208. }
  1209. void GuiCanvas::setFirstResponder( GuiControl* newResponder )
  1210. {
  1211. GuiControl* oldResponder = mFirstResponder;
  1212. Parent::setFirstResponder( newResponder );
  1213. if ( oldResponder && ( oldResponder != mFirstResponder ) )
  1214. oldResponder->onLoseFirstResponder();
  1215. }