guiControl.cc 57 KB

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