Control.cpp 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225
  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()), _clip(Rectangle::empty()),
  8. _dirty(true), _consumeTouchEvents(true), _listeners(NULL), _styleOverridden(false)
  9. {
  10. }
  11. Control::Control(const Control& copy)
  12. {
  13. }
  14. Control::~Control()
  15. {
  16. if (_listeners)
  17. {
  18. for (std::map<Listener::EventType, std::list<Listener*>*>::const_iterator itr = _listeners->begin(); itr != _listeners->end(); itr++)
  19. {
  20. std::list<Listener*>* list = itr->second;
  21. SAFE_DELETE(list);
  22. }
  23. SAFE_DELETE(_listeners);
  24. }
  25. if (_styleOverridden)
  26. {
  27. SAFE_DELETE(_style);
  28. }
  29. }
  30. void Control::initialize(Theme::Style* style, Properties* properties)
  31. {
  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. Vector2 position;
  38. Vector2 size;
  39. if (properties->exists("position"))
  40. {
  41. properties->getVector2("position", &position);
  42. }
  43. else
  44. {
  45. position.x = properties->getFloat("x");
  46. position.y = properties->getFloat("y");
  47. }
  48. if (properties->exists("size"))
  49. {
  50. properties->getVector2("size", &size);
  51. }
  52. else
  53. {
  54. size.x = properties->getFloat("width");
  55. size.y = properties->getFloat("height");
  56. }
  57. _bounds.set(position.x, position.y, size.x, size.y);
  58. _state = Control::getState(properties->getString("state"));
  59. const char* id = properties->getId();
  60. if (id)
  61. _id = id;
  62. // Potentially override themed properties for all states.
  63. overrideThemedProperties(properties, STATE_ALL);
  64. // Override themed properties on specific states.
  65. Properties* innerSpace = properties->getNextNamespace();
  66. while (innerSpace != NULL)
  67. {
  68. std::string spaceName(innerSpace->getNamespace());
  69. std::transform(spaceName.begin(), spaceName.end(), spaceName.begin(), (int(*)(int))toupper);
  70. if (spaceName == "STATENORMAL")
  71. {
  72. overrideThemedProperties(innerSpace, NORMAL);
  73. }
  74. else if (spaceName == "STATEFOCUS")
  75. {
  76. overrideThemedProperties(innerSpace, FOCUS);
  77. }
  78. else if (spaceName == "STATEACTIVE")
  79. {
  80. overrideThemedProperties(innerSpace, ACTIVE);
  81. }
  82. else if (spaceName == "STATEDISABLED")
  83. {
  84. overrideThemedProperties(innerSpace, DISABLED);
  85. }
  86. else if (spaceName == "MARGIN")
  87. {
  88. setMargin(innerSpace->getFloat("top"), innerSpace->getFloat("bottom"),
  89. innerSpace->getFloat("left"), innerSpace->getFloat("right"));
  90. }
  91. else if (spaceName == "PADDING")
  92. {
  93. setPadding(innerSpace->getFloat("top"), innerSpace->getFloat("bottom"),
  94. innerSpace->getFloat("left"), innerSpace->getFloat("right"));
  95. }
  96. innerSpace = properties->getNextNamespace();
  97. }
  98. }
  99. const char* Control::getID() const
  100. {
  101. return _id.c_str();
  102. }
  103. void Control::setPosition(float x, float y)
  104. {
  105. _bounds.x = x;
  106. _bounds.y = y;
  107. _dirty = true;
  108. }
  109. void Control::setSize(float width, float height)
  110. {
  111. _bounds.width = width;
  112. _bounds.height = height;
  113. _dirty = true;
  114. }
  115. void Control::setBounds(const Rectangle& bounds)
  116. {
  117. _bounds.set(bounds);
  118. }
  119. const Rectangle& Control::getBounds() const
  120. {
  121. return _bounds;
  122. }
  123. float Control::getX() const
  124. {
  125. return _bounds.x;
  126. }
  127. float Control::getY() const
  128. {
  129. return _bounds.y;
  130. }
  131. float Control::getWidth() const
  132. {
  133. return _bounds.width;
  134. }
  135. float Control::getHeight() const
  136. {
  137. return _bounds.height;
  138. }
  139. void Control::setAlignment(Alignment alignment)
  140. {
  141. _alignment = alignment;
  142. }
  143. Control::Alignment Control::getAlignment() const
  144. {
  145. return _alignment;
  146. }
  147. void Control::setAutoWidth(bool autoWidth)
  148. {
  149. _autoWidth = autoWidth;
  150. }
  151. bool Control::getAutoWidth() const
  152. {
  153. return _autoWidth;
  154. }
  155. void Control::setAutoHeight(bool autoHeight)
  156. {
  157. _autoHeight = autoHeight;
  158. }
  159. void Control::setOpacity(float opacity, unsigned char states)
  160. {
  161. overrideStyle();
  162. Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
  163. getOverlays(states, overlays);
  164. for (int i = 0; i < Theme::Style::OVERLAY_MAX - 1 && overlays[i]; ++i)
  165. {
  166. overlays[i]->setOpacity(opacity);
  167. }
  168. _dirty = true;
  169. }
  170. float Control::getOpacity(State state) const
  171. {
  172. return getOverlay(state)->getOpacity();
  173. }
  174. void Control::setBorder(float top, float bottom, float left, float right, unsigned char states)
  175. {
  176. overrideStyle();
  177. Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
  178. getOverlays(states, overlays);
  179. for (int i = 0; i < Theme::Style::OVERLAY_MAX - 1 && overlays[i]; ++i)
  180. {
  181. overlays[i]->setBorder(top, bottom, left, right);
  182. }
  183. _dirty = true;
  184. }
  185. const Theme::Border& Control::getBorder(State state) const
  186. {
  187. return getOverlay(state)->getBorder();
  188. }
  189. void Control::setSkinRegion(const Rectangle& region, unsigned char states)
  190. {
  191. overrideStyle();
  192. Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
  193. getOverlays(states, overlays);
  194. for (int i = 0; i < Theme::Style::OVERLAY_MAX - 1 && overlays[i]; ++i)
  195. {
  196. overlays[i]->setSkinRegion(region, _style->_tw, _style->_th);
  197. }
  198. _dirty = true;
  199. }
  200. const Rectangle& Control::getSkinRegion(State state) const
  201. {
  202. return getOverlay(state)->getSkinRegion();
  203. }
  204. const Theme::UVs& Control::getSkinUVs(Theme::Skin::SkinArea area, State state) const
  205. {
  206. return getOverlay(state)->getSkinUVs(area);
  207. }
  208. void Control::setSkinColor(const Vector4& color, unsigned char states)
  209. {
  210. overrideStyle();
  211. Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
  212. getOverlays(states, overlays);
  213. for (int i = 0; i < Theme::Style::OVERLAY_MAX - 1 && overlays[i]; ++i)
  214. {
  215. overlays[i]->setSkinColor(color);
  216. }
  217. _dirty = true;
  218. }
  219. const Vector4& Control::getSkinColor(State state) const
  220. {
  221. return getOverlay(state)->getSkinColor();
  222. }
  223. void Control::setMargin(float top, float bottom, float left, float right)
  224. {
  225. _style->setMargin(top, bottom, left, right);
  226. _dirty = true;
  227. }
  228. const Theme::Margin& Control::getMargin() const
  229. {
  230. return _style->getMargin();
  231. }
  232. void Control::setPadding(float top, float bottom, float left, float right)
  233. {
  234. _style->setPadding(top, bottom, left, right);
  235. _dirty = true;
  236. }
  237. const Theme::Padding& Control::getPadding() const
  238. {
  239. return _style->getPadding();
  240. }
  241. void Control::setImageRegion(const char* id, const Rectangle& region, unsigned char states)
  242. {
  243. overrideStyle();
  244. Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
  245. getOverlays(states, overlays);
  246. for (int i = 0; i < Theme::Style::OVERLAY_MAX - 1 && overlays[i]; ++i)
  247. {
  248. overlays[i]->setImageRegion(id, region, _style->_tw, _style->_th);
  249. }
  250. _dirty = true;
  251. }
  252. const Rectangle& Control::getImageRegion(const char* id, State state) const
  253. {
  254. return getOverlay(state)->getImageRegion(id);
  255. }
  256. void Control::setImageColor(const char* id, const Vector4& color, unsigned char states)
  257. {
  258. overrideStyle();
  259. Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
  260. getOverlays(states, overlays);
  261. for (int i = 0; i < Theme::Style::OVERLAY_MAX - 1 && overlays[i]; ++i)
  262. {
  263. overlays[i]->setImageColor(id, color);
  264. }
  265. _dirty = true;
  266. }
  267. const Vector4& Control::getImageColor(const char* id, State state) const
  268. {
  269. return getOverlay(state)->getImageColor(id);
  270. }
  271. const Theme::UVs& Control::getImageUVs(const char* id, State state) const
  272. {
  273. return getOverlay(state)->getImageUVs(id);
  274. }
  275. void Control::setCursorRegion(const Rectangle& region, unsigned char states)
  276. {
  277. overrideStyle();
  278. Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
  279. getOverlays(states, overlays);
  280. for (int i = 0; i < Theme::Style::OVERLAY_MAX - 1 && overlays[i]; ++i)
  281. {
  282. overlays[i]->setCursorRegion(region, _style->_tw, _style->_th);
  283. }
  284. _dirty = true;
  285. }
  286. const Rectangle& Control::getCursorRegion(State state) const
  287. {
  288. return getOverlay(state)->getCursorRegion();
  289. }
  290. void Control::setCursorColor(const Vector4& color, unsigned char states)
  291. {
  292. overrideStyle();
  293. Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
  294. getOverlays(states, overlays);
  295. for (int i = 0; i < Theme::Style::OVERLAY_MAX - 1 && overlays[i]; ++i)
  296. {
  297. overlays[i]->setCursorColor(color);
  298. }
  299. _dirty = true;
  300. }
  301. const Vector4& Control::getCursorColor(State state)
  302. {
  303. return getOverlay(state)->getCursorColor();
  304. }
  305. const Theme::UVs& Control::getCursorUVs(State state)
  306. {
  307. return getOverlay(state)->getCursorUVs();
  308. }
  309. void Control::setFont(Font* font, unsigned char states)
  310. {
  311. overrideStyle();
  312. Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
  313. getOverlays(states, overlays);
  314. for (int i = 0; i < Theme::Style::OVERLAY_MAX - 1 && overlays[i]; ++i)
  315. {
  316. overlays[i]->setFont(font);
  317. }
  318. _dirty = true;
  319. }
  320. Font* Control::getFont(State state) const
  321. {
  322. return getOverlay(state)->getFont();
  323. }
  324. void Control::setFontSize(unsigned int fontSize, unsigned char states)
  325. {
  326. overrideStyle();
  327. Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
  328. getOverlays(states, overlays);
  329. for (int i = 0; i < Theme::Style::OVERLAY_MAX - 1 && overlays[i]; ++i)
  330. {
  331. overlays[i]->setFontSize(fontSize);
  332. }
  333. _dirty = true;
  334. }
  335. unsigned int Control::getFontSize(State state) const
  336. {
  337. return getOverlay(state)->getFontSize();
  338. }
  339. void Control::setTextColor(const Vector4& color, unsigned char states)
  340. {
  341. overrideStyle();
  342. Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
  343. getOverlays(states, overlays);
  344. for (int i = 0; i < Theme::Style::OVERLAY_MAX - 1 && overlays[i]; ++i)
  345. {
  346. overlays[i]->setTextColor(color);
  347. }
  348. _dirty = true;
  349. }
  350. const Vector4& Control::getTextColor(State state) const
  351. {
  352. return getOverlay(state)->getTextColor();
  353. }
  354. void Control::setTextAlignment(Font::Justify alignment, 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 - 1 && overlays[i]; ++i)
  360. {
  361. overlays[i]->setTextAlignment(alignment);
  362. }
  363. _dirty = true;
  364. }
  365. Font::Justify Control::getTextAlignment(State state) const
  366. {
  367. return getOverlay(state)->getTextAlignment();
  368. }
  369. void Control::setTextRightToLeft(bool rightToLeft, unsigned char states)
  370. {
  371. overrideStyle();
  372. Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
  373. getOverlays(states, overlays);
  374. for (int i = 0; i < Theme::Style::OVERLAY_MAX - 1 && overlays[i]; ++i)
  375. {
  376. overlays[i]->setTextRightToLeft(rightToLeft);
  377. }
  378. _dirty = true;
  379. }
  380. bool Control::getTextRightToLeft(State state) const
  381. {
  382. return getOverlay(state)->getTextRightToLeft();
  383. }
  384. const Rectangle& Control::getClipBounds() const
  385. {
  386. return _clipBounds;
  387. }
  388. const Rectangle& Control::getClip() const
  389. {
  390. return _clip;
  391. }
  392. void Control::setStyle(Theme::Style* style)
  393. {
  394. if (style != _style)
  395. {
  396. _dirty = true;
  397. }
  398. _style = style;
  399. }
  400. Theme::Style* Control::getStyle() const
  401. {
  402. return _style;
  403. }
  404. void Control::setState(State state)
  405. {
  406. _state = state;
  407. _dirty = true;
  408. }
  409. Control::State Control::getState() const
  410. {
  411. return _state;
  412. }
  413. void Control::disable()
  414. {
  415. _state = DISABLED;
  416. _dirty = true;
  417. }
  418. void Control::enable()
  419. {
  420. _state = NORMAL;
  421. _dirty = true;
  422. }
  423. bool Control::isEnabled()
  424. {
  425. return _state != DISABLED;
  426. }
  427. Theme::Style::OverlayType Control::getOverlayType() const
  428. {
  429. switch (_state)
  430. {
  431. case Control::NORMAL:
  432. return Theme::Style::OVERLAY_NORMAL;
  433. case Control::FOCUS:
  434. return Theme::Style::OVERLAY_FOCUS;
  435. case Control::ACTIVE:
  436. return Theme::Style::OVERLAY_ACTIVE;
  437. case Control::DISABLED:
  438. return Theme::Style::OVERLAY_DISABLED;
  439. default:
  440. return Theme::Style::OVERLAY_NORMAL;
  441. }
  442. }
  443. void Control::setConsumeTouchEvents(bool consume)
  444. {
  445. _consumeTouchEvents = consume;
  446. }
  447. bool Control::getConsumeTouchEvents()
  448. {
  449. return _consumeTouchEvents;
  450. }
  451. void Control::addListener(Control::Listener* listener, int eventFlags)
  452. {
  453. if ((eventFlags & Listener::PRESS) == Listener::PRESS)
  454. {
  455. addSpecificListener(listener, Listener::PRESS);
  456. }
  457. if ((eventFlags & Listener::RELEASE) == Listener::RELEASE)
  458. {
  459. addSpecificListener(listener, Listener::RELEASE);
  460. }
  461. if ((eventFlags & Listener::CLICK) == Listener::CLICK)
  462. {
  463. addSpecificListener(listener, Listener::CLICK);
  464. }
  465. if ((eventFlags & Listener::VALUE_CHANGED) == Listener::VALUE_CHANGED)
  466. {
  467. addSpecificListener(listener, Listener::VALUE_CHANGED);
  468. }
  469. if ((eventFlags & Listener::TEXT_CHANGED) == Listener::TEXT_CHANGED)
  470. {
  471. addSpecificListener(listener, Listener::TEXT_CHANGED);
  472. }
  473. }
  474. void Control::addSpecificListener(Control::Listener* listener, Listener::EventType eventType)
  475. {
  476. if (!_listeners)
  477. {
  478. _listeners = new std::map<Listener::EventType, std::list<Listener*>*>();
  479. }
  480. std::map<Listener::EventType, std::list<Listener*>*>::const_iterator itr = _listeners->find(eventType);
  481. if (itr == _listeners->end())
  482. {
  483. _listeners->insert(std::make_pair(eventType, new std::list<Listener*>()));
  484. itr = _listeners->find(eventType);
  485. }
  486. std::list<Listener*>* listenerList = itr->second;
  487. listenerList->push_back(listener);
  488. }
  489. bool Control::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  490. {
  491. if (!isEnabled())
  492. {
  493. return false;
  494. }
  495. switch (evt)
  496. {
  497. case Touch::TOUCH_PRESS:
  498. notifyListeners(Listener::PRESS);
  499. break;
  500. case Touch::TOUCH_RELEASE:
  501. // Always trigger Listener::RELEASE
  502. notifyListeners(Listener::RELEASE);
  503. // Only trigger Listener::CLICK if both PRESS and RELEASE took place within the control's bounds.
  504. if (x > 0 && x <= _bounds.width &&
  505. y > 0 && y <= _bounds.height)
  506. {
  507. notifyListeners(Listener::CLICK);
  508. }
  509. break;
  510. }
  511. return _consumeTouchEvents;
  512. }
  513. void Control::keyEvent(Keyboard::KeyEvent evt, int key)
  514. {
  515. }
  516. void Control::notifyListeners(Listener::EventType eventType)
  517. {
  518. if (_listeners)
  519. {
  520. std::map<Listener::EventType, std::list<Listener*>*>::const_iterator itr = _listeners->find(eventType);
  521. if (itr != _listeners->end())
  522. {
  523. std::list<Listener*>* listenerList = itr->second;
  524. for (std::list<Listener*>::iterator listenerItr = listenerList->begin(); listenerItr != listenerList->end(); listenerItr++)
  525. {
  526. (*listenerItr)->controlEvent(this, eventType);
  527. }
  528. }
  529. }
  530. }
  531. void Control::update(const Rectangle& clip, const Vector2& offset)
  532. {
  533. // Calculate the clipped bounds.
  534. float x = _bounds.x + offset.x;
  535. float y = _bounds.y + offset.y;
  536. float width = _bounds.width;
  537. float height = _bounds.height;
  538. float clipX2 = clip.x + clip.width;
  539. float x2 = clip.x + x + width;
  540. if (x2 > clipX2)
  541. width -= x2 - clipX2;
  542. float clipY2 = clip.y + clip.height;
  543. float y2 = clip.y + y + height;
  544. if (y2 > clipY2)
  545. height -= y2 - clipY2;
  546. if (x < 0)
  547. {
  548. width += x;
  549. x = -x;
  550. }
  551. else
  552. {
  553. x = 0;
  554. }
  555. if (y < 0)
  556. {
  557. height += y;
  558. y = -y;
  559. }
  560. else
  561. {
  562. y = 0;
  563. }
  564. _clipBounds.set(x, y, width, height);
  565. // Calculate the absolute bounds.
  566. x = _bounds.x + offset.x + clip.x;
  567. y = _bounds.y + offset.y + clip.y;
  568. _absoluteBounds.set(x, y, _bounds.width, _bounds.height);
  569. // Calculate the absolute viewport bounds.
  570. // Absolute bounds minus border and padding.
  571. const Theme::Border& border = getBorder(_state);
  572. const Theme::Padding& padding = getPadding();
  573. x += border.left + padding.left;
  574. y += border.top + padding.top;
  575. width = _bounds.width - border.left - padding.left - border.right - padding.right;
  576. height = _bounds.height - border.top - padding.top - border.bottom - padding.bottom;
  577. _viewportBounds.set(x, y, width, height);
  578. // Calculate the clip area.
  579. // Absolute bounds, minus border and padding,
  580. // clipped to the parent container's clip area.
  581. clipX2 = clip.x + clip.width;
  582. x2 = x + width;
  583. if (x2 > clipX2)
  584. width = clipX2 - x;
  585. clipY2 = clip.y + clip.height;
  586. y2 = y + height;
  587. if (y2 > clipY2)
  588. height = clipY2 - y;
  589. if (x < clip.x)
  590. {
  591. float dx = clip.x - x;
  592. width -= dx;
  593. x = clip.x;
  594. }
  595. if (y < clip.y)
  596. {
  597. float dy = clip.y - y;
  598. height -= dy;
  599. y = clip.y;
  600. }
  601. _clip.set(x, y, width, height);
  602. // Cache themed attributes for performance.
  603. _skin = getSkin(_state);
  604. _opacity = getOpacity(_state);
  605. }
  606. void Control::drawBorder(SpriteBatch* spriteBatch, const Rectangle& clip, const Vector2& offset)
  607. {
  608. if (!_skin || _bounds.width <= 0 || _bounds.height <= 0)
  609. return;
  610. // Get the border and background images for this control's current state.
  611. const Theme::UVs& topLeft = _skin->getUVs(Theme::Skin::TOP_LEFT);
  612. const Theme::UVs& top = _skin->getUVs(Theme::Skin::TOP);
  613. const Theme::UVs& topRight = _skin->getUVs(Theme::Skin::TOP_RIGHT);
  614. const Theme::UVs& left = _skin->getUVs(Theme::Skin::LEFT);
  615. const Theme::UVs& center = _skin->getUVs(Theme::Skin::CENTER);
  616. const Theme::UVs& right = _skin->getUVs(Theme::Skin::RIGHT);
  617. const Theme::UVs& bottomLeft = _skin->getUVs(Theme::Skin::BOTTOM_LEFT);
  618. const Theme::UVs& bottom = _skin->getUVs(Theme::Skin::BOTTOM);
  619. const Theme::UVs& bottomRight = _skin->getUVs(Theme::Skin::BOTTOM_RIGHT);
  620. // Calculate screen-space positions.
  621. const Theme::Border& border = getBorder(_state);
  622. const Theme::Padding& padding = getPadding();
  623. Vector4 skinColor = _skin->getColor();
  624. skinColor.w *= _opacity;
  625. float midWidth = _bounds.width - border.left - border.right;
  626. float midHeight = _bounds.height - border.top - border.bottom;
  627. float midX = _absoluteBounds.x + border.left;
  628. float midY = _absoluteBounds.y + border.top;
  629. float rightX = _absoluteBounds.x + _bounds.width - border.right;
  630. float bottomY = _absoluteBounds.y + _bounds.height - border.bottom;
  631. // Draw themed border sprites.
  632. if (!border.left && !border.right && !border.top && !border.bottom)
  633. {
  634. // No border, just draw the image.
  635. spriteBatch->draw(_absoluteBounds.x, _absoluteBounds.y, _bounds.width, _bounds.height, center.u1, center.v1, center.u2, center.v2, skinColor, clip);
  636. }
  637. else
  638. {
  639. if (border.left && border.top)
  640. spriteBatch->draw(_absoluteBounds.x, _absoluteBounds.y, border.left, border.top, topLeft.u1, topLeft.v1, topLeft.u2, topLeft.v2, skinColor, clip);
  641. if (border.top)
  642. spriteBatch->draw(_absoluteBounds.x + border.left, _absoluteBounds.y, midWidth, border.top, top.u1, top.v1, top.u2, top.v2, skinColor, clip);
  643. if (border.right && border.top)
  644. spriteBatch->draw(rightX, _absoluteBounds.y, border.right, border.top, topRight.u1, topRight.v1, topRight.u2, topRight.v2, skinColor, clip);
  645. if (border.left)
  646. spriteBatch->draw(_absoluteBounds.x, midY, border.left, midHeight, left.u1, left.v1, left.u2, left.v2, skinColor, clip);
  647. if (border.left && border.right && border.top && border.bottom)
  648. spriteBatch->draw(_absoluteBounds.x + border.left, _absoluteBounds.y + border.top, _bounds.width - border.left - border.right, _bounds.height - border.top - border.bottom,
  649. center.u1, center.v1, center.u2, center.v2, skinColor, clip);
  650. if (border.right)
  651. spriteBatch->draw(rightX, midY, border.right, midHeight, right.u1, right.v1, right.u2, right.v2, skinColor, clip);
  652. if (border.bottom && border.left)
  653. spriteBatch->draw(_absoluteBounds.x, bottomY, border.left, border.bottom, bottomLeft.u1, bottomLeft.v1, bottomLeft.u2, bottomLeft.v2, skinColor, clip);
  654. if (border.bottom)
  655. spriteBatch->draw(midX, bottomY, midWidth, border.bottom, bottom.u1, bottom.v1, bottom.u2, bottom.v2, skinColor, clip);
  656. if (border.bottom && border.right)
  657. spriteBatch->draw(rightX, bottomY, border.right, border.bottom, bottomRight.u1, bottomRight.v1, bottomRight.u2, bottomRight.v2, skinColor, clip);
  658. }
  659. }
  660. void Control::drawImages(SpriteBatch* spriteBatch, const Rectangle& position)
  661. {
  662. }
  663. void Control::drawText(const Rectangle& position)
  664. {
  665. }
  666. bool Control::isDirty()
  667. {
  668. return _dirty;
  669. }
  670. bool Control::isContainer()
  671. {
  672. return false;
  673. }
  674. Control::State Control::getState(const char* state)
  675. {
  676. if (!state)
  677. {
  678. return NORMAL;
  679. }
  680. if (strcmp(state, "NORMAL") == 0)
  681. {
  682. return NORMAL;
  683. }
  684. else if (strcmp(state, "ACTIVE") == 0)
  685. {
  686. return ACTIVE;
  687. }
  688. else if (strcmp(state, "FOCUS") == 0)
  689. {
  690. return FOCUS;
  691. }
  692. else if (strcmp(state, "DISABLED") == 0)
  693. {
  694. return DISABLED;
  695. }
  696. return NORMAL;
  697. }
  698. Theme::ThemeImage* Control::getImage(const char* id, State state)
  699. {
  700. return getOverlay(state)->getImageList()->getImage(id);
  701. }
  702. // Implementation of AnimationHandler
  703. unsigned int Control::getAnimationPropertyComponentCount(int propertyId) const
  704. {
  705. switch(propertyId)
  706. {
  707. case ANIMATE_POSITION:
  708. case ANIMATE_SIZE:
  709. return 2;
  710. case ANIMATE_POSITION_X:
  711. case ANIMATE_POSITION_Y:
  712. case ANIMATE_SIZE_WIDTH:
  713. case ANIMATE_SIZE_HEIGHT:
  714. case ANIMATE_OPACITY:
  715. return 1;
  716. default:
  717. return -1;
  718. }
  719. }
  720. void Control::getAnimationPropertyValue(int propertyId, AnimationValue* value)
  721. {
  722. switch(propertyId)
  723. {
  724. case ANIMATE_POSITION:
  725. value->setFloat(0, _bounds.x);
  726. value->setFloat(1, _bounds.y);
  727. break;
  728. case ANIMATE_SIZE:
  729. value->setFloat(0, _bounds.width);
  730. value->setFloat(1, _bounds.height);
  731. break;
  732. case ANIMATE_POSITION_X:
  733. value->setFloat(0, _bounds.x);
  734. break;
  735. case ANIMATE_POSITION_Y:
  736. value->setFloat(0, _bounds.y);
  737. break;
  738. case ANIMATE_SIZE_WIDTH:
  739. value->setFloat(0, _bounds.width);
  740. break;
  741. case ANIMATE_SIZE_HEIGHT:
  742. value->setFloat(0, _bounds.height);
  743. break;
  744. case ANIMATE_OPACITY:
  745. default:
  746. break;
  747. }
  748. }
  749. void Control::setAnimationPropertyValue(int propertyId, AnimationValue* value, float blendWeight)
  750. {
  751. switch(propertyId)
  752. {
  753. case ANIMATE_POSITION:
  754. applyAnimationValuePositionX(value->getFloat(0), blendWeight);
  755. applyAnimationValuePositionY(value->getFloat(1), blendWeight);
  756. break;
  757. case ANIMATE_POSITION_X:
  758. applyAnimationValuePositionX(value->getFloat(0), blendWeight);
  759. break;
  760. case ANIMATE_POSITION_Y:
  761. applyAnimationValuePositionY(value->getFloat(0), blendWeight);
  762. break;
  763. case ANIMATE_SIZE:
  764. applyAnimationValueSizeWidth(value->getFloat(0), blendWeight);
  765. applyAnimationValueSizeHeight(value->getFloat(1), blendWeight);
  766. break;
  767. case ANIMATE_SIZE_WIDTH:
  768. applyAnimationValueSizeWidth(value->getFloat(0), blendWeight);
  769. break;
  770. case ANIMATE_SIZE_HEIGHT:
  771. applyAnimationValueSizeHeight(value->getFloat(0), blendWeight);
  772. break;
  773. case ANIMATE_OPACITY:
  774. applyAnimationValueOpacity();
  775. default:
  776. break;
  777. }
  778. }
  779. void Control::applyAnimationValuePositionX(float x, float blendWeight)
  780. {
  781. if ((_animationPropertyBitFlag & ANIMATION_POSITION_X_BIT) != ANIMATION_POSITION_X_BIT)
  782. {
  783. _animationPropertyBitFlag |= ANIMATION_POSITION_X_BIT;
  784. }
  785. else
  786. {
  787. x = Curve::lerp(blendWeight, _bounds.x, x);
  788. }
  789. _bounds.x = x;
  790. _dirty = true;
  791. }
  792. void Control::applyAnimationValuePositionY(float y, float blendWeight)
  793. {
  794. if ((_animationPropertyBitFlag & ANIMATION_POSITION_Y_BIT) != ANIMATION_POSITION_Y_BIT)
  795. {
  796. _animationPropertyBitFlag |= ANIMATION_POSITION_Y_BIT;
  797. }
  798. else
  799. {
  800. y = Curve::lerp(blendWeight, _bounds.y, y);
  801. }
  802. _bounds.y = y;
  803. _dirty = true;
  804. }
  805. void Control::applyAnimationValueSizeWidth(float width, float blendWeight)
  806. {
  807. if ((_animationPropertyBitFlag & ANIMATION_SIZE_WIDTH_BIT) != ANIMATION_SIZE_WIDTH_BIT)
  808. {
  809. _animationPropertyBitFlag |= ANIMATION_SIZE_WIDTH_BIT;
  810. }
  811. else
  812. {
  813. width = Curve::lerp(blendWeight, _bounds.width, width);
  814. }
  815. _bounds.width = width;
  816. _dirty = true;
  817. }
  818. void Control::applyAnimationValueSizeHeight(float height, float blendWeight)
  819. {
  820. if ((_animationPropertyBitFlag & ANIMATION_SIZE_HEIGHT_BIT) != ANIMATION_SIZE_HEIGHT_BIT)
  821. {
  822. _animationPropertyBitFlag |= ANIMATION_SIZE_HEIGHT_BIT;
  823. }
  824. else
  825. {
  826. height = Curve::lerp(blendWeight, _bounds.height, height);
  827. }
  828. _bounds.height = height;
  829. _dirty = true;
  830. }
  831. void Control::applyAnimationValueOpacity()
  832. {
  833. if ((_animationPropertyBitFlag & ANIMATION_OPACITY_BIT) != ANIMATION_OPACITY_BIT)
  834. {
  835. _animationPropertyBitFlag |= ANIMATION_OPACITY_BIT;
  836. }
  837. _dirty = true;
  838. }
  839. Theme::Style::Overlay** Control::getOverlays(unsigned char overlayTypes, Theme::Style::Overlay** overlays)
  840. {
  841. unsigned int index = 0;
  842. if ((overlayTypes & NORMAL) == NORMAL)
  843. {
  844. overlays[index++] = _style->getOverlay(Theme::Style::OVERLAY_NORMAL);
  845. }
  846. if ((overlayTypes & FOCUS) == FOCUS)
  847. {
  848. overlays[index++] = _style->getOverlay(Theme::Style::OVERLAY_FOCUS);
  849. }
  850. if ((overlayTypes & ACTIVE) == ACTIVE)
  851. {
  852. overlays[index++] = _style->getOverlay(Theme::Style::OVERLAY_ACTIVE);
  853. }
  854. if ((overlayTypes & DISABLED) == DISABLED)
  855. {
  856. overlays[index++] = _style->getOverlay(Theme::Style::OVERLAY_DISABLED);
  857. }
  858. return overlays;
  859. }
  860. Theme::Style::Overlay* Control::getOverlay(State state) const
  861. {
  862. switch(state)
  863. {
  864. case Control::NORMAL:
  865. return _style->getOverlay(Theme::Style::OVERLAY_NORMAL);
  866. case Control::FOCUS:
  867. return _style->getOverlay(Theme::Style::OVERLAY_FOCUS);
  868. case Control::ACTIVE:
  869. return _style->getOverlay(Theme::Style::OVERLAY_ACTIVE);
  870. case Control::DISABLED:
  871. return _style->getOverlay(Theme::Style::OVERLAY_DISABLED);
  872. default:
  873. return NULL;
  874. }
  875. }
  876. void Control::overrideStyle()
  877. {
  878. if (_styleOverridden)
  879. {
  880. return;
  881. }
  882. // Copy the style.
  883. WARN_VARG("%d", sizeof(Theme::Style::Overlay));
  884. _style = new Theme::Style(*_style);
  885. _styleOverridden = true;
  886. }
  887. void Control::overrideThemedProperties(Properties* properties, unsigned char states)
  888. {
  889. Theme::ImageList* imageList = NULL;
  890. Theme::ThemeImage* cursor = NULL;
  891. Theme::Skin* skin = NULL;
  892. _style->_theme->lookUpSprites(properties, &imageList, &cursor, &skin);
  893. if (imageList)
  894. {
  895. setImageList(imageList, states);
  896. }
  897. if (cursor)
  898. {
  899. setCursor(cursor, states);
  900. }
  901. if (skin)
  902. {
  903. setSkin(skin, states);
  904. }
  905. if (properties->exists("font"))
  906. {
  907. Font* font = Font::create(properties->getString("font"));
  908. setFont(font, states);
  909. font->release();
  910. }
  911. if (properties->exists("fontSize"))
  912. {
  913. setFontSize(properties->getInt("fontSize"), states);
  914. }
  915. if (properties->exists("textColor"))
  916. {
  917. Vector4 textColor(0, 0, 0, 1);
  918. properties->getColor("textColor", &textColor);
  919. setTextColor(textColor, states);
  920. }
  921. if (properties->exists("textAlignment"))
  922. {
  923. setTextAlignment(Font::getJustify(properties->getString("textAlignment")), states);
  924. }
  925. if (properties->exists("rightToLeft"))
  926. {
  927. setTextRightToLeft(properties->getBool("rightToLeft"), states);
  928. }
  929. if (properties->exists("opacity"))
  930. {
  931. setOpacity(properties->getFloat("opacity"), states);
  932. }
  933. }
  934. void Control::setImageList(Theme::ImageList* imageList, unsigned char states)
  935. {
  936. overrideStyle();
  937. Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
  938. getOverlays(states, overlays);
  939. for (int i = 0; i < Theme::Style::OVERLAY_MAX - 1 && overlays[i]; ++i)
  940. {
  941. overlays[i]->setImageList(imageList);
  942. }
  943. _dirty = true;
  944. }
  945. void Control::setCursor(Theme::ThemeImage* cursor, unsigned char states)
  946. {
  947. overrideStyle();
  948. Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
  949. getOverlays(states, overlays);
  950. for (int i = 0; i < Theme::Style::OVERLAY_MAX - 1 && overlays[i]; ++i)
  951. {
  952. overlays[i]->setCursor(cursor);
  953. }
  954. _dirty = true;
  955. }
  956. void Control::setSkin(Theme::Skin* skin, unsigned char states)
  957. {
  958. overrideStyle();
  959. Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
  960. getOverlays(states, overlays);
  961. for (int i = 0; i < Theme::Style::OVERLAY_MAX - 1 && overlays[i]; ++i)
  962. {
  963. overlays[i]->setSkin(skin);
  964. }
  965. _dirty = true;
  966. }
  967. Theme::Skin* Control::getSkin(State state)
  968. {
  969. return getOverlay(state)->getSkin();
  970. }
  971. Control::Alignment Control::getAlignment(const char* alignment)
  972. {
  973. if (!alignment)
  974. {
  975. return Control::ALIGN_TOP_LEFT;
  976. }
  977. if (strcmp(alignment, "ALIGN_LEFT") == 0)
  978. {
  979. return Control::ALIGN_LEFT;
  980. }
  981. else if (strcmp(alignment, "ALIGN_HCENTER") == 0)
  982. {
  983. return Control::ALIGN_HCENTER;
  984. }
  985. else if (strcmp(alignment, "ALIGN_RIGHT") == 0)
  986. {
  987. return Control::ALIGN_RIGHT;
  988. }
  989. else if (strcmp(alignment, "ALIGN_TOP") == 0)
  990. {
  991. return Control::ALIGN_TOP;
  992. }
  993. else if (strcmp(alignment, "ALIGN_VCENTER") == 0)
  994. {
  995. return Control::ALIGN_VCENTER;
  996. }
  997. else if (strcmp(alignment, "ALIGN_BOTTOM") == 0)
  998. {
  999. return Control::ALIGN_BOTTOM;
  1000. }
  1001. else if (strcmp(alignment, "ALIGN_TOP_LEFT") == 0)
  1002. {
  1003. return Control::ALIGN_TOP_LEFT;
  1004. }
  1005. else if (strcmp(alignment, "ALIGN_VCENTER_LEFT") == 0)
  1006. {
  1007. return Control::ALIGN_VCENTER_LEFT;
  1008. }
  1009. else if (strcmp(alignment, "ALIGN_BOTTOM_LEFT") == 0)
  1010. {
  1011. return Control::ALIGN_BOTTOM_LEFT;
  1012. }
  1013. else if (strcmp(alignment, "ALIGN_TOP_HCENTER") == 0)
  1014. {
  1015. return Control::ALIGN_TOP_HCENTER;
  1016. }
  1017. else if (strcmp(alignment, "ALIGN_VCENTER_HCENTER") == 0)
  1018. {
  1019. return Control::ALIGN_VCENTER_HCENTER;
  1020. }
  1021. else if (strcmp(alignment, "ALIGN_BOTTOM_HCENTER") == 0)
  1022. {
  1023. return Control::ALIGN_BOTTOM_HCENTER;
  1024. }
  1025. else if (strcmp(alignment, "ALIGN_TOP_RIGHT") == 0)
  1026. {
  1027. return Control::ALIGN_TOP_RIGHT;
  1028. }
  1029. else if (strcmp(alignment, "ALIGN_VCENTER_RIGHT") == 0)
  1030. {
  1031. return Control::ALIGN_VCENTER_RIGHT;
  1032. }
  1033. else if (strcmp(alignment, "ALIGN_BOTTOM_RIGHT") == 0)
  1034. {
  1035. return Control::ALIGN_BOTTOM_RIGHT;
  1036. }
  1037. // Default.
  1038. return Control::ALIGN_TOP_LEFT;
  1039. }
  1040. }