guiControl.cc 51 KB

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