Control.cpp 46 KB

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