guiControl.cc 50 KB

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