Control.cpp 50 KB

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