Control.cpp 35 KB

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