ThemeStyle.cpp 9.7 KB

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