Control.cpp 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576
  1. #include "Base.h"
  2. #include "Game.h"
  3. #include "Control.h"
  4. namespace gameplay
  5. {
  6. Control::Control()
  7. : _id(""), _state(Control::NORMAL), _bounds(Rectangle::empty()), _clipBounds(Rectangle::empty()), _viewportClipBounds(Rectangle::empty()),
  8. _clearBounds(Rectangle::empty()), _dirty(true), _consumeInputEvents(false), _alignment(ALIGN_TOP_LEFT), _isAlignmentSet(false), _autoWidth(false), _autoHeight(false), _listeners(NULL), _visible(true),
  9. _zIndex(-1), _contactIndex(INVALID_CONTACT_INDEX), _focusIndex(-1), _parent(NULL), _styleOverridden(false), _skin(NULL), _previousState(NORMAL)
  10. {
  11. addScriptEvent("controlEvent", "<Control>[Control::Listener::EventType]");
  12. }
  13. Control::~Control()
  14. {
  15. if (_listeners)
  16. {
  17. for (std::map<Control::Listener::EventType, std::list<Control::Listener*>*>::const_iterator itr = _listeners->begin(); itr != _listeners->end(); ++itr)
  18. {
  19. std::list<Control::Listener*>* list = itr->second;
  20. SAFE_DELETE(list);
  21. }
  22. SAFE_DELETE(_listeners);
  23. }
  24. if (_styleOverridden)
  25. {
  26. SAFE_DELETE(_style);
  27. }
  28. }
  29. void Control::initialize(Theme::Style* style, Properties* properties)
  30. {
  31. GP_ASSERT(properties);
  32. _style = style;
  33. // Properties not defined by the style.
  34. _alignment = getAlignment(properties->getString("alignment"));
  35. _autoWidth = properties->getBool("autoWidth");
  36. _autoHeight = properties->getBool("autoHeight");
  37. _consumeInputEvents = properties->getBool("consumeInputEvents", false);
  38. _visible = properties->getBool("visible", true);
  39. if (properties->exists("zIndex"))
  40. {
  41. _zIndex = properties->getInt("zIndex");
  42. }
  43. else
  44. {
  45. _zIndex = -1;
  46. }
  47. if (properties->exists("focusIndex"))
  48. {
  49. _focusIndex = properties->getInt("focusIndex");
  50. }
  51. else
  52. {
  53. _focusIndex = -1;
  54. }
  55. Vector2 position;
  56. Vector2 size;
  57. if (properties->exists("position"))
  58. {
  59. properties->getVector2("position", &position);
  60. }
  61. else
  62. {
  63. position.x = properties->getFloat("x");
  64. position.y = properties->getFloat("y");
  65. }
  66. if (properties->exists("size"))
  67. {
  68. properties->getVector2("size", &size);
  69. }
  70. else
  71. {
  72. size.x = properties->getFloat("width");
  73. size.y = properties->getFloat("height");
  74. }
  75. setBounds(Rectangle(position.x, position.y, size.x, size.y));
  76. const char* id = properties->getId();
  77. if (id)
  78. _id = id;
  79. if (properties->exists("enabled"))
  80. {
  81. setEnabled(properties->getBool("enabled"));
  82. }
  83. // Potentially override themed properties for all states.
  84. overrideThemedProperties(properties, STATE_ALL);
  85. // Override themed properties on specific states.
  86. Properties* innerSpace = properties->getNextNamespace();
  87. while (innerSpace != NULL)
  88. {
  89. std::string spaceName(innerSpace->getNamespace());
  90. std::transform(spaceName.begin(), spaceName.end(), spaceName.begin(), (int(*)(int))toupper);
  91. if (spaceName == "STATENORMAL")
  92. {
  93. overrideThemedProperties(innerSpace, NORMAL);
  94. }
  95. else if (spaceName == "STATEFOCUS")
  96. {
  97. overrideThemedProperties(innerSpace, FOCUS);
  98. }
  99. else if (spaceName == "STATEACTIVE")
  100. {
  101. overrideThemedProperties(innerSpace, ACTIVE);
  102. }
  103. else if (spaceName == "STATEDISABLED")
  104. {
  105. overrideThemedProperties(innerSpace, DISABLED);
  106. }
  107. else if (spaceName == "STATEHOVER")
  108. {
  109. overrideThemedProperties(innerSpace, HOVER);
  110. }
  111. else if (spaceName == "MARGIN")
  112. {
  113. setMargin(innerSpace->getFloat("top"), innerSpace->getFloat("bottom"),
  114. innerSpace->getFloat("left"), innerSpace->getFloat("right"));
  115. }
  116. else if (spaceName == "PADDING")
  117. {
  118. setPadding(innerSpace->getFloat("top"), innerSpace->getFloat("bottom"),
  119. innerSpace->getFloat("left"), innerSpace->getFloat("right"));
  120. }
  121. innerSpace = properties->getNextNamespace();
  122. }
  123. }
  124. const char* Control::getId() const
  125. {
  126. return _id.c_str();
  127. }
  128. void Control::setPosition(float x, float y)
  129. {
  130. if (x != _bounds.x || y != _bounds.y)
  131. {
  132. _bounds.x = x;
  133. _bounds.y = y;
  134. _dirty = true;
  135. }
  136. }
  137. void Control::setSize(float width, float height)
  138. {
  139. if (width != _bounds.width || height != _bounds.height)
  140. {
  141. _bounds.width = width;
  142. _bounds.height = height;
  143. _dirty = true;
  144. }
  145. }
  146. void Control::setWidth(float width)
  147. {
  148. if (width != _bounds.width)
  149. {
  150. _bounds.width = width;
  151. _dirty = true;
  152. }
  153. }
  154. void Control::setHeight(float height)
  155. {
  156. if (height != _bounds.height)
  157. {
  158. _bounds.height = height;
  159. _dirty = true;
  160. }
  161. }
  162. void Control::setBounds(const Rectangle& bounds)
  163. {
  164. if (bounds != _bounds)
  165. {
  166. _bounds.set(bounds);
  167. _dirty = true;
  168. }
  169. }
  170. const Rectangle& Control::getBounds() const
  171. {
  172. return _bounds;
  173. }
  174. const Rectangle& Control::getAbsoluteBounds() const
  175. {
  176. return _absoluteBounds;
  177. }
  178. float Control::getX() const
  179. {
  180. return _bounds.x;
  181. }
  182. float Control::getY() const
  183. {
  184. return _bounds.y;
  185. }
  186. float Control::getWidth() const
  187. {
  188. return _bounds.width;
  189. }
  190. float Control::getHeight() const
  191. {
  192. return _bounds.height;
  193. }
  194. void Control::setAlignment(Alignment alignment)
  195. {
  196. _alignment = alignment;
  197. _isAlignmentSet = true;
  198. _dirty = true;
  199. }
  200. Control::Alignment Control::getAlignment() const
  201. {
  202. return _alignment;
  203. }
  204. void Control::setAutoWidth(bool autoWidth)
  205. {
  206. if (_autoWidth != autoWidth)
  207. {
  208. _autoWidth = autoWidth;
  209. _dirty = true;
  210. }
  211. }
  212. bool Control::getAutoWidth() const
  213. {
  214. return _autoWidth;
  215. }
  216. void Control::setAutoHeight(bool autoHeight)
  217. {
  218. if (_autoHeight != autoHeight)
  219. {
  220. _autoHeight = autoHeight;
  221. _dirty = true;
  222. }
  223. }
  224. bool Control::getAutoHeight() const
  225. {
  226. return _autoHeight;
  227. }
  228. void Control::setVisible(bool visible)
  229. {
  230. if (visible && !_visible)
  231. {
  232. _visible = true;
  233. _dirty = true;
  234. }
  235. else if (!visible && _visible)
  236. {
  237. _visible = false;
  238. _dirty = true;
  239. }
  240. }
  241. bool Control::isVisible() const
  242. {
  243. return _visible;
  244. }
  245. bool Control::hasFocus() const
  246. {
  247. return (_state == FOCUS || (_state == HOVER && _previousState == FOCUS));
  248. }
  249. void Control::setOpacity(float opacity, unsigned char states)
  250. {
  251. overrideStyle();
  252. Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
  253. getOverlays(states, overlays);
  254. for (int i = 0; i < Theme::Style::OVERLAY_MAX; ++i)
  255. {
  256. if( overlays[i] )
  257. overlays[i]->setOpacity(opacity);
  258. }
  259. _dirty = true;
  260. }
  261. float Control::getOpacity(State state) const
  262. {
  263. Theme::Style::Overlay* overlay = getOverlay(state);
  264. GP_ASSERT(overlay);
  265. return overlay->getOpacity();
  266. }
  267. void Control::setEnabled(bool enabled)
  268. {
  269. if (enabled && _state == Control::DISABLED)
  270. {
  271. _state = Control::NORMAL;
  272. _dirty = true;
  273. }
  274. else if (!enabled && _state != Control::DISABLED)
  275. {
  276. _state = Control::DISABLED;
  277. _dirty = true;
  278. }
  279. }
  280. bool Control::isEnabled() const
  281. {
  282. return _state != DISABLED;
  283. }
  284. void Control::setBorder(float top, float bottom, float left, float right, unsigned char states)
  285. {
  286. overrideStyle();
  287. Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
  288. getOverlays(states, overlays);
  289. for (int i = 0; i < Theme::Style::OVERLAY_MAX; ++i)
  290. {
  291. if( overlays[i] )
  292. overlays[i]->setBorder(top, bottom, left, right);
  293. }
  294. _dirty = true;
  295. }
  296. const Theme::Border& Control::getBorder(State state) const
  297. {
  298. Theme::Style::Overlay* overlay = getOverlay(state);
  299. GP_ASSERT(overlay);
  300. return overlay->getBorder();
  301. }
  302. void Control::setSkinRegion(const Rectangle& region, unsigned char states)
  303. {
  304. overrideStyle();
  305. Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
  306. getOverlays(states, overlays);
  307. for (int i = 0; i < Theme::Style::OVERLAY_MAX; ++i)
  308. {
  309. if( overlays[i] )
  310. overlays[i]->setSkinRegion(region, _style->_tw, _style->_th);
  311. }
  312. _dirty = true;
  313. }
  314. const Rectangle& Control::getSkinRegion(State state) const
  315. {
  316. Theme::Style::Overlay* overlay = getOverlay(state);
  317. GP_ASSERT(overlay);
  318. return overlay->getSkinRegion();
  319. }
  320. void Control::setSkinColor(const Vector4& color, unsigned char states)
  321. {
  322. overrideStyle();
  323. Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
  324. getOverlays(states, overlays);
  325. for (int i = 0; i < Theme::Style::OVERLAY_MAX; ++i)
  326. {
  327. if( overlays[i] )
  328. overlays[i]->setSkinColor(color);
  329. }
  330. _dirty = true;
  331. }
  332. const Vector4& Control::getSkinColor(State state) const
  333. {
  334. Theme::Style::Overlay* overlay = getOverlay(state);
  335. GP_ASSERT(overlay);
  336. return overlay->getSkinColor();
  337. }
  338. void Control::setMargin(float top, float bottom, float left, float right)
  339. {
  340. GP_ASSERT(_style);
  341. overrideStyle();
  342. _style->setMargin(top, bottom, left, right);
  343. _dirty = true;
  344. }
  345. const Theme::Margin& Control::getMargin() const
  346. {
  347. GP_ASSERT(_style);
  348. return _style->getMargin();
  349. }
  350. void Control::setPadding(float top, float bottom, float left, float right)
  351. {
  352. GP_ASSERT(_style);
  353. overrideStyle();
  354. _style->setPadding(top, bottom, left, right);
  355. _dirty = true;
  356. }
  357. const Theme::Padding& Control::getPadding() const
  358. {
  359. GP_ASSERT(_style);
  360. return _style->getPadding();
  361. }
  362. void Control::setImageRegion(const char* id, const Rectangle& region, unsigned char states)
  363. {
  364. overrideStyle();
  365. Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
  366. getOverlays(states, overlays);
  367. for (int i = 0; i < Theme::Style::OVERLAY_MAX; ++i)
  368. {
  369. if( overlays[i] )
  370. overlays[i]->setImageRegion(id, region, _style->_tw, _style->_th);
  371. }
  372. _dirty = true;
  373. }
  374. const Rectangle& Control::getImageRegion(const char* id, State state) const
  375. {
  376. Theme::Style::Overlay* overlay = getOverlay(state);
  377. GP_ASSERT(overlay);
  378. return overlay->getImageRegion(id);
  379. }
  380. void Control::setImageColor(const char* id, const Vector4& color, unsigned char states)
  381. {
  382. overrideStyle();
  383. Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
  384. getOverlays(states, overlays);
  385. for (int i = 0; i < Theme::Style::OVERLAY_MAX; ++i)
  386. {
  387. if( overlays[i] )
  388. overlays[i]->setImageColor(id, color);
  389. }
  390. _dirty = true;
  391. }
  392. const Vector4& Control::getImageColor(const char* id, State state) const
  393. {
  394. Theme::Style::Overlay* overlay = getOverlay(state);
  395. GP_ASSERT(overlay);
  396. return overlay->getImageColor(id);
  397. }
  398. const Theme::UVs& Control::getImageUVs(const char* id, State state) const
  399. {
  400. Theme::Style::Overlay* overlay = getOverlay(state);
  401. GP_ASSERT(overlay);
  402. return overlay->getImageUVs(id);
  403. }
  404. void Control::setCursorRegion(const Rectangle& region, unsigned char states)
  405. {
  406. overrideStyle();
  407. Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
  408. getOverlays(states, overlays);
  409. for (int i = 0; i < Theme::Style::OVERLAY_MAX; ++i)
  410. {
  411. if( overlays[i] )
  412. overlays[i]->setCursorRegion(region, _style->_tw, _style->_th);
  413. }
  414. _dirty = true;
  415. }
  416. const Rectangle& Control::getCursorRegion(State state) const
  417. {
  418. Theme::Style::Overlay* overlay = getOverlay(state);
  419. GP_ASSERT(overlay);
  420. return overlay->getCursorRegion();
  421. }
  422. void Control::setCursorColor(const Vector4& color, unsigned char states)
  423. {
  424. overrideStyle();
  425. Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
  426. getOverlays(states, overlays);
  427. for (int i = 0; i < Theme::Style::OVERLAY_MAX; ++i)
  428. {
  429. if( overlays[i] )
  430. overlays[i]->setCursorColor(color);
  431. }
  432. _dirty = true;
  433. }
  434. const Vector4& Control::getCursorColor(State state)
  435. {
  436. Theme::Style::Overlay* overlay = getOverlay(state);
  437. GP_ASSERT(overlay);
  438. return overlay->getCursorColor();
  439. }
  440. const Theme::UVs& Control::getCursorUVs(State state)
  441. {
  442. Theme::Style::Overlay* overlay = getOverlay(state);
  443. GP_ASSERT(overlay);
  444. return overlay->getCursorUVs();
  445. }
  446. void Control::setFont(Font* font, unsigned char states)
  447. {
  448. overrideStyle();
  449. Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
  450. getOverlays(states, overlays);
  451. for (int i = 0; i < Theme::Style::OVERLAY_MAX; ++i)
  452. {
  453. if( overlays[i] )
  454. overlays[i]->setFont(font);
  455. }
  456. _dirty = true;
  457. }
  458. Font* Control::getFont(State state) const
  459. {
  460. Theme::Style::Overlay* overlay = getOverlay(state);
  461. GP_ASSERT(overlay);
  462. return overlay->getFont();
  463. }
  464. void Control::setFontSize(unsigned int fontSize, unsigned char states)
  465. {
  466. overrideStyle();
  467. Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
  468. getOverlays(states, overlays);
  469. for (int i = 0; i < Theme::Style::OVERLAY_MAX; ++i)
  470. {
  471. if( overlays[i] )
  472. overlays[i]->setFontSize(fontSize);
  473. }
  474. _dirty = true;
  475. }
  476. unsigned int Control::getFontSize(State state) const
  477. {
  478. Theme::Style::Overlay* overlay = getOverlay(state);
  479. GP_ASSERT(overlay);
  480. return overlay->getFontSize();
  481. }
  482. void Control::setTextColor(const Vector4& color, unsigned char states)
  483. {
  484. overrideStyle();
  485. Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
  486. getOverlays(states, overlays);
  487. for (int i = 0; i < Theme::Style::OVERLAY_MAX; ++i)
  488. {
  489. if( overlays[i] )
  490. overlays[i]->setTextColor(color);
  491. }
  492. _dirty = true;
  493. }
  494. const Vector4& Control::getTextColor(State state) const
  495. {
  496. Theme::Style::Overlay* overlay = getOverlay(state);
  497. GP_ASSERT(overlay);
  498. return overlay->getTextColor();
  499. }
  500. void Control::setTextAlignment(Font::Justify alignment, 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]->setTextAlignment(alignment);
  509. }
  510. _dirty = true;
  511. }
  512. Font::Justify Control::getTextAlignment(State state) const
  513. {
  514. Theme::Style::Overlay* overlay = getOverlay(state);
  515. GP_ASSERT(overlay);
  516. return overlay->getTextAlignment();
  517. }
  518. void Control::setTextRightToLeft(bool rightToLeft, 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]->setTextRightToLeft(rightToLeft);
  527. }
  528. _dirty = true;
  529. }
  530. bool Control::getTextRightToLeft(State state) const
  531. {
  532. Theme::Style::Overlay* overlay = getOverlay(state);
  533. GP_ASSERT(overlay);
  534. return overlay->getTextRightToLeft();
  535. }
  536. const Rectangle& Control::getClipBounds() const
  537. {
  538. return _clipBounds;
  539. }
  540. const Rectangle& Control::getClip() const
  541. {
  542. return _viewportClipBounds;
  543. }
  544. void Control::setStyle(Theme::Style* style)
  545. {
  546. if (style != _style)
  547. {
  548. _dirty = true;
  549. }
  550. _style = style;
  551. }
  552. Theme::Style* Control::getStyle() const
  553. {
  554. return _style;
  555. }
  556. void Control::setState(State state)
  557. {
  558. if (getOverlay(_state) != getOverlay(state))
  559. _dirty = true;
  560. _state = state;
  561. }
  562. Control::State Control::getState() const
  563. {
  564. return _state;
  565. }
  566. Theme::Style::OverlayType Control::getOverlayType() const
  567. {
  568. switch (_state)
  569. {
  570. case Control::NORMAL:
  571. return Theme::Style::OVERLAY_NORMAL;
  572. case Control::FOCUS:
  573. return Theme::Style::OVERLAY_FOCUS;
  574. case Control::ACTIVE:
  575. return Theme::Style::OVERLAY_ACTIVE;
  576. case Control::DISABLED:
  577. return Theme::Style::OVERLAY_DISABLED;
  578. case Control::HOVER:
  579. return Theme::Style::OVERLAY_HOVER;
  580. default:
  581. return Theme::Style::OVERLAY_NORMAL;
  582. }
  583. }
  584. void Control::setConsumeInputEvents(bool consume)
  585. {
  586. _consumeInputEvents = consume;
  587. }
  588. bool Control::getConsumeInputEvents()
  589. {
  590. return _consumeInputEvents;
  591. }
  592. int Control::getZIndex() const
  593. {
  594. return _zIndex;
  595. }
  596. void Control::setZIndex(int zIndex)
  597. {
  598. if (zIndex != _zIndex)
  599. {
  600. _zIndex = zIndex;
  601. _dirty = true;
  602. }
  603. }
  604. int Control::getFocusIndex() const
  605. {
  606. return _focusIndex;
  607. }
  608. void Control::setFocusIndex(int focusIndex)
  609. {
  610. _focusIndex = focusIndex;
  611. }
  612. void Control::addListener(Control::Listener* listener, int eventFlags)
  613. {
  614. GP_ASSERT(listener);
  615. if ((eventFlags & Control::Listener::PRESS) == Control::Listener::PRESS)
  616. {
  617. addSpecificListener(listener, Control::Listener::PRESS);
  618. }
  619. if ((eventFlags & Control::Listener::RELEASE) == Control::Listener::RELEASE)
  620. {
  621. addSpecificListener(listener, Control::Listener::RELEASE);
  622. }
  623. if ((eventFlags & Control::Listener::CLICK) == Control::Listener::CLICK)
  624. {
  625. addSpecificListener(listener, Control::Listener::CLICK);
  626. }
  627. if ((eventFlags & Control::Listener::VALUE_CHANGED) == Control::Listener::VALUE_CHANGED)
  628. {
  629. addSpecificListener(listener, Control::Listener::VALUE_CHANGED);
  630. }
  631. if ((eventFlags & Control::Listener::TEXT_CHANGED) == Control::Listener::TEXT_CHANGED)
  632. {
  633. addSpecificListener(listener, Control::Listener::TEXT_CHANGED);
  634. }
  635. }
  636. void Control::removeListener(Control::Listener* listener)
  637. {
  638. if (_listeners == NULL || listener == NULL)
  639. return;
  640. for (std::map<Control::Listener::EventType, std::list<Control::Listener*>*>::iterator itr = _listeners->begin(); itr != _listeners->end();)
  641. {
  642. itr->second->remove(listener);
  643. if(itr->second->empty())
  644. {
  645. std::list<Control::Listener*>* list = itr->second;
  646. _listeners->erase(itr++);
  647. SAFE_DELETE(list);
  648. }
  649. else
  650. ++itr;
  651. }
  652. if (_listeners->empty())
  653. SAFE_DELETE(_listeners);
  654. }
  655. void Control::addSpecificListener(Control::Listener* listener, Control::Listener::EventType eventType)
  656. {
  657. GP_ASSERT(listener);
  658. if (!_listeners)
  659. {
  660. _listeners = new std::map<Control::Listener::EventType, std::list<Control::Listener*>*>();
  661. }
  662. std::map<Control::Listener::EventType, std::list<Control::Listener*>*>::const_iterator itr = _listeners->find(eventType);
  663. if (itr == _listeners->end())
  664. {
  665. _listeners->insert(std::make_pair(eventType, new std::list<Control::Listener*>()));
  666. itr = _listeners->find(eventType);
  667. }
  668. std::list<Control::Listener*>* listenerList = itr->second;
  669. listenerList->push_back(listener);
  670. }
  671. bool Control::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  672. {
  673. switch (evt)
  674. {
  675. case Touch::TOUCH_PRESS:
  676. // Controls that don't have an ACTIVE state go to the FOCUS state when pressed.
  677. // (Other controls, such as buttons and sliders, become ACTIVE when pressed and go to the FOCUS state on release.)
  678. // Labels are never any state other than NORMAL.
  679. if (_contactIndex == INVALID_CONTACT_INDEX && x > _clipBounds.x && x <= _clipBounds.x + _clipBounds.width &&
  680. y > _clipBounds.y && y <= _clipBounds.y + _clipBounds.height)
  681. {
  682. _contactIndex = (int) contactIndex;
  683. notifyListeners(Control::Listener::PRESS);
  684. return _consumeInputEvents;
  685. }
  686. else
  687. {
  688. // If this control was in focus, it's not any more.
  689. _state = NORMAL;
  690. }
  691. break;
  692. case Touch::TOUCH_MOVE:
  693. break;
  694. case Touch::TOUCH_RELEASE:
  695. if (_contactIndex == (int)contactIndex)
  696. {
  697. _contactIndex = INVALID_CONTACT_INDEX;
  698. // Always trigger Control::Listener::RELEASE
  699. notifyListeners(Control::Listener::RELEASE);
  700. // Only trigger Control::Listener::CLICK if both PRESS and RELEASE took place within the control's bounds.
  701. if (x > _clipBounds.x && x <= _clipBounds.x + _clipBounds.width &&
  702. y > _clipBounds.y && y <= _clipBounds.y + _clipBounds.height)
  703. {
  704. // Leave this control in the FOCUS state.
  705. notifyListeners(Control::Listener::CLICK);
  706. }
  707. return _consumeInputEvents;
  708. }
  709. break;
  710. }
  711. return false;
  712. }
  713. bool Control::keyEvent(Keyboard::KeyEvent evt, int key)
  714. {
  715. return false;
  716. }
  717. bool Control::mouseEvent(Mouse::MouseEvent evt, int x, int y, int wheelDelta)
  718. {
  719. // By default, mouse events are either interpreted as touch events or ignored.
  720. switch (evt)
  721. {
  722. case Mouse::MOUSE_PRESS_LEFT_BUTTON:
  723. return touchEvent(Touch::TOUCH_PRESS, x, y, 0);
  724. case Mouse::MOUSE_RELEASE_LEFT_BUTTON:
  725. return touchEvent(Touch::TOUCH_RELEASE, x, y, 0);
  726. case Mouse::MOUSE_MOVE:
  727. if (_state != ACTIVE)
  728. {
  729. if (_state != HOVER &&
  730. x > _clipBounds.x && x <= _clipBounds.x + _clipBounds.width &&
  731. y > _clipBounds.y && y <= _clipBounds.y + _clipBounds.height)
  732. {
  733. _previousState = _state;
  734. setState(HOVER);
  735. notifyListeners(Control::Listener::ENTER);
  736. return _consumeInputEvents;
  737. }
  738. else if (_state == HOVER && !(x > _clipBounds.x && x <= _clipBounds.x + _clipBounds.width &&
  739. y > _clipBounds.y && y <= _clipBounds.y + _clipBounds.height))
  740. {
  741. setState(_previousState);
  742. notifyListeners(Control::Listener::LEAVE);
  743. return _consumeInputEvents;
  744. }
  745. }
  746. return touchEvent(Touch::TOUCH_MOVE, x, y, 0);
  747. default:
  748. break;
  749. }
  750. return false;
  751. }
  752. bool Control::gamepadEvent(Gamepad::GamepadEvent evt, Gamepad* gamepad, unsigned int analogIndex)
  753. {
  754. // Default behavior for gamepad events.
  755. switch (evt)
  756. {
  757. case Gamepad::BUTTON_EVENT:
  758. if (_state == Control::FOCUS)
  759. {
  760. if (gamepad->isButtonDown(Gamepad::BUTTON_A) ||
  761. gamepad->isButtonDown(Gamepad::BUTTON_X))
  762. {
  763. notifyListeners(Control::Listener::PRESS);
  764. return _consumeInputEvents;
  765. }
  766. }
  767. else if (_state == Control::ACTIVE)
  768. {
  769. if (!gamepad->isButtonDown(Gamepad::BUTTON_A) &&
  770. !gamepad->isButtonDown(Gamepad::BUTTON_X))
  771. {
  772. notifyListeners(Control::Listener::RELEASE);
  773. notifyListeners(Control::Listener::CLICK);
  774. return _consumeInputEvents;
  775. }
  776. }
  777. break;
  778. case Gamepad::JOYSTICK_EVENT:
  779. break;
  780. case Gamepad::TRIGGER_EVENT:
  781. break;
  782. }
  783. return false;
  784. }
  785. void Control::notifyListeners(Control::Listener::EventType eventType)
  786. {
  787. // This method runs untrusted code by notifying listeners of events.
  788. // If the user calls exit() or otherwise releases this control, we
  789. // need to keep it alive until the method returns.
  790. addRef();
  791. if (_listeners)
  792. {
  793. std::map<Control::Listener::EventType, std::list<Control::Listener*>*>::const_iterator itr = _listeners->find(eventType);
  794. if (itr != _listeners->end())
  795. {
  796. std::list<Control::Listener*>* listenerList = itr->second;
  797. for (std::list<Control::Listener*>::iterator listenerItr = listenerList->begin(); listenerItr != listenerList->end(); ++listenerItr)
  798. {
  799. GP_ASSERT(*listenerItr);
  800. (*listenerItr)->controlEvent(this, eventType);
  801. }
  802. }
  803. }
  804. fireScriptEvent<void>("controlEvent", this, eventType);
  805. release();
  806. }
  807. void Control::update(const Control* container, const Vector2& offset)
  808. {
  809. const Rectangle& clip = container->getClip();
  810. const Rectangle& absoluteViewport = container->_viewportBounds;
  811. _clearBounds.set(_absoluteClipBounds);
  812. // Calculate the clipped bounds.
  813. float x = _bounds.x + offset.x;
  814. float y = _bounds.y + offset.y;
  815. float width = _bounds.width;
  816. float height = _bounds.height;
  817. float clipX2 = clip.x + clip.width;
  818. float x2 = clip.x + x + width;
  819. if (x2 > clipX2)
  820. width -= x2 - clipX2;
  821. float clipY2 = clip.y + clip.height;
  822. float y2 = clip.y + y + height;
  823. if (y2 > clipY2)
  824. height -= y2 - clipY2;
  825. if (x < 0)
  826. {
  827. width += x;
  828. x = -x;
  829. }
  830. else
  831. {
  832. x = 0;
  833. }
  834. if (y < 0)
  835. {
  836. height += y;
  837. y = -y;
  838. }
  839. else
  840. {
  841. y = 0;
  842. }
  843. _clipBounds.set(x, y, width, height);
  844. // Calculate the absolute bounds.
  845. x = _bounds.x + offset.x + absoluteViewport.x;
  846. y = _bounds.y + offset.y + absoluteViewport.y;
  847. _absoluteBounds.set(x, y, _bounds.width, _bounds.height);
  848. // Calculate the absolute viewport bounds.
  849. // Absolute bounds minus border and padding.
  850. const Theme::Border& border = getBorder(_state);
  851. const Theme::Padding& padding = getPadding();
  852. x += border.left + padding.left;
  853. y += border.top + padding.top;
  854. width = _bounds.width - border.left - padding.left - border.right - padding.right;
  855. height = _bounds.height - border.top - padding.top - border.bottom - padding.bottom;
  856. _viewportBounds.set(x, y, width, height);
  857. // Calculate the clip area.
  858. // Absolute bounds, minus border and padding,
  859. // clipped to the parent container's clip area.
  860. clipX2 = clip.x + clip.width;
  861. x2 = x + width;
  862. if (x2 > clipX2)
  863. width = clipX2 - x;
  864. clipY2 = clip.y + clip.height;
  865. y2 = y + height;
  866. if (y2 > clipY2)
  867. height = clipY2 - y;
  868. if (x < clip.x)
  869. {
  870. float dx = clip.x - x;
  871. width -= dx;
  872. x = clip.x;
  873. }
  874. if (y < clip.y)
  875. {
  876. float dy = clip.y - y;
  877. height -= dy;
  878. y = clip.y;
  879. }
  880. _viewportClipBounds.set(x, y, width, height);
  881. width += border.left + padding.left + border.right + padding.right;
  882. height += border.top + padding.top + border.bottom + padding.bottom;
  883. _absoluteClipBounds.set(x - border.left - padding.left, y - border.top - padding.top, max(width, 0.0f), max(height, 0.0f));
  884. if (_clearBounds.isEmpty())
  885. {
  886. _clearBounds.set(_absoluteClipBounds);
  887. }
  888. // Cache themed attributes for performance.
  889. _skin = getSkin(_state);
  890. // Current opacity should be multiplied by that of the parent container.
  891. _opacity = getOpacity(_state) * container->_opacity;
  892. }
  893. void Control::drawBorder(SpriteBatch* spriteBatch, const Rectangle& clip)
  894. {
  895. if (!spriteBatch || !_skin || _bounds.width <= 0 || _bounds.height <= 0)
  896. return;
  897. // Get the border and background images for this control's current state.
  898. const Theme::UVs& topLeft = _skin->getUVs(Theme::Skin::TOP_LEFT);
  899. const Theme::UVs& top = _skin->getUVs(Theme::Skin::TOP);
  900. const Theme::UVs& topRight = _skin->getUVs(Theme::Skin::TOP_RIGHT);
  901. const Theme::UVs& left = _skin->getUVs(Theme::Skin::LEFT);
  902. const Theme::UVs& center = _skin->getUVs(Theme::Skin::CENTER);
  903. const Theme::UVs& right = _skin->getUVs(Theme::Skin::RIGHT);
  904. const Theme::UVs& bottomLeft = _skin->getUVs(Theme::Skin::BOTTOM_LEFT);
  905. const Theme::UVs& bottom = _skin->getUVs(Theme::Skin::BOTTOM);
  906. const Theme::UVs& bottomRight = _skin->getUVs(Theme::Skin::BOTTOM_RIGHT);
  907. // Calculate screen-space positions.
  908. const Theme::Border& border = getBorder(_state);
  909. const Theme::Padding& padding = getPadding();
  910. Vector4 skinColor = _skin->getColor();
  911. skinColor.w *= _opacity;
  912. float midWidth = _bounds.width - border.left - border.right;
  913. float midHeight = _bounds.height - border.top - border.bottom;
  914. float midX = _absoluteBounds.x + border.left;
  915. float midY = _absoluteBounds.y + border.top;
  916. float rightX = _absoluteBounds.x + _bounds.width - border.right;
  917. float bottomY = _absoluteBounds.y + _bounds.height - border.bottom;
  918. // Draw themed border sprites.
  919. if (!border.left && !border.right && !border.top && !border.bottom)
  920. {
  921. // No border, just draw the image.
  922. spriteBatch->draw(_absoluteBounds.x, _absoluteBounds.y, _bounds.width, _bounds.height, center.u1, center.v1, center.u2, center.v2, skinColor, clip);
  923. }
  924. else
  925. {
  926. if (border.left && border.top)
  927. spriteBatch->draw(_absoluteBounds.x, _absoluteBounds.y, border.left, border.top, topLeft.u1, topLeft.v1, topLeft.u2, topLeft.v2, skinColor, clip);
  928. if (border.top)
  929. spriteBatch->draw(_absoluteBounds.x + border.left, _absoluteBounds.y, midWidth, border.top, top.u1, top.v1, top.u2, top.v2, skinColor, clip);
  930. if (border.right && border.top)
  931. spriteBatch->draw(rightX, _absoluteBounds.y, border.right, border.top, topRight.u1, topRight.v1, topRight.u2, topRight.v2, skinColor, clip);
  932. if (border.left)
  933. spriteBatch->draw(_absoluteBounds.x, midY, border.left, midHeight, left.u1, left.v1, left.u2, left.v2, skinColor, clip);
  934. // Always draw the background.
  935. spriteBatch->draw(_absoluteBounds.x + border.left, _absoluteBounds.y + border.top, _bounds.width - border.left - border.right, _bounds.height - border.top - border.bottom,
  936. center.u1, center.v1, center.u2, center.v2, skinColor, clip);
  937. if (border.right)
  938. spriteBatch->draw(rightX, midY, border.right, midHeight, right.u1, right.v1, right.u2, right.v2, skinColor, clip);
  939. if (border.bottom && border.left)
  940. spriteBatch->draw(_absoluteBounds.x, bottomY, border.left, border.bottom, bottomLeft.u1, bottomLeft.v1, bottomLeft.u2, bottomLeft.v2, skinColor, clip);
  941. if (border.bottom)
  942. spriteBatch->draw(midX, bottomY, midWidth, border.bottom, bottom.u1, bottom.v1, bottom.u2, bottom.v2, skinColor, clip);
  943. if (border.bottom && border.right)
  944. spriteBatch->draw(rightX, bottomY, border.right, border.bottom, bottomRight.u1, bottomRight.v1, bottomRight.u2, bottomRight.v2, skinColor, clip);
  945. }
  946. }
  947. void Control::drawImages(SpriteBatch* spriteBatch, const Rectangle& position)
  948. {
  949. }
  950. void Control::drawText(const Rectangle& position)
  951. {
  952. }
  953. void Control::draw(SpriteBatch* spriteBatch, const Rectangle& clip, bool needsClear, bool cleared, float targetHeight)
  954. {
  955. if (needsClear)
  956. {
  957. GL_ASSERT( glEnable(GL_SCISSOR_TEST) );
  958. GL_ASSERT( glScissor(_clearBounds.x, targetHeight - _clearBounds.y - _clearBounds.height, _clearBounds.width, _clearBounds.height) );
  959. Game::getInstance()->clear(Game::CLEAR_COLOR, Vector4::zero(), 1.0f, 0);
  960. GL_ASSERT( glDisable(GL_SCISSOR_TEST) );
  961. }
  962. if (!_visible)
  963. {
  964. _dirty = false;
  965. return;
  966. }
  967. spriteBatch->start();
  968. drawBorder(spriteBatch, clip);
  969. drawImages(spriteBatch, clip);
  970. spriteBatch->finish();
  971. drawText(clip);
  972. _dirty = false;
  973. }
  974. bool Control::isDirty()
  975. {
  976. return _dirty;
  977. }
  978. bool Control::isContainer() const
  979. {
  980. return false;
  981. }
  982. Control::State Control::getState(const char* state)
  983. {
  984. if (!state)
  985. {
  986. return NORMAL;
  987. }
  988. if (strcmp(state, "NORMAL") == 0)
  989. {
  990. return NORMAL;
  991. }
  992. else if (strcmp(state, "ACTIVE") == 0)
  993. {
  994. return ACTIVE;
  995. }
  996. else if (strcmp(state, "FOCUS") == 0)
  997. {
  998. return FOCUS;
  999. }
  1000. else if (strcmp(state, "DISABLED") == 0)
  1001. {
  1002. return DISABLED;
  1003. }
  1004. else if (strcmp(state, "HOVER") == 0)
  1005. {
  1006. return HOVER;
  1007. }
  1008. return NORMAL;
  1009. }
  1010. Theme::ThemeImage* Control::getImage(const char* id, State state)
  1011. {
  1012. Theme::Style::Overlay* overlay = getOverlay(state);
  1013. GP_ASSERT(overlay);
  1014. Theme::ImageList* imageList = overlay->getImageList();
  1015. if (!imageList)
  1016. return NULL;
  1017. return imageList->getImage(id);
  1018. }
  1019. const char* Control::getType() const
  1020. {
  1021. return "control";
  1022. }
  1023. // Implementation of AnimationHandler
  1024. unsigned int Control::getAnimationPropertyComponentCount(int propertyId) const
  1025. {
  1026. switch(propertyId)
  1027. {
  1028. case ANIMATE_POSITION:
  1029. case ANIMATE_SIZE:
  1030. return 2;
  1031. case ANIMATE_POSITION_X:
  1032. case ANIMATE_POSITION_Y:
  1033. case ANIMATE_SIZE_WIDTH:
  1034. case ANIMATE_SIZE_HEIGHT:
  1035. case ANIMATE_OPACITY:
  1036. return 1;
  1037. default:
  1038. return -1;
  1039. }
  1040. }
  1041. void Control::getAnimationPropertyValue(int propertyId, AnimationValue* value)
  1042. {
  1043. GP_ASSERT(value);
  1044. switch(propertyId)
  1045. {
  1046. case ANIMATE_POSITION:
  1047. value->setFloat(0, _bounds.x);
  1048. value->setFloat(1, _bounds.y);
  1049. break;
  1050. case ANIMATE_SIZE:
  1051. value->setFloat(0, _bounds.width);
  1052. value->setFloat(1, _bounds.height);
  1053. break;
  1054. case ANIMATE_POSITION_X:
  1055. value->setFloat(0, _bounds.x);
  1056. break;
  1057. case ANIMATE_POSITION_Y:
  1058. value->setFloat(0, _bounds.y);
  1059. break;
  1060. case ANIMATE_SIZE_WIDTH:
  1061. value->setFloat(0, _bounds.width);
  1062. break;
  1063. case ANIMATE_SIZE_HEIGHT:
  1064. value->setFloat(0, _bounds.height);
  1065. break;
  1066. case ANIMATE_OPACITY:
  1067. value->setFloat(0, _opacity);
  1068. break;
  1069. default:
  1070. break;
  1071. }
  1072. }
  1073. void Control::setAnimationPropertyValue(int propertyId, AnimationValue* value, float blendWeight)
  1074. {
  1075. GP_ASSERT(value);
  1076. switch(propertyId)
  1077. {
  1078. case ANIMATE_POSITION:
  1079. _bounds.x = Curve::lerp(blendWeight, _bounds.x, value->getFloat(0));
  1080. _bounds.y = Curve::lerp(blendWeight, _bounds.y, value->getFloat(1));
  1081. _dirty = true;
  1082. break;
  1083. case ANIMATE_POSITION_X:
  1084. _bounds.x = Curve::lerp(blendWeight, _bounds.x, value->getFloat(0));
  1085. _dirty = true;
  1086. break;
  1087. case ANIMATE_POSITION_Y:
  1088. _bounds.y = Curve::lerp(blendWeight, _bounds.y, value->getFloat(0));
  1089. _dirty = true;
  1090. break;
  1091. case ANIMATE_SIZE:
  1092. _bounds.width = Curve::lerp(blendWeight, _bounds.width, value->getFloat(0));
  1093. _bounds.height = Curve::lerp(blendWeight, _bounds.height, value->getFloat(1));
  1094. _dirty = true;
  1095. break;
  1096. case ANIMATE_SIZE_WIDTH:
  1097. _bounds.width = Curve::lerp(blendWeight, _bounds.width, value->getFloat(0));
  1098. _dirty = true;
  1099. break;
  1100. case ANIMATE_SIZE_HEIGHT:
  1101. _bounds.height = Curve::lerp(blendWeight, _bounds.height, value->getFloat(0));
  1102. _dirty = true;
  1103. break;
  1104. case ANIMATE_OPACITY:
  1105. setOpacity(Curve::lerp(blendWeight, _opacity, value->getFloat(0)));
  1106. _dirty = true;
  1107. break;
  1108. }
  1109. }
  1110. Theme::Style::Overlay** Control::getOverlays(unsigned char overlayTypes, Theme::Style::Overlay** overlays)
  1111. {
  1112. GP_ASSERT(overlays);
  1113. GP_ASSERT(_style);
  1114. unsigned int index = 0;
  1115. if ((overlayTypes & NORMAL) == NORMAL)
  1116. {
  1117. overlays[index++] = _style->getOverlay(Theme::Style::OVERLAY_NORMAL);
  1118. }
  1119. if ((overlayTypes & FOCUS) == FOCUS)
  1120. {
  1121. overlays[index++] = _style->getOverlay(Theme::Style::OVERLAY_FOCUS);
  1122. }
  1123. if ((overlayTypes & ACTIVE) == ACTIVE)
  1124. {
  1125. overlays[index++] = _style->getOverlay(Theme::Style::OVERLAY_ACTIVE);
  1126. }
  1127. if ((overlayTypes & DISABLED) == DISABLED)
  1128. {
  1129. overlays[index++] = _style->getOverlay(Theme::Style::OVERLAY_DISABLED);
  1130. }
  1131. if ((overlayTypes & HOVER) == HOVER)
  1132. {
  1133. overlays[index++] = _style->getOverlay(Theme::Style::OVERLAY_HOVER);
  1134. }
  1135. return overlays;
  1136. }
  1137. Theme::Style::Overlay* Control::getOverlay(State state) const
  1138. {
  1139. GP_ASSERT(_style);
  1140. switch(state)
  1141. {
  1142. case Control::NORMAL:
  1143. return _style->getOverlay(Theme::Style::OVERLAY_NORMAL);
  1144. case Control::FOCUS:
  1145. return _style->getOverlay(Theme::Style::OVERLAY_FOCUS);
  1146. case Control::ACTIVE:
  1147. {
  1148. Theme::Style::Overlay* activeOverlay = _style->getOverlay(Theme::Style::OVERLAY_ACTIVE);
  1149. if (activeOverlay)
  1150. return activeOverlay;
  1151. else
  1152. return getOverlay(_previousState);
  1153. }
  1154. case Control::DISABLED:
  1155. return _style->getOverlay(Theme::Style::OVERLAY_DISABLED);
  1156. case Control::HOVER:
  1157. {
  1158. Theme::Style::Overlay* hoverOverlay = _style->getOverlay(Theme::Style::OVERLAY_HOVER);
  1159. if (hoverOverlay)
  1160. return hoverOverlay;
  1161. else
  1162. return getOverlay(_previousState);
  1163. }
  1164. default:
  1165. return NULL;
  1166. }
  1167. }
  1168. void Control::overrideStyle()
  1169. {
  1170. if (_styleOverridden)
  1171. {
  1172. return;
  1173. }
  1174. // Copy the style.
  1175. GP_ASSERT(_style);
  1176. _style = new Theme::Style(*_style);
  1177. _styleOverridden = true;
  1178. }
  1179. void Control::overrideThemedProperties(Properties* properties, unsigned char states)
  1180. {
  1181. GP_ASSERT(properties);
  1182. GP_ASSERT(_style);
  1183. GP_ASSERT(_style->_theme);
  1184. Theme::ImageList* imageList = NULL;
  1185. Theme::ThemeImage* cursor = NULL;
  1186. Theme::Skin* skin = NULL;
  1187. _style->_theme->lookUpSprites(properties, &imageList, &cursor, &skin);
  1188. if (imageList)
  1189. {
  1190. setImageList(imageList, states);
  1191. }
  1192. if (cursor)
  1193. {
  1194. setCursor(cursor, states);
  1195. }
  1196. if (skin)
  1197. {
  1198. setSkin(skin, states);
  1199. }
  1200. if (properties->exists("font"))
  1201. {
  1202. Font* font = Font::create(properties->getString("font"));
  1203. setFont(font, states);
  1204. font->release();
  1205. }
  1206. if (properties->exists("fontSize"))
  1207. {
  1208. setFontSize(properties->getInt("fontSize"), states);
  1209. }
  1210. if (properties->exists("textColor"))
  1211. {
  1212. Vector4 textColor(0, 0, 0, 1);
  1213. properties->getColor("textColor", &textColor);
  1214. setTextColor(textColor, states);
  1215. }
  1216. if (properties->exists("textAlignment"))
  1217. {
  1218. setTextAlignment(Font::getJustify(properties->getString("textAlignment")), states);
  1219. }
  1220. if (properties->exists("rightToLeft"))
  1221. {
  1222. setTextRightToLeft(properties->getBool("rightToLeft"), states);
  1223. }
  1224. if (properties->exists("opacity"))
  1225. {
  1226. setOpacity(properties->getFloat("opacity"), states);
  1227. }
  1228. }
  1229. void Control::setImageList(Theme::ImageList* imageList, unsigned char states)
  1230. {
  1231. overrideStyle();
  1232. Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
  1233. getOverlays(states, overlays);
  1234. for (int i = 0; i < Theme::Style::OVERLAY_MAX; ++i)
  1235. {
  1236. if( overlays[i] )
  1237. overlays[i]->setImageList(imageList);
  1238. }
  1239. _dirty = true;
  1240. }
  1241. void Control::setCursor(Theme::ThemeImage* cursor, unsigned char states)
  1242. {
  1243. overrideStyle();
  1244. Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
  1245. getOverlays(states, overlays);
  1246. for (int i = 0; i < Theme::Style::OVERLAY_MAX; ++i)
  1247. {
  1248. if( overlays[i] )
  1249. overlays[i]->setCursor(cursor);
  1250. }
  1251. _dirty = true;
  1252. }
  1253. void Control::setSkin(Theme::Skin* skin, unsigned char states)
  1254. {
  1255. overrideStyle();
  1256. Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
  1257. getOverlays(states, overlays);
  1258. for (int i = 0; i < Theme::Style::OVERLAY_MAX; ++i)
  1259. {
  1260. if( overlays[i] )
  1261. overlays[i]->setSkin(skin);
  1262. }
  1263. _dirty = true;
  1264. }
  1265. Theme::Skin* Control::getSkin(State state)
  1266. {
  1267. Theme::Style::Overlay* overlay = getOverlay(state);
  1268. GP_ASSERT(overlay);
  1269. return overlay->getSkin();
  1270. }
  1271. Control::Alignment Control::getAlignment(const char* alignment)
  1272. {
  1273. if (!alignment)
  1274. {
  1275. return Control::ALIGN_TOP_LEFT;
  1276. }
  1277. if (strcmp(alignment, "ALIGN_LEFT") == 0)
  1278. {
  1279. return Control::ALIGN_LEFT;
  1280. }
  1281. else if (strcmp(alignment, "ALIGN_HCENTER") == 0)
  1282. {
  1283. return Control::ALIGN_HCENTER;
  1284. }
  1285. else if (strcmp(alignment, "ALIGN_RIGHT") == 0)
  1286. {
  1287. return Control::ALIGN_RIGHT;
  1288. }
  1289. else if (strcmp(alignment, "ALIGN_TOP") == 0)
  1290. {
  1291. return Control::ALIGN_TOP;
  1292. }
  1293. else if (strcmp(alignment, "ALIGN_VCENTER") == 0)
  1294. {
  1295. return Control::ALIGN_VCENTER;
  1296. }
  1297. else if (strcmp(alignment, "ALIGN_BOTTOM") == 0)
  1298. {
  1299. return Control::ALIGN_BOTTOM;
  1300. }
  1301. else if (strcmp(alignment, "ALIGN_TOP_LEFT") == 0)
  1302. {
  1303. return Control::ALIGN_TOP_LEFT;
  1304. }
  1305. else if (strcmp(alignment, "ALIGN_VCENTER_LEFT") == 0)
  1306. {
  1307. return Control::ALIGN_VCENTER_LEFT;
  1308. }
  1309. else if (strcmp(alignment, "ALIGN_BOTTOM_LEFT") == 0)
  1310. {
  1311. return Control::ALIGN_BOTTOM_LEFT;
  1312. }
  1313. else if (strcmp(alignment, "ALIGN_TOP_HCENTER") == 0)
  1314. {
  1315. return Control::ALIGN_TOP_HCENTER;
  1316. }
  1317. else if (strcmp(alignment, "ALIGN_VCENTER_HCENTER") == 0)
  1318. {
  1319. return Control::ALIGN_VCENTER_HCENTER;
  1320. }
  1321. else if (strcmp(alignment, "ALIGN_BOTTOM_HCENTER") == 0)
  1322. {
  1323. return Control::ALIGN_BOTTOM_HCENTER;
  1324. }
  1325. else if (strcmp(alignment, "ALIGN_TOP_RIGHT") == 0)
  1326. {
  1327. return Control::ALIGN_TOP_RIGHT;
  1328. }
  1329. else if (strcmp(alignment, "ALIGN_VCENTER_RIGHT") == 0)
  1330. {
  1331. return Control::ALIGN_VCENTER_RIGHT;
  1332. }
  1333. else if (strcmp(alignment, "ALIGN_BOTTOM_RIGHT") == 0)
  1334. {
  1335. return Control::ALIGN_BOTTOM_RIGHT;
  1336. }
  1337. else
  1338. {
  1339. GP_ERROR("Failed to get corresponding control alignment for unsupported value '%s'.", alignment);
  1340. }
  1341. // Default.
  1342. return Control::ALIGN_TOP_LEFT;
  1343. }
  1344. }