2
0

Control.cpp 49 KB

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