guiControl.cc 43 KB

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