Control.cpp 36 KB

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