guiControl.cc 66 KB

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