Theme.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. #ifndef THEME_H_
  2. #define THEME_H_
  3. #include "Base.h"
  4. #include "Ref.h"
  5. #include "Font.h"
  6. #include "Rectangle.h"
  7. #include "Texture.h"
  8. #include "Properties.h"
  9. namespace gameplay
  10. {
  11. /**
  12. * A theme is created and stored as part of a form and represents its appearance.
  13. * Once loaded, the appearance properties can be retrieved from their style IDs and set on other
  14. * UI controls. A Theme has one property, 'texture', which points to a texture atlas containing
  15. * all the images used by the theme. Cursor images, skins, and lists of images used by controls
  16. * are defined in their own namespaces. The rest of the Theme consists of Style namespaces.
  17. * A Style describes the border, margin, and padding of a Control, what images, skins, and cursors
  18. * are associated with a Control, and Font properties to apply to a Control's text.
  19. *
  20. * Below is an explanation of the properties that can be set within themes:
  21. @verbatim
  22. theme
  23. {
  24. texture = <Path to texture>
  25. // Describes a single image, to be used as a cursor.
  26. cursor <Cursor ID>
  27. {
  28. region = <x, y, width, height>
  29. color = <#ffffffff>
  30. }
  31. // Describes all the images used by a control for one or more states.
  32. imageList <ImageList ID>
  33. {
  34. image checked
  35. {
  36. region = <x, y, width, height>
  37. }
  38. image unchecked
  39. {
  40. region = <x, y, width, height>
  41. color = <#fffffffff> // Optionally override image-list color.
  42. }
  43. color = <#fffffffff> // Default blend color for images that don't specify their own.
  44. }
  45. // Defines the border and background of a Control.
  46. skin <Skin ID>
  47. {
  48. // The corners and edges of the given region will be used as border sprites.
  49. border
  50. {
  51. top = <int> // Height of top border, top corners.
  52. bottom = <int> // Height of bottom border, bottom corners.
  53. left = <int> // Width of left border, left corners.
  54. right = <int> // Width of right border, right corners.
  55. }
  56. region = <x, y, width, height> // Total container region including entire border.
  57. color = <#ffffffff> // Tint to apply to skin.
  58. }
  59. style <Style ID>
  60. {
  61. // Layouts may make use of a style's margin to put space between adjacent controls.
  62. margin
  63. {
  64. top = <int>
  65. bottom = <int>
  66. left = <int>
  67. right = <int>
  68. }
  69. // Padding is the space between a control's border and its content.
  70. padding
  71. {
  72. top = <int>
  73. bottom = <int>
  74. left = <int>
  75. right = <int>
  76. }
  77. // Properties used when in control is in the normal state.
  78. stateNormal
  79. {
  80. skin = <Skin ID> // Skin to use for border and background sprites.
  81. imageList = <ImageList ID>
  82. cursor = <Cursor ID> // Cursor to use when the mouse is over this control.
  83. font = <Path to font> // Font to use for rendering text.
  84. fontSize = <int> // Size of text.
  85. textColor = <#ffffffff> // Color of text.
  86. alignment = <Text alignment constant> // Member of Font::Justify enum.
  87. rightToLeft = <bool> // Whether to draw text from right to left.
  88. opacity = <float> // Opacity to apply to all text/border/icon colors.
  89. }
  90. // Properties used when in control is in the focus state
  91. // If not specified, the 'normal' overlay will be used.
  92. stateFocus
  93. {
  94. skin = <Skin ID> // Skin to use for border and background sprites.
  95. ...
  96. }
  97. // Properties used when in control is in the focus.
  98. // This is when a touch/mouse is down within the control.
  99. // If not specified, the 'normal' overlay will be used.
  100. stateActive
  101. {
  102. skin = <Skin ID> // Skin to use for border and background sprites.
  103. ...
  104. }
  105. // Properties used when in control is in the focus.
  106. // This is when a touch/mouse is down within the control.
  107. // If not specified, the 'normal' overlay will be used.
  108. state-disabled
  109. {
  110. skin = <Skin ID> // Skin to use for border and background sprites.
  111. ...
  112. }
  113. }
  114. }
  115. @endverbatim
  116. *
  117. */
  118. class Theme: public Ref
  119. {
  120. friend class Control;
  121. friend class Container;
  122. friend class Form;
  123. friend class Skin;
  124. public:
  125. /**
  126. * Class representing a set of themed attributes that can be
  127. * assigned to a control.
  128. *
  129. * Defined in "ThemeStyle.h"
  130. */
  131. class Style;
  132. friend class Style;
  133. /**
  134. * Struct representing the UV coordinates of a rectangular image.
  135. */
  136. struct UVs
  137. {
  138. /**
  139. * Constructor.
  140. */
  141. UVs();
  142. /**
  143. * Constructor
  144. *
  145. * @param u1 u component of the first UV coordinate.
  146. * @param v1 v component of the first UV coordinate.
  147. * @param u2 u component of the second UV coordinate.
  148. * @param v2 v component of the second UV coordinate.
  149. */
  150. UVs(float u1, float v1, float u2, float v2);
  151. /**
  152. * Get's an empty UVs.
  153. */
  154. static const UVs& empty();
  155. /**
  156. * u component of the first UV coordinate.
  157. */
  158. float u1;
  159. /**
  160. * v component of the first UV coordinate.
  161. */
  162. float v1;
  163. /**
  164. * u component of the second UV coordinate.
  165. */
  166. float u2;
  167. /**
  168. * v component of the second UV coordinate.
  169. */
  170. float v2;
  171. };
  172. /**
  173. * Struct representing margin, border, and padding areas by
  174. * the width or height of each side.
  175. */
  176. struct SideRegions
  177. {
  178. /**
  179. * Constructor.
  180. */
  181. SideRegions() : top(0), bottom(0), left(0), right(0) {}
  182. /**
  183. * Gets an empty SideRegion.
  184. */
  185. static const SideRegions& empty();
  186. /**
  187. * The top of the SideRegion.
  188. */
  189. float top;
  190. /**
  191. * The bottom of the SideRegion.
  192. */
  193. float bottom;
  194. /**
  195. * The left side of the SideRegion.
  196. */
  197. float left;
  198. /**
  199. * The right side of the SideRegion.
  200. */
  201. float right;
  202. };
  203. /**
  204. * Struct representing margin areas by the width or height of each side.
  205. */
  206. typedef SideRegions Margin;
  207. /**
  208. * Struct representing border areas by the width or height of each side.
  209. */
  210. typedef SideRegions Border;
  211. /**
  212. * Struct representing padding areas by the width or height of each side.
  213. */
  214. typedef SideRegions Padding;
  215. /**
  216. * Class representing an image within the theme's texture atlas.
  217. * An image has a region and a blend color in addition to an ID.
  218. * UV coordinates are calculated from the region and can be retrieved.
  219. */
  220. class ThemeImage : public Ref
  221. {
  222. friend class Theme;
  223. friend class Control;
  224. public:
  225. /**
  226. * Gets the ID of the ThemeImage.
  227. */
  228. const char* getId() const;
  229. /**
  230. * Gets the UV coordinates for the ThemeImage.
  231. */
  232. const UVs& getUVs() const;
  233. /**
  234. * Gets the Rectangle region of the ThemeImage.
  235. */
  236. const Rectangle& getRegion() const;
  237. /**
  238. * Gets the color of the ThemeImage in a Vector4.
  239. */
  240. const Vector4& getColor() const;
  241. private:
  242. ThemeImage(float tw, float th, const Rectangle& region, const Vector4& color);
  243. ~ThemeImage();
  244. static ThemeImage* create(float tw, float th, Properties* properties, const Vector4& defaultColor);
  245. std::string _id;
  246. UVs _uvs;
  247. Rectangle _region;
  248. Vector4 _color;
  249. };
  250. private:
  251. /**
  252. * Class representing a collection of theme images. An image list
  253. * can be assigned to each overlay of a style, and controls
  254. * using the style can then retrieve images by ID in order to draw themselves.
  255. */
  256. class ImageList : public Ref
  257. {
  258. friend class Theme;
  259. friend class Control;
  260. public:
  261. const char* getId() const;
  262. ThemeImage* getImage(const char* imageId) const;
  263. private:
  264. ImageList(const Vector4& color);
  265. ImageList(const ImageList& copy);
  266. ~ImageList();
  267. static ImageList* create(float tw, float th, Properties* properties);
  268. std::string _id;
  269. std::vector<ThemeImage*> _images;
  270. Vector4 _color;
  271. };
  272. /**
  273. * A skin defines the border and background of a control.
  274. */
  275. class Skin : public Ref
  276. {
  277. friend class Theme;
  278. public:
  279. enum SkinArea
  280. {
  281. TOP_LEFT, TOP, TOP_RIGHT,
  282. LEFT, CENTER, RIGHT,
  283. BOTTOM_LEFT, BOTTOM, BOTTOM_RIGHT
  284. };
  285. /**
  286. * Gets this skin's ID.
  287. *
  288. * @return This skin's ID.
  289. */
  290. const char* getId() const;
  291. /**
  292. * Gets this skin's border.
  293. *
  294. * @return This skin's border.
  295. */
  296. const Theme::Border& getBorder() const;
  297. const Rectangle& getRegion() const;
  298. /**
  299. * Gets this skin's UVs.
  300. *
  301. * @return This skin's UVs.
  302. */
  303. const Theme::UVs& getUVs(SkinArea area) const;
  304. /**
  305. * Gets this skin's color.
  306. *
  307. * @return This skin's color.
  308. */
  309. const Vector4& getColor() const;
  310. private:
  311. Skin(float tw, float th, const Rectangle& region, const Theme::Border& border, const Vector4& color);
  312. ~Skin();
  313. static Skin* create(const char* id, float tw, float th, const Rectangle& region, const Theme::Border& border, const Vector4& color);
  314. void setRegion(const Rectangle& region, float tw, float th);
  315. std::string _id;
  316. Theme::Border _border;
  317. UVs _uvs[9];
  318. Vector4 _color;
  319. Rectangle _region;
  320. float _tw, _th;
  321. };
  322. /**
  323. * Constructor.
  324. */
  325. Theme();
  326. /**
  327. * Constructor.
  328. */
  329. Theme(const Theme& theme);
  330. /**
  331. * Destructor.
  332. */
  333. ~Theme();
  334. /**
  335. * Creates an instance of a Theme using the data from the Properties object defined at the specified URL,
  336. * where the URL is of the format "<file-path>.<extension>#<namespace-id>/<namespace-id>/.../<namespace-id>"
  337. * (and "#<namespace-id>/<namespace-id>/.../<namespace-id>" is optional).
  338. *
  339. * @param url The URL pointing to the Properties object defining the theme.
  340. *
  341. * @return A new Theme.
  342. */
  343. static Theme* create(const char* url);
  344. Theme::Style* getStyle(const char* id) const;
  345. void setProjectionMatrix(const Matrix& matrix);
  346. SpriteBatch* getSpriteBatch() const;
  347. static void generateUVs(float tw, float th, float x, float y, float width, float height, UVs* uvs);
  348. void lookUpSprites(const Properties* overlaySpace, ImageList** imageList, ThemeImage** mouseCursor, Skin** skin);
  349. std::string _url;
  350. Texture* _texture;
  351. SpriteBatch* _spriteBatch;
  352. std::vector<Style*> _styles;
  353. std::vector<ThemeImage*> _images;
  354. std::vector<ImageList*> _imageLists;
  355. std::vector<Skin*> _skins;
  356. std::set<Font*> _fonts;
  357. };
  358. }
  359. #endif