guiControl.cc 43 KB

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