guiCanvas.cpp 81 KB

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