guiCanvas.cpp 83 KB

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