2
0

guiControl.cc 50 KB

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