guiControl.cc 68 KB

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