guiControl.cc 76 KB

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