ThemeStyle.cpp 10 KB

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