guiControl.cc 72 KB

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