guiControl.cc 67 KB

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