Control.cpp 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454
  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. _absoluteClipBounds.set(x - border.left - padding.left, y - border.top - padding.top,
  797. width + border.left + padding.left + border.right + padding.right,
  798. height + border.top + padding.top + border.bottom + padding.bottom);
  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. if (border.left && border.right && border.top && border.bottom)
  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. }