guiControl.cc 50 KB

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