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
31#ifndef CANVAS_ITEM_H
32#define CANVAS_ITEM_H
33
34#include "scene/main/node.h"
35#include "scene/resources/canvas_item_material.h"
36#include "scene/resources/font.h"
37
38class CanvasLayer;
39class MultiMesh;
40class StyleBox;
41class Window;
42class World2D;
43
44class CanvasItem : public Node {
45 GDCLASS(CanvasItem, Node);
46
47 friend class CanvasLayer;
48
49public:
50 enum TextureFilter {
51 TEXTURE_FILTER_PARENT_NODE,
52 TEXTURE_FILTER_NEAREST,
53 TEXTURE_FILTER_LINEAR,
54 TEXTURE_FILTER_NEAREST_WITH_MIPMAPS,
55 TEXTURE_FILTER_LINEAR_WITH_MIPMAPS,
56 TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC,
57 TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC,
58 TEXTURE_FILTER_MAX
59 };
60
61 enum TextureRepeat {
62 TEXTURE_REPEAT_PARENT_NODE,
63 TEXTURE_REPEAT_DISABLED,
64 TEXTURE_REPEAT_ENABLED,
65 TEXTURE_REPEAT_MIRROR,
66 TEXTURE_REPEAT_MAX,
67 };
68
69 enum ClipChildrenMode {
70 CLIP_CHILDREN_DISABLED,
71 CLIP_CHILDREN_ONLY,
72 CLIP_CHILDREN_AND_DRAW,
73 CLIP_CHILDREN_MAX,
74 };
75
76private:
77 mutable SelfList<Node>
78 xform_change;
79
80 RID canvas_item;
81 StringName canvas_group;
82
83 CanvasLayer *canvas_layer = nullptr;
84
85 Color modulate = Color(1, 1, 1, 1);
86 Color self_modulate = Color(1, 1, 1, 1);
87
88 List<CanvasItem *> children_items;
89 List<CanvasItem *>::Element *C = nullptr;
90
91 int light_mask = 1;
92 uint32_t visibility_layer = 1;
93
94 int z_index = 0;
95 bool z_relative = true;
96 bool y_sort_enabled = false;
97
98 Window *window = nullptr;
99 bool visible = true;
100 bool parent_visible_in_tree = false;
101 bool pending_update = false;
102 bool top_level = false;
103 bool drawing = false;
104 bool block_transform_notify = false;
105 bool behind = false;
106 bool use_parent_material = false;
107 bool notify_local_transform = false;
108 bool notify_transform = false;
109 bool hide_clip_children = false;
110
111 ClipChildrenMode clip_children_mode = CLIP_CHILDREN_DISABLED;
112
113 mutable RS::CanvasItemTextureFilter texture_filter_cache = RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR;
114 mutable RS::CanvasItemTextureRepeat texture_repeat_cache = RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED;
115 TextureFilter texture_filter = TEXTURE_FILTER_PARENT_NODE;
116 TextureRepeat texture_repeat = TEXTURE_REPEAT_PARENT_NODE;
117
118 Ref<Material> material;
119
120 mutable Transform2D global_transform;
121 mutable MTFlag global_invalid;
122
123 _FORCE_INLINE_ bool _is_global_invalid() const { return is_group_processing() ? global_invalid.mt.is_set() : global_invalid.st; }
124 void _set_global_invalid(bool p_invalid) const;
125
126 void _top_level_raise_self();
127
128 void _propagate_visibility_changed(bool p_parent_visible_in_tree);
129 void _handle_visibility_change(bool p_visible);
130
131 virtual void _top_level_changed();
132 virtual void _top_level_changed_on_parent();
133
134 void _redraw_callback();
135
136 void _enter_canvas();
137 void _exit_canvas();
138
139 void _window_visibility_changed();
140
141 void _notify_transform(CanvasItem *p_node);
142
143 static CanvasItem *current_item_drawn;
144 friend class Viewport;
145 void _refresh_texture_repeat_cache() const;
146 void _update_texture_repeat_changed(bool p_propagate);
147 void _refresh_texture_filter_cache() const;
148 void _update_texture_filter_changed(bool p_propagate);
149
150 void _notify_transform_deferred();
151
152protected:
153 _FORCE_INLINE_ void _notify_transform() {
154 _notify_transform(this);
155 if (is_inside_tree() && !block_transform_notify && notify_local_transform) {
156 notification(NOTIFICATION_LOCAL_TRANSFORM_CHANGED);
157 }
158 }
159
160 void item_rect_changed(bool p_size_changed = true);
161
162 void _notification(int p_what);
163 static void _bind_methods();
164 void _validate_property(PropertyInfo &p_property) const;
165
166 _FORCE_INLINE_ void set_hide_clip_children(bool p_value) { hide_clip_children = p_value; }
167
168 GDVIRTUAL0(_draw)
169
170public:
171 enum {
172 NOTIFICATION_TRANSFORM_CHANGED = SceneTree::NOTIFICATION_TRANSFORM_CHANGED, //unique
173 NOTIFICATION_DRAW = 30,
174 NOTIFICATION_VISIBILITY_CHANGED = 31,
175 NOTIFICATION_ENTER_CANVAS = 32,
176 NOTIFICATION_EXIT_CANVAS = 33,
177 NOTIFICATION_LOCAL_TRANSFORM_CHANGED = 35,
178 NOTIFICATION_WORLD_2D_CHANGED = 36,
179 };
180
181 /* EDITOR */
182#ifdef TOOLS_ENABLED
183 // Select the node
184 virtual bool _edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const;
185
186 // Save and restore a CanvasItem state
187 virtual void _edit_set_state(const Dictionary &p_state) {}
188 virtual Dictionary _edit_get_state() const { return Dictionary(); }
189
190 // Used to move the node
191 virtual void _edit_set_position(const Point2 &p_position) = 0;
192 virtual Point2 _edit_get_position() const = 0;
193
194 // Used to scale the node
195 virtual void _edit_set_scale(const Size2 &p_scale) = 0;
196 virtual Size2 _edit_get_scale() const = 0;
197
198 // Used to rotate the node
199 virtual bool _edit_use_rotation() const { return false; };
200 virtual void _edit_set_rotation(real_t p_rotation) {}
201 virtual real_t _edit_get_rotation() const { return 0.0; };
202
203 // Used to resize/move the node
204 virtual bool _edit_use_rect() const { return false; }; // MAYBE REPLACE BY A _edit_get_editmode()
205 virtual void _edit_set_rect(const Rect2 &p_rect) {}
206 virtual Rect2 _edit_get_rect() const { return Rect2(0, 0, 0, 0); };
207 virtual Size2 _edit_get_minimum_size() const { return Size2(-1, -1); }; // LOOKS WEIRD
208
209 // Used to set a pivot
210 virtual bool _edit_use_pivot() const { return false; };
211 virtual void _edit_set_pivot(const Point2 &p_pivot) {}
212 virtual Point2 _edit_get_pivot() const { return Point2(); };
213
214 virtual Transform2D _edit_get_transform() const;
215#endif
216
217 void update_draw_order();
218
219 /* VISIBILITY */
220
221 void set_visible(bool p_visible);
222 bool is_visible() const;
223 bool is_visible_in_tree() const;
224 void show();
225 void hide();
226
227 void queue_redraw();
228 void move_to_front();
229
230 void set_clip_children_mode(ClipChildrenMode p_clip_mode);
231 ClipChildrenMode get_clip_children_mode() const;
232
233 virtual void set_light_mask(int p_light_mask);
234 int get_light_mask() const;
235
236 void set_modulate(const Color &p_modulate);
237 Color get_modulate() const;
238 Color get_modulate_in_tree() const;
239
240 void set_self_modulate(const Color &p_self_modulate);
241 Color get_self_modulate() const;
242
243 void set_visibility_layer(uint32_t p_visibility_layer);
244 uint32_t get_visibility_layer() const;
245
246 void set_visibility_layer_bit(uint32_t p_visibility_layer, bool p_enable);
247 bool get_visibility_layer_bit(uint32_t p_visibility_layer) const;
248
249 /* ORDERING */
250
251 void set_z_index(int p_z);
252 int get_z_index() const;
253 int get_effective_z_index() const;
254
255 void set_z_as_relative(bool p_enabled);
256 bool is_z_relative() const;
257
258 virtual void set_y_sort_enabled(bool p_enabled);
259 virtual bool is_y_sort_enabled() const;
260
261 /* DRAWING API */
262
263 void draw_dashed_line(const Point2 &p_from, const Point2 &p_to, const Color &p_color, real_t p_width = -1.0, real_t p_dash = 2.0, bool p_aligned = true);
264 void draw_line(const Point2 &p_from, const Point2 &p_to, const Color &p_color, real_t p_width = -1.0, bool p_antialiased = false);
265 void draw_polyline(const Vector<Point2> &p_points, const Color &p_color, real_t p_width = -1.0, bool p_antialiased = false);
266 void draw_polyline_colors(const Vector<Point2> &p_points, const Vector<Color> &p_colors, real_t p_width = -1.0, bool p_antialiased = false);
267 void draw_arc(const Vector2 &p_center, real_t p_radius, real_t p_start_angle, real_t p_end_angle, int p_point_count, const Color &p_color, real_t p_width = -1.0, bool p_antialiased = false);
268 void draw_multiline(const Vector<Point2> &p_points, const Color &p_color, real_t p_width = -1.0);
269 void draw_multiline_colors(const Vector<Point2> &p_points, const Vector<Color> &p_colors, real_t p_width = -1.0);
270 void draw_rect(const Rect2 &p_rect, const Color &p_color, bool p_filled = true, real_t p_width = -1.0);
271 void draw_circle(const Point2 &p_pos, real_t p_radius, const Color &p_color);
272 void draw_texture(const Ref<Texture2D> &p_texture, const Point2 &p_pos, const Color &p_modulate = Color(1, 1, 1, 1));
273 void draw_texture_rect(const Ref<Texture2D> &p_texture, const Rect2 &p_rect, bool p_tile = false, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false);
274 void draw_texture_rect_region(const Ref<Texture2D> &p_texture, const Rect2 &p_rect, const Rect2 &p_src_rect, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false, bool p_clip_uv = false);
275 void draw_msdf_texture_rect_region(const Ref<Texture2D> &p_texture, const Rect2 &p_rect, const Rect2 &p_src_rect, const Color &p_modulate = Color(1, 1, 1), double p_outline = 0.0, double p_pixel_range = 4.0, double p_scale = 1.0);
276 void draw_lcd_texture_rect_region(const Ref<Texture2D> &p_texture, const Rect2 &p_rect, const Rect2 &p_src_rect, const Color &p_modulate = Color(1, 1, 1));
277 void draw_style_box(const Ref<StyleBox> &p_style_box, const Rect2 &p_rect);
278 void draw_primitive(const Vector<Point2> &p_points, const Vector<Color> &p_colors, const Vector<Point2> &p_uvs, Ref<Texture2D> p_texture = Ref<Texture2D>());
279 void draw_polygon(const Vector<Point2> &p_points, const Vector<Color> &p_colors, const Vector<Point2> &p_uvs = Vector<Point2>(), Ref<Texture2D> p_texture = Ref<Texture2D>());
280 void draw_colored_polygon(const Vector<Point2> &p_points, const Color &p_color, const Vector<Point2> &p_uvs = Vector<Point2>(), Ref<Texture2D> p_texture = Ref<Texture2D>());
281
282 void draw_mesh(const Ref<Mesh> &p_mesh, const Ref<Texture2D> &p_texture, const Transform2D &p_transform = Transform2D(), const Color &p_modulate = Color(1, 1, 1));
283 void draw_multimesh(const Ref<MultiMesh> &p_multimesh, const Ref<Texture2D> &p_texture);
284
285 void draw_string(const Ref<Font> &p_font, const Point2 &p_pos, const String &p_text, HorizontalAlignment p_alignment = HORIZONTAL_ALIGNMENT_LEFT, float p_width = -1, int p_font_size = Font::DEFAULT_FONT_SIZE, const Color &p_modulate = Color(1.0, 1.0, 1.0), BitField<TextServer::JustificationFlag> p_jst_flags = TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND, TextServer::Direction p_direction = TextServer::DIRECTION_AUTO, TextServer::Orientation p_orientation = TextServer::ORIENTATION_HORIZONTAL) const;
286 void draw_multiline_string(const Ref<Font> &p_font, const Point2 &p_pos, const String &p_text, HorizontalAlignment p_alignment = HORIZONTAL_ALIGNMENT_LEFT, float p_width = -1, int p_font_size = Font::DEFAULT_FONT_SIZE, int p_max_lines = -1, const Color &p_modulate = Color(1.0, 1.0, 1.0), BitField<TextServer::LineBreakFlag> p_brk_flags = TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND, BitField<TextServer::JustificationFlag> p_jst_flags = TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND, TextServer::Direction p_direction = TextServer::DIRECTION_AUTO, TextServer::Orientation p_orientation = TextServer::ORIENTATION_HORIZONTAL) const;
287
288 void draw_string_outline(const Ref<Font> &p_font, const Point2 &p_pos, const String &p_text, HorizontalAlignment p_alignment = HORIZONTAL_ALIGNMENT_LEFT, float p_width = -1, int p_font_size = Font::DEFAULT_FONT_SIZE, int p_size = 1, const Color &p_modulate = Color(1.0, 1.0, 1.0), BitField<TextServer::JustificationFlag> p_jst_flags = TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND, TextServer::Direction p_direction = TextServer::DIRECTION_AUTO, TextServer::Orientation p_orientation = TextServer::ORIENTATION_HORIZONTAL) const;
289 void draw_multiline_string_outline(const Ref<Font> &p_font, const Point2 &p_pos, const String &p_text, HorizontalAlignment p_alignment = HORIZONTAL_ALIGNMENT_LEFT, float p_width = -1, int p_font_size = Font::DEFAULT_FONT_SIZE, int p_max_lines = -1, int p_size = 1, const Color &p_modulate = Color(1.0, 1.0, 1.0), BitField<TextServer::LineBreakFlag> p_brk_flags = TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND, BitField<TextServer::JustificationFlag> p_jst_flags = TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND, TextServer::Direction p_direction = TextServer::DIRECTION_AUTO, TextServer::Orientation p_orientation = TextServer::ORIENTATION_HORIZONTAL) const;
290
291 void draw_char(const Ref<Font> &p_font, const Point2 &p_pos, const String &p_char, int p_font_size = Font::DEFAULT_FONT_SIZE, const Color &p_modulate = Color(1.0, 1.0, 1.0)) const;
292 void draw_char_outline(const Ref<Font> &p_font, const Point2 &p_pos, const String &p_char, int p_font_size = Font::DEFAULT_FONT_SIZE, int p_size = 1, const Color &p_modulate = Color(1.0, 1.0, 1.0)) const;
293
294 void draw_set_transform(const Point2 &p_offset, real_t p_rot = 0.0, const Size2 &p_scale = Size2(1.0, 1.0));
295 void draw_set_transform_matrix(const Transform2D &p_matrix);
296 void draw_animation_slice(double p_animation_length, double p_slice_begin, double p_slice_end, double p_offset = 0);
297 void draw_end_animation();
298
299 static CanvasItem *get_current_item_drawn();
300
301 /* RECT / TRANSFORM */
302
303 void set_as_top_level(bool p_top_level);
304 bool is_set_as_top_level() const;
305
306 void set_draw_behind_parent(bool p_enable);
307 bool is_draw_behind_parent_enabled() const;
308
309 CanvasItem *get_parent_item() const;
310
311 virtual Transform2D get_transform() const = 0;
312
313 virtual Transform2D get_global_transform() const;
314 virtual Transform2D get_global_transform_with_canvas() const;
315 virtual Transform2D get_screen_transform() const;
316
317 CanvasItem *get_top_level() const;
318 _FORCE_INLINE_ RID get_canvas_item() const {
319 return canvas_item;
320 }
321
322 void set_block_transform_notify(bool p_enable);
323 bool is_block_transform_notify_enabled() const;
324
325 Transform2D get_canvas_transform() const;
326 Transform2D get_viewport_transform() const;
327 Rect2 get_viewport_rect() const;
328 RID get_viewport_rid() const;
329 RID get_canvas() const;
330 ObjectID get_canvas_layer_instance_id() const;
331 Ref<World2D> get_world_2d() const;
332
333 virtual void set_material(const Ref<Material> &p_material);
334 Ref<Material> get_material() const;
335
336 virtual void set_use_parent_material(bool p_use_parent_material);
337 bool get_use_parent_material() const;
338
339 Ref<InputEvent> make_input_local(const Ref<InputEvent> &p_event) const;
340 Vector2 make_canvas_position_local(const Vector2 &screen_point) const;
341
342 Vector2 get_global_mouse_position() const;
343 Vector2 get_local_mouse_position() const;
344
345 void set_notify_local_transform(bool p_enable);
346 bool is_local_transform_notification_enabled() const;
347
348 void set_notify_transform(bool p_enable);
349 bool is_transform_notification_enabled() const;
350
351 void force_update_transform();
352
353 virtual void set_texture_filter(TextureFilter p_texture_filter);
354 TextureFilter get_texture_filter() const;
355
356 virtual void set_texture_repeat(TextureRepeat p_texture_repeat);
357 TextureRepeat get_texture_repeat() const;
358
359 TextureFilter get_texture_filter_in_tree() const;
360 TextureRepeat get_texture_repeat_in_tree() const;
361
362 // Used by control nodes to retrieve the parent's anchorable area
363 virtual Rect2 get_anchorable_rect() const { return Rect2(0, 0, 0, 0); };
364
365 int get_canvas_layer() const;
366
367 CanvasItem();
368 ~CanvasItem();
369};
370
371VARIANT_ENUM_CAST(CanvasItem::TextureFilter)
372VARIANT_ENUM_CAST(CanvasItem::TextureRepeat)
373VARIANT_ENUM_CAST(CanvasItem::ClipChildrenMode)
374
375class CanvasTexture : public Texture2D {
376 GDCLASS(CanvasTexture, Texture2D);
377 OBJ_SAVE_TYPE(Texture2D); // Saves derived classes with common type so they can be interchanged.
378
379 Ref<Texture2D> diffuse_texture;
380 Ref<Texture2D> normal_texture;
381 Ref<Texture2D> specular_texture;
382 Color specular = Color(1, 1, 1, 1);
383 real_t shininess = 1.0;
384
385 RID canvas_texture;
386
387 CanvasItem::TextureFilter texture_filter = CanvasItem::TEXTURE_FILTER_PARENT_NODE;
388 CanvasItem::TextureRepeat texture_repeat = CanvasItem::TEXTURE_REPEAT_PARENT_NODE;
389
390protected:
391 static void _bind_methods();
392
393public:
394 void set_diffuse_texture(const Ref<Texture2D> &p_diffuse);
395 Ref<Texture2D> get_diffuse_texture() const;
396
397 void set_normal_texture(const Ref<Texture2D> &p_normal);
398 Ref<Texture2D> get_normal_texture() const;
399
400 void set_specular_texture(const Ref<Texture2D> &p_specular);
401 Ref<Texture2D> get_specular_texture() const;
402
403 void set_specular_color(const Color &p_color);
404 Color get_specular_color() const;
405
406 void set_specular_shininess(real_t p_shininess);
407 real_t get_specular_shininess() const;
408
409 void set_texture_filter(CanvasItem::TextureFilter p_filter);
410 CanvasItem::TextureFilter get_texture_filter() const;
411
412 void set_texture_repeat(CanvasItem::TextureRepeat p_repeat);
413 CanvasItem::TextureRepeat get_texture_repeat() const;
414
415 virtual int get_width() const override;
416 virtual int get_height() const override;
417
418 virtual bool is_pixel_opaque(int p_x, int p_y) const override;
419 virtual bool has_alpha() const override;
420
421 virtual Ref<Image> get_image() const override;
422
423 virtual RID get_rid() const override;
424
425 CanvasTexture();
426 ~CanvasTexture();
427};
428
429#endif // CANVAS_ITEM_H
430