ThemeStyle.cpp 9.7 KB

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