viewport.h
1 /*************************************************************************/
2 /* viewport.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 VIEWPORT_H
30 #define VIEWPORT_H
31 
32 #include "scene/main/node.h"
33 #include "servers/visual_server.h"
34 #include "scene/resources/world_2d.h"
35 #include "math_2d.h"
36 #include "scene/resources/texture.h"
41 class Camera;
42 class Control;
43 class CanvasItem;
44 class Panel;
45 class Label;
46 class Timer;
47 class Viewport;
48 
49 class RenderTargetTexture : public Texture {
50 
51  OBJ_TYPE( RenderTargetTexture, Texture );
52 
53  int flags;
54 friend class Viewport;
55  Viewport *vp;
56 
57 
58 public:
59 
60 
61  virtual int get_width() const;
62  virtual int get_height() const;
63  virtual Size2 get_size() const;
64  virtual RID get_rid() const;
65 
66  virtual bool has_alpha() const;
67 
68  virtual void set_flags(uint32_t p_flags);
69  virtual uint32_t get_flags() const;
70 
71  RenderTargetTexture(Viewport *p_vp=NULL);
72 
73 };
74 
75 class Viewport : public Node {
76 
77  OBJ_TYPE( Viewport, Node );
78 public:
79 
80  enum RenderTargetUpdateMode {
81  RENDER_TARGET_UPDATE_DISABLED,
82  RENDER_TARGET_UPDATE_ONCE, //then goes to disabled
83  RENDER_TARGET_UPDATE_WHEN_VISIBLE, // default
84  RENDER_TARGET_UPDATE_ALWAYS
85  };
86 
87 private:
88 
89 friend class RenderTargetTexture;
90 
91 
92  Control *parent_control;
93  Viewport *parent;
94 
95  Camera *camera;
96  Set<Camera*> cameras;
97 
98  RID viewport;
99  RID canvas_item;
100  RID current_canvas;
101 
102  bool audio_listener;
103  RID listener;
104 
105  bool audio_listener_2d;
106  RID listener_2d;
107 
108  Matrix32 canvas_transform;
109  Matrix32 global_canvas_transform;
110  Matrix32 stretch_transform;
111 
112  Rect2 rect;
113  Rect2 to_screen_rect;
114 
115  RID contact_2d_debug;
116  RID contact_3d_debug_multimesh;
117  RID contact_3d_debug_instance;
118 
119 
120 
121  bool size_override;
122  bool size_override_stretch;
123  Size2 size_override_size;
124  Size2 size_override_margin;
125 
126  Rect2 last_vp_rect;
127 
128  bool transparent_bg;
129  bool render_target_vflip;
130  bool render_target_clear_on_new_frame;
131  bool render_target_filter;
132  bool render_target_gen_mipmaps;
133 
134  bool physics_object_picking;
135  List<InputEvent> physics_picking_events;
136  ObjectID physics_object_capture;
137  ObjectID physics_object_over;
138  Vector2 physics_last_mousepos;
139  void _test_new_mouseover(ObjectID new_collider);
140  Map<ObjectID,uint64_t> physics_2d_mouseover;
141 
142  void _update_rect();
143 
144  void _parent_resized();
145  void _parent_draw();
146  void _parent_visibility_changed();
147 
148  Ref<World2D> world_2d;
149  Ref<World> world;
150  Ref<World> own_world;
151 
152  StringName input_group;
153  StringName gui_input_group;
154  StringName unhandled_input_group;
155  StringName unhandled_key_input_group;
156 
157  void _update_listener();
158  void _update_listener_2d();
159 
160  void _propagate_enter_world(Node *p_node);
161  void _propagate_exit_world(Node *p_node);
162 
163 
164  void _update_stretch_transform();
165  void _update_global_transform();
166 
167  bool render_target;
168  RenderTargetUpdateMode render_target_update_mode;
169  RID render_target_texture_rid;
170  Ref<RenderTargetTexture> render_target_texture;
171 
172 
173  struct GUI {
174  // info used when this is a window
175 
176  bool key_event_accepted;
177  Control *mouse_focus;
178  int mouse_focus_button;
179  Control *key_focus;
180  Control *mouse_over;
181  Control *tooltip;
182  Panel *tooltip_popup;
183  Label *tooltip_label;
184  Point2 tooltip_pos;
185  Point2 last_mouse_pos;
186  Point2 drag_accum;
187  bool drag_attempted;
188  Variant drag_data;
189  Control *drag_preview;
190  float tooltip_timer;
191  float tooltip_delay;
192  List<Control*> modal_stack;
193  unsigned int cancelled_input_ID;
194  Matrix32 focus_inv_xform;
195  bool subwindow_order_dirty;
196  List<Control*> subwindows;
197  bool roots_order_dirty;
198  List<Control*> roots;
199 
200 
201  GUI();
202  } gui;
203 
204  bool disable_input;
205 
206  void _gui_call_input(Control *p_control,const InputEvent& p_input);
207  void _gui_sort_subwindows();
208  void _gui_sort_roots();
209  void _gui_sort_modal_stack();
210  Control* _gui_find_control(const Point2& p_global);
211  Control* _gui_find_control_at_pos(CanvasItem* p_node,const Point2& p_global,const Matrix32& p_xform,Matrix32& r_inv_xform);
212 
213  void _gui_input_event(InputEvent p_event);
214 
215 
216  void update_worlds();
217 
218  _FORCE_INLINE_ Matrix32 _get_input_pre_xform() const;
219 
220  void _vp_enter_tree();
221  void _vp_exit_tree();
222 
223  void _vp_input(const InputEvent& p_ev);
224  void _vp_input_text(const String& p_text);
225  void _vp_unhandled_input(const InputEvent& p_ev);
226  void _make_input_local(InputEvent& ev);
227 
228 
229 friend class Control;
230 
231  List<Control*>::Element* _gui_add_root_control(Control* p_control);
232  List<Control*>::Element* _gui_add_subwindow_control(Control* p_control);
233 
234  void _gui_set_subwindow_order_dirty();
235  void _gui_set_root_order_dirty();
236 
237 
238  void _gui_remove_modal_control(List<Control*>::Element *MI);
239  void _gui_remove_from_modal_stack(List<Control*>::Element *MI,ObjectID p_prev_focus_owner);
240  void _gui_remove_root_control(List<Control*>::Element *RI);
241  void _gui_remove_subwindow_control(List<Control*>::Element* SI);
242 
243  void _gui_cancel_tooltip();
244  void _gui_show_tooltip();
245 
246 
247  void _gui_remove_control(Control *p_control);
248  void _gui_hid_control(Control *p_control);
249 
250  void _gui_force_drag(Control *p_base,const Variant& p_data,Control *p_control);
251  void _gui_set_drag_preview(Control *p_base,Control *p_control);
252 
253  bool _gui_is_modal_on_top(const Control* p_control);
254  List<Control*>::Element* _gui_show_modal(Control* p_control);
255 
256  void _gui_remove_focus();
257  void _gui_unfocus_control(Control *p_control);
258  bool _gui_control_has_focus(const Control* p_control);
259  void _gui_control_grab_focus(Control* p_control);
260  void _gui_grab_click_focus(Control *p_control);
261  void _gui_accept_event();
262 
263  Control *_gui_get_focus_owner();
264 
265 friend class Camera;
266  void _camera_transform_changed_notify();
267  void _camera_set(Camera* p_camera);
268  bool _camera_add(Camera* p_camera); //true if first
269  void _camera_remove(Camera* p_camera);
270  void _camera_make_next_current(Camera* p_exclude);
271 
272 
273 protected:
274  void _notification(int p_what);
275  static void _bind_methods();
276 public:
277 
278 
279  Camera* get_camera() const;
280 
281  void set_as_audio_listener(bool p_enable);
282  bool is_audio_listener() const;
283 
284  void set_as_audio_listener_2d(bool p_enable);
285  bool is_audio_listener_2d() const;
286 
287  void set_rect(const Rect2& p_rect);
288  Rect2 get_rect() const;
289  Rect2 get_visible_rect() const;
290  RID get_viewport() const;
291 
292  void set_world(const Ref<World>& p_world);
293  Ref<World> get_world() const;
294  Ref<World> find_world() const;
295 
296  Ref<World2D> find_world_2d() const;
297 
298 
299  void set_canvas_transform(const Matrix32& p_transform);
300  Matrix32 get_canvas_transform() const;
301 
302  void set_global_canvas_transform(const Matrix32& p_transform);
303  Matrix32 get_global_canvas_transform() const;
304 
305  Matrix32 get_final_transform() const;
306 
307  void set_transparent_background(bool p_enable);
308  bool has_transparent_background() const;
309 
310 
311  void set_size_override(bool p_enable,const Size2& p_size=Size2(-1,-1),const Vector2& p_margin=Vector2());
312  Size2 get_size_override() const;
313  bool is_size_override_enabled() const;
314  void set_size_override_stretch(bool p_enable);
315  bool is_size_override_stretch_enabled() const;
316 
317  void set_as_render_target(bool p_enable);
318  bool is_set_as_render_target() const;
319 
320  void set_render_target_vflip(bool p_enable);
321  bool get_render_target_vflip() const;
322 
323  void set_render_target_clear_on_new_frame(bool p_enable);
324  bool get_render_target_clear_on_new_frame() const;
325  void render_target_clear();
326 
327  void set_render_target_filter(bool p_enable);
328  bool get_render_target_filter() const;
329 
330  void set_render_target_gen_mipmaps(bool p_enable);
331  bool get_render_target_gen_mipmaps() const;
332 
333  void set_render_target_update_mode(RenderTargetUpdateMode p_mode);
334  RenderTargetUpdateMode get_render_target_update_mode() const;
335  Ref<RenderTargetTexture> get_render_target_texture() const;
336 
337 
338  Vector2 get_camera_coords(const Vector2& p_viewport_coords) const;
339  Vector2 get_camera_rect_size() const;
340 
341  void queue_screen_capture();
342  Image get_screen_capture() const;
343 
344  void set_use_own_world(bool p_world);
345  bool is_using_own_world() const;
346 
347  void input(const InputEvent& p_event);
348  void unhandled_input(const InputEvent& p_event);
349 
350  void set_disable_input(bool p_disable);
351  bool is_input_disabled() const;
352 
353  void set_render_target_to_screen_rect(const Rect2& p_rect);
354  Rect2 get_render_target_to_screen_rect() const;
355 
356  Vector2 get_mouse_pos() const;
357  void warp_mouse(const Vector2& p_pos);
358 
359  void set_physics_object_picking(bool p_enable);
360  bool get_physics_object_picking();
361 
362  bool gui_has_modal_stack() const;
363 
364 
365  Viewport();
366  ~Viewport();
367 
368 };
369 
370 VARIANT_ENUM_CAST(Viewport::RenderTargetUpdateMode);
371 #endif
Definition: viewport.h:75
Definition: timer.h:34
Definition: variant.h:74
Definition: node.h:42
Definition: math_2d.h:204
Definition: image.h:47
Definition: camera.h:39
Definition: string_db.h:48
Definition: canvas_item.h:87
Definition: input_event.h:263
Definition: panel.h:36
Definition: rid.h:47
Definition: viewport.h:49
Definition: math_2d.h:65
Definition: texture.h:42
Definition: math_2d.h:554
Definition: control.h:47
Definition: ustring.h:64
Definition: label.h:36