Control.cpp 41 KB

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