guiControl.cc 54 KB

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