guiControl.cc 52 KB

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