Control.cpp 36 KB

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