ThemeStyle.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. #include "ThemeStyle.h"
  2. namespace gameplay
  3. {
  4. /****************
  5. * Theme::Style *
  6. ****************/
  7. Theme::Style::Style(Theme* theme, const char* id, float tw, float th,
  8. const Theme::Margin& margin, const Theme::Padding& padding,
  9. Theme::Style::Overlay* normal, Theme::Style::Overlay* focus,
  10. Theme::Style::Overlay* active, Theme::Style::Overlay* disabled,
  11. Theme::Style::Overlay* hover)
  12. : _theme(theme), _id(id), _tw(tw), _th(th), _margin(margin), _padding(padding)
  13. {
  14. _overlays[OVERLAY_NORMAL] = normal;
  15. _overlays[OVERLAY_FOCUS] = focus;
  16. _overlays[OVERLAY_ACTIVE] = active;
  17. _overlays[OVERLAY_DISABLED] = disabled;
  18. _overlays[OVERLAY_HOVER] = hover;
  19. }
  20. Theme::Style::Style(const Style& copy)
  21. {
  22. _id = copy._id;
  23. _margin = copy._margin;
  24. _padding = copy._padding;
  25. _tw = copy._tw;
  26. _th = copy._th;
  27. for (int i = 0; i < OVERLAY_MAX; i++)
  28. {
  29. if (copy._overlays[i])
  30. _overlays[i] = new Theme::Style::Overlay(*copy._overlays[i]);
  31. else
  32. _overlays[i] = NULL;
  33. }
  34. }
  35. Theme::Style::~Style()
  36. {
  37. for (unsigned int i = 0; i < OVERLAY_MAX; i++)
  38. {
  39. SAFE_RELEASE(_overlays[i]);
  40. }
  41. }
  42. Theme* Theme::Style::getTheme() const
  43. {
  44. return _theme;
  45. }
  46. const char* Theme::Style::getId() const
  47. {
  48. return _id.c_str();
  49. }
  50. Theme::Style::Overlay* Theme::Style::getOverlay(OverlayType overlayType) const
  51. {
  52. return _overlays[overlayType];
  53. }
  54. void Theme::Style::setMargin(float top, float bottom, float left, float right)
  55. {
  56. _margin.top = top;
  57. _margin.bottom = bottom;
  58. _margin.left = left;
  59. _margin.right = right;
  60. }
  61. const Theme::Margin& Theme::Style::getMargin() const
  62. {
  63. return _margin;
  64. }
  65. void Theme::Style::setPadding(float top, float bottom, float left, float right)
  66. {
  67. _padding.top = top;
  68. _padding.bottom = bottom;
  69. _padding.left = left;
  70. _padding.right = right;
  71. }
  72. const Theme::Padding& Theme::Style::getPadding() const
  73. {
  74. return _padding;
  75. }
  76. /*************************
  77. * Theme::Style::Overlay *
  78. *************************/
  79. Theme::Style::Overlay* Theme::Style::Overlay::create()
  80. {
  81. Overlay* overlay = new Overlay();
  82. return overlay;
  83. }
  84. Theme::Style::Overlay::Overlay()
  85. : _skin(NULL), _cursor(NULL), _imageList(NULL), _font(NULL),
  86. _fontSize(0), _alignment(Font::ALIGN_TOP_LEFT), _textRightToLeft(false), _textColor(Vector4::one()), _opacity(1.0f)
  87. {
  88. }
  89. Theme::Style::Overlay::Overlay(const Overlay& copy) : _skin(NULL), _cursor(NULL), _imageList(NULL), _font(NULL)
  90. {
  91. if (copy._skin)
  92. {
  93. _skin = new Skin(*copy._skin);
  94. }
  95. if (copy._cursor)
  96. {
  97. _cursor = new ThemeImage(*copy._cursor);
  98. }
  99. if (copy._imageList)
  100. {
  101. _imageList = new ImageList(*copy._imageList);
  102. }
  103. _font = copy._font;
  104. _fontSize = copy._fontSize;
  105. _alignment = copy._alignment;
  106. _textRightToLeft = copy._textRightToLeft;
  107. _textColor = Vector4(copy._textColor);
  108. _opacity = copy._opacity;
  109. if (_font)
  110. {
  111. _font->addRef();
  112. }
  113. }
  114. Theme::Style::Overlay::~Overlay()
  115. {
  116. SAFE_RELEASE(_skin);
  117. SAFE_RELEASE(_imageList);
  118. SAFE_RELEASE(_cursor);
  119. SAFE_RELEASE(_font);
  120. }
  121. float Theme::Style::Overlay::getOpacity() const
  122. {
  123. return _opacity;
  124. }
  125. void Theme::Style::Overlay::setOpacity(float opacity)
  126. {
  127. _opacity = opacity;
  128. }
  129. void Theme::Style::Overlay::setBorder(float top, float bottom, float left, float right)
  130. {
  131. if (_skin)
  132. {
  133. _skin->_border.top = top;
  134. _skin->_border.bottom = bottom;
  135. _skin->_border.left = left;
  136. _skin->_border.right = right;
  137. }
  138. }
  139. const Theme::Border& Theme::Style::Overlay::getBorder() const
  140. {
  141. if (_skin)
  142. {
  143. return _skin->getBorder();
  144. }
  145. else
  146. {
  147. return Theme::Border::empty();
  148. }
  149. }
  150. void Theme::Style::Overlay::setSkinColor(const Vector4& color)
  151. {
  152. if (_skin)
  153. {
  154. _skin->_color.set(color);
  155. }
  156. }
  157. const Vector4& Theme::Style::Overlay::getSkinColor() const
  158. {
  159. if (_skin)
  160. {
  161. return _skin->getColor();
  162. }
  163. return Vector4::one();
  164. }
  165. void Theme::Style::Overlay::setSkinRegion(const Rectangle& region, float tw, float th)
  166. {
  167. GP_ASSERT(_skin);
  168. _skin->setRegion(region, tw, th);
  169. }
  170. const Rectangle& Theme::Style::Overlay::getSkinRegion() const
  171. {
  172. if (_skin)
  173. {
  174. return _skin->getRegion();
  175. }
  176. return Rectangle::empty();
  177. }
  178. const Theme::UVs& Theme::Style::Overlay::getSkinUVs(Theme::Skin::SkinArea area) const
  179. {
  180. if (_skin)
  181. {
  182. return _skin->_uvs[area];
  183. }
  184. return UVs::empty();
  185. }
  186. Font* Theme::Style::Overlay::getFont() const
  187. {
  188. return _font;
  189. }
  190. void Theme::Style::Overlay::setFont(Font* font)
  191. {
  192. if (_font != font)
  193. {
  194. SAFE_RELEASE(_font);
  195. _font = font;
  196. if (_font)
  197. {
  198. _font->addRef();
  199. }
  200. }
  201. }
  202. unsigned int Theme::Style::Overlay::getFontSize() const
  203. {
  204. return _fontSize;
  205. }
  206. void Theme::Style::Overlay::setFontSize(unsigned int fontSize)
  207. {
  208. _fontSize = fontSize;
  209. }
  210. Font::Justify Theme::Style::Overlay::getTextAlignment() const
  211. {
  212. return _alignment;
  213. }
  214. void Theme::Style::Overlay::setTextAlignment(Font::Justify alignment)
  215. {
  216. _alignment = alignment;
  217. }
  218. bool Theme::Style::Overlay::getTextRightToLeft() const
  219. {
  220. return _textRightToLeft;
  221. }
  222. void Theme::Style::Overlay::setTextRightToLeft(bool rightToLeft)
  223. {
  224. _textRightToLeft = rightToLeft;
  225. }
  226. const Vector4& Theme::Style::Overlay::getTextColor() const
  227. {
  228. return _textColor;
  229. }
  230. void Theme::Style::Overlay::setTextColor(const Vector4& color)
  231. {
  232. _textColor = color;
  233. }
  234. const Rectangle& Theme::Style::Overlay::getImageRegion(const char* id) const
  235. {
  236. if (!_imageList)
  237. {
  238. return Rectangle::empty();
  239. }
  240. ThemeImage* image = _imageList->getImage(id);
  241. if (image)
  242. {
  243. return image->getRegion();
  244. }
  245. else
  246. {
  247. return Rectangle::empty();
  248. }
  249. }
  250. void Theme::Style::Overlay::setImageRegion(const char* id, const Rectangle& region, float tw, float th)
  251. {
  252. GP_ASSERT(_imageList);
  253. ThemeImage* image = _imageList->getImage(id);
  254. GP_ASSERT(image);
  255. image->_region.set(region);
  256. generateUVs(tw, th, region.x, region.y, region.width, region.height, &(image->_uvs));
  257. }
  258. const Vector4& Theme::Style::Overlay::getImageColor(const char* id) const
  259. {
  260. GP_ASSERT(_imageList);
  261. ThemeImage* image = _imageList->getImage(id);
  262. if (image)
  263. {
  264. return image->getColor();
  265. }
  266. else
  267. {
  268. return Vector4::zero();
  269. }
  270. }
  271. void Theme::Style::Overlay::setImageColor(const char* id, const Vector4& color)
  272. {
  273. GP_ASSERT(_imageList);
  274. ThemeImage* image = _imageList->getImage(id);
  275. GP_ASSERT(image);
  276. image->_color.set(color);
  277. }
  278. const Theme::UVs& Theme::Style::Overlay::getImageUVs(const char* id) const
  279. {
  280. GP_ASSERT(_imageList);
  281. ThemeImage* image = _imageList->getImage(id);
  282. if (image)
  283. {
  284. return image->getUVs();
  285. }
  286. else
  287. {
  288. return UVs::empty();
  289. }
  290. }
  291. const Rectangle& Theme::Style::Overlay::getCursorRegion() const
  292. {
  293. if (_cursor)
  294. {
  295. return _cursor->getRegion();
  296. }
  297. else
  298. {
  299. return Rectangle::empty();
  300. }
  301. }
  302. void Theme::Style::Overlay::setCursorRegion(const Rectangle& region, float tw, float th)
  303. {
  304. GP_ASSERT(_cursor);
  305. _cursor->_region.set(region);
  306. generateUVs(tw, th, region.x, region.y, region.width, region.height, &(_cursor->_uvs));
  307. }
  308. const Vector4& Theme::Style::Overlay::getCursorColor() const
  309. {
  310. if (_cursor)
  311. {
  312. return _cursor->getColor();
  313. }
  314. else
  315. {
  316. return Vector4::zero();
  317. }
  318. }
  319. void Theme::Style::Overlay::setCursorColor(const Vector4& color)
  320. {
  321. GP_ASSERT(_cursor);
  322. _cursor->_color.set(color);
  323. }
  324. const Theme::UVs& Theme::Style::Overlay::getCursorUVs() const
  325. {
  326. if (_cursor)
  327. {
  328. return _cursor->getUVs();
  329. }
  330. else
  331. {
  332. return UVs::empty();
  333. }
  334. }
  335. void Theme::Style::Overlay::setSkin(Skin* skin)
  336. {
  337. if (_skin != skin)
  338. {
  339. SAFE_RELEASE(_skin);
  340. _skin = skin;
  341. if (skin)
  342. {
  343. skin->addRef();
  344. }
  345. }
  346. }
  347. Theme::Skin* Theme::Style::Overlay::getSkin() const
  348. {
  349. return _skin;
  350. }
  351. void Theme::Style::Overlay::setCursor(ThemeImage* cursor)
  352. {
  353. if (_cursor != cursor)
  354. {
  355. SAFE_RELEASE(_cursor);
  356. _cursor = cursor;
  357. if (cursor)
  358. {
  359. cursor->addRef();
  360. }
  361. }
  362. }
  363. Theme::ThemeImage* Theme::Style::Overlay::getCursor() const
  364. {
  365. return _cursor;
  366. }
  367. void Theme::Style::Overlay::setImageList(ImageList* imageList)
  368. {
  369. if (_imageList != imageList)
  370. {
  371. SAFE_RELEASE(_imageList);
  372. _imageList = imageList;
  373. if (imageList)
  374. {
  375. imageList->addRef();
  376. }
  377. }
  378. }
  379. Theme::ImageList* Theme::Style::Overlay::getImageList() const
  380. {
  381. return _imageList;
  382. }
  383. // Implementation of AnimationHandler
  384. unsigned int Theme::Style::Overlay::getAnimationPropertyComponentCount(int propertyId) const
  385. {
  386. switch(propertyId)
  387. {
  388. case Theme::Style::Overlay::ANIMATE_OPACITY:
  389. return 1;
  390. default:
  391. return -1;
  392. }
  393. }
  394. void Theme::Style::Overlay::getAnimationPropertyValue(int propertyId, AnimationValue* value)
  395. {
  396. GP_ASSERT(value);
  397. switch(propertyId)
  398. {
  399. case ANIMATE_OPACITY:
  400. value->setFloat(0, _opacity);
  401. break;
  402. default:
  403. break;
  404. }
  405. }
  406. void Theme::Style::Overlay::setAnimationPropertyValue(int propertyId, AnimationValue* value, float blendWeight)
  407. {
  408. GP_ASSERT(value);
  409. switch(propertyId)
  410. {
  411. case ANIMATE_OPACITY:
  412. {
  413. _opacity = Curve::lerp(blendWeight, _opacity, value->getFloat(0));
  414. break;
  415. }
  416. default:
  417. break;
  418. }
  419. }
  420. }