guiCanvas.cpp 89 KB

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