canvas_item.h
1 /*************************************************************************/
2 /* canvas_item.h */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* http://www.godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
9 /* */
10 /* Permission is hereby granted, free of charge, to any person obtaining */
11 /* a copy of this software and associated documentation files (the */
12 /* "Software"), to deal in the Software without restriction, including */
13 /* without limitation the rights to use, copy, modify, merge, publish, */
14 /* distribute, sublicense, and/or sell copies of the Software, and to */
15 /* permit persons to whom the Software is furnished to do so, subject to */
16 /* the following conditions: */
17 /* */
18 /* The above copyright notice and this permission notice shall be */
19 /* included in all copies or substantial portions of the Software. */
20 /* */
21 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
22 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
23 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
24 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
25 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
26 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
27 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
28 /*************************************************************************/
29 #ifndef CANVAS_ITEM_H
30 #define CANVAS_ITEM_H
31 
32 #include "scene/main/node.h"
33 #include "scene/resources/texture.h"
34 #include "scene/main/scene_main_loop.h"
35 #include "scene/resources/shader.h"
36 
37 class CanvasLayer;
38 class Viewport;
39 class Font;
40 
41 class StyleBox;
42 
44 
45  OBJ_TYPE(CanvasItemMaterial,Resource);
46  RID material;
47  Ref<Shader> shader;
48 public:
49  enum ShadingMode {
50  SHADING_NORMAL,
51  SHADING_UNSHADED,
52  SHADING_ONLY_LIGHT,
53  };
54 
55 protected:
56 
57  ShadingMode shading_mode;
58 
59  bool _set(const StringName& p_name, const Variant& p_value);
60  bool _get(const StringName& p_name,Variant &r_ret) const;
61  void _get_property_list( List<PropertyInfo> *p_list) const;
62 
63  void _shader_changed();
64  static void _bind_methods();
65 
66  void get_argument_options(const StringName& p_function,int p_idx,List<String>*r_options) const;
67 
68 public:
69 
70  void set_shader(const Ref<Shader>& p_shader);
71  Ref<Shader> get_shader() const;
72 
73  void set_shader_param(const StringName& p_param,const Variant& p_value);
74  Variant get_shader_param(const StringName& p_param) const;
75 
76  void set_shading_mode(ShadingMode p_mode);
77  ShadingMode get_shading_mode() const;
78 
79  virtual RID get_rid() const;
82 };
83 
84 VARIANT_ENUM_CAST( CanvasItemMaterial::ShadingMode );
85 
86 
87 class CanvasItem : public Node {
88 
89  OBJ_TYPE( CanvasItem, Node );
90 public:
91 
92  enum BlendMode {
93 
94  BLEND_MODE_MIX, //default
95  BLEND_MODE_ADD,
96  BLEND_MODE_SUB,
97  BLEND_MODE_MUL,
98  BLEND_MODE_PREMULT_ALPHA
99  };
100 
101 private:
102 
103  mutable SelfList<Node> xform_change;
104 
105  RID canvas_item;
106  String group;
107 
108 
109  CanvasLayer *canvas_layer;
110 
111  float opacity;
112  float self_opacity;
113 
114  List<CanvasItem*> children_items;
116 
117  BlendMode blend_mode;
118  int light_mask;
119 
120  bool first_draw;
121  bool hidden;
122  bool pending_update;
123  bool toplevel;
124  bool pending_children_sort;
125  bool drawing;
126  bool block_transform_notify;
127  bool behind;
128  bool use_parent_material;
129  bool notify_local_transform;
130 
131  Ref<CanvasItemMaterial> material;
132 
133  mutable Matrix32 global_transform;
134  mutable bool global_invalid;
135 
136 
137  void _raise_self();
138 
139  void _propagate_visibility_changed(bool p_visible);
140 
141  void _set_visible_(bool p_visible);
142  bool _is_visible_() const;
143 
144  void _update_callback();
145 
146  void _enter_canvas();
147  void _exit_canvas();
148 
149  void _queue_sort_children();
150  void _sort_children();
151 
152  void _notify_transform(CanvasItem *p_node);
153 
154  void _set_on_top(bool p_on_top) { set_draw_behind_parent(!p_on_top); }
155  bool _is_on_top() const { return !is_draw_behind_parent_enabled(); }
156 
157 protected:
158 
159  _FORCE_INLINE_ void _notify_transform() { if (!is_inside_tree()) return; _notify_transform(this); if (!block_transform_notify && notify_local_transform) notification(NOTIFICATION_LOCAL_TRANSFORM_CHANGED); }
160 
161  void item_rect_changed();
162 
163  void _notification(int p_what);
164  static void _bind_methods();
165 public:
166 
167 
168  enum {
169  NOTIFICATION_TRANSFORM_CHANGED=SceneTree::NOTIFICATION_TRANSFORM_CHANGED, //unique
170  NOTIFICATION_DRAW=30,
171  NOTIFICATION_VISIBILITY_CHANGED=31,
172  NOTIFICATION_ENTER_CANVAS=32,
173  NOTIFICATION_EXIT_CANVAS=33,
174  NOTIFICATION_LOCAL_TRANSFORM_CHANGED=35,
175  NOTIFICATION_WORLD_2D_CHANGED=36,
176 
177  };
178 
179  /* EDITOR */
180 
181  virtual Variant edit_get_state() const;
182  virtual void edit_set_state(const Variant& p_state);
183  virtual void edit_set_rect(const Rect2& p_edit_rect);
184  virtual void edit_rotate(float p_rot);
185  virtual Size2 edit_get_minimum_size() const;
186 
187  /* VISIBILITY */
188 
189  bool is_visible() const;
190  bool is_hidden() const;
191  void show();
192  void hide();
193  void set_hidden(bool p_hidden);
194 
195  void update();
196 
197  void set_blend_mode(BlendMode p_blend_mode);
198  BlendMode get_blend_mode() const;
199 
200  virtual void set_light_mask(int p_light_mask);
201  int get_light_mask() const;
202 
203  void set_opacity(float p_opacity);
204  float get_opacity() const;
205 
206  void set_self_opacity(float p_self_opacity);
207  float get_self_opacity() const;
208 
209  /* DRAWING API */
210 
211  void draw_line(const Point2& p_from, const Point2& p_to,const Color& p_color,float p_width=1.0);
212  void draw_rect(const Rect2& p_rect, const Color& p_color);
213  void draw_circle(const Point2& p_pos, float p_radius, const Color& p_color);
214  void draw_texture(const Ref<Texture>& p_texture, const Point2& p_pos, const Color &p_modulate=Color(1,1,1,1));
215  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);
216  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);
217  void draw_style_box(const Ref<StyleBox>& p_style_box,const Rect2& p_rect);
218  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);
219  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>());
220  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>());
221 
222  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);
223  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));
224 
225  void draw_set_transform(const Point2& p_offset, float p_rot, const Size2& p_scale);
226 
227  /* RECT / TRANSFORM */
228 
229  void set_as_toplevel(bool p_toplevel);
230  bool is_set_as_toplevel() const;
231 
232  void set_draw_behind_parent(bool p_enable);
233  bool is_draw_behind_parent_enabled() const;
234 
235  CanvasItem *get_parent_item() const;
236 
237  virtual Rect2 get_item_rect() const=0;
238  virtual Matrix32 get_transform() const=0;
239 
240  virtual Matrix32 get_global_transform() const;
241  virtual Matrix32 get_global_transform_with_canvas() const;
242 
243  CanvasItem *get_toplevel() const;
244  _FORCE_INLINE_ RID get_canvas_item() const { return canvas_item; }
245 
246  void set_block_transform_notify(bool p_enable);
247  bool is_block_transform_notify_enabled() const;
248 
249 
250  Matrix32 get_canvas_transform() const;
251  Matrix32 get_viewport_transform() const;
252  Rect2 get_viewport_rect() const;
253  RID get_viewport_rid() const;
254  RID get_canvas() const;
255  Ref<World2D> get_world_2d() const;
256 
257  void set_material(const Ref<CanvasItemMaterial>& p_material);
258  Ref<CanvasItemMaterial> get_material() const;
259 
260  void set_use_parent_material(bool p_use_parent_material);
261  bool get_use_parent_material() const;
262 
263  InputEvent make_input_local(const InputEvent& pevent) const;
264 
265  Vector2 get_global_mouse_pos() const;
266  Vector2 get_local_mouse_pos() const;
267 
268  void set_notify_local_transform(bool p_enable);
269  bool is_local_transform_notification_enabled() const;
270 
271  int get_canvas_layer() const;
272 
273  CanvasItem();
274  ~CanvasItem();
275 };
276 
277 VARIANT_ENUM_CAST( CanvasItem::BlendMode );
278 
279 #endif // CANVAS_ITEM_H
Definition: viewport.h:75
Definition: variant.h:74
Definition: node.h:42
Definition: font.h:38
Definition: color.h:37
Definition: math_2d.h:204
Definition: string_db.h:48
Definition: canvas_item.h:87
Definition: input_event.h:263
Definition: resource.h:89
Definition: rid.h:47
Definition: canvas_layer.h:37
Definition: math_2d.h:65
Definition: canvas_item.h:43
Definition: math_2d.h:554
Definition: ustring.h:64
Definition: vector.h:43
Definition: style_box.h:38