guiCanvas.cc 43 KB

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