Control.cpp 44 KB

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