ThemeStyle.cpp 9.8 KB

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