canvas_item.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*************************************************************************/
  2. /* canvas_item.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  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 CANVAS_ITEM_H
  31. #define CANVAS_ITEM_H
  32. #include "scene/main/node.h"
  33. #include "scene/main/scene_tree.h"
  34. #include "scene/resources/material.h"
  35. #include "scene/resources/shader.h"
  36. #include "scene/resources/texture.h"
  37. class CanvasLayer;
  38. class Viewport;
  39. class Font;
  40. class StyleBox;
  41. class CanvasItemMaterial : public Material {
  42. GDCLASS(CanvasItemMaterial, Material)
  43. public:
  44. enum BlendMode {
  45. BLEND_MODE_MIX,
  46. BLEND_MODE_ADD,
  47. BLEND_MODE_SUB,
  48. BLEND_MODE_MUL,
  49. BLEND_MODE_PREMULT_ALPHA
  50. };
  51. enum LightMode {
  52. LIGHT_MODE_NORMAL,
  53. LIGHT_MODE_UNSHADED,
  54. LIGHT_MODE_LIGHT_ONLY
  55. };
  56. private:
  57. union MaterialKey {
  58. struct {
  59. uint32_t blend_mode : 4;
  60. uint32_t light_mode : 4;
  61. uint32_t invalid_key : 1;
  62. };
  63. uint32_t key;
  64. bool operator<(const MaterialKey &p_key) const {
  65. return key < p_key.key;
  66. }
  67. };
  68. struct ShaderData {
  69. RID shader;
  70. int users;
  71. };
  72. static Map<MaterialKey, ShaderData> shader_map;
  73. MaterialKey current_key;
  74. _FORCE_INLINE_ MaterialKey _compute_key() const {
  75. MaterialKey mk;
  76. mk.key = 0;
  77. mk.blend_mode = blend_mode;
  78. mk.light_mode = light_mode;
  79. return mk;
  80. }
  81. static Mutex *material_mutex;
  82. static SelfList<CanvasItemMaterial>::List dirty_materials;
  83. SelfList<CanvasItemMaterial> element;
  84. void _update_shader();
  85. _FORCE_INLINE_ void _queue_shader_change();
  86. _FORCE_INLINE_ bool _is_shader_dirty() const;
  87. BlendMode blend_mode;
  88. LightMode light_mode;
  89. protected:
  90. static void _bind_methods();
  91. void _validate_property(PropertyInfo &property) const;
  92. public:
  93. void set_blend_mode(BlendMode p_blend_mode);
  94. BlendMode get_blend_mode() const;
  95. void set_light_mode(LightMode p_light_mode);
  96. LightMode get_light_mode() const;
  97. static void init_shaders();
  98. static void finish_shaders();
  99. static void flush_changes();
  100. CanvasItemMaterial();
  101. virtual ~CanvasItemMaterial();
  102. };
  103. VARIANT_ENUM_CAST(CanvasItemMaterial::BlendMode)
  104. VARIANT_ENUM_CAST(CanvasItemMaterial::LightMode)
  105. class CanvasItem : public Node {
  106. GDCLASS(CanvasItem, Node);
  107. public:
  108. enum BlendMode {
  109. BLEND_MODE_MIX, //default
  110. BLEND_MODE_ADD,
  111. BLEND_MODE_SUB,
  112. BLEND_MODE_MUL,
  113. BLEND_MODE_PREMULT_ALPHA
  114. };
  115. private:
  116. mutable SelfList<Node> xform_change;
  117. RID canvas_item;
  118. String group;
  119. CanvasLayer *canvas_layer;
  120. Color modulate;
  121. Color self_modulate;
  122. List<CanvasItem *> children_items;
  123. List<CanvasItem *>::Element *C;
  124. int light_mask;
  125. bool first_draw;
  126. bool visible;
  127. bool pending_update;
  128. bool toplevel;
  129. bool drawing;
  130. bool block_transform_notify;
  131. bool behind;
  132. bool use_parent_material;
  133. bool notify_local_transform;
  134. bool notify_transform;
  135. Ref<Material> material;
  136. mutable Transform2D global_transform;
  137. mutable bool global_invalid;
  138. void _toplevel_raise_self();
  139. void _propagate_visibility_changed(bool p_visible);
  140. void _update_callback();
  141. void _enter_canvas();
  142. void _exit_canvas();
  143. void _notify_transform(CanvasItem *p_node);
  144. void _set_on_top(bool p_on_top) { set_draw_behind_parent(!p_on_top); }
  145. bool _is_on_top() const { return !is_draw_behind_parent_enabled(); }
  146. protected:
  147. _FORCE_INLINE_ void _notify_transform() {
  148. if (!is_inside_tree()) return;
  149. _notify_transform(this);
  150. if (!block_transform_notify && notify_local_transform) notification(NOTIFICATION_LOCAL_TRANSFORM_CHANGED);
  151. }
  152. void item_rect_changed(bool p_size_changed = true);
  153. void _notification(int p_what);
  154. static void _bind_methods();
  155. public:
  156. enum {
  157. NOTIFICATION_TRANSFORM_CHANGED = SceneTree::NOTIFICATION_TRANSFORM_CHANGED, //unique
  158. NOTIFICATION_DRAW = 30,
  159. NOTIFICATION_VISIBILITY_CHANGED = 31,
  160. NOTIFICATION_ENTER_CANVAS = 32,
  161. NOTIFICATION_EXIT_CANVAS = 33,
  162. NOTIFICATION_LOCAL_TRANSFORM_CHANGED = 35,
  163. NOTIFICATION_WORLD_2D_CHANGED = 36,
  164. };
  165. /* EDITOR */
  166. virtual Variant edit_get_state() const;
  167. virtual void edit_set_state(const Variant &p_state);
  168. virtual void edit_set_rect(const Rect2 &p_edit_rect);
  169. virtual void edit_rotate(float p_rot);
  170. virtual Size2 edit_get_minimum_size() const;
  171. /* VISIBILITY */
  172. void set_visible(bool p_visible);
  173. bool is_visible() const;
  174. bool is_visible_in_tree() const;
  175. void show();
  176. void hide();
  177. void update();
  178. virtual void set_light_mask(int p_light_mask);
  179. int get_light_mask() const;
  180. void set_modulate(const Color &p_modulate);
  181. Color get_modulate() const;
  182. void set_self_modulate(const Color &p_self_modulate);
  183. Color get_self_modulate() const;
  184. /* DRAWING API */
  185. void draw_line(const Point2 &p_from, const Point2 &p_to, const Color &p_color, float p_width = 1.0, bool p_antialiased = false);
  186. void draw_polyline(const Vector<Point2> &p_points, const Color &p_color, float p_width = 1.0, bool p_antialiased = false);
  187. void draw_polyline_colors(const Vector<Point2> &p_points, const Vector<Color> &p_colors, float p_width = 1.0, bool p_antialiased = false);
  188. void draw_rect(const Rect2 &p_rect, const Color &p_color, bool p_filled = true);
  189. void draw_circle(const Point2 &p_pos, float p_radius, const Color &p_color);
  190. void draw_texture(const Ref<Texture> &p_texture, const Point2 &p_pos, const Color &p_modulate = Color(1, 1, 1, 1), const Ref<Texture> &p_normal_map = Ref<Texture>());
  191. void draw_texture_rect(const Ref<Texture> &p_texture, const Rect2 &p_rect, bool p_tile = false, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false, const Ref<Texture> &p_normal_map = Ref<Texture>());
  192. void draw_texture_rect_region(const Ref<Texture> &p_texture, const Rect2 &p_rect, const Rect2 &p_src_rect, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false, const Ref<Texture> &p_normal_map = Ref<Texture>(), bool p_clip_uv = true);
  193. void draw_style_box(const Ref<StyleBox> &p_style_box, const Rect2 &p_rect);
  194. void draw_primitive(const Vector<Point2> &p_points, const Vector<Color> &p_colors, const Vector<Point2> &p_uvs, Ref<Texture> p_texture = Ref<Texture>(), float p_width = 1, const Ref<Texture> &p_normal_map = Ref<Texture>());
  195. void draw_polygon(const Vector<Point2> &p_points, const Vector<Color> &p_colors, const Vector<Point2> &p_uvs = Vector<Point2>(), Ref<Texture> p_texture = Ref<Texture>(), const Ref<Texture> &p_normal_map = Ref<Texture>(), bool p_antialiased = false);
  196. void draw_colored_polygon(const Vector<Point2> &p_points, const Color &p_color, const Vector<Point2> &p_uvs = Vector<Point2>(), Ref<Texture> p_texture = Ref<Texture>(), const Ref<Texture> &p_normal_map = Ref<Texture>(), bool p_antialiased = false);
  197. void draw_string(const Ref<Font> &p_font, const Point2 &p_pos, const String &p_text, const Color &p_modulate = Color(1, 1, 1), int p_clip_w = -1);
  198. float draw_char(const Ref<Font> &p_font, const Point2 &p_pos, const String &p_char, const String &p_next = "", const Color &p_modulate = Color(1, 1, 1));
  199. void draw_set_transform(const Point2 &p_offset, float p_rot, const Size2 &p_scale);
  200. void draw_set_transform_matrix(const Transform2D &p_matrix);
  201. /* RECT / TRANSFORM */
  202. void set_as_toplevel(bool p_toplevel);
  203. bool is_set_as_toplevel() const;
  204. void set_draw_behind_parent(bool p_enable);
  205. bool is_draw_behind_parent_enabled() const;
  206. CanvasItem *get_parent_item() const;
  207. virtual Rect2 get_item_rect() const = 0;
  208. virtual Transform2D get_transform() const = 0;
  209. virtual Transform2D get_global_transform() const;
  210. virtual Transform2D get_global_transform_with_canvas() const;
  211. Rect2 get_item_and_children_rect() const;
  212. CanvasItem *get_toplevel() const;
  213. _FORCE_INLINE_ RID get_canvas_item() const { return canvas_item; }
  214. void set_block_transform_notify(bool p_enable);
  215. bool is_block_transform_notify_enabled() const;
  216. Transform2D get_canvas_transform() const;
  217. Transform2D get_viewport_transform() const;
  218. Rect2 get_viewport_rect() const;
  219. RID get_viewport_rid() const;
  220. RID get_canvas() const;
  221. Ref<World2D> get_world_2d() const;
  222. virtual void set_material(const Ref<Material> &p_material);
  223. Ref<Material> get_material() const;
  224. virtual void set_use_parent_material(bool p_use_parent_material);
  225. bool get_use_parent_material() const;
  226. Ref<InputEvent> make_input_local(const Ref<InputEvent> &p_event) const;
  227. Vector2 make_canvas_position_local(const Vector2 &screen_point) const;
  228. Vector2 get_global_mouse_position() const;
  229. Vector2 get_local_mouse_position() const;
  230. void set_notify_local_transform(bool p_enable);
  231. bool is_local_transform_notification_enabled() const;
  232. void set_notify_transform(bool p_enable);
  233. bool is_transform_notification_enabled() const;
  234. int get_canvas_layer() const;
  235. CanvasItem();
  236. ~CanvasItem();
  237. };
  238. VARIANT_ENUM_CAST(CanvasItem::BlendMode);
  239. #endif // CANVAS_ITEM_H