guiControl.cc 77 KB

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