guiCanvas.cpp 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 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. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  23. // Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
  24. // Copyright (C) 2015 Faust Logic, Inc.
  25. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  26. #include "platform/platform.h"
  27. #include "gui/core/guiCanvas.h"
  28. #include "console/console.h"
  29. #include "console/engineAPI.h"
  30. #include "platform/profiler.h"
  31. #include "gfx/gfxDevice.h"
  32. #include "gfx/gfxDrawUtil.h"
  33. #include "gui/core/guiTypes.h"
  34. #include "gui/core/guiControl.h"
  35. #include "gui/editor/guiMenuBar.h"
  36. #include "console/consoleTypes.h"
  37. #include "gfx/screenshot.h"
  38. #include "gfx/video/videoCapture.h"
  39. #include "lighting/lightManager.h"
  40. #include "core/strings/stringUnit.h"
  41. #include "gui/core/guiOffscreenCanvas.h"
  42. #ifndef TORQUE_TGB_ONLY
  43. #include "scene/sceneObject.h"
  44. #endif
  45. #include "gfx/gfxInit.h"
  46. #include "core/util/journal/process.h"
  47. #ifdef TORQUE_GFX_STATE_DEBUG
  48. #include "gfx/gfxDebugStateTracker.h"
  49. #endif
  50. IMPLEMENT_CONOBJECT(GuiCanvas);
  51. ConsoleDocClass( GuiCanvas,
  52. "@brief A canvas on which rendering occurs.\n\n"
  53. "@section GuiCanvas_contents What a GUICanvas Can Contain...\n\n"
  54. "@subsection GuiCanvas_content_contentcontrol Content Control\n"
  55. "A content control is the top level GuiControl for a screen. This GuiControl "
  56. "will be the parent control for all other GuiControls on that particular "
  57. "screen.\n\n"
  58. "@subsection GuiCanvas_content_dialogs Dialogs\n\n"
  59. "A dialog is essentially another screen, only it gets overlaid on top of the "
  60. "current content control, and all input goes to the dialog. This is most akin "
  61. "to the \"Open File\" dialog box found in most operating systems. When you "
  62. "choose to open a file, and the \"Open File\" dialog pops up, you can no longer "
  63. "send input to the application, and must complete or cancel the open file "
  64. "request. Torque keeps track of layers of dialogs. The dialog with the highest "
  65. "layer is on top and will get all the input, unless the dialog is "
  66. "modeless, which is a profile option.\n\n"
  67. "@see GuiControlProfile\n\n"
  68. "@section GuiCanvas_dirty Dirty Rectangles\n\n"
  69. "The GuiCanvas is based on dirty regions. "
  70. "Every frame the canvas paints only the areas of the canvas that are 'dirty' "
  71. "or need updating. In most cases, this only is the area under the mouse cursor. "
  72. "This is why if you look in guiCanvas.cc the call to glClear is commented out. "
  73. "What you will see is a black screen, except in the dirty regions, where the "
  74. "screen will be painted normally. If you are making an animated GuiControl "
  75. "you need to add your control to the dirty areas of the canvas.\n\n"
  76. "@see GuiControl\n\n"
  77. "@ingroup GuiCore\n");
  78. ColorI gCanvasClearColor( 255, 0, 255 ); ///< For GFX->clear
  79. extern InputModifiers convertModifierBits(const U32 in);
  80. //-----------------------------------------------------------------------------
  81. GuiCanvas::GuiCanvas(): GuiControl(),
  82. mCurUpdateRect(0, 0, 0, 0),
  83. mCursorEnabled(true),
  84. mForceMouseToGUI(false),
  85. mAlwaysHandleMouseButtons(false),
  86. mShowCursor(true),
  87. mClampTorqueCursor(true),
  88. mCursorChanged(0),
  89. mLastCursorEnabled(false),
  90. mMouseCapturedControl(NULL),
  91. mMouseControl(NULL),
  92. mMouseControlClicked(false),
  93. mMouseButtonDown(false),
  94. mMouseRightButtonDown(false),
  95. mDefaultCursor(NULL),
  96. mMouseMiddleButtonDown(false),
  97. mCursorPt(0,0),
  98. mLastCursorPt(0,0),
  99. mLastCursor(NULL),
  100. mLastMouseClickCount(0),
  101. mRenderFront(false),
  102. mPrevMouseTime(0),
  103. mLastMouseDownTime(0),
  104. mHoverControl(NULL),
  105. mHoverPositionSet(false),
  106. mLeftMouseLast(false),
  107. mHoverLeftControlTime(0),
  108. mMiddleMouseLast(false),
  109. mRightMouseLast(false),
  110. mMouseDownPoint(0.0f,0.0f),
  111. mLastRenderMs(0),
  112. mPlatformWindow(NULL),
  113. mDisplayWindow(true),
  114. mMenuBarCtrl(nullptr),
  115. mMenuBackground(nullptr)
  116. {
  117. setBounds(0, 0, 640, 480);
  118. mAwake = true;
  119. mHoverControlStart = Platform::getRealMilliseconds();
  120. mHoverPosition = getCursorPos();
  121. mFences = NULL;
  122. mNextFenceIdx = -1;
  123. #ifndef _XBOX
  124. mNumFences = Con::getIntVariable( "$pref::Video::defaultFenceCount", 0 );
  125. #else
  126. mNumFences = 0;
  127. #endif
  128. }
  129. GuiCanvas::~GuiCanvas()
  130. {
  131. SAFE_DELETE(mPlatformWindow);
  132. SAFE_DELETE_ARRAY( mFences );
  133. }
  134. //------------------------------------------------------------------------------
  135. bool GuiCanvas::setProtectedNumFences( void *object, const char *index, const char *data)
  136. {
  137. GuiCanvas *canvas = reinterpret_cast<GuiCanvas *>( object );
  138. canvas->mNumFences = dAtoi( data );
  139. canvas->setupFences();
  140. return false;
  141. }
  142. void GuiCanvas::initPersistFields()
  143. {
  144. addGroup("Mouse Handling");
  145. addField("alwaysHandleMouseButtons", TypeBool, Offset(mAlwaysHandleMouseButtons, GuiCanvas),
  146. "Deal with mouse buttons, even if the cursor is hidden." );
  147. endGroup("Mouse Handling");
  148. addGroup("Canvas Rendering");
  149. addProtectedField( "numFences", TypeS32, Offset( mNumFences, GuiCanvas ), &setProtectedNumFences, &defaultProtectedGetFn, "The number of GFX fences to use." );
  150. addField("displayWindow", TypeBool, Offset(mDisplayWindow, GuiCanvas), "Controls if the canvas window is rendered or not." );
  151. endGroup("Canvas Rendering");
  152. Parent::initPersistFields();
  153. }
  154. //------------------------------------------------------------------------------
  155. bool GuiCanvas::onAdd()
  156. {
  157. // ensure that we have a cursor
  158. setCursor(dynamic_cast<GuiCursor*>(Sim::findObject("DefaultCursor")));
  159. // Enumerate things for GFX before we have an active device.
  160. GFXInit::enumerateAdapters();
  161. // Create a device.
  162. GFXAdapter *a = GFXInit::getBestAdapterChoice();
  163. // Do we have a global device already? (This is the site if you want
  164. // to start rendering to multiple devices simultaneously)
  165. GFXDevice *newDevice = GFX;
  166. if(newDevice == NULL)
  167. newDevice = GFXInit::createDevice(a);
  168. newDevice->setAllowRender( false );
  169. // Disable starting a new journal recording or playback from here on
  170. Journal::Disable();
  171. // Initialize the window...
  172. GFXVideoMode vm = GFXInit::getInitialVideoMode();
  173. //If we're recording, store the intial video resolution
  174. if (Journal::IsRecording())
  175. {
  176. Journal::Write(vm.resolution.x);
  177. Journal::Write(vm.resolution.y);
  178. Journal::Write(vm.fullScreen);
  179. }
  180. //If we're playing, read the intial video resolution from the journal
  181. if (Journal::IsPlaying())
  182. {
  183. Journal::Read(&vm.resolution.x);
  184. Journal::Read(&vm.resolution.y);
  185. Journal::Read(&vm.fullScreen);
  186. }
  187. if (a && a->mType != NullDevice)
  188. {
  189. mPlatformWindow = WindowManager->createWindow(newDevice, vm);
  190. //Disable window resizing if recording ir playing a journal
  191. if (Journal::IsRecording() || Journal::IsPlaying())
  192. mPlatformWindow->lockSize(true);
  193. // Set a minimum on the window size so people can't break us by resizing tiny.
  194. mPlatformWindow->setMinimumWindowSize(Point2I(640,480));
  195. // Now, we have to hook in our event callbacks so we'll get
  196. // appropriate events from the window.
  197. mPlatformWindow->resizeEvent .notify(this, &GuiCanvas::handleResize);
  198. mPlatformWindow->appEvent .notify(this, &GuiCanvas::handleAppEvent);
  199. mPlatformWindow->displayEvent.notify(this, &GuiCanvas::handlePaintEvent);
  200. mPlatformWindow->setInputController( dynamic_cast<IProcessInput*>(this) );
  201. }
  202. // Need to get painted, too! :)
  203. Process::notify(this, &GuiCanvas::paint, PROCESS_RENDER_ORDER);
  204. // Set up the fences
  205. setupFences();
  206. // Make sure we're able to render.
  207. newDevice->setAllowRender( true );
  208. if(mDisplayWindow)
  209. {
  210. getPlatformWindow()->show();
  211. WindowManager->setDisplayWindow(true);
  212. getPlatformWindow()->setDisplayWindow(true);
  213. }
  214. else
  215. {
  216. getPlatformWindow()->hide();
  217. WindowManager->setDisplayWindow(false);
  218. getPlatformWindow()->setDisplayWindow(false);
  219. }
  220. // Propagate add to parents.
  221. // CodeReview - if GuiCanvas fails to add for whatever reason, what happens to
  222. // all the event registration above?
  223. bool parentRet = Parent::onAdd();
  224. // Define the menu bar for this canvas (if any)
  225. Con::executef(this, "onCreateMenu");
  226. return parentRet;
  227. }
  228. void GuiCanvas::onRemove()
  229. {
  230. // And the process list
  231. Process::remove(this, &GuiCanvas::paint);
  232. // Destroy the menu bar for this canvas (if any)
  233. Con::executef(this, "onDestroyMenu");
  234. Parent::onRemove();
  235. }
  236. void GuiCanvas::setMenuBar(SimObject *obj)
  237. {
  238. GuiControl *oldMenuBar = mMenuBarCtrl;
  239. mMenuBarCtrl = dynamic_cast<GuiControl*>(obj);
  240. //remove old menubar
  241. if (oldMenuBar)
  242. {
  243. Parent::removeObject(oldMenuBar);
  244. Parent::removeObject(mMenuBackground); //also remove the modeless wrapper
  245. }
  246. // set new menubar
  247. if (mMenuBarCtrl)
  248. {
  249. //Add a wrapper control so that the menubar sizes correctly
  250. GuiControlProfile* profile;
  251. Sim::findObject("GuiModelessDialogProfile", profile);
  252. if (!profile)
  253. {
  254. Con::errorf("GuiCanvas::setMenuBar: Unable to find the GuiModelessDialogProfile profile!");
  255. return;
  256. }
  257. if (mMenuBackground == nullptr)
  258. {
  259. mMenuBackground = new GuiControl();
  260. mMenuBackground->registerObject();
  261. mMenuBackground->setControlProfile(profile);
  262. }
  263. mMenuBackground->addObject(mMenuBarCtrl);
  264. Parent::addObject(mMenuBackground);
  265. }
  266. // update window accelerator keys
  267. if( oldMenuBar != mMenuBarCtrl )
  268. {
  269. GuiMenuBar* oldMenu = dynamic_cast<GuiMenuBar*>(oldMenuBar);
  270. GuiMenuBar* newMenu = dynamic_cast<GuiMenuBar*>(mMenuBarCtrl);
  271. if(oldMenu)
  272. oldMenu->removeWindowAcceleratorMap(*getPlatformWindow()->getInputGenerator());
  273. if(newMenu)
  274. newMenu->buildWindowAcceleratorMap(*getPlatformWindow()->getInputGenerator());
  275. }
  276. }
  277. void GuiCanvas::setWindowTitle(const char *newTitle)
  278. {
  279. if (mPlatformWindow)
  280. mPlatformWindow->setCaption(newTitle);
  281. }
  282. CanvasSizeChangeSignal GuiCanvas::smCanvasSizeChangeSignal;
  283. void GuiCanvas::handleResize( WindowId did, S32 width, S32 height )
  284. {
  285. getCanvasSizeChangeSignal().trigger(this);
  286. if (Journal::IsPlaying() && mPlatformWindow)
  287. {
  288. mPlatformWindow->lockSize(false);
  289. mPlatformWindow->setSize(Point2I(width, height));
  290. mPlatformWindow->lockSize(true);
  291. }
  292. // Notify the scripts
  293. if ( isMethod( "onResize" ) )
  294. Con::executef( this, "onResize", Con::getIntArg( width ), Con::getIntArg( height ) );
  295. }
  296. void GuiCanvas::handlePaintEvent(WindowId did)
  297. {
  298. bool canRender = mPlatformWindow->isVisible() && GFX->allowRender() && !GFX->canCurrentlyRender();
  299. // Do the screenshot first.
  300. if ( gScreenShot != NULL && gScreenShot->isPending() && canRender )
  301. gScreenShot->capture( this );
  302. // If the video capture is waiting for a canvas, start the capture
  303. if ( VIDCAP->isWaitingForCanvas() && canRender )
  304. VIDCAP->begin( this );
  305. // Now capture the video
  306. if ( VIDCAP->isRecording() && canRender )
  307. VIDCAP->capture();
  308. renderFrame(false);
  309. }
  310. void GuiCanvas::handleAppEvent( WindowId did, S32 event )
  311. {
  312. // Notify script if we gain or lose focus.
  313. if(event == LoseFocus)
  314. {
  315. if(isMethod("onLoseFocus"))
  316. Con::executef(this, "onLoseFocus");
  317. }
  318. if(event == GainFocus)
  319. {
  320. if(isMethod("onGainFocus"))
  321. Con::executef(this, "onGainFocus");
  322. }
  323. if(event == WindowClose || event == WindowDestroy)
  324. {
  325. if(isMethod("onWindowClose"))
  326. {
  327. // First see if there is a method on this window to handle
  328. // it's closure
  329. Con::executef(this,"onWindowClose");
  330. }
  331. else if(Con::isFunction("onWindowClose"))
  332. {
  333. // otherwise check to see if there is a global function handling it
  334. Con::executef("onWindowClose", getIdString());
  335. }
  336. else
  337. {
  338. // Else just shutdown
  339. Process::requestShutdown();
  340. }
  341. }
  342. }
  343. Point2I GuiCanvas::getWindowSize()
  344. {
  345. // CodeReview Asserting on this breaks previous logic
  346. // and code assumptions. It seems logical that we would
  347. // handle this and return an error value rather than implementing
  348. // if(!mPlatformWindow) whenever we need to call getWindowSize.
  349. // This should help keep our API error free and easy to use, while
  350. // cutting down on code duplication for sanity checking. [5/5/2007 justind]
  351. if( !mPlatformWindow )
  352. return Point2I(-1,-1);
  353. return mPlatformWindow->getClientExtent();
  354. }
  355. void GuiCanvas::enableKeyboardTranslation()
  356. {
  357. AssertISV(mPlatformWindow, "GuiCanvas::enableKeyboardTranslation - no window present!");
  358. mPlatformWindow->setKeyboardTranslation(true);
  359. }
  360. void GuiCanvas::disableKeyboardTranslation()
  361. {
  362. AssertISV(mPlatformWindow, "GuiCanvas::disableKeyboardTranslation - no window present!");
  363. mPlatformWindow->setKeyboardTranslation(false);
  364. }
  365. void GuiCanvas::setNativeAcceleratorsEnabled( bool enabled )
  366. {
  367. AssertISV(mPlatformWindow, "GuiCanvas::setNativeAcceleratorsEnabled - no window present!");
  368. mPlatformWindow->setAcceleratorsEnabled( enabled );
  369. }
  370. void GuiCanvas::setForceMouseToGUI(bool onOff)
  371. {
  372. mForceMouseToGUI = onOff;
  373. }
  374. void GuiCanvas::setClampTorqueCursor(bool onOff)
  375. {
  376. mClampTorqueCursor = onOff;
  377. }
  378. void GuiCanvas::setCursor(GuiCursor *curs)
  379. {
  380. mDefaultCursor = curs;
  381. }
  382. void GuiCanvas::setCursorON(bool onOff)
  383. {
  384. mCursorEnabled = onOff;
  385. if(!mCursorEnabled)
  386. mMouseControl = NULL;
  387. }
  388. Point2I GuiCanvas::getCursorPos()
  389. {
  390. Point2I p( 0, 0 );
  391. if( mPlatformWindow )
  392. mPlatformWindow->getCursorPosition( p );
  393. return p;
  394. }
  395. void GuiCanvas::setCursorPos(const Point2I &pt)
  396. {
  397. AssertISV(mPlatformWindow, "GuiCanvas::setCursorPos - no window present!");
  398. if ( mPlatformWindow->isMouseLocked() )
  399. {
  400. mCursorPt.x = F32( pt.x );
  401. mCursorPt.y = F32( pt.y );
  402. }
  403. else
  404. {
  405. Point2I screenPt( mPlatformWindow->clientToScreen( pt ) );
  406. mPlatformWindow->setCursorPosition( screenPt.x, screenPt.y );
  407. }
  408. }
  409. void GuiCanvas::showCursor(bool state)
  410. {
  411. mShowCursor = state;
  412. mPlatformWindow->setCursorVisible( state );
  413. }
  414. bool GuiCanvas::isCursorShown()
  415. {
  416. if ( !mPlatformWindow->getCursorController() )
  417. {
  418. return mShowCursor;
  419. }
  420. return mPlatformWindow->isCursorVisible();
  421. }
  422. void GuiCanvas::cursorClick(S32 buttonId, bool isDown)
  423. {
  424. InputEventInfo inputEvent;
  425. inputEvent.deviceType = MouseDeviceType;
  426. inputEvent.deviceInst = 0;
  427. inputEvent.objType = SI_BUTTON;
  428. inputEvent.objInst = (InputObjectInstances)(KEY_BUTTON0 + buttonId);
  429. inputEvent.modifier = (InputModifiers)0;
  430. inputEvent.ascii = 0;
  431. inputEvent.action = isDown ? SI_MAKE : SI_BREAK;
  432. inputEvent.fValue = isDown ? 1.0 : 0.0;
  433. processMouseEvent(inputEvent);
  434. }
  435. void GuiCanvas::cursorNudge(F32 x, F32 y)
  436. {
  437. // Generate a base Movement along and Axis event
  438. InputEventInfo inputEvent;
  439. inputEvent.deviceType = MouseDeviceType;
  440. inputEvent.deviceInst = 0;
  441. inputEvent.objType = SI_AXIS;
  442. inputEvent.modifier = (InputModifiers)0;
  443. inputEvent.ascii = 0;
  444. // Generate delta movement along each axis
  445. Point2F cursDelta(x, y);
  446. // If X axis changed, generate a relative event
  447. if(mFabs(cursDelta.x) > 0.1)
  448. {
  449. inputEvent.objInst = SI_XAXIS;
  450. inputEvent.action = SI_MOVE;
  451. inputEvent.fValue = cursDelta.x;
  452. processMouseEvent(inputEvent);
  453. }
  454. // If Y axis changed, generate a relative event
  455. if(mFabs(cursDelta.y) > 0.1)
  456. {
  457. inputEvent.objInst = SI_YAXIS;
  458. inputEvent.action = SI_MOVE;
  459. inputEvent.fValue = cursDelta.y;
  460. processMouseEvent(inputEvent);
  461. }
  462. processMouseEvent(inputEvent);
  463. }
  464. void GuiCanvas::addAcceleratorKey(GuiControl *ctrl, U32 index, U32 keyCode, U32 modifier)
  465. {
  466. if (keyCode > 0 && ctrl)
  467. {
  468. AccKeyMap newMap;
  469. newMap.ctrl = ctrl;
  470. newMap.index = index;
  471. newMap.keyCode = keyCode;
  472. newMap.modifier = modifier;
  473. mAcceleratorMap.push_back(newMap);
  474. }
  475. }
  476. bool GuiCanvas::tabNext(void)
  477. {
  478. GuiControl *ctrl = static_cast<GuiControl *>(last());
  479. if (ctrl)
  480. {
  481. //save the old
  482. GuiControl *oldResponder = mFirstResponder;
  483. GuiControl* newResponder = ctrl->findNextTabable(mFirstResponder);
  484. if ( !newResponder )
  485. newResponder = ctrl->findFirstTabable();
  486. if ( newResponder && newResponder != oldResponder )
  487. {
  488. newResponder->setFirstResponder();
  489. // CodeReview Can this get killed? Note tabPrev code. BJG - 3/25/07
  490. // if ( oldResponder )
  491. // oldResponder->onLoseFirstResponder();
  492. return true;
  493. }
  494. }
  495. return false;
  496. }
  497. bool GuiCanvas::tabPrev(void)
  498. {
  499. GuiControl *ctrl = static_cast<GuiControl *>(last());
  500. if (ctrl)
  501. {
  502. //save the old
  503. GuiControl *oldResponder = mFirstResponder;
  504. GuiControl* newResponder = ctrl->findPrevTabable(mFirstResponder);
  505. if ( !newResponder )
  506. newResponder = ctrl->findLastTabable();
  507. if ( newResponder && newResponder != oldResponder )
  508. {
  509. newResponder->setFirstResponder();
  510. // CodeReview As with tabNext() above, looks like this can now go. DAW - 7/05/09
  511. //if ( oldResponder )
  512. // oldResponder->onLoseFirstResponder();
  513. return true;
  514. }
  515. }
  516. return false;
  517. }
  518. bool GuiCanvas::processInputEvent(InputEventInfo &inputEvent)
  519. {
  520. mConsumeLastInputEvent = true;
  521. // First call the general input handler (on the extremely off-chance that it will be handled):
  522. if (mFirstResponder && mFirstResponder->onInputEvent(inputEvent))
  523. {
  524. return mConsumeLastInputEvent;
  525. }
  526. switch (inputEvent.deviceType)
  527. {
  528. case KeyboardDeviceType:
  529. return processKeyboardEvent(inputEvent);
  530. break;
  531. case GamepadDeviceType:
  532. return processGamepadEvent(inputEvent);
  533. break;
  534. case MouseDeviceType:
  535. if (mCursorEnabled || mForceMouseToGUI ||
  536. (mAlwaysHandleMouseButtons && inputEvent.objType == SI_BUTTON) )
  537. {
  538. return processMouseEvent(inputEvent);
  539. }
  540. break;
  541. default:
  542. break;
  543. }
  544. return false;
  545. }
  546. bool GuiCanvas::processKeyboardEvent(InputEventInfo &inputEvent)
  547. {
  548. mLastEvent.ascii = inputEvent.ascii;
  549. mLastEvent.modifier = inputEvent.modifier;
  550. mLastEvent.keyCode = inputEvent.objInst;
  551. // Combine left/right shift bits - if one shift modifier key
  552. // bit is set, then set the other one. This way we can simplify
  553. // our processing logic by treating the keys identically.
  554. U32 eventModifier = inputEvent.modifier;
  555. if(eventModifier & SI_SHIFT)
  556. {
  557. eventModifier |= SI_SHIFT;
  558. }
  559. if(eventModifier & SI_CTRL)
  560. {
  561. eventModifier |= SI_CTRL;
  562. }
  563. if(eventModifier & SI_ALT)
  564. {
  565. eventModifier |= SI_ALT;
  566. }
  567. if (inputEvent.action == SI_MAKE)
  568. {
  569. //see if we should now pass the event to the first responder
  570. if (mFirstResponder)
  571. {
  572. if(mFirstResponder->onKeyDown(mLastEvent))
  573. return mConsumeLastInputEvent;
  574. }
  575. //see if we should tab next/prev
  576. if ( isCursorON() && ( inputEvent.objInst == KEY_TAB ) )
  577. {
  578. if (size() > 0)
  579. {
  580. if (inputEvent.modifier & SI_SHIFT)
  581. {
  582. if(tabPrev())
  583. return mConsumeLastInputEvent;
  584. }
  585. else if (inputEvent.modifier == 0)
  586. {
  587. if(tabNext())
  588. return mConsumeLastInputEvent;
  589. }
  590. }
  591. }
  592. //if not handled, search for an accelerator
  593. for (U32 i = 0; i < mAcceleratorMap.size(); i++)
  594. {
  595. if ((U32)mAcceleratorMap[i].keyCode == (U32)inputEvent.objInst && (U32)mAcceleratorMap[i].modifier == eventModifier)
  596. {
  597. mAcceleratorMap[i].ctrl->acceleratorKeyPress(mAcceleratorMap[i].index);
  598. return mConsumeLastInputEvent;
  599. }
  600. }
  601. }
  602. else if(inputEvent.action == SI_BREAK)
  603. {
  604. if(mFirstResponder && mFirstResponder->onKeyUp(mLastEvent))
  605. return mConsumeLastInputEvent;
  606. //see if there's an accelerator
  607. for (U32 i = 0; i < mAcceleratorMap.size(); i++)
  608. {
  609. if ((U32)mAcceleratorMap[i].keyCode == (U32)inputEvent.objInst && (U32)mAcceleratorMap[i].modifier == eventModifier)
  610. {
  611. mAcceleratorMap[i].ctrl->acceleratorKeyRelease(mAcceleratorMap[i].index);
  612. return mConsumeLastInputEvent;
  613. }
  614. }
  615. }
  616. else if(inputEvent.action == SI_REPEAT)
  617. {
  618. //if not handled, search for an accelerator
  619. for (U32 i = 0; i < mAcceleratorMap.size(); i++)
  620. {
  621. if ((U32)mAcceleratorMap[i].keyCode == (U32)inputEvent.objInst && (U32)mAcceleratorMap[i].modifier == eventModifier)
  622. {
  623. mAcceleratorMap[i].ctrl->acceleratorKeyPress(mAcceleratorMap[i].index);
  624. return mConsumeLastInputEvent;
  625. }
  626. }
  627. if(mFirstResponder)
  628. {
  629. bool ret = mFirstResponder->onKeyRepeat(mLastEvent);
  630. return ret && mConsumeLastInputEvent;
  631. }
  632. }
  633. return false;
  634. }
  635. bool GuiCanvas::processMouseEvent(InputEventInfo &inputEvent)
  636. {
  637. // [rene 09/09/10] This custom mouse cursor tracking that is happening here is bad. It will frequently
  638. // get ouf of step with where the cursor actually is. We really should *not* track the cursor; it's
  639. // just another thing that can/will go wrong. Let the input system pass us absolute screen coordinates
  640. // for every mouse event instead and work off that.
  641. //
  642. // 'mCursorPt' basically is an accumulation of errors and the number of bugs that have cropped up with
  643. // the GUI clicking stuff where it is not supposed to are probably all to blame on this.
  644. S32 mouseDoubleClickWidth = 12;
  645. S32 mouseDoubleClickHeight = 12;
  646. U32 mouseDoubleClickTime = 500;
  647. // Query platform for mouse info if its available
  648. PlatformCursorController *pController = mPlatformWindow ? mPlatformWindow->getCursorController() : NULL;
  649. if (pController)
  650. {
  651. mouseDoubleClickWidth = pController->getDoubleClickWidth();
  652. mouseDoubleClickHeight = pController->getDoubleClickHeight();
  653. mouseDoubleClickTime = pController->getDoubleClickTime();
  654. }
  655. //copy the modifier into the new event
  656. mLastEvent.modifier = inputEvent.modifier;
  657. if(inputEvent.objType == SI_AXIS &&
  658. (inputEvent.objInst == SI_XAXIS || inputEvent.objInst == SI_YAXIS))
  659. {
  660. // Set the absolute position if we get an SI_MAKE on an axis
  661. if( inputEvent.objInst == SI_XAXIS )
  662. {
  663. if( inputEvent.action == SI_MAKE )
  664. mCursorPt.x = (S32)inputEvent.fValue;
  665. else if( inputEvent.action == SI_MOVE )
  666. mCursorPt.x += (S32)inputEvent.fValue;
  667. mCursorPt.x = getMax(0, getMin((S32)mCursorPt.x, getBounds().extent.x - 1));
  668. }
  669. else if( inputEvent.objInst == SI_YAXIS )
  670. {
  671. if( inputEvent.action == SI_MAKE )
  672. mCursorPt.y = (S32)inputEvent.fValue;
  673. else if( inputEvent.action == SI_MOVE )
  674. mCursorPt.y += (S32)inputEvent.fValue;
  675. mCursorPt.y = getMax(0, getMin((S32)mCursorPt.y, getBounds().extent.y - 1));
  676. }
  677. // Store new cursor position.
  678. mLastEvent.mousePoint.x = S32(mCursorPt.x);
  679. mLastEvent.mousePoint.y = S32(mCursorPt.y);
  680. // See if we need to invalidate a possible dbl click due to the cursor
  681. // moving too much.
  682. Point2F movement = mMouseDownPoint - mCursorPt;
  683. if ((mAbs((S32)movement.x) > mouseDoubleClickWidth) || (mAbs((S32)movement.y) > mouseDoubleClickHeight ) )
  684. {
  685. mLeftMouseLast = false;
  686. mMiddleMouseLast = false;
  687. mRightMouseLast = false;
  688. }
  689. if (mMouseButtonDown)
  690. rootMouseDragged(mLastEvent);
  691. else if (mMouseRightButtonDown)
  692. rootRightMouseDragged(mLastEvent);
  693. else if(mMouseMiddleButtonDown)
  694. rootMiddleMouseDragged(mLastEvent);
  695. else
  696. rootMouseMove(mLastEvent);
  697. return mConsumeLastInputEvent;
  698. }
  699. else if ( inputEvent.objInst == SI_ZAXIS
  700. || inputEvent.objInst == SI_RZAXIS )
  701. {
  702. mLastEvent.mousePoint.x = S32( mCursorPt.x );
  703. mLastEvent.mousePoint.y = S32( mCursorPt.y );
  704. mLastEvent.fval = inputEvent.fValue;
  705. if( inputEvent.objInst == SI_ZAXIS )
  706. mLastEvent.mouseAxis = 1;
  707. else
  708. mLastEvent.mouseAxis = 0;
  709. if ( inputEvent.fValue < 0.0f )
  710. return rootMouseWheelDown( mLastEvent );
  711. else
  712. return rootMouseWheelUp( mLastEvent );
  713. }
  714. else if(inputEvent.objType == SI_BUTTON)
  715. {
  716. //copy the cursor point into the event
  717. mLastEvent.mousePoint.x = S32(mCursorPt.x);
  718. mLastEvent.mousePoint.y = S32(mCursorPt.y);
  719. mMouseDownPoint = mCursorPt;
  720. if(inputEvent.objInst == KEY_BUTTON0) // left button
  721. {
  722. //see if button was pressed
  723. if (inputEvent.action == SI_MAKE)
  724. {
  725. U32 curTime = Platform::getVirtualMilliseconds();
  726. //if the last button pressed was the left...
  727. if (mLeftMouseLast)
  728. {
  729. //if it was within the double click time count the clicks
  730. if (curTime - mLastMouseDownTime <= mouseDoubleClickTime)
  731. mLastMouseClickCount++;
  732. else
  733. mLastMouseClickCount = 1;
  734. }
  735. else
  736. {
  737. mLeftMouseLast = true;
  738. mLastMouseClickCount = 1;
  739. }
  740. mLastMouseDownTime = curTime;
  741. mLastEvent.mouseClickCount = mLastMouseClickCount;
  742. rootMouseDown(mLastEvent);
  743. }
  744. //else button was released
  745. else
  746. {
  747. rootMouseUp(mLastEvent);
  748. }
  749. return mConsumeLastInputEvent;
  750. }
  751. else if(inputEvent.objInst == KEY_BUTTON1) // right button
  752. {
  753. if(inputEvent.action == SI_MAKE)
  754. {
  755. U32 curTime = Platform::getVirtualMilliseconds();
  756. //if the last button pressed was the right...
  757. if (mRightMouseLast)
  758. {
  759. //if it was within the double click time count the clicks
  760. if (curTime - mLastMouseDownTime <= mouseDoubleClickTime)
  761. mLastMouseClickCount++;
  762. else
  763. mLastMouseClickCount = 1;
  764. }
  765. else
  766. {
  767. mRightMouseLast = true;
  768. mLastMouseClickCount = 1;
  769. }
  770. mLastMouseDownTime = curTime;
  771. mLastEvent.mouseClickCount = mLastMouseClickCount;
  772. rootRightMouseDown(mLastEvent);
  773. }
  774. else // it was a mouse up
  775. rootRightMouseUp(mLastEvent);
  776. return mConsumeLastInputEvent;
  777. }
  778. else if(inputEvent.objInst == KEY_BUTTON2) // middle button
  779. {
  780. if(inputEvent.action == SI_MAKE)
  781. {
  782. U32 curTime = Platform::getVirtualMilliseconds();
  783. //if the last button pressed was the right...
  784. if (mMiddleMouseLast)
  785. {
  786. //if it was within the double click time count the clicks
  787. if (curTime - mLastMouseDownTime <= mouseDoubleClickTime)
  788. mLastMouseClickCount++;
  789. else
  790. mLastMouseClickCount = 1;
  791. }
  792. else
  793. {
  794. mMiddleMouseLast = true;
  795. mLastMouseClickCount = 1;
  796. }
  797. mLastMouseDownTime = curTime;
  798. mLastEvent.mouseClickCount = mLastMouseClickCount;
  799. rootMiddleMouseDown(mLastEvent);
  800. }
  801. else // it was a mouse up
  802. rootMiddleMouseUp(mLastEvent);
  803. return mConsumeLastInputEvent;
  804. }
  805. }
  806. return false;
  807. }
  808. bool GuiCanvas::processGamepadEvent(InputEventInfo &inputEvent)
  809. {
  810. if (! mFirstResponder)
  811. {
  812. // early out, no first responder to receive gamepad input
  813. return false;
  814. }
  815. if (inputEvent.deviceInst >= MAX_GAMEPADS)
  816. {
  817. // early out, we only support the first MAX_GAMEPADS gamepads
  818. return false;
  819. }
  820. mLastEvent.keyCode = inputEvent.objInst;
  821. if (inputEvent.objType == SI_BUTTON)
  822. {
  823. switch (inputEvent.action)
  824. {
  825. case SI_MAKE:
  826. switch (inputEvent.objInst)
  827. {
  828. case SI_UPOV:
  829. return mFirstResponder->onGamepadAxisUp(mLastEvent);
  830. case SI_DPOV:
  831. return mFirstResponder->onGamepadAxisDown(mLastEvent);
  832. case SI_LPOV:
  833. return mFirstResponder->onGamepadAxisLeft(mLastEvent);
  834. case SI_RPOV:
  835. return mFirstResponder->onGamepadAxisRight(mLastEvent);
  836. default:
  837. return mFirstResponder->onGamepadButtonDown(mLastEvent);
  838. }
  839. break;
  840. case SI_BREAK:
  841. return mFirstResponder->onGamepadButtonUp(mLastEvent);
  842. default:
  843. return false;
  844. }
  845. }
  846. else if (inputEvent.objType == SI_AXIS)
  847. {
  848. F32 incomingValue = mFabs(inputEvent.fValue);
  849. static const F32 DEAD_ZONE = 0.5f;
  850. static const F32 MIN_CLICK_TIME = 500.0f;
  851. static const F32 MAX_CLICK_TIME = 1000.0f;
  852. static F32 xDecay [] = {1.0f, 1.0f, 1.0f, 1.0f};
  853. static F32 yDecay [] = {1.0f, 1.0f, 1.0f, 1.0f};
  854. static F32 zDecay [] = {1.0f, 1.0f, 1.0f, 1.0f};
  855. static U32 xLastClickTime [] = {0, 0, 0, 0};
  856. static U32 yLastClickTime [] = {0, 0, 0, 0};
  857. static U32 zLastClickTime [] = {0, 0, 0, 0};
  858. U32 curTime = Platform::getRealMilliseconds();
  859. F32 *decay;
  860. U32 *lastClickTime;
  861. switch (inputEvent.objInst)
  862. {
  863. case SI_ZAXIS:
  864. case XI_LEFT_TRIGGER:
  865. case XI_RIGHT_TRIGGER:
  866. decay = &zDecay[inputEvent.deviceInst];
  867. lastClickTime = &zLastClickTime[inputEvent.deviceInst];
  868. break;
  869. case SI_YAXIS:
  870. case XI_THUMBLY:
  871. case XI_THUMBRY:
  872. decay = &yDecay[inputEvent.deviceInst];
  873. lastClickTime = &yLastClickTime[inputEvent.deviceInst];
  874. break;
  875. case SI_XAXIS:
  876. case XI_THUMBLX:
  877. case XI_THUMBRX:
  878. default:
  879. decay = &xDecay[inputEvent.deviceInst];
  880. lastClickTime = &xLastClickTime[inputEvent.deviceInst];
  881. break;
  882. }
  883. if (incomingValue < DEAD_ZONE)
  884. {
  885. // early out, control movement is within the deadzone
  886. *decay = 1.0f;
  887. *lastClickTime = 0;
  888. return false;
  889. }
  890. // Rescales the input between 0.0 and 1.0
  891. incomingValue = (incomingValue - DEAD_ZONE) * (1.0f / (1.0f - DEAD_ZONE));
  892. F32 clickTime = MIN_CLICK_TIME + (MAX_CLICK_TIME - MIN_CLICK_TIME) * (1.0f - incomingValue);
  893. clickTime *= *decay;
  894. if (clickTime < (curTime - *lastClickTime))
  895. {
  896. *decay *= 0.9f;
  897. if (*decay < 0.2f)
  898. {
  899. *decay = 0.2f;
  900. }
  901. *lastClickTime = curTime;
  902. bool negative = (inputEvent.fValue < 0.0f);
  903. switch (inputEvent.objInst)
  904. {
  905. case XI_LEFT_TRIGGER:
  906. case XI_RIGHT_TRIGGER:
  907. return mFirstResponder->onGamepadTrigger(mLastEvent);
  908. case SI_ZAXIS:
  909. case SI_YAXIS:
  910. case XI_THUMBLY:
  911. case XI_THUMBRY:
  912. if (negative)
  913. {
  914. return mFirstResponder->onGamepadAxisDown(mLastEvent);
  915. }
  916. else
  917. {
  918. return mFirstResponder->onGamepadAxisUp(mLastEvent);
  919. }
  920. case SI_XAXIS:
  921. case XI_THUMBLX:
  922. case XI_THUMBRX:
  923. default:
  924. if (negative)
  925. {
  926. return mFirstResponder->onGamepadAxisLeft(mLastEvent);
  927. }
  928. else
  929. {
  930. return mFirstResponder->onGamepadAxisRight(mLastEvent);
  931. }
  932. }
  933. }
  934. }
  935. return false;
  936. }
  937. void GuiCanvas::rootMouseDown(const GuiEvent &event)
  938. {
  939. mPrevMouseTime = Platform::getVirtualMilliseconds();
  940. mMouseButtonDown = true;
  941. //pass the event to the mouse locked control
  942. if (bool(mMouseCapturedControl))
  943. mMouseCapturedControl->onMouseDown(event);
  944. else
  945. {
  946. //else pass it to whoever is underneath the cursor
  947. iterator i;
  948. i = end();
  949. while (i != begin())
  950. {
  951. i--;
  952. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  953. GuiControl *controlHit = ctrl->findHitControl( event.mousePoint - ctrl->getPosition() );
  954. //see if the controlHit is a modeless dialog...
  955. if( !controlHit->getControlProfile()->mModal )
  956. continue;
  957. else
  958. {
  959. controlHit->onMouseDown(event);
  960. break;
  961. }
  962. }
  963. }
  964. if (bool(mMouseControl))
  965. mMouseControlClicked = true;
  966. }
  967. void GuiCanvas::findMouseControl(const GuiEvent &event)
  968. {
  969. // Any children at all?
  970. if(size() == 0)
  971. {
  972. mMouseControl = NULL;
  973. return;
  974. }
  975. // Otherwise, check the point and find the overlapped control.
  976. GuiControl *controlHit = findHitControl(event.mousePoint);
  977. if(controlHit != static_cast<GuiControl*>(mMouseControl))
  978. {
  979. if(bool(mMouseControl))
  980. mMouseControl->onMouseLeave(event);
  981. mMouseControl = controlHit;
  982. mMouseControl->onMouseEnter(event);
  983. }
  984. }
  985. void GuiCanvas::refreshMouseControl()
  986. {
  987. GuiEvent evt;
  988. evt.mousePoint.x = S32(mCursorPt.x);
  989. evt.mousePoint.y = S32(mCursorPt.y);
  990. findMouseControl(evt);
  991. }
  992. void GuiCanvas::checkLockMouseMove( const GuiEvent& event )
  993. {
  994. GuiControl* controlHit = findHitControl( event.mousePoint );
  995. if( controlHit != mMouseControl )
  996. {
  997. if( mMouseControl == mMouseCapturedControl )
  998. mMouseCapturedControl->onMouseLeave( event );
  999. else if( controlHit == mMouseCapturedControl )
  1000. mMouseCapturedControl->onMouseEnter( event );
  1001. mMouseControl = controlHit;
  1002. }
  1003. }
  1004. void GuiCanvas::rootMouseUp(const GuiEvent &event)
  1005. {
  1006. mPrevMouseTime = Platform::getVirtualMilliseconds();
  1007. mMouseButtonDown = false;
  1008. // pass the event to the mouse locked control
  1009. if (bool(mMouseCapturedControl))
  1010. {
  1011. checkLockMouseMove( event );
  1012. mMouseCapturedControl->onMouseUp(event);
  1013. }
  1014. else
  1015. {
  1016. findMouseControl(event);
  1017. if(bool(mMouseControl))
  1018. mMouseControl->onMouseUp(event);
  1019. }
  1020. }
  1021. void GuiCanvas::rootMouseDragged(const GuiEvent &event)
  1022. {
  1023. //pass the event to the mouse locked control
  1024. if (bool(mMouseCapturedControl))
  1025. {
  1026. checkLockMouseMove( event );
  1027. mMouseCapturedControl->onMouseDragged(event);
  1028. }
  1029. else
  1030. {
  1031. findMouseControl(event);
  1032. if(bool(mMouseControl))
  1033. mMouseControl->onMouseDragged(event);
  1034. }
  1035. }
  1036. void GuiCanvas::rootMouseMove(const GuiEvent &event)
  1037. {
  1038. if (bool(mMouseCapturedControl))
  1039. {
  1040. mMouseCapturedControl->onMouseMove(event);
  1041. }
  1042. else
  1043. {
  1044. findMouseControl(event);
  1045. if(bool(mMouseControl))
  1046. mMouseControl->onMouseMove(event);
  1047. }
  1048. }
  1049. void GuiCanvas::rootRightMouseDown(const GuiEvent &event)
  1050. {
  1051. mPrevMouseTime = Platform::getVirtualMilliseconds();
  1052. mMouseRightButtonDown = true;
  1053. if (bool(mMouseCapturedControl))
  1054. {
  1055. checkLockMouseMove( event );
  1056. mMouseCapturedControl->onRightMouseDown(event);
  1057. }
  1058. else
  1059. {
  1060. findMouseControl(event);
  1061. if(bool(mMouseControl))
  1062. {
  1063. mMouseControl->onRightMouseDown(event);
  1064. }
  1065. }
  1066. }
  1067. void GuiCanvas::rootRightMouseUp(const GuiEvent &event)
  1068. {
  1069. mPrevMouseTime = Platform::getVirtualMilliseconds();
  1070. mMouseRightButtonDown = false;
  1071. if (bool(mMouseCapturedControl))
  1072. mMouseCapturedControl->onRightMouseUp(event);
  1073. else
  1074. {
  1075. findMouseControl(event);
  1076. if(bool(mMouseControl))
  1077. mMouseControl->onRightMouseUp(event);
  1078. }
  1079. }
  1080. void GuiCanvas::rootRightMouseDragged(const GuiEvent &event)
  1081. {
  1082. mPrevMouseTime = Platform::getVirtualMilliseconds();
  1083. if (bool(mMouseCapturedControl))
  1084. {
  1085. mMouseCapturedControl->onRightMouseDragged(event);
  1086. }
  1087. else
  1088. {
  1089. findMouseControl(event);
  1090. if(bool(mMouseControl))
  1091. mMouseControl->onRightMouseDragged(event);
  1092. }
  1093. }
  1094. void GuiCanvas::rootMiddleMouseDown(const GuiEvent &event)
  1095. {
  1096. mPrevMouseTime = Platform::getVirtualMilliseconds();
  1097. mMouseMiddleButtonDown = true;
  1098. if (bool(mMouseCapturedControl))
  1099. mMouseCapturedControl->onMiddleMouseDown(event);
  1100. else
  1101. {
  1102. findMouseControl(event);
  1103. if(bool(mMouseControl))
  1104. {
  1105. mMouseControl->onMiddleMouseDown(event);
  1106. }
  1107. }
  1108. }
  1109. void GuiCanvas::rootMiddleMouseUp(const GuiEvent &event)
  1110. {
  1111. mPrevMouseTime = Platform::getVirtualMilliseconds();
  1112. mMouseMiddleButtonDown = false;
  1113. if (bool(mMouseCapturedControl))
  1114. mMouseCapturedControl->onMiddleMouseUp(event);
  1115. else
  1116. {
  1117. findMouseControl(event);
  1118. if(bool(mMouseControl))
  1119. mMouseControl->onMiddleMouseUp(event);
  1120. }
  1121. }
  1122. void GuiCanvas::rootMiddleMouseDragged(const GuiEvent &event)
  1123. {
  1124. mPrevMouseTime = Platform::getVirtualMilliseconds();
  1125. if (bool(mMouseCapturedControl))
  1126. {
  1127. checkLockMouseMove( event );
  1128. mMouseCapturedControl->onMiddleMouseDragged(event);
  1129. }
  1130. else
  1131. {
  1132. findMouseControl(event);
  1133. if(bool(mMouseControl))
  1134. mMouseControl->onMiddleMouseDragged(event);
  1135. }
  1136. }
  1137. bool GuiCanvas::rootMouseWheelUp(const GuiEvent &event)
  1138. {
  1139. if (bool(mMouseCapturedControl))
  1140. return mMouseCapturedControl->onMouseWheelUp(event);
  1141. else
  1142. {
  1143. findMouseControl(event);
  1144. if (bool(mMouseControl))
  1145. return mMouseControl->onMouseWheelUp(event);
  1146. }
  1147. return false;
  1148. }
  1149. bool GuiCanvas::rootMouseWheelDown(const GuiEvent &event)
  1150. {
  1151. if (bool(mMouseCapturedControl))
  1152. return mMouseCapturedControl->onMouseWheelDown(event);
  1153. else
  1154. {
  1155. findMouseControl(event);
  1156. if (bool(mMouseControl))
  1157. return mMouseControl->onMouseWheelDown(event);
  1158. }
  1159. return false;
  1160. }
  1161. void GuiCanvas::setContentControl(GuiControl *gui)
  1162. {
  1163. // Skip out if we got passed NULL (why would that happen?)
  1164. if(!gui)
  1165. return;
  1166. GuiControl *oldContent = getContentControl();
  1167. if(oldContent)
  1168. Con::executef(oldContent, "onUnsetContent", Con::getIntArg(gui->getId()));
  1169. //remove all dialogs on layer 0
  1170. U32 index = 0;
  1171. while (size() > index)
  1172. {
  1173. GuiControl *ctrl = static_cast<GuiControl*>((*this)[index]);
  1174. if (ctrl == gui || ctrl->mLayer != 0)
  1175. index++;
  1176. Sim::getGuiGroup()->addObject( ctrl );
  1177. }
  1178. // set current menu bar
  1179. setMenuBar( mMenuBarCtrl );
  1180. // lose the first responder from the old GUI
  1181. GuiControl* responder = gui->findFirstTabable();
  1182. if(responder)
  1183. responder->setFirstResponder();
  1184. //add the gui to the front
  1185. if(!size() || gui != (*this)[0])
  1186. {
  1187. // automatically wakes objects in GuiControl::onWake
  1188. addObject(gui);
  1189. if (size() >= 2)
  1190. reOrder(gui, *begin());
  1191. }
  1192. //refresh the entire gui
  1193. resetUpdateRegions();
  1194. //rebuild the accelerator map
  1195. mAcceleratorMap.clear();
  1196. for(iterator i = end(); i != begin() ; )
  1197. {
  1198. i--;
  1199. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  1200. ctrl->buildAcceleratorMap();
  1201. if (ctrl->getControlProfile()->mModal)
  1202. break;
  1203. }
  1204. refreshMouseControl();
  1205. // Force the canvas to update the sizing of the new content control
  1206. maintainSizing();
  1207. // Do this last so onWake gets called first
  1208. Con::executef(gui, "onSetContent", Con::getIntArg(oldContent ? oldContent->getId() : 0));
  1209. }
  1210. GuiControl *GuiCanvas::getContentControl()
  1211. {
  1212. if(size() > 0)
  1213. return (GuiControl *) first();
  1214. return NULL;
  1215. }
  1216. void GuiCanvas::pushDialogControl(GuiControl *gui, S32 layer, bool center)
  1217. {
  1218. if( center )
  1219. gui->setPosition( getExtent().x / 2 - gui->getExtent().x / 2,
  1220. getExtent().y / 2 - gui->getExtent().y / 2 );
  1221. //add the gui
  1222. gui->mLayer = layer;
  1223. // GuiControl::addObject wakes the object
  1224. addObject(gui);
  1225. //reorder it to the correct layer
  1226. iterator i;
  1227. for (i = begin(); i != end(); i++)
  1228. {
  1229. GuiControl *ctrl = static_cast<GuiControl*>(*i);
  1230. if (ctrl->mLayer > gui->mLayer)
  1231. {
  1232. reOrder(gui, ctrl);
  1233. break;
  1234. }
  1235. }
  1236. //call the dialog push method
  1237. gui->onDialogPush();
  1238. //find the first responder
  1239. GuiControl* responder = gui->findFirstTabable();
  1240. if(responder)
  1241. responder->setFirstResponder();
  1242. // call the 'onWake' method?
  1243. //if(wakedGui)
  1244. // Con::executef(gui, 1, "onWake");
  1245. //refresh the entire gui
  1246. resetUpdateRegions();
  1247. //rebuild the accelerator map
  1248. mAcceleratorMap.clear();
  1249. if (size() > 0)
  1250. {
  1251. GuiControl *ctrl = static_cast<GuiControl*>(last());
  1252. ctrl->buildAcceleratorMap();
  1253. }
  1254. refreshMouseControl();
  1255. // I don't see the purpose of this, and it's causing issues when showing, for instance the
  1256. // metrics dialog while in a 3d scene, causing the cursor to be shown even when the mouse
  1257. // is locked [4/25/2007 justind]
  1258. //if(gui->mProfile && gui->mProfile->mModal)
  1259. // mPlatformWindow->getCursorController()->pushCursor(PlatformCursorController::curArrow);
  1260. }
  1261. void GuiCanvas::popDialogControl(GuiControl *gui)
  1262. {
  1263. if (size() < 1)
  1264. return;
  1265. //first, find the dialog, and call the "onDialogPop()" method
  1266. GuiControl *ctrl = NULL;
  1267. if (gui)
  1268. {
  1269. //make sure the gui really exists on the stack
  1270. iterator i;
  1271. bool found = false;
  1272. for(i = begin(); i != end(); i++)
  1273. {
  1274. GuiControl *check = static_cast<GuiControl *>(*i);
  1275. if (check == gui)
  1276. {
  1277. ctrl = check;
  1278. found = true;
  1279. }
  1280. }
  1281. if (!found)
  1282. return;
  1283. }
  1284. else
  1285. ctrl = static_cast<GuiControl*>(last());
  1286. //call the "on pop" function
  1287. ctrl->onDialogPop();
  1288. //now pop the last child (will sleep if awake)
  1289. Sim::getGuiGroup()->addObject(ctrl);
  1290. if (size() > 0)
  1291. {
  1292. GuiControl *ctrl = static_cast<GuiControl *>(last());
  1293. if( ctrl->getFirstResponder() )
  1294. ctrl->getFirstResponder()->setFirstResponder();
  1295. }
  1296. else
  1297. {
  1298. setFirstResponder(NULL);
  1299. }
  1300. //refresh the entire gui
  1301. resetUpdateRegions();
  1302. //rebuild the accelerator map
  1303. mAcceleratorMap.clear();
  1304. if (size() > 0)
  1305. {
  1306. GuiControl *ctrl = static_cast<GuiControl*>(last());
  1307. ctrl->buildAcceleratorMap();
  1308. }
  1309. refreshMouseControl();
  1310. }
  1311. void GuiCanvas::popDialogControl(S32 layer)
  1312. {
  1313. if (size() < 1)
  1314. return;
  1315. GuiControl *ctrl = NULL;
  1316. iterator i = end(); // find in z order (last to first)
  1317. while (i != begin())
  1318. {
  1319. i--;
  1320. ctrl = static_cast<GuiControl*>(*i);
  1321. if (ctrl->mLayer == layer)
  1322. break;
  1323. }
  1324. if (ctrl)
  1325. popDialogControl(ctrl);
  1326. }
  1327. void GuiCanvas::mouseLock(GuiControl *lockingControl)
  1328. {
  1329. if (bool(mMouseCapturedControl))
  1330. return;
  1331. mMouseCapturedControl = lockingControl;
  1332. if(mMouseControl && mMouseControl != mMouseCapturedControl)
  1333. {
  1334. GuiEvent evt;
  1335. evt.mousePoint.x = S32(mCursorPt.x);
  1336. evt.mousePoint.y = S32(mCursorPt.y);
  1337. mMouseControl->onMouseLeave(evt);
  1338. }
  1339. }
  1340. void GuiCanvas::mouseUnlock(GuiControl *lockingControl)
  1341. {
  1342. if (static_cast<GuiControl*>(mMouseCapturedControl) != lockingControl)
  1343. return;
  1344. GuiEvent evt;
  1345. evt.mousePoint.x = S32(mCursorPt.x);
  1346. evt.mousePoint.y = S32(mCursorPt.y);
  1347. GuiControl * controlHit = findHitControl(evt.mousePoint);
  1348. if(controlHit != mMouseCapturedControl)
  1349. {
  1350. mMouseControl = controlHit;
  1351. mMouseControlClicked = false;
  1352. if(bool(mMouseControl))
  1353. mMouseControl->onMouseEnter(evt);
  1354. }
  1355. mMouseCapturedControl = NULL;
  1356. }
  1357. void GuiCanvas::paint()
  1358. {
  1359. resetUpdateRegions();
  1360. // inhibit explicit refreshes in the case we're swapped out
  1361. if( mPlatformWindow && mPlatformWindow->isVisible() && GFX->allowRender())
  1362. mPlatformWindow->displayEvent.trigger(mPlatformWindow->getWindowId());
  1363. }
  1364. void GuiCanvas::repaint(U32 elapsedMS)
  1365. {
  1366. // Make sure we have a window.
  1367. if ( !mPlatformWindow )
  1368. return;
  1369. // Has enough time elapsed?
  1370. U32 elapsed = Platform::getRealMilliseconds() - mLastRenderMs;
  1371. if (elapsed < elapsedMS)
  1372. return;
  1373. // Do the render.
  1374. resetUpdateRegions();
  1375. handlePaintEvent(mPlatformWindow->getWindowId());
  1376. }
  1377. void GuiCanvas::maintainSizing()
  1378. {
  1379. Point2I size = getWindowSize();
  1380. if(size.x == -1 || size.y == -1)
  1381. return;
  1382. RectI screenRect(0, 0, size.x, size.y);
  1383. setBounds(screenRect);
  1384. // all bottom level controls should be the same dimensions as the canvas
  1385. // this is necessary for passing mouse events accurately
  1386. iterator i;
  1387. for (i = begin(); i != end(); i++)
  1388. {
  1389. AssertFatal(static_cast<GuiControl*>((*i))->isAwake(), "GuiCanvas::maintainSizing - ctrl is not awake");
  1390. GuiControl *ctrl = static_cast<GuiControl*>(*i);
  1391. Point2I ext = ctrl->getExtent();
  1392. Point2I pos = ctrl->getPosition();
  1393. Point2I newExt = screenRect.extent;
  1394. Point2I newPos = screenRect.point;
  1395. // if menubar is active displace content gui control
  1396. if (mMenuBarCtrl && (ctrl == getContentControl()))
  1397. {
  1398. /*const SimObject *menu = mMenuBarCtrl->findObjectByInternalName( StringTable->insert("menubar"), true);
  1399. if( !menu )
  1400. continue;
  1401. AssertFatal( dynamic_cast<const GuiControl*>(menu), "");*/
  1402. const U32 yOffset = static_cast<const GuiMenuBar*>(mMenuBarCtrl)->mMenubarHeight;
  1403. newPos.y += yOffset;
  1404. newExt.y -= yOffset;
  1405. }
  1406. if (pos != newPos || ext != newExt)
  1407. {
  1408. ctrl->resize(newPos, newExt);
  1409. resetUpdateRegions();
  1410. }
  1411. }
  1412. }
  1413. void GuiCanvas::setupFences()
  1414. {
  1415. // Destroy old fences
  1416. SAFE_DELETE_ARRAY( mFences );
  1417. // Now create the new ones
  1418. if( mNumFences > 0 )
  1419. {
  1420. mFences = new GFXFence*[mNumFences];
  1421. // Allocate the new fences
  1422. for( S32 i = 0; i < mNumFences; i++ )
  1423. mFences[i] = GFX->createFence();
  1424. }
  1425. // Reset state
  1426. mNextFenceIdx = 0;
  1427. }
  1428. void GuiCanvas::renderFrame(bool preRenderOnly, bool bufferSwap /* = true */)
  1429. {
  1430. AssertISV(mPlatformWindow, "GuiCanvas::renderFrame - no window present!");
  1431. if(!mPlatformWindow->isVisible() || !GFX->allowRender() || GFX->canCurrentlyRender())
  1432. return;
  1433. PROFILE_START(CanvasPreRender);
  1434. // Set our window as the current render target so we can see outputs.
  1435. GFX->setActiveRenderTarget(mPlatformWindow->getGFXTarget());
  1436. if (!GFX->getActiveRenderTarget())
  1437. {
  1438. PROFILE_END();
  1439. return;
  1440. }
  1441. #ifdef TORQUE_GFX_STATE_DEBUG
  1442. GFX->getDebugStateManager()->startFrame();
  1443. #endif
  1444. GFXTarget* renderTarget = GFX->getActiveRenderTarget();
  1445. if (renderTarget == NULL)
  1446. {
  1447. PROFILE_END();
  1448. return;
  1449. }
  1450. // Make sure the root control is the size of the canvas.
  1451. Point2I size = renderTarget->getSize();
  1452. if(size.x == 0 || size.y == 0)
  1453. {
  1454. PROFILE_END();
  1455. return;
  1456. }
  1457. RectI screenRect(0, 0, size.x, size.y);
  1458. maintainSizing();
  1459. //preRender (recursive) all controls
  1460. preRender();
  1461. PROFILE_END();
  1462. // Are we just doing pre-render?
  1463. if(preRenderOnly)
  1464. return;
  1465. // Signal the interested parties.
  1466. GuiCanvas::getGuiCanvasFrameSignal().trigger(true);
  1467. // Gross hack to make sure we don't end up with advanced lighting and msaa
  1468. // at the same time, which causes artifacts. At the same time we don't
  1469. // want to just throw the settings the user has chosen if the light manager
  1470. // changes at a later time.
  1471. GFXVideoMode mode = mPlatformWindow->getVideoMode();
  1472. if ( dStricmp( LIGHTMGR->getId(), "ADVLM" ) == 0 && mode.antialiasLevel > 0 )
  1473. {
  1474. const char *pref = Con::getVariable( "$pref::Video::mode" );
  1475. mode.parseFromString( pref );
  1476. mode.antialiasLevel = 0;
  1477. mPlatformWindow->setVideoMode(mode);
  1478. Con::printf( "AntiAliasing has been disabled; it is not compatible with AdvancedLighting." );
  1479. }
  1480. else if ( dStricmp( LIGHTMGR->getId(), "BLM" ) == 0)
  1481. {
  1482. const char *pref = Con::getVariable( "$pref::Video::mode" );
  1483. U32 prefAA = dAtoi( StringUnit::getUnit(pref, 5, " ") );
  1484. if ( prefAA != mode.antialiasLevel )
  1485. {
  1486. mode.parseFromString( pref );
  1487. mPlatformWindow->setVideoMode(mode);
  1488. Con::printf( "AntiAliasing has been enabled while running BasicLighting." );
  1489. }
  1490. }
  1491. // for now, just always reset the update regions - this is a
  1492. // fix for FSAA on ATI cards
  1493. resetUpdateRegions();
  1494. PROFILE_START(CanvasRenderControls);
  1495. // Draw the mouse
  1496. GuiCursor *mouseCursor = NULL;
  1497. bool cursorVisible = true;
  1498. if(bool(mMouseCapturedControl))
  1499. mMouseCapturedControl->getCursor(mouseCursor, cursorVisible, mLastEvent);
  1500. else if(bool(mMouseControl))
  1501. mMouseControl->getCursor(mouseCursor, cursorVisible, mLastEvent);
  1502. Point2I cursorPos((S32)mCursorPt.x, (S32)mCursorPt.y);
  1503. if(!mouseCursor)
  1504. mouseCursor = mDefaultCursor;
  1505. if(mLastCursorEnabled && mLastCursor)
  1506. {
  1507. Point2I spot = mLastCursor->getHotSpot();
  1508. Point2I cext = mLastCursor->getExtent();
  1509. Point2I pos = mLastCursorPt - spot;
  1510. addUpdateRegion(pos - Point2I(2, 2), Point2I(cext.x + 4, cext.y + 4));
  1511. }
  1512. if(cursorVisible && mouseCursor)
  1513. {
  1514. Point2I spot = mouseCursor->getHotSpot();
  1515. Point2I cext = mouseCursor->getExtent();
  1516. Point2I pos = cursorPos - spot;
  1517. addUpdateRegion(pos - Point2I(2, 2), Point2I(cext.x + 4, cext.y + 4));
  1518. }
  1519. mLastCursorEnabled = cursorVisible;
  1520. mLastCursor = mouseCursor;
  1521. mLastCursorPt = cursorPos;
  1522. // Begin GFX
  1523. PROFILE_START(GFXBeginScene);
  1524. bool beginSceneRes = GFX->beginScene();
  1525. PROFILE_END();
  1526. // Render all offscreen canvas objects here since we may need them in the render loop
  1527. if (GuiOffscreenCanvas::sList.size() != 0)
  1528. {
  1529. // Reset the entire state since oculus shit will have barfed it.
  1530. //GFX->disableShaders(true);
  1531. GFX->updateStates(true);
  1532. for (Vector<GuiOffscreenCanvas*>::iterator itr = GuiOffscreenCanvas::sList.begin(); itr != GuiOffscreenCanvas::sList.end(); itr++)
  1533. {
  1534. (*itr)->renderFrame(false, false);
  1535. }
  1536. GFX->setActiveRenderTarget(renderTarget);
  1537. }
  1538. // Can't render if waiting for device to reset.
  1539. if ( !beginSceneRes )
  1540. {
  1541. PROFILE_END(); // CanvasRenderControls
  1542. // Since we already triggered the signal once for begin-of-frame,
  1543. // we should be consistent and trigger it again for end-of-frame.
  1544. GuiCanvas::getGuiCanvasFrameSignal().trigger(false);
  1545. return;
  1546. }
  1547. // Clear the current viewport area
  1548. GFX->setViewport( screenRect );
  1549. GFX->clear( GFXClearZBuffer | GFXClearStencil | GFXClearTarget, gCanvasClearColor, 1.0f, 0 );
  1550. resetUpdateRegions();
  1551. // Make sure we have a clean matrix state
  1552. // before we start rendering anything!
  1553. GFX->setWorldMatrix( MatrixF::Identity );
  1554. GFX->setViewMatrix( MatrixF::Identity );
  1555. GFX->setProjectionMatrix( MatrixF::Identity );
  1556. // If we're taking a screenshot then let it have
  1557. // a chance at altering the view matrix.
  1558. if ( gScreenShot && gScreenShot->isPending() )
  1559. gScreenShot->tileGui( size );
  1560. RectI updateUnion;
  1561. buildUpdateUnion(&updateUnion);
  1562. if (updateUnion.intersect(screenRect))
  1563. {
  1564. // Render active GUI Dialogs
  1565. for(iterator i = begin(); i != end(); i++)
  1566. {
  1567. // Get the control
  1568. GuiControl *contentCtrl = static_cast<GuiControl*>(*i);
  1569. GFX->setClipRect( updateUnion );
  1570. GFX->setStateBlock(mDefaultGuiSB);
  1571. contentCtrl->onRender(contentCtrl->getPosition(), updateUnion);
  1572. }
  1573. // Fill Black if no Dialogs
  1574. if(this->size() == 0)
  1575. GFX->clear( GFXClearTarget, ColorI(0,0,0,0), 1.0f, 0 );
  1576. // Tooltip resource
  1577. if(bool(mMouseControl))
  1578. {
  1579. U32 curTime = Platform::getRealMilliseconds();
  1580. if(mHoverControl == mMouseControl)
  1581. {
  1582. if(mHoverPositionSet || (curTime - mHoverControlStart) >= mHoverControl->mTipHoverTime || (curTime - mHoverLeftControlTime) <= mHoverControl->mTipHoverTime)
  1583. {
  1584. if(!mHoverPositionSet)
  1585. {
  1586. mHoverPosition = cursorPos;
  1587. }
  1588. mHoverPositionSet = mMouseControl->mRenderTooltipDelegate( mHoverPosition, cursorPos, NULL );
  1589. }
  1590. }
  1591. else
  1592. {
  1593. if(mHoverPositionSet)
  1594. {
  1595. mHoverLeftControlTime = curTime;
  1596. mHoverPositionSet = false;
  1597. }
  1598. mHoverControl = mMouseControl;
  1599. mHoverControlStart = curTime;
  1600. }
  1601. }
  1602. GFX->setClipRect( updateUnion );
  1603. // Draw an ugly box if we don't have a cursor available...
  1604. //if (mCursorEnabled && mShowCursor && !mouseCursor)
  1605. //{
  1606. // GFX->drawRectFill( RectI( mCursorPt.x, mCursorPt.y, mCursorPt.x + 2, mCursorPt.y + 2 ), ColorI( 255, 0, 0 ) );
  1607. //}
  1608. // CodeReview - Make sure our bitmap modulation is clear or else there's a black modulation
  1609. // that ruins rendering of textures at startup.. This was done in mouseCursor
  1610. // onRender and so at startup when it wasn't called the modulation was black, ruining
  1611. // the loading screen display. This fixes the issue, but is it only masking a deeper issue
  1612. // in GFX with regard to gui rendering? [5/3/2007 justind]
  1613. GFX->getDrawUtil()->clearBitmapModulation();
  1614. // Really draw the cursor. :)
  1615. // Only if the platform cursor controller is missing or the platform cursor
  1616. // isn't visible.
  1617. if (!mPlatformWindow->getCursorController() || (mCursorEnabled && mouseCursor && mShowCursor &&
  1618. !mPlatformWindow->getCursorController()->isCursorVisible()))
  1619. {
  1620. Point2I pos((S32)mCursorPt.x, (S32)mCursorPt.y);
  1621. Point2I spot = mouseCursor->getHotSpot();
  1622. pos -= spot;
  1623. mouseCursor->render(pos);
  1624. }
  1625. }
  1626. // Render all RTT end of frame updates HERE
  1627. //DynamicTexture::updateScreenTextures();
  1628. //DynamicTexture::updateEndOfFrameTextures();
  1629. // mPending is set when the console function "screenShot()" is called
  1630. // this situation is necessary because it needs to take the screenshot
  1631. // before the buffers swap
  1632. PROFILE_END();
  1633. // Fence logic here, because this is where endScene is called.
  1634. if( mNumFences > 0 )
  1635. {
  1636. // Issue next fence
  1637. mFences[mNextFenceIdx]->issue();
  1638. mNextFenceIdx++;
  1639. // Wrap the next fence around to first if we're maxxed
  1640. if( mNextFenceIdx >= mNumFences )
  1641. mNextFenceIdx = 0;
  1642. // Block on previous fence
  1643. mFences[mNextFenceIdx]->block();
  1644. }
  1645. PROFILE_START(GFXEndScene);
  1646. GFX->endScene();
  1647. PROFILE_END();
  1648. GFX->getDeviceEventSignal().trigger( GFXDevice::dePostFrame );
  1649. swapBuffers();
  1650. GuiCanvas::getGuiCanvasFrameSignal().trigger(false);
  1651. #ifdef TORQUE_GFX_STATE_DEBUG
  1652. GFX->getDebugStateManager()->endFrame();
  1653. #endif
  1654. // Keep track of the last time we rendered.
  1655. mLastRenderMs = Platform::getRealMilliseconds();
  1656. }
  1657. GuiCanvas::GuiCanvasFrameSignal& GuiCanvas::getGuiCanvasFrameSignal()
  1658. {
  1659. static GuiCanvasFrameSignal theSignal;
  1660. return theSignal;
  1661. }
  1662. void GuiCanvas::swapBuffers()
  1663. {
  1664. AssertISV(mPlatformWindow, "GuiCanvas::swapBuffers - no window present!");
  1665. if(!mPlatformWindow->isVisible())
  1666. return;
  1667. PROFILE_START(SwapBuffers);
  1668. mPlatformWindow->getGFXTarget()->present();
  1669. PROFILE_END();
  1670. }
  1671. void GuiCanvas::buildUpdateUnion(RectI *updateUnion)
  1672. {
  1673. *updateUnion = mOldUpdateRects[0];
  1674. //the update region should encompass the oldUpdateRects, and the curUpdateRect
  1675. Point2I upperL;
  1676. Point2I lowerR;
  1677. upperL.x = getMin(mOldUpdateRects[0].point.x, mOldUpdateRects[1].point.x);
  1678. upperL.x = getMin(upperL.x, mCurUpdateRect.point.x);
  1679. upperL.y = getMin(mOldUpdateRects[0].point.y, mOldUpdateRects[1].point.y);
  1680. upperL.y = getMin(upperL.y, mCurUpdateRect.point.y);
  1681. lowerR.x = getMax(mOldUpdateRects[0].point.x + mOldUpdateRects[0].extent.x, mOldUpdateRects[1].point.x + mOldUpdateRects[1].extent.x);
  1682. lowerR.x = getMax(lowerR.x, mCurUpdateRect.point.x + mCurUpdateRect.extent.x);
  1683. lowerR.y = getMax(mOldUpdateRects[0].point.y + mOldUpdateRects[0].extent.y, mOldUpdateRects[1].point.y + mOldUpdateRects[1].extent.y);
  1684. lowerR.y = getMax(lowerR.y, mCurUpdateRect.point.y + mCurUpdateRect.extent.y);
  1685. updateUnion->point = upperL;
  1686. updateUnion->extent = lowerR - upperL;
  1687. //shift the oldUpdateRects
  1688. mOldUpdateRects[0] = mOldUpdateRects[1];
  1689. mOldUpdateRects[1] = mCurUpdateRect;
  1690. mCurUpdateRect.point.set(0,0);
  1691. mCurUpdateRect.extent.set(0,0);
  1692. }
  1693. void GuiCanvas::addUpdateRegion(Point2I pos, Point2I ext)
  1694. {
  1695. if(mCurUpdateRect.extent.x == 0)
  1696. {
  1697. mCurUpdateRect.point = pos;
  1698. mCurUpdateRect.extent = ext;
  1699. }
  1700. else
  1701. {
  1702. Point2I upperL;
  1703. upperL.x = getMin(mCurUpdateRect.point.x, pos.x);
  1704. upperL.y = getMin(mCurUpdateRect.point.y, pos.y);
  1705. Point2I lowerR;
  1706. lowerR.x = getMax(mCurUpdateRect.point.x + mCurUpdateRect.extent.x, pos.x + ext.x);
  1707. lowerR.y = getMax(mCurUpdateRect.point.y + mCurUpdateRect.extent.y, pos.y + ext.y);
  1708. mCurUpdateRect.point = upperL;
  1709. mCurUpdateRect.extent = lowerR - upperL;
  1710. }
  1711. }
  1712. void GuiCanvas::resetUpdateRegions()
  1713. {
  1714. //DEBUG - get surface width and height
  1715. mOldUpdateRects[0] = getBounds();
  1716. mOldUpdateRects[1] = mOldUpdateRects[0];
  1717. mCurUpdateRect = mOldUpdateRects[0];
  1718. }
  1719. void GuiCanvas::setFirstResponder( GuiControl* newResponder )
  1720. {
  1721. GuiControl* oldResponder = mFirstResponder;
  1722. Parent::setFirstResponder( newResponder );
  1723. if( oldResponder == mFirstResponder )
  1724. return;
  1725. if( oldResponder && ( oldResponder != newResponder ) )
  1726. oldResponder->onLoseFirstResponder();
  1727. if( newResponder && ( newResponder != oldResponder ) )
  1728. newResponder->onGainFirstResponder();
  1729. }
  1730. DefineEngineMethod( GuiCanvas, getContent, S32, (),,
  1731. "@brief Get the GuiControl which is being used as the content.\n\n"
  1732. "@tsexample\n"
  1733. "Canvas.getContent();\n"
  1734. "@endtsexample\n\n"
  1735. "@return ID of current content control")
  1736. {
  1737. GuiControl *ctrl = object->getContentControl();
  1738. if(ctrl)
  1739. return ctrl->getId();
  1740. return -1;
  1741. }
  1742. DefineEngineMethod( GuiCanvas, setContent, void, (GuiControl* ctrl),,
  1743. "@brief Set the content of the canvas to a specified control.\n\n"
  1744. "@param ctrl ID or name of GuiControl to set content to\n\n"
  1745. "@tsexample\n"
  1746. "Canvas.setContent(PlayGui);\n"
  1747. "@endtsexample\n\n")
  1748. {
  1749. // Not using old error reporting until we modify the engineAPI - mperry
  1750. //GuiControl *gui = NULL;
  1751. // if(argv[2][0])
  1752. // {
  1753. // if (!Sim::findObject(argv[2], gui))
  1754. // {
  1755. // Con::printf("%s(): Invalid control: %s", argv[0], argv[2]);
  1756. // return;
  1757. // }
  1758. // }
  1759. if(!ctrl)
  1760. {
  1761. Con::errorf("GuiCanvas::setContent - Invalid control specified')");
  1762. return;
  1763. }
  1764. //set the new content control
  1765. object->setContentControl(ctrl);
  1766. }
  1767. ConsoleDocFragment _pushDialog(
  1768. "@brief Adds a dialog control onto the stack of dialogs\n\n"
  1769. "@param ctrl Dialog to add\n"
  1770. "@param layer Layer to put dialog on (optional)\n"
  1771. "@param center True to center dialog on canvas (optional)\n\n"
  1772. "@tsexample\n"
  1773. "Canvas.pushDialog(RecordingsDlg);\n"
  1774. "@endtsexample\n\n",
  1775. "GuiCanvas",
  1776. "void pushDialog( GuiControl ctrl, int layer=0, bool center=false);"
  1777. );
  1778. DefineConsoleMethod( GuiCanvas, pushDialog, void, (const char * ctrlName, S32 layer, bool center), ( 0, false), "(GuiControl ctrl, int layer=0, bool center=false)"
  1779. "@hide")
  1780. {
  1781. GuiControl *gui;
  1782. if (! Sim::findObject(ctrlName, gui))
  1783. {
  1784. Con::printf("pushDialog(): Invalid control: %s", ctrlName);
  1785. return;
  1786. }
  1787. //find the layer
  1788. //set the new content control
  1789. object->pushDialogControl(gui, layer, center);
  1790. }
  1791. ConsoleDocFragment _popDialog1(
  1792. "@brief Removes a specific dialog control\n\n"
  1793. "@param ctrl Dialog to pop\n"
  1794. "@tsexample\n"
  1795. "Canvas.popDialog(RecordingsDlg);\n"
  1796. "@endtsexample\n\n",
  1797. "GuiCanvas",
  1798. "void popDialog( GuiControl ctrl);"
  1799. );
  1800. ConsoleDocFragment _popDialog2(
  1801. "@brief Removes a dialog at the front most layer\n\n"
  1802. "@tsexample\n"
  1803. "// Pops whatever is on layer 0\n"
  1804. "Canvas.popDialog();\n"
  1805. "@endtsexample\n\n",
  1806. "GuiCanvas",
  1807. "void popDialog();"
  1808. );
  1809. DefineConsoleMethod( GuiCanvas, popDialog, void, (GuiControl * gui), (nullAsType<GuiControl*>()), "(GuiControl ctrl=NULL)"
  1810. "@hide")
  1811. {
  1812. if (gui)
  1813. object->popDialogControl(gui);
  1814. else
  1815. object->popDialogControl();
  1816. }
  1817. ConsoleDocFragment _popLayer1(
  1818. "@brief Removes the top most layer of dialogs\n\n"
  1819. "@tsexample\n"
  1820. "Canvas.popLayer();\n"
  1821. "@endtsexample\n\n",
  1822. "GuiCanvas",
  1823. "void popLayer();"
  1824. );
  1825. ConsoleDocFragment _popLayer2(
  1826. "@brief Removes a specified layer of dialogs\n\n"
  1827. "@param layer Number of the layer to pop\n\n"
  1828. "@tsexample\n"
  1829. "Canvas.popLayer(1);\n"
  1830. "@endtsexample\n\n",
  1831. "GuiCanvas",
  1832. "void popLayer(S32 layer);"
  1833. );
  1834. DefineConsoleMethod( GuiCanvas, popLayer, void, (S32 layer), (0), "(int layer)"
  1835. "@hide")
  1836. {
  1837. object->popDialogControl(layer);
  1838. }
  1839. DefineEngineMethod( GuiCanvas, cursorOn, void, (),,
  1840. "@brief Turns on the mouse cursor.\n\n"
  1841. "@tsexample\n"
  1842. "Canvas.cursorOn();\n"
  1843. "@endtsexample\n\n")
  1844. {
  1845. object->setCursorON(true);
  1846. }
  1847. DefineEngineMethod( GuiCanvas, cursorOff, void, (),,
  1848. "@brief Turns on the mouse off.\n\n"
  1849. "@tsexample\n"
  1850. "Canvas.cursorOff();\n"
  1851. "@endtsexample\n\n")
  1852. {
  1853. object->setCursorON(false);
  1854. }
  1855. DefineEngineMethod( GuiCanvas, setCursor, void, (GuiCursor* cursor),,
  1856. "@brief Sets the cursor for the canvas.\n\n"
  1857. "@param cursor Name of the GuiCursor to use\n\n"
  1858. "@tsexample\n"
  1859. "Canvas.setCursor(\"DefaultCursor\");\n"
  1860. "@endtsexample\n\n")
  1861. {
  1862. if(!cursor)
  1863. {
  1864. Con::errorf("GuiCanvas::setCursor - Invalid GuiCursor name or ID");
  1865. return;
  1866. }
  1867. object->setCursor(cursor);
  1868. }
  1869. DefineEngineMethod( GuiCanvas, renderFront, void, ( bool enable ),,
  1870. "@brief This turns on/off front-buffer rendering.\n\n"
  1871. "@param enable True if all rendering should be done to the front buffer\n\n"
  1872. "@tsexample\n"
  1873. "Canvas.renderFront(false);\n"
  1874. "@endtsexample\n\n")
  1875. {
  1876. object->setRenderFront(enable);
  1877. }
  1878. DefineEngineMethod( GuiCanvas, showCursor, void, (),,
  1879. "@brief Enable rendering of the cursor.\n\n"
  1880. "@tsexample\n"
  1881. "Canvas.showCursor();\n"
  1882. "@endtsexample\n\n")
  1883. {
  1884. object->showCursor(true);
  1885. }
  1886. DefineEngineMethod( GuiCanvas, hideCursor, void, (),,
  1887. "@brief Disable rendering of the cursor.\n\n"
  1888. "@tsexample\n"
  1889. "Canvas.hideCursor();\n"
  1890. "@endtsexample\n\n")
  1891. {
  1892. object->showCursor(false);
  1893. }
  1894. DefineEngineMethod( GuiCanvas, isCursorOn, bool, (),,
  1895. "@brief Determines if mouse cursor is enabled.\n\n"
  1896. "@tsexample\n"
  1897. "// Is cursor on?\n"
  1898. "if(Canvas.isCursorOn())\n"
  1899. " echo(\"Canvas cursor is on\");\n"
  1900. "@endtsexample\n\n"
  1901. "@return Returns true if the cursor is on.\n\n")
  1902. {
  1903. return object->isCursorON();
  1904. }
  1905. DefineEngineMethod( GuiCanvas, isCursorShown, bool, (),,
  1906. "@brief Determines if mouse cursor is rendering.\n\n"
  1907. "@tsexample\n"
  1908. "// Is cursor rendering?\n"
  1909. "if(Canvas.isCursorShown())\n"
  1910. " echo(\"Canvas cursor is rendering\");\n"
  1911. "@endtsexample\n\n"
  1912. "@return Returns true if the cursor is rendering.\n\n")
  1913. {
  1914. return object->isCursorShown();
  1915. }
  1916. DefineEngineMethod( GuiCanvas, repaint, void, ( S32 elapsedMS ), (0),
  1917. "@brief Force canvas to redraw.\n"
  1918. "If the elapsed time is greater than the time since the last paint "
  1919. "then the repaint will be skipped.\n"
  1920. "@param elapsedMS The optional elapsed time in milliseconds.\n\n"
  1921. "@tsexample\n"
  1922. "Canvas.repaint();\n"
  1923. "@endtsexample\n\n")
  1924. {
  1925. object->repaint(elapsedMS < 0 ? 0 : elapsedMS);
  1926. }
  1927. DefineEngineMethod( GuiCanvas, reset, void, (),,
  1928. "@brief Reset the update regions for the canvas.\n\n"
  1929. "@tsexample\n"
  1930. "Canvas.reset();\n"
  1931. "@endtsexample\n\n")
  1932. {
  1933. object->resetUpdateRegions();
  1934. }
  1935. DefineEngineMethod( GuiCanvas, getCursorPos, Point2I, (),,
  1936. "@brief Get the current position of the cursor in screen-space. Note that this position"
  1937. " might be outside the Torque window. If you want to get the position within the Canvas,"
  1938. " call screenToClient on the result.\n\n"
  1939. "@see Canvas::screenToClient()\n\n"
  1940. "@param param Description\n\n"
  1941. "@tsexample\n"
  1942. "%cursorPos = Canvas.getCursorPos();\n"
  1943. "@endtsexample\n\n"
  1944. "@return Screen coordinates of mouse cursor, in format \"X Y\"")
  1945. {
  1946. return object->getCursorPos();
  1947. }
  1948. ConsoleDocFragment _setCursorPos1(
  1949. "@brief Sets the position of the cursor\n\n"
  1950. "@param pos Point, in screenspace for the cursor. Formatted as (\"x y\")\n\n"
  1951. "@tsexample\n"
  1952. "Canvas.setCursorPos(\"0 0\");\n"
  1953. "@endtsexample\n\n",
  1954. "GuiCanvas",
  1955. "bool setCursorPos( Point2I pos );"
  1956. );
  1957. ConsoleDocFragment _setCursorPos2(
  1958. "@brief Sets the position of the cursor\n\n"
  1959. "@param posX X-coordinate, in screenspace for the cursor.\n"
  1960. "@param posY Y-coordinate, in screenspace for the cursor.\n\n"
  1961. "@tsexample\n"
  1962. "Canvas.setCursorPos(0,0);\n"
  1963. "@endtsexample\n\n",
  1964. "GuiCanvas",
  1965. "bool setCursorPos( F32 posX, F32 posY);"
  1966. );
  1967. DefineConsoleMethod( GuiCanvas, setCursorPos, void, (Point2I pos), , "(Point2I pos)"
  1968. "@hide")
  1969. {
  1970. object->setCursorPos(pos);
  1971. }
  1972. DefineEngineMethod( GuiCanvas, getMouseControl, S32, (),,
  1973. "@brief Gets the gui control under the mouse.\n\n"
  1974. "@tsexample\n"
  1975. "%underMouse = Canvas.getMouseControl();\n"
  1976. "@endtsexample\n\n"
  1977. "@return ID of the gui control, if one was found. NULL otherwise")
  1978. {
  1979. GuiControl* control = object->getMouseControl();
  1980. if (control)
  1981. return control->getId();
  1982. return NULL;
  1983. }
  1984. DefineEngineFunction(excludeOtherInstance, bool, (const char* appIdentifer),,
  1985. "@brief Used to exclude/prevent all other instances using the same identifier specified\n\n"
  1986. "@note Not used on OSX, Xbox, or in Win debug builds\n\n"
  1987. "@param appIdentifier Name of the app set up for exclusive use.\n"
  1988. "@return False if another app is running that specified the same appIdentifier\n\n"
  1989. "@ingroup Platform\n"
  1990. "@ingroup GuiCore")
  1991. {
  1992. // mac can only run one instance in general.
  1993. #if !defined(TORQUE_OS_MAC) && !defined(TORQUE_DEBUG) && !defined(TORQUE_OS_LINUX)
  1994. return Platform::excludeOtherInstances(appIdentifer);
  1995. #else
  1996. // We can just return true if we get here.
  1997. return true;
  1998. #endif
  1999. }
  2000. DefineEngineMethod( GuiCanvas, getExtent, Point2I, (),,
  2001. "@brief Returns the dimensions of the canvas\n\n"
  2002. "@tsexample\n"
  2003. "%extent = Canvas.getExtent();\n"
  2004. "@endtsexample\n\n"
  2005. "@return Width and height of canvas. Formatted as numerical values in a single string \"# #\"")
  2006. {
  2007. return object->getExtent();
  2008. }
  2009. DefineEngineMethod( GuiCanvas, setWindowTitle, void, ( const char* newTitle),,
  2010. "@brief Change the title of the OS window.\n\n"
  2011. "@param newTitle String containing the new name\n\n"
  2012. "@tsexample\n"
  2013. "Canvas.setWindowTitle(\"Documentation Rocks!\");\n"
  2014. "@endtsexample\n\n")
  2015. {
  2016. object->setWindowTitle(newTitle);
  2017. }
  2018. DefineEngineMethod( GuiCanvas, findFirstMatchingMonitor, S32, (const char* name),,
  2019. "@brief Find the first monitor index that matches the given name.\n\n"
  2020. "The actual match algorithm depends on the implementation.\n"
  2021. "@param name The name to search for.\n\n"
  2022. "@return The number of monitors attached to the system, including the default monoitor.")
  2023. {
  2024. return PlatformWindowManager::get()->findFirstMatchingMonitor(name);
  2025. }
  2026. DefineEngineMethod( GuiCanvas, getMonitorCount, S32, (),,
  2027. "@brief Gets the number of monitors attached to the system.\n\n"
  2028. "@return The number of monitors attached to the system, including the default monoitor.")
  2029. {
  2030. return PlatformWindowManager::get()->getMonitorCount();
  2031. }
  2032. DefineEngineMethod( GuiCanvas, getMonitorName, const char*, (S32 index),,
  2033. "@brief Gets the name of the requested monitor.\n\n"
  2034. "@param index The monitor index.\n\n"
  2035. "@return The name of the requested monitor.")
  2036. {
  2037. return PlatformWindowManager::get()->getMonitorName(index);
  2038. }
  2039. DefineEngineMethod( GuiCanvas, getMonitorRect, RectI, (S32 index),,
  2040. "@brief Gets the region of the requested monitor.\n\n"
  2041. "@param index The monitor index.\n\n"
  2042. "@return The rectangular region of the requested monitor.")
  2043. {
  2044. return PlatformWindowManager::get()->getMonitorRect(index);
  2045. }
  2046. DefineEngineMethod( GuiCanvas, getVideoMode, const char*, (),,
  2047. "@brief Gets the current screen mode as a string.\n\n"
  2048. "The return string will contain 5 values (width, height, fullscreen, bitdepth, refreshRate). "
  2049. "You will need to parse out each one for individual use.\n\n"
  2050. "@tsexample\n"
  2051. "%screenWidth = getWord(Canvas.getVideoMode(), 0);\n"
  2052. "%screenHeight = getWord(Canvas.getVideoMode(), 1);\n"
  2053. "%isFullscreen = getWord(Canvas.getVideoMode(), 2);\n"
  2054. "%bitdepth = getWord(Canvas.getVideoMode(), 3);\n"
  2055. "%refreshRate = getWord(Canvas.getVideoMode(), 4);\n"
  2056. "@endtsexample\n\n"
  2057. "@return String formatted with screen width, screen height, screen mode, bit depth, and refresh rate.")
  2058. {
  2059. // Grab the video mode.
  2060. if (!object->getPlatformWindow())
  2061. return "";
  2062. GFXVideoMode vm = object->getPlatformWindow()->getVideoMode();
  2063. char* buf = Con::getReturnBuffer(vm.toString());
  2064. return buf;
  2065. }
  2066. DefineEngineMethod( GuiCanvas, getModeCount, S32, (),,
  2067. "@brief Gets the number of modes available on this device.\n\n"
  2068. "@param param Description\n\n"
  2069. "@tsexample\n"
  2070. "%modeCount = Canvas.getModeCount()\n"
  2071. "@endtsexample\n\n"
  2072. "@return The number of video modes supported by the device")
  2073. {
  2074. if (!object->getPlatformWindow())
  2075. return 0;
  2076. // Grab the available mode list from the device.
  2077. const Vector<GFXVideoMode>* const modeList =
  2078. object->getPlatformWindow()->getGFXDevice()->getVideoModeList();
  2079. // Return the number of resolutions.
  2080. return modeList->size();
  2081. }
  2082. DefineEngineMethod( GuiCanvas, getMode, const char*, (S32 modeId),,
  2083. "@brief Gets information on the specified mode of this device.\n\n"
  2084. "@param modeId Index of the mode to get data from.\n"
  2085. "@return A video mode string given an adapter and mode index.\n\n"
  2086. "@see GuiCanvas::getVideoMode()")
  2087. {
  2088. if (!object->getPlatformWindow())
  2089. return 0;
  2090. // Grab the available mode list from the device.
  2091. const Vector<GFXVideoMode>* const modeList =
  2092. object->getPlatformWindow()->getGFXDevice()->getVideoModeList();
  2093. // Get the desired index and confirm it's valid.
  2094. S32 idx = modeId;
  2095. if((idx < 0) || (idx >= modeList->size()))
  2096. {
  2097. Con::errorf("GuiCanvas::getResolution - You requested an out of range index of %d. Please specify an index in the range [0, %d).", idx, modeList->size());
  2098. return "";
  2099. }
  2100. // Great - we got something valid, so convert the videomode into a
  2101. // string and return to the user.
  2102. GFXVideoMode vm = (*modeList)[idx];
  2103. char *retString = Con::getReturnBuffer(vm.toString());
  2104. return retString;
  2105. }
  2106. DefineEngineMethod( GuiCanvas, toggleFullscreen, void, (),,
  2107. "@brief toggle canvas from fullscreen to windowed mode or back.\n\n"
  2108. "@tsexample\n"
  2109. "// If we are in windowed mode, the following will put is in fullscreen\n"
  2110. "Canvas.toggleFullscreen();"
  2111. "@endtsexample\n\n")
  2112. {
  2113. if (Platform::getWebDeployment())
  2114. return;
  2115. if (!object->getPlatformWindow())
  2116. return;
  2117. if (Journal::IsRecording() || Journal::IsPlaying())
  2118. return;
  2119. // Get the window's video mode.
  2120. GFXVideoMode origMode = object->getPlatformWindow()->getVideoMode();
  2121. // And grab the device its using.
  2122. GFXDevice *device = object->getPlatformWindow()->getGFXDevice();
  2123. // Toggle the fullscreen bit.
  2124. GFXVideoMode newMode = origMode;
  2125. newMode.fullScreen = !origMode.fullScreen;
  2126. // CodeReview Toggling might be better served by reading the fullscreen
  2127. // or windowed video mode pref and setting that instead [bjg 5/2/07]
  2128. if(newMode.fullScreen == true)
  2129. {
  2130. // Are we going to fullscreen? If so find the first matching resolution that
  2131. // is equal to or bigger in size, and has same BPP - windows
  2132. // are often strangely sized and will need to be adjusted to a viable
  2133. // fullscreen res.
  2134. for(S32 i=0; i<device->getVideoModeList()->size(); i++)
  2135. {
  2136. const GFXVideoMode &newVm = (*(device->getVideoModeList()))[i];
  2137. if(newMode.resolution.x > newVm.resolution.x)
  2138. continue;
  2139. if(newMode.resolution.y > newVm.resolution.y)
  2140. continue;
  2141. if(newMode.bitDepth != newVm.bitDepth)
  2142. continue;
  2143. // Great - got a match.
  2144. newMode = newVm;
  2145. newMode.fullScreen = true;
  2146. break;
  2147. }
  2148. }
  2149. // Ok, we have new video mode. Set it!
  2150. object->getPlatformWindow()->setVideoMode(newMode);
  2151. }
  2152. DefineEngineMethod( GuiCanvas, clientToScreen, Point2I, ( Point2I coordinate ),,
  2153. "Translate a coordinate from canvas window-space to screen-space.\n"
  2154. "@param coordinate The coordinate in window-space.\n"
  2155. "@return The given coordinate translated to screen-space." )
  2156. {
  2157. if( !object->getPlatformWindow() )
  2158. return coordinate;
  2159. return object->getPlatformWindow()->clientToScreen( coordinate );
  2160. }
  2161. DefineEngineMethod( GuiCanvas, screenToClient, Point2I, ( Point2I coordinate ),,
  2162. "Translate a coordinate from screen-space to canvas window-space.\n"
  2163. "@param coordinate The coordinate in screen-space.\n"
  2164. "@return The given coordinate translated to window-space." )
  2165. {
  2166. if( !object->getPlatformWindow() )
  2167. return coordinate;
  2168. return object->getPlatformWindow()->screenToClient( coordinate );
  2169. }
  2170. DefineEngineMethod( GuiCanvas, getWindowPosition, Point2I, (),,
  2171. "Get the current position of the platform window associated with the canvas.\n"
  2172. "@return The window position of the canvas in screen-space." )
  2173. {
  2174. if( !object->getPlatformWindow() )
  2175. return Point2I( 0, 0 );
  2176. return object->getPlatformWindow()->getPosition();
  2177. }
  2178. DefineEngineMethod( GuiCanvas, setWindowPosition, void, ( Point2I position ),,
  2179. "Set the position of the platform window associated with the canvas.\n"
  2180. "@param position The new position of the window in screen-space." )
  2181. {
  2182. if( !object->getPlatformWindow() )
  2183. return;
  2184. object->getPlatformWindow()->setPosition( position );
  2185. }
  2186. DefineConsoleMethod( GuiCanvas, isFullscreen, bool, (), , "() - Is this canvas currently fullscreen?" )
  2187. {
  2188. if (Platform::getWebDeployment())
  2189. return false;
  2190. if (!object->getPlatformWindow())
  2191. return false;
  2192. return object->getPlatformWindow()->getVideoMode().fullScreen;
  2193. }
  2194. DefineConsoleMethod( GuiCanvas, minimizeWindow, void, (), , "() - minimize this canvas' window." )
  2195. {
  2196. PlatformWindow* window = object->getPlatformWindow();
  2197. if ( window )
  2198. window->minimize();
  2199. }
  2200. DefineConsoleMethod( GuiCanvas, isMinimized, bool, (), , "()" )
  2201. {
  2202. PlatformWindow* window = object->getPlatformWindow();
  2203. if ( window )
  2204. return window->isMinimized();
  2205. return false;
  2206. }
  2207. DefineConsoleMethod( GuiCanvas, isMaximized, bool, (), , "()" )
  2208. {
  2209. PlatformWindow* window = object->getPlatformWindow();
  2210. if ( window )
  2211. return window->isMaximized();
  2212. return false;
  2213. }
  2214. DefineConsoleMethod( GuiCanvas, maximizeWindow, void, (), , "() - maximize this canvas' window." )
  2215. {
  2216. PlatformWindow* window = object->getPlatformWindow();
  2217. if ( window )
  2218. window->maximize();
  2219. }
  2220. DefineConsoleMethod( GuiCanvas, restoreWindow, void, (), , "() - restore this canvas' window." )
  2221. {
  2222. PlatformWindow* window = object->getPlatformWindow();
  2223. if( window )
  2224. window->restore();
  2225. }
  2226. DefineConsoleMethod( GuiCanvas, setFocus, void, (), , "() - Claim OS input focus for this canvas' window.")
  2227. {
  2228. PlatformWindow* window = object->getPlatformWindow();
  2229. if( window )
  2230. window->setFocus();
  2231. }
  2232. DefineEngineMethod( GuiCanvas, setMenuBar, void, ( GuiControl* menu ),,
  2233. "Translate a coordinate from canvas window-space to screen-space.\n"
  2234. "@param coordinate The coordinate in window-space.\n"
  2235. "@return The given coordinate translated to screen-space." )
  2236. {
  2237. return object->setMenuBar( menu );
  2238. }
  2239. DefineConsoleMethod( GuiCanvas, setVideoMode, void,
  2240. (U32 width, U32 height, bool fullscreen, U32 bitDepth, U32 refreshRate, U32 antialiasLevel),
  2241. ( false, 0, 0, 0),
  2242. "(int width, int height, bool fullscreen, [int bitDepth], [int refreshRate], [int antialiasLevel] )\n"
  2243. "Change the video mode of this canvas. This method has the side effect of setting the $pref::Video::mode to the new values.\n\n"
  2244. "\\param width The screen width to set.\n"
  2245. "\\param height The screen height to set.\n"
  2246. "\\param fullscreen Specify true to run fullscreen or false to run in a window\n"
  2247. "\\param bitDepth [optional] The desired bit-depth. Defaults to the current setting. This parameter is ignored if you are running in a window.\n"
  2248. "\\param refreshRate [optional] The desired refresh rate. Defaults to the current setting. This parameter is ignored if you are running in a window"
  2249. "\\param antialiasLevel [optional] The level of anti-aliasing to apply 0 = none" )
  2250. {
  2251. if (!object->getPlatformWindow())
  2252. return;
  2253. if (Journal::IsRecording() || Journal::IsPlaying())
  2254. return;
  2255. // Update the video mode and tell the window to reset.
  2256. GFXVideoMode vm = object->getPlatformWindow()->getVideoMode();
  2257. bool changed = false;
  2258. if (width == 0 && height > 0)
  2259. {
  2260. // Our width is 0 but our height isn't...
  2261. // Try to find a matching width
  2262. for(S32 i=0; i<object->getPlatformWindow()->getGFXDevice()->getVideoModeList()->size(); i++)
  2263. {
  2264. const GFXVideoMode &newVm = (*(object->getPlatformWindow()->getGFXDevice()->getVideoModeList()))[i];
  2265. if(newVm.resolution.y == height)
  2266. {
  2267. width = newVm.resolution.x;
  2268. changed = true;
  2269. break;
  2270. }
  2271. }
  2272. }
  2273. else if (height == 0 && width > 0)
  2274. {
  2275. // Our height is 0 but our width isn't...
  2276. // Try to find a matching height
  2277. for(S32 i=0; i<object->getPlatformWindow()->getGFXDevice()->getVideoModeList()->size(); i++)
  2278. {
  2279. const GFXVideoMode &newVm = (*(object->getPlatformWindow()->getGFXDevice()->getVideoModeList()))[i];
  2280. if(newVm.resolution.x == width)
  2281. {
  2282. height = newVm.resolution.y;
  2283. changed = true;
  2284. break;
  2285. }
  2286. }
  2287. }
  2288. if (width == 0 || height == 0)
  2289. {
  2290. // Got a bad size for both of our dimensions or one of our dimensions and
  2291. // didn't get a match for the other default back to our current resolution
  2292. width = vm.resolution.x;
  2293. height = vm.resolution.y;
  2294. changed = true;
  2295. }
  2296. if (changed)
  2297. {
  2298. Con::errorf("GuiCanvas::setVideoMode(): Error - Invalid resolution of (%d, %d) - attempting (%d, %d)", width, height, width, height);
  2299. }
  2300. vm.resolution = Point2I(width, height);
  2301. vm.fullScreen = fullscreen;
  2302. if (Platform::getWebDeployment())
  2303. vm.fullScreen = false;
  2304. // These optional params are set to default at construction of vm. If they
  2305. // aren't specified, just leave them at whatever they were set to.
  2306. if (bitDepth > 0)
  2307. {
  2308. vm.bitDepth = bitDepth;
  2309. }
  2310. if (refreshRate > 0)
  2311. {
  2312. vm.refreshRate = refreshRate;
  2313. }
  2314. if (antialiasLevel > 0)
  2315. {
  2316. vm.antialiasLevel = antialiasLevel;
  2317. }
  2318. object->getPlatformWindow()->setVideoMode(vm);
  2319. // Store the new mode into a pref.
  2320. Con::setVariable( "$pref::Video::mode", vm.toString() );
  2321. }
  2322. ConsoleMethod( GuiCanvas, showWindow, void, 2, 2, "" )
  2323. {
  2324. if (!object->getPlatformWindow())
  2325. return;
  2326. object->getPlatformWindow()->show();
  2327. WindowManager->setDisplayWindow(true);
  2328. object->getPlatformWindow()->setDisplayWindow(true);
  2329. }
  2330. ConsoleMethod( GuiCanvas, hideWindow, void, 2, 2, "" )
  2331. {
  2332. if (!object->getPlatformWindow())
  2333. return;
  2334. object->getPlatformWindow()->hide();
  2335. WindowManager->setDisplayWindow(false);
  2336. object->getPlatformWindow()->setDisplayWindow(false);
  2337. }
  2338. ConsoleMethod( GuiCanvas, cursorClick, void, 4, 4, "button, isDown" )
  2339. {
  2340. const S32 buttonId = dAtoi(argv[2]);
  2341. const bool isDown = dAtob(argv[3]);
  2342. object->cursorClick(buttonId, isDown);
  2343. }
  2344. ConsoleMethod( GuiCanvas, cursorNudge, void, 4, 4, "x, y" )
  2345. {
  2346. object->cursorNudge(dAtof(argv[2]), dAtof(argv[3]));
  2347. }
  2348. // This function allows resetting of the video-mode from script. It was motivated by
  2349. // the need to temporarily disable vsync during datablock cache load to avoid a
  2350. // significant slowdown.
  2351. bool AFX_forceVideoReset = false;
  2352. ConsoleMethod( GuiCanvas, resetVideoMode, void, 2,2, "()")
  2353. {
  2354. PlatformWindow* window = object->getPlatformWindow();
  2355. if( window )
  2356. {
  2357. GFXWindowTarget* gfx_target = window->getGFXTarget();
  2358. if ( gfx_target )
  2359. {
  2360. AFX_forceVideoReset = true;
  2361. gfx_target->resetMode();
  2362. AFX_forceVideoReset = false;
  2363. }
  2364. }
  2365. }