Control.cpp 38 KB

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