guiControl.cc 59 KB

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