guiControl.cc 68 KB

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