Control.cpp 50 KB

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