Control.cpp 38 KB

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