guiControl.cc 66 KB

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