guiControl.cc 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930
  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. //used to locate the next/prev responder when tab is pressed
  48. S32 GuiControl::smCursorChanged = -1;
  49. GuiControl *GuiControl::smPrevResponder = NULL;
  50. GuiControl *GuiControl::smCurResponder = NULL;
  51. GuiEditCtrl *GuiControl::smEditorHandle = NULL;
  52. bool GuiControl::smDesignTime = false;
  53. GuiControl::GuiControl()
  54. {
  55. mLayer = 0;
  56. mBounds.set(0, 0, 64, 64);
  57. mStoredExtent.set(0, 0);
  58. mRenderInsetLT.set(0, 0);
  59. mRenderInsetRB.set(0, 0);
  60. mMinExtent.set(0, 0);
  61. mProfile = NULL;
  62. mConsoleVariable = StringTable->EmptyString;
  63. mConsoleCommand = StringTable->EmptyString;
  64. mAltConsoleCommand = StringTable->EmptyString;
  65. mAcceleratorKey = StringTable->EmptyString;
  66. mLangTableName = StringTable->EmptyString;
  67. mText = StringTable->EmptyString;
  68. mTextID = StringTable->EmptyString;
  69. mLangTable = NULL;
  70. mFirstResponder = NULL;
  71. mCanSaveFieldDictionary = false;
  72. mVisible = true;
  73. mActive = false;
  74. mAwake = false;
  75. mCanSave = true;
  76. mHorizSizing = horizResizeRight;
  77. mVertSizing = vertResizeBottom;
  78. mTooltipProfile = NULL;
  79. mTooltip = StringTable->EmptyString;
  80. mTipHoverTime = 1000;
  81. mTooltipWidth = 250;
  82. mIsContainer = false;
  83. mTextWrap = false;
  84. }
  85. GuiControl::~GuiControl()
  86. {
  87. }
  88. bool GuiControl::onAdd()
  89. {
  90. // Let Parent Do Work.
  91. if(!Parent::onAdd())
  92. return false;
  93. // Grab the classname of this object
  94. const char *cName = getClassName();
  95. // if we're a pure GuiControl, then we're a container by default.
  96. if(dStrcmp("GuiControl", cName) == 0)
  97. mIsContainer = true;
  98. // Clamp to minExtent
  99. mBounds.extent.x = getMax( mMinExtent.x, mBounds.extent.x );
  100. mBounds.extent.y = getMax( mMinExtent.y, mBounds.extent.y );
  101. // Add to root group.
  102. Sim::getGuiGroup()->addObject(this);
  103. // Return Success.
  104. return true;
  105. }
  106. void GuiControl::onChildAdded( GuiControl *child )
  107. {
  108. if(mProfile)
  109. {
  110. //This will cause the child control to be centered if it needs to be.
  111. RectI innerRect = this->getInnerRect(mBounds.point, mBounds.extent, GuiControlState::NormalState, mProfile);
  112. child->parentResized(innerRect.extent, innerRect.extent);
  113. }
  114. }
  115. static EnumTable::Enums horzEnums[] =
  116. {
  117. { GuiControl::horizResizeRight, "right" },
  118. { GuiControl::horizResizeWidth, "width" },
  119. { GuiControl::horizResizeLeft, "left" },
  120. { GuiControl::horizResizeCenter, "center" },
  121. { GuiControl::horizResizeRelative, "relative" }
  122. };
  123. static EnumTable gHorizSizingTable(5, &horzEnums[0]);
  124. static EnumTable::Enums vertEnums[] =
  125. {
  126. { GuiControl::vertResizeBottom, "bottom" },
  127. { GuiControl::vertResizeHeight, "height" },
  128. { GuiControl::vertResizeTop, "top" },
  129. { GuiControl::vertResizeCenter, "center" },
  130. { GuiControl::vertResizeRelative, "relative" }
  131. };
  132. static EnumTable gVertSizingTable(5, &vertEnums[0]);
  133. void GuiControl::initPersistFields()
  134. {
  135. Parent::initPersistFields();
  136. // Things relevant only to the editor.
  137. addGroup("Gui Editing");
  138. addField("isContainer", TypeBool, Offset(mIsContainer, GuiControl));
  139. endGroup("Gui Editing");
  140. // Parent Group.
  141. addGroup("GuiControl");
  142. addField("Profile", TypeGuiProfile, Offset(mProfile, GuiControl));
  143. addField("HorizSizing", TypeEnum, Offset(mHorizSizing, GuiControl), 1, &gHorizSizingTable);
  144. addField("VertSizing", TypeEnum, Offset(mVertSizing, GuiControl), 1, &gVertSizingTable);
  145. addField("Position", TypePoint2I, Offset(mBounds.point, GuiControl));
  146. addProtectedField("Extent", TypePoint2I, Offset(mBounds.extent, GuiControl), &setExtentFn, &defaultProtectedGetFn, "The size of the control writen as width and height.");
  147. addProtectedField("MinExtent", TypePoint2I, Offset(mMinExtent, GuiControl), &setMinExtentFn, &defaultProtectedGetFn, &writeMinExtentFn, "The extent will not shrink below this size.");
  148. addField("canSave", TypeBool, Offset(mCanSave, GuiControl));
  149. addField("Visible", TypeBool, Offset(mVisible, GuiControl));
  150. addDepricatedField("Modal");
  151. addDepricatedField("SetFirstResponder");
  152. addField("Variable", TypeString, Offset(mConsoleVariable, GuiControl));
  153. addField("Command", TypeString, Offset(mConsoleCommand, GuiControl));
  154. addField("AltCommand", TypeString, Offset(mAltConsoleCommand, GuiControl));
  155. addField("Accelerator", TypeString, Offset(mAcceleratorKey, GuiControl));
  156. addField("Active", TypeBool, Offset(mActive, GuiControl));
  157. endGroup("GuiControl");
  158. addGroup("ToolTip");
  159. addField("tooltipprofile", TypeGuiProfile, Offset(mTooltipProfile, GuiControl));
  160. addField("tooltip", TypeString, Offset(mTooltip, GuiControl));
  161. addField("tooltipWidth", TypeS32, Offset(mTooltipWidth, GuiControl));
  162. addField("hovertime", TypeS32, Offset(mTipHoverTime, GuiControl));
  163. endGroup("ToolTip");
  164. addGroup("Localization");
  165. addField("langTableMod", TypeString, Offset(mLangTableName, GuiControl));
  166. endGroup("Localization");
  167. addGroup("Text");
  168. addProtectedField("text", TypeCaseString, Offset(mText, GuiControl), setTextProperty, getTextProperty, "");
  169. addField("textID", TypeString, Offset(mTextID, GuiControl));
  170. addField("textWrap", TypeBool, Offset(mTextWrap, GuiControl), &writeTextWrapFn, "If true, text will wrap to additional lines.");
  171. endGroup("Text");
  172. }
  173. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //
  174. LangTable * GuiControl::getGUILangTable()
  175. {
  176. if(mLangTable)
  177. return mLangTable;
  178. if(mLangTableName && *mLangTableName)
  179. {
  180. mLangTable = (LangTable *)getModLangTable((const UTF8*)mLangTableName);
  181. return mLangTable;
  182. }
  183. GuiControl *parent = getParent();
  184. if(parent)
  185. return parent->getGUILangTable();
  186. return NULL;
  187. }
  188. const UTF8 * GuiControl::getGUIString(S32 id)
  189. {
  190. LangTable *lt = getGUILangTable();
  191. if(lt)
  192. return lt->getString(id);
  193. return NULL;
  194. }
  195. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //
  196. void GuiControl::addObject(SimObject *object)
  197. {
  198. GuiControl *ctrl = dynamic_cast<GuiControl *>(object);
  199. if(!ctrl)
  200. {
  201. AssertWarn(0, "GuiControl::addObject: attempted to add NON GuiControl to set");
  202. return;
  203. }
  204. if(object->getGroup() == this)
  205. return;
  206. Parent::addObject(object);
  207. AssertFatal(!ctrl->isAwake(), "GuiControl::addObject: object is already awake before add");
  208. if(mAwake)
  209. ctrl->awaken();
  210. // If we are a child, notify our parent that we've been added
  211. GuiControl *parent = ctrl->getParent();
  212. if( parent )
  213. parent->onChildAdded( ctrl );
  214. }
  215. void GuiControl::removeObject(SimObject *object)
  216. {
  217. GuiControl *ctrl = dynamic_cast<GuiControl *>(object);
  218. if (!ctrl)
  219. {
  220. AssertWarn(0, "GuiControl::removeObject: attempted to remove NON GuiControl from set");
  221. return;
  222. }
  223. GuiControl *parent = ctrl->getParent();
  224. AssertFatal(mAwake == static_cast<GuiControl*>(object)->isAwake(), "GuiControl::removeObject: child control wake state is bad");
  225. if (mAwake)
  226. static_cast<GuiControl*>(object)->sleep();
  227. Parent::removeObject(object);
  228. // If we are a child, notify our parent that we've been removed
  229. if (parent)
  230. parent->onChildRemoved(ctrl);
  231. }
  232. GuiControl *GuiControl::getParent()
  233. {
  234. SimObject *obj = getGroup();
  235. if (GuiControl* gui = dynamic_cast<GuiControl*>(obj))
  236. return gui;
  237. return 0;
  238. }
  239. GuiCanvas *GuiControl::getRoot()
  240. {
  241. GuiControl *root = NULL;
  242. GuiControl *parent = getParent();
  243. while (parent)
  244. {
  245. root = parent;
  246. parent = parent->getParent();
  247. }
  248. if (root)
  249. return dynamic_cast<GuiCanvas*>(root);
  250. else
  251. return NULL;
  252. }
  253. void GuiControl::inspectPreApply()
  254. {
  255. if(smDesignTime && smEditorHandle)
  256. smEditorHandle->controlInspectPreApply(this);
  257. // The canvas never sleeps
  258. if(mAwake && dynamic_cast<GuiCanvas*>(this) == NULL )
  259. {
  260. onSleep(); // release all our resources.
  261. mAwake = true;
  262. }
  263. }
  264. void GuiControl::inspectPostApply()
  265. {
  266. // Shhhhhhh, you don't want to wake the canvas!
  267. if(mAwake && dynamic_cast<GuiCanvas*>(this) == NULL )
  268. {
  269. mAwake = false;
  270. onWake();
  271. }
  272. if(smDesignTime && smEditorHandle)
  273. smEditorHandle->controlInspectPostApply(this);
  274. }
  275. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //
  276. Point2I GuiControl::localToGlobalCoord(const Point2I &src)
  277. {
  278. Point2I ret = src;
  279. ret += (mBounds.point + mRenderInsetLT);
  280. GuiControl *walk = getParent();
  281. while(walk)
  282. {
  283. ret += (walk->getPosition() + walk->mRenderInsetLT);
  284. walk = walk->getParent();
  285. }
  286. return ret;
  287. }
  288. Point2I GuiControl::globalToLocalCoord(const Point2I &src)
  289. {
  290. Point2I ret = src;
  291. ret -= (mBounds.point + mRenderInsetLT);
  292. GuiControl *walk = getParent();
  293. while(walk)
  294. {
  295. ret -= (walk->getPosition() + walk->mRenderInsetLT);
  296. walk = walk->getParent();
  297. }
  298. return ret;
  299. }
  300. //----------------------------------------------------------------
  301. void GuiControl::resize(const Point2I &newPosition, const Point2I &newExtent)
  302. {
  303. Point2I actualNewExtent = Point2I(getMax(mMinExtent.x, newExtent.x),
  304. getMax(mMinExtent.y, newExtent.y));
  305. Point2I oldExtent = mBounds.extent;
  306. // only do the child control resizing stuff if you really need to.
  307. bool extentChanged = (actualNewExtent != oldExtent);
  308. if (extentChanged) {
  309. //call set update both before and after
  310. setUpdate();
  311. mBounds.set(newPosition, actualNewExtent);
  312. iterator i;
  313. for(i = begin(); i != end(); i++)
  314. {
  315. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  316. ctrl->parentResized(oldExtent - (ctrl->mRenderInsetLT + ctrl->mRenderInsetRB), actualNewExtent - (ctrl->mRenderInsetLT + ctrl->mRenderInsetRB));
  317. }
  318. GuiControl *parent = getParent();
  319. if (parent)
  320. parent->childResized(this);
  321. setUpdate();
  322. }
  323. else {
  324. mBounds.point = newPosition;
  325. }
  326. }
  327. void GuiControl::setPosition( const Point2I &newPosition )
  328. {
  329. resize( newPosition, mBounds.extent );
  330. }
  331. void GuiControl::setExtent( const Point2I &newExtent )
  332. {
  333. resize( mBounds.point, newExtent );
  334. }
  335. void GuiControl::setBounds( const RectI &newBounds )
  336. {
  337. resize( newBounds.point, newBounds.extent );
  338. }
  339. void GuiControl::setLeft( S32 newLeft )
  340. {
  341. resize( Point2I( newLeft, mBounds.point.y), mBounds.extent );
  342. }
  343. void GuiControl::setTop( S32 newTop )
  344. {
  345. resize( Point2I( mBounds.point.x, newTop ), mBounds.extent );
  346. }
  347. void GuiControl::setWidth( S32 newWidth )
  348. {
  349. resize( mBounds.point, Point2I( newWidth, mBounds.extent.y ) );
  350. }
  351. void GuiControl::setHeight( S32 newHeight )
  352. {
  353. resize( mBounds.point, Point2I( mBounds.extent.x, newHeight ) );
  354. }
  355. void GuiControl::childResized(GuiControl *child)
  356. {
  357. // Default to do nothing. Do not call resize from here as it will create an infinite loop.
  358. }
  359. void GuiControl::parentResized(const Point2I &oldParentExtent, const Point2I &newParentExtent)
  360. {
  361. Point2I newPosition = getPosition();
  362. Point2I newExtent = getExtent();
  363. S32 deltaX = newParentExtent.x - oldParentExtent.x;
  364. S32 deltaY = newParentExtent.y - oldParentExtent.y;
  365. //In the case of centering, we want to make doubly sure we are using the inner rect.
  366. GuiControl* parent = getParent();
  367. Point2I parentInnerExt = Point2I(newParentExtent);
  368. if(mHorizSizing == horizResizeCenter || mVertSizing == vertResizeCenter)
  369. {
  370. //This is based on the "new" outer extent of the parent.
  371. Point2I origin = Point2I(0, 0);
  372. parentInnerExt = getInnerRect(origin, parent->mBounds.extent, NormalState, parent->mProfile).extent;
  373. }
  374. if (mHorizSizing == horizResizeCenter)
  375. newPosition.x = (parentInnerExt.x - mBounds.extent.x) >> 1;
  376. else if (mHorizSizing == horizResizeWidth)
  377. newExtent.x += deltaX;
  378. else if (mHorizSizing == horizResizeLeft)
  379. newPosition.x += deltaX;
  380. else if (mHorizSizing == horizResizeRelative && oldParentExtent.x != 0)
  381. {
  382. S32 newLeft = (newPosition.x * newParentExtent.x) / oldParentExtent.x;
  383. S32 newRight = ((newPosition.x + newExtent.x) * newParentExtent.x) / oldParentExtent.x;
  384. newPosition.x = newLeft;
  385. newExtent.x = newRight - newLeft;
  386. }
  387. if (mVertSizing == vertResizeCenter)
  388. newPosition.y = (parentInnerExt.y - mBounds.extent.y) >> 1;
  389. else if (mVertSizing == vertResizeHeight)
  390. newExtent.y += deltaY;
  391. else if (mVertSizing == vertResizeTop)
  392. newPosition.y += deltaY;
  393. else if(mVertSizing == vertResizeRelative && oldParentExtent.y != 0)
  394. {
  395. S32 newTop = (newPosition.y * newParentExtent.y) / oldParentExtent.y;
  396. S32 newBottom = ((newPosition.y + newExtent.y) * newParentExtent.y) / oldParentExtent.y;
  397. newPosition.y = newTop;
  398. newExtent.y = newBottom - newTop;
  399. }
  400. newExtent = extentBattery(newExtent);
  401. resize(newPosition, newExtent);
  402. }
  403. Point2I GuiControl::extentBattery(Point2I &newExtent)
  404. {
  405. if (mMinExtent.x == 0 && mMinExtent.y == 0)
  406. {
  407. return newExtent;
  408. }
  409. Point2I result = Point2I(newExtent);
  410. if (newExtent.x < mBounds.extent.x && newExtent.x < mMinExtent.x)
  411. {
  412. mStoredExtent.x += mBounds.extent.x > mMinExtent.x ? (mMinExtent.x - newExtent.x) : (mBounds.extent.x - newExtent.x);
  413. result.x = mMinExtent.x;
  414. }
  415. else if (newExtent.x > mBounds.extent.x && mStoredExtent.x > 0)
  416. {
  417. S32 charge = getMin(newExtent.x - mBounds.extent.x, mStoredExtent.x);
  418. mStoredExtent.x -= charge;
  419. result.x = newExtent.x - charge;
  420. }
  421. if (newExtent.y < mBounds.extent.y && newExtent.y < mMinExtent.y)
  422. {
  423. mStoredExtent.y += mBounds.extent.y > mMinExtent.y ? (mMinExtent.y - newExtent.y) : (mBounds.extent.y - newExtent.y);
  424. result.y = mMinExtent.y;
  425. }
  426. else if (newExtent.y > mBounds.extent.y && mStoredExtent.y > 0)
  427. {
  428. S32 charge = getMin(newExtent.y - mBounds.extent.y, mStoredExtent.y);
  429. mStoredExtent.y -= charge;
  430. result.y = newExtent.y - charge;
  431. }
  432. return result;
  433. }
  434. //----------------------------------------------------------------
  435. void GuiControl::onRender(Point2I offset, const RectI &updateRect)
  436. {
  437. RectI ctrlRect = applyMargins(offset, mBounds.extent, NormalState, mProfile);
  438. if (!ctrlRect.isValidRect())
  439. {
  440. return;
  441. }
  442. renderUniversalRect(ctrlRect, mProfile, NormalState);
  443. //Render Text
  444. dglSetBitmapModulation(mProfile->mFontColor);
  445. RectI fillRect = applyBorders(ctrlRect.point, ctrlRect.extent, NormalState, mProfile);
  446. RectI contentRect = applyPadding(fillRect.point, fillRect.extent, NormalState, mProfile);
  447. if(contentRect.isValidRect())
  448. {
  449. renderText(contentRect.point, contentRect.extent, mText, mProfile);
  450. //Render the childen
  451. renderChildControls(offset, contentRect, updateRect);
  452. }
  453. }
  454. RectI GuiControl::applyMargins(Point2I &offset, Point2I &extent, GuiControlState currentState, GuiControlProfile *profile)
  455. {
  456. //Get the border profiles
  457. GuiBorderProfile *leftProfile = profile->getLeftBorder();
  458. GuiBorderProfile *rightProfile = profile->getRightBorder();
  459. GuiBorderProfile *topProfile = profile->getTopBorder();
  460. GuiBorderProfile *bottomProfile = profile->getBottomBorder();
  461. S32 leftSize = (leftProfile) ? leftProfile->getMargin(currentState) : 0;
  462. S32 rightSize = (rightProfile) ? rightProfile->getMargin(currentState) : 0;
  463. S32 topSize = (topProfile) ? topProfile->getMargin(currentState) : 0;
  464. S32 bottomSize = (bottomProfile) ? bottomProfile->getMargin(currentState) : 0;
  465. return RectI(offset.x + leftSize, offset.y + topSize, (extent.x - leftSize) - rightSize, (extent.y - topSize) - bottomSize);
  466. }
  467. RectI GuiControl::applyBorders(Point2I &offset, Point2I &extent, GuiControlState currentState, GuiControlProfile *profile)
  468. {
  469. //Get the border profiles
  470. GuiBorderProfile *leftProfile = profile->getLeftBorder();
  471. GuiBorderProfile *rightProfile = profile->getRightBorder();
  472. GuiBorderProfile *topProfile = profile->getTopBorder();
  473. GuiBorderProfile *bottomProfile = profile->getBottomBorder();
  474. S32 leftSize = (leftProfile) ? leftProfile->getBorder(currentState) : 0;
  475. S32 rightSize = (rightProfile) ? rightProfile->getBorder(currentState) : 0;
  476. S32 topSize = (topProfile) ? topProfile->getBorder(currentState) : 0;
  477. S32 bottomSize = (bottomProfile) ? bottomProfile->getBorder(currentState) : 0;
  478. return RectI(offset.x + leftSize, offset.y + topSize, (extent.x - leftSize) - rightSize, (extent.y - topSize) - bottomSize);
  479. }
  480. RectI GuiControl::applyPadding(Point2I &offset, Point2I &extent, GuiControlState currentState, GuiControlProfile *profile)
  481. {
  482. //Get the border profiles
  483. GuiBorderProfile *leftProfile = profile->getLeftBorder();
  484. GuiBorderProfile *rightProfile = profile->getRightBorder();
  485. GuiBorderProfile *topProfile = profile->getTopBorder();
  486. GuiBorderProfile *bottomProfile = profile->getBottomBorder();
  487. S32 leftSize = (leftProfile) ? leftProfile->getPadding(currentState) : 0;
  488. S32 rightSize = (rightProfile) ? rightProfile->getPadding(currentState) : 0;
  489. S32 topSize = (topProfile) ? topProfile->getPadding(currentState) : 0;
  490. S32 bottomSize = (bottomProfile) ? bottomProfile->getPadding(currentState) : 0;
  491. return RectI(offset.x + leftSize, offset.y + topSize, (extent.x - leftSize) - rightSize, (extent.y - topSize) - bottomSize);
  492. }
  493. RectI GuiControl::getInnerRect(Point2I &offset, Point2I &extent, GuiControlState currentState, GuiControlProfile *profile)
  494. {
  495. //Get the border profiles
  496. GuiBorderProfile *leftProfile = profile->getLeftBorder();
  497. GuiBorderProfile *rightProfile = profile->getRightBorder();
  498. GuiBorderProfile *topProfile = profile->getTopBorder();
  499. GuiBorderProfile *bottomProfile = profile->getBottomBorder();
  500. S32 leftSize = (leftProfile) ? leftProfile->getMargin(currentState) + leftProfile->getBorder(currentState) + leftProfile->getPadding(currentState) : 0;
  501. S32 rightSize = (rightProfile) ? rightProfile->getMargin(currentState) + rightProfile->getBorder(currentState) + rightProfile->getPadding(currentState) : 0;
  502. S32 topSize = (topProfile) ? topProfile->getMargin(currentState) + topProfile->getBorder(currentState) + topProfile->getPadding(currentState) : 0;
  503. S32 bottomSize = (bottomProfile) ? bottomProfile->getMargin(currentState) + bottomProfile->getBorder(currentState) + bottomProfile->getPadding(currentState) : 0;
  504. return RectI(offset.x + leftSize, offset.y + topSize, (extent.x - leftSize) - rightSize, (extent.y - topSize) - bottomSize);
  505. }
  506. Point2I GuiControl::getOuterExtent(Point2I &innerExtent, GuiControlState currentState, GuiControlProfile *profile)
  507. {
  508. //Get the border profiles
  509. GuiBorderProfile *leftProfile = profile->getLeftBorder();
  510. GuiBorderProfile *rightProfile = profile->getRightBorder();
  511. GuiBorderProfile *topProfile = profile->getTopBorder();
  512. GuiBorderProfile *bottomProfile = profile->getBottomBorder();
  513. S32 leftSize = (leftProfile) ? leftProfile->getMargin(currentState) + leftProfile->getBorder(currentState) + leftProfile->getPadding(currentState) : 0;
  514. S32 rightSize = (rightProfile) ? rightProfile->getMargin(currentState) + rightProfile->getBorder(currentState) + rightProfile->getPadding(currentState) : 0;
  515. S32 topSize = (topProfile) ? topProfile->getMargin(currentState) + topProfile->getBorder(currentState) + topProfile->getPadding(currentState) : 0;
  516. S32 bottomSize = (bottomProfile) ? bottomProfile->getMargin(currentState) + bottomProfile->getBorder(currentState) + bottomProfile->getPadding(currentState) : 0;
  517. return Point2I(innerExtent.x + leftSize + rightSize, innerExtent.y + topSize + bottomSize);
  518. }
  519. bool GuiControl::renderTooltip(Point2I &cursorPos, const char* tipText )
  520. {
  521. #if !defined(TORQUE_OS_IOS) && !defined(TORQUE_OS_ANDROID) && !defined(TORQUE_OS_EMSCRIPTEN)
  522. // Short Circuit.
  523. if (!mAwake)
  524. return false;
  525. if ( dStrlen( mTooltip ) == 0 && ( tipText == NULL || dStrlen( tipText ) == 0 ) )
  526. return false;
  527. const char* renderTip = mTooltip;
  528. if( tipText != NULL )
  529. renderTip = tipText;
  530. // Finish if no root.
  531. GuiCanvas *root = getRoot();
  532. if ( !root )
  533. return false;
  534. if (!mTooltipProfile)
  535. setField("TooltipProfile", "GuiTooltipProfile");
  536. GFont *font = mTooltipProfile->mFont;
  537. // Set text bounds.
  538. Point2I textBounds( 0, 0 );
  539. // Fetch the width of a space.
  540. const S32 spaceWidth = (S32)font->getStrWidth(" ");
  541. // Fetch the maximum allowed tooltip extent.
  542. const S32 maxTooltipWidth = mTooltipWidth;
  543. // Fetch word count.
  544. const S32 wordCount = StringUnit::getUnitCount( renderTip, " " );
  545. // Reset line storage.
  546. const S32 tooltipLineStride = (S32)font->getHeight() + 4;
  547. const S32 maxTooltipLines = 20;
  548. S32 tooltipLineCount = 0;
  549. S32 tooltipLineWidth = 0;
  550. FrameTemp<StringBuffer> tooltipLines( maxTooltipLines );
  551. // Reset word indexing.
  552. S32 wordStartIndex = 0;
  553. S32 wordEndIndex = 0;
  554. // Search for end word.
  555. while( true )
  556. {
  557. // Do we have any words left?
  558. if ( wordEndIndex < wordCount )
  559. {
  560. // Yes, so fetch the word.
  561. const char* pWord = StringUnit::getUnit( renderTip, wordEndIndex, " " );
  562. // Add word length.
  563. const S32 wordLength = (S32)font->getStrWidth( pWord ) + spaceWidth;
  564. // Do we still have room?
  565. if ( (tooltipLineWidth + wordLength) < maxTooltipWidth )
  566. {
  567. // Yes, so add word length.
  568. tooltipLineWidth += wordLength;
  569. // Next word.
  570. wordEndIndex++;
  571. continue;
  572. }
  573. // Do we have any lines left?
  574. if ( tooltipLineCount < maxTooltipLines )
  575. {
  576. // Yes, so insert line.
  577. tooltipLines[tooltipLineCount++] = StringUnit::getUnits( renderTip, wordStartIndex, wordEndIndex-1, " " );
  578. // Update horizontal text bounds.
  579. if ( tooltipLineWidth > textBounds.x )
  580. textBounds.x = tooltipLineWidth;
  581. }
  582. // Set new line length.
  583. tooltipLineWidth = wordLength;
  584. // Set word start.
  585. wordStartIndex = wordEndIndex;
  586. // Next word.
  587. wordEndIndex++;
  588. continue;
  589. }
  590. // Do we have any words left?
  591. if ( wordStartIndex < wordCount )
  592. {
  593. // Yes, so do we have any lines left?
  594. if ( tooltipLineCount < maxTooltipLines )
  595. {
  596. // Yes, so insert line.
  597. tooltipLines[tooltipLineCount++] = StringUnit::getUnits( renderTip, wordStartIndex, wordCount-1, " " );
  598. // Update horizontal text bounds.
  599. if ( tooltipLineWidth > textBounds.x )
  600. textBounds.x = tooltipLineWidth;
  601. }
  602. }
  603. break;
  604. }
  605. // Controls the size of the inside (gutter) tooltip region.
  606. const S32 tooltipGutterSize = 5;
  607. // Adjust text bounds.
  608. textBounds.x += tooltipGutterSize * 2;
  609. textBounds.y = (((S32)font->getHeight() + 4) * tooltipLineCount - 4) + (tooltipGutterSize * 2);
  610. // Adjust to tooltip is always on-screen.
  611. Point2I screensize = Platform::getWindowSize();
  612. Point2I offset = cursorPos;
  613. offset.y += 22;
  614. if (screensize.x < (offset.x + textBounds.x))
  615. offset.x = screensize.x - textBounds.x;
  616. if(screensize.y < (offset.y + textBounds.y) )
  617. offset.y = screensize.y - textBounds.y;
  618. // Fetch the old clip.
  619. RectI oldClip = dglGetClipRect();
  620. // Set rectangle for the box, and set the clip rectangle.
  621. RectI rect(offset, textBounds);
  622. dglSetClipRect(rect);
  623. // Draw body and border of the tool tip
  624. renderUniversalRect(rect, mTooltipProfile, NormalState);
  625. // Draw the text centered in the tool tip box
  626. dglSetBitmapModulation( mTooltipProfile->mFontColor );
  627. Point2I start( tooltipGutterSize, tooltipGutterSize );
  628. for ( S32 lineIndex = 0; lineIndex < tooltipLineCount; lineIndex++ )
  629. {
  630. dglDrawText( font, start + offset, tooltipLines[lineIndex].getPtr8(), mProfile->mFontColors );
  631. offset.y += tooltipLineStride;
  632. }
  633. dglSetClipRect( oldClip );
  634. #endif
  635. return true;
  636. }
  637. void GuiControl::renderChildControls(Point2I offset, RectI content, const RectI &updateRect)
  638. {
  639. // 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.
  640. // updateRect is the area that this control was allowed to draw in. It should almost always be the same as the value in onRender.
  641. // content is the area that child controls are allowed to draw in.
  642. RectI clipRect = content;
  643. if(clipRect.intersect(dglGetClipRect()))
  644. {
  645. S32 size = objectList.size();
  646. S32 size_cpy = size;
  647. //-Mat look through our vector all normal-like, trying to use an iterator sometimes gives us
  648. //bad cast on good objects
  649. for( S32 count = 0; count < objectList.size(); count++ )
  650. {
  651. GuiControl *ctrl = (GuiControl *)objectList[count];
  652. if( ctrl == NULL ) {
  653. Con::errorf( "GuiControl::renderChildControls() object %i is NULL", count );
  654. continue;
  655. }
  656. if (ctrl->mVisible)
  657. {
  658. ctrl->mRenderInsetLT = content.point - offset;
  659. ctrl->mRenderInsetRB = mBounds.extent - (ctrl->mRenderInsetLT + content.extent);
  660. Point2I childPosition = content.point + ctrl->getPosition();
  661. RectI childClip(childPosition, ctrl->getExtent());
  662. if (childClip.intersect(clipRect))
  663. {
  664. RectI old = dglGetClipRect();
  665. dglSetClipRect(clipRect);
  666. glDisable(GL_CULL_FACE);
  667. ctrl->onRender(childPosition, RectI(childPosition, ctrl->getExtent()));
  668. dglSetClipRect(old);
  669. }
  670. }
  671. size_cpy = objectList.size(); // CHRIS: i know its wierd but the size of the list changes sometimes during execution of this loop
  672. if(size != size_cpy)
  673. {
  674. size = size_cpy;
  675. count--; // CHRIS: just to make sure one wasnt skipped.
  676. }
  677. }
  678. }
  679. }
  680. void GuiControl::setUpdateRegion(Point2I pos, Point2I ext)
  681. {
  682. Point2I upos = localToGlobalCoord(pos);
  683. GuiCanvas *root = getRoot();
  684. if (root)
  685. {
  686. root->addUpdateRegion(upos, ext);
  687. }
  688. }
  689. void GuiControl::setUpdate()
  690. {
  691. setUpdateRegion(Point2I(0,0), mBounds.extent);
  692. }
  693. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //
  694. void GuiControl::awaken()
  695. {
  696. AssertFatal(!mAwake, "GuiControl::awaken: control is already awake");
  697. if(mAwake)
  698. return;
  699. iterator i;
  700. for(i = begin(); i != end(); i++)
  701. {
  702. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  703. AssertFatal(!ctrl->isAwake(), "GuiControl::awaken: child control is already awake");
  704. if(!ctrl->isAwake())
  705. ctrl->awaken();
  706. }
  707. AssertFatal(!mAwake, "GuiControl::awaken: should not be awake here");
  708. if(!mAwake)
  709. {
  710. if(!onWake())
  711. {
  712. Con::errorf(ConsoleLogEntry::General, "GuiControl::awaken: failed onWake for obj: %s", getName());
  713. AssertFatal(0, "GuiControl::awaken: failed onWake");
  714. deleteObject();
  715. }
  716. else
  717. {
  718. if (mTextID && *mTextID != 0)
  719. setTextID(mTextID);
  720. }
  721. }
  722. }
  723. void GuiControl::sleep()
  724. {
  725. AssertFatal(mAwake, "GuiControl::sleep: control is not awake");
  726. if(!mAwake)
  727. return;
  728. iterator i;
  729. for(i = begin(); i != end(); i++)
  730. {
  731. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  732. AssertFatal(ctrl->isAwake(), "GuiControl::sleep: child control is already asleep");
  733. if(ctrl->isAwake())
  734. ctrl->sleep();
  735. }
  736. AssertFatal(mAwake, "GuiControl::sleep: should not be asleep here");
  737. if(mAwake)
  738. onSleep();
  739. }
  740. void GuiControl::preRender()
  741. {
  742. AssertFatal(mAwake, "GuiControl::preRender: control is not awake");
  743. if(!mAwake)
  744. return;
  745. iterator i;
  746. for(i = begin(); i != end(); i++)
  747. {
  748. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  749. ctrl->preRender();
  750. }
  751. onPreRender();
  752. }
  753. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //
  754. bool GuiControl::onWake()
  755. {
  756. AssertFatal( !mAwake, "GuiControl::onWake: control is already awake" );
  757. if( mAwake )
  758. return false;
  759. // [tom, 4/18/2005] Cause mLangTable to be refreshed in case it was changed
  760. mLangTable = NULL;
  761. // Grab the classname of this object
  762. const char *cName = getClassName();
  763. //make sure we have a profile
  764. if( !mProfile )
  765. {
  766. // Ensure the classname is a valid name...
  767. if( cName && cName[0] )
  768. {
  769. S32 pos = 0;
  770. for( pos = 0; pos <= (S32)dStrlen( cName ); pos++ )
  771. if( !dStrncmp( cName + pos, "Ctrl", 4 ) )
  772. break;
  773. if( pos != 0 ) {
  774. char buff[255];
  775. dStrncpy( buff, cName, pos );
  776. buff[pos] = '\0';
  777. dStrcat( buff, "Profile\0" );
  778. SimObject *obj = Sim::findObject( buff );
  779. if( obj )
  780. mProfile = dynamic_cast<GuiControlProfile*>( obj );
  781. }
  782. }
  783. // Ok lets check to see if that worked
  784. if( !mProfile ) {
  785. SimObject *obj = Sim::findObject( "GuiDefaultProfile" );
  786. if( obj )
  787. mProfile = dynamic_cast<GuiControlProfile*>(obj);
  788. }
  789. AssertFatal( mProfile, avar( "GuiControl: %s created with no profile.", getName() ) );
  790. }
  791. //set the flag
  792. mAwake = true;
  793. //set the layer
  794. GuiCanvas *root = getRoot();
  795. AssertFatal(root, "Unable to get the root Canvas.");
  796. GuiControl *parent = getParent();
  797. if (parent && parent != root)
  798. mLayer = parent->mLayer;
  799. //make sure the first responder exists
  800. if (! mFirstResponder)
  801. mFirstResponder = findFirstTabable();
  802. //see if we should force this control to be the first responder
  803. //if (mProfile->mTabable && mProfile->mCanKeyFocus)
  804. // setFirstResponder();
  805. //increment the profile
  806. mProfile->incRefCount();
  807. // Only invoke script callbacks if we have a namespace in which to do so
  808. // This will suppress warnings
  809. if( isMethod("onWake") )
  810. Con::executef(this, 1, "onWake");
  811. if (mTooltipProfile != NULL)
  812. mTooltipProfile->incRefCount();
  813. return true;
  814. }
  815. void GuiControl::onSleep()
  816. {
  817. AssertFatal(mAwake, "GuiControl::onSleep: control is not awake");
  818. if(!mAwake)
  819. return;
  820. //decrement the profile referrence
  821. if( mProfile != NULL )
  822. mProfile->decRefCount();
  823. clearFirstResponder();
  824. mouseUnlock();
  825. // Only invoke script callbacks if we have a namespace in which to do so
  826. // This will suppress warnings
  827. if( isMethod("onSleep") )
  828. Con::executef(this, 1, "onSleep");
  829. if (mTooltipProfile != NULL)
  830. mTooltipProfile->decRefCount();
  831. // Set Flag
  832. mAwake = false;
  833. }
  834. void GuiControl::setControlProfile(GuiControlProfile *prof)
  835. {
  836. AssertFatal(prof, "GuiControl::setControlProfile: invalid profile");
  837. if(prof == mProfile)
  838. return;
  839. if(mAwake)
  840. mProfile->decRefCount();
  841. mProfile = prof;
  842. if(mAwake)
  843. mProfile->incRefCount();
  844. }
  845. void GuiControl::onPreRender()
  846. {
  847. // do nothing.
  848. }
  849. //-----------------------------------------------------------------------------
  850. // checks up the parent hierarchy - if anyone above us is not savable returns false
  851. // otherwise, returns true.
  852. //-----------------------------------------------------------------------------
  853. bool GuiControl::getCanSaveParent()
  854. {
  855. GuiControl *walk = this;
  856. while(walk)
  857. {
  858. if(!walk->getCanSave())
  859. return false;
  860. walk = walk->getParent();
  861. }
  862. return true;
  863. }
  864. //-----------------------------------------------------------------------------
  865. // Can we Save to a TorqueScript file?
  866. //-----------------------------------------------------------------------------
  867. bool GuiControl::getCanSave()
  868. {
  869. return mCanSave;
  870. }
  871. //-----------------------------------------------------------------------------
  872. // Sets whether we can save out to a file (TorqueScript)
  873. //-----------------------------------------------------------------------------
  874. void GuiControl::setCanSave(bool bCanSave)
  875. {
  876. mCanSave = bCanSave;
  877. }
  878. ////////////////////////////////////////////////////////////////////////////////////////////////////
  879. // checks out mCanSave flag, if true just passes along to our parent,
  880. // if false, then we return without writing. Note, also, that
  881. // if our parent is not writeable, then we should not be writable...
  882. ////////////////////////////////////////////////////////////////////////////////////////////////////
  883. void GuiControl::write(Stream &stream, U32 tabStop, U32 flags)
  884. {
  885. //note: this will return false if either we, or any of our parents, are non-save controls
  886. bool bCanSave = getCanSaveParent();
  887. if(bCanSave)
  888. {
  889. Parent::write(stream, tabStop, flags);
  890. }
  891. }
  892. //This is only called if the control is deleted, not when the control is removed from its parent.
  893. void GuiControl::onRemove()
  894. {
  895. Parent::onRemove();
  896. }
  897. //For GuiControls, this will always just before it is actually removed.
  898. void GuiControl::onGroupRemove()
  899. {
  900. clearFirstResponder();
  901. }
  902. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //
  903. const char *GuiControl::getScriptValue()
  904. {
  905. return NULL;
  906. }
  907. void GuiControl::setScriptValue(const char *value)
  908. {
  909. }
  910. void GuiControl::setConsoleVariable(const char *variable)
  911. {
  912. if (variable)
  913. {
  914. mConsoleVariable = StringTable->insert(variable);
  915. }
  916. else
  917. {
  918. mConsoleVariable = StringTable->EmptyString;
  919. }
  920. }
  921. //-----------------------------------------------------------------------------
  922. // finds and returns the first immediate child of ours whose
  923. // internal name matches the passed String. returns Null if not found.
  924. //-----------------------------------------------------------------------------
  925. void GuiControl::setConsoleCommand(const char *newCmd)
  926. {
  927. if (newCmd)
  928. mConsoleCommand = StringTable->insert(newCmd);
  929. else
  930. mConsoleCommand = StringTable->EmptyString;
  931. }
  932. const char * GuiControl::getConsoleCommand()
  933. {
  934. return mConsoleCommand;
  935. }
  936. void GuiControl::setSizing(S32 horz, S32 vert)
  937. {
  938. mHorizSizing = horz;
  939. mVertSizing = vert;
  940. }
  941. void GuiControl::setVariable(const char *value)
  942. {
  943. if (mConsoleVariable[0])
  944. Con::setVariable(mConsoleVariable, value);
  945. }
  946. void GuiControl::setIntVariable(S32 value)
  947. {
  948. if (mConsoleVariable[0])
  949. Con::setIntVariable(mConsoleVariable, value);
  950. }
  951. void GuiControl::setFloatVariable(F32 value)
  952. {
  953. if (mConsoleVariable[0])
  954. Con::setFloatVariable(mConsoleVariable, value);
  955. }
  956. const char * GuiControl::getVariable()
  957. {
  958. if (mConsoleVariable[0])
  959. return Con::getVariable(mConsoleVariable);
  960. else return NULL;
  961. }
  962. S32 GuiControl::getIntVariable()
  963. {
  964. if (mConsoleVariable[0])
  965. return Con::getIntVariable(mConsoleVariable);
  966. else return 0;
  967. }
  968. F32 GuiControl::getFloatVariable()
  969. {
  970. if (mConsoleVariable[0])
  971. return Con::getFloatVariable(mConsoleVariable);
  972. else return 0.0f;
  973. }
  974. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //
  975. bool GuiControl::cursorInControl()
  976. {
  977. GuiCanvas *root = getRoot();
  978. if (! root) return false;
  979. Point2I pt = root->getCursorPos();
  980. Point2I offset = localToGlobalCoord(Point2I(0, 0));
  981. if (pt.x >= offset.x && pt.y >= offset.y &&
  982. pt.x < offset.x + mBounds.extent.x && pt.y < offset.y + mBounds.extent.y)
  983. {
  984. return true;
  985. }
  986. else
  987. {
  988. return false;
  989. }
  990. }
  991. bool GuiControl::pointInControl(const Point2I& parentCoordPoint)
  992. {
  993. S32 xt = parentCoordPoint.x - mBounds.point.x;
  994. S32 yt = parentCoordPoint.y - mBounds.point.y;
  995. return xt >= 0 && yt >= 0 && xt < mBounds.extent.x && yt < mBounds.extent.y;
  996. }
  997. GuiControl* GuiControl::findHitControl(const Point2I &pt, S32 initialLayer)
  998. {
  999. iterator i = end(); // find in z order (last to first)
  1000. while (i != begin())
  1001. {
  1002. i--;
  1003. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  1004. if (initialLayer >= 0 && ctrl->mLayer > initialLayer)
  1005. {
  1006. continue;
  1007. }
  1008. else if (ctrl->mVisible && ctrl->pointInControl(pt - ctrl->mRenderInsetLT))
  1009. {
  1010. Point2I ptemp = pt - (ctrl->mBounds.point + ctrl->mRenderInsetLT);
  1011. GuiControl *hitCtrl = ctrl->findHitControl(ptemp);
  1012. if(hitCtrl->mProfile->mUseInput)
  1013. return hitCtrl;
  1014. }
  1015. }
  1016. return this;
  1017. }
  1018. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //
  1019. bool GuiControl::isMouseLocked()
  1020. {
  1021. GuiCanvas *root = getRoot();
  1022. return root ? root->getMouseLockedControl() == this : false;
  1023. }
  1024. void GuiControl::mouseLock(GuiControl *lockingControl)
  1025. {
  1026. GuiCanvas *root = getRoot();
  1027. if (root)
  1028. root->mouseLock(lockingControl);
  1029. }
  1030. void GuiControl::mouseLock()
  1031. {
  1032. GuiCanvas *root = getRoot();
  1033. if (root)
  1034. root->mouseLock(this);
  1035. }
  1036. void GuiControl::mouseUnlock()
  1037. {
  1038. GuiCanvas *root = getRoot();
  1039. if (root)
  1040. root->mouseUnlock(this);
  1041. }
  1042. bool GuiControl::onInputEvent(const InputEvent &event)
  1043. {
  1044. // Do nothing by default...
  1045. return( false );
  1046. }
  1047. void GuiControl::onTouchUp(const GuiEvent &event)
  1048. {
  1049. }
  1050. void GuiControl::onTouchDown(const GuiEvent &event)
  1051. {
  1052. }
  1053. void GuiControl::onTouchMove(const GuiEvent &event)
  1054. {
  1055. //if this control is a dead end, make sure the event stops here
  1056. if ( !mVisible || !mAwake )
  1057. return;
  1058. //pass the event to the parent
  1059. GuiControl *parent = getParent();
  1060. if ( parent )
  1061. parent->onTouchMove( event );
  1062. }
  1063. void GuiControl::onTouchDragged(const GuiEvent &event)
  1064. {
  1065. }
  1066. void GuiControl::onTouchEnter(const GuiEvent &)
  1067. {
  1068. }
  1069. void GuiControl::onTouchLeave(const GuiEvent &)
  1070. {
  1071. }
  1072. bool GuiControl::onMouseWheelUp( const GuiEvent &event )
  1073. {
  1074. //if this control is a dead end, make sure the event stops here
  1075. if ( !mVisible || !mAwake )
  1076. return true;
  1077. //pass the event to the parent
  1078. GuiControl *parent = getParent();
  1079. if ( parent )
  1080. return parent->onMouseWheelUp( event );
  1081. else
  1082. return false;
  1083. }
  1084. bool GuiControl::onMouseWheelDown( const GuiEvent &event )
  1085. {
  1086. //if this control is a dead end, make sure the event stops here
  1087. if ( !mVisible || !mAwake )
  1088. return true;
  1089. //pass the event to the parent
  1090. GuiControl *parent = getParent();
  1091. if ( parent )
  1092. return parent->onMouseWheelDown( event );
  1093. else
  1094. return false;
  1095. }
  1096. void GuiControl::onRightMouseDown(const GuiEvent &)
  1097. {
  1098. }
  1099. void GuiControl::onRightMouseUp(const GuiEvent &)
  1100. {
  1101. }
  1102. void GuiControl::onRightMouseDragged(const GuiEvent &)
  1103. {
  1104. }
  1105. void GuiControl::onMiddleMouseDown(const GuiEvent &)
  1106. {
  1107. }
  1108. void GuiControl::onMiddleMouseUp(const GuiEvent &)
  1109. {
  1110. }
  1111. void GuiControl::onMiddleMouseDragged(const GuiEvent &)
  1112. {
  1113. }
  1114. GuiControl* GuiControl::findFirstTabable()
  1115. {
  1116. GuiControl *tabCtrl = NULL;
  1117. iterator i;
  1118. for (i = begin(); i != end(); i++)
  1119. {
  1120. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  1121. tabCtrl = ctrl->findFirstTabable();
  1122. if (tabCtrl)
  1123. {
  1124. mFirstResponder = tabCtrl;
  1125. return tabCtrl;
  1126. }
  1127. }
  1128. //nothing was found, therefore, see if this ctrl is tabable
  1129. return ( mProfile != NULL ) ? ( ( mProfile->mTabable && mAwake && mVisible ) ? this : NULL ) : NULL;
  1130. }
  1131. GuiControl* GuiControl::findLastTabable(bool firstCall)
  1132. {
  1133. //if this is the first call, clear the global
  1134. if (firstCall)
  1135. smPrevResponder = NULL;
  1136. //if this control is tabable, set the global
  1137. if (mProfile->mTabable)
  1138. smPrevResponder = this;
  1139. iterator i;
  1140. for (i = begin(); i != end(); i++)
  1141. {
  1142. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  1143. ctrl->findLastTabable(false);
  1144. }
  1145. //after the entire tree has been traversed, return the last responder found
  1146. mFirstResponder = smPrevResponder;
  1147. return smPrevResponder;
  1148. }
  1149. GuiControl *GuiControl::findNextTabable(GuiControl *curResponder, bool firstCall)
  1150. {
  1151. //if this is the first call, clear the global
  1152. if (firstCall)
  1153. smCurResponder = NULL;
  1154. //first find the current responder
  1155. if (curResponder == this)
  1156. smCurResponder = this;
  1157. //if the first responder has been found, return the very next *tabable* control
  1158. else if ( smCurResponder && mProfile->mTabable && mAwake && mVisible && mActive )
  1159. return( this );
  1160. //loop through, checking each child to see if it is the one that follows the firstResponder
  1161. GuiControl *tabCtrl = NULL;
  1162. iterator i;
  1163. for (i = begin(); i != end(); i++)
  1164. {
  1165. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  1166. tabCtrl = ctrl->findNextTabable(curResponder, false);
  1167. if (tabCtrl) break;
  1168. }
  1169. mFirstResponder = tabCtrl;
  1170. return tabCtrl;
  1171. }
  1172. GuiControl *GuiControl::findPrevTabable(GuiControl *curResponder, bool firstCall)
  1173. {
  1174. if (firstCall)
  1175. smPrevResponder = NULL;
  1176. //if this is the current reponder, return the previous one
  1177. if (curResponder == this)
  1178. return smPrevResponder;
  1179. //else if this is a responder, store it in case the next found is the current responder
  1180. else if ( mProfile->mTabable && mAwake && mVisible && mActive )
  1181. smPrevResponder = this;
  1182. //loop through, checking each child to see if it is the one that follows the firstResponder
  1183. GuiControl *tabCtrl = NULL;
  1184. iterator i;
  1185. for (i = begin(); i != end(); i++)
  1186. {
  1187. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  1188. tabCtrl = ctrl->findPrevTabable(curResponder, false);
  1189. if (tabCtrl) break;
  1190. }
  1191. mFirstResponder = tabCtrl;
  1192. return tabCtrl;
  1193. }
  1194. void GuiControl::onLoseFirstResponder()
  1195. {
  1196. // Since many controls have visual cues when they are the firstResponder...
  1197. setUpdate();
  1198. }
  1199. bool GuiControl::ControlIsChild(GuiControl *child)
  1200. {
  1201. //function returns true if this control, or one of it's children is the child control
  1202. if (child == this)
  1203. return true;
  1204. //loop through, checking each child to see if it is ,or contains, the firstResponder
  1205. iterator i;
  1206. for (i = begin(); i != end(); i++)
  1207. {
  1208. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  1209. if (ctrl->ControlIsChild(child)) return true;
  1210. }
  1211. //not found, therefore false
  1212. return false;
  1213. }
  1214. void GuiControl::onFocus()
  1215. {
  1216. //bubble the focus up
  1217. GuiControl *parent = getParent();
  1218. if (parent)
  1219. parent->onFocus();
  1220. }
  1221. bool GuiControl::isFirstResponder()
  1222. {
  1223. GuiCanvas *root = getRoot();
  1224. return root && root->getFirstResponder() == this;
  1225. }
  1226. void GuiControl::setFirstResponder( GuiControl* firstResponder )
  1227. {
  1228. if ( firstResponder && firstResponder->mProfile->mCanKeyFocus )
  1229. mFirstResponder = firstResponder;
  1230. GuiControl *parent = getParent();
  1231. if ( parent )
  1232. parent->setFirstResponder( firstResponder );
  1233. }
  1234. void GuiControl::setFirstResponder()
  1235. {
  1236. if ( mAwake && mVisible )
  1237. {
  1238. GuiControl *parent = getParent();
  1239. if (mProfile->mCanKeyFocus == true && parent != NULL )
  1240. {
  1241. parent->setFirstResponder(this);
  1242. // Since many controls have visual cues when they are the firstResponder...
  1243. this->setUpdate();
  1244. }
  1245. }
  1246. }
  1247. void GuiControl::clearFirstResponder()
  1248. {
  1249. GuiControl *parent = this;
  1250. while((parent = parent->getParent()) != NULL)
  1251. {
  1252. if(parent->mFirstResponder == this)
  1253. parent->mFirstResponder = NULL;
  1254. else
  1255. break;
  1256. }
  1257. }
  1258. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //
  1259. void GuiControl::buildAcceleratorMap()
  1260. {
  1261. //add my own accel key
  1262. addAcceleratorKey();
  1263. //add all my childrens keys
  1264. iterator i;
  1265. for(i = begin(); i != end(); i++)
  1266. {
  1267. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  1268. ctrl->buildAcceleratorMap();
  1269. }
  1270. }
  1271. void GuiControl::addAcceleratorKey()
  1272. {
  1273. //see if we have an accelerator
  1274. if (mAcceleratorKey == StringTable->EmptyString)
  1275. return;
  1276. EventDescriptor accelEvent;
  1277. ActionMap::createEventDescriptor(mAcceleratorKey, &accelEvent);
  1278. //now we have a modifier, and a key, add them to the canvas
  1279. GuiCanvas *root = getRoot();
  1280. if (root)
  1281. root->addAcceleratorKey(this, 0, accelEvent.eventCode, accelEvent.flags);
  1282. }
  1283. void GuiControl::acceleratorKeyPress(U32 index)
  1284. {
  1285. onAction();
  1286. }
  1287. void GuiControl::acceleratorKeyRelease(U32 index)
  1288. {
  1289. //do nothing
  1290. }
  1291. bool GuiControl::onKeyDown(const GuiEvent &event)
  1292. {
  1293. //pass the event to the parent
  1294. GuiControl *parent = getParent();
  1295. if (parent)
  1296. return parent->onKeyDown(event);
  1297. else
  1298. return false;
  1299. }
  1300. bool GuiControl::onKeyRepeat(const GuiEvent &event)
  1301. {
  1302. // default to just another key down.
  1303. return onKeyDown(event);
  1304. }
  1305. bool GuiControl::onKeyUp(const GuiEvent &event)
  1306. {
  1307. //pass the event to the parent
  1308. GuiControl *parent = getParent();
  1309. if (parent)
  1310. return parent->onKeyUp(event);
  1311. else
  1312. return false;
  1313. }
  1314. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //
  1315. void GuiControl::onAction()
  1316. {
  1317. if (! mActive)
  1318. return;
  1319. //execute the console command
  1320. if (mConsoleCommand && mConsoleCommand[0])
  1321. {
  1322. execConsoleCallback();
  1323. }
  1324. else
  1325. Con::executef(this, 1, "onAction");
  1326. }
  1327. void GuiControl::onMessage(GuiControl *sender, S32 msg)
  1328. {
  1329. }
  1330. void GuiControl::messageSiblings(S32 message)
  1331. {
  1332. GuiControl *parent = getParent();
  1333. if (! parent) return;
  1334. GuiControl::iterator i;
  1335. for(i = parent->begin(); i != parent->end(); i++)
  1336. {
  1337. GuiControl *ctrl = dynamic_cast<GuiControl *>(*i);
  1338. if (ctrl != this)
  1339. ctrl->onMessage(this, message);
  1340. }
  1341. }
  1342. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //
  1343. void GuiControl::onDialogPush()
  1344. {
  1345. // Notify Script.
  1346. if( isMethod("onDialogPush") )
  1347. Con::executef(this, 1, "onDialogPush");
  1348. }
  1349. void GuiControl::onDialogPop()
  1350. {
  1351. // Notify Script.
  1352. if( isMethod("onDialogPop") )
  1353. Con::executef(this, 1, "onDialogPop");
  1354. }
  1355. //------------------------------------------------------------------------------
  1356. void GuiControl::setVisible(bool value)
  1357. {
  1358. mVisible = value;
  1359. iterator i;
  1360. setUpdate();
  1361. for(i = begin(); i != end(); i++)
  1362. {
  1363. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  1364. ctrl->clearFirstResponder();
  1365. }
  1366. GuiControl *parent = getParent();
  1367. if (parent)
  1368. parent->childResized(this);
  1369. }
  1370. void GuiControl::makeFirstResponder(bool value)
  1371. {
  1372. if ( value )
  1373. //setFirstResponder(this);
  1374. setFirstResponder();
  1375. else
  1376. clearFirstResponder();
  1377. }
  1378. void GuiControl::setActive( bool value )
  1379. {
  1380. mActive = value;
  1381. if (value && isMethod("onActive"))
  1382. Con::executef(this, 1, "onActive");
  1383. else if (!value && isMethod("onInactive"))
  1384. Con::executef(this, 1, "onInactive");
  1385. if ( !mActive )
  1386. clearFirstResponder();
  1387. if ( mVisible && mAwake )
  1388. setUpdate();
  1389. }
  1390. void GuiControl::getScrollLineSizes(U32 *rowHeight, U32 *columnWidth)
  1391. {
  1392. // default to 10 pixels in y, 30 pixels in x
  1393. *columnWidth = 30;
  1394. *rowHeight = 30;
  1395. }
  1396. void GuiControl::renderText(const Point2I& offset, const Point2I& extent, const char* text, GuiControlProfile* profile, TextRotationOptions rot)
  1397. {
  1398. RectI old = dglGetClipRect();
  1399. RectI clipRect = RectI(offset, extent);
  1400. if (clipRect.intersect(old))
  1401. {
  1402. dglSetClipRect(clipRect);
  1403. const S32 textHeight = profile->mFont->getHeight();
  1404. S32 totalWidth = (rot == tRotateNone) ? extent.x : extent.y;
  1405. S32 totalHeight = (rot == tRotateNone) ? extent.y : extent.x;
  1406. S32 startOffsetY = 0;
  1407. vector<string> lineList = getLineList(text, profile, totalWidth);
  1408. //first align vertical
  1409. S32 blockHeight = textHeight * lineList.size();
  1410. if (blockHeight < totalHeight)
  1411. {
  1412. startOffsetY = getTextVerticalOffset(blockHeight, totalHeight, profile->mVAlignment);
  1413. }
  1414. else if (!mTextWrap)
  1415. {
  1416. startOffsetY = getTextVerticalOffset(blockHeight, totalHeight, GuiControlProfile::VertAlignmentType::MiddleVAlign);
  1417. }
  1418. else
  1419. {
  1420. startOffsetY = getTextVerticalOffset(blockHeight, totalHeight, GuiControlProfile::VertAlignmentType::TopVAlign);
  1421. }
  1422. renderLineList(offset, extent, startOffsetY, lineList, profile, rot);
  1423. dglSetClipRect(old);
  1424. }
  1425. }
  1426. void GuiControl::renderLineList(const Point2I& offset, const Point2I& extent, const S32 startOffsetY, const vector<string> lineList, GuiControlProfile* profile, const TextRotationOptions rot)
  1427. {
  1428. const S32 textHeight = profile->mFont->getHeight();
  1429. S32 totalWidth = (rot == tRotateNone) ? extent.x : extent.y;
  1430. //Now print each line
  1431. U32 ibeamPos = 0;
  1432. U32 lineNumber = 0;
  1433. S32 offsetX = 0;
  1434. S32 offsetY = startOffsetY;
  1435. for(string line : lineList)
  1436. {
  1437. // align the horizontal
  1438. string trimmedLine = Utility::trim_copy(line);
  1439. U32 textWidth = profile->mFont->getStrWidth(trimmedLine.c_str());
  1440. if(textWidth < totalWidth)
  1441. {
  1442. offsetX = getTextHorizontalOffset(textWidth, totalWidth, profile->mAlignment);
  1443. }
  1444. Point2I start = Point2I(0, 0);
  1445. F32 rotation;
  1446. if (rot == tRotateNone)
  1447. {
  1448. start.x += offsetX;
  1449. start.y += offsetY;
  1450. rotation = 0.0f;
  1451. }
  1452. else if (rot == tRotateLeft)
  1453. {
  1454. start.x = offsetY;
  1455. start.y = extent.y + offsetX;
  1456. rotation = 90.0f;
  1457. }
  1458. else if (rot == tRotateRight)
  1459. {
  1460. start.x = extent.x - offsetY;
  1461. start.y = offsetX;
  1462. rotation = -90.0f;
  1463. }
  1464. renderTextLine(start + offset + profile->mTextOffset, trimmedLine, profile, rotation, ibeamPos, lineNumber);
  1465. offsetY += textHeight;
  1466. ibeamPos += line.length();
  1467. lineNumber++;
  1468. }
  1469. }
  1470. vector<string> GuiControl::getLineList(const char* text, GuiControlProfile* profile, S32 totalWidth)
  1471. {
  1472. GFont* font = profile->mFont;
  1473. vector<string> lineList = vector<string>();
  1474. if (!mTextWrap)
  1475. {
  1476. lineList.push_back(text);
  1477. }
  1478. else
  1479. {
  1480. vector<string> paragraphList = vector<string>();
  1481. istringstream f(text);
  1482. string s;
  1483. while (getline(f, s)) {
  1484. paragraphList.push_back(s);
  1485. }
  1486. for (string& paragraph : paragraphList)
  1487. {
  1488. vector<string> wordList = vector<string>();
  1489. istringstream f2(paragraph);
  1490. string s2;
  1491. while (getline(f2, s2, ' ')) {
  1492. wordList.push_back(s2);
  1493. }
  1494. //now process the word list
  1495. string line;
  1496. bool newLine = true;
  1497. line.clear();
  1498. for (string& word : wordList)
  1499. {
  1500. if (font->getStrWidth(word.c_str()) >= totalWidth)
  1501. {
  1502. if (line.size() > 0)
  1503. {
  1504. lineList.push_back(string(line + " "));
  1505. line.clear();
  1506. }
  1507. lineList.push_back(word + " ");
  1508. newLine = true;
  1509. continue;
  1510. }
  1511. string prevLine = string(line);
  1512. line += (!newLine) ? " " + word : word;
  1513. newLine = false;
  1514. if (font->getStrWidth(line.c_str()) >= totalWidth && word.length() != 0)
  1515. {
  1516. lineList.push_back(prevLine + " ");
  1517. line = word;
  1518. }
  1519. }
  1520. if (paragraph.back() == ' ')
  1521. {
  1522. line += " ";
  1523. }
  1524. lineList.push_back(string(line));
  1525. }
  1526. }
  1527. return lineList;
  1528. }
  1529. void GuiControl::renderTextLine(const Point2I& startPoint, const string line, GuiControlProfile* profile, F32 rotationInDegrees, U32, U32)
  1530. {
  1531. dglDrawText(profile->mFont, startPoint, line.c_str(), profile->mFontColors, 9, rotationInDegrees);
  1532. }
  1533. S32 GuiControl::getTextHorizontalOffset(S32 textWidth, S32 totalWidth, GuiControlProfile::AlignmentType align)
  1534. {
  1535. if (align == GuiControlProfile::RightAlign)
  1536. {
  1537. return totalWidth - textWidth;
  1538. }
  1539. else if (align == GuiControlProfile::CenterAlign)
  1540. {
  1541. return (totalWidth - textWidth) / 2;
  1542. }
  1543. return 0;//left aligned
  1544. }
  1545. S32 GuiControl::getTextVerticalOffset(S32 textHeight, S32 totalHeight, GuiControlProfile::VertAlignmentType align)
  1546. {
  1547. if (align == GuiControlProfile::MiddleVAlign)
  1548. {
  1549. return (totalHeight - textHeight) / 2;
  1550. }
  1551. else if (align == GuiControlProfile::BottomVAlign)
  1552. {
  1553. return totalHeight - textHeight;
  1554. }
  1555. return 0;
  1556. }
  1557. void GuiControl::getCursor(GuiCursor *&cursor, bool &showCursor, const GuiEvent &lastGuiEvent)
  1558. {
  1559. lastGuiEvent;
  1560. if(GuiControl::smCursorChanged != -1 && !isMouseLocked())
  1561. {
  1562. // We've already changed the cursor,
  1563. // so set it back before we change it again.
  1564. Input::popCursor();
  1565. // We haven't changed it
  1566. GuiControl::smCursorChanged = -1;
  1567. }
  1568. }
  1569. const char* GuiControl::execConsoleCallback()
  1570. {
  1571. if (mConsoleCommand && mConsoleCommand[0])
  1572. {
  1573. Con::setVariable("$ThisControl", avar("%d",getId()));
  1574. return Con::evaluate(mConsoleCommand, false);
  1575. }
  1576. return "";
  1577. }
  1578. const char* GuiControl::execAltConsoleCallback()
  1579. {
  1580. if(mAltConsoleCommand && mAltConsoleCommand[0])
  1581. {
  1582. Con::setVariable("$ThisControl", avar("%d",getId()));
  1583. return Con::evaluate(mAltConsoleCommand, false);
  1584. }
  1585. return "";
  1586. }
  1587. void GuiControl::setText(const char *text)
  1588. {
  1589. mText = StringTable->insert(text, true);
  1590. }
  1591. void GuiControl::setTextID(const char *id)
  1592. {
  1593. S32 n = Con::getIntVariable(id, -1);
  1594. if (n != -1)
  1595. {
  1596. mTextID = StringTable->insert(id);
  1597. setTextID(n);
  1598. }
  1599. }
  1600. void GuiControl::setTextID(S32 id)
  1601. {
  1602. const UTF8 *str = getGUIString(id);
  1603. if (str)
  1604. setText((const char*)str);
  1605. }
  1606. const char *GuiControl::getText()
  1607. {
  1608. return mText;
  1609. }