guiControl.cc 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "console/consoleTypes.h"
  23. #include "console/console.h"
  24. #include "console/consoleInternal.h"
  25. #include "console/codeBlock.h"
  26. #include "platform/event.h"
  27. #include "graphics/gBitmap.h"
  28. #include "graphics/dgl.h"
  29. #include "input/actionMap.h"
  30. #include "gui/guiCanvas.h"
  31. #include "gui/guiControl.h"
  32. #include "gui/guiDefaultControlRender.h"
  33. #include "gui/editor/guiEditCtrl.h"
  34. #include "string/unicode.h"
  35. #include "collection/vector.h"
  36. #include "2d/core/Utility.h"
  37. #include "gui/containers/guiScrollCtrl.h"
  38. #include "gui/editor/guiEditCtrl.h"
  39. #include <sstream>
  40. #include <iostream>
  41. #include <vector>
  42. #include <string>
  43. #include "guiControl_ScriptBinding.h"
  44. #ifndef _FRAMEALLOCATOR_H_
  45. #include "memory/frameAllocator.h"
  46. #endif
  47. //------------------------------------------------------------------------------
  48. IMPLEMENT_CONOBJECT_CHILDREN(GuiControl);
  49. static EnumTable::Enums alignCtrlEnums[] =
  50. {
  51. { AlignmentType::LeftAlign, "left" },
  52. { AlignmentType::CenterAlign, "center" },
  53. { AlignmentType::RightAlign, "right" },
  54. { AlignmentType::DefaultAlign, "default" }
  55. };
  56. static EnumTable gAlignCtrlTable(3, &alignCtrlEnums[0]);
  57. static EnumTable::Enums vAlignCtrlEnums[] =
  58. {
  59. { VertAlignmentType::TopVAlign, "top" },
  60. { VertAlignmentType::MiddleVAlign, "middle" },
  61. { VertAlignmentType::BottomVAlign, "bottom" },
  62. { VertAlignmentType::DefaultVAlign, "default" }
  63. };
  64. static EnumTable gVAlignCtrlTable(3, &vAlignCtrlEnums[0]);
  65. //used to locate the next/prev responder when tab is pressed
  66. S32 GuiControl::smCursorChanged = -1;
  67. GuiControl *GuiControl::smPrevResponder = NULL;
  68. GuiControl *GuiControl::smCurResponder = NULL;
  69. GuiEditCtrl *GuiControl::smEditorHandle = NULL;
  70. bool GuiControl::smDesignTime = false;
  71. GuiControl::GuiControl()
  72. {
  73. mLayer = 0;
  74. mBounds.set(0, 0, 64, 64);
  75. mStoredExtent.set(0, 0);
  76. mRenderInsetLT.set(0, 0);
  77. mRenderInsetRB.set(0, 0);
  78. mMinExtent.set(0, 0);
  79. mStoredRelativePosH.set(0, 0);
  80. mStoredRelativePosV.set(0, 0);
  81. mUseRelPosH = false;
  82. mUseRelPosV = false;
  83. mProfile = NULL;
  84. mConsoleVariable = StringTable->EmptyString;
  85. mConsoleCommand = StringTable->EmptyString;
  86. mAltConsoleCommand = StringTable->EmptyString;
  87. mAcceleratorKey = StringTable->EmptyString;
  88. mLangTableName = StringTable->EmptyString;
  89. mText = StringTable->EmptyString;
  90. mTextID = StringTable->EmptyString;
  91. mAlignment = AlignmentType::DefaultAlign;
  92. mVAlignment = VertAlignmentType::DefaultVAlign;
  93. mFontSizeAdjust = 1;
  94. mFontColor.set(0, 0, 0, 255);
  95. mOverrideFontColor = false;
  96. mLangTable = NULL;
  97. mFirstResponder = NULL;
  98. mCanSaveFieldDictionary = false;
  99. mVisible = true;
  100. mActive = false;
  101. mAwake = false;
  102. mCanSave = true;
  103. mHorizSizing = horizResizeRight;
  104. mVertSizing = vertResizeBottom;
  105. mTooltipProfile = NULL;
  106. mTooltip = StringTable->EmptyString;
  107. mTipHoverTime = 1000;
  108. mTooltipWidth = 250;
  109. mIsContainer = true;
  110. mTextWrap = false;
  111. mTextExtend = false;
  112. mUseInput = true;
  113. }
  114. GuiControl::~GuiControl()
  115. {
  116. }
  117. bool GuiControl::onAdd()
  118. {
  119. // Let Parent Do Work.
  120. if(!Parent::onAdd())
  121. return false;
  122. // Grab the classname of this object
  123. const char *cName = getClassName();
  124. // if we're a pure GuiControl, then we're a container by default.
  125. if(dStrcmp("GuiControl", cName) == 0)
  126. mIsContainer = true;
  127. // Clamp to minExtent
  128. mBounds.extent.x = getMax( mMinExtent.x, mBounds.extent.x );
  129. mBounds.extent.y = getMax( mMinExtent.y, mBounds.extent.y );
  130. // Add to root group.
  131. Sim::getGuiGroup()->addObject(this);
  132. // Return Success.
  133. return true;
  134. }
  135. void GuiControl::onChildAdded( GuiControl *child )
  136. {
  137. if(mProfile)
  138. {
  139. //This will cause the child control to be centered if it needs to be.
  140. RectI innerRect = getInnerRect();
  141. child->parentResized(innerRect.extent, innerRect.extent);
  142. if (isMethod("onChildAdded"))
  143. {
  144. Con::executef(this, 3, "onChildAdded", child->getIdString());
  145. }
  146. }
  147. }
  148. void GuiControl::onChildRemoved(GuiControl* child)
  149. {
  150. if (mProfile && isMethod("onChildRemoved"))
  151. {
  152. Con::executef(this, 3, "onChildRemoved", child->getIdString());
  153. }
  154. }
  155. static EnumTable::Enums horzEnums[] =
  156. {
  157. { GuiControl::horizResizeRight, "right" },
  158. { GuiControl::horizResizeWidth, "width" },
  159. { GuiControl::horizResizeLeft, "left" },
  160. { GuiControl::horizResizeCenter, "center" },
  161. { GuiControl::horizResizeRelative, "relative" },
  162. { GuiControl::horizResizeFill, "fill" }
  163. };
  164. static EnumTable gHorizSizingTable(6, &horzEnums[0]);
  165. static EnumTable::Enums vertEnums[] =
  166. {
  167. { GuiControl::vertResizeBottom, "bottom" },
  168. { GuiControl::vertResizeHeight, "height" },
  169. { GuiControl::vertResizeTop, "top" },
  170. { GuiControl::vertResizeCenter, "center" },
  171. { GuiControl::vertResizeRelative, "relative" },
  172. { GuiControl::vertResizeFill, "fill" }
  173. };
  174. static EnumTable gVertSizingTable(6, &vertEnums[0]);
  175. void GuiControl::initPersistFields()
  176. {
  177. Parent::initPersistFields();
  178. // Things relevant only to the editor.
  179. addGroup("Gui Editing");
  180. addField("isContainer", TypeBool, Offset(mIsContainer, GuiControl));
  181. endGroup("Gui Editing");
  182. // Parent Group.
  183. addGroup("GuiControl");
  184. addField("Profile", TypeGuiProfile, Offset(mProfile, GuiControl));
  185. addField("HorizSizing", TypeEnum, Offset(mHorizSizing, GuiControl), 1, &gHorizSizingTable);
  186. addField("VertSizing", TypeEnum, Offset(mVertSizing, GuiControl), 1, &gVertSizingTable);
  187. addProtectedField("Position", TypePoint2I, Offset(mBounds.point, GuiControl), &setPositionFn, &defaultProtectedGetFn, "The location of the control in relation to its parent's content area.");
  188. addProtectedField("Extent", TypePoint2I, Offset(mBounds.extent, GuiControl), &setExtentFn, &defaultProtectedGetFn, "The size of the control writen as width and height.");
  189. addProtectedField("MinExtent", TypePoint2I, Offset(mMinExtent, GuiControl), &setMinExtentFn, &defaultProtectedGetFn, &writeMinExtentFn, "The extent will not shrink below this size.");
  190. addField("canSave", TypeBool, Offset(mCanSave, GuiControl));
  191. addField("Visible", TypeBool, Offset(mVisible, GuiControl));
  192. addField("useInput", TypeBool, Offset(mUseInput, GuiControl));
  193. addField("Variable", TypeString, Offset(mConsoleVariable, GuiControl));
  194. addField("Command", TypeString, Offset(mConsoleCommand, GuiControl));
  195. addField("AltCommand", TypeString, Offset(mAltConsoleCommand, GuiControl));
  196. addField("Accelerator", TypeString, Offset(mAcceleratorKey, GuiControl));
  197. addField("Active", TypeBool, Offset(mActive, GuiControl));
  198. endGroup("GuiControl");
  199. addGroup("ToolTip");
  200. addField("tooltipprofile", TypeGuiProfile, Offset(mTooltipProfile, GuiControl));
  201. addField("tooltip", TypeString, Offset(mTooltip, GuiControl));
  202. addField("tooltipWidth", TypeS32, Offset(mTooltipWidth, GuiControl));
  203. addField("hovertime", TypeS32, Offset(mTipHoverTime, GuiControl));
  204. endGroup("ToolTip");
  205. addGroup("Localization");
  206. addField("langTableMod", TypeString, Offset(mLangTableName, GuiControl));
  207. endGroup("Localization");
  208. addGroup("Text");
  209. addProtectedField("text", TypeCaseString, Offset(mText, GuiControl), setTextProperty, getTextProperty, "");
  210. addField("textID", TypeString, Offset(mTextID, GuiControl));
  211. addField("textWrap", TypeBool, Offset(mTextWrap, GuiControl), &writeTextWrapFn, "If true, text will wrap to additional lines.");
  212. addField("textExtend", TypeBool, Offset(mTextExtend, GuiControl), &writeTextExtendFn, "If true, extent will change based on the size of the control's text when possible.");
  213. addField("align", TypeEnum, Offset(mAlignment, GuiControl), 1, &gAlignCtrlTable);
  214. addField("vAlign", TypeEnum, Offset(mVAlignment, GuiControl), 1, &gVAlignCtrlTable);
  215. addField("fontSizeAdjust", TypeF32, Offset(mFontSizeAdjust, GuiControl), "A decimal value that is multiplied with the profile's fontSize to determine the control's actual font size.");
  216. addField("overrideFontColor", TypeBool, Offset(mOverrideFontColor, GuiControl), "If true, the control's fontColor will override the profile's font color.");
  217. addField("fontColor", TypeColorI, Offset(mFontColor, GuiControl), "A color to override the font color of the control's profile. OverrideFontColor must be set to true for this to work.");
  218. endGroup("Text");
  219. }
  220. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //
  221. LangTable * GuiControl::getGUILangTable()
  222. {
  223. if(mLangTable)
  224. return mLangTable;
  225. if(mLangTableName && *mLangTableName)
  226. {
  227. mLangTable = (LangTable *)getModLangTable((const UTF8*)mLangTableName);
  228. return mLangTable;
  229. }
  230. GuiControl *parent = getParent();
  231. if(parent)
  232. return parent->getGUILangTable();
  233. return NULL;
  234. }
  235. const UTF8 * GuiControl::getGUIString(S32 id)
  236. {
  237. LangTable *lt = getGUILangTable();
  238. if(lt)
  239. return lt->getString(id);
  240. return NULL;
  241. }
  242. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //
  243. void GuiControl::addObject(SimObject *object)
  244. {
  245. GuiControl *ctrl = dynamic_cast<GuiControl *>(object);
  246. if(!ctrl)
  247. {
  248. AssertWarn(0, "GuiControl::addObject: attempted to add NON GuiControl to set");
  249. return;
  250. }
  251. if(object->getGroup() == this)
  252. return;
  253. Parent::addObject(object);
  254. AssertFatal(!ctrl->isAwake(), "GuiControl::addObject: object is already awake before add");
  255. if(mAwake)
  256. ctrl->awaken();
  257. onChildAdded( ctrl );
  258. }
  259. void GuiControl::removeObject(SimObject *object)
  260. {
  261. GuiControl *ctrl = dynamic_cast<GuiControl *>(object);
  262. if (!ctrl)
  263. {
  264. AssertWarn(0, "GuiControl::removeObject: attempted to remove NON GuiControl from set");
  265. return;
  266. }
  267. AssertFatal(mAwake == ctrl->isAwake(), "GuiControl::removeObject: child control wake state is bad");
  268. if (mAwake)
  269. ctrl->sleep();
  270. Parent::removeObject(object);
  271. // If we are a child, notify our parent that we've been removed
  272. onChildRemoved(ctrl);
  273. }
  274. GuiControl *GuiControl::getParent()
  275. {
  276. SimObject *obj = getGroup();
  277. if (GuiControl* gui = dynamic_cast<GuiControl*>(obj))
  278. return gui;
  279. return 0;
  280. }
  281. GuiCanvas *GuiControl::getRoot()
  282. {
  283. GuiControl *root = NULL;
  284. GuiControl *parent = getParent();
  285. while (parent)
  286. {
  287. root = parent;
  288. parent = parent->getParent();
  289. }
  290. if (root)
  291. return dynamic_cast<GuiCanvas*>(root);
  292. else
  293. return NULL;
  294. }
  295. void GuiControl::inspectPreApply()
  296. {
  297. if(isEditMode())
  298. smEditorHandle->controlInspectPreApply(this);
  299. // The canvas never sleeps
  300. // This forced sleep will allow us to unload and reload things in the editor.
  301. mPreviouslyAwake = mAwake;
  302. if(mAwake && dynamic_cast<GuiCanvas*>(this) == NULL )
  303. {
  304. onSleep(); // release all our resources.
  305. }
  306. }
  307. void GuiControl::inspectPostApply()
  308. {
  309. // Shhhhhhh, you don't want to wake the canvas!
  310. // If this control was awake before we should revive it.
  311. if(mPreviouslyAwake && !mAwake && dynamic_cast<GuiCanvas*>(this) == NULL )
  312. {
  313. onWake();
  314. }
  315. if(isEditMode())
  316. smEditorHandle->controlInspectPostApply(this);
  317. }
  318. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //
  319. Point2I GuiControl::localToGlobalCoord(const Point2I &src)
  320. {
  321. Point2I ret = src;
  322. ret += (mBounds.point + mRenderInsetLT);
  323. GuiControl *walk = getParent();
  324. while(walk)
  325. {
  326. ret += (walk->getPosition() + walk->mRenderInsetLT);
  327. walk = walk->getParent();
  328. }
  329. return ret;
  330. }
  331. Point2I GuiControl::globalToLocalCoord(const Point2I &src)
  332. {
  333. Point2I ret = src;
  334. ret -= (mBounds.point + mRenderInsetLT);
  335. GuiControl *walk = getParent();
  336. while(walk)
  337. {
  338. ret -= (walk->getPosition() + walk->mRenderInsetLT);
  339. walk = walk->getParent();
  340. }
  341. return ret;
  342. }
  343. //----------------------------------------------------------------
  344. void GuiControl::resize(const Point2I &newPosition, const Point2I &newExtent)
  345. {
  346. Point2I actualNewExtent = Point2I(getMax(mMinExtent.x, newExtent.x),
  347. getMax(mMinExtent.y, newExtent.y));
  348. Point2I oldExtent = mBounds.extent;
  349. //force center if using center positioning
  350. Point2I oldPosition = mBounds.point;
  351. Point2I actualNewPosition = Point2I(newPosition);
  352. GuiControl* parent = getParent();
  353. if (parent && parent->mProfile)
  354. {
  355. Point2I parentInnerExtent = parent->getInnerRect().extent;
  356. if (mHorizSizing == horizResizeCenter)
  357. {
  358. actualNewPosition.x = (parentInnerExtent.x - actualNewExtent.x) / 2;
  359. }
  360. else if (mHorizSizing == horizResizeFill)
  361. {
  362. actualNewPosition.x = 0;
  363. actualNewExtent.x = parentInnerExtent.x;
  364. }
  365. if (mVertSizing == vertResizeCenter)
  366. {
  367. actualNewPosition.y = (parentInnerExtent.y - actualNewExtent.y) / 2;
  368. }
  369. else if (mVertSizing == vertResizeFill)
  370. {
  371. actualNewPosition.y = 0;
  372. actualNewExtent.y = parentInnerExtent.y;
  373. }
  374. }
  375. // only do the child control resizing stuff if you really need to.
  376. bool extentChanged = (actualNewExtent != oldExtent);
  377. bool positionChanged = (actualNewPosition != oldPosition);
  378. if (extentChanged) {
  379. //call set update both before and after
  380. setUpdate();
  381. mBounds.set(actualNewPosition, actualNewExtent);
  382. iterator i;
  383. for(i = begin(); i != end(); i++)
  384. {
  385. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  386. ctrl->parentResized(oldExtent - (ctrl->mRenderInsetLT + ctrl->mRenderInsetRB), actualNewExtent - (ctrl->mRenderInsetLT + ctrl->mRenderInsetRB));
  387. }
  388. if (parent)
  389. parent->childResized(this);
  390. setUpdate();
  391. if (isMethod("onResize"))
  392. {
  393. Con::executef(this, 2, "onResize");
  394. }
  395. }
  396. if(positionChanged)
  397. {
  398. mBounds.point = actualNewPosition;
  399. if(parent)
  400. parent->childMoved(this);
  401. if (isMethod("onMoved"))
  402. {
  403. Con::executef(this, 2, "onMoved");
  404. }
  405. }
  406. }
  407. void GuiControl::setPosition( const Point2I &newPosition )
  408. {
  409. resize( newPosition, mBounds.extent );
  410. }
  411. void GuiControl::setExtent( const Point2I &newExtent )
  412. {
  413. resize( mBounds.point, newExtent );
  414. }
  415. void GuiControl::setBounds( const RectI &newBounds )
  416. {
  417. resize( newBounds.point, newBounds.extent );
  418. }
  419. void GuiControl::setLeft( S32 newLeft )
  420. {
  421. resize( Point2I( newLeft, mBounds.point.y), mBounds.extent );
  422. }
  423. void GuiControl::setTop( S32 newTop )
  424. {
  425. resize( Point2I( mBounds.point.x, newTop ), mBounds.extent );
  426. }
  427. void GuiControl::setWidth( S32 newWidth )
  428. {
  429. resize( mBounds.point, Point2I( newWidth, mBounds.extent.y ) );
  430. }
  431. void GuiControl::setHeight( S32 newHeight )
  432. {
  433. resize( mBounds.point, Point2I( mBounds.extent.x, newHeight ) );
  434. }
  435. void GuiControl::childResized(GuiControl *child)
  436. {
  437. // Default to do nothing. Do not call resize from here as it will create an infinite loop.
  438. if (isMethod("onChildResized"))
  439. {
  440. Con::executef(this, 3, "onChildResized", child->getIdString());
  441. }
  442. }
  443. void GuiControl::childMoved(GuiControl* child)
  444. {
  445. // Default to do nothing. Do not call resize from here as it will create an infinite loop.
  446. if (isMethod("onChildMoved"))
  447. {
  448. Con::executef(this, 3, "onChildMoved", child->getIdString());
  449. }
  450. }
  451. void GuiControl::childrenReordered()
  452. {
  453. // Default to do nothing.
  454. if (isMethod("onChildrenReordered"))
  455. {
  456. Con::executef(this, 2, "onChildrenReordered");
  457. }
  458. }
  459. void GuiControl::parentResized(const Point2I &oldParentExtent, const Point2I &newParentExtent)
  460. {
  461. Point2I newPosition = getPosition();
  462. Point2I newExtent = getExtent();
  463. S32 deltaX = newParentExtent.x - oldParentExtent.x;
  464. S32 deltaY = newParentExtent.y - oldParentExtent.y;
  465. //In the case of centering, we want to make doubly sure we are using the inner rect.
  466. GuiControl* parent = getParent();
  467. Point2I parentInnerExt = Point2I(newParentExtent);
  468. if(mHorizSizing == horizResizeCenter || mVertSizing == vertResizeCenter ||
  469. mHorizSizing == horizResizeFill || mVertSizing == vertResizeFill)
  470. {
  471. //This is based on the "new" outer extent of the parent.
  472. parentInnerExt = parent->getInnerRect().extent;
  473. }
  474. if (mHorizSizing == horizResizeCenter)
  475. newPosition.x = (parentInnerExt.x - mBounds.extent.x) >> 1;
  476. else if (mHorizSizing == horizResizeWidth)
  477. newExtent.x += deltaX;
  478. else if (mHorizSizing == horizResizeLeft)
  479. newPosition.x += deltaX;
  480. else if (mHorizSizing == horizResizeFill)
  481. {
  482. newPosition.x = 0;
  483. newExtent.x = parentInnerExt.x;
  484. }
  485. else if (mHorizSizing == horizResizeRelative && oldParentExtent.x != 0)
  486. {
  487. Point2F percent = relPosBatteryH(newPosition.x, newExtent.x, oldParentExtent.x);
  488. S32 newLeft = mRound(percent.x * newParentExtent.x);
  489. S32 newRight = mRound(percent.y * newParentExtent.x);
  490. newPosition.x = newLeft;
  491. newExtent.x = newRight - newLeft;
  492. }
  493. if (mVertSizing == vertResizeCenter)
  494. newPosition.y = (parentInnerExt.y - mBounds.extent.y) >> 1;
  495. else if (mVertSizing == vertResizeHeight)
  496. newExtent.y += deltaY;
  497. else if (mVertSizing == vertResizeTop)
  498. newPosition.y += deltaY;
  499. else if (mVertSizing == vertResizeFill)
  500. {
  501. newPosition.y = 0;
  502. newExtent.y = parentInnerExt.y;
  503. }
  504. else if(mVertSizing == vertResizeRelative && oldParentExtent.y != 0)
  505. {
  506. Point2F percent = relPosBatteryV(newPosition.y, newExtent.y, oldParentExtent.y);
  507. S32 newTop = mRound(percent.x * newParentExtent.y);
  508. S32 newBottom = mRound(percent.y * newParentExtent.y);
  509. newPosition.y = newTop;
  510. newExtent.y = newBottom - newTop;
  511. }
  512. newExtent = extentBattery(newExtent);
  513. resize(newPosition, newExtent);
  514. }
  515. void GuiControl::preventResizeModeFill()
  516. {
  517. if (getHorizSizing() == horizResizeFill)
  518. {
  519. setHorizSizing(horizResizeRight);
  520. }
  521. if (getVertSizing() == vertResizeFill)
  522. {
  523. setVertSizing(vertResizeBottom);
  524. }
  525. }
  526. void GuiControl::preventResizeModeCenter()
  527. {
  528. if (getHorizSizing() == horizResizeCenter)
  529. {
  530. setHorizSizing(horizResizeRight);
  531. }
  532. if (getVertSizing() == vertResizeCenter)
  533. {
  534. setVertSizing(vertResizeBottom);
  535. }
  536. }
  537. Point2I GuiControl::extentBattery(Point2I &newExtent)
  538. {
  539. if (mMinExtent.x == 0 && mMinExtent.y == 0)
  540. {
  541. return newExtent;
  542. }
  543. Point2I result = Point2I(newExtent);
  544. if (mHorizSizing != horizResizeRelative)
  545. {
  546. if (newExtent.x < mBounds.extent.x && newExtent.x < mMinExtent.x)
  547. {
  548. mStoredExtent.x += mBounds.extent.x > mMinExtent.x ? (mMinExtent.x - newExtent.x) : (mBounds.extent.x - newExtent.x);
  549. result.x = mMinExtent.x;
  550. }
  551. else if (newExtent.x > mBounds.extent.x && mStoredExtent.x > 0)
  552. {
  553. S32 charge = getMin(newExtent.x - mBounds.extent.x, mStoredExtent.x);
  554. mStoredExtent.x -= charge;
  555. result.x = newExtent.x - charge;
  556. }
  557. }
  558. if (mVertSizing != vertResizeRelative)
  559. {
  560. if (newExtent.y < mBounds.extent.y && newExtent.y < mMinExtent.y)
  561. {
  562. mStoredExtent.y += mBounds.extent.y > mMinExtent.y ? (mMinExtent.y - newExtent.y) : (mBounds.extent.y - newExtent.y);
  563. result.y = mMinExtent.y;
  564. }
  565. else if (newExtent.y > mBounds.extent.y && mStoredExtent.y > 0)
  566. {
  567. S32 charge = getMin(newExtent.y - mBounds.extent.y, mStoredExtent.y);
  568. mStoredExtent.y -= charge;
  569. result.y = newExtent.y - charge;
  570. }
  571. }
  572. return result;
  573. }
  574. Point2F GuiControl::relPosBatteryH(S32 pos, S32 ext, S32 parentExt)
  575. {
  576. if (!mUseRelPosH)
  577. {
  578. relPosBattery(mStoredRelativePosH, pos, ext, parentExt);
  579. mUseRelPosH = true;
  580. }
  581. return mStoredRelativePosH;
  582. }
  583. Point2F GuiControl::relPosBatteryV(S32 pos, S32 ext, S32 parentExt)
  584. {
  585. if (!mUseRelPosV)
  586. {
  587. relPosBattery(mStoredRelativePosV, pos, ext, parentExt);
  588. mUseRelPosV = true;
  589. }
  590. return mStoredRelativePosV;
  591. }
  592. void GuiControl::relPosBattery(Point2F& battery, S32 pos, S32 ext, S32 parentExt)
  593. {
  594. battery.x = static_cast<F32>(pos) / parentExt;
  595. battery.y = static_cast<F32>(pos + ext) / parentExt;
  596. }
  597. //----------------------------------------------------------------
  598. void GuiControl::onRender(Point2I offset, const RectI &updateRect)
  599. {
  600. RectI ctrlRect = applyMargins(offset, mBounds.extent, NormalState, mProfile);
  601. if (!ctrlRect.isValidRect())
  602. {
  603. return;
  604. }
  605. renderUniversalRect(ctrlRect, mProfile, NormalState);
  606. //Render Text
  607. dglSetBitmapModulation(getFontColor(mProfile));
  608. RectI fillRect = applyBorders(ctrlRect.point, ctrlRect.extent, NormalState, mProfile);
  609. RectI contentRect = applyPadding(fillRect.point, fillRect.extent, NormalState, mProfile);
  610. if(contentRect.isValidRect())
  611. {
  612. renderText(contentRect.point, contentRect.extent, mText, mProfile);
  613. //Render the childen
  614. renderChildControls(offset, contentRect, updateRect);
  615. }
  616. }
  617. RectI GuiControl::applyMargins(Point2I &offset, Point2I &extent, GuiControlState currentState, GuiControlProfile *profile)
  618. {
  619. //Get the border profiles
  620. GuiBorderProfile *leftProfile = profile->getLeftBorder();
  621. GuiBorderProfile *rightProfile = profile->getRightBorder();
  622. GuiBorderProfile *topProfile = profile->getTopBorder();
  623. GuiBorderProfile *bottomProfile = profile->getBottomBorder();
  624. S32 leftSize = (leftProfile) ? leftProfile->getMargin(currentState) : 0;
  625. S32 rightSize = (rightProfile) ? rightProfile->getMargin(currentState) : 0;
  626. S32 topSize = (topProfile) ? topProfile->getMargin(currentState) : 0;
  627. S32 bottomSize = (bottomProfile) ? bottomProfile->getMargin(currentState) : 0;
  628. return RectI(offset.x + leftSize, offset.y + topSize, (extent.x - leftSize) - rightSize, (extent.y - topSize) - bottomSize);
  629. }
  630. RectI GuiControl::applyBorders(Point2I &offset, Point2I &extent, GuiControlState currentState, GuiControlProfile *profile)
  631. {
  632. //Get the border profiles
  633. GuiBorderProfile *leftProfile = profile->getLeftBorder();
  634. GuiBorderProfile *rightProfile = profile->getRightBorder();
  635. GuiBorderProfile *topProfile = profile->getTopBorder();
  636. GuiBorderProfile *bottomProfile = profile->getBottomBorder();
  637. S32 leftSize = (leftProfile) ? leftProfile->getBorder(currentState) : 0;
  638. S32 rightSize = (rightProfile) ? rightProfile->getBorder(currentState) : 0;
  639. S32 topSize = (topProfile) ? topProfile->getBorder(currentState) : 0;
  640. S32 bottomSize = (bottomProfile) ? bottomProfile->getBorder(currentState) : 0;
  641. return RectI(offset.x + leftSize, offset.y + topSize, (extent.x - leftSize) - rightSize, (extent.y - topSize) - bottomSize);
  642. }
  643. RectI GuiControl::applyPadding(Point2I &offset, Point2I &extent, GuiControlState currentState, GuiControlProfile *profile)
  644. {
  645. //Get the border profiles
  646. GuiBorderProfile *leftProfile = profile->getLeftBorder();
  647. GuiBorderProfile *rightProfile = profile->getRightBorder();
  648. GuiBorderProfile *topProfile = profile->getTopBorder();
  649. GuiBorderProfile *bottomProfile = profile->getBottomBorder();
  650. S32 leftSize = (leftProfile) ? leftProfile->getPadding(currentState) : 0;
  651. S32 rightSize = (rightProfile) ? rightProfile->getPadding(currentState) : 0;
  652. S32 topSize = (topProfile) ? topProfile->getPadding(currentState) : 0;
  653. S32 bottomSize = (bottomProfile) ? bottomProfile->getPadding(currentState) : 0;
  654. return RectI(offset.x + leftSize, offset.y + topSize, (extent.x - leftSize) - rightSize, (extent.y - topSize) - bottomSize);
  655. }
  656. RectI GuiControl::getInnerRect(GuiControlState currentState)
  657. {
  658. return getInnerRect(mBounds.point, mBounds.extent, currentState, mProfile);
  659. }
  660. RectI GuiControl::getInnerRect(Point2I& offset, GuiControlState currentState)
  661. {
  662. return getInnerRect(offset, mBounds.extent, currentState, mProfile);
  663. }
  664. RectI GuiControl::getInnerRect(Point2I &offset, Point2I &extent, GuiControlState currentState, GuiControlProfile *profile)
  665. {
  666. if (!profile)
  667. {
  668. return mBounds;
  669. }
  670. //Get the border profiles
  671. GuiBorderProfile *leftProfile = profile->getLeftBorder();
  672. GuiBorderProfile *rightProfile = profile->getRightBorder();
  673. GuiBorderProfile *topProfile = profile->getTopBorder();
  674. GuiBorderProfile *bottomProfile = profile->getBottomBorder();
  675. S32 leftSize = (leftProfile) ? leftProfile->getMargin(currentState) + leftProfile->getBorder(currentState) + leftProfile->getPadding(currentState) : 0;
  676. S32 rightSize = (rightProfile) ? rightProfile->getMargin(currentState) + rightProfile->getBorder(currentState) + rightProfile->getPadding(currentState) : 0;
  677. S32 topSize = (topProfile) ? topProfile->getMargin(currentState) + topProfile->getBorder(currentState) + topProfile->getPadding(currentState) : 0;
  678. S32 bottomSize = (bottomProfile) ? bottomProfile->getMargin(currentState) + bottomProfile->getBorder(currentState) + bottomProfile->getPadding(currentState) : 0;
  679. return RectI(offset.x + leftSize, offset.y + topSize, (extent.x - leftSize) - rightSize, (extent.y - topSize) - bottomSize);
  680. }
  681. Point2I GuiControl::getOuterExtent(Point2I &innerExtent, GuiControlState currentState, GuiControlProfile *profile)
  682. {
  683. return Point2I(getOuterWidth(innerExtent.x, currentState, profile), getOuterHeight(innerExtent.y, currentState, profile));
  684. }
  685. S32 GuiControl::getOuterWidth(S32 innerWidth, GuiControlState currentState, GuiControlProfile* profile)
  686. {
  687. //Get the border profiles
  688. GuiBorderProfile* leftProfile = profile->getLeftBorder();
  689. GuiBorderProfile* rightProfile = profile->getRightBorder();
  690. S32 leftSize = (leftProfile) ? leftProfile->getMargin(currentState) + leftProfile->getBorder(currentState) + leftProfile->getPadding(currentState) : 0;
  691. S32 rightSize = (rightProfile) ? rightProfile->getMargin(currentState) + rightProfile->getBorder(currentState) + rightProfile->getPadding(currentState) : 0;
  692. return innerWidth + leftSize + rightSize;
  693. }
  694. S32 GuiControl::getOuterHeight(S32 innerHeight, GuiControlState currentState, GuiControlProfile* profile)
  695. {
  696. //Get the border profiles
  697. GuiBorderProfile* topProfile = profile->getTopBorder();
  698. GuiBorderProfile* bottomProfile = profile->getBottomBorder();
  699. S32 topSize = (topProfile) ? topProfile->getMargin(currentState) + topProfile->getBorder(currentState) + topProfile->getPadding(currentState) : 0;
  700. S32 bottomSize = (bottomProfile) ? bottomProfile->getMargin(currentState) + bottomProfile->getBorder(currentState) + bottomProfile->getPadding(currentState) : 0;
  701. return innerHeight + topSize + bottomSize;
  702. }
  703. bool GuiControl::renderTooltip(Point2I &cursorPos, const char* tipText )
  704. {
  705. #if !defined(TORQUE_OS_IOS) && !defined(TORQUE_OS_ANDROID) && !defined(TORQUE_OS_EMSCRIPTEN)
  706. // Short Circuit.
  707. if (!mAwake)
  708. return false;
  709. if ( dStrlen( mTooltip ) == 0 && ( tipText == NULL || dStrlen( tipText ) == 0 ) )
  710. return false;
  711. const char* renderTip = mTooltip;
  712. if( tipText != NULL )
  713. renderTip = tipText;
  714. // Finish if no root.
  715. GuiCanvas *root = getRoot();
  716. if ( !root )
  717. return false;
  718. if (!mTooltipProfile)
  719. setField("TooltipProfile", "GuiTooltipProfile");
  720. GFont *font = mTooltipProfile->getFont();
  721. // Set text bounds.
  722. Point2I textBounds( 0, 0 );
  723. // Fetch the width of a space.
  724. const S32 spaceWidth = (S32)font->getStrWidth(" ");
  725. // Fetch the maximum allowed tooltip extent.
  726. const S32 maxTooltipWidth = mTooltipWidth;
  727. // Fetch word count.
  728. const S32 wordCount = StringUnit::getUnitCount( renderTip, " " );
  729. // Reset line storage.
  730. const S32 tooltipLineStride = (S32)font->getHeight() + 4;
  731. const S32 maxTooltipLines = 20;
  732. S32 tooltipLineCount = 0;
  733. S32 tooltipLineWidth = 0;
  734. FrameTemp<StringBuffer> tooltipLines( maxTooltipLines );
  735. // Reset word indexing.
  736. S32 wordStartIndex = 0;
  737. S32 wordEndIndex = 0;
  738. // Search for end word.
  739. while( true )
  740. {
  741. // Do we have any words left?
  742. if ( wordEndIndex < wordCount )
  743. {
  744. // Yes, so fetch the word.
  745. const char* pWord = StringUnit::getUnit( renderTip, wordEndIndex, " " );
  746. // Add word length.
  747. const S32 wordLength = (S32)font->getStrWidth( pWord ) + spaceWidth;
  748. // Do we still have room?
  749. if ( (tooltipLineWidth + wordLength) < maxTooltipWidth )
  750. {
  751. // Yes, so add word length.
  752. tooltipLineWidth += wordLength;
  753. // Next word.
  754. wordEndIndex++;
  755. continue;
  756. }
  757. // Do we have any lines left?
  758. if ( tooltipLineCount < maxTooltipLines )
  759. {
  760. // Yes, so insert line.
  761. tooltipLines[tooltipLineCount++] = StringUnit::getUnits( renderTip, wordStartIndex, wordEndIndex-1, " " );
  762. // Update horizontal text bounds.
  763. if ( tooltipLineWidth > textBounds.x )
  764. textBounds.x = tooltipLineWidth;
  765. }
  766. // Set new line length.
  767. tooltipLineWidth = wordLength;
  768. // Set word start.
  769. wordStartIndex = wordEndIndex;
  770. // Next word.
  771. wordEndIndex++;
  772. continue;
  773. }
  774. // Do we have any words left?
  775. if ( wordStartIndex < wordCount )
  776. {
  777. // Yes, so do we have any lines left?
  778. if ( tooltipLineCount < maxTooltipLines )
  779. {
  780. // Yes, so insert line.
  781. tooltipLines[tooltipLineCount++] = StringUnit::getUnits( renderTip, wordStartIndex, wordCount-1, " " );
  782. // Update horizontal text bounds.
  783. if ( tooltipLineWidth > textBounds.x )
  784. textBounds.x = tooltipLineWidth;
  785. }
  786. }
  787. break;
  788. }
  789. // Controls the size of the inside (gutter) tooltip region.
  790. const S32 tooltipGutterSize = 5;
  791. // Adjust text bounds.
  792. textBounds.x += tooltipGutterSize * 2;
  793. textBounds.y = (((S32)font->getHeight() + 4) * tooltipLineCount - 4) + (tooltipGutterSize * 2);
  794. // Adjust to tooltip is always on-screen.
  795. Point2I screensize = Platform::getWindowSize();
  796. Point2I offset = cursorPos;
  797. offset.y += 22;
  798. if (screensize.x < (offset.x + textBounds.x))
  799. offset.x = screensize.x - textBounds.x;
  800. if(screensize.y < (offset.y + textBounds.y) )
  801. offset.y = screensize.y - textBounds.y;
  802. // Fetch the old clip.
  803. RectI oldClip = dglGetClipRect();
  804. // Set rectangle for the box, and set the clip rectangle.
  805. RectI rect(offset, textBounds);
  806. dglSetClipRect(rect);
  807. // Draw body and border of the tool tip
  808. renderUniversalRect(rect, mTooltipProfile, NormalState);
  809. // Draw the text centered in the tool tip box
  810. dglSetBitmapModulation( mTooltipProfile->mFontColor );
  811. Point2I start( tooltipGutterSize, tooltipGutterSize );
  812. for ( S32 lineIndex = 0; lineIndex < tooltipLineCount; lineIndex++ )
  813. {
  814. dglDrawText( font, start + offset, tooltipLines[lineIndex].getPtr8(), mProfile->mFontColors );
  815. offset.y += tooltipLineStride;
  816. }
  817. dglSetClipRect( oldClip );
  818. #endif
  819. return true;
  820. }
  821. void GuiControl::renderChildControls(const Point2I& offset, const RectI& content, const RectI& updateRect)
  822. {
  823. // offset is the upper-left corner of this control in screen coordinates. It should almost always be the same offset passed into the onRender method.
  824. // updateRect is the area that this control was allowed to draw in. It should almost always be the same as the value in onRender.
  825. // content is the area that child controls are allowed to draw in.
  826. RectI clipRect = content;
  827. if(clipRect.intersect(dglGetClipRect()))
  828. {
  829. S32 size = objectList.size();
  830. S32 size_cpy = size;
  831. //-Mat look through our vector all normal-like, trying to use an iterator sometimes gives us
  832. //bad cast on good objects
  833. for( S32 count = 0; count < objectList.size(); count++ )
  834. {
  835. GuiControl *ctrl = (GuiControl *)objectList[count];
  836. if( ctrl == NULL ) {
  837. Con::errorf( "GuiControl::renderChildControls() object %i is NULL", count );
  838. continue;
  839. }
  840. if (ctrl->mVisible)
  841. {
  842. renderChild(ctrl, offset, content, clipRect);
  843. }
  844. size_cpy = objectList.size(); // CHRIS: i know its wierd but the size of the list changes sometimes during execution of this loop
  845. if(size != size_cpy)
  846. {
  847. size = size_cpy;
  848. count--; // CHRIS: just to make sure one wasnt skipped.
  849. }
  850. }
  851. }
  852. }
  853. void GuiControl::renderChild(GuiControl* ctrl, const Point2I& offset, const RectI& content, const RectI& clipRect)
  854. {
  855. ctrl->mRenderInsetLT = content.point - offset;
  856. ctrl->mRenderInsetRB = mBounds.extent - (ctrl->mRenderInsetLT + content.extent);
  857. Point2I childPosition = content.point + ctrl->getPosition();
  858. RectI childClip(childPosition, ctrl->getExtent());
  859. if (childClip.intersect(clipRect))
  860. {
  861. RectI old = dglGetClipRect();
  862. dglSetClipRect(clipRect);
  863. glDisable(GL_CULL_FACE);
  864. ctrl->onRender(childPosition, RectI(childPosition, ctrl->getExtent()));
  865. dglSetClipRect(old);
  866. }
  867. }
  868. void GuiControl::setUpdateRegion(Point2I pos, Point2I ext)
  869. {
  870. Point2I upos = localToGlobalCoord(pos);
  871. GuiCanvas *root = getRoot();
  872. if (root)
  873. {
  874. root->addUpdateRegion(upos, ext);
  875. }
  876. }
  877. void GuiControl::setUpdate()
  878. {
  879. setUpdateRegion(Point2I(0,0), mBounds.extent);
  880. }
  881. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //
  882. void GuiControl::awaken()
  883. {
  884. AssertFatal(!mAwake, "GuiControl::awaken: control is already awake");
  885. if(mAwake)
  886. return;
  887. iterator i;
  888. for(i = begin(); i != end(); i++)
  889. {
  890. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  891. AssertFatal(!ctrl->isAwake(), "GuiControl::awaken: child control is already awake");
  892. if(!ctrl->isAwake())
  893. ctrl->awaken();
  894. }
  895. AssertFatal(!mAwake, "GuiControl::awaken: should not be awake here");
  896. if(!mAwake)
  897. {
  898. if(!onWake())
  899. {
  900. Con::errorf(ConsoleLogEntry::General, "GuiControl::awaken: failed onWake for obj: %s", getName());
  901. AssertFatal(0, "GuiControl::awaken: failed onWake");
  902. deleteObject();
  903. }
  904. else
  905. {
  906. if (mTextID && *mTextID != 0)
  907. setTextID(mTextID);
  908. }
  909. }
  910. }
  911. void GuiControl::sleep()
  912. {
  913. AssertFatal(mAwake, "GuiControl::sleep: control is not awake");
  914. if(!mAwake)
  915. return;
  916. iterator i;
  917. for(i = begin(); i != end(); i++)
  918. {
  919. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  920. AssertFatal(ctrl->isAwake(), "GuiControl::sleep: child control is already asleep");
  921. if(ctrl->isAwake())
  922. ctrl->sleep();
  923. }
  924. AssertFatal(mAwake, "GuiControl::sleep: should not be asleep here");
  925. if(mAwake)
  926. onSleep();
  927. }
  928. void GuiControl::preRender()
  929. {
  930. AssertFatal(mAwake, "GuiControl::preRender: control is not awake");
  931. if(!mAwake)
  932. return;
  933. iterator i;
  934. for(i = begin(); i != end(); i++)
  935. {
  936. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  937. if (ctrl->isVisible())
  938. {
  939. ctrl->preRender();
  940. }
  941. }
  942. onPreRender();
  943. }
  944. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //
  945. bool GuiControl::onWake()
  946. {
  947. AssertFatal( !mAwake, "GuiControl::onWake: control is already awake" );
  948. if( mAwake )
  949. return false;
  950. // [tom, 4/18/2005] Cause mLangTable to be refreshed in case it was changed
  951. mLangTable = NULL;
  952. // Grab the classname of this object
  953. const char *cName = getClassName();
  954. //make sure we have a profile
  955. if( !mProfile )
  956. {
  957. // Ensure the classname is a valid name...
  958. if( cName && cName[0] )
  959. {
  960. S32 pos = 0;
  961. for( pos = 0; pos <= (S32)dStrlen( cName ); pos++ )
  962. if( !dStrncmp( cName + pos, "Ctrl", 4 ) )
  963. break;
  964. if( pos != 0 ) {
  965. char buff[255];
  966. dStrncpy( buff, cName, pos );
  967. buff[pos] = '\0';
  968. dStrcat( buff, "Profile\0" );
  969. SimObject *obj = Sim::findObject( buff );
  970. if( obj )
  971. mProfile = dynamic_cast<GuiControlProfile*>( obj );
  972. }
  973. }
  974. // Ok lets check to see if that worked
  975. if( !mProfile ) {
  976. SimObject *obj = Sim::findObject( "GuiDefaultProfile" );
  977. if( obj )
  978. mProfile = dynamic_cast<GuiControlProfile*>(obj);
  979. }
  980. AssertFatal( mProfile, avar( "GuiControl: %s created with no profile.", getName() ) );
  981. }
  982. //set the flag
  983. mAwake = true;
  984. //set the layer
  985. GuiCanvas *root = getRoot();
  986. AssertFatal(root, "Unable to get the root Canvas.");
  987. GuiControl *parent = getParent();
  988. if (parent && parent != root)
  989. mLayer = parent->mLayer;
  990. //make sure the first responder exists
  991. if (! mFirstResponder)
  992. mFirstResponder = findFirstTabable();
  993. //see if we should force this control to be the first responder
  994. //if (mProfile->mTabable && mProfile->mCanKeyFocus)
  995. // setFirstResponder();
  996. //increment the profile
  997. mProfile->incRefCount();
  998. // Only invoke script callbacks if we have a namespace in which to do so
  999. // This will suppress warnings
  1000. if( isMethod("onWake") )
  1001. Con::executef(this, 1, "onWake");
  1002. if (mTooltipProfile != NULL)
  1003. mTooltipProfile->incRefCount();
  1004. return true;
  1005. }
  1006. void GuiControl::onSleep()
  1007. {
  1008. AssertFatal(mAwake, "GuiControl::onSleep: control is not awake");
  1009. if(!mAwake)
  1010. return;
  1011. //decrement the profile referrence
  1012. if( mProfile != NULL )
  1013. mProfile->decRefCount();
  1014. clearFirstResponder();
  1015. mouseUnlock();
  1016. // Only invoke script callbacks if we have a namespace in which to do so
  1017. // This will suppress warnings
  1018. if( isMethod("onSleep") )
  1019. Con::executef(this, 1, "onSleep");
  1020. if (mTooltipProfile != NULL)
  1021. mTooltipProfile->decRefCount();
  1022. // Set Flag
  1023. mAwake = false;
  1024. }
  1025. void GuiControl::setControlProfile(GuiControlProfile *prof)
  1026. {
  1027. AssertFatal(prof, "GuiControl::setControlProfile: invalid profile");
  1028. if(prof == mProfile)
  1029. return;
  1030. if(mAwake)
  1031. mProfile->decRefCount();
  1032. mProfile = prof;
  1033. if(mAwake)
  1034. mProfile->incRefCount();
  1035. }
  1036. void GuiControl::onPreRender()
  1037. {
  1038. // do nothing.
  1039. }
  1040. //-----------------------------------------------------------------------------
  1041. // checks up the parent hierarchy - if anyone above us is not savable returns false
  1042. // otherwise, returns true.
  1043. //-----------------------------------------------------------------------------
  1044. bool GuiControl::getCanSaveParent()
  1045. {
  1046. GuiControl *walk = this;
  1047. while(walk)
  1048. {
  1049. if(!walk->getCanSave())
  1050. return false;
  1051. walk = walk->getParent();
  1052. }
  1053. return true;
  1054. }
  1055. //-----------------------------------------------------------------------------
  1056. // Can we Save to a TorqueScript file?
  1057. //-----------------------------------------------------------------------------
  1058. bool GuiControl::getCanSave()
  1059. {
  1060. return mCanSave;
  1061. }
  1062. //-----------------------------------------------------------------------------
  1063. // Sets whether we can save out to a file (TorqueScript)
  1064. //-----------------------------------------------------------------------------
  1065. void GuiControl::setCanSave(bool bCanSave)
  1066. {
  1067. mCanSave = bCanSave;
  1068. }
  1069. ////////////////////////////////////////////////////////////////////////////////////////////////////
  1070. // checks out mCanSave flag, if true just passes along to our parent,
  1071. // if false, then we return without writing. Note, also, that
  1072. // if our parent is not writeable, then we should not be writable...
  1073. ////////////////////////////////////////////////////////////////////////////////////////////////////
  1074. void GuiControl::write(Stream &stream, U32 tabStop, U32 flags)
  1075. {
  1076. //note: this will return false if either we, or any of our parents, are non-save controls
  1077. bool bCanSave = getCanSaveParent();
  1078. if(bCanSave)
  1079. {
  1080. Parent::write(stream, tabStop, flags);
  1081. }
  1082. }
  1083. //This is only called if the control is deleted, not when the control is removed from its parent.
  1084. void GuiControl::onRemove()
  1085. {
  1086. Parent::onRemove();
  1087. }
  1088. //For GuiControls, this will always just before it is actually removed.
  1089. void GuiControl::onGroupRemove()
  1090. {
  1091. clearFirstResponder();
  1092. }
  1093. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //
  1094. const char *GuiControl::getScriptValue()
  1095. {
  1096. return NULL;
  1097. }
  1098. void GuiControl::setScriptValue(const char *value)
  1099. {
  1100. }
  1101. void GuiControl::setConsoleVariable(const char *variable)
  1102. {
  1103. if (variable)
  1104. {
  1105. mConsoleVariable = StringTable->insert(variable);
  1106. }
  1107. else
  1108. {
  1109. mConsoleVariable = StringTable->EmptyString;
  1110. }
  1111. }
  1112. //-----------------------------------------------------------------------------
  1113. // finds and returns the first immediate child of ours whose
  1114. // internal name matches the passed String. returns Null if not found.
  1115. //-----------------------------------------------------------------------------
  1116. void GuiControl::setConsoleCommand(const char *newCmd)
  1117. {
  1118. if (newCmd)
  1119. mConsoleCommand = StringTable->insert(newCmd);
  1120. else
  1121. mConsoleCommand = StringTable->EmptyString;
  1122. }
  1123. const char * GuiControl::getConsoleCommand()
  1124. {
  1125. return mConsoleCommand;
  1126. }
  1127. void GuiControl::setSizing(S32 horz, S32 vert)
  1128. {
  1129. mHorizSizing = horz;
  1130. mVertSizing = vert;
  1131. }
  1132. void GuiControl::setVariable(const char *value)
  1133. {
  1134. if (mConsoleVariable[0])
  1135. Con::setVariable(mConsoleVariable, value);
  1136. }
  1137. void GuiControl::setIntVariable(S32 value)
  1138. {
  1139. if (mConsoleVariable[0])
  1140. Con::setIntVariable(mConsoleVariable, value);
  1141. }
  1142. void GuiControl::setFloatVariable(F32 value)
  1143. {
  1144. if (mConsoleVariable[0])
  1145. Con::setFloatVariable(mConsoleVariable, value);
  1146. }
  1147. const char * GuiControl::getVariable()
  1148. {
  1149. if (mConsoleVariable[0])
  1150. return Con::getVariable(mConsoleVariable);
  1151. else return NULL;
  1152. }
  1153. S32 GuiControl::getIntVariable()
  1154. {
  1155. if (mConsoleVariable[0])
  1156. return Con::getIntVariable(mConsoleVariable);
  1157. else return 0;
  1158. }
  1159. F32 GuiControl::getFloatVariable()
  1160. {
  1161. if (mConsoleVariable[0])
  1162. return Con::getFloatVariable(mConsoleVariable);
  1163. else return 0.0f;
  1164. }
  1165. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //
  1166. bool GuiControl::cursorInControl()
  1167. {
  1168. GuiCanvas *root = getRoot();
  1169. if (! root) return false;
  1170. Point2I pt = root->getCursorPos();
  1171. Point2I offset = localToGlobalCoord(Point2I(0, 0));
  1172. if (pt.x >= offset.x && pt.y >= offset.y &&
  1173. pt.x < offset.x + mBounds.extent.x && pt.y < offset.y + mBounds.extent.y)
  1174. {
  1175. return true;
  1176. }
  1177. else
  1178. {
  1179. return false;
  1180. }
  1181. }
  1182. bool GuiControl::pointInControl(const Point2I& parentCoordPoint)
  1183. {
  1184. S32 xt = parentCoordPoint.x - mBounds.point.x;
  1185. S32 yt = parentCoordPoint.y - mBounds.point.y;
  1186. return xt >= 0 && yt >= 0 && xt < mBounds.extent.x && yt < mBounds.extent.y;
  1187. }
  1188. GuiControl* GuiControl::findHitControl(const Point2I &pt, S32 initialLayer)
  1189. {
  1190. iterator i = end(); // find in z order (last to first)
  1191. while (i != begin())
  1192. {
  1193. i--;
  1194. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  1195. if (initialLayer >= 0 && ctrl->mLayer > initialLayer)
  1196. {
  1197. continue;
  1198. }
  1199. else if (ctrl->mVisible && ctrl->pointInControl(pt - ctrl->mRenderInsetLT) && ctrl->mUseInput)
  1200. {
  1201. Point2I ptemp = pt - (ctrl->mBounds.point + ctrl->mRenderInsetLT);
  1202. GuiControl *hitCtrl = ctrl->findHitControl(ptemp);
  1203. if(hitCtrl->mUseInput)
  1204. return hitCtrl;
  1205. }
  1206. }
  1207. return this;
  1208. }
  1209. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //
  1210. bool GuiControl::isMouseLocked()
  1211. {
  1212. if (isEditMode())
  1213. {
  1214. return smEditorHandle->editIsMouseLocked(this);
  1215. }
  1216. GuiCanvas *root = getRoot();
  1217. return root ? root->getMouseLockedControl() == this : false;
  1218. }
  1219. void GuiControl::mouseLock(GuiControl *lockingControl)
  1220. {
  1221. if (isEditMode())
  1222. {
  1223. smEditorHandle->editMouseLock(lockingControl);
  1224. return;
  1225. }
  1226. GuiCanvas *root = getRoot();
  1227. if (root)
  1228. root->mouseLock(lockingControl);
  1229. }
  1230. void GuiControl::mouseLock()
  1231. {
  1232. mouseLock(this);
  1233. }
  1234. void GuiControl::mouseUnlock()
  1235. {
  1236. if (isEditMode())
  1237. {
  1238. smEditorHandle->editMouseUnlock();
  1239. return;
  1240. }
  1241. GuiCanvas *root = getRoot();
  1242. if (root)
  1243. root->mouseUnlock(this);
  1244. }
  1245. bool GuiControl::sendScriptMouseEvent(const char* name, const GuiEvent& event)
  1246. {
  1247. bool consumed = false;
  1248. if (isMethod(name))
  1249. {
  1250. char buf[3][32];
  1251. dSprintf(buf[0], 32, "%d", event.modifier);
  1252. dSprintf(buf[1], 32, "%d %d", event.mousePoint.x, event.mousePoint.y);
  1253. dSprintf(buf[2], 32, "%d", event.mouseClickCount);
  1254. consumed = dAtob(Con::executef(this, 4, name, buf[0], buf[1], buf[2]));
  1255. }
  1256. return consumed;
  1257. }
  1258. bool GuiControl::sendScriptKeyEvent(const char* name, const InputEvent& event)
  1259. {
  1260. bool consumed = false;
  1261. if (isMethod(name))
  1262. {
  1263. char buf[2][32];
  1264. dSprintf(buf[0], 32, "%d", event.modifier);
  1265. if (!ActionMap::getKeyString(event.objInst, buf[1]))
  1266. return(false);
  1267. consumed = dAtob(Con::executef(this, 3, name, buf[0], buf[1]));
  1268. }
  1269. return consumed;
  1270. }
  1271. bool GuiControl::onInputEvent(const InputEvent &event)
  1272. {
  1273. if (event.objType == SI_KEY)
  1274. {
  1275. if (event.action == SI_MAKE)
  1276. {
  1277. return sendScriptKeyEvent("onKeyDown", event);
  1278. }
  1279. else if (event.action == SI_BREAK)
  1280. {
  1281. return sendScriptKeyEvent("onKeyUp", event);
  1282. }
  1283. else if (event.action == SI_REPEAT)
  1284. {
  1285. return sendScriptKeyEvent("onKeyRepeat", event);
  1286. }
  1287. }
  1288. return false;
  1289. }
  1290. void GuiControl::onTouchUp(const GuiEvent &event)
  1291. {
  1292. if (!mVisible || !mAwake)
  1293. return;
  1294. bool consumed1 = sendScriptMouseEvent("onTouchUp", event);
  1295. bool consumed2 = sendScriptMouseEvent("onMouseUp", event);
  1296. GuiControl* parent = getParent();
  1297. if (parent && !consumed1 && !consumed2)
  1298. parent->onTouchUp(event);
  1299. }
  1300. void GuiControl::onTouchDown(const GuiEvent &event)
  1301. {
  1302. if (!mVisible || !mAwake)
  1303. return;
  1304. bool consumed1 = sendScriptMouseEvent("onTouchDown", event);
  1305. bool consumed2 = sendScriptMouseEvent("onMouseDown", event);
  1306. GuiControl* parent = getParent();
  1307. if (parent && !consumed1 && !consumed2)
  1308. parent->onTouchDown(event);
  1309. }
  1310. void GuiControl::onTouchMove(const GuiEvent &event)
  1311. {
  1312. if ( !mVisible || !mAwake )
  1313. return;
  1314. bool consumed1 = sendScriptMouseEvent("onTouchMove", event);
  1315. bool consumed2 = sendScriptMouseEvent("onMouseMove", event);
  1316. GuiControl *parent = getParent();
  1317. if (parent && !consumed1 && !consumed2)
  1318. parent->onTouchMove( event );
  1319. }
  1320. void GuiControl::onTouchDragged(const GuiEvent &event)
  1321. {
  1322. if (!mVisible || !mAwake)
  1323. return;
  1324. bool consumed1 = sendScriptMouseEvent("onTouchDragged", event);
  1325. bool consumed2 = sendScriptMouseEvent("onMouseDragged", event);
  1326. GuiControl* parent = getParent();
  1327. if (parent && !consumed1 && !consumed2)
  1328. parent->onTouchDragged(event);
  1329. }
  1330. void GuiControl::onTouchEnter(const GuiEvent &event)
  1331. {
  1332. if (!mVisible || !mAwake)
  1333. return;
  1334. sendScriptMouseEvent("onTouchEnter", event);
  1335. sendScriptMouseEvent("onMouseEnter", event);
  1336. //Entering a child means nothing to a parent
  1337. }
  1338. void GuiControl::onTouchLeave(const GuiEvent &event)
  1339. {
  1340. if (!mVisible || !mAwake)
  1341. return;
  1342. sendScriptMouseEvent("onTouchLeave", event);
  1343. sendScriptMouseEvent("onMouseLeave", event);
  1344. //Leaving a child means nothing to a parent
  1345. }
  1346. void GuiControl::onMouseWheelUp( const GuiEvent &event )
  1347. {
  1348. if ( !mVisible || !mAwake )
  1349. return;
  1350. bool consumed = sendScriptMouseEvent("onMouseWheelUp", event);
  1351. GuiControl *parent = getParent();
  1352. if (parent && !consumed)
  1353. return parent->onMouseWheelUp(event);
  1354. }
  1355. void GuiControl::onMouseWheelDown( const GuiEvent &event )
  1356. {
  1357. if ( !mVisible || !mAwake )
  1358. return;
  1359. bool consumed = sendScriptMouseEvent("onMouseWheelDown", event);
  1360. GuiControl *parent = getParent();
  1361. if (parent && !consumed)
  1362. return parent->onMouseWheelDown(event);
  1363. }
  1364. void GuiControl::onRightMouseDown(const GuiEvent &event)
  1365. {
  1366. if (!mVisible || !mAwake)
  1367. return;
  1368. bool consumed = sendScriptMouseEvent("onRightMouseDown", event);
  1369. GuiControl* parent = getParent();
  1370. if (parent && !consumed)
  1371. parent->onRightMouseDown(event);
  1372. }
  1373. void GuiControl::onRightMouseUp(const GuiEvent &event)
  1374. {
  1375. if (!mVisible || !mAwake)
  1376. return;
  1377. bool consumed = sendScriptMouseEvent("onRightMouseUp", event);
  1378. GuiControl* parent = getParent();
  1379. if (parent && !consumed)
  1380. parent->onRightMouseUp(event);
  1381. }
  1382. void GuiControl::onRightMouseDragged(const GuiEvent &event)
  1383. {
  1384. if (!mVisible || !mAwake)
  1385. return;
  1386. bool consumed = sendScriptMouseEvent("onRightMouseDragged", event);
  1387. GuiControl* parent = getParent();
  1388. if (parent && !consumed)
  1389. parent->onRightMouseDragged(event);
  1390. }
  1391. void GuiControl::onMiddleMouseDown(const GuiEvent &event)
  1392. {
  1393. if (!mVisible || !mAwake)
  1394. return;
  1395. bool consumed = sendScriptMouseEvent("onMiddleMouseDown", event);
  1396. GuiControl* parent = getParent();
  1397. if (parent && !consumed)
  1398. parent->onMiddleMouseDown(event);
  1399. }
  1400. void GuiControl::onMiddleMouseUp(const GuiEvent &event)
  1401. {
  1402. if (!mVisible || !mAwake)
  1403. return;
  1404. bool consumed = sendScriptMouseEvent("onMiddleMouseUp", event);
  1405. GuiControl* parent = getParent();
  1406. if (parent && !consumed)
  1407. parent->onMiddleMouseUp(event);
  1408. }
  1409. void GuiControl::onMiddleMouseDragged(const GuiEvent &event)
  1410. {
  1411. if (!mVisible || !mAwake)
  1412. return;
  1413. bool consumed = sendScriptMouseEvent("onMiddleMouseDragged", event);
  1414. GuiControl* parent = getParent();
  1415. if (parent && !consumed)
  1416. parent->onMiddleMouseDragged(event);
  1417. }
  1418. GuiControl* GuiControl::findFirstTabable()
  1419. {
  1420. GuiControl *tabCtrl = NULL;
  1421. if(mVisible && mAwake)
  1422. {
  1423. iterator i;
  1424. for (i = begin(); i != end(); i++)
  1425. {
  1426. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  1427. tabCtrl = ctrl->findFirstTabable();
  1428. if (tabCtrl)
  1429. {
  1430. mFirstResponder = tabCtrl;
  1431. return tabCtrl;
  1432. }
  1433. }
  1434. }
  1435. //nothing was found, therefore, see if this ctrl is tabable
  1436. return ( mProfile != NULL ) ? ( ( mProfile->mTabable && mAwake && mVisible ) ? this : NULL ) : NULL;
  1437. }
  1438. GuiControl* GuiControl::findLastTabable(bool firstCall)
  1439. {
  1440. //if this is the first call, clear the global
  1441. if (firstCall)
  1442. smPrevResponder = NULL;
  1443. //if this control is tabable, set the global
  1444. if (mProfile->mTabable)
  1445. smPrevResponder = this;
  1446. if(mVisible && mAwake)
  1447. {
  1448. iterator i;
  1449. for (i = begin(); i != end(); i++)
  1450. {
  1451. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  1452. ctrl->findLastTabable(false);
  1453. }
  1454. }
  1455. //after the entire tree has been traversed, return the last responder found
  1456. mFirstResponder = smPrevResponder;
  1457. return smPrevResponder;
  1458. }
  1459. GuiControl *GuiControl::findNextTabable(GuiControl *curResponder, bool firstCall)
  1460. {
  1461. //if this is the first call, clear the global
  1462. if (firstCall)
  1463. smCurResponder = NULL;
  1464. //first find the current responder
  1465. if (curResponder == this)
  1466. smCurResponder = this;
  1467. //if the first responder has been found, return the very next *tabable* control
  1468. else if ( smCurResponder && mProfile->mTabable && mAwake && mVisible && mActive )
  1469. return( this );
  1470. //loop through, checking each child to see if it is the one that follows the firstResponder
  1471. GuiControl *tabCtrl = NULL;
  1472. if (mVisible && mAwake)
  1473. {
  1474. iterator i;
  1475. for (i = begin(); i != end(); i++)
  1476. {
  1477. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  1478. tabCtrl = ctrl->findNextTabable(curResponder, false);
  1479. if (tabCtrl) break;
  1480. }
  1481. }
  1482. mFirstResponder = tabCtrl;
  1483. return tabCtrl;
  1484. }
  1485. GuiControl *GuiControl::findPrevTabable(GuiControl *curResponder, bool firstCall)
  1486. {
  1487. if (firstCall)
  1488. smPrevResponder = NULL;
  1489. //if this is the current reponder, return the previous one
  1490. if (curResponder == this)
  1491. return smPrevResponder;
  1492. //else if this is a responder, store it in case the next found is the current responder
  1493. else if ( mProfile->mTabable && mAwake && mVisible && mActive )
  1494. smPrevResponder = this;
  1495. //loop through, checking each child to see if it is the one that follows the firstResponder
  1496. GuiControl *tabCtrl = NULL;
  1497. if (mVisible && mAwake)
  1498. {
  1499. iterator i;
  1500. for (i = begin(); i != end(); i++)
  1501. {
  1502. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  1503. tabCtrl = ctrl->findPrevTabable(curResponder, false);
  1504. if (tabCtrl) break;
  1505. }
  1506. }
  1507. mFirstResponder = tabCtrl;
  1508. return tabCtrl;
  1509. }
  1510. void GuiControl::onLoseFirstResponder()
  1511. {
  1512. // Since many controls have visual cues when they are the firstResponder...
  1513. setUpdate();
  1514. if (isMethod("onLoseFirstResponder"))
  1515. {
  1516. Con::executef(this, 2, "onLoseFirstResponder");
  1517. }
  1518. else if (isMethod("onBlur"))
  1519. {
  1520. Con::executef(this, 2, "onBlur");
  1521. }
  1522. }
  1523. bool GuiControl::ControlIsChild(GuiControl *child)
  1524. {
  1525. //function returns true if this control, or one of it's children is the child control
  1526. if (child == this)
  1527. return true;
  1528. //loop through, checking each child to see if it is ,or contains, the firstResponder
  1529. iterator i;
  1530. for (i = begin(); i != end(); i++)
  1531. {
  1532. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  1533. if (ctrl->ControlIsChild(child)) return true;
  1534. }
  1535. //not found, therefore false
  1536. return false;
  1537. }
  1538. void GuiControl::onFocus(bool foundFirstResponder)
  1539. {
  1540. if (!foundFirstResponder && isFirstResponder())
  1541. {
  1542. foundFirstResponder = true;
  1543. }
  1544. //bubble the focus up
  1545. GuiControl *parent = getParent();
  1546. if (parent)
  1547. parent->onFocus(foundFirstResponder);
  1548. }
  1549. bool GuiControl::isFirstResponder()
  1550. {
  1551. GuiCanvas *root = getRoot();
  1552. return root && root->getFirstResponder() == this;
  1553. }
  1554. void GuiControl::setFirstResponder( GuiControl* firstResponder )
  1555. {
  1556. if ( firstResponder && firstResponder->mProfile && firstResponder->mProfile->mCanKeyFocus )
  1557. mFirstResponder = firstResponder;
  1558. GuiControl *parent = getParent();
  1559. if ( parent )
  1560. parent->setFirstResponder( firstResponder );
  1561. }
  1562. void GuiControl::setFirstResponder()
  1563. {
  1564. if ( mAwake && mVisible )
  1565. {
  1566. GuiControl *parent = getParent();
  1567. if (mProfile->mCanKeyFocus && parent )
  1568. {
  1569. parent->setFirstResponder(this);
  1570. // Since many controls have visual cues when they are the firstResponder...
  1571. this->setUpdate();
  1572. if (isMethod("onGainFirstResponder"))
  1573. {
  1574. Con::executef(this, 2, "onGainFirstResponder");
  1575. }
  1576. else if (isMethod("onFocus"))
  1577. {
  1578. Con::executef(this, 2, "onFocus");
  1579. }
  1580. }
  1581. }
  1582. }
  1583. void GuiControl::clearFirstResponder()
  1584. {
  1585. clearFirstResponder(this);
  1586. }
  1587. void GuiControl::clearFirstResponder(GuiControl* target)
  1588. {
  1589. GuiControl *parent = this;
  1590. while((parent = parent->getParent()) != NULL)
  1591. {
  1592. if(parent->mFirstResponder == target)
  1593. parent->mFirstResponder = NULL;
  1594. else
  1595. break;
  1596. }
  1597. }
  1598. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //
  1599. void GuiControl::buildAcceleratorMap()
  1600. {
  1601. //add my own accel key
  1602. addAcceleratorKey();
  1603. //add all my childrens keys
  1604. iterator i;
  1605. for(i = begin(); i != end(); i++)
  1606. {
  1607. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  1608. ctrl->buildAcceleratorMap();
  1609. }
  1610. }
  1611. void GuiControl::addAcceleratorKey()
  1612. {
  1613. //see if we have an accelerator
  1614. if (mAcceleratorKey == StringTable->EmptyString)
  1615. return;
  1616. EventDescriptor accelEvent;
  1617. ActionMap::createEventDescriptor(mAcceleratorKey, &accelEvent);
  1618. //now we have a modifier, and a key, add them to the canvas
  1619. GuiCanvas *root = getRoot();
  1620. if (root)
  1621. root->addAcceleratorKey(this, 0, accelEvent.eventCode, accelEvent.flags);
  1622. }
  1623. void GuiControl::acceleratorKeyPress(U32 index)
  1624. {
  1625. onAction();
  1626. }
  1627. void GuiControl::acceleratorKeyRelease(U32 index)
  1628. {
  1629. //do nothing
  1630. }
  1631. bool GuiControl::onKeyDown(const GuiEvent &event)
  1632. {
  1633. //pass the event to the parent
  1634. GuiControl *parent = getParent();
  1635. if (parent)
  1636. return parent->onKeyDown(event);
  1637. else
  1638. return false;
  1639. }
  1640. bool GuiControl::onKeyRepeat(const GuiEvent &event)
  1641. {
  1642. // default to just another key down.
  1643. return onKeyDown(event);
  1644. }
  1645. bool GuiControl::onKeyUp(const GuiEvent &event)
  1646. {
  1647. //pass the event to the parent
  1648. GuiControl *parent = getParent();
  1649. if (parent)
  1650. return parent->onKeyUp(event);
  1651. else
  1652. return false;
  1653. }
  1654. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //
  1655. void GuiControl::onAction()
  1656. {
  1657. if (! mActive)
  1658. return;
  1659. //execute the console command
  1660. if (mConsoleCommand && mConsoleCommand[0])
  1661. {
  1662. execConsoleCallback();
  1663. }
  1664. else if(isMethod("onAction"))
  1665. {
  1666. Con::executef(this, 1, "onAction");
  1667. }
  1668. }
  1669. void GuiControl::onMessage(GuiControl *sender, S32 msg)
  1670. {
  1671. }
  1672. void GuiControl::messageSiblings(S32 message)
  1673. {
  1674. GuiControl *parent = getParent();
  1675. if (! parent) return;
  1676. GuiControl::iterator i;
  1677. for(i = parent->begin(); i != parent->end(); i++)
  1678. {
  1679. GuiControl *ctrl = dynamic_cast<GuiControl *>(*i);
  1680. if (ctrl != this)
  1681. ctrl->onMessage(this, message);
  1682. }
  1683. }
  1684. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //
  1685. void GuiControl::onDialogPush()
  1686. {
  1687. // Notify Script.
  1688. if( isMethod("onDialogPush") )
  1689. Con::executef(this, 1, "onDialogPush");
  1690. }
  1691. void GuiControl::onDialogPop()
  1692. {
  1693. // Notify Script.
  1694. if( isMethod("onDialogPop") )
  1695. Con::executef(this, 1, "onDialogPop");
  1696. }
  1697. //------------------------------------------------------------------------------
  1698. void GuiControl::setVisible(bool value)
  1699. {
  1700. mVisible = value;
  1701. iterator i;
  1702. setUpdate();
  1703. for(i = begin(); i != end(); i++)
  1704. {
  1705. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  1706. ctrl->clearFirstResponder();
  1707. }
  1708. GuiControl *parent = getParent();
  1709. if (parent)
  1710. parent->childResized(this);
  1711. }
  1712. void GuiControl::makeFirstResponder(bool value)
  1713. {
  1714. if ( value )
  1715. //setFirstResponder(this);
  1716. setFirstResponder();
  1717. else
  1718. clearFirstResponder();
  1719. }
  1720. void GuiControl::setActive( bool value )
  1721. {
  1722. mActive = value;
  1723. if (value && isMethod("onActive"))
  1724. Con::executef(this, 1, "onActive");
  1725. else if (!value && isMethod("onInactive"))
  1726. Con::executef(this, 1, "onInactive");
  1727. if ( !mActive )
  1728. clearFirstResponder();
  1729. if ( mVisible && mAwake )
  1730. setUpdate();
  1731. }
  1732. void GuiControl::getScrollLineSizes(U32 *rowHeight, U32 *columnWidth)
  1733. {
  1734. // default to 10 pixels in y, 30 pixels in x
  1735. *columnWidth = 30;
  1736. *rowHeight = 30;
  1737. }
  1738. void GuiControl::renderText(const Point2I& offset, const Point2I& extent, const char* text, GuiControlProfile* profile, TextRotationOptions rot)
  1739. {
  1740. RectI old = dglGetClipRect();
  1741. RectI clipRect = RectI(offset, extent);
  1742. if (clipRect.intersect(old))
  1743. {
  1744. dglSetClipRect(clipRect);
  1745. const S32 textHeight = profile->getFont(mFontSizeAdjust)->getHeight();
  1746. S32 totalWidth = (rot == tRotateNone) ? extent.x : extent.y;
  1747. S32 totalHeight = (rot == tRotateNone) ? extent.y : extent.x;
  1748. S32 startOffsetY = 0;
  1749. vector<string> lineList = getLineList(text, profile, totalWidth);
  1750. //first align vertical
  1751. S32 blockHeight = textHeight * lineList.size();
  1752. if (mTextExtend)
  1753. {
  1754. Point2I extent = getExtent();
  1755. if (mTextWrap)
  1756. {
  1757. extent.y = getOuterHeight(blockHeight, NormalState, profile);
  1758. }
  1759. else
  1760. {
  1761. extent.x = getOuterWidth(profile->getFont(mFontSizeAdjust)->getStrWidth(text), NormalState, profile);
  1762. }
  1763. setExtent(extent);
  1764. }
  1765. if (blockHeight < totalHeight)
  1766. {
  1767. startOffsetY = getTextVerticalOffset(blockHeight, totalHeight, getVertAlignmentType(profile));
  1768. }
  1769. else if (!mTextWrap)
  1770. {
  1771. startOffsetY = getTextVerticalOffset(blockHeight, totalHeight, VertAlignmentType::MiddleVAlign);
  1772. }
  1773. else
  1774. {
  1775. startOffsetY = getTextVerticalOffset(blockHeight, totalHeight, VertAlignmentType::TopVAlign);
  1776. }
  1777. renderLineList(offset, extent, startOffsetY, lineList, profile, rot);
  1778. dglSetClipRect(old);
  1779. }
  1780. }
  1781. void GuiControl::renderLineList(const Point2I& offset, const Point2I& extent, const S32 startOffsetY, const vector<string> lineList, GuiControlProfile* profile, const TextRotationOptions rot)
  1782. {
  1783. const S32 textHeight = profile->getFont(mFontSizeAdjust)->getHeight();
  1784. S32 totalWidth = (rot == tRotateNone) ? extent.x : extent.y;
  1785. //Now print each line
  1786. U32 ibeamPos = 0;
  1787. U32 lineNumber = 0;
  1788. S32 offsetX = 0;
  1789. S32 offsetY = startOffsetY;
  1790. for(string line : lineList)
  1791. {
  1792. // align the horizontal
  1793. string trimmedLine = Utility::trim_copy(line);
  1794. U32 textWidth = profile->getFont(mFontSizeAdjust)->getStrWidth(trimmedLine.c_str());
  1795. if(textWidth < totalWidth)
  1796. {
  1797. offsetX = getTextHorizontalOffset(textWidth, totalWidth, getAlignmentType(profile));
  1798. }
  1799. Point2I start = Point2I(0, 0);
  1800. F32 rotation = 0.0f;
  1801. if (rot == tRotateNone)
  1802. {
  1803. start.x += offsetX;
  1804. start.y += offsetY;
  1805. rotation = 0.0f;
  1806. }
  1807. else if (rot == tRotateLeft)
  1808. {
  1809. start.x = offsetY;
  1810. start.y = extent.y + offsetX;
  1811. rotation = 90.0f;
  1812. }
  1813. else if (rot == tRotateRight)
  1814. {
  1815. start.x = extent.x - offsetY;
  1816. start.y = offsetX;
  1817. rotation = -90.0f;
  1818. }
  1819. renderTextLine(start + offset + profile->mTextOffset, trimmedLine, profile, rotation, ibeamPos, lineNumber);
  1820. offsetY += textHeight;
  1821. ibeamPos += line.length();
  1822. lineNumber++;
  1823. }
  1824. }
  1825. vector<string> GuiControl::getLineList(const char* text, GuiControlProfile* profile, S32 totalWidth)
  1826. {
  1827. GFont* font = profile->getFont(mFontSizeAdjust);
  1828. vector<string> lineList = vector<string>();
  1829. if (!mTextWrap)
  1830. {
  1831. lineList.push_back(text);
  1832. }
  1833. else
  1834. {
  1835. vector<string> paragraphList = vector<string>();
  1836. istringstream f(text);
  1837. string s;
  1838. while (getline(f, s)) {
  1839. paragraphList.push_back(s);
  1840. }
  1841. for (string& paragraph : paragraphList)
  1842. {
  1843. vector<string> wordList = vector<string>();
  1844. istringstream f2(paragraph);
  1845. string s2;
  1846. while (getline(f2, s2, ' ')) {
  1847. wordList.push_back(s2);
  1848. }
  1849. //now process the word list
  1850. string line;
  1851. bool newLine = true;
  1852. line.clear();
  1853. for (string& word : wordList)
  1854. {
  1855. if (font->getStrWidth(word.c_str()) >= totalWidth)
  1856. {
  1857. if (line.size() > 0)
  1858. {
  1859. lineList.push_back(string(line + " "));
  1860. line.clear();
  1861. }
  1862. lineList.push_back(word + " ");
  1863. newLine = true;
  1864. continue;
  1865. }
  1866. string prevLine = string(line);
  1867. line += (!newLine) ? " " + word : word;
  1868. newLine = false;
  1869. if (font->getStrWidth(line.c_str()) >= totalWidth && word.length() != 0)
  1870. {
  1871. lineList.push_back(prevLine + " ");
  1872. line = word;
  1873. }
  1874. }
  1875. if (paragraph.back() == ' ')
  1876. {
  1877. line += " ";
  1878. }
  1879. lineList.push_back(string(line));
  1880. }
  1881. }
  1882. return lineList;
  1883. }
  1884. void GuiControl::renderTextLine(const Point2I& startPoint, const string line, GuiControlProfile* profile, F32 rotationInDegrees, U32, U32)
  1885. {
  1886. dglDrawText(profile->getFont(mFontSizeAdjust), startPoint, line.c_str(), profile->mFontColors, 9, rotationInDegrees);
  1887. }
  1888. S32 GuiControl::getTextHorizontalOffset(S32 textWidth, S32 totalWidth, AlignmentType align)
  1889. {
  1890. if (align == RightAlign)
  1891. {
  1892. return totalWidth - textWidth;
  1893. }
  1894. else if (align == CenterAlign)
  1895. {
  1896. return (totalWidth - textWidth) / 2;
  1897. }
  1898. return 0;//left aligned
  1899. }
  1900. S32 GuiControl::getTextVerticalOffset(S32 textHeight, S32 totalHeight, VertAlignmentType align)
  1901. {
  1902. if (align == MiddleVAlign)
  1903. {
  1904. return (totalHeight - textHeight) / 2;
  1905. }
  1906. else if (align == BottomVAlign)
  1907. {
  1908. return totalHeight - textHeight;
  1909. }
  1910. return 0;
  1911. }
  1912. void GuiControl::getCursor(GuiCursor *&cursor, bool &showCursor, const GuiEvent &lastGuiEvent)
  1913. {
  1914. lastGuiEvent;
  1915. if(GuiControl::smCursorChanged != -1 && !isMouseLocked())
  1916. {
  1917. // We've already changed the cursor,
  1918. // so set it back before we change it again.
  1919. Input::popCursor();
  1920. // We haven't changed it
  1921. GuiControl::smCursorChanged = -1;
  1922. }
  1923. }
  1924. const char* GuiControl::execConsoleCallback()
  1925. {
  1926. if (mConsoleCommand && mConsoleCommand[0])
  1927. {
  1928. Con::setVariable("$ThisControl", avar("%d",getId()));
  1929. return Con::evaluate(mConsoleCommand, false);
  1930. }
  1931. return "";
  1932. }
  1933. const char* GuiControl::execAltConsoleCallback()
  1934. {
  1935. if(mAltConsoleCommand && mAltConsoleCommand[0])
  1936. {
  1937. Con::setVariable("$ThisControl", avar("%d",getId()));
  1938. return Con::evaluate(mAltConsoleCommand, false);
  1939. }
  1940. return "";
  1941. }
  1942. void GuiControl::setText(const char *text)
  1943. {
  1944. mText = StringTable->insert(text, true);
  1945. }
  1946. void GuiControl::setTextID(const char *id)
  1947. {
  1948. S32 n = Con::getIntVariable(id, -1);
  1949. if (n != -1)
  1950. {
  1951. mTextID = StringTable->insert(id);
  1952. setTextID(n);
  1953. }
  1954. }
  1955. void GuiControl::setTextID(S32 id)
  1956. {
  1957. const UTF8 *str = getGUIString(id);
  1958. if (str)
  1959. setText((const char*)str);
  1960. }
  1961. const char *GuiControl::getText()
  1962. {
  1963. return mText;
  1964. }
  1965. void GuiControl::setDataField(StringTableEntry slotName, const char* array, const char* value)
  1966. {
  1967. this->findField(slotName);
  1968. const AbstractClassRep::Field* fld = this->findField(slotName);
  1969. if(fld)
  1970. {
  1971. if (fld->type == AbstractClassRep::DepricatedFieldType ||
  1972. fld->type == AbstractClassRep::StartGroupFieldType ||
  1973. fld->type == AbstractClassRep::EndGroupFieldType)
  1974. return;
  1975. ConsoleBaseType* cbt = ConsoleBaseType::getType(fld->type);
  1976. bool isProfile = strcmp(cbt->getTypeName(), "TypeGuiProfile") == 0;
  1977. if(isProfile && mAwake)
  1978. {
  1979. //Decrease the ref count on the old profile
  1980. void* dptr = (void*)(((const char*)this) + fld->offset);
  1981. GuiControlProfile** obj = (GuiControlProfile**)dptr;
  1982. if((*obj))
  1983. (*obj)->decRefCount();
  1984. }
  1985. SimObject::setDataField(slotName, array, value);
  1986. if (isProfile && mAwake)
  1987. {
  1988. //Increase the ref count on the new profile
  1989. void* dptr = (void*)(((const char*)this) + fld->offset);
  1990. GuiControlProfile** obj = (GuiControlProfile**)dptr;
  1991. if ((*obj))
  1992. (*obj)->incRefCount();
  1993. }
  1994. }
  1995. else
  1996. {
  1997. SimObject::setDataField(slotName, array, value);
  1998. }
  1999. }
  2000. AlignmentType GuiControl::getAlignmentType()
  2001. {
  2002. return getAlignmentType(mProfile);
  2003. }
  2004. AlignmentType GuiControl::getAlignmentType(GuiControlProfile* profile)
  2005. {
  2006. return mAlignment == AlignmentType::DefaultAlign ? profile->mAlignment : mAlignment;
  2007. }
  2008. VertAlignmentType GuiControl::getVertAlignmentType()
  2009. {
  2010. return getVertAlignmentType(mProfile);
  2011. }
  2012. VertAlignmentType GuiControl::getVertAlignmentType(GuiControlProfile* profile)
  2013. {
  2014. return mVAlignment == VertAlignmentType::DefaultVAlign ? profile->mVAlignment : mVAlignment;
  2015. }
  2016. const ColorI& GuiControl::getFontColor(GuiControlProfile* profile, const GuiControlState state)
  2017. {
  2018. return mOverrideFontColor ? mFontColor : profile->getFontColor(state);
  2019. }
  2020. bool GuiControl::isEditMode()
  2021. {
  2022. if (smDesignTime && smEditorHandle)
  2023. {
  2024. GuiEditCtrl* edit = GuiControl::smEditorHandle;
  2025. if (this == edit->getRoot())
  2026. {
  2027. return true;
  2028. }
  2029. //work up the parent chain to see if one of the parents is the edit root
  2030. GuiControl* parent = getParent();
  2031. if (parent)
  2032. {
  2033. return parent->isEditMode();
  2034. }
  2035. }
  2036. return false;
  2037. }
  2038. bool GuiControl::isEditSelected()
  2039. {
  2040. if (smDesignTime && smEditorHandle)
  2041. {
  2042. GuiEditCtrl* edit = GuiControl::smEditorHandle;
  2043. bool selected = false;
  2044. auto list = edit->getSelected();
  2045. for (auto i = list->begin(); i < list->end(); i++)
  2046. {
  2047. GuiControl* ctrl = dynamic_cast<GuiControl*>(*i);
  2048. if (ctrl && ctrl == this)
  2049. {
  2050. return true;
  2051. }
  2052. }
  2053. }
  2054. return false;
  2055. }
  2056. bool GuiControl::onMouseDownEditor(const GuiEvent& event, const Point2I& offset)
  2057. {
  2058. GuiEditCtrl* edit = GuiControl::smEditorHandle;
  2059. GuiControl* parent = getParent();
  2060. if (this != edit->getRoot() && parent)
  2061. {
  2062. return parent->onMouseDownEditor(event, offset);
  2063. }
  2064. return false;
  2065. }
  2066. //--------------------------------------------------------------------
  2067. GuiEasingSupport::GuiEasingSupport()
  2068. {
  2069. //fill color
  2070. mEaseFillColorHL = EasingFunction::Linear;
  2071. mEaseFillColorSL = EasingFunction::Linear;
  2072. mEaseTimeFillColorHL = 500;
  2073. mEaseTimeFillColorSL = 0;
  2074. //control state
  2075. mPreviousState = GuiControlState::DisabledState;
  2076. mCurrentState = GuiControlState::DisabledState;
  2077. mFluidFillColor = FluidColorI(); //The actual fill color as it moves fluidly from one color to another.
  2078. }
  2079. void GuiEasingSupport::initPersistFields()
  2080. {
  2081. Parent::initPersistFields();
  2082. addGroup("Gui Easing Settings");
  2083. addField("easeFillColorHL", TypeEnum, Offset(mEaseFillColorHL, GuiEasingSupport), 1, &gEasingTable);
  2084. addField("easeFillColorSL", TypeEnum, Offset(mEaseFillColorSL, GuiEasingSupport), 1, &gEasingTable);
  2085. addField("easeTimeFillColorHL", TypeS32, Offset(mEaseTimeFillColorHL, GuiEasingSupport));
  2086. addField("easeTimeFillColorSL", TypeS32, Offset(mEaseTimeFillColorSL, GuiEasingSupport));
  2087. endGroup("Gui Easing Settings");
  2088. }
  2089. const ColorI& GuiEasingSupport::getFillColor(const GuiControlState state)
  2090. {
  2091. if (state != mCurrentState)
  2092. {
  2093. //We have just switched states!
  2094. mPreviousState = mCurrentState;
  2095. mCurrentState = state;
  2096. if (mCurrentState == GuiControlState::DisabledState || mPreviousState == GuiControlState::DisabledState)
  2097. {
  2098. mFluidFillColor.stopFluidAnimation();
  2099. mFluidFillColor.set(mProfile->getFillColor(state));
  2100. }
  2101. else if (mCurrentState == GuiControlState::SelectedState || mPreviousState == GuiControlState::SelectedState)
  2102. {
  2103. mFluidFillColor.setEasingFunction(mEaseFillColorSL);
  2104. mFluidFillColor.setAnimationLength(mEaseTimeFillColorSL);
  2105. mFluidFillColor.startFluidAnimation(mProfile->getFillColor(state));
  2106. }
  2107. else if (mCurrentState == GuiControlState::HighlightState || mPreviousState == GuiControlState::HighlightState)
  2108. {
  2109. mFluidFillColor.setEasingFunction(mEaseFillColorHL);
  2110. mFluidFillColor.setAnimationLength(mEaseTimeFillColorHL);
  2111. mFluidFillColor.startFluidAnimation(mProfile->getFillColor(state));
  2112. }
  2113. else
  2114. {
  2115. //we should never get here...
  2116. mFluidFillColor.stopFluidAnimation();
  2117. mFluidFillColor.set(mProfile->getFillColor(state));
  2118. }
  2119. }
  2120. if (mFluidFillColor.isAnimating() && !isProcessingTicks())
  2121. {
  2122. setProcessTicks(true);
  2123. }
  2124. if (!mFluidFillColor.isAnimating())
  2125. {
  2126. mFluidFillColor.set(mProfile->getFillColor(state));
  2127. }
  2128. return mFluidFillColor;
  2129. }
  2130. void GuiEasingSupport::processTick()
  2131. {
  2132. bool shouldWeContinue = false;
  2133. shouldWeContinue |= mFluidFillColor.processTick();
  2134. if (!shouldWeContinue)
  2135. {
  2136. setProcessTicks(false);
  2137. }
  2138. }
  2139. void GuiEasingSupport::setControlProfile(GuiControlProfile* prof)
  2140. {
  2141. Parent::setControlProfile(prof);
  2142. mCurrentState = mCurrentState == DisabledState ? NormalState : DisabledState;
  2143. }