guiCanvas.cc 41 KB

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