guiCanvas.cpp 83 KB

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