Control.cpp 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881
  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. if ((eventFlags & Control::Listener::ACTIVATED) == Control::Listener::ACTIVATED)
  845. {
  846. addSpecificListener(listener, Control::Listener::ACTIVATED);
  847. }
  848. }
  849. void Control::removeListener(Control::Listener* listener)
  850. {
  851. if (_listeners == NULL || listener == NULL)
  852. return;
  853. for (std::map<Control::Listener::EventType, std::list<Control::Listener*>*>::iterator itr = _listeners->begin(); itr != _listeners->end();)
  854. {
  855. itr->second->remove(listener);
  856. if(itr->second->empty())
  857. {
  858. std::list<Control::Listener*>* list = itr->second;
  859. _listeners->erase(itr++);
  860. SAFE_DELETE(list);
  861. }
  862. else
  863. ++itr;
  864. }
  865. if (_listeners->empty())
  866. SAFE_DELETE(_listeners);
  867. }
  868. void Control::addSpecificListener(Control::Listener* listener, Control::Listener::EventType eventType)
  869. {
  870. GP_ASSERT(listener);
  871. if (!_listeners)
  872. {
  873. _listeners = new std::map<Control::Listener::EventType, std::list<Control::Listener*>*>();
  874. }
  875. std::map<Control::Listener::EventType, std::list<Control::Listener*>*>::const_iterator itr = _listeners->find(eventType);
  876. if (itr == _listeners->end())
  877. {
  878. _listeners->insert(std::make_pair(eventType, new std::list<Control::Listener*>()));
  879. itr = _listeners->find(eventType);
  880. }
  881. std::list<Control::Listener*>* listenerList = itr->second;
  882. listenerList->push_back(listener);
  883. }
  884. bool Control::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  885. {
  886. return false;
  887. }
  888. bool Control::keyEvent(Keyboard::KeyEvent evt, int key)
  889. {
  890. return false;
  891. }
  892. bool Control::mouseEvent(Mouse::MouseEvent evt, int x, int y, int wheelDelta)
  893. {
  894. // Return false instead of _consumeInputEvents to allow handling to be
  895. // routed to touchEvent before consuming.
  896. return false;
  897. }
  898. bool Control::gamepadButtonEvent(Gamepad* gamepad)
  899. {
  900. return false;
  901. }
  902. bool Control::gamepadTriggerEvent(Gamepad* gamepad, unsigned int index)
  903. {
  904. return false;
  905. }
  906. bool Control::gamepadJoystickEvent(Gamepad* gamepad, unsigned int index)
  907. {
  908. return false;
  909. }
  910. void Control::notifyListeners(Control::Listener::EventType eventType)
  911. {
  912. // This method runs untrusted code by notifying listeners of events.
  913. // If the user calls exit() or otherwise releases this control, we
  914. // need to keep it alive until the method returns.
  915. this->addRef();
  916. controlEvent(eventType);
  917. if (_listeners)
  918. {
  919. std::map<Control::Listener::EventType, std::list<Control::Listener*>*>::const_iterator itr = _listeners->find(eventType);
  920. if (itr != _listeners->end())
  921. {
  922. std::list<Control::Listener*>* listenerList = itr->second;
  923. for (std::list<Control::Listener*>::iterator listenerItr = listenerList->begin(); listenerItr != listenerList->end(); ++listenerItr)
  924. {
  925. GP_ASSERT(*listenerItr);
  926. (*listenerItr)->controlEvent(this, eventType);
  927. }
  928. }
  929. }
  930. fireScriptEvent<void>(GP_GET_SCRIPT_EVENT(Control, controlEvent), dynamic_cast<void*>(this), eventType);
  931. this->release();
  932. }
  933. void Control::controlEvent(Control::Listener::EventType evt)
  934. {
  935. }
  936. void Control::setDirty(int bits)
  937. {
  938. _dirtyBits |= bits;
  939. }
  940. bool Control::isDirty(int bit) const
  941. {
  942. return (_dirtyBits & bit) == bit;
  943. }
  944. void Control::update(float elapsedTime)
  945. {
  946. State state = getState();
  947. // Update visual state if it's dirty
  948. if (_dirtyBits & DIRTY_STATE)
  949. updateState(getState());
  950. // Since opacity is pre-multiplied, we compute it every frame so that we don't need to
  951. // dirty the entire hierarchy any time a state changes (which could affect opacity).
  952. _opacity = getOpacity(state);
  953. if (_parent)
  954. _opacity *= _parent->_opacity;
  955. }
  956. void Control::updateState(State state)
  957. {
  958. // Clear dirty state bit
  959. _dirtyBits &= ~DIRTY_STATE;
  960. // Cache themed attributes for performance.
  961. _skin = getSkin(state);
  962. }
  963. bool Control::updateBoundsInternal(const Vector2& offset)
  964. {
  965. // If our state is currently dirty, update it here so that any rendering state objects needed
  966. // for bounds computation are accessible.
  967. State state = getState();
  968. if (_dirtyBits & DIRTY_STATE)
  969. {
  970. updateState(state);
  971. _dirtyBits &= ~DIRTY_STATE;
  972. }
  973. // Clear our dirty bounds bit
  974. bool dirtyBounds = (_dirtyBits & DIRTY_BOUNDS) != 0;
  975. _dirtyBits &= ~DIRTY_BOUNDS;
  976. // If we are a container, always update child bounds first
  977. bool changed = false;
  978. if (isContainer())
  979. changed = static_cast<Container*>(this)->updateChildBounds();
  980. if (dirtyBounds)
  981. {
  982. // Store old bounds so we can determine if they change
  983. Rectangle oldAbsoluteBounds(_absoluteBounds);
  984. Rectangle oldAbsoluteClipBounds(_absoluteClipBounds);
  985. Rectangle oldViewportBounds(_viewportBounds);
  986. Rectangle oldViewportClipBounds(_viewportClipBounds);
  987. updateBounds();
  988. updateAbsoluteBounds(offset);
  989. if (_absoluteBounds != oldAbsoluteBounds ||
  990. _absoluteClipBounds != oldAbsoluteClipBounds ||
  991. _viewportBounds != oldViewportBounds ||
  992. _viewportClipBounds != oldViewportClipBounds)
  993. {
  994. if (isContainer())
  995. static_cast<Container*>(this)->setChildrenDirty(DIRTY_BOUNDS, true);
  996. changed = true;
  997. }
  998. }
  999. return changed;
  1000. }
  1001. void Control::updateBounds()
  1002. {
  1003. Game* game = Game::getInstance();
  1004. const Rectangle parentAbsoluteBounds = _parent ? _parent->_viewportBounds : Rectangle(0, 0, game->getViewport().width, game->getViewport().height);
  1005. // Calculate local unclipped bounds.
  1006. _bounds.set(_relativeBounds);
  1007. if (isXPercentage())
  1008. _bounds.x *= parentAbsoluteBounds.width;
  1009. if (isYPercentage())
  1010. _bounds.y *= parentAbsoluteBounds.height;
  1011. if (isWidthPercentage())
  1012. _bounds.width *= parentAbsoluteBounds.width;
  1013. if (isHeightPercentage())
  1014. _bounds.height *= parentAbsoluteBounds.height;
  1015. // Apply control alignment
  1016. if (_alignment != Control::ALIGN_TOP_LEFT)
  1017. {
  1018. const Theme::Margin& margin = _style->getMargin();
  1019. const Rectangle& parentBounds = _parent ? _parent->getBounds() : Rectangle(0, 0, game->getViewport().width, game->getViewport().height);
  1020. const Theme::Border& parentBorder = _parent ? _parent->getBorder(_parent->getState()) : Theme::Border::empty();
  1021. const Theme::Padding& parentPadding = _parent ? _parent->getPadding() : Theme::Padding::empty();
  1022. float clipWidth, clipHeight;
  1023. if (_parent && (_parent->getScroll() != Container::SCROLL_NONE))
  1024. {
  1025. const Rectangle& verticalScrollBarBounds = _parent->getImageRegion("verticalScrollBar", _parent->getState());
  1026. const Rectangle& horizontalScrollBarBounds = _parent->getImageRegion("horizontalScrollBar", _parent->getState());
  1027. clipWidth = parentBounds.width - parentBorder.left - parentBorder.right - parentPadding.left - parentPadding.right - verticalScrollBarBounds.width;
  1028. clipHeight = parentBounds.height - parentBorder.top - parentBorder.bottom - parentPadding.top - parentPadding.bottom - horizontalScrollBarBounds.height;
  1029. }
  1030. else
  1031. {
  1032. clipWidth = parentBounds.width - parentBorder.left - parentBorder.right - parentPadding.left - parentPadding.right;
  1033. clipHeight = parentBounds.height - parentBorder.top - parentBorder.bottom - parentPadding.top - parentPadding.bottom;
  1034. }
  1035. // Vertical alignment
  1036. if ((_alignment & Control::ALIGN_BOTTOM) == Control::ALIGN_BOTTOM)
  1037. {
  1038. _bounds.y = clipHeight - _bounds.height - margin.bottom;
  1039. }
  1040. else if ((_alignment & Control::ALIGN_VCENTER) == Control::ALIGN_VCENTER)
  1041. {
  1042. _bounds.y = clipHeight * 0.5f - _bounds.height * 0.5f;
  1043. }
  1044. else if ((_alignment & Control::ALIGN_TOP) == Control::ALIGN_TOP)
  1045. {
  1046. _bounds.y = margin.top;
  1047. }
  1048. // Horizontal alignment
  1049. if ((_alignment & Control::ALIGN_RIGHT) == Control::ALIGN_RIGHT)
  1050. {
  1051. _bounds.x = clipWidth - _bounds.width - margin.right;
  1052. }
  1053. else if ((_alignment & Control::ALIGN_HCENTER) == Control::ALIGN_HCENTER)
  1054. {
  1055. _bounds.x = clipWidth * 0.5f - _bounds.width * 0.5f;
  1056. }
  1057. else if ((_alignment & Control::ALIGN_LEFT) == Control::ALIGN_LEFT)
  1058. {
  1059. _bounds.x = margin.left;
  1060. }
  1061. }
  1062. }
  1063. void Control::updateAbsoluteBounds(const Vector2& offset)
  1064. {
  1065. Game* game = Game::getInstance();
  1066. const Rectangle parentAbsoluteBounds = _parent ? _parent->_viewportBounds : Rectangle(0, 0, game->getViewport().width, game->getViewport().height);
  1067. const Rectangle parentAbsoluteClip = _parent ? _parent->_viewportClipBounds : parentAbsoluteBounds;
  1068. // Compute content area padding values
  1069. const Theme::Border& border = getBorder(NORMAL);
  1070. const Theme::Padding& padding = getPadding();
  1071. float lpadding = border.left + padding.left;
  1072. float rpadding = border.right + padding.right;
  1073. float tpadding = border.top + padding.top;
  1074. float bpadding = border.bottom + padding.bottom;
  1075. float hpadding = lpadding + rpadding;
  1076. float vpadding = tpadding + bpadding;
  1077. // Calculate absolute unclipped bounds
  1078. _absoluteBounds.set(
  1079. parentAbsoluteBounds.x + offset.x + _bounds.x,
  1080. parentAbsoluteBounds.y + offset.y + _bounds.y,
  1081. _bounds.width,
  1082. _bounds.height);
  1083. // Calculate absolute clipped bounds
  1084. Rectangle::intersect(_absoluteBounds, parentAbsoluteClip, &_absoluteClipBounds);
  1085. // Calculate the local clipped bounds
  1086. _clipBounds.set(
  1087. max(_absoluteClipBounds.x - _absoluteBounds.x, 0.0f),
  1088. max(_absoluteClipBounds.y - _absoluteBounds.y, 0.0f),
  1089. _absoluteClipBounds.width,
  1090. _absoluteClipBounds.height
  1091. );
  1092. // Calculate the absolute unclipped viewport bounds (content area, which does not include border and padding)
  1093. _viewportBounds.set(
  1094. _absoluteBounds.x + lpadding,
  1095. _absoluteBounds.y + tpadding,
  1096. _absoluteBounds.width - hpadding,
  1097. _absoluteBounds.height - vpadding);
  1098. // Calculate the absolute clipped viewport bounds
  1099. Rectangle::intersect(_viewportBounds, parentAbsoluteClip, &_viewportClipBounds);
  1100. }
  1101. void Control::startBatch(Form* form, SpriteBatch* batch)
  1102. {
  1103. form->startBatch(batch);
  1104. }
  1105. void Control::finishBatch(Form* form, SpriteBatch* batch)
  1106. {
  1107. form->finishBatch(batch);
  1108. }
  1109. unsigned int Control::draw(Form* form, const Rectangle& clip)
  1110. {
  1111. if (!_visible)
  1112. return 0;
  1113. unsigned int drawCalls = drawBorder(form, clip);
  1114. drawCalls += drawImages(form, clip);
  1115. drawCalls += drawText(form, clip);
  1116. return drawCalls;
  1117. }
  1118. unsigned int Control::drawBorder(Form* form, const Rectangle& clip)
  1119. {
  1120. if (!form || !_skin || _absoluteBounds.width <= 0 || _absoluteBounds.height <= 0)
  1121. return 0;
  1122. unsigned int drawCalls = 0;
  1123. SpriteBatch* batch = _style->getTheme()->getSpriteBatch();
  1124. startBatch(form, batch);
  1125. // Get the border and background images for this control's current state.
  1126. const Theme::UVs& topLeft = _skin->getUVs(Theme::Skin::TOP_LEFT);
  1127. const Theme::UVs& top = _skin->getUVs(Theme::Skin::TOP);
  1128. const Theme::UVs& topRight = _skin->getUVs(Theme::Skin::TOP_RIGHT);
  1129. const Theme::UVs& left = _skin->getUVs(Theme::Skin::LEFT);
  1130. const Theme::UVs& center = _skin->getUVs(Theme::Skin::CENTER);
  1131. const Theme::UVs& right = _skin->getUVs(Theme::Skin::RIGHT);
  1132. const Theme::UVs& bottomLeft = _skin->getUVs(Theme::Skin::BOTTOM_LEFT);
  1133. const Theme::UVs& bottom = _skin->getUVs(Theme::Skin::BOTTOM);
  1134. const Theme::UVs& bottomRight = _skin->getUVs(Theme::Skin::BOTTOM_RIGHT);
  1135. // Calculate screen-space positions.
  1136. const Theme::Border& border = getBorder(getState());
  1137. const Theme::Padding& padding = getPadding();
  1138. Vector4 skinColor = _skin->getColor();
  1139. skinColor.w *= _opacity;
  1140. float midWidth = _absoluteBounds.width - border.left - border.right;
  1141. float midHeight = _absoluteBounds.height - border.top - border.bottom;
  1142. float midX = _absoluteBounds.x + border.left;
  1143. float midY = _absoluteBounds.y + border.top;
  1144. float rightX = _absoluteBounds.x + _absoluteBounds.width - border.right;
  1145. float bottomY = _absoluteBounds.y + _absoluteBounds.height - border.bottom;
  1146. // Draw themed border sprites.
  1147. if (!border.left && !border.right && !border.top && !border.bottom)
  1148. {
  1149. // No border, just draw the image.
  1150. batch->draw(_absoluteBounds.x, _absoluteBounds.y, _absoluteBounds.width, _absoluteBounds.height, center.u1, center.v1, center.u2, center.v2, skinColor, clip);
  1151. ++drawCalls;
  1152. }
  1153. else
  1154. {
  1155. if (border.left && border.top)
  1156. {
  1157. batch->draw(_absoluteBounds.x, _absoluteBounds.y, border.left, border.top, topLeft.u1, topLeft.v1, topLeft.u2, topLeft.v2, skinColor, clip);
  1158. ++drawCalls;
  1159. }
  1160. if (border.top)
  1161. {
  1162. batch->draw(_absoluteBounds.x + border.left, _absoluteBounds.y, midWidth, border.top, top.u1, top.v1, top.u2, top.v2, skinColor, clip);
  1163. ++drawCalls;
  1164. }
  1165. if (border.right && border.top)
  1166. {
  1167. batch->draw(rightX, _absoluteBounds.y, border.right, border.top, topRight.u1, topRight.v1, topRight.u2, topRight.v2, skinColor, clip);
  1168. ++drawCalls;
  1169. }
  1170. if (border.left)
  1171. {
  1172. batch->draw(_absoluteBounds.x, midY, border.left, midHeight, left.u1, left.v1, left.u2, left.v2, skinColor, clip);
  1173. ++drawCalls;
  1174. }
  1175. // Always draw the background.
  1176. batch->draw(_absoluteBounds.x + border.left, _absoluteBounds.y + border.top, _absoluteBounds.width - border.left - border.right, _absoluteBounds.height - border.top - border.bottom,
  1177. center.u1, center.v1, center.u2, center.v2, skinColor, clip);
  1178. ++drawCalls;
  1179. if (border.right)
  1180. {
  1181. batch->draw(rightX, midY, border.right, midHeight, right.u1, right.v1, right.u2, right.v2, skinColor, clip);
  1182. ++drawCalls;
  1183. }
  1184. if (border.bottom && border.left)
  1185. {
  1186. batch->draw(_absoluteBounds.x, bottomY, border.left, border.bottom, bottomLeft.u1, bottomLeft.v1, bottomLeft.u2, bottomLeft.v2, skinColor, clip);
  1187. ++drawCalls;
  1188. }
  1189. if (border.bottom)
  1190. {
  1191. batch->draw(midX, bottomY, midWidth, border.bottom, bottom.u1, bottom.v1, bottom.u2, bottom.v2, skinColor, clip);
  1192. ++drawCalls;
  1193. }
  1194. if (border.bottom && border.right)
  1195. {
  1196. batch->draw(rightX, bottomY, border.right, border.bottom, bottomRight.u1, bottomRight.v1, bottomRight.u2, bottomRight.v2, skinColor, clip);
  1197. ++drawCalls;
  1198. }
  1199. }
  1200. finishBatch(form, batch);
  1201. return drawCalls;
  1202. }
  1203. unsigned int Control::drawImages(Form* form, const Rectangle& position)
  1204. {
  1205. return 0;
  1206. }
  1207. unsigned int Control::drawText(Form* form, const Rectangle& position)
  1208. {
  1209. return 0;
  1210. }
  1211. bool Control::isContainer() const
  1212. {
  1213. return false;
  1214. }
  1215. Control::State Control::getState(const char* state)
  1216. {
  1217. if (!state)
  1218. {
  1219. return NORMAL;
  1220. }
  1221. if (strcmp(state, "NORMAL") == 0)
  1222. {
  1223. return NORMAL;
  1224. }
  1225. else if (strcmp(state, "ACTIVE") == 0)
  1226. {
  1227. return ACTIVE;
  1228. }
  1229. else if (strcmp(state, "FOCUS") == 0)
  1230. {
  1231. return FOCUS;
  1232. }
  1233. else if (strcmp(state, "DISABLED") == 0)
  1234. {
  1235. return DISABLED;
  1236. }
  1237. else if (strcmp(state, "HOVER") == 0)
  1238. {
  1239. return HOVER;
  1240. }
  1241. return NORMAL;
  1242. }
  1243. Theme::ThemeImage* Control::getImage(const char* id, State state)
  1244. {
  1245. Theme::ThemeImage* image = NULL;
  1246. Theme::Style::Overlay* overlay = getOverlay(state);
  1247. if (overlay)
  1248. {
  1249. Theme::ImageList* imageList = overlay->getImageList();
  1250. if (imageList)
  1251. image = imageList->getImage(id);
  1252. }
  1253. return image ? image : _style->getTheme()->_emptyImage;
  1254. }
  1255. Control* Control::getParent() const
  1256. {
  1257. return _parent;
  1258. }
  1259. bool Control::isChild(Control* control) const
  1260. {
  1261. if (!control)
  1262. return false;
  1263. Control* parent = _parent;
  1264. while (parent)
  1265. {
  1266. if (parent == control)
  1267. return true;
  1268. parent = parent->_parent;
  1269. }
  1270. return false;
  1271. }
  1272. Form* Control::getTopLevelForm() const
  1273. {
  1274. if (_parent)
  1275. return _parent->getTopLevelForm();
  1276. if (isContainer())
  1277. {
  1278. Container* container = static_cast<Container*>(const_cast<Control*>(this));
  1279. if (container->isForm())
  1280. return static_cast<Form*>(container);
  1281. }
  1282. return NULL;
  1283. }
  1284. // Implementation of AnimationHandler
  1285. unsigned int Control::getAnimationPropertyComponentCount(int propertyId) const
  1286. {
  1287. switch(propertyId)
  1288. {
  1289. case ANIMATE_POSITION:
  1290. case ANIMATE_SIZE:
  1291. return 2;
  1292. case ANIMATE_POSITION_X:
  1293. case ANIMATE_POSITION_Y:
  1294. case ANIMATE_SIZE_WIDTH:
  1295. case ANIMATE_SIZE_HEIGHT:
  1296. case ANIMATE_OPACITY:
  1297. return 1;
  1298. default:
  1299. return -1;
  1300. }
  1301. }
  1302. void Control::getAnimationPropertyValue(int propertyId, AnimationValue* value)
  1303. {
  1304. GP_ASSERT(value);
  1305. switch (propertyId)
  1306. {
  1307. case ANIMATE_POSITION:
  1308. value->setFloat(0, _bounds.x);
  1309. value->setFloat(1, _bounds.y);
  1310. break;
  1311. case ANIMATE_SIZE:
  1312. value->setFloat(0, _bounds.width);
  1313. value->setFloat(1, _bounds.height);
  1314. break;
  1315. case ANIMATE_POSITION_X:
  1316. value->setFloat(0, _bounds.x);
  1317. break;
  1318. case ANIMATE_POSITION_Y:
  1319. value->setFloat(0, _bounds.y);
  1320. break;
  1321. case ANIMATE_SIZE_WIDTH:
  1322. value->setFloat(0, _bounds.width);
  1323. break;
  1324. case ANIMATE_SIZE_HEIGHT:
  1325. value->setFloat(0, _bounds.height);
  1326. break;
  1327. case ANIMATE_OPACITY:
  1328. value->setFloat(0, _opacity);
  1329. break;
  1330. default:
  1331. break;
  1332. }
  1333. }
  1334. void Control::setAnimationPropertyValue(int propertyId, AnimationValue* value, float blendWeight)
  1335. {
  1336. GP_ASSERT(value);
  1337. switch(propertyId)
  1338. {
  1339. case ANIMATE_POSITION:
  1340. setX(Curve::lerp(blendWeight, _bounds.x, value->getFloat(0)), isXPercentage());
  1341. setY(Curve::lerp(blendWeight, _bounds.y, value->getFloat(1)), isYPercentage());
  1342. break;
  1343. case ANIMATE_POSITION_X:
  1344. setX(Curve::lerp(blendWeight, _bounds.x, value->getFloat(0)), isXPercentage());
  1345. break;
  1346. case ANIMATE_POSITION_Y:
  1347. setY(Curve::lerp(blendWeight, _bounds.y, value->getFloat(0)), isYPercentage());
  1348. break;
  1349. case ANIMATE_SIZE:
  1350. setWidth(Curve::lerp(blendWeight, _bounds.width, value->getFloat(0)), isWidthPercentage());
  1351. setHeight(Curve::lerp(blendWeight, _bounds.height, value->getFloat(1)), isHeightPercentage());
  1352. break;
  1353. case ANIMATE_SIZE_WIDTH:
  1354. setWidth(Curve::lerp(blendWeight, _bounds.width, value->getFloat(0)), isWidthPercentage());
  1355. break;
  1356. case ANIMATE_SIZE_HEIGHT:
  1357. setHeight(Curve::lerp(blendWeight, _bounds.height, value->getFloat(0)), isHeightPercentage());
  1358. break;
  1359. case ANIMATE_OPACITY:
  1360. setOpacity(Curve::lerp(blendWeight, _opacity, value->getFloat(0)));
  1361. break;
  1362. }
  1363. }
  1364. Theme::Style::Overlay** Control::getOverlays(unsigned char overlayTypes, Theme::Style::Overlay** overlays)
  1365. {
  1366. GP_ASSERT(overlays);
  1367. GP_ASSERT(_style);
  1368. unsigned int index = 0;
  1369. if ((overlayTypes & NORMAL) == NORMAL)
  1370. {
  1371. overlays[index++] = _style->getOverlay(Theme::Style::OVERLAY_NORMAL);
  1372. }
  1373. if ((overlayTypes & FOCUS) == FOCUS)
  1374. {
  1375. overlays[index++] = _style->getOverlay(Theme::Style::OVERLAY_FOCUS);
  1376. }
  1377. if ((overlayTypes & ACTIVE) == ACTIVE)
  1378. {
  1379. overlays[index++] = _style->getOverlay(Theme::Style::OVERLAY_ACTIVE);
  1380. }
  1381. if ((overlayTypes & DISABLED) == DISABLED)
  1382. {
  1383. overlays[index++] = _style->getOverlay(Theme::Style::OVERLAY_DISABLED);
  1384. }
  1385. if ((overlayTypes & HOVER) == HOVER)
  1386. {
  1387. overlays[index++] = _style->getOverlay(Theme::Style::OVERLAY_HOVER);
  1388. }
  1389. return overlays;
  1390. }
  1391. Theme::Style::Overlay* Control::getOverlay(State state) const
  1392. {
  1393. GP_ASSERT(_style);
  1394. Theme::Style::Overlay* overlay = NULL;
  1395. switch (state)
  1396. {
  1397. case Control::NORMAL:
  1398. return _style->getOverlay(Theme::Style::OVERLAY_NORMAL);
  1399. case Control::FOCUS:
  1400. overlay = _style->getOverlay(Theme::Style::OVERLAY_FOCUS);
  1401. break;
  1402. case Control::ACTIVE:
  1403. overlay = _style->getOverlay(Theme::Style::OVERLAY_ACTIVE);
  1404. if (!overlay && hasFocus())
  1405. overlay = _style->getOverlay(Theme::Style::OVERLAY_FOCUS);
  1406. break;
  1407. case Control::DISABLED:
  1408. overlay = _style->getOverlay(Theme::Style::OVERLAY_DISABLED);
  1409. break;
  1410. case Control::HOVER:
  1411. overlay = _style->getOverlay(Theme::Style::OVERLAY_HOVER);
  1412. if (!overlay && hasFocus())
  1413. overlay = _style->getOverlay(Theme::Style::OVERLAY_FOCUS);
  1414. break;
  1415. }
  1416. // Fall back to normal overlay if more specific state overlay not found
  1417. if (!overlay)
  1418. overlay = _style->getOverlay(Theme::Style::OVERLAY_NORMAL);
  1419. return overlay;
  1420. }
  1421. void Control::overrideStyle()
  1422. {
  1423. if (_styleOverridden)
  1424. {
  1425. return;
  1426. }
  1427. // Copy the style.
  1428. GP_ASSERT(_style);
  1429. _style = new Theme::Style(*_style);
  1430. _styleOverridden = true;
  1431. }
  1432. void Control::overrideThemedProperties(Properties* properties, unsigned char states)
  1433. {
  1434. GP_ASSERT(properties);
  1435. GP_ASSERT(_style);
  1436. GP_ASSERT(_style->_theme);
  1437. Theme::ImageList* imageList = NULL;
  1438. Theme::ThemeImage* cursor = NULL;
  1439. Theme::Skin* skin = NULL;
  1440. _style->_theme->lookUpSprites(properties, &imageList, &cursor, &skin);
  1441. if (imageList)
  1442. {
  1443. setImageList(imageList, states);
  1444. }
  1445. if (cursor)
  1446. {
  1447. setCursor(cursor, states);
  1448. }
  1449. if (skin)
  1450. {
  1451. setSkin(skin, states);
  1452. }
  1453. if (properties->exists("font"))
  1454. {
  1455. Font* font = Font::create(properties->getString("font"));
  1456. setFont(font, states);
  1457. font->release();
  1458. }
  1459. if (properties->exists("fontSize"))
  1460. {
  1461. setFontSize(properties->getInt("fontSize"), states);
  1462. }
  1463. if (properties->exists("textColor"))
  1464. {
  1465. Vector4 textColor(0, 0, 0, 1);
  1466. properties->getColor("textColor", &textColor);
  1467. setTextColor(textColor, states);
  1468. }
  1469. if (properties->exists("textAlignment"))
  1470. {
  1471. setTextAlignment(Font::getJustify(properties->getString("textAlignment")), states);
  1472. }
  1473. if (properties->exists("rightToLeft"))
  1474. {
  1475. setTextRightToLeft(properties->getBool("rightToLeft"), states);
  1476. }
  1477. if (properties->exists("opacity"))
  1478. {
  1479. setOpacity(properties->getFloat("opacity"), states);
  1480. }
  1481. }
  1482. void Control::setImageList(Theme::ImageList* imageList, unsigned char states)
  1483. {
  1484. overrideStyle();
  1485. Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
  1486. getOverlays(states, overlays);
  1487. for (int i = 0; i < Theme::Style::OVERLAY_MAX; ++i)
  1488. {
  1489. if( overlays[i] )
  1490. overlays[i]->setImageList(imageList);
  1491. }
  1492. if (_autoSize != AUTO_SIZE_NONE)
  1493. setDirty(DIRTY_BOUNDS);
  1494. }
  1495. void Control::setCursor(Theme::ThemeImage* cursor, unsigned char states)
  1496. {
  1497. overrideStyle();
  1498. Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
  1499. getOverlays(states, overlays);
  1500. for (int i = 0; i < Theme::Style::OVERLAY_MAX; ++i)
  1501. {
  1502. if( overlays[i] )
  1503. overlays[i]->setCursor(cursor);
  1504. }
  1505. }
  1506. void Control::setSkin(Theme::Skin* skin, unsigned char states)
  1507. {
  1508. overrideStyle();
  1509. Theme::Style::Overlay* overlays[Theme::Style::OVERLAY_MAX] = { 0 };
  1510. getOverlays(states, overlays);
  1511. for (int i = 0; i < Theme::Style::OVERLAY_MAX; ++i)
  1512. {
  1513. if( overlays[i] )
  1514. overlays[i]->setSkin(skin);
  1515. }
  1516. if (_autoSize != AUTO_SIZE_NONE)
  1517. setDirty(DIRTY_BOUNDS);
  1518. }
  1519. Theme::Skin* Control::getSkin(State state)
  1520. {
  1521. Theme::Style::Overlay* overlay = getOverlay(state);
  1522. GP_ASSERT(overlay);
  1523. return overlay->getSkin();
  1524. }
  1525. Control::Alignment Control::getAlignment(const char* alignment)
  1526. {
  1527. if (!alignment)
  1528. {
  1529. return Control::ALIGN_TOP_LEFT;
  1530. }
  1531. if (strcmp(alignment, "ALIGN_LEFT") == 0)
  1532. {
  1533. return Control::ALIGN_LEFT;
  1534. }
  1535. else if (strcmp(alignment, "ALIGN_HCENTER") == 0)
  1536. {
  1537. return Control::ALIGN_HCENTER;
  1538. }
  1539. else if (strcmp(alignment, "ALIGN_RIGHT") == 0)
  1540. {
  1541. return Control::ALIGN_RIGHT;
  1542. }
  1543. else if (strcmp(alignment, "ALIGN_TOP") == 0)
  1544. {
  1545. return Control::ALIGN_TOP;
  1546. }
  1547. else if (strcmp(alignment, "ALIGN_VCENTER") == 0)
  1548. {
  1549. return Control::ALIGN_VCENTER;
  1550. }
  1551. else if (strcmp(alignment, "ALIGN_BOTTOM") == 0)
  1552. {
  1553. return Control::ALIGN_BOTTOM;
  1554. }
  1555. else if (strcmp(alignment, "ALIGN_TOP_LEFT") == 0)
  1556. {
  1557. return Control::ALIGN_TOP_LEFT;
  1558. }
  1559. else if (strcmp(alignment, "ALIGN_VCENTER_LEFT") == 0)
  1560. {
  1561. return Control::ALIGN_VCENTER_LEFT;
  1562. }
  1563. else if (strcmp(alignment, "ALIGN_BOTTOM_LEFT") == 0)
  1564. {
  1565. return Control::ALIGN_BOTTOM_LEFT;
  1566. }
  1567. else if (strcmp(alignment, "ALIGN_TOP_HCENTER") == 0)
  1568. {
  1569. return Control::ALIGN_TOP_HCENTER;
  1570. }
  1571. else if (strcmp(alignment, "ALIGN_VCENTER_HCENTER") == 0)
  1572. {
  1573. return Control::ALIGN_VCENTER_HCENTER;
  1574. }
  1575. else if (strcmp(alignment, "ALIGN_BOTTOM_HCENTER") == 0)
  1576. {
  1577. return Control::ALIGN_BOTTOM_HCENTER;
  1578. }
  1579. else if (strcmp(alignment, "ALIGN_TOP_RIGHT") == 0)
  1580. {
  1581. return Control::ALIGN_TOP_RIGHT;
  1582. }
  1583. else if (strcmp(alignment, "ALIGN_VCENTER_RIGHT") == 0)
  1584. {
  1585. return Control::ALIGN_VCENTER_RIGHT;
  1586. }
  1587. else if (strcmp(alignment, "ALIGN_BOTTOM_RIGHT") == 0)
  1588. {
  1589. return Control::ALIGN_BOTTOM_RIGHT;
  1590. }
  1591. else
  1592. {
  1593. GP_ERROR("Failed to get corresponding control alignment for unsupported value '%s'.", alignment);
  1594. }
  1595. return Control::ALIGN_TOP_LEFT;
  1596. }
  1597. float Control::parseCoord(const char* s, bool* isPercentage)
  1598. {
  1599. const char* p;
  1600. if ((p = strchr(s, '%')) != NULL)
  1601. {
  1602. std::string value(s, (std::string::size_type)(p - s));
  1603. *isPercentage = true;
  1604. return (float)(atof(value.c_str()) * 0.01);
  1605. }
  1606. *isPercentage = false;
  1607. return (float)atof(s);
  1608. }
  1609. bool Control::parseCoordPair(const char* s, float* v1, float* v2, bool* v1Percentage, bool* v2Percentage)
  1610. {
  1611. size_t len = strlen(s);
  1612. const char* s2 = strchr(s, ',');
  1613. if (s2 == NULL)
  1614. return false;
  1615. std::string v1Str(s, (std::string::size_type)(s2 - s));
  1616. std::string v2Str(s2 + 1);
  1617. *v1 = parseCoord(v1Str.c_str(), v1Percentage);
  1618. *v2 = parseCoord(v2Str.c_str(), v2Percentage);
  1619. return true;
  1620. }
  1621. }