2
0

guiControl.cc 66 KB

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