guiControl.cc 50 KB

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