Control.cpp 34 KB

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