guiControl.cc 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727
  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() + walk->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() + walk->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. resize(newPosition, newExtent);
  369. }
  370. //----------------------------------------------------------------
  371. void GuiControl::onRender(Point2I offset, const RectI &updateRect)
  372. {
  373. RectI ctrlRect = applyMargins(offset, mBounds.extent, NormalState, mProfile);
  374. if (!ctrlRect.isValidRect())
  375. {
  376. return;
  377. }
  378. renderBorderedRect(ctrlRect, mProfile, NormalState);
  379. //Render Text
  380. dglSetBitmapModulation(mProfile->mFontColor);
  381. RectI fillRect = applyBorders(ctrlRect.point, ctrlRect.extent, NormalState, mProfile);
  382. RectI contentRect = applyPadding(fillRect.point, fillRect.extent, NormalState, mProfile);
  383. if(contentRect.isValidRect())
  384. {
  385. renderText(contentRect.point, contentRect.extent, mText, mProfile);
  386. //Render the childen
  387. renderChildControls(offset, contentRect, updateRect);
  388. }
  389. }
  390. RectI GuiControl::applyMargins(Point2I offset, Point2I extent, GuiControlState currentState, GuiControlProfile *profile)
  391. {
  392. //Get the border profiles
  393. GuiBorderProfile *leftProfile = profile->getLeftBorder();
  394. GuiBorderProfile *rightProfile = profile->getRightBorder();
  395. GuiBorderProfile *topProfile = profile->getTopBorder();
  396. GuiBorderProfile *bottomProfile = profile->getBottomBorder();
  397. S32 leftSize = (leftProfile) ? leftProfile->getMargin(currentState) : 0;
  398. S32 rightSize = (rightProfile) ? rightProfile->getMargin(currentState) : 0;
  399. S32 topSize = (topProfile) ? topProfile->getMargin(currentState) : 0;
  400. S32 bottomSize = (bottomProfile) ? bottomProfile->getMargin(currentState) : 0;
  401. return RectI(offset.x + leftSize, offset.y + topSize, (extent.x - leftSize) - rightSize, (extent.y - topSize) - bottomSize);
  402. }
  403. RectI GuiControl::applyBorders(Point2I offset, Point2I extent, GuiControlState currentState, GuiControlProfile *profile)
  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->getBorder(currentState) : 0;
  411. S32 rightSize = (rightProfile) ? rightProfile->getBorder(currentState) : 0;
  412. S32 topSize = (topProfile) ? topProfile->getBorder(currentState) : 0;
  413. S32 bottomSize = (bottomProfile) ? bottomProfile->getBorder(currentState) : 0;
  414. return RectI(offset.x + leftSize, offset.y + topSize, (extent.x - leftSize) - rightSize, (extent.y - topSize) - bottomSize);
  415. }
  416. RectI GuiControl::applyPadding(Point2I offset, Point2I extent, GuiControlState currentState, GuiControlProfile *profile)
  417. {
  418. //Get the border profiles
  419. GuiBorderProfile *leftProfile = profile->getLeftBorder();
  420. GuiBorderProfile *rightProfile = profile->getRightBorder();
  421. GuiBorderProfile *topProfile = profile->getTopBorder();
  422. GuiBorderProfile *bottomProfile = profile->getBottomBorder();
  423. S32 leftSize = (leftProfile) ? leftProfile->getPadding(currentState) : 0;
  424. S32 rightSize = (rightProfile) ? rightProfile->getPadding(currentState) : 0;
  425. S32 topSize = (topProfile) ? topProfile->getPadding(currentState) : 0;
  426. S32 bottomSize = (bottomProfile) ? bottomProfile->getPadding(currentState) : 0;
  427. return RectI(offset.x + leftSize, offset.y + topSize, (extent.x - leftSize) - rightSize, (extent.y - topSize) - bottomSize);
  428. }
  429. RectI GuiControl::getInnerRect(Point2I offset, Point2I extent, GuiControlState currentState, GuiControlProfile *profile)
  430. {
  431. //Get the border profiles
  432. GuiBorderProfile *leftProfile = profile->getLeftBorder();
  433. GuiBorderProfile *rightProfile = profile->getRightBorder();
  434. GuiBorderProfile *topProfile = profile->getTopBorder();
  435. GuiBorderProfile *bottomProfile = profile->getBottomBorder();
  436. S32 leftSize = (leftProfile) ? leftProfile->getMargin(currentState) + leftProfile->getBorder(currentState) + leftProfile->getPadding(currentState) : 0;
  437. S32 rightSize = (rightProfile) ? rightProfile->getMargin(currentState) + rightProfile->getBorder(currentState) + rightProfile->getPadding(currentState) : 0;
  438. S32 topSize = (topProfile) ? topProfile->getMargin(currentState) + topProfile->getBorder(currentState) + topProfile->getPadding(currentState) : 0;
  439. S32 bottomSize = (bottomProfile) ? bottomProfile->getMargin(currentState) + bottomProfile->getBorder(currentState) + bottomProfile->getPadding(currentState) : 0;
  440. return RectI(offset.x + leftSize, offset.y + topSize, (extent.x - leftSize) - rightSize, (extent.y - topSize) - bottomSize);
  441. }
  442. Point2I GuiControl::getOuterExtent(Point2I innerExtent, GuiControlState currentState, GuiControlProfile *profile)
  443. {
  444. //Get the border profiles
  445. GuiBorderProfile *leftProfile = profile->getLeftBorder();
  446. GuiBorderProfile *rightProfile = profile->getRightBorder();
  447. GuiBorderProfile *topProfile = profile->getTopBorder();
  448. GuiBorderProfile *bottomProfile = profile->getBottomBorder();
  449. S32 leftSize = (leftProfile) ? leftProfile->getMargin(currentState) + leftProfile->getBorder(currentState) + leftProfile->getPadding(currentState) : 0;
  450. S32 rightSize = (rightProfile) ? rightProfile->getMargin(currentState) + rightProfile->getBorder(currentState) + rightProfile->getPadding(currentState) : 0;
  451. S32 topSize = (topProfile) ? topProfile->getMargin(currentState) + topProfile->getBorder(currentState) + topProfile->getPadding(currentState) : 0;
  452. S32 bottomSize = (bottomProfile) ? bottomProfile->getMargin(currentState) + bottomProfile->getBorder(currentState) + bottomProfile->getPadding(currentState) : 0;
  453. return Point2I(innerExtent.x + leftSize + rightSize, innerExtent.y + topSize + bottomSize);
  454. }
  455. bool GuiControl::renderTooltip(Point2I cursorPos, const char* tipText )
  456. {
  457. #if !defined(TORQUE_OS_IOS) && !defined(TORQUE_OS_ANDROID) && !defined(TORQUE_OS_EMSCRIPTEN)
  458. // Short Circuit.
  459. if (!mAwake)
  460. return false;
  461. if ( dStrlen( mTooltip ) == 0 && ( tipText == NULL || dStrlen( tipText ) == 0 ) )
  462. return false;
  463. const char* renderTip = mTooltip;
  464. if( tipText != NULL )
  465. renderTip = tipText;
  466. // Finish if no root.
  467. GuiCanvas *root = getRoot();
  468. if ( !root )
  469. return false;
  470. if (!mTooltipProfile)
  471. mTooltipProfile = mProfile;
  472. GFont *font = mTooltipProfile->mFont;
  473. // Set text bounds.
  474. Point2I textBounds( 0, 0 );
  475. // Fetch the width of a space.
  476. const S32 spaceWidth = (S32)font->getStrWidth(" ");
  477. // Fetch the maximum allowed tooltip extent.
  478. const S32 maxTooltipWidth = mTooltipWidth;
  479. // Fetch word count.
  480. const S32 wordCount = StringUnit::getUnitCount( renderTip, " " );
  481. // Reset line storage.
  482. const S32 tooltipLineStride = (S32)font->getHeight() + 4;
  483. const S32 maxTooltipLines = 20;
  484. S32 tooltipLineCount = 0;
  485. S32 tooltipLineWidth = 0;
  486. FrameTemp<StringBuffer> tooltipLines( maxTooltipLines );
  487. // Reset word indexing.
  488. S32 wordStartIndex = 0;
  489. S32 wordEndIndex = 0;
  490. // Search for end word.
  491. while( true )
  492. {
  493. // Do we have any words left?
  494. if ( wordEndIndex < wordCount )
  495. {
  496. // Yes, so fetch the word.
  497. const char* pWord = StringUnit::getUnit( renderTip, wordEndIndex, " " );
  498. // Add word length.
  499. const S32 wordLength = (S32)font->getStrWidth( pWord ) + spaceWidth;
  500. // Do we still have room?
  501. if ( (tooltipLineWidth + wordLength) < maxTooltipWidth )
  502. {
  503. // Yes, so add word length.
  504. tooltipLineWidth += wordLength;
  505. // Next word.
  506. wordEndIndex++;
  507. continue;
  508. }
  509. // Do we have any lines left?
  510. if ( tooltipLineCount < maxTooltipLines )
  511. {
  512. // Yes, so insert line.
  513. tooltipLines[tooltipLineCount++] = StringUnit::getUnits( renderTip, wordStartIndex, wordEndIndex-1, " " );
  514. // Update horizontal text bounds.
  515. if ( tooltipLineWidth > textBounds.x )
  516. textBounds.x = tooltipLineWidth;
  517. }
  518. // Set new line length.
  519. tooltipLineWidth = wordLength;
  520. // Set word start.
  521. wordStartIndex = wordEndIndex;
  522. // Next word.
  523. wordEndIndex++;
  524. continue;
  525. }
  526. // Do we have any words left?
  527. if ( wordStartIndex < wordCount )
  528. {
  529. // Yes, so do we have any lines left?
  530. if ( tooltipLineCount < maxTooltipLines )
  531. {
  532. // Yes, so insert line.
  533. tooltipLines[tooltipLineCount++] = StringUnit::getUnits( renderTip, wordStartIndex, wordCount-1, " " );
  534. // Update horizontal text bounds.
  535. if ( tooltipLineWidth > textBounds.x )
  536. textBounds.x = tooltipLineWidth;
  537. }
  538. }
  539. break;
  540. }
  541. // Controls the size of the inside (gutter) tooltip region.
  542. const S32 tooltipGutterSize = 5;
  543. // Adjust text bounds.
  544. textBounds.x += tooltipGutterSize * 2;
  545. textBounds.y = (((S32)font->getHeight() + 4) * tooltipLineCount - 4) + (tooltipGutterSize * 2);
  546. // Adjust to tooltip is always on-screen.
  547. Point2I screensize = Platform::getWindowSize();
  548. Point2I offset = cursorPos;
  549. offset.y += 22;
  550. if (screensize.x < (offset.x + textBounds.x))
  551. offset.x = screensize.x - textBounds.x;
  552. if(screensize.y < (offset.y + textBounds.y) )
  553. offset.y = screensize.y - textBounds.y;
  554. // Fetch the old clip.
  555. RectI oldClip = dglGetClipRect();
  556. // Set rectangle for the box, and set the clip rectangle.
  557. RectI rect(offset, textBounds);
  558. dglSetClipRect(rect);
  559. // Draw body and border of the tool tip
  560. renderBorderedRect(rect, mTooltipProfile, NormalState);
  561. // Draw the text centered in the tool tip box
  562. dglSetBitmapModulation( mTooltipProfile->mFontColor );
  563. Point2I start( tooltipGutterSize, tooltipGutterSize );
  564. for ( S32 lineIndex = 0; lineIndex < tooltipLineCount; lineIndex++ )
  565. {
  566. dglDrawText( font, start + offset, tooltipLines[lineIndex].getPtr8(), mProfile->mFontColors );
  567. offset.y += tooltipLineStride;
  568. }
  569. dglSetClipRect( oldClip );
  570. #endif
  571. return true;
  572. }
  573. void GuiControl::renderChildControls(Point2I offset, RectI content, const RectI &updateRect)
  574. {
  575. // 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.
  576. // updateRect is the area that this control was allowed to draw in. It should almost always be the same as the value in onRender.
  577. // content is the area that child controls are allowed to draw in.
  578. RectI clipRect = content;
  579. if(clipRect.intersect(dglGetClipRect()))
  580. {
  581. S32 size = objectList.size();
  582. S32 size_cpy = size;
  583. //-Mat look through our vector all normal-like, trying to use an iterator sometimes gives us
  584. //bad cast on good objects
  585. for( S32 count = 0; count < objectList.size(); count++ )
  586. {
  587. GuiControl *ctrl = (GuiControl *)objectList[count];
  588. if( ctrl == NULL ) {
  589. Con::errorf( "GuiControl::renderChildControls() object %i is NULL", count );
  590. continue;
  591. }
  592. if (ctrl->mVisible)
  593. {
  594. ctrl->mRenderInsetLT = content.point - offset;
  595. ctrl->mRenderInsetRB = mBounds.extent - (ctrl->mRenderInsetLT + content.extent);
  596. Point2I childPosition = content.point + ctrl->getPosition();
  597. RectI childClip(childPosition, ctrl->getExtent());
  598. if (childClip.intersect(clipRect))
  599. {
  600. dglSetClipRect(clipRect);
  601. glDisable(GL_CULL_FACE);
  602. ctrl->onRender(childPosition, RectI(childPosition, ctrl->getExtent()));
  603. }
  604. }
  605. size_cpy = objectList.size(); // CHRIS: i know its wierd but the size of the list changes sometimes during execution of this loop
  606. if(size != size_cpy)
  607. {
  608. size = size_cpy;
  609. count--; // CHRIS: just to make sure one wasnt skipped.
  610. }
  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. //This is only called if the control is deleted, not when the control is removed from its parent.
  823. void GuiControl::onRemove()
  824. {
  825. Parent::onRemove();
  826. }
  827. //For GuiControls, this will always just before it is actually removed.
  828. void GuiControl::onGroupRemove()
  829. {
  830. clearFirstResponder();
  831. }
  832. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //
  833. const char *GuiControl::getScriptValue()
  834. {
  835. return NULL;
  836. }
  837. void GuiControl::setScriptValue(const char *value)
  838. {
  839. }
  840. void GuiControl::setConsoleVariable(const char *variable)
  841. {
  842. if (variable)
  843. {
  844. mConsoleVariable = StringTable->insert(variable);
  845. }
  846. else
  847. {
  848. mConsoleVariable = StringTable->EmptyString;
  849. }
  850. }
  851. //-----------------------------------------------------------------------------
  852. // finds and returns the first immediate child of ours whose
  853. // internal name matches the passed String. returns Null if not found.
  854. //-----------------------------------------------------------------------------
  855. void GuiControl::setConsoleCommand(const char *newCmd)
  856. {
  857. if (newCmd)
  858. mConsoleCommand = StringTable->insert(newCmd);
  859. else
  860. mConsoleCommand = StringTable->EmptyString;
  861. }
  862. const char * GuiControl::getConsoleCommand()
  863. {
  864. return mConsoleCommand;
  865. }
  866. void GuiControl::setSizing(S32 horz, S32 vert)
  867. {
  868. mHorizSizing = horz;
  869. mVertSizing = vert;
  870. }
  871. void GuiControl::setVariable(const char *value)
  872. {
  873. if (mConsoleVariable[0])
  874. Con::setVariable(mConsoleVariable, value);
  875. }
  876. void GuiControl::setIntVariable(S32 value)
  877. {
  878. if (mConsoleVariable[0])
  879. Con::setIntVariable(mConsoleVariable, value);
  880. }
  881. void GuiControl::setFloatVariable(F32 value)
  882. {
  883. if (mConsoleVariable[0])
  884. Con::setFloatVariable(mConsoleVariable, value);
  885. }
  886. const char * GuiControl::getVariable()
  887. {
  888. if (mConsoleVariable[0])
  889. return Con::getVariable(mConsoleVariable);
  890. else return NULL;
  891. }
  892. S32 GuiControl::getIntVariable()
  893. {
  894. if (mConsoleVariable[0])
  895. return Con::getIntVariable(mConsoleVariable);
  896. else return 0;
  897. }
  898. F32 GuiControl::getFloatVariable()
  899. {
  900. if (mConsoleVariable[0])
  901. return Con::getFloatVariable(mConsoleVariable);
  902. else return 0.0f;
  903. }
  904. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //
  905. bool GuiControl::cursorInControl()
  906. {
  907. GuiCanvas *root = getRoot();
  908. if (! root) return false;
  909. Point2I pt = root->getCursorPos();
  910. Point2I offset = localToGlobalCoord(Point2I(0, 0));
  911. if (pt.x >= offset.x && pt.y >= offset.y &&
  912. pt.x < offset.x + mBounds.extent.x && pt.y < offset.y + mBounds.extent.y)
  913. {
  914. return true;
  915. }
  916. else
  917. {
  918. return false;
  919. }
  920. }
  921. bool GuiControl::pointInControl(const Point2I& parentCoordPoint)
  922. {
  923. S32 xt = parentCoordPoint.x - mBounds.point.x;
  924. S32 yt = parentCoordPoint.y - mBounds.point.y;
  925. return xt >= 0 && yt >= 0 && xt < mBounds.extent.x && yt < mBounds.extent.y;
  926. }
  927. GuiControl* GuiControl::findHitControl(const Point2I &pt, S32 initialLayer)
  928. {
  929. iterator i = end(); // find in z order (last to first)
  930. while (i != begin())
  931. {
  932. i--;
  933. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  934. if (initialLayer >= 0 && ctrl->mLayer > initialLayer)
  935. {
  936. continue;
  937. }
  938. else if (ctrl->mVisible && ctrl->pointInControl(pt - ctrl->mRenderInsetLT))
  939. {
  940. Point2I ptemp = pt - (ctrl->mBounds.point + ctrl->mRenderInsetLT);
  941. GuiControl *hitCtrl = ctrl->findHitControl(ptemp);
  942. if(hitCtrl->mProfile->mModal)
  943. return hitCtrl;
  944. }
  945. }
  946. return this;
  947. }
  948. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //
  949. bool GuiControl::isMouseLocked()
  950. {
  951. GuiCanvas *root = getRoot();
  952. return root ? root->getMouseLockedControl() == this : false;
  953. }
  954. void GuiControl::mouseLock(GuiControl *lockingControl)
  955. {
  956. GuiCanvas *root = getRoot();
  957. if (root)
  958. root->mouseLock(lockingControl);
  959. }
  960. void GuiControl::mouseLock()
  961. {
  962. GuiCanvas *root = getRoot();
  963. if (root)
  964. root->mouseLock(this);
  965. }
  966. void GuiControl::mouseUnlock()
  967. {
  968. GuiCanvas *root = getRoot();
  969. if (root)
  970. root->mouseUnlock(this);
  971. }
  972. bool GuiControl::onInputEvent(const InputEvent &event)
  973. {
  974. // Do nothing by default...
  975. return( false );
  976. }
  977. void GuiControl::onTouchUp(const GuiEvent &event)
  978. {
  979. }
  980. void GuiControl::onTouchDown(const GuiEvent &event)
  981. {
  982. }
  983. void GuiControl::onTouchMove(const GuiEvent &event)
  984. {
  985. //if this control is a dead end, make sure the event stops here
  986. if ( !mVisible || !mAwake )
  987. return;
  988. //pass the event to the parent
  989. GuiControl *parent = getParent();
  990. if ( parent )
  991. parent->onTouchMove( event );
  992. }
  993. void GuiControl::onTouchDragged(const GuiEvent &event)
  994. {
  995. }
  996. void GuiControl::onTouchEnter(const GuiEvent &)
  997. {
  998. }
  999. void GuiControl::onTouchLeave(const GuiEvent &)
  1000. {
  1001. }
  1002. bool GuiControl::onMouseWheelUp( const GuiEvent &event )
  1003. {
  1004. //if this control is a dead end, make sure the event stops here
  1005. if ( !mVisible || !mAwake )
  1006. return true;
  1007. //pass the event to the parent
  1008. GuiControl *parent = getParent();
  1009. if ( parent )
  1010. return parent->onMouseWheelUp( event );
  1011. else
  1012. return false;
  1013. }
  1014. bool GuiControl::onMouseWheelDown( const GuiEvent &event )
  1015. {
  1016. //if this control is a dead end, make sure the event stops here
  1017. if ( !mVisible || !mAwake )
  1018. return true;
  1019. //pass the event to the parent
  1020. GuiControl *parent = getParent();
  1021. if ( parent )
  1022. return parent->onMouseWheelDown( event );
  1023. else
  1024. return false;
  1025. }
  1026. void GuiControl::onRightMouseDown(const GuiEvent &)
  1027. {
  1028. }
  1029. void GuiControl::onRightMouseUp(const GuiEvent &)
  1030. {
  1031. }
  1032. void GuiControl::onRightMouseDragged(const GuiEvent &)
  1033. {
  1034. }
  1035. void GuiControl::onMiddleMouseDown(const GuiEvent &)
  1036. {
  1037. }
  1038. void GuiControl::onMiddleMouseUp(const GuiEvent &)
  1039. {
  1040. }
  1041. void GuiControl::onMiddleMouseDragged(const GuiEvent &)
  1042. {
  1043. }
  1044. GuiControl* GuiControl::findFirstTabable()
  1045. {
  1046. GuiControl *tabCtrl = NULL;
  1047. iterator i;
  1048. for (i = begin(); i != end(); i++)
  1049. {
  1050. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  1051. tabCtrl = ctrl->findFirstTabable();
  1052. if (tabCtrl)
  1053. {
  1054. mFirstResponder = tabCtrl;
  1055. return tabCtrl;
  1056. }
  1057. }
  1058. //nothing was found, therefore, see if this ctrl is tabable
  1059. return ( mProfile != NULL ) ? ( ( mProfile->mTabable && mAwake && mVisible ) ? this : NULL ) : NULL;
  1060. }
  1061. GuiControl* GuiControl::findLastTabable(bool firstCall)
  1062. {
  1063. //if this is the first call, clear the global
  1064. if (firstCall)
  1065. smPrevResponder = NULL;
  1066. //if this control is tabable, set the global
  1067. if (mProfile->mTabable)
  1068. smPrevResponder = this;
  1069. iterator i;
  1070. for (i = begin(); i != end(); i++)
  1071. {
  1072. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  1073. ctrl->findLastTabable(false);
  1074. }
  1075. //after the entire tree has been traversed, return the last responder found
  1076. mFirstResponder = smPrevResponder;
  1077. return smPrevResponder;
  1078. }
  1079. GuiControl *GuiControl::findNextTabable(GuiControl *curResponder, bool firstCall)
  1080. {
  1081. //if this is the first call, clear the global
  1082. if (firstCall)
  1083. smCurResponder = NULL;
  1084. //first find the current responder
  1085. if (curResponder == this)
  1086. smCurResponder = this;
  1087. //if the first responder has been found, return the very next *tabable* control
  1088. else if ( smCurResponder && mProfile->mTabable && mAwake && mVisible && mActive )
  1089. return( this );
  1090. //loop through, checking each child to see if it is the one that follows the firstResponder
  1091. GuiControl *tabCtrl = NULL;
  1092. iterator i;
  1093. for (i = begin(); i != end(); i++)
  1094. {
  1095. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  1096. tabCtrl = ctrl->findNextTabable(curResponder, false);
  1097. if (tabCtrl) break;
  1098. }
  1099. mFirstResponder = tabCtrl;
  1100. return tabCtrl;
  1101. }
  1102. GuiControl *GuiControl::findPrevTabable(GuiControl *curResponder, bool firstCall)
  1103. {
  1104. if (firstCall)
  1105. smPrevResponder = NULL;
  1106. //if this is the current reponder, return the previous one
  1107. if (curResponder == this)
  1108. return smPrevResponder;
  1109. //else if this is a responder, store it in case the next found is the current responder
  1110. else if ( mProfile->mTabable && mAwake && mVisible && mActive )
  1111. smPrevResponder = this;
  1112. //loop through, checking each child to see if it is the one that follows the firstResponder
  1113. GuiControl *tabCtrl = NULL;
  1114. iterator i;
  1115. for (i = begin(); i != end(); i++)
  1116. {
  1117. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  1118. tabCtrl = ctrl->findPrevTabable(curResponder, false);
  1119. if (tabCtrl) break;
  1120. }
  1121. mFirstResponder = tabCtrl;
  1122. return tabCtrl;
  1123. }
  1124. void GuiControl::onLoseFirstResponder()
  1125. {
  1126. // Since many controls have visual cues when they are the firstResponder...
  1127. setUpdate();
  1128. }
  1129. bool GuiControl::ControlIsChild(GuiControl *child)
  1130. {
  1131. //function returns true if this control, or one of it's children is the child control
  1132. if (child == this)
  1133. return true;
  1134. //loop through, checking each child to see if it is ,or contains, the firstResponder
  1135. iterator i;
  1136. for (i = begin(); i != end(); i++)
  1137. {
  1138. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  1139. if (ctrl->ControlIsChild(child)) return true;
  1140. }
  1141. //not found, therefore false
  1142. return false;
  1143. }
  1144. bool GuiControl::isFirstResponder()
  1145. {
  1146. GuiCanvas *root = getRoot();
  1147. return root && root->getFirstResponder() == this;
  1148. }
  1149. void GuiControl::setFirstResponder( GuiControl* firstResponder )
  1150. {
  1151. if ( firstResponder && firstResponder->mProfile->mCanKeyFocus )
  1152. mFirstResponder = firstResponder;
  1153. GuiControl *parent = getParent();
  1154. if ( parent )
  1155. parent->setFirstResponder( firstResponder );
  1156. }
  1157. void GuiControl::setFirstResponder()
  1158. {
  1159. if ( mAwake && mVisible )
  1160. {
  1161. GuiControl *parent = getParent();
  1162. if (mProfile->mCanKeyFocus == true && parent != NULL )
  1163. {
  1164. parent->setFirstResponder(this);
  1165. // Since many controls have visual cues when they are the firstResponder...
  1166. this->setUpdate();
  1167. }
  1168. }
  1169. }
  1170. void GuiControl::clearFirstResponder()
  1171. {
  1172. GuiControl *parent = this;
  1173. while((parent = parent->getParent()) != NULL)
  1174. {
  1175. if(parent->mFirstResponder == this)
  1176. parent->mFirstResponder = NULL;
  1177. else
  1178. break;
  1179. }
  1180. }
  1181. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //
  1182. void GuiControl::buildAcceleratorMap()
  1183. {
  1184. //add my own accel key
  1185. addAcceleratorKey();
  1186. //add all my childrens keys
  1187. iterator i;
  1188. for(i = begin(); i != end(); i++)
  1189. {
  1190. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  1191. ctrl->buildAcceleratorMap();
  1192. }
  1193. }
  1194. void GuiControl::addAcceleratorKey()
  1195. {
  1196. //see if we have an accelerator
  1197. if (mAcceleratorKey == StringTable->EmptyString)
  1198. return;
  1199. EventDescriptor accelEvent;
  1200. ActionMap::createEventDescriptor(mAcceleratorKey, &accelEvent);
  1201. //now we have a modifier, and a key, add them to the canvas
  1202. GuiCanvas *root = getRoot();
  1203. if (root)
  1204. root->addAcceleratorKey(this, 0, accelEvent.eventCode, accelEvent.flags);
  1205. }
  1206. void GuiControl::acceleratorKeyPress(U32 index)
  1207. {
  1208. onAction();
  1209. }
  1210. void GuiControl::acceleratorKeyRelease(U32 index)
  1211. {
  1212. //do nothing
  1213. }
  1214. bool GuiControl::onKeyDown(const GuiEvent &event)
  1215. {
  1216. //pass the event to the parent
  1217. GuiControl *parent = getParent();
  1218. if (parent)
  1219. return parent->onKeyDown(event);
  1220. else
  1221. return false;
  1222. }
  1223. bool GuiControl::onKeyRepeat(const GuiEvent &event)
  1224. {
  1225. // default to just another key down.
  1226. return onKeyDown(event);
  1227. }
  1228. bool GuiControl::onKeyUp(const GuiEvent &event)
  1229. {
  1230. //pass the event to the parent
  1231. GuiControl *parent = getParent();
  1232. if (parent)
  1233. return parent->onKeyUp(event);
  1234. else
  1235. return false;
  1236. }
  1237. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //
  1238. void GuiControl::onAction()
  1239. {
  1240. if (! mActive)
  1241. return;
  1242. //execute the console command
  1243. if (mConsoleCommand && mConsoleCommand[0])
  1244. {
  1245. execConsoleCallback();
  1246. }
  1247. else
  1248. Con::executef(this, 1, "onAction");
  1249. }
  1250. void GuiControl::onMessage(GuiControl *sender, S32 msg)
  1251. {
  1252. }
  1253. void GuiControl::messageSiblings(S32 message)
  1254. {
  1255. GuiControl *parent = getParent();
  1256. if (! parent) return;
  1257. GuiControl::iterator i;
  1258. for(i = parent->begin(); i != parent->end(); i++)
  1259. {
  1260. GuiControl *ctrl = dynamic_cast<GuiControl *>(*i);
  1261. if (ctrl != this)
  1262. ctrl->onMessage(this, message);
  1263. }
  1264. }
  1265. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //
  1266. void GuiControl::onDialogPush()
  1267. {
  1268. // Notify Script.
  1269. if( isMethod("onDialogPush") )
  1270. Con::executef(this, 1, "onDialogPush");
  1271. }
  1272. void GuiControl::onDialogPop()
  1273. {
  1274. // Notify Script.
  1275. if( isMethod("onDialogPop") )
  1276. Con::executef(this, 1, "onDialogPop");
  1277. }
  1278. //------------------------------------------------------------------------------
  1279. void GuiControl::setVisible(bool value)
  1280. {
  1281. mVisible = value;
  1282. iterator i;
  1283. setUpdate();
  1284. for(i = begin(); i != end(); i++)
  1285. {
  1286. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  1287. ctrl->clearFirstResponder();
  1288. }
  1289. GuiControl *parent = getParent();
  1290. if (parent)
  1291. parent->childResized(this);
  1292. }
  1293. void GuiControl::makeFirstResponder(bool value)
  1294. {
  1295. if ( value )
  1296. //setFirstResponder(this);
  1297. setFirstResponder();
  1298. else
  1299. clearFirstResponder();
  1300. }
  1301. void GuiControl::setActive( bool value )
  1302. {
  1303. mActive = value;
  1304. if ( !mActive )
  1305. clearFirstResponder();
  1306. if ( mVisible && mAwake )
  1307. setUpdate();
  1308. }
  1309. void GuiControl::getScrollLineSizes(U32 *rowHeight, U32 *columnWidth)
  1310. {
  1311. // default to 10 pixels in y, 30 pixels in x
  1312. *columnWidth = 30;
  1313. *rowHeight = 30;
  1314. }
  1315. void GuiControl::renderText(Point2I offset, Point2I extent, const char *text, GuiControlProfile *profile, TextRotationOptions rot)
  1316. {
  1317. GFont *font = profile->mFont;
  1318. S32 textWidth = font->getStrWidth((const UTF8*)text);
  1319. S32 textHeight = font->getHeight();
  1320. S32 totalWidth = extent.x;
  1321. S32 totalHeight = extent.y;
  1322. if (rot != tRotateNone)
  1323. {
  1324. totalWidth = extent.y;
  1325. totalHeight = extent.x;
  1326. }
  1327. Point2I startOffset = Point2I(0,0);
  1328. // align the horizontal
  1329. if(textWidth < totalWidth)
  1330. {
  1331. if (profile->mAlignment == GuiControlProfile::RightAlign)
  1332. {
  1333. startOffset.x = totalWidth - textWidth;
  1334. }
  1335. else if (profile->mAlignment == GuiControlProfile::CenterAlign)
  1336. {
  1337. startOffset.x = (totalWidth - textWidth) / 2;
  1338. }
  1339. }
  1340. // align the vertical
  1341. if (textHeight < totalHeight)
  1342. {
  1343. if (profile->mVAlignment == GuiControlProfile::MiddleVAlign)
  1344. {
  1345. startOffset.y = (totalHeight - textHeight) / 2;
  1346. }
  1347. else if (profile->mVAlignment == GuiControlProfile::BottomVAlign)
  1348. {
  1349. startOffset.y = (totalHeight - textHeight);
  1350. }
  1351. }
  1352. else
  1353. {
  1354. startOffset.y = -((textHeight - totalHeight) / 2);
  1355. }
  1356. Point2I start = Point2I(0, 0);
  1357. F32 rotation;
  1358. if (rot == tRotateNone)
  1359. {
  1360. start += startOffset;
  1361. rotation = 0.0f;
  1362. }
  1363. else if (rot == tRotateLeft)
  1364. {
  1365. start.x = startOffset.y;
  1366. start.y = extent.y + startOffset.x;
  1367. rotation = 90.0f;
  1368. }
  1369. else if (rot == tRotateRight)
  1370. {
  1371. start.x = extent.x - startOffset.y;
  1372. start.y = startOffset.x;
  1373. rotation = -90.0f;
  1374. }
  1375. dglDrawText( font, start + offset + profile->mTextOffset, text, profile->mFontColors, 9, rotation );
  1376. }
  1377. void GuiControl::getCursor(GuiCursor *&cursor, bool &showCursor, const GuiEvent &lastGuiEvent)
  1378. {
  1379. if(GuiControl::smCursorChanged != -1 && !isMouseLocked())
  1380. {
  1381. // We've already changed the cursor,
  1382. // so set it back before we change it again.
  1383. Input::popCursor();
  1384. // We haven't changed it
  1385. GuiControl::smCursorChanged = -1;
  1386. }
  1387. }
  1388. const char* GuiControl::execConsoleCallback()
  1389. {
  1390. if (mConsoleCommand && mConsoleCommand[0])
  1391. {
  1392. Con::setVariable("$ThisControl", avar("%d",getId()));
  1393. return Con::evaluate(mConsoleCommand, false);
  1394. }
  1395. return "";
  1396. }
  1397. const char* GuiControl::execAltConsoleCallback()
  1398. {
  1399. if(mAltConsoleCommand && mAltConsoleCommand[0])
  1400. {
  1401. Con::setVariable("$ThisControl", avar("%d",getId()));
  1402. return Con::evaluate(mAltConsoleCommand, false);
  1403. }
  1404. return "";
  1405. }
  1406. void GuiControl::setText(const char *text)
  1407. {
  1408. mText = StringTable->insert(text);
  1409. }
  1410. void GuiControl::setTextID(const char *id)
  1411. {
  1412. S32 n = Con::getIntVariable(id, -1);
  1413. if (n != -1)
  1414. {
  1415. mTextID = StringTable->insert(id);
  1416. setTextID(n);
  1417. }
  1418. }
  1419. void GuiControl::setTextID(S32 id)
  1420. {
  1421. const UTF8 *str = getGUIString(id);
  1422. if (str)
  1423. setText((const char*)str);
  1424. }
  1425. const char *GuiControl::getText()
  1426. {
  1427. return mText;
  1428. }