Control.cpp 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898
  1. #include "Base.h"
  2. #include "Game.h"
  3. #include "Control.h"
  4. #include "Form.h"
  5. #include "Theme.h"
  6. namespace gameplay
  7. {
  8. Control::Control()
  9. : _id(""), _boundsBits(0), _dirtyBits(DIRTY_BOUNDS | DIRTY_STATE), _consumeInputEvents(true), _alignment(ALIGN_TOP_LEFT),
  10. _autoSize(AUTO_SIZE_BOTH), _listeners(NULL), _style(NULL), _visible(true), _opacity(0.0f), _zIndex(-1),
  11. _contactIndex(INVALID_CONTACT_INDEX), _focusIndex(-1), _canFocus(false), _state(NORMAL), _parent(NULL), _styleOverridden(false), _skin(NULL)
  12. {
  13. GP_REGISTER_SCRIPT_EVENTS();
  14. }
  15. Control::~Control()
  16. {
  17. Form::verifyRemovedControlState(this);
  18. if (_listeners)
  19. {
  20. for (std::map<Control::Listener::EventType, std::list<Control::Listener*>*>::const_iterator itr = _listeners->begin(); itr != _listeners->end(); ++itr)
  21. {
  22. std::list<Control::Listener*>* list = itr->second;
  23. SAFE_DELETE(list);
  24. }
  25. SAFE_DELETE(_listeners);
  26. }
  27. if (_style)
  28. {
  29. // Release the style's theme since we addRef'd it in initialize()
  30. _style->getTheme()->release();
  31. if (_styleOverridden)
  32. {
  33. SAFE_DELETE(_style);
  34. }
  35. }
  36. }
  37. Control::AutoSize Control::parseAutoSize(const char* str)
  38. {
  39. if (str == NULL)
  40. return _autoSize;
  41. if (strcmpnocase(str, "AUTO_SIZE_WIDTH") == 0 )
  42. return AUTO_SIZE_WIDTH;
  43. if (strcmpnocase(str, "AUTO_SIZE_HEIGHT") == 0)
  44. return AUTO_SIZE_HEIGHT;
  45. if (strcmpnocase(str, "AUTO_SIZE_BOTH") == 0)
  46. return AUTO_SIZE_BOTH;
  47. return _autoSize;
  48. }
  49. void Control::initialize(const char* typeName, Theme::Style* style, Properties* properties)
  50. {
  51. // Load our theme style
  52. if (properties)
  53. {
  54. // The style passed is in our parent control's style.
  55. // Attempt to load our own style from our parent style's theme.
  56. const char* styleName = properties->getString("style", typeName);
  57. if (style)
  58. {
  59. // The passed in style is our parent control's style : attempt to load our style from it.
  60. _style = style->getTheme()->getStyle(styleName);
  61. }
  62. if (!_style)
  63. {
  64. // Use an empty style from our parent's theme
  65. _style = style->getTheme()->getEmptyStyle();
  66. }
  67. }
  68. else
  69. {
  70. // No properties passed in - the style passed in was explicity for us.
  71. _style = style;
  72. }
  73. if (!_style)
  74. {
  75. // Search for a style from the default theme that matches this control's name
  76. _style = Theme::getDefault()->getStyle(typeName);
  77. if (!_style)
  78. {
  79. // No style was found, use an empty style
  80. _style = style ? style->getTheme()->getEmptyStyle() : Theme::getDefault()->getEmptyStyle();
  81. }
  82. }
  83. // Increase the reference count of the style's theme while we hold the style
  84. _style->getTheme()->addRef();
  85. if (properties)
  86. {
  87. const char* id = properties->getId();
  88. if (id)
  89. _id = id;
  90. // Properties not defined by the style.
  91. const char* alignmentString = properties->getString("alignment");
  92. _alignment = getAlignment(alignmentString);
  93. _consumeInputEvents = properties->getBool("consumeInputEvents", true);
  94. _visible = properties->getBool("visible", true);
  95. if (properties->exists("zIndex"))
  96. {
  97. _zIndex = properties->getInt("zIndex");
  98. }
  99. else
  100. {
  101. _zIndex = -1;
  102. }
  103. if (properties->exists("canFocus"))
  104. _canFocus = properties->getBool("canFocus", false);
  105. if (properties->exists("focusIndex"))
  106. {
  107. _focusIndex = properties->getInt("focusIndex");
  108. }
  109. else
  110. {
  111. _focusIndex = -1;
  112. }
  113. float bounds[2];
  114. bool boundsBits[2];
  115. const char* position = properties->getString("position");
  116. if (position && parseCoordPair(position, &bounds[0], &bounds[1], &boundsBits[0], &boundsBits[1]))
  117. {
  118. setX(bounds[0], boundsBits[0]);
  119. setY(bounds[1], boundsBits[1]);
  120. }
  121. else
  122. {
  123. if (properties->exists("x"))
  124. {
  125. bounds[0] = parseCoord(properties->getString("x", "0"), &boundsBits[0]);
  126. setX(bounds[0], boundsBits[0]);
  127. }
  128. if (properties->exists("y"))
  129. {
  130. bounds[1] = parseCoord(properties->getString("y", "0"), &boundsBits[1]);
  131. setY(bounds[1], boundsBits[1]);
  132. }
  133. }
  134. // If there is an explicitly specified size, width or height, unset the corresponding autoSize bit
  135. const char* size = properties->getString("size");
  136. if (size && parseCoordPair(size, &bounds[0], &bounds[1], &boundsBits[0], &boundsBits[1]))
  137. {
  138. setWidth(bounds[0], boundsBits[0]);
  139. setHeight(bounds[1], boundsBits[1]);
  140. }
  141. else
  142. {
  143. const char* width = properties->getString("width");
  144. if (width)
  145. {
  146. bounds[0] = parseCoord(width, &boundsBits[0]);
  147. setWidth(bounds[0], boundsBits[0]);
  148. }
  149. const char* height = properties->getString("height");
  150. if (height)
  151. {
  152. bounds[1] = parseCoord(height, &boundsBits[1]);
  153. setHeight(bounds[1], boundsBits[1]);
  154. }
  155. }
  156. // Backwards Compatibility: Support deprecated autoWidth and autoHeight properties,
  157. // which resolve to width=100% and height=100%.
  158. if (properties->getBool("autoWidth"))
  159. setWidth(1.0f, true);
  160. if (properties->getBool("autoHeight"))
  161. setHeight(1.0f, true);
  162. // Parse the auto-size mode for the control (this overrides explicit sizes and legacy autoWidth/autoHeight)
  163. _autoSize = parseAutoSize(properties->getString("autoSize"));
  164. // If there is are simple padding or margin variables, parse them
  165. if (properties->exists("padding"))
  166. {
  167. float pad = properties->getFloat("padding");
  168. setPadding(pad, pad, pad, pad);
  169. }
  170. if (properties->exists("margin"))
  171. {
  172. float margin = properties->getFloat("margin");
  173. setPadding(margin, margin, margin, margin);
  174. }
  175. if (properties->exists("enabled"))
  176. {
  177. setEnabled(properties->getBool("enabled"));
  178. }
  179. // Register script listeners for control events
  180. if (properties->exists("script"))
  181. addScript(properties->getString("script"));
  182. // Potentially override themed properties for all states.
  183. overrideThemedProperties(properties, STATE_ALL);
  184. // Override themed properties on specific states.
  185. Properties* innerSpace = properties->getNextNamespace();
  186. while (innerSpace != NULL)
  187. {
  188. std::string spaceName(innerSpace->getNamespace());
  189. std::transform(spaceName.begin(), spaceName.end(), spaceName.begin(), (int(*)(int))toupper);
  190. if (spaceName == "STATENORMAL")
  191. {
  192. overrideThemedProperties(innerSpace, NORMAL);
  193. }
  194. else if (spaceName == "STATEFOCUS")
  195. {
  196. overrideThemedProperties(innerSpace, FOCUS);
  197. }
  198. else if (spaceName == "STATEACTIVE")
  199. {
  200. overrideThemedProperties(innerSpace, ACTIVE);
  201. }
  202. else if (spaceName == "STATEDISABLED")
  203. {
  204. overrideThemedProperties(innerSpace, DISABLED);
  205. }
  206. else if (spaceName == "STATEHOVER")
  207. {
  208. overrideThemedProperties(innerSpace, HOVER);
  209. }
  210. else if (spaceName == "MARGIN")
  211. {
  212. setMargin(innerSpace->getFloat("top"), innerSpace->getFloat("bottom"),
  213. innerSpace->getFloat("left"), innerSpace->getFloat("right"));
  214. }
  215. else if (spaceName == "PADDING")
  216. {
  217. setPadding(innerSpace->getFloat("top"), innerSpace->getFloat("bottom"),
  218. innerSpace->getFloat("left"), innerSpace->getFloat("right"));
  219. }
  220. innerSpace = properties->getNextNamespace();
  221. }
  222. }
  223. }
  224. const char* Control::getTypeName() const
  225. {
  226. return "Control";
  227. }
  228. const char* Control::getId() const
  229. {
  230. return _id.c_str();
  231. }
  232. void Control::setId(const char* id)
  233. {
  234. _id = id ? id : "";
  235. }
  236. float Control::getX() const
  237. {
  238. return _bounds.x;
  239. }
  240. void Control::setX(float x, bool percentage)
  241. {
  242. if (_relativeBounds.x != x || percentage != ((_boundsBits & BOUNDS_X_PERCENTAGE_BIT) != 0))
  243. {
  244. setXInternal(x, percentage);
  245. setDirty(DIRTY_BOUNDS);
  246. }
  247. }
  248. void Control::setXInternal(float x, bool percentage)
  249. {
  250. _relativeBounds.x = x;
  251. if (percentage)
  252. {
  253. _boundsBits |= BOUNDS_X_PERCENTAGE_BIT;
  254. }
  255. else
  256. {
  257. _boundsBits &= ~BOUNDS_X_PERCENTAGE_BIT;
  258. _bounds.x = x;
  259. }
  260. }
  261. bool Control::isXPercentage() const
  262. {
  263. return (_boundsBits & BOUNDS_X_PERCENTAGE_BIT) != 0;
  264. }
  265. float Control::getY() const
  266. {
  267. return _bounds.y;
  268. }
  269. void Control::setY(float y, bool percentage)
  270. {
  271. if (_relativeBounds.y != y || percentage != ((_boundsBits & BOUNDS_Y_PERCENTAGE_BIT) != 0))
  272. {
  273. setYInternal(y, percentage);
  274. setDirty(DIRTY_BOUNDS);
  275. }
  276. }
  277. void Control::setYInternal(float y, bool percentage)
  278. {
  279. _relativeBounds.y = y;
  280. if (percentage)
  281. {
  282. _boundsBits |= BOUNDS_Y_PERCENTAGE_BIT;
  283. }
  284. else
  285. {
  286. _boundsBits &= ~BOUNDS_Y_PERCENTAGE_BIT;
  287. _bounds.y = y;
  288. }
  289. }
  290. bool Control::isYPercentage() const
  291. {
  292. return (_boundsBits & BOUNDS_Y_PERCENTAGE_BIT) != 0;
  293. }
  294. float Control::getWidth() const
  295. {
  296. return _bounds.width;
  297. }
  298. void Control::setWidth(float width, bool percentage)
  299. {
  300. _autoSize = (AutoSize)(_autoSize & ~AUTO_SIZE_WIDTH);
  301. if (_relativeBounds.width != width || percentage != ((_boundsBits & BOUNDS_WIDTH_PERCENTAGE_BIT) != 0))
  302. {
  303. setWidthInternal(width, percentage);
  304. setDirty(DIRTY_BOUNDS);
  305. }
  306. }
  307. void Control::setWidthInternal(float width, bool percentage)
  308. {
  309. _relativeBounds.width = width;
  310. if (percentage)
  311. {
  312. _boundsBits |= BOUNDS_WIDTH_PERCENTAGE_BIT;
  313. }
  314. else
  315. {
  316. _boundsBits &= ~BOUNDS_WIDTH_PERCENTAGE_BIT;
  317. _bounds.width = width;
  318. }
  319. }
  320. bool Control::isWidthPercentage() const
  321. {
  322. return (_boundsBits & BOUNDS_WIDTH_PERCENTAGE_BIT) != 0;
  323. }
  324. float Control::getHeight() const
  325. {
  326. return _bounds.height;
  327. }
  328. void Control::setHeight(float height, bool percentage)
  329. {
  330. _autoSize = (AutoSize)(_autoSize & ~AUTO_SIZE_HEIGHT);
  331. if (_relativeBounds.height != height || percentage != ((_boundsBits & BOUNDS_HEIGHT_PERCENTAGE_BIT) != 0))
  332. {
  333. setHeightInternal(height, percentage);
  334. setDirty(DIRTY_BOUNDS);
  335. }
  336. }
  337. void Control::setHeightInternal(float height, bool percentage)
  338. {
  339. _relativeBounds.height = height;
  340. if (percentage)
  341. {
  342. _boundsBits |= BOUNDS_HEIGHT_PERCENTAGE_BIT;
  343. }
  344. else
  345. {
  346. _boundsBits &= ~BOUNDS_HEIGHT_PERCENTAGE_BIT;
  347. _bounds.height = height;
  348. }
  349. }
  350. bool Control::isHeightPercentage() const
  351. {
  352. return (_boundsBits & BOUNDS_HEIGHT_PERCENTAGE_BIT) != 0;
  353. }
  354. void Control::setPosition(float x, float y)
  355. {
  356. setX(x);
  357. setY(y);
  358. }
  359. void Control::setSize(float width, float height)
  360. {
  361. setWidth(width);
  362. setHeight(height);
  363. }
  364. const Rectangle& Control::getBounds() const
  365. {
  366. return _bounds;
  367. }
  368. void Control::setBounds(const Rectangle& bounds)
  369. {
  370. setX(bounds.x);
  371. setY(bounds.y);
  372. setWidth(bounds.width);
  373. setHeight(bounds.height);
  374. }
  375. const Rectangle& Control::getAbsoluteBounds() const
  376. {
  377. return _absoluteBounds;
  378. }
  379. const Rectangle& Control::getClipBounds() const
  380. {
  381. return _clipBounds;
  382. }
  383. const Rectangle& Control::getClip() const
  384. {
  385. return _viewportClipBounds;
  386. }
  387. void Control::setAlignment(Alignment alignment)
  388. {
  389. if (_alignment != alignment)
  390. {
  391. _alignment = alignment;
  392. setDirty(DIRTY_BOUNDS);
  393. }
  394. }
  395. Control::Alignment Control::getAlignment() const
  396. {
  397. return _alignment;
  398. }
  399. Control::AutoSize Control::getAutoSize() const
  400. {
  401. return _autoSize;
  402. }
  403. void Control::setAutoSize(AutoSize mode)
  404. {
  405. if (_autoSize != mode)
  406. {
  407. _autoSize = mode;
  408. setDirty(DIRTY_BOUNDS);
  409. }
  410. }
  411. void Control::setVisible(bool visible)
  412. {
  413. if (_visible != visible)
  414. {
  415. _visible = visible;
  416. if (!_visible)
  417. Form::controlDisabled(this);
  418. setDirty(DIRTY_BOUNDS);
  419. // force to update parent boundaries when child is hidden
  420. Control* parent = _parent;
  421. while (parent && (parent->_autoSize != AUTO_SIZE_NONE || static_cast<Container *>(parent)->getLayout()->getType() != Layout::LAYOUT_ABSOLUTE))
  422. {
  423. parent->setDirty(DIRTY_BOUNDS);
  424. parent = parent->_parent;
  425. }
  426. }
  427. }
  428. bool Control::isVisible() const
  429. {
  430. return _visible;
  431. }
  432. bool Control::isVisibleInHierarchy() const
  433. {
  434. if (!_visible)
  435. return false;
  436. if (_parent)
  437. return _parent->isVisibleInHierarchy();
  438. return true;
  439. }
  440. bool Control::canFocus() const
  441. {
  442. return _canFocus;
  443. }
  444. void Control::setCanFocus(bool acceptsFocus)
  445. {
  446. _canFocus = acceptsFocus;
  447. }
  448. bool Control::hasFocus() const
  449. {
  450. return (Form::getFocusControl() == this);
  451. }
  452. bool Control::setFocus()
  453. {
  454. if (Form::getFocusControl() != this && canFocus())
  455. {
  456. Form::setFocusControl(this);
  457. return true;
  458. }
  459. return false;
  460. }
  461. void Control::setOpacity(float opacity, unsigned char states)
  462. {
  463. overrideStyle();
  464. Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
  465. getOverlays(states, overlays);
  466. for (int i = 0; i < Theme::Style::OVERLAY_MAX; ++i)
  467. {
  468. if (overlays[i])
  469. overlays[i]->setOpacity(opacity);
  470. }
  471. }
  472. float Control::getOpacity(State state) const
  473. {
  474. Theme::Style::Overlay* overlay = getOverlay(state);
  475. GP_ASSERT(overlay);
  476. return overlay->getOpacity();
  477. }
  478. void Control::setEnabled(bool enabled)
  479. {
  480. if (enabled != isEnabled())
  481. {
  482. if (!enabled)
  483. Form::controlDisabled(this);
  484. _state = enabled ? NORMAL : DISABLED;
  485. setDirty(DIRTY_STATE);
  486. }
  487. }
  488. bool Control::isEnabled() const
  489. {
  490. return (_state != DISABLED);
  491. }
  492. bool Control::isEnabledInHierarchy() const
  493. {
  494. if (!isEnabled())
  495. return false;
  496. if (_parent)
  497. return _parent->isEnabledInHierarchy();
  498. return true;
  499. }
  500. void Control::setBorder(float top, float bottom, float left, float right, unsigned char states)
  501. {
  502. overrideStyle();
  503. Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
  504. getOverlays(states, overlays);
  505. for (int i = 0; i < Theme::Style::OVERLAY_MAX; ++i)
  506. {
  507. if (overlays[i])
  508. overlays[i]->setBorder(top, bottom, left, right);
  509. }
  510. setDirty(DIRTY_BOUNDS);
  511. }
  512. const Theme::Border& Control::getBorder(State state) const
  513. {
  514. Theme::Style::Overlay* overlay = getOverlay(state);
  515. GP_ASSERT(overlay);
  516. return overlay->getBorder();
  517. }
  518. void Control::setSkinRegion(const Rectangle& region, unsigned char states)
  519. {
  520. overrideStyle();
  521. Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
  522. getOverlays(states, overlays);
  523. for (int i = 0; i < Theme::Style::OVERLAY_MAX; ++i)
  524. {
  525. if( overlays[i] )
  526. overlays[i]->setSkinRegion(region, _style->_tw, _style->_th);
  527. }
  528. }
  529. const Rectangle& Control::getSkinRegion(State state) const
  530. {
  531. Theme::Style::Overlay* overlay = getOverlay(state);
  532. GP_ASSERT(overlay);
  533. return overlay->getSkinRegion();
  534. }
  535. void Control::setSkinColor(const Vector4& color, unsigned char states)
  536. {
  537. overrideStyle();
  538. Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
  539. getOverlays(states, overlays);
  540. for (int i = 0; i < Theme::Style::OVERLAY_MAX; ++i)
  541. {
  542. if( overlays[i] )
  543. overlays[i]->setSkinColor(color);
  544. }
  545. }
  546. const Vector4& Control::getSkinColor(State state) const
  547. {
  548. Theme::Style::Overlay* overlay = getOverlay(state);
  549. GP_ASSERT(overlay);
  550. return overlay->getSkinColor();
  551. }
  552. void Control::setMargin(float top, float bottom, float left, float right)
  553. {
  554. GP_ASSERT(_style);
  555. overrideStyle();
  556. _style->setMargin(top, bottom, left, right);
  557. setDirty(DIRTY_BOUNDS);
  558. }
  559. const Theme::Margin& Control::getMargin() const
  560. {
  561. GP_ASSERT(_style);
  562. return _style->getMargin();
  563. }
  564. void Control::setPadding(float top, float bottom, float left, float right)
  565. {
  566. GP_ASSERT(_style);
  567. overrideStyle();
  568. _style->setPadding(top, bottom, left, right);
  569. setDirty(DIRTY_BOUNDS);
  570. }
  571. const Theme::Padding& Control::getPadding() const
  572. {
  573. GP_ASSERT(_style);
  574. return _style->getPadding();
  575. }
  576. void Control::setImageRegion(const char* id, const Rectangle& region, unsigned char states)
  577. {
  578. overrideStyle();
  579. Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
  580. getOverlays(states, overlays);
  581. for (int i = 0; i < Theme::Style::OVERLAY_MAX; ++i)
  582. {
  583. if( overlays[i] )
  584. overlays[i]->setImageRegion(id, region, _style->_tw, _style->_th);
  585. }
  586. }
  587. const Rectangle& Control::getImageRegion(const char* id, State state) const
  588. {
  589. Theme::Style::Overlay* overlay = getOverlay(state);
  590. GP_ASSERT(overlay);
  591. return overlay->getImageRegion(id);
  592. }
  593. void Control::setImageColor(const char* id, const Vector4& color, unsigned char states)
  594. {
  595. overrideStyle();
  596. Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
  597. getOverlays(states, overlays);
  598. for (int i = 0; i < Theme::Style::OVERLAY_MAX; ++i)
  599. {
  600. if( overlays[i] )
  601. overlays[i]->setImageColor(id, color);
  602. }
  603. }
  604. const Vector4& Control::getImageColor(const char* id, State state) const
  605. {
  606. Theme::Style::Overlay* overlay = getOverlay(state);
  607. GP_ASSERT(overlay);
  608. return overlay->getImageColor(id);
  609. }
  610. const Theme::UVs& Control::getImageUVs(const char* id, State state) const
  611. {
  612. Theme::Style::Overlay* overlay = getOverlay(state);
  613. GP_ASSERT(overlay);
  614. return overlay->getImageUVs(id);
  615. }
  616. void Control::setCursorRegion(const Rectangle& region, unsigned char states)
  617. {
  618. overrideStyle();
  619. Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
  620. getOverlays(states, overlays);
  621. for (int i = 0; i < Theme::Style::OVERLAY_MAX; ++i)
  622. {
  623. if( overlays[i] )
  624. overlays[i]->setCursorRegion(region, _style->_tw, _style->_th);
  625. }
  626. }
  627. const Rectangle& Control::getCursorRegion(State state) const
  628. {
  629. Theme::Style::Overlay* overlay = getOverlay(state);
  630. GP_ASSERT(overlay);
  631. return overlay->getCursorRegion();
  632. }
  633. void Control::setCursorColor(const Vector4& color, unsigned char states)
  634. {
  635. overrideStyle();
  636. Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
  637. getOverlays(states, overlays);
  638. for (int i = 0; i < Theme::Style::OVERLAY_MAX; ++i)
  639. {
  640. if( overlays[i] )
  641. overlays[i]->setCursorColor(color);
  642. }
  643. }
  644. const Vector4& Control::getCursorColor(State state)
  645. {
  646. Theme::Style::Overlay* overlay = getOverlay(state);
  647. GP_ASSERT(overlay);
  648. return overlay->getCursorColor();
  649. }
  650. const Theme::UVs& Control::getCursorUVs(State state)
  651. {
  652. Theme::Style::Overlay* overlay = getOverlay(state);
  653. GP_ASSERT(overlay);
  654. return overlay->getCursorUVs();
  655. }
  656. void Control::setFont(Font* font, unsigned char states)
  657. {
  658. overrideStyle();
  659. Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
  660. getOverlays(states, overlays);
  661. for (int i = 0; i < Theme::Style::OVERLAY_MAX; ++i)
  662. {
  663. if( overlays[i] )
  664. overlays[i]->setFont(font);
  665. }
  666. if (_autoSize != AUTO_SIZE_NONE)
  667. setDirty(DIRTY_BOUNDS);
  668. }
  669. Font* Control::getFont(State state) const
  670. {
  671. Theme::Style::Overlay* overlay = getOverlay(state);
  672. GP_ASSERT(overlay);
  673. return overlay->getFont();
  674. }
  675. void Control::setFontSize(unsigned int fontSize, unsigned char states)
  676. {
  677. overrideStyle();
  678. Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
  679. getOverlays(states, overlays);
  680. for (int i = 0; i < Theme::Style::OVERLAY_MAX; ++i)
  681. {
  682. if( overlays[i] )
  683. overlays[i]->setFontSize(fontSize);
  684. }
  685. if (_autoSize != AUTO_SIZE_NONE)
  686. setDirty(DIRTY_BOUNDS);
  687. }
  688. unsigned int Control::getFontSize(State state) const
  689. {
  690. Theme::Style::Overlay* overlay = getOverlay(state);
  691. GP_ASSERT(overlay);
  692. return overlay->getFontSize();
  693. }
  694. void Control::setTextColor(const Vector4& color, unsigned char states)
  695. {
  696. overrideStyle();
  697. Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
  698. getOverlays(states, overlays);
  699. for (int i = 0; i < Theme::Style::OVERLAY_MAX; ++i)
  700. {
  701. if( overlays[i] )
  702. overlays[i]->setTextColor(color);
  703. }
  704. }
  705. const Vector4& Control::getTextColor(State state) const
  706. {
  707. Theme::Style::Overlay* overlay = getOverlay(state);
  708. GP_ASSERT(overlay);
  709. return overlay->getTextColor();
  710. }
  711. void Control::setTextAlignment(Font::Justify alignment, unsigned char states)
  712. {
  713. overrideStyle();
  714. Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
  715. getOverlays(states, overlays);
  716. for (int i = 0; i < Theme::Style::OVERLAY_MAX; ++i)
  717. {
  718. if( overlays[i] )
  719. overlays[i]->setTextAlignment(alignment);
  720. }
  721. }
  722. Font::Justify Control::getTextAlignment(State state) const
  723. {
  724. Theme::Style::Overlay* overlay = getOverlay(state);
  725. GP_ASSERT(overlay);
  726. return overlay->getTextAlignment();
  727. }
  728. void Control::setTextRightToLeft(bool rightToLeft, unsigned char states)
  729. {
  730. overrideStyle();
  731. Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
  732. getOverlays(states, overlays);
  733. for (int i = 0; i < Theme::Style::OVERLAY_MAX; ++i)
  734. {
  735. if( overlays[i] )
  736. overlays[i]->setTextRightToLeft(rightToLeft);
  737. }
  738. }
  739. bool Control::getTextRightToLeft(State state) const
  740. {
  741. Theme::Style::Overlay* overlay = getOverlay(state);
  742. GP_ASSERT(overlay);
  743. return overlay->getTextRightToLeft();
  744. }
  745. Theme::Style* Control::getStyle() const
  746. {
  747. return _style;
  748. }
  749. void Control::setStyle(Theme::Style* style)
  750. {
  751. if (style != _style)
  752. {
  753. _style = style;
  754. setDirty(DIRTY_BOUNDS);
  755. }
  756. }
  757. Theme* Control::getTheme() const
  758. {
  759. return _style ? _style->getTheme() : NULL;
  760. }
  761. Control::State Control::getState() const
  762. {
  763. if (Form::getFocusControl() == this)
  764. {
  765. // Active is the only state that overrides focus state
  766. return _state == ACTIVE ? ACTIVE : FOCUS;
  767. }
  768. return _state;
  769. }
  770. Theme::Style::OverlayType Control::getOverlayType() const
  771. {
  772. switch (getState())
  773. {
  774. case Control::NORMAL:
  775. return Theme::Style::OVERLAY_NORMAL;
  776. case Control::FOCUS:
  777. return Theme::Style::OVERLAY_FOCUS;
  778. case Control::ACTIVE:
  779. return Theme::Style::OVERLAY_ACTIVE;
  780. case Control::DISABLED:
  781. return Theme::Style::OVERLAY_DISABLED;
  782. case Control::HOVER:
  783. return Theme::Style::OVERLAY_HOVER;
  784. default:
  785. return Theme::Style::OVERLAY_NORMAL;
  786. }
  787. }
  788. void Control::setConsumeInputEvents(bool consume)
  789. {
  790. _consumeInputEvents = consume;
  791. }
  792. bool Control::getConsumeInputEvents()
  793. {
  794. return _consumeInputEvents;
  795. }
  796. int Control::getZIndex() const
  797. {
  798. return _zIndex;
  799. }
  800. void Control::setZIndex(int zIndex)
  801. {
  802. if (zIndex != _zIndex)
  803. {
  804. _zIndex = zIndex;
  805. if (_parent)
  806. {
  807. _parent->sortControls();
  808. }
  809. }
  810. }
  811. int Control::getFocusIndex() const
  812. {
  813. return _focusIndex;
  814. }
  815. void Control::setFocusIndex(int focusIndex)
  816. {
  817. _focusIndex = focusIndex;
  818. }
  819. void Control::addListener(Control::Listener* listener, int eventFlags)
  820. {
  821. GP_ASSERT(listener);
  822. if ((eventFlags & Control::Listener::PRESS) == Control::Listener::PRESS)
  823. {
  824. addSpecificListener(listener, Control::Listener::PRESS);
  825. }
  826. if ((eventFlags & Control::Listener::RELEASE) == Control::Listener::RELEASE)
  827. {
  828. addSpecificListener(listener, Control::Listener::RELEASE);
  829. }
  830. if ((eventFlags & Control::Listener::CLICK) == Control::Listener::CLICK)
  831. {
  832. addSpecificListener(listener, Control::Listener::CLICK);
  833. }
  834. if ((eventFlags & Control::Listener::VALUE_CHANGED) == Control::Listener::VALUE_CHANGED)
  835. {
  836. addSpecificListener(listener, Control::Listener::VALUE_CHANGED);
  837. }
  838. if ((eventFlags & Control::Listener::TEXT_CHANGED) == Control::Listener::TEXT_CHANGED)
  839. {
  840. addSpecificListener(listener, Control::Listener::TEXT_CHANGED);
  841. }
  842. }
  843. void Control::removeListener(Control::Listener* listener)
  844. {
  845. if (_listeners == NULL || listener == NULL)
  846. return;
  847. for (std::map<Control::Listener::EventType, std::list<Control::Listener*>*>::iterator itr = _listeners->begin(); itr != _listeners->end();)
  848. {
  849. itr->second->remove(listener);
  850. if(itr->second->empty())
  851. {
  852. std::list<Control::Listener*>* list = itr->second;
  853. _listeners->erase(itr++);
  854. SAFE_DELETE(list);
  855. }
  856. else
  857. ++itr;
  858. }
  859. if (_listeners->empty())
  860. SAFE_DELETE(_listeners);
  861. }
  862. void Control::addSpecificListener(Control::Listener* listener, Control::Listener::EventType eventType)
  863. {
  864. GP_ASSERT(listener);
  865. if (!_listeners)
  866. {
  867. _listeners = new std::map<Control::Listener::EventType, std::list<Control::Listener*>*>();
  868. }
  869. std::map<Control::Listener::EventType, std::list<Control::Listener*>*>::const_iterator itr = _listeners->find(eventType);
  870. if (itr == _listeners->end())
  871. {
  872. _listeners->insert(std::make_pair(eventType, new std::list<Control::Listener*>()));
  873. itr = _listeners->find(eventType);
  874. }
  875. std::list<Control::Listener*>* listenerList = itr->second;
  876. listenerList->push_back(listener);
  877. }
  878. bool Control::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  879. {
  880. return false;
  881. }
  882. bool Control::keyEvent(Keyboard::KeyEvent evt, int key)
  883. {
  884. return false;
  885. }
  886. bool Control::mouseEvent(Mouse::MouseEvent evt, int x, int y, int wheelDelta)
  887. {
  888. // Return false instead of _consumeInputEvents to allow handling to be
  889. // routed to touchEvent before consuming.
  890. return false;
  891. }
  892. bool Control::gamepadButtonEvent(Gamepad* gamepad)
  893. {
  894. return false;
  895. }
  896. bool Control::gamepadTriggerEvent(Gamepad* gamepad, unsigned int index)
  897. {
  898. return false;
  899. }
  900. bool Control::gamepadJoystickEvent(Gamepad* gamepad, unsigned int index)
  901. {
  902. return false;
  903. }
  904. void Control::notifyListeners(Control::Listener::EventType eventType)
  905. {
  906. // This method runs untrusted code by notifying listeners of events.
  907. // If the user calls exit() or otherwise releases this control, we
  908. // need to keep it alive until the method returns.
  909. addRef();
  910. controlEvent(eventType);
  911. if (_listeners)
  912. {
  913. std::map<Control::Listener::EventType, std::list<Control::Listener*>*>::const_iterator itr = _listeners->find(eventType);
  914. if (itr != _listeners->end())
  915. {
  916. std::list<Control::Listener*>* listenerList = itr->second;
  917. for (std::list<Control::Listener*>::iterator listenerItr = listenerList->begin(); listenerItr != listenerList->end(); ++listenerItr)
  918. {
  919. GP_ASSERT(*listenerItr);
  920. (*listenerItr)->controlEvent(this, eventType);
  921. }
  922. }
  923. }
  924. fireScriptEvent<void>(GP_GET_SCRIPT_EVENT(Control, controlEvent), dynamic_cast<void*>(this), eventType);
  925. release();
  926. }
  927. void Control::controlEvent(Control::Listener::EventType evt)
  928. {
  929. }
  930. void Control::setDirty(int bits)
  931. {
  932. _dirtyBits |= bits;
  933. }
  934. bool Control::isDirty(int bit) const
  935. {
  936. return (_dirtyBits & bit) == bit;
  937. }
  938. void Control::update(float elapsedTime)
  939. {
  940. State state = getState();
  941. // Update visual state if it's dirty
  942. if (_dirtyBits & DIRTY_STATE)
  943. updateState(getState());
  944. // Since opacity is pre-multiplied, we compute it every frame so that we don't need to
  945. // dirty the entire hierarchy any time a state changes (which could affect opacity).
  946. _opacity = getOpacity(state);
  947. if (_parent)
  948. _opacity *= _parent->_opacity;
  949. }
  950. void Control::updateState(State state)
  951. {
  952. // Clear dirty state bit
  953. _dirtyBits &= ~DIRTY_STATE;
  954. // Cache themed attributes for performance.
  955. _skin = getSkin(state);
  956. }
  957. bool Control::updateBoundsInternal(const Vector2& offset)
  958. {
  959. // If our state is currently dirty, update it here so that any rendering state objects needed
  960. // for bounds computation are accessible.
  961. State state = getState();
  962. if (_dirtyBits & DIRTY_STATE)
  963. {
  964. updateState(state);
  965. _dirtyBits &= ~DIRTY_STATE;
  966. }
  967. // Clear our dirty bounds bit
  968. bool dirtyBounds = (_dirtyBits & DIRTY_BOUNDS) != 0;
  969. _dirtyBits &= ~DIRTY_BOUNDS;
  970. // If we are a container, always update child bounds first
  971. bool changed = false;
  972. if (isContainer())
  973. changed = static_cast<Container*>(this)->updateChildBounds();
  974. if (dirtyBounds)
  975. {
  976. // Store old bounds so we can determine if they change
  977. Rectangle oldAbsoluteBounds(_absoluteBounds);
  978. Rectangle oldAbsoluteClipBounds(_absoluteClipBounds);
  979. Rectangle oldViewportBounds(_viewportBounds);
  980. Rectangle oldViewportClipBounds(_viewportClipBounds);
  981. updateBounds();
  982. updateAbsoluteBounds(offset);
  983. if (_absoluteBounds != oldAbsoluteBounds ||
  984. _absoluteClipBounds != oldAbsoluteClipBounds ||
  985. _viewportBounds != oldViewportBounds ||
  986. _viewportClipBounds != oldViewportClipBounds)
  987. {
  988. if (isContainer())
  989. static_cast<Container*>(this)->setChildrenDirty(DIRTY_BOUNDS, true);
  990. changed = true;
  991. }
  992. }
  993. return changed;
  994. }
  995. void Control::updateBounds()
  996. {
  997. Game* game = Game::getInstance();
  998. const Rectangle parentAbsoluteBounds = _parent ? _parent->_viewportBounds : Rectangle(0, 0, game->getViewport().width, game->getViewport().height);
  999. // Calculate local unclipped bounds.
  1000. _bounds.set(_relativeBounds);
  1001. if (isXPercentage())
  1002. _bounds.x *= parentAbsoluteBounds.width;
  1003. if (isYPercentage())
  1004. _bounds.y *= parentAbsoluteBounds.height;
  1005. if (isWidthPercentage())
  1006. _bounds.width *= parentAbsoluteBounds.width;
  1007. if (isHeightPercentage())
  1008. _bounds.height *= parentAbsoluteBounds.height;
  1009. // Apply control alignment
  1010. if (_alignment != Control::ALIGN_TOP_LEFT)
  1011. {
  1012. const Theme::Margin& margin = _style->getMargin();
  1013. const Rectangle& parentBounds = _parent ? _parent->getBounds() : Rectangle(0, 0, game->getViewport().width, game->getViewport().height);
  1014. const Theme::Border& parentBorder = _parent ? _parent->getBorder(_parent->getState()) : Theme::Border::empty();
  1015. const Theme::Padding& parentPadding = _parent ? _parent->getPadding() : Theme::Padding::empty();
  1016. float clipWidth, clipHeight;
  1017. if (_parent && (_parent->getScroll() != Container::SCROLL_NONE))
  1018. {
  1019. const Rectangle& verticalScrollBarBounds = _parent->getImageRegion("verticalScrollBar", _parent->getState());
  1020. const Rectangle& horizontalScrollBarBounds = _parent->getImageRegion("horizontalScrollBar", _parent->getState());
  1021. clipWidth = parentBounds.width - parentBorder.left - parentBorder.right - parentPadding.left - parentPadding.right - verticalScrollBarBounds.width;
  1022. clipHeight = parentBounds.height - parentBorder.top - parentBorder.bottom - parentPadding.top - parentPadding.bottom - horizontalScrollBarBounds.height;
  1023. }
  1024. else
  1025. {
  1026. clipWidth = parentBounds.width - parentBorder.left - parentBorder.right - parentPadding.left - parentPadding.right;
  1027. clipHeight = parentBounds.height - parentBorder.top - parentBorder.bottom - parentPadding.top - parentPadding.bottom;
  1028. }
  1029. // Vertical alignment
  1030. if ((_alignment & Control::ALIGN_BOTTOM) == Control::ALIGN_BOTTOM)
  1031. {
  1032. _bounds.y = clipHeight - _bounds.height - margin.bottom;
  1033. }
  1034. else if ((_alignment & Control::ALIGN_VCENTER) == Control::ALIGN_VCENTER)
  1035. {
  1036. _bounds.y = clipHeight * 0.5f - _bounds.height * 0.5f;
  1037. }
  1038. else if ((_alignment & Control::ALIGN_TOP) == Control::ALIGN_TOP)
  1039. {
  1040. _bounds.y = margin.top;
  1041. }
  1042. // Horizontal alignment
  1043. if ((_alignment & Control::ALIGN_RIGHT) == Control::ALIGN_RIGHT)
  1044. {
  1045. _bounds.x = clipWidth - _bounds.width - margin.right;
  1046. }
  1047. else if ((_alignment & Control::ALIGN_HCENTER) == Control::ALIGN_HCENTER)
  1048. {
  1049. _bounds.x = clipWidth * 0.5f - _bounds.width * 0.5f;
  1050. }
  1051. else if ((_alignment & Control::ALIGN_LEFT) == Control::ALIGN_LEFT)
  1052. {
  1053. _bounds.x = margin.left;
  1054. }
  1055. }
  1056. }
  1057. void Control::updateAbsoluteBounds(const Vector2& offset)
  1058. {
  1059. Game* game = Game::getInstance();
  1060. const Rectangle parentAbsoluteBounds = _parent ? _parent->_viewportBounds : Rectangle(0, 0, game->getViewport().width, game->getViewport().height);
  1061. const Rectangle parentAbsoluteClip = _parent ? _parent->_viewportClipBounds : parentAbsoluteBounds;
  1062. // Compute content area padding values
  1063. const Theme::Border& border = getBorder(NORMAL);
  1064. const Theme::Padding& padding = getPadding();
  1065. float lpadding = border.left + padding.left;
  1066. float rpadding = border.right + padding.right;
  1067. float tpadding = border.top + padding.top;
  1068. float bpadding = border.bottom + padding.bottom;
  1069. float hpadding = lpadding + rpadding;
  1070. float vpadding = tpadding + bpadding;
  1071. // Calculate absolute unclipped bounds
  1072. _absoluteBounds.set(
  1073. parentAbsoluteBounds.x + offset.x + _bounds.x,
  1074. parentAbsoluteBounds.y + offset.y + _bounds.y,
  1075. _bounds.width,
  1076. _bounds.height);
  1077. // Calculate absolute clipped bounds
  1078. Rectangle::intersect(_absoluteBounds, parentAbsoluteClip, &_absoluteClipBounds);
  1079. // Calculate the local clipped bounds
  1080. _clipBounds.set(
  1081. max(_absoluteClipBounds.x - _absoluteBounds.x, 0.0f),
  1082. max(_absoluteClipBounds.y - _absoluteBounds.y, 0.0f),
  1083. _absoluteClipBounds.width,
  1084. _absoluteClipBounds.height
  1085. );
  1086. // Calculate the absolute unclipped viewport bounds (content area, which does not include border and padding)
  1087. _viewportBounds.set(
  1088. _absoluteBounds.x + lpadding,
  1089. _absoluteBounds.y + tpadding,
  1090. _absoluteBounds.width - hpadding,
  1091. _absoluteBounds.height - vpadding);
  1092. // Calculate the absolute clipped viewport bounds
  1093. Rectangle::intersect(_viewportBounds, parentAbsoluteClip, &_viewportClipBounds);
  1094. }
  1095. void Control::startBatch(Form* form, SpriteBatch* batch)
  1096. {
  1097. form->startBatch(batch);
  1098. }
  1099. void Control::finishBatch(Form* form, SpriteBatch* batch)
  1100. {
  1101. form->finishBatch(batch);
  1102. }
  1103. unsigned int Control::draw(Form* form, const Rectangle& clip)
  1104. {
  1105. if (!_visible)
  1106. return 0;
  1107. unsigned int drawCalls = drawBorder(form, clip);
  1108. drawCalls += drawImages(form, clip);
  1109. drawCalls += drawText(form, clip);
  1110. return drawCalls;
  1111. }
  1112. unsigned int Control::drawBorder(Form* form, const Rectangle& clip)
  1113. {
  1114. if (!form || !_skin || _absoluteBounds.width <= 0 || _absoluteBounds.height <= 0)
  1115. return 0;
  1116. unsigned int drawCalls = 0;
  1117. SpriteBatch* batch = _style->getTheme()->getSpriteBatch();
  1118. startBatch(form, batch);
  1119. // Get the border and background images for this control's current state.
  1120. const Theme::UVs& topLeft = _skin->getUVs(Theme::Skin::TOP_LEFT);
  1121. const Theme::UVs& top = _skin->getUVs(Theme::Skin::TOP);
  1122. const Theme::UVs& topRight = _skin->getUVs(Theme::Skin::TOP_RIGHT);
  1123. const Theme::UVs& left = _skin->getUVs(Theme::Skin::LEFT);
  1124. const Theme::UVs& center = _skin->getUVs(Theme::Skin::CENTER);
  1125. const Theme::UVs& right = _skin->getUVs(Theme::Skin::RIGHT);
  1126. const Theme::UVs& bottomLeft = _skin->getUVs(Theme::Skin::BOTTOM_LEFT);
  1127. const Theme::UVs& bottom = _skin->getUVs(Theme::Skin::BOTTOM);
  1128. const Theme::UVs& bottomRight = _skin->getUVs(Theme::Skin::BOTTOM_RIGHT);
  1129. // Calculate screen-space positions.
  1130. const Theme::Border& border = getBorder(getState());
  1131. const Theme::Padding& padding = getPadding();
  1132. Vector4 skinColor = _skin->getColor();
  1133. skinColor.w *= _opacity;
  1134. float midWidth = _absoluteBounds.width - border.left - border.right;
  1135. float midHeight = _absoluteBounds.height - border.top - border.bottom;
  1136. float midX = _absoluteBounds.x + border.left;
  1137. float midY = _absoluteBounds.y + border.top;
  1138. float rightX = _absoluteBounds.x + _absoluteBounds.width - border.right;
  1139. float bottomY = _absoluteBounds.y + _absoluteBounds.height - border.bottom;
  1140. // Draw themed border sprites.
  1141. if (!border.left && !border.right && !border.top && !border.bottom)
  1142. {
  1143. // No border, just draw the image.
  1144. batch->draw(_absoluteBounds.x, _absoluteBounds.y, _absoluteBounds.width, _absoluteBounds.height, center.u1, center.v1, center.u2, center.v2, skinColor, clip);
  1145. ++drawCalls;
  1146. }
  1147. else
  1148. {
  1149. if (border.left && border.top)
  1150. {
  1151. batch->draw(_absoluteBounds.x, _absoluteBounds.y, border.left, border.top, topLeft.u1, topLeft.v1, topLeft.u2, topLeft.v2, skinColor, clip);
  1152. ++drawCalls;
  1153. }
  1154. if (border.top)
  1155. {
  1156. batch->draw(_absoluteBounds.x + border.left, _absoluteBounds.y, midWidth, border.top, top.u1, top.v1, top.u2, top.v2, skinColor, clip);
  1157. ++drawCalls;
  1158. }
  1159. if (border.right && border.top)
  1160. {
  1161. batch->draw(rightX, _absoluteBounds.y, border.right, border.top, topRight.u1, topRight.v1, topRight.u2, topRight.v2, skinColor, clip);
  1162. ++drawCalls;
  1163. }
  1164. if (border.left)
  1165. {
  1166. batch->draw(_absoluteBounds.x, midY, border.left, midHeight, left.u1, left.v1, left.u2, left.v2, skinColor, clip);
  1167. ++drawCalls;
  1168. }
  1169. // Always draw the background.
  1170. batch->draw(_absoluteBounds.x + border.left, _absoluteBounds.y + border.top, _absoluteBounds.width - border.left - border.right, _absoluteBounds.height - border.top - border.bottom,
  1171. center.u1, center.v1, center.u2, center.v2, skinColor, clip);
  1172. ++drawCalls;
  1173. if (border.right)
  1174. {
  1175. batch->draw(rightX, midY, border.right, midHeight, right.u1, right.v1, right.u2, right.v2, skinColor, clip);
  1176. ++drawCalls;
  1177. }
  1178. if (border.bottom && border.left)
  1179. {
  1180. batch->draw(_absoluteBounds.x, bottomY, border.left, border.bottom, bottomLeft.u1, bottomLeft.v1, bottomLeft.u2, bottomLeft.v2, skinColor, clip);
  1181. ++drawCalls;
  1182. }
  1183. if (border.bottom)
  1184. {
  1185. batch->draw(midX, bottomY, midWidth, border.bottom, bottom.u1, bottom.v1, bottom.u2, bottom.v2, skinColor, clip);
  1186. ++drawCalls;
  1187. }
  1188. if (border.bottom && border.right)
  1189. {
  1190. batch->draw(rightX, bottomY, border.right, border.bottom, bottomRight.u1, bottomRight.v1, bottomRight.u2, bottomRight.v2, skinColor, clip);
  1191. ++drawCalls;
  1192. }
  1193. }
  1194. finishBatch(form, batch);
  1195. return drawCalls;
  1196. }
  1197. unsigned int Control::drawImages(Form* form, const Rectangle& position)
  1198. {
  1199. return 0;
  1200. }
  1201. unsigned int Control::drawText(Form* form, const Rectangle& position)
  1202. {
  1203. return 0;
  1204. }
  1205. bool Control::isContainer() const
  1206. {
  1207. return false;
  1208. }
  1209. Control::State Control::getState(const char* state)
  1210. {
  1211. if (!state)
  1212. {
  1213. return NORMAL;
  1214. }
  1215. if (strcmp(state, "NORMAL") == 0)
  1216. {
  1217. return NORMAL;
  1218. }
  1219. else if (strcmp(state, "ACTIVE") == 0)
  1220. {
  1221. return ACTIVE;
  1222. }
  1223. else if (strcmp(state, "FOCUS") == 0)
  1224. {
  1225. return FOCUS;
  1226. }
  1227. else if (strcmp(state, "DISABLED") == 0)
  1228. {
  1229. return DISABLED;
  1230. }
  1231. else if (strcmp(state, "HOVER") == 0)
  1232. {
  1233. return HOVER;
  1234. }
  1235. return NORMAL;
  1236. }
  1237. Theme::ThemeImage* Control::getImage(const char* id, State state)
  1238. {
  1239. Theme::ThemeImage* image = NULL;
  1240. Theme::Style::Overlay* overlay = getOverlay(state);
  1241. if (overlay)
  1242. {
  1243. Theme::ImageList* imageList = overlay->getImageList();
  1244. if (imageList)
  1245. image = imageList->getImage(id);
  1246. }
  1247. return image ? image : _style->getTheme()->_emptyImage;
  1248. }
  1249. Control* Control::getParent() const
  1250. {
  1251. return _parent;
  1252. }
  1253. bool Control::isChild(Control* control) const
  1254. {
  1255. if (!control)
  1256. return false;
  1257. Control* parent = _parent;
  1258. while (parent)
  1259. {
  1260. if (parent == control)
  1261. return true;
  1262. parent = parent->_parent;
  1263. }
  1264. return false;
  1265. }
  1266. Form* Control::getTopLevelForm() const
  1267. {
  1268. if (_parent)
  1269. return _parent->getTopLevelForm();
  1270. if (isContainer())
  1271. {
  1272. Container* container = static_cast<Container*>(const_cast<Control*>(this));
  1273. if (container->isForm())
  1274. return static_cast<Form*>(container);
  1275. }
  1276. return NULL;
  1277. }
  1278. // Implementation of AnimationHandler
  1279. unsigned int Control::getAnimationPropertyComponentCount(int propertyId) const
  1280. {
  1281. switch(propertyId)
  1282. {
  1283. case ANIMATE_POSITION:
  1284. case ANIMATE_SIZE:
  1285. return 2;
  1286. case ANIMATE_POSITION_X:
  1287. case ANIMATE_POSITION_Y:
  1288. case ANIMATE_SIZE_WIDTH:
  1289. case ANIMATE_SIZE_HEIGHT:
  1290. case ANIMATE_OPACITY:
  1291. return 1;
  1292. default:
  1293. return -1;
  1294. }
  1295. }
  1296. void Control::getAnimationPropertyValue(int propertyId, AnimationValue* value)
  1297. {
  1298. GP_ASSERT(value);
  1299. switch (propertyId)
  1300. {
  1301. case ANIMATE_POSITION:
  1302. value->setFloat(0, _bounds.x);
  1303. value->setFloat(1, _bounds.y);
  1304. break;
  1305. case ANIMATE_SIZE:
  1306. value->setFloat(0, _bounds.width);
  1307. value->setFloat(1, _bounds.height);
  1308. break;
  1309. case ANIMATE_POSITION_X:
  1310. value->setFloat(0, _bounds.x);
  1311. break;
  1312. case ANIMATE_POSITION_Y:
  1313. value->setFloat(0, _bounds.y);
  1314. break;
  1315. case ANIMATE_SIZE_WIDTH:
  1316. value->setFloat(0, _bounds.width);
  1317. break;
  1318. case ANIMATE_SIZE_HEIGHT:
  1319. value->setFloat(0, _bounds.height);
  1320. break;
  1321. case ANIMATE_OPACITY:
  1322. value->setFloat(0, _opacity);
  1323. break;
  1324. default:
  1325. break;
  1326. }
  1327. }
  1328. void Control::setAnimationPropertyValue(int propertyId, AnimationValue* value, float blendWeight)
  1329. {
  1330. GP_ASSERT(value);
  1331. switch(propertyId)
  1332. {
  1333. case ANIMATE_POSITION:
  1334. setX(Curve::lerp(blendWeight, _bounds.x, value->getFloat(0)), isXPercentage());
  1335. setY(Curve::lerp(blendWeight, _bounds.y, value->getFloat(1)), isYPercentage());
  1336. break;
  1337. case ANIMATE_POSITION_X:
  1338. setX(Curve::lerp(blendWeight, _bounds.x, value->getFloat(0)), isXPercentage());
  1339. break;
  1340. case ANIMATE_POSITION_Y:
  1341. setY(Curve::lerp(blendWeight, _bounds.y, value->getFloat(0)), isYPercentage());
  1342. break;
  1343. case ANIMATE_SIZE:
  1344. setWidth(Curve::lerp(blendWeight, _bounds.width, value->getFloat(0)), isWidthPercentage());
  1345. setHeight(Curve::lerp(blendWeight, _bounds.height, value->getFloat(1)), isHeightPercentage());
  1346. break;
  1347. case ANIMATE_SIZE_WIDTH:
  1348. setWidth(Curve::lerp(blendWeight, _bounds.width, value->getFloat(0)), isWidthPercentage());
  1349. break;
  1350. case ANIMATE_SIZE_HEIGHT:
  1351. setHeight(Curve::lerp(blendWeight, _bounds.height, value->getFloat(0)), isHeightPercentage());
  1352. break;
  1353. case ANIMATE_OPACITY:
  1354. setOpacity(Curve::lerp(blendWeight, _opacity, value->getFloat(0)));
  1355. break;
  1356. }
  1357. }
  1358. Theme::Style::Overlay** Control::getOverlays(unsigned char overlayTypes, Theme::Style::Overlay** overlays)
  1359. {
  1360. GP_ASSERT(overlays);
  1361. GP_ASSERT(_style);
  1362. unsigned int index = 0;
  1363. if ((overlayTypes & NORMAL) == NORMAL)
  1364. {
  1365. overlays[index++] = _style->getOverlay(Theme::Style::OVERLAY_NORMAL);
  1366. }
  1367. if ((overlayTypes & FOCUS) == FOCUS)
  1368. {
  1369. overlays[index++] = _style->getOverlay(Theme::Style::OVERLAY_FOCUS);
  1370. }
  1371. if ((overlayTypes & ACTIVE) == ACTIVE)
  1372. {
  1373. overlays[index++] = _style->getOverlay(Theme::Style::OVERLAY_ACTIVE);
  1374. }
  1375. if ((overlayTypes & DISABLED) == DISABLED)
  1376. {
  1377. overlays[index++] = _style->getOverlay(Theme::Style::OVERLAY_DISABLED);
  1378. }
  1379. if ((overlayTypes & HOVER) == HOVER)
  1380. {
  1381. overlays[index++] = _style->getOverlay(Theme::Style::OVERLAY_HOVER);
  1382. }
  1383. return overlays;
  1384. }
  1385. Theme::Style::Overlay* Control::getOverlay(State state) const
  1386. {
  1387. GP_ASSERT(_style);
  1388. Theme::Style::Overlay* overlay = NULL;
  1389. switch (state)
  1390. {
  1391. case Control::NORMAL:
  1392. return _style->getOverlay(Theme::Style::OVERLAY_NORMAL);
  1393. case Control::FOCUS:
  1394. overlay = _style->getOverlay(Theme::Style::OVERLAY_FOCUS);
  1395. break;
  1396. case Control::ACTIVE:
  1397. overlay = _style->getOverlay(Theme::Style::OVERLAY_ACTIVE);
  1398. if (!overlay && hasFocus())
  1399. overlay = _style->getOverlay(Theme::Style::OVERLAY_FOCUS);
  1400. break;
  1401. case Control::DISABLED:
  1402. overlay = _style->getOverlay(Theme::Style::OVERLAY_DISABLED);
  1403. break;
  1404. case Control::HOVER:
  1405. overlay = _style->getOverlay(Theme::Style::OVERLAY_HOVER);
  1406. if (!overlay && hasFocus())
  1407. overlay = _style->getOverlay(Theme::Style::OVERLAY_FOCUS);
  1408. break;
  1409. }
  1410. // Fall back to normal overlay if more specific state overlay not found
  1411. if (!overlay)
  1412. overlay = _style->getOverlay(Theme::Style::OVERLAY_NORMAL);
  1413. return overlay;
  1414. }
  1415. void Control::overrideStyle()
  1416. {
  1417. if (_styleOverridden)
  1418. {
  1419. return;
  1420. }
  1421. // Copy the style.
  1422. GP_ASSERT(_style);
  1423. _style = new Theme::Style(*_style);
  1424. _styleOverridden = true;
  1425. }
  1426. void Control::overrideThemedProperties(Properties* properties, unsigned char states)
  1427. {
  1428. GP_ASSERT(properties);
  1429. GP_ASSERT(_style);
  1430. GP_ASSERT(_style->_theme);
  1431. Theme::ImageList* imageList = NULL;
  1432. Theme::ThemeImage* cursor = NULL;
  1433. Theme::Skin* skin = NULL;
  1434. _style->_theme->lookUpSprites(properties, &imageList, &cursor, &skin);
  1435. if (imageList)
  1436. {
  1437. setImageList(imageList, states);
  1438. }
  1439. if (cursor)
  1440. {
  1441. setCursor(cursor, states);
  1442. }
  1443. if (skin)
  1444. {
  1445. setSkin(skin, states);
  1446. }
  1447. if (properties->exists("font"))
  1448. {
  1449. Font* font = Font::create(properties->getString("font"));
  1450. setFont(font, states);
  1451. font->release();
  1452. }
  1453. if (properties->exists("fontSize"))
  1454. {
  1455. setFontSize(properties->getInt("fontSize"), states);
  1456. }
  1457. if (properties->exists("textColor"))
  1458. {
  1459. Vector4 textColor(0, 0, 0, 1);
  1460. properties->getColor("textColor", &textColor);
  1461. setTextColor(textColor, states);
  1462. }
  1463. if (properties->exists("textAlignment"))
  1464. {
  1465. setTextAlignment(Font::getJustify(properties->getString("textAlignment")), states);
  1466. }
  1467. if (properties->exists("rightToLeft"))
  1468. {
  1469. setTextRightToLeft(properties->getBool("rightToLeft"), states);
  1470. }
  1471. if (properties->exists("opacity"))
  1472. {
  1473. setOpacity(properties->getFloat("opacity"), states);
  1474. }
  1475. }
  1476. void Control::setImageList(Theme::ImageList* imageList, unsigned char states)
  1477. {
  1478. overrideStyle();
  1479. Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
  1480. getOverlays(states, overlays);
  1481. for (int i = 0; i < Theme::Style::OVERLAY_MAX; ++i)
  1482. {
  1483. if( overlays[i] )
  1484. overlays[i]->setImageList(imageList);
  1485. }
  1486. if (_autoSize != AUTO_SIZE_NONE)
  1487. setDirty(DIRTY_BOUNDS);
  1488. }
  1489. void Control::setCursor(Theme::ThemeImage* cursor, unsigned char states)
  1490. {
  1491. overrideStyle();
  1492. Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
  1493. getOverlays(states, overlays);
  1494. for (int i = 0; i < Theme::Style::OVERLAY_MAX; ++i)
  1495. {
  1496. if( overlays[i] )
  1497. overlays[i]->setCursor(cursor);
  1498. }
  1499. }
  1500. void Control::setSkin(Theme::Skin* skin, unsigned char states)
  1501. {
  1502. overrideStyle();
  1503. Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
  1504. getOverlays(states, overlays);
  1505. for (int i = 0; i < Theme::Style::OVERLAY_MAX; ++i)
  1506. {
  1507. if( overlays[i] )
  1508. overlays[i]->setSkin(skin);
  1509. }
  1510. if (_autoSize != AUTO_SIZE_NONE)
  1511. setDirty(DIRTY_BOUNDS);
  1512. }
  1513. Theme::Skin* Control::getSkin(State state)
  1514. {
  1515. Theme::Style::Overlay* overlay = getOverlay(state);
  1516. GP_ASSERT(overlay);
  1517. return overlay->getSkin();
  1518. }
  1519. Control::Alignment Control::getAlignment(const char* alignment)
  1520. {
  1521. if (!alignment)
  1522. {
  1523. return Control::ALIGN_TOP_LEFT;
  1524. }
  1525. if (strcmp(alignment, "ALIGN_LEFT") == 0)
  1526. {
  1527. return Control::ALIGN_LEFT;
  1528. }
  1529. else if (strcmp(alignment, "ALIGN_HCENTER") == 0)
  1530. {
  1531. return Control::ALIGN_HCENTER;
  1532. }
  1533. else if (strcmp(alignment, "ALIGN_RIGHT") == 0)
  1534. {
  1535. return Control::ALIGN_RIGHT;
  1536. }
  1537. else if (strcmp(alignment, "ALIGN_TOP") == 0)
  1538. {
  1539. return Control::ALIGN_TOP;
  1540. }
  1541. else if (strcmp(alignment, "ALIGN_VCENTER") == 0)
  1542. {
  1543. return Control::ALIGN_VCENTER;
  1544. }
  1545. else if (strcmp(alignment, "ALIGN_BOTTOM") == 0)
  1546. {
  1547. return Control::ALIGN_BOTTOM;
  1548. }
  1549. else if (strcmp(alignment, "ALIGN_TOP_LEFT") == 0)
  1550. {
  1551. return Control::ALIGN_TOP_LEFT;
  1552. }
  1553. else if (strcmp(alignment, "ALIGN_VCENTER_LEFT") == 0)
  1554. {
  1555. return Control::ALIGN_VCENTER_LEFT;
  1556. }
  1557. else if (strcmp(alignment, "ALIGN_BOTTOM_LEFT") == 0)
  1558. {
  1559. return Control::ALIGN_BOTTOM_LEFT;
  1560. }
  1561. else if (strcmp(alignment, "ALIGN_TOP_HCENTER") == 0)
  1562. {
  1563. return Control::ALIGN_TOP_HCENTER;
  1564. }
  1565. else if (strcmp(alignment, "ALIGN_VCENTER_HCENTER") == 0)
  1566. {
  1567. return Control::ALIGN_VCENTER_HCENTER;
  1568. }
  1569. else if (strcmp(alignment, "ALIGN_BOTTOM_HCENTER") == 0)
  1570. {
  1571. return Control::ALIGN_BOTTOM_HCENTER;
  1572. }
  1573. else if (strcmp(alignment, "ALIGN_TOP_RIGHT") == 0)
  1574. {
  1575. return Control::ALIGN_TOP_RIGHT;
  1576. }
  1577. else if (strcmp(alignment, "ALIGN_VCENTER_RIGHT") == 0)
  1578. {
  1579. return Control::ALIGN_VCENTER_RIGHT;
  1580. }
  1581. else if (strcmp(alignment, "ALIGN_BOTTOM_RIGHT") == 0)
  1582. {
  1583. return Control::ALIGN_BOTTOM_RIGHT;
  1584. }
  1585. else
  1586. {
  1587. GP_ERROR("Failed to get corresponding control alignment for unsupported value '%s'.", alignment);
  1588. }
  1589. // Default.
  1590. return Control::ALIGN_TOP_LEFT;
  1591. }
  1592. float Control::parseCoord(const char* s, bool* isPercentage)
  1593. {
  1594. const char* p;
  1595. if ((p = strchr(s, '%')) != NULL)
  1596. {
  1597. std::string value(s, (std::string::size_type)(p - s));
  1598. *isPercentage = true;
  1599. return (float)(atof(value.c_str()) * 0.01);
  1600. }
  1601. *isPercentage = false;
  1602. return (float)atof(s);
  1603. }
  1604. bool Control::parseCoordPair(const char* s, float* v1, float* v2, bool* v1Percentage, bool* v2Percentage)
  1605. {
  1606. size_t len = strlen(s);
  1607. const char* s2 = strchr(s, ',');
  1608. if (s2 == NULL)
  1609. return false;
  1610. std::string v1Str(s, (std::string::size_type)(s2 - s));
  1611. std::string v2Str(s2 + 1);
  1612. *v1 = parseCoord(v1Str.c_str(), v1Percentage);
  1613. *v2 = parseCoord(v2Str.c_str(), v2Percentage);
  1614. return true;
  1615. }
  1616. }