ThemeStyle.cpp 9.9 KB

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