Control.cpp 42 KB

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