Control.cpp 45 KB

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