guiControl.cc 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755
  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. setField("TooltipProfile", "GuiTooltipProfile");
  477. GFont *font = mTooltipProfile->mFont;
  478. // Set text bounds.
  479. Point2I textBounds( 0, 0 );
  480. // Fetch the width of a space.
  481. const S32 spaceWidth = (S32)font->getStrWidth(" ");
  482. // Fetch the maximum allowed tooltip extent.
  483. const S32 maxTooltipWidth = mTooltipWidth;
  484. // Fetch word count.
  485. const S32 wordCount = StringUnit::getUnitCount( renderTip, " " );
  486. // Reset line storage.
  487. const S32 tooltipLineStride = (S32)font->getHeight() + 4;
  488. const S32 maxTooltipLines = 20;
  489. S32 tooltipLineCount = 0;
  490. S32 tooltipLineWidth = 0;
  491. FrameTemp<StringBuffer> tooltipLines( maxTooltipLines );
  492. // Reset word indexing.
  493. S32 wordStartIndex = 0;
  494. S32 wordEndIndex = 0;
  495. // Search for end word.
  496. while( true )
  497. {
  498. // Do we have any words left?
  499. if ( wordEndIndex < wordCount )
  500. {
  501. // Yes, so fetch the word.
  502. const char* pWord = StringUnit::getUnit( renderTip, wordEndIndex, " " );
  503. // Add word length.
  504. const S32 wordLength = (S32)font->getStrWidth( pWord ) + spaceWidth;
  505. // Do we still have room?
  506. if ( (tooltipLineWidth + wordLength) < maxTooltipWidth )
  507. {
  508. // Yes, so add word length.
  509. tooltipLineWidth += wordLength;
  510. // Next word.
  511. wordEndIndex++;
  512. continue;
  513. }
  514. // Do we have any lines left?
  515. if ( tooltipLineCount < maxTooltipLines )
  516. {
  517. // Yes, so insert line.
  518. tooltipLines[tooltipLineCount++] = StringUnit::getUnits( renderTip, wordStartIndex, wordEndIndex-1, " " );
  519. // Update horizontal text bounds.
  520. if ( tooltipLineWidth > textBounds.x )
  521. textBounds.x = tooltipLineWidth;
  522. }
  523. // Set new line length.
  524. tooltipLineWidth = wordLength;
  525. // Set word start.
  526. wordStartIndex = wordEndIndex;
  527. // Next word.
  528. wordEndIndex++;
  529. continue;
  530. }
  531. // Do we have any words left?
  532. if ( wordStartIndex < wordCount )
  533. {
  534. // Yes, so do we have any lines left?
  535. if ( tooltipLineCount < maxTooltipLines )
  536. {
  537. // Yes, so insert line.
  538. tooltipLines[tooltipLineCount++] = StringUnit::getUnits( renderTip, wordStartIndex, wordCount-1, " " );
  539. // Update horizontal text bounds.
  540. if ( tooltipLineWidth > textBounds.x )
  541. textBounds.x = tooltipLineWidth;
  542. }
  543. }
  544. break;
  545. }
  546. // Controls the size of the inside (gutter) tooltip region.
  547. const S32 tooltipGutterSize = 5;
  548. // Adjust text bounds.
  549. textBounds.x += tooltipGutterSize * 2;
  550. textBounds.y = (((S32)font->getHeight() + 4) * tooltipLineCount - 4) + (tooltipGutterSize * 2);
  551. // Adjust to tooltip is always on-screen.
  552. Point2I screensize = Platform::getWindowSize();
  553. Point2I offset = cursorPos;
  554. offset.y += 22;
  555. if (screensize.x < (offset.x + textBounds.x))
  556. offset.x = screensize.x - textBounds.x;
  557. if(screensize.y < (offset.y + textBounds.y) )
  558. offset.y = screensize.y - textBounds.y;
  559. // Fetch the old clip.
  560. RectI oldClip = dglGetClipRect();
  561. // Set rectangle for the box, and set the clip rectangle.
  562. RectI rect(offset, textBounds);
  563. dglSetClipRect(rect);
  564. // Draw body and border of the tool tip
  565. renderUniversalRect(rect, mTooltipProfile, NormalState);
  566. // Draw the text centered in the tool tip box
  567. dglSetBitmapModulation( mTooltipProfile->mFontColor );
  568. Point2I start( tooltipGutterSize, tooltipGutterSize );
  569. for ( S32 lineIndex = 0; lineIndex < tooltipLineCount; lineIndex++ )
  570. {
  571. dglDrawText( font, start + offset, tooltipLines[lineIndex].getPtr8(), mProfile->mFontColors );
  572. offset.y += tooltipLineStride;
  573. }
  574. dglSetClipRect( oldClip );
  575. #endif
  576. return true;
  577. }
  578. void GuiControl::renderChildControls(Point2I offset, RectI content, const RectI &updateRect)
  579. {
  580. // offset is the upper-left corner of this control in screen coordinates. It should almost always be the same offset passed into the onRender method.
  581. // updateRect is the area that this control was allowed to draw in. It should almost always be the same as the value in onRender.
  582. // content is the area that child controls are allowed to draw in.
  583. RectI clipRect = content;
  584. if(clipRect.intersect(dglGetClipRect()))
  585. {
  586. S32 size = objectList.size();
  587. S32 size_cpy = size;
  588. //-Mat look through our vector all normal-like, trying to use an iterator sometimes gives us
  589. //bad cast on good objects
  590. for( S32 count = 0; count < objectList.size(); count++ )
  591. {
  592. GuiControl *ctrl = (GuiControl *)objectList[count];
  593. if( ctrl == NULL ) {
  594. Con::errorf( "GuiControl::renderChildControls() object %i is NULL", count );
  595. continue;
  596. }
  597. if (ctrl->mVisible)
  598. {
  599. ctrl->mRenderInsetLT = content.point - offset;
  600. ctrl->mRenderInsetRB = mBounds.extent - (ctrl->mRenderInsetLT + content.extent);
  601. Point2I childPosition = content.point + ctrl->getPosition();
  602. RectI childClip(childPosition, ctrl->getExtent());
  603. if (childClip.intersect(clipRect))
  604. {
  605. RectI old = dglGetClipRect();
  606. dglSetClipRect(clipRect);
  607. glDisable(GL_CULL_FACE);
  608. ctrl->onRender(childPosition, RectI(childPosition, ctrl->getExtent()));
  609. dglSetClipRect(old);
  610. }
  611. }
  612. size_cpy = objectList.size(); // CHRIS: i know its wierd but the size of the list changes sometimes during execution of this loop
  613. if(size != size_cpy)
  614. {
  615. size = size_cpy;
  616. count--; // CHRIS: just to make sure one wasnt skipped.
  617. }
  618. }
  619. }
  620. }
  621. void GuiControl::setUpdateRegion(Point2I pos, Point2I ext)
  622. {
  623. Point2I upos = localToGlobalCoord(pos);
  624. GuiCanvas *root = getRoot();
  625. if (root)
  626. {
  627. root->addUpdateRegion(upos, ext);
  628. }
  629. }
  630. void GuiControl::setUpdate()
  631. {
  632. setUpdateRegion(Point2I(0,0), mBounds.extent);
  633. }
  634. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //
  635. void GuiControl::awaken()
  636. {
  637. AssertFatal(!mAwake, "GuiControl::awaken: control is already awake");
  638. if(mAwake)
  639. return;
  640. iterator i;
  641. for(i = begin(); i != end(); i++)
  642. {
  643. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  644. AssertFatal(!ctrl->isAwake(), "GuiControl::awaken: child control is already awake");
  645. if(!ctrl->isAwake())
  646. ctrl->awaken();
  647. }
  648. AssertFatal(!mAwake, "GuiControl::awaken: should not be awake here");
  649. if(!mAwake)
  650. {
  651. if(!onWake())
  652. {
  653. Con::errorf(ConsoleLogEntry::General, "GuiControl::awaken: failed onWake for obj: %s", getName());
  654. AssertFatal(0, "GuiControl::awaken: failed onWake");
  655. deleteObject();
  656. }
  657. else
  658. {
  659. if (mTextID && *mTextID != 0)
  660. setTextID(mTextID);
  661. }
  662. }
  663. }
  664. void GuiControl::sleep()
  665. {
  666. AssertFatal(mAwake, "GuiControl::sleep: control is not awake");
  667. if(!mAwake)
  668. return;
  669. iterator i;
  670. for(i = begin(); i != end(); i++)
  671. {
  672. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  673. AssertFatal(ctrl->isAwake(), "GuiControl::sleep: child control is already asleep");
  674. if(ctrl->isAwake())
  675. ctrl->sleep();
  676. }
  677. AssertFatal(mAwake, "GuiControl::sleep: should not be asleep here");
  678. if(mAwake)
  679. onSleep();
  680. }
  681. void GuiControl::preRender()
  682. {
  683. AssertFatal(mAwake, "GuiControl::preRender: control is not awake");
  684. if(!mAwake)
  685. return;
  686. iterator i;
  687. for(i = begin(); i != end(); i++)
  688. {
  689. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  690. ctrl->preRender();
  691. }
  692. onPreRender();
  693. }
  694. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //
  695. bool GuiControl::onWake()
  696. {
  697. AssertFatal( !mAwake, "GuiControl::onWake: control is already awake" );
  698. if( mAwake )
  699. return false;
  700. // [tom, 4/18/2005] Cause mLangTable to be refreshed in case it was changed
  701. mLangTable = NULL;
  702. // Grab the classname of this object
  703. const char *cName = getClassName();
  704. //make sure we have a profile
  705. if( !mProfile )
  706. {
  707. // Ensure the classname is a valid name...
  708. if( cName && cName[0] )
  709. {
  710. S32 pos = 0;
  711. for( pos = 0; pos <= (S32)dStrlen( cName ); pos++ )
  712. if( !dStrncmp( cName + pos, "Ctrl", 4 ) )
  713. break;
  714. if( pos != 0 ) {
  715. char buff[255];
  716. dStrncpy( buff, cName, pos );
  717. buff[pos] = '\0';
  718. dStrcat( buff, "Profile\0" );
  719. SimObject *obj = Sim::findObject( buff );
  720. if( obj )
  721. mProfile = dynamic_cast<GuiControlProfile*>( obj );
  722. }
  723. }
  724. // Ok lets check to see if that worked
  725. if( !mProfile ) {
  726. SimObject *obj = Sim::findObject( "GuiDefaultProfile" );
  727. if( obj )
  728. mProfile = dynamic_cast<GuiControlProfile*>(obj);
  729. }
  730. AssertFatal( mProfile, avar( "GuiControl: %s created with no profile.", getName() ) );
  731. }
  732. //set the flag
  733. mAwake = true;
  734. //set the layer
  735. GuiCanvas *root = getRoot();
  736. AssertFatal(root, "Unable to get the root Canvas.");
  737. GuiControl *parent = getParent();
  738. if (parent && parent != root)
  739. mLayer = parent->mLayer;
  740. //make sure the first responder exists
  741. if (! mFirstResponder)
  742. mFirstResponder = findFirstTabable();
  743. //see if we should force this control to be the first responder
  744. //if (mProfile->mTabable && mProfile->mCanKeyFocus)
  745. // setFirstResponder();
  746. //increment the profile
  747. mProfile->incRefCount();
  748. // Only invoke script callbacks if we have a namespace in which to do so
  749. // This will suppress warnings
  750. if( isMethod("onWake") )
  751. Con::executef(this, 1, "onWake");
  752. if (mTooltipProfile != NULL)
  753. mTooltipProfile->incRefCount();
  754. return true;
  755. }
  756. void GuiControl::onSleep()
  757. {
  758. AssertFatal(mAwake, "GuiControl::onSleep: control is not awake");
  759. if(!mAwake)
  760. return;
  761. //decrement the profile referrence
  762. if( mProfile != NULL )
  763. mProfile->decRefCount();
  764. clearFirstResponder();
  765. mouseUnlock();
  766. // Only invoke script callbacks if we have a namespace in which to do so
  767. // This will suppress warnings
  768. if( isMethod("onSleep") )
  769. Con::executef(this, 1, "onSleep");
  770. if (mTooltipProfile != NULL)
  771. mTooltipProfile->decRefCount();
  772. // Set Flag
  773. mAwake = false;
  774. }
  775. void GuiControl::setControlProfile(GuiControlProfile *prof)
  776. {
  777. AssertFatal(prof, "GuiControl::setControlProfile: invalid profile");
  778. if(prof == mProfile)
  779. return;
  780. if(mAwake)
  781. mProfile->decRefCount();
  782. mProfile = prof;
  783. if(mAwake)
  784. mProfile->incRefCount();
  785. }
  786. void GuiControl::onPreRender()
  787. {
  788. // do nothing.
  789. }
  790. //-----------------------------------------------------------------------------
  791. // checks up the parent hierarchy - if anyone above us is not savable returns false
  792. // otherwise, returns true.
  793. //-----------------------------------------------------------------------------
  794. bool GuiControl::getCanSaveParent()
  795. {
  796. GuiControl *walk = this;
  797. while(walk)
  798. {
  799. if(!walk->getCanSave())
  800. return false;
  801. walk = walk->getParent();
  802. }
  803. return true;
  804. }
  805. //-----------------------------------------------------------------------------
  806. // Can we Save to a TorqueScript file?
  807. //-----------------------------------------------------------------------------
  808. bool GuiControl::getCanSave()
  809. {
  810. return mCanSave;
  811. }
  812. //-----------------------------------------------------------------------------
  813. // Sets whether we can save out to a file (TorqueScript)
  814. //-----------------------------------------------------------------------------
  815. void GuiControl::setCanSave(bool bCanSave)
  816. {
  817. mCanSave = bCanSave;
  818. }
  819. ////////////////////////////////////////////////////////////////////////////////////////////////////
  820. // checks out mCanSave flag, if true just passes along to our parent,
  821. // if false, then we return without writing. Note, also, that
  822. // if our parent is not writeable, then we should not be writable...
  823. ////////////////////////////////////////////////////////////////////////////////////////////////////
  824. void GuiControl::write(Stream &stream, U32 tabStop, U32 flags)
  825. {
  826. //note: this will return false if either we, or any of our parents, are non-save controls
  827. bool bCanSave = getCanSaveParent();
  828. if(bCanSave)
  829. {
  830. Parent::write(stream, tabStop, flags);
  831. }
  832. }
  833. //This is only called if the control is deleted, not when the control is removed from its parent.
  834. void GuiControl::onRemove()
  835. {
  836. Parent::onRemove();
  837. }
  838. //For GuiControls, this will always just before it is actually removed.
  839. void GuiControl::onGroupRemove()
  840. {
  841. clearFirstResponder();
  842. }
  843. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //
  844. const char *GuiControl::getScriptValue()
  845. {
  846. return NULL;
  847. }
  848. void GuiControl::setScriptValue(const char *value)
  849. {
  850. }
  851. void GuiControl::setConsoleVariable(const char *variable)
  852. {
  853. if (variable)
  854. {
  855. mConsoleVariable = StringTable->insert(variable);
  856. }
  857. else
  858. {
  859. mConsoleVariable = StringTable->EmptyString;
  860. }
  861. }
  862. //-----------------------------------------------------------------------------
  863. // finds and returns the first immediate child of ours whose
  864. // internal name matches the passed String. returns Null if not found.
  865. //-----------------------------------------------------------------------------
  866. void GuiControl::setConsoleCommand(const char *newCmd)
  867. {
  868. if (newCmd)
  869. mConsoleCommand = StringTable->insert(newCmd);
  870. else
  871. mConsoleCommand = StringTable->EmptyString;
  872. }
  873. const char * GuiControl::getConsoleCommand()
  874. {
  875. return mConsoleCommand;
  876. }
  877. void GuiControl::setSizing(S32 horz, S32 vert)
  878. {
  879. mHorizSizing = horz;
  880. mVertSizing = vert;
  881. }
  882. void GuiControl::setVariable(const char *value)
  883. {
  884. if (mConsoleVariable[0])
  885. Con::setVariable(mConsoleVariable, value);
  886. }
  887. void GuiControl::setIntVariable(S32 value)
  888. {
  889. if (mConsoleVariable[0])
  890. Con::setIntVariable(mConsoleVariable, value);
  891. }
  892. void GuiControl::setFloatVariable(F32 value)
  893. {
  894. if (mConsoleVariable[0])
  895. Con::setFloatVariable(mConsoleVariable, value);
  896. }
  897. const char * GuiControl::getVariable()
  898. {
  899. if (mConsoleVariable[0])
  900. return Con::getVariable(mConsoleVariable);
  901. else return NULL;
  902. }
  903. S32 GuiControl::getIntVariable()
  904. {
  905. if (mConsoleVariable[0])
  906. return Con::getIntVariable(mConsoleVariable);
  907. else return 0;
  908. }
  909. F32 GuiControl::getFloatVariable()
  910. {
  911. if (mConsoleVariable[0])
  912. return Con::getFloatVariable(mConsoleVariable);
  913. else return 0.0f;
  914. }
  915. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //
  916. bool GuiControl::cursorInControl()
  917. {
  918. GuiCanvas *root = getRoot();
  919. if (! root) return false;
  920. Point2I pt = root->getCursorPos();
  921. Point2I offset = localToGlobalCoord(Point2I(0, 0));
  922. if (pt.x >= offset.x && pt.y >= offset.y &&
  923. pt.x < offset.x + mBounds.extent.x && pt.y < offset.y + mBounds.extent.y)
  924. {
  925. return true;
  926. }
  927. else
  928. {
  929. return false;
  930. }
  931. }
  932. bool GuiControl::pointInControl(const Point2I& parentCoordPoint)
  933. {
  934. S32 xt = parentCoordPoint.x - mBounds.point.x;
  935. S32 yt = parentCoordPoint.y - mBounds.point.y;
  936. return xt >= 0 && yt >= 0 && xt < mBounds.extent.x && yt < mBounds.extent.y;
  937. }
  938. GuiControl* GuiControl::findHitControl(const Point2I &pt, S32 initialLayer)
  939. {
  940. iterator i = end(); // find in z order (last to first)
  941. while (i != begin())
  942. {
  943. i--;
  944. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  945. if (initialLayer >= 0 && ctrl->mLayer > initialLayer)
  946. {
  947. continue;
  948. }
  949. else if (ctrl->mVisible && ctrl->pointInControl(pt - ctrl->mRenderInsetLT))
  950. {
  951. Point2I ptemp = pt - (ctrl->mBounds.point + ctrl->mRenderInsetLT);
  952. GuiControl *hitCtrl = ctrl->findHitControl(ptemp);
  953. if(hitCtrl->mProfile->mUseInput)
  954. return hitCtrl;
  955. }
  956. }
  957. return this;
  958. }
  959. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //
  960. bool GuiControl::isMouseLocked()
  961. {
  962. GuiCanvas *root = getRoot();
  963. return root ? root->getMouseLockedControl() == this : false;
  964. }
  965. void GuiControl::mouseLock(GuiControl *lockingControl)
  966. {
  967. GuiCanvas *root = getRoot();
  968. if (root)
  969. root->mouseLock(lockingControl);
  970. }
  971. void GuiControl::mouseLock()
  972. {
  973. GuiCanvas *root = getRoot();
  974. if (root)
  975. root->mouseLock(this);
  976. }
  977. void GuiControl::mouseUnlock()
  978. {
  979. GuiCanvas *root = getRoot();
  980. if (root)
  981. root->mouseUnlock(this);
  982. }
  983. bool GuiControl::onInputEvent(const InputEvent &event)
  984. {
  985. // Do nothing by default...
  986. return( false );
  987. }
  988. void GuiControl::onTouchUp(const GuiEvent &event)
  989. {
  990. }
  991. void GuiControl::onTouchDown(const GuiEvent &event)
  992. {
  993. }
  994. void GuiControl::onTouchMove(const GuiEvent &event)
  995. {
  996. //if this control is a dead end, make sure the event stops here
  997. if ( !mVisible || !mAwake )
  998. return;
  999. //pass the event to the parent
  1000. GuiControl *parent = getParent();
  1001. if ( parent )
  1002. parent->onTouchMove( event );
  1003. }
  1004. void GuiControl::onTouchDragged(const GuiEvent &event)
  1005. {
  1006. }
  1007. void GuiControl::onTouchEnter(const GuiEvent &)
  1008. {
  1009. }
  1010. void GuiControl::onTouchLeave(const GuiEvent &)
  1011. {
  1012. }
  1013. bool GuiControl::onMouseWheelUp( const GuiEvent &event )
  1014. {
  1015. //if this control is a dead end, make sure the event stops here
  1016. if ( !mVisible || !mAwake )
  1017. return true;
  1018. //pass the event to the parent
  1019. GuiControl *parent = getParent();
  1020. if ( parent )
  1021. return parent->onMouseWheelUp( event );
  1022. else
  1023. return false;
  1024. }
  1025. bool GuiControl::onMouseWheelDown( const GuiEvent &event )
  1026. {
  1027. //if this control is a dead end, make sure the event stops here
  1028. if ( !mVisible || !mAwake )
  1029. return true;
  1030. //pass the event to the parent
  1031. GuiControl *parent = getParent();
  1032. if ( parent )
  1033. return parent->onMouseWheelDown( event );
  1034. else
  1035. return false;
  1036. }
  1037. void GuiControl::onRightMouseDown(const GuiEvent &)
  1038. {
  1039. }
  1040. void GuiControl::onRightMouseUp(const GuiEvent &)
  1041. {
  1042. }
  1043. void GuiControl::onRightMouseDragged(const GuiEvent &)
  1044. {
  1045. }
  1046. void GuiControl::onMiddleMouseDown(const GuiEvent &)
  1047. {
  1048. }
  1049. void GuiControl::onMiddleMouseUp(const GuiEvent &)
  1050. {
  1051. }
  1052. void GuiControl::onMiddleMouseDragged(const GuiEvent &)
  1053. {
  1054. }
  1055. GuiControl* GuiControl::findFirstTabable()
  1056. {
  1057. GuiControl *tabCtrl = NULL;
  1058. iterator i;
  1059. for (i = begin(); i != end(); i++)
  1060. {
  1061. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  1062. tabCtrl = ctrl->findFirstTabable();
  1063. if (tabCtrl)
  1064. {
  1065. mFirstResponder = tabCtrl;
  1066. return tabCtrl;
  1067. }
  1068. }
  1069. //nothing was found, therefore, see if this ctrl is tabable
  1070. return ( mProfile != NULL ) ? ( ( mProfile->mTabable && mAwake && mVisible ) ? this : NULL ) : NULL;
  1071. }
  1072. GuiControl* GuiControl::findLastTabable(bool firstCall)
  1073. {
  1074. //if this is the first call, clear the global
  1075. if (firstCall)
  1076. smPrevResponder = NULL;
  1077. //if this control is tabable, set the global
  1078. if (mProfile->mTabable)
  1079. smPrevResponder = this;
  1080. iterator i;
  1081. for (i = begin(); i != end(); i++)
  1082. {
  1083. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  1084. ctrl->findLastTabable(false);
  1085. }
  1086. //after the entire tree has been traversed, return the last responder found
  1087. mFirstResponder = smPrevResponder;
  1088. return smPrevResponder;
  1089. }
  1090. GuiControl *GuiControl::findNextTabable(GuiControl *curResponder, bool firstCall)
  1091. {
  1092. //if this is the first call, clear the global
  1093. if (firstCall)
  1094. smCurResponder = NULL;
  1095. //first find the current responder
  1096. if (curResponder == this)
  1097. smCurResponder = this;
  1098. //if the first responder has been found, return the very next *tabable* control
  1099. else if ( smCurResponder && mProfile->mTabable && mAwake && mVisible && mActive )
  1100. return( this );
  1101. //loop through, checking each child to see if it is the one that follows the firstResponder
  1102. GuiControl *tabCtrl = NULL;
  1103. iterator i;
  1104. for (i = begin(); i != end(); i++)
  1105. {
  1106. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  1107. tabCtrl = ctrl->findNextTabable(curResponder, false);
  1108. if (tabCtrl) break;
  1109. }
  1110. mFirstResponder = tabCtrl;
  1111. return tabCtrl;
  1112. }
  1113. GuiControl *GuiControl::findPrevTabable(GuiControl *curResponder, bool firstCall)
  1114. {
  1115. if (firstCall)
  1116. smPrevResponder = NULL;
  1117. //if this is the current reponder, return the previous one
  1118. if (curResponder == this)
  1119. return smPrevResponder;
  1120. //else if this is a responder, store it in case the next found is the current responder
  1121. else if ( mProfile->mTabable && mAwake && mVisible && mActive )
  1122. smPrevResponder = this;
  1123. //loop through, checking each child to see if it is the one that follows the firstResponder
  1124. GuiControl *tabCtrl = NULL;
  1125. iterator i;
  1126. for (i = begin(); i != end(); i++)
  1127. {
  1128. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  1129. tabCtrl = ctrl->findPrevTabable(curResponder, false);
  1130. if (tabCtrl) break;
  1131. }
  1132. mFirstResponder = tabCtrl;
  1133. return tabCtrl;
  1134. }
  1135. void GuiControl::onLoseFirstResponder()
  1136. {
  1137. // Since many controls have visual cues when they are the firstResponder...
  1138. setUpdate();
  1139. }
  1140. bool GuiControl::ControlIsChild(GuiControl *child)
  1141. {
  1142. //function returns true if this control, or one of it's children is the child control
  1143. if (child == this)
  1144. return true;
  1145. //loop through, checking each child to see if it is ,or contains, the firstResponder
  1146. iterator i;
  1147. for (i = begin(); i != end(); i++)
  1148. {
  1149. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  1150. if (ctrl->ControlIsChild(child)) return true;
  1151. }
  1152. //not found, therefore false
  1153. return false;
  1154. }
  1155. void GuiControl::onFocus()
  1156. {
  1157. //bubble the focus up
  1158. GuiControl *parent = getParent();
  1159. if (parent)
  1160. parent->onFocus();
  1161. }
  1162. bool GuiControl::isFirstResponder()
  1163. {
  1164. GuiCanvas *root = getRoot();
  1165. return root && root->getFirstResponder() == this;
  1166. }
  1167. void GuiControl::setFirstResponder( GuiControl* firstResponder )
  1168. {
  1169. if ( firstResponder && firstResponder->mProfile->mCanKeyFocus )
  1170. mFirstResponder = firstResponder;
  1171. GuiControl *parent = getParent();
  1172. if ( parent )
  1173. parent->setFirstResponder( firstResponder );
  1174. }
  1175. void GuiControl::setFirstResponder()
  1176. {
  1177. if ( mAwake && mVisible )
  1178. {
  1179. GuiControl *parent = getParent();
  1180. if (mProfile->mCanKeyFocus == true && parent != NULL )
  1181. {
  1182. parent->setFirstResponder(this);
  1183. // Since many controls have visual cues when they are the firstResponder...
  1184. this->setUpdate();
  1185. }
  1186. }
  1187. }
  1188. void GuiControl::clearFirstResponder()
  1189. {
  1190. GuiControl *parent = this;
  1191. while((parent = parent->getParent()) != NULL)
  1192. {
  1193. if(parent->mFirstResponder == this)
  1194. parent->mFirstResponder = NULL;
  1195. else
  1196. break;
  1197. }
  1198. }
  1199. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //
  1200. void GuiControl::buildAcceleratorMap()
  1201. {
  1202. //add my own accel key
  1203. addAcceleratorKey();
  1204. //add all my childrens keys
  1205. iterator i;
  1206. for(i = begin(); i != end(); i++)
  1207. {
  1208. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  1209. ctrl->buildAcceleratorMap();
  1210. }
  1211. }
  1212. void GuiControl::addAcceleratorKey()
  1213. {
  1214. //see if we have an accelerator
  1215. if (mAcceleratorKey == StringTable->EmptyString)
  1216. return;
  1217. EventDescriptor accelEvent;
  1218. ActionMap::createEventDescriptor(mAcceleratorKey, &accelEvent);
  1219. //now we have a modifier, and a key, add them to the canvas
  1220. GuiCanvas *root = getRoot();
  1221. if (root)
  1222. root->addAcceleratorKey(this, 0, accelEvent.eventCode, accelEvent.flags);
  1223. }
  1224. void GuiControl::acceleratorKeyPress(U32 index)
  1225. {
  1226. onAction();
  1227. }
  1228. void GuiControl::acceleratorKeyRelease(U32 index)
  1229. {
  1230. //do nothing
  1231. }
  1232. bool GuiControl::onKeyDown(const GuiEvent &event)
  1233. {
  1234. //pass the event to the parent
  1235. GuiControl *parent = getParent();
  1236. if (parent)
  1237. return parent->onKeyDown(event);
  1238. else
  1239. return false;
  1240. }
  1241. bool GuiControl::onKeyRepeat(const GuiEvent &event)
  1242. {
  1243. // default to just another key down.
  1244. return onKeyDown(event);
  1245. }
  1246. bool GuiControl::onKeyUp(const GuiEvent &event)
  1247. {
  1248. //pass the event to the parent
  1249. GuiControl *parent = getParent();
  1250. if (parent)
  1251. return parent->onKeyUp(event);
  1252. else
  1253. return false;
  1254. }
  1255. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //
  1256. void GuiControl::onAction()
  1257. {
  1258. if (! mActive)
  1259. return;
  1260. //execute the console command
  1261. if (mConsoleCommand && mConsoleCommand[0])
  1262. {
  1263. execConsoleCallback();
  1264. }
  1265. else
  1266. Con::executef(this, 1, "onAction");
  1267. }
  1268. void GuiControl::onMessage(GuiControl *sender, S32 msg)
  1269. {
  1270. }
  1271. void GuiControl::messageSiblings(S32 message)
  1272. {
  1273. GuiControl *parent = getParent();
  1274. if (! parent) return;
  1275. GuiControl::iterator i;
  1276. for(i = parent->begin(); i != parent->end(); i++)
  1277. {
  1278. GuiControl *ctrl = dynamic_cast<GuiControl *>(*i);
  1279. if (ctrl != this)
  1280. ctrl->onMessage(this, message);
  1281. }
  1282. }
  1283. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //
  1284. void GuiControl::onDialogPush()
  1285. {
  1286. // Notify Script.
  1287. if( isMethod("onDialogPush") )
  1288. Con::executef(this, 1, "onDialogPush");
  1289. }
  1290. void GuiControl::onDialogPop()
  1291. {
  1292. // Notify Script.
  1293. if( isMethod("onDialogPop") )
  1294. Con::executef(this, 1, "onDialogPop");
  1295. }
  1296. //------------------------------------------------------------------------------
  1297. void GuiControl::setVisible(bool value)
  1298. {
  1299. mVisible = value;
  1300. iterator i;
  1301. setUpdate();
  1302. for(i = begin(); i != end(); i++)
  1303. {
  1304. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  1305. ctrl->clearFirstResponder();
  1306. }
  1307. GuiControl *parent = getParent();
  1308. if (parent)
  1309. parent->childResized(this);
  1310. }
  1311. void GuiControl::makeFirstResponder(bool value)
  1312. {
  1313. if ( value )
  1314. //setFirstResponder(this);
  1315. setFirstResponder();
  1316. else
  1317. clearFirstResponder();
  1318. }
  1319. void GuiControl::setActive( bool value )
  1320. {
  1321. mActive = value;
  1322. if ( !mActive )
  1323. clearFirstResponder();
  1324. if ( mVisible && mAwake )
  1325. setUpdate();
  1326. }
  1327. void GuiControl::getScrollLineSizes(U32 *rowHeight, U32 *columnWidth)
  1328. {
  1329. // default to 10 pixels in y, 30 pixels in x
  1330. *columnWidth = 30;
  1331. *rowHeight = 30;
  1332. }
  1333. void GuiControl::renderText(Point2I offset, Point2I extent, const char *text, GuiControlProfile *profile, TextRotationOptions rot)
  1334. {
  1335. GFont *font = profile->mFont;
  1336. S32 textWidth = font->getStrWidth((const UTF8*)text);
  1337. S32 textHeight = font->getHeight();
  1338. S32 totalWidth = extent.x;
  1339. S32 totalHeight = extent.y;
  1340. if (rot != tRotateNone)
  1341. {
  1342. totalWidth = extent.y;
  1343. totalHeight = extent.x;
  1344. }
  1345. Point2I startOffset = Point2I(0,0);
  1346. // align the horizontal
  1347. if(textWidth < totalWidth)
  1348. {
  1349. if (profile->mAlignment == GuiControlProfile::RightAlign)
  1350. {
  1351. startOffset.x = totalWidth - textWidth;
  1352. }
  1353. else if (profile->mAlignment == GuiControlProfile::CenterAlign)
  1354. {
  1355. startOffset.x = (totalWidth - textWidth) / 2;
  1356. }
  1357. }
  1358. // align the vertical
  1359. if (textHeight < totalHeight)
  1360. {
  1361. if (profile->mVAlignment == GuiControlProfile::MiddleVAlign)
  1362. {
  1363. startOffset.y = (totalHeight - textHeight) / 2;
  1364. }
  1365. else if (profile->mVAlignment == GuiControlProfile::BottomVAlign)
  1366. {
  1367. startOffset.y = (totalHeight - textHeight);
  1368. }
  1369. }
  1370. else
  1371. {
  1372. startOffset.y = -((textHeight - totalHeight) / 2);
  1373. }
  1374. Point2I start = Point2I(0, 0);
  1375. F32 rotation;
  1376. if (rot == tRotateNone)
  1377. {
  1378. start += startOffset;
  1379. rotation = 0.0f;
  1380. }
  1381. else if (rot == tRotateLeft)
  1382. {
  1383. start.x = startOffset.y;
  1384. start.y = extent.y + startOffset.x;
  1385. rotation = 90.0f;
  1386. }
  1387. else if (rot == tRotateRight)
  1388. {
  1389. start.x = extent.x - startOffset.y;
  1390. start.y = startOffset.x;
  1391. rotation = -90.0f;
  1392. }
  1393. RectI old = dglGetClipRect();
  1394. RectI clipRect = RectI(offset, extent);
  1395. if(clipRect.intersect(old))
  1396. {
  1397. dglSetClipRect(clipRect);
  1398. dglDrawText( font, start + offset + profile->mTextOffset, text, profile->mFontColors, 9, rotation );
  1399. dglSetClipRect(old);
  1400. }
  1401. }
  1402. void GuiControl::getCursor(GuiCursor *&cursor, bool &showCursor, const GuiEvent &lastGuiEvent)
  1403. {
  1404. lastGuiEvent;
  1405. if(GuiControl::smCursorChanged != -1 && !isMouseLocked())
  1406. {
  1407. // We've already changed the cursor,
  1408. // so set it back before we change it again.
  1409. Input::popCursor();
  1410. // We haven't changed it
  1411. GuiControl::smCursorChanged = -1;
  1412. }
  1413. }
  1414. const char* GuiControl::execConsoleCallback()
  1415. {
  1416. if (mConsoleCommand && mConsoleCommand[0])
  1417. {
  1418. Con::setVariable("$ThisControl", avar("%d",getId()));
  1419. return Con::evaluate(mConsoleCommand, false);
  1420. }
  1421. return "";
  1422. }
  1423. const char* GuiControl::execAltConsoleCallback()
  1424. {
  1425. if(mAltConsoleCommand && mAltConsoleCommand[0])
  1426. {
  1427. Con::setVariable("$ThisControl", avar("%d",getId()));
  1428. return Con::evaluate(mAltConsoleCommand, false);
  1429. }
  1430. return "";
  1431. }
  1432. void GuiControl::setText(const char *text)
  1433. {
  1434. mText = StringTable->insert(text);
  1435. }
  1436. void GuiControl::setTextID(const char *id)
  1437. {
  1438. S32 n = Con::getIntVariable(id, -1);
  1439. if (n != -1)
  1440. {
  1441. mTextID = StringTable->insert(id);
  1442. setTextID(n);
  1443. }
  1444. }
  1445. void GuiControl::setTextID(S32 id)
  1446. {
  1447. const UTF8 *str = getGUIString(id);
  1448. if (str)
  1449. setText((const char*)str);
  1450. }
  1451. const char *GuiControl::getText()
  1452. {
  1453. return mText;
  1454. }