style_box.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /**************************************************************************/
  2. /* style_box.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef STYLE_BOX_H
  31. #define STYLE_BOX_H
  32. #include "core/resource.h"
  33. #include "scene/resources/texture.h"
  34. #include "servers/visual_server.h"
  35. class CanvasItem;
  36. class StyleBox : public Resource {
  37. GDCLASS(StyleBox, Resource);
  38. RES_BASE_EXTENSION("stylebox");
  39. OBJ_SAVE_TYPE(StyleBox);
  40. float margin[4];
  41. protected:
  42. virtual float get_style_margin(Margin p_margin) const = 0;
  43. static void _bind_methods();
  44. public:
  45. virtual bool test_mask(const Point2 &p_point, const Rect2 &p_rect) const;
  46. void set_default_margin(Margin p_margin, float p_value);
  47. float get_default_margin(Margin p_margin) const;
  48. float get_margin(Margin p_margin) const;
  49. virtual Size2 get_center_size() const;
  50. virtual Rect2 get_draw_rect(const Rect2 &p_rect) const;
  51. virtual void draw(RID p_canvas_item, const Rect2 &p_rect) const = 0;
  52. CanvasItem *get_current_item_drawn() const;
  53. Size2 get_minimum_size() const;
  54. Point2 get_offset() const;
  55. StyleBox();
  56. };
  57. class StyleBoxEmpty : public StyleBox {
  58. GDCLASS(StyleBoxEmpty, StyleBox);
  59. virtual float get_style_margin(Margin p_margin) const { return 0; }
  60. public:
  61. virtual void draw(RID p_canvas_item, const Rect2 &p_rect) const {}
  62. StyleBoxEmpty() {}
  63. };
  64. class StyleBoxTexture : public StyleBox {
  65. GDCLASS(StyleBoxTexture, StyleBox);
  66. public:
  67. enum AxisStretchMode {
  68. AXIS_STRETCH_MODE_STRETCH,
  69. AXIS_STRETCH_MODE_TILE,
  70. AXIS_STRETCH_MODE_TILE_FIT,
  71. };
  72. private:
  73. float expand_margin[4];
  74. float margin[4];
  75. Rect2 region_rect;
  76. Ref<Texture> texture;
  77. Ref<Texture> normal_map;
  78. bool draw_center;
  79. Color modulate;
  80. AxisStretchMode axis_h;
  81. AxisStretchMode axis_v;
  82. protected:
  83. virtual float get_style_margin(Margin p_margin) const;
  84. static void _bind_methods();
  85. public:
  86. void set_expand_margin_size(Margin p_expand_margin, float p_size);
  87. void set_expand_margin_size_all(float p_expand_margin_size);
  88. void set_expand_margin_size_individual(float p_left, float p_top, float p_right, float p_bottom);
  89. float get_expand_margin_size(Margin p_expand_margin) const;
  90. void set_margin_size(Margin p_margin, float p_size);
  91. float get_margin_size(Margin p_margin) const;
  92. void set_region_rect(const Rect2 &p_region_rect);
  93. Rect2 get_region_rect() const;
  94. void set_texture(Ref<Texture> p_texture);
  95. Ref<Texture> get_texture() const;
  96. void set_normal_map(Ref<Texture> p_normal_map);
  97. Ref<Texture> get_normal_map() const;
  98. void set_draw_center(bool p_enabled);
  99. bool is_draw_center_enabled() const;
  100. virtual Size2 get_center_size() const;
  101. void set_h_axis_stretch_mode(AxisStretchMode p_mode);
  102. AxisStretchMode get_h_axis_stretch_mode() const;
  103. void set_v_axis_stretch_mode(AxisStretchMode p_mode);
  104. AxisStretchMode get_v_axis_stretch_mode() const;
  105. void set_modulate(const Color &p_modulate);
  106. Color get_modulate() const;
  107. virtual Rect2 get_draw_rect(const Rect2 &p_rect) const;
  108. virtual void draw(RID p_canvas_item, const Rect2 &p_rect) const;
  109. StyleBoxTexture();
  110. ~StyleBoxTexture();
  111. };
  112. VARIANT_ENUM_CAST(StyleBoxTexture::AxisStretchMode)
  113. class StyleBoxFlat : public StyleBox {
  114. GDCLASS(StyleBoxFlat, StyleBox);
  115. Color bg_color;
  116. Color shadow_color;
  117. Color border_color;
  118. real_t border_width[4] = {};
  119. real_t expand_margin[4] = {};
  120. real_t corner_radius[4] = {};
  121. bool draw_center;
  122. bool blend_border;
  123. Vector2 skew;
  124. bool anti_aliased;
  125. int corner_detail;
  126. int shadow_size;
  127. Point2 shadow_offset;
  128. real_t aa_size;
  129. protected:
  130. virtual float get_style_margin(Margin p_margin) const;
  131. static void _bind_methods();
  132. public:
  133. void set_bg_color(const Color &p_color);
  134. Color get_bg_color() const;
  135. void set_border_color(const Color &p_color);
  136. Color get_border_color() const;
  137. void set_border_width_all(int p_size);
  138. int get_border_width_min() const;
  139. void set_border_width(Margin p_margin, int p_width);
  140. int get_border_width(Margin p_margin) const;
  141. void set_border_blend(bool p_blend);
  142. bool get_border_blend() const;
  143. void set_corner_radius_all(int radius);
  144. void set_corner_radius_individual(const int radius_top_left, const int radius_top_right, const int radius_bottom_right, const int radius_bottom_left);
  145. int get_corner_radius_min() const;
  146. void set_corner_radius(Corner p_corner, const int radius);
  147. int get_corner_radius(Corner p_corner) const;
  148. void set_corner_detail(const int &p_corner_detail);
  149. int get_corner_detail() const;
  150. void set_expand_margin_size(Margin p_expand_margin, float p_size);
  151. void set_expand_margin_size_all(float p_expand_margin_size);
  152. void set_expand_margin_size_individual(float p_left, float p_top, float p_right, float p_bottom);
  153. float get_expand_margin_size(Margin p_expand_margin) const;
  154. void set_draw_center(bool p_enabled);
  155. bool is_draw_center_enabled() const;
  156. void set_skew(Vector2 p_skew);
  157. Vector2 get_skew() const;
  158. void set_shadow_color(const Color &p_color);
  159. Color get_shadow_color() const;
  160. void set_shadow_size(const int &p_size);
  161. int get_shadow_size() const;
  162. void set_shadow_offset(const Point2 &p_offset);
  163. Point2 get_shadow_offset() const;
  164. void set_anti_aliased(const bool &p_anti_aliased);
  165. bool is_anti_aliased() const;
  166. void set_aa_size(const float &p_aa_size);
  167. float get_aa_size() const;
  168. virtual Size2 get_center_size() const;
  169. virtual Rect2 get_draw_rect(const Rect2 &p_rect) const;
  170. virtual void draw(RID p_canvas_item, const Rect2 &p_rect) const;
  171. StyleBoxFlat();
  172. ~StyleBoxFlat();
  173. };
  174. // Just used to draw lines.
  175. class StyleBoxLine : public StyleBox {
  176. GDCLASS(StyleBoxLine, StyleBox);
  177. Color color;
  178. int thickness;
  179. bool vertical;
  180. float grow_begin;
  181. float grow_end;
  182. protected:
  183. virtual float get_style_margin(Margin p_margin) const;
  184. static void _bind_methods();
  185. public:
  186. void set_color(const Color &p_color);
  187. Color get_color() const;
  188. void set_thickness(int p_thickness);
  189. int get_thickness() const;
  190. void set_vertical(bool p_vertical);
  191. bool is_vertical() const;
  192. void set_grow_begin(float p_grow);
  193. float get_grow_begin() const;
  194. void set_grow_end(float p_grow);
  195. float get_grow_end() const;
  196. virtual Size2 get_center_size() const;
  197. virtual void draw(RID p_canvas_item, const Rect2 &p_rect) const;
  198. StyleBoxLine();
  199. ~StyleBoxLine();
  200. };
  201. #endif // STYLE_BOX_H