guiControl.cc 62 KB

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