Control.cpp 34 KB

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