canvas_item.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*************************************************************************/
  2. /* canvas_item.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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/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. BlendMode blend_mode;
  98. LightMode light_mode;
  99. bool particles_animation;
  100. int particles_anim_h_frames;
  101. int particles_anim_v_frames;
  102. bool particles_anim_loop;
  103. protected:
  104. static void _bind_methods();
  105. void _validate_property(PropertyInfo &property) const;
  106. public:
  107. void set_blend_mode(BlendMode p_blend_mode);
  108. BlendMode get_blend_mode() const;
  109. void set_light_mode(LightMode p_light_mode);
  110. LightMode get_light_mode() const;
  111. void set_particles_animation(bool p_particles_anim);
  112. bool get_particles_animation() const;
  113. void set_particles_anim_h_frames(int p_frames);
  114. int get_particles_anim_h_frames() const;
  115. void set_particles_anim_v_frames(int p_frames);
  116. int get_particles_anim_v_frames() const;
  117. void set_particles_anim_loop(bool p_frames);
  118. bool get_particles_anim_loop() const;
  119. static void init_shaders();
  120. static void finish_shaders();
  121. static void flush_changes();
  122. RID get_shader_rid() const;
  123. virtual Shader::Mode get_shader_mode() const;
  124. CanvasItemMaterial();
  125. virtual ~CanvasItemMaterial();
  126. };
  127. VARIANT_ENUM_CAST(CanvasItemMaterial::BlendMode)
  128. VARIANT_ENUM_CAST(CanvasItemMaterial::LightMode)
  129. class CanvasItem : public Node {
  130. GDCLASS(CanvasItem, Node);
  131. public:
  132. enum BlendMode {
  133. BLEND_MODE_MIX, //default
  134. BLEND_MODE_ADD,
  135. BLEND_MODE_SUB,
  136. BLEND_MODE_MUL,
  137. BLEND_MODE_PREMULT_ALPHA,
  138. BLEND_MODE_DISABLED
  139. };
  140. private:
  141. mutable SelfList<Node> xform_change;
  142. RID canvas_item;
  143. String group;
  144. CanvasLayer *canvas_layer;
  145. Color modulate;
  146. Color self_modulate;
  147. List<CanvasItem *> children_items;
  148. List<CanvasItem *>::Element *C;
  149. int light_mask;
  150. bool first_draw;
  151. bool visible;
  152. bool pending_update;
  153. bool toplevel;
  154. bool drawing;
  155. bool block_transform_notify;
  156. bool behind;
  157. bool use_parent_material;
  158. bool notify_local_transform;
  159. bool notify_transform;
  160. Ref<Material> material;
  161. mutable Transform2D global_transform;
  162. mutable bool global_invalid;
  163. void _toplevel_raise_self();
  164. void _propagate_visibility_changed(bool p_visible);
  165. void _update_callback();
  166. void _enter_canvas();
  167. void _exit_canvas();
  168. void _notify_transform(CanvasItem *p_node);
  169. void _set_on_top(bool p_on_top) { set_draw_behind_parent(!p_on_top); }
  170. bool _is_on_top() const { return !is_draw_behind_parent_enabled(); }
  171. protected:
  172. _FORCE_INLINE_ void _notify_transform() {
  173. if (!is_inside_tree()) return;
  174. _notify_transform(this);
  175. if (!block_transform_notify && notify_local_transform) notification(NOTIFICATION_LOCAL_TRANSFORM_CHANGED);
  176. }
  177. void item_rect_changed(bool p_size_changed = true);
  178. void _notification(int p_what);
  179. static void _bind_methods();
  180. public:
  181. enum {
  182. NOTIFICATION_TRANSFORM_CHANGED = SceneTree::NOTIFICATION_TRANSFORM_CHANGED, //unique
  183. NOTIFICATION_DRAW = 30,
  184. NOTIFICATION_VISIBILITY_CHANGED = 31,
  185. NOTIFICATION_ENTER_CANVAS = 32,
  186. NOTIFICATION_EXIT_CANVAS = 33,
  187. NOTIFICATION_LOCAL_TRANSFORM_CHANGED = 35,
  188. NOTIFICATION_WORLD_2D_CHANGED = 36,
  189. };
  190. /* EDITOR */
  191. // Select the node
  192. virtual bool _edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const;
  193. // Save and restore a CanvasItem state
  194. virtual void _edit_set_state(const Dictionary &p_state){};
  195. virtual Dictionary _edit_get_state() const { return Dictionary(); };
  196. // Used to move the node
  197. virtual void _edit_set_position(const Point2 &p_position) = 0;
  198. virtual Point2 _edit_get_position() const = 0;
  199. // Used to scale the node
  200. virtual void _edit_set_scale(const Size2 &p_scale) = 0;
  201. virtual Size2 _edit_get_scale() const = 0;
  202. // Used to rotate the node
  203. virtual bool _edit_use_rotation() const { return false; };
  204. virtual void _edit_set_rotation(float p_rotation){};
  205. virtual float _edit_get_rotation() const { return 0.0; };
  206. // Used to resize/move the node
  207. virtual bool _edit_use_rect() const { return false; }; // MAYBE REPLACE BY A _edit_get_editmode()
  208. virtual void _edit_set_rect(const Rect2 &p_rect){};
  209. virtual Rect2 _edit_get_rect() const { return Rect2(0, 0, 0, 0); };
  210. virtual Size2 _edit_get_minimum_size() const { return Size2(-1, -1); }; // LOOKS WEIRD
  211. // Used to set a pivot
  212. virtual bool _edit_use_pivot() const { return false; };
  213. virtual void _edit_set_pivot(const Point2 &p_pivot){};
  214. virtual Point2 _edit_get_pivot() const { return Point2(); };
  215. /* VISIBILITY */
  216. void set_visible(bool p_visible);
  217. bool is_visible() const;
  218. bool is_visible_in_tree() const;
  219. void show();
  220. void hide();
  221. void update();
  222. virtual void set_light_mask(int p_light_mask);
  223. int get_light_mask() const;
  224. void set_modulate(const Color &p_modulate);
  225. Color get_modulate() const;
  226. void set_self_modulate(const Color &p_self_modulate);
  227. Color get_self_modulate() const;
  228. /* DRAWING API */
  229. void draw_line(const Point2 &p_from, const Point2 &p_to, const Color &p_color, float p_width = 1.0, bool p_antialiased = false);
  230. void draw_polyline(const Vector<Point2> &p_points, const Color &p_color, float p_width = 1.0, bool p_antialiased = false);
  231. void draw_polyline_colors(const Vector<Point2> &p_points, const Vector<Color> &p_colors, float p_width = 1.0, bool p_antialiased = false);
  232. void draw_multiline(const Vector<Point2> &p_points, const Color &p_color, float p_width = 1.0, bool p_antialiased = false);
  233. void draw_multiline_colors(const Vector<Point2> &p_points, const Vector<Color> &p_colors, float p_width = 1.0, bool p_antialiased = false);
  234. void draw_rect(const Rect2 &p_rect, const Color &p_color, bool p_filled = true);
  235. void draw_circle(const Point2 &p_pos, float p_radius, const Color &p_color);
  236. 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>());
  237. 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>());
  238. 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);
  239. void draw_style_box(const Ref<StyleBox> &p_style_box, const Rect2 &p_rect);
  240. 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>());
  241. 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);
  242. 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);
  243. void draw_mesh(const Ref<Mesh> &p_mesh, const Ref<Texture> &p_texture, const Ref<Texture> &p_normal_map);
  244. void draw_multimesh(const Ref<MultiMesh> &p_multimesh, const Ref<Texture> &p_texture, const Ref<Texture> &p_normal_map);
  245. 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);
  246. 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));
  247. void draw_set_transform(const Point2 &p_offset, float p_rot, const Size2 &p_scale);
  248. void draw_set_transform_matrix(const Transform2D &p_matrix);
  249. /* RECT / TRANSFORM */
  250. void set_as_toplevel(bool p_toplevel);
  251. bool is_set_as_toplevel() const;
  252. void set_draw_behind_parent(bool p_enable);
  253. bool is_draw_behind_parent_enabled() const;
  254. CanvasItem *get_parent_item() const;
  255. virtual Transform2D get_transform() const = 0;
  256. virtual Transform2D get_global_transform() const;
  257. virtual Transform2D get_global_transform_with_canvas() const;
  258. CanvasItem *get_toplevel() const;
  259. _FORCE_INLINE_ RID get_canvas_item() const {
  260. return canvas_item;
  261. }
  262. void set_block_transform_notify(bool p_enable);
  263. bool is_block_transform_notify_enabled() const;
  264. Transform2D get_canvas_transform() const;
  265. Transform2D get_viewport_transform() const;
  266. Rect2 get_viewport_rect() const;
  267. RID get_viewport_rid() const;
  268. RID get_canvas() const;
  269. ObjectID get_canvas_layer_instance_id() const;
  270. Ref<World2D> get_world_2d() const;
  271. virtual void set_material(const Ref<Material> &p_material);
  272. Ref<Material> get_material() const;
  273. virtual void set_use_parent_material(bool p_use_parent_material);
  274. bool get_use_parent_material() const;
  275. Ref<InputEvent> make_input_local(const Ref<InputEvent> &p_event) const;
  276. Vector2 make_canvas_position_local(const Vector2 &screen_point) const;
  277. Vector2 get_global_mouse_position() const;
  278. Vector2 get_local_mouse_position() const;
  279. void set_notify_local_transform(bool p_enable);
  280. bool is_local_transform_notification_enabled() const;
  281. void set_notify_transform(bool p_enable);
  282. bool is_transform_notification_enabled() const;
  283. void force_update_transform();
  284. // Used by control nodes to retrieve the parent's anchorable area
  285. virtual Rect2 get_anchorable_rect() const { return Rect2(0, 0, 0, 0); };
  286. int get_canvas_layer() const;
  287. CanvasItem();
  288. ~CanvasItem();
  289. };
  290. VARIANT_ENUM_CAST(CanvasItem::BlendMode);
  291. #endif // CANVAS_ITEM_H