canvas_item.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /**************************************************************************/
  2. /* canvas_item.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 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/multimesh.h"
  36. #include "scene/resources/shader.h"
  37. #include "scene/resources/texture.h"
  38. class CanvasLayer;
  39. class Viewport;
  40. class Font;
  41. class StyleBox;
  42. class CanvasItemMaterial : public Material {
  43. GDCLASS(CanvasItemMaterial, Material);
  44. public:
  45. enum BlendMode {
  46. BLEND_MODE_MIX,
  47. BLEND_MODE_ADD,
  48. BLEND_MODE_SUB,
  49. BLEND_MODE_MUL,
  50. BLEND_MODE_PREMULT_ALPHA,
  51. BLEND_MODE_DISABLED
  52. };
  53. enum LightMode {
  54. LIGHT_MODE_NORMAL,
  55. LIGHT_MODE_UNSHADED,
  56. LIGHT_MODE_LIGHT_ONLY
  57. };
  58. private:
  59. union MaterialKey {
  60. struct {
  61. uint32_t blend_mode : 4;
  62. uint32_t light_mode : 4;
  63. uint32_t particles_animation : 1;
  64. uint32_t invalid_key : 1;
  65. };
  66. uint32_t key;
  67. bool operator<(const MaterialKey &p_key) const {
  68. return key < p_key.key;
  69. }
  70. };
  71. struct ShaderNames {
  72. StringName particles_anim_h_frames;
  73. StringName particles_anim_v_frames;
  74. StringName particles_anim_loop;
  75. };
  76. static ShaderNames *shader_names;
  77. struct ShaderData {
  78. RID shader;
  79. int users;
  80. };
  81. static Map<MaterialKey, ShaderData> shader_map;
  82. MaterialKey current_key;
  83. _FORCE_INLINE_ MaterialKey _compute_key() const {
  84. MaterialKey mk;
  85. mk.key = 0;
  86. mk.blend_mode = blend_mode;
  87. mk.light_mode = light_mode;
  88. mk.particles_animation = particles_animation;
  89. return mk;
  90. }
  91. static Mutex material_mutex;
  92. static SelfList<CanvasItemMaterial>::List *dirty_materials;
  93. SelfList<CanvasItemMaterial> element;
  94. void _update_shader();
  95. _FORCE_INLINE_ void _queue_shader_change();
  96. _FORCE_INLINE_ bool _is_shader_dirty() const;
  97. bool is_initialized = false;
  98. BlendMode blend_mode;
  99. LightMode light_mode;
  100. bool particles_animation;
  101. int particles_anim_h_frames;
  102. int particles_anim_v_frames;
  103. bool particles_anim_loop;
  104. protected:
  105. static void _bind_methods();
  106. void _validate_property(PropertyInfo &property) const;
  107. public:
  108. void set_blend_mode(BlendMode p_blend_mode);
  109. BlendMode get_blend_mode() const;
  110. void set_light_mode(LightMode p_light_mode);
  111. LightMode get_light_mode() const;
  112. void set_particles_animation(bool p_particles_anim);
  113. bool get_particles_animation() const;
  114. void set_particles_anim_h_frames(int p_frames);
  115. int get_particles_anim_h_frames() const;
  116. void set_particles_anim_v_frames(int p_frames);
  117. int get_particles_anim_v_frames() const;
  118. void set_particles_anim_loop(bool p_loop);
  119. bool get_particles_anim_loop() const;
  120. static void init_shaders();
  121. static void finish_shaders();
  122. static void flush_changes();
  123. RID get_shader_rid() const;
  124. virtual Shader::Mode get_shader_mode() const;
  125. CanvasItemMaterial();
  126. virtual ~CanvasItemMaterial();
  127. };
  128. VARIANT_ENUM_CAST(CanvasItemMaterial::BlendMode)
  129. VARIANT_ENUM_CAST(CanvasItemMaterial::LightMode)
  130. class CanvasItem : public Node {
  131. GDCLASS(CanvasItem, Node);
  132. friend class CanvasLayer;
  133. public:
  134. enum BlendMode {
  135. BLEND_MODE_MIX, //default
  136. BLEND_MODE_ADD,
  137. BLEND_MODE_SUB,
  138. BLEND_MODE_MUL,
  139. BLEND_MODE_PREMULT_ALPHA,
  140. BLEND_MODE_DISABLED
  141. };
  142. private:
  143. mutable SelfList<Node> xform_change;
  144. RID canvas_item;
  145. String canvas_group;
  146. CanvasLayer *canvas_layer;
  147. Color modulate;
  148. Color self_modulate;
  149. List<CanvasItem *> children_items;
  150. List<CanvasItem *>::Element *C;
  151. int light_mask;
  152. bool first_draw;
  153. bool visible;
  154. bool pending_update;
  155. bool toplevel;
  156. bool drawing;
  157. bool block_transform_notify;
  158. bool behind;
  159. bool use_parent_material;
  160. bool notify_local_transform;
  161. bool notify_transform;
  162. Ref<Material> material;
  163. mutable Transform2D global_transform;
  164. mutable bool global_invalid;
  165. void _toplevel_raise_self();
  166. void _toplevel_visibility_changed(bool p_visible);
  167. void _propagate_visibility_changed(bool p_visible);
  168. void _update_callback();
  169. void _enter_canvas();
  170. void _exit_canvas();
  171. void _notify_transform(CanvasItem *p_node);
  172. void _set_on_top(bool p_on_top) { set_draw_behind_parent(!p_on_top); }
  173. bool _is_on_top() const { return !is_draw_behind_parent_enabled(); }
  174. static CanvasItem *current_item_drawn;
  175. protected:
  176. _FORCE_INLINE_ void _notify_transform() {
  177. if (!is_inside_tree()) {
  178. return;
  179. }
  180. _notify_transform(this);
  181. if (!block_transform_notify && notify_local_transform) {
  182. notification(NOTIFICATION_LOCAL_TRANSFORM_CHANGED);
  183. }
  184. }
  185. void item_rect_changed(bool p_size_changed = true);
  186. void _notification(int p_what);
  187. static void _bind_methods();
  188. public:
  189. enum {
  190. NOTIFICATION_TRANSFORM_CHANGED = SceneTree::NOTIFICATION_TRANSFORM_CHANGED, //unique
  191. NOTIFICATION_DRAW = 30,
  192. NOTIFICATION_VISIBILITY_CHANGED = 31,
  193. NOTIFICATION_ENTER_CANVAS = 32,
  194. NOTIFICATION_EXIT_CANVAS = 33,
  195. NOTIFICATION_LOCAL_TRANSFORM_CHANGED = 35,
  196. NOTIFICATION_WORLD_2D_CHANGED = 36,
  197. };
  198. /* EDITOR */
  199. #ifdef TOOLS_ENABLED
  200. // Select the node
  201. virtual bool _edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const;
  202. // Save and restore a CanvasItem state
  203. virtual void _edit_set_state(const Dictionary &p_state){};
  204. virtual Dictionary _edit_get_state() const { return Dictionary(); };
  205. // Used to move the node
  206. virtual void _edit_set_position(const Point2 &p_position) = 0;
  207. virtual Point2 _edit_get_position() const = 0;
  208. // Used to scale the node
  209. virtual void _edit_set_scale(const Size2 &p_scale) = 0;
  210. virtual Size2 _edit_get_scale() const = 0;
  211. // Used to rotate the node
  212. virtual bool _edit_use_rotation() const { return false; };
  213. virtual void _edit_set_rotation(float p_rotation){};
  214. virtual float _edit_get_rotation() const { return 0.0; };
  215. // Used to resize/move the node
  216. virtual bool _edit_use_rect() const { return false; }; // MAYBE REPLACE BY A _edit_get_editmode()
  217. virtual void _edit_set_rect(const Rect2 &p_rect){};
  218. virtual Rect2 _edit_get_rect() const { return Rect2(0, 0, 0, 0); };
  219. virtual Size2 _edit_get_minimum_size() const { return Size2(-1, -1); }; // LOOKS WEIRD
  220. // Used to set a pivot
  221. virtual bool _edit_use_pivot() const { return false; };
  222. virtual void _edit_set_pivot(const Point2 &p_pivot){};
  223. virtual Point2 _edit_get_pivot() const { return Point2(); };
  224. virtual Transform2D _edit_get_transform() const;
  225. #endif
  226. /* VISIBILITY */
  227. void set_visible(bool p_visible);
  228. bool is_visible() const;
  229. bool is_visible_in_tree() const;
  230. void show();
  231. void hide();
  232. void update();
  233. virtual void set_light_mask(int p_light_mask);
  234. int get_light_mask() const;
  235. void set_modulate(const Color &p_modulate);
  236. Color get_modulate() const;
  237. void set_self_modulate(const Color &p_self_modulate);
  238. Color get_self_modulate() const;
  239. /* DRAWING API */
  240. void draw_line(const Point2 &p_from, const Point2 &p_to, const Color &p_color, float p_width = 1.0, bool p_antialiased = false);
  241. void draw_polyline(const Vector<Point2> &p_points, const Color &p_color, float p_width = 1.0, bool p_antialiased = false);
  242. void draw_polyline_colors(const Vector<Point2> &p_points, const Vector<Color> &p_colors, float p_width = 1.0, bool p_antialiased = false);
  243. void draw_arc(const Vector2 &p_center, float p_radius, float p_start_angle, float p_end_angle, int p_point_count, const Color &p_color, float p_width = 1.0, bool p_antialiased = false);
  244. void draw_multiline(const Vector<Point2> &p_points, const Color &p_color, float p_width = 1.0, bool p_antialiased = false);
  245. void draw_multiline_colors(const Vector<Point2> &p_points, const Vector<Color> &p_colors, float p_width = 1.0, bool p_antialiased = false);
  246. void draw_rect(const Rect2 &p_rect, const Color &p_color, bool p_filled = true, float p_width = 1.0, bool p_antialiased = false);
  247. void draw_circle(const Point2 &p_pos, float p_radius, const Color &p_color);
  248. 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>());
  249. 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>());
  250. 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 = false);
  251. void draw_style_box(const Ref<StyleBox> &p_style_box, const Rect2 &p_rect);
  252. 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>());
  253. 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);
  254. 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);
  255. void draw_mesh(const Ref<Mesh> &p_mesh, const Ref<Texture> &p_texture, const Ref<Texture> &p_normal_map, const Transform2D &p_transform = Transform2D(), const Color &p_modulate = Color(1, 1, 1));
  256. void draw_multimesh(const Ref<MultiMesh> &p_multimesh, const Ref<Texture> &p_texture, const Ref<Texture> &p_normal_map);
  257. 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);
  258. 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));
  259. void draw_set_transform(const Point2 &p_offset, float p_rot, const Size2 &p_scale);
  260. void draw_set_transform_matrix(const Transform2D &p_matrix);
  261. static CanvasItem *get_current_item_drawn();
  262. /* RECT / TRANSFORM */
  263. void set_as_toplevel(bool p_toplevel);
  264. bool is_set_as_toplevel() const;
  265. void set_draw_behind_parent(bool p_enable);
  266. bool is_draw_behind_parent_enabled() const;
  267. CanvasItem *get_parent_item() const;
  268. virtual Transform2D get_transform() const = 0;
  269. virtual Transform2D get_global_transform() const;
  270. virtual Transform2D get_global_transform_with_canvas() const;
  271. CanvasItem *get_toplevel() const;
  272. _FORCE_INLINE_ RID get_canvas_item() const {
  273. return canvas_item;
  274. }
  275. void set_block_transform_notify(bool p_enable);
  276. bool is_block_transform_notify_enabled() const;
  277. Transform2D get_canvas_transform() const;
  278. Transform2D get_viewport_transform() const;
  279. Rect2 get_viewport_rect() const;
  280. RID get_viewport_rid() const;
  281. RID get_canvas() const;
  282. ObjectID get_canvas_layer_instance_id() const;
  283. Ref<World2D> get_world_2d() const;
  284. virtual void set_material(const Ref<Material> &p_material);
  285. Ref<Material> get_material() const;
  286. virtual void set_use_parent_material(bool p_use_parent_material);
  287. bool get_use_parent_material() const;
  288. Ref<InputEvent> make_input_local(const Ref<InputEvent> &p_event) const;
  289. Vector2 make_canvas_position_local(const Vector2 &screen_point) const;
  290. Vector2 get_global_mouse_position() const;
  291. Vector2 get_local_mouse_position() const;
  292. void set_notify_local_transform(bool p_enable);
  293. bool is_local_transform_notification_enabled() const;
  294. void set_notify_transform(bool p_enable);
  295. bool is_transform_notification_enabled() const;
  296. void force_update_transform();
  297. // Used by control nodes to retrieve the parent's anchorable area
  298. virtual Rect2 get_anchorable_rect() const { return Rect2(0, 0, 0, 0); };
  299. int get_canvas_layer() const;
  300. CanvasItem();
  301. ~CanvasItem();
  302. };
  303. VARIANT_ENUM_CAST(CanvasItem::BlendMode);
  304. #endif // CANVAS_ITEM_H