diff options
Diffstat (limited to 'scene/main/viewport.cpp')
-rw-r--r-- | scene/main/viewport.cpp | 197 |
1 files changed, 158 insertions, 39 deletions
diff --git a/scene/main/viewport.cpp b/scene/main/viewport.cpp index de553dd39e..b91a4b810f 100644 --- a/scene/main/viewport.cpp +++ b/scene/main/viewport.cpp @@ -65,13 +65,11 @@ void ViewportTexture::setup_local_to_scene() { } Node *vpn = local_scene->get_node(path); - ERR_EXPLAIN("ViewportTexture: Path to node is invalid"); - ERR_FAIL_COND(!vpn); + ERR_FAIL_COND_MSG(!vpn, "ViewportTexture: Path to node is invalid."); vp = Object::cast_to<Viewport>(vpn); - ERR_EXPLAIN("ViewportTexture: Path to node does not point to a viewport"); - ERR_FAIL_COND(!vp); + ERR_FAIL_COND_MSG(!vp, "ViewportTexture: Path to node does not point to a viewport."); vp->viewport_textures.insert(this); @@ -100,22 +98,22 @@ NodePath ViewportTexture::get_viewport_path_in_scene() const { int ViewportTexture::get_width() const { - ERR_FAIL_COND_V(!vp, 0); + ERR_FAIL_COND_V_MSG(!vp, 0, "Viewport Texture must be set to use it."); return vp->size.width; } int ViewportTexture::get_height() const { - ERR_FAIL_COND_V(!vp, 0); + ERR_FAIL_COND_V_MSG(!vp, 0, "Viewport Texture must be set to use it."); return vp->size.height; } Size2 ViewportTexture::get_size() const { - ERR_FAIL_COND_V(!vp, Size2()); + ERR_FAIL_COND_V_MSG(!vp, Size2(), "Viewport Texture must be set to use it."); return vp->size; } RID ViewportTexture::get_rid() const { - //ERR_FAIL_COND_V(!vp, RID()); + //ERR_FAIL_COND_V_MSG(!vp, RID(), "Viewport Texture must be set to use it."); return proxy; } @@ -125,7 +123,7 @@ bool ViewportTexture::has_alpha() const { } Ref<Image> ViewportTexture::get_data() const { - ERR_FAIL_COND_V(!vp, Ref<Image>()); + ERR_FAIL_COND_V_MSG(!vp, Ref<Image>(), "Viewport Texture must be set to use it."); return VS::get_singleton()->texture_get_data(vp->texture_rid); } void ViewportTexture::set_flags(uint32_t p_flags) { @@ -172,14 +170,16 @@ ViewportTexture::~ViewportTexture() { class TooltipPanel : public PanelContainer { - GDCLASS(TooltipPanel, PanelContainer) + GDCLASS(TooltipPanel, PanelContainer); + public: TooltipPanel(){}; }; class TooltipLabel : public Label { - GDCLASS(TooltipLabel, Label) + GDCLASS(TooltipLabel, Label); + public: TooltipLabel(){}; }; @@ -530,7 +530,7 @@ void Viewport::_notification(int p_what) { Map<ObjectID, uint64_t>::Element *F = physics_2d_mouseover.find(res[i].collider_id); if (!F) { - F = physics_2d_mouseover.insert(res[i].collider_id, frame); + physics_2d_mouseover.insert(res[i].collider_id, frame); co->_mouse_enter(); } else { F->get() = frame; @@ -579,7 +579,7 @@ void Viewport::_notification(int p_what) { if (physics_object_capture != 0) { CollisionObject *co = Object::cast_to<CollisionObject>(ObjectDB::get_instance(physics_object_capture)); - if (co) { + if (co && camera) { _collision_object_input_event(co, camera, ev, Vector3(), Vector3(), 0); captured = true; if (mb.is_valid() && mb->get_button_index() == 1 && !mb->is_pressed()) { @@ -779,10 +779,45 @@ bool Viewport::is_audio_listener_2d() const { return audio_listener_2d; } +void Viewport::enable_canvas_transform_override(bool p_enable) { + if (override_canvas_transform == p_enable) { + return; + } + + override_canvas_transform = p_enable; + if (p_enable) { + VisualServer::get_singleton()->viewport_set_canvas_transform(viewport, find_world_2d()->get_canvas(), canvas_transform_override); + } else { + VisualServer::get_singleton()->viewport_set_canvas_transform(viewport, find_world_2d()->get_canvas(), canvas_transform); + } +} + +bool Viewport::is_canvas_transform_override_enbled() const { + return override_canvas_transform; +} + +void Viewport::set_canvas_transform_override(const Transform2D &p_transform) { + if (canvas_transform_override == p_transform) { + return; + } + + canvas_transform_override = p_transform; + if (override_canvas_transform) { + VisualServer::get_singleton()->viewport_set_canvas_transform(viewport, find_world_2d()->get_canvas(), canvas_transform_override); + } +} + +Transform2D Viewport::get_canvas_transform_override() const { + return canvas_transform_override; +} + void Viewport::set_canvas_transform(const Transform2D &p_transform) { canvas_transform = p_transform; - VisualServer::get_singleton()->viewport_set_canvas_transform(viewport, find_world_2d()->get_canvas(), canvas_transform); + + if (!override_canvas_transform) { + VisualServer::get_singleton()->viewport_set_canvas_transform(viewport, find_world_2d()->get_canvas(), canvas_transform); + } } Transform2D Viewport::get_canvas_transform() const { @@ -890,10 +925,12 @@ void Viewport::_camera_set(Camera *p_camera) { camera->notification(Camera::NOTIFICATION_LOST_CURRENT); } camera = p_camera; - if (camera) - VisualServer::get_singleton()->viewport_attach_camera(viewport, camera->get_camera()); - else - VisualServer::get_singleton()->viewport_attach_camera(viewport, RID()); + if (!camera_override) { + if (camera) + VisualServer::get_singleton()->viewport_attach_camera(viewport, camera->get_camera()); + else + VisualServer::get_singleton()->viewport_attach_camera(viewport, RID()); + } if (camera) { camera->notification(Camera::NOTIFICATION_BECAME_CURRENT); @@ -1108,10 +1145,82 @@ Listener *Viewport::get_listener() const { } Camera *Viewport::get_camera() const { - return camera; } +void Viewport::enable_camera_override(bool p_enable) { + +#ifndef _3D_DISABLED + if (p_enable == camera_override) { + return; + } + + if (p_enable) { + camera_override.rid = VisualServer::get_singleton()->camera_create(); + } else { + VisualServer::get_singleton()->free(camera_override.rid); + camera_override.rid = RID(); + } + + if (p_enable) { + VisualServer::get_singleton()->viewport_attach_camera(viewport, camera_override.rid); + } else if (camera) { + VisualServer::get_singleton()->viewport_attach_camera(viewport, camera->get_camera()); + } else { + VisualServer::get_singleton()->viewport_attach_camera(viewport, RID()); + } +#endif +} + +bool Viewport::is_camera_override_enabled() const { + return camera_override; +} + +void Viewport::set_camera_override_transform(const Transform &p_transform) { + if (camera_override) { + camera_override.transform = p_transform; + VisualServer::get_singleton()->camera_set_transform(camera_override.rid, p_transform); + } +} + +Transform Viewport::get_camera_override_transform() const { + if (camera_override) { + return camera_override.transform; + } + + return Transform(); +} + +void Viewport::set_camera_override_perspective(float p_fovy_degrees, float p_z_near, float p_z_far) { + if (camera_override) { + if (camera_override.fov == p_fovy_degrees && camera_override.z_near == p_z_near && + camera_override.z_far == p_z_far && camera_override.projection == CameraOverrideData::PROJECTION_PERSPECTIVE) + return; + + camera_override.fov = p_fovy_degrees; + camera_override.z_near = p_z_near; + camera_override.z_far = p_z_far; + camera_override.projection = CameraOverrideData::PROJECTION_PERSPECTIVE; + + VisualServer::get_singleton()->camera_set_perspective(camera_override.rid, camera_override.fov, camera_override.z_near, camera_override.z_far); + } +} + +void Viewport::set_camera_override_orthogonal(float p_size, float p_z_near, float p_z_far) { + if (camera_override) { + if (camera_override.size == p_size && camera_override.z_near == p_z_near && + camera_override.z_far == p_z_far && camera_override.projection == CameraOverrideData::PROJECTION_ORTHOGONAL) + return; + + camera_override.size = p_size; + camera_override.z_near = p_z_near; + camera_override.z_far = p_z_far; + camera_override.projection = CameraOverrideData::PROJECTION_ORTHOGONAL; + + VisualServer::get_singleton()->camera_set_orthogonal(camera_override.rid, camera_override.size, camera_override.z_near, camera_override.z_far); + } +} + Transform2D Viewport::get_final_transform() const { return stretch_transform * global_canvas_transform; @@ -1429,6 +1538,7 @@ void Viewport::_gui_show_tooltip() { Control *which = NULL; String tooltip = _gui_get_tooltip(gui.tooltip, gui.tooltip->get_global_transform().xform_inv(gui.tooltip_pos), &which); + tooltip = tooltip.strip_edges(); if (tooltip.length() == 0) return; // bye @@ -1442,9 +1552,7 @@ void Viewport::_gui_show_tooltip() { return; } - Control *rp = which; //->get_root_parent_control(); - if (!rp) - return; + Control *rp = which; gui.tooltip_popup = which->make_custom_tooltip(tooltip); @@ -1460,24 +1568,25 @@ void Viewport::_gui_show_tooltip() { gui.tooltip_label->set_anchor_and_margin(MARGIN_TOP, Control::ANCHOR_BEGIN, ttp->get_margin(MARGIN_TOP)); gui.tooltip_label->set_anchor_and_margin(MARGIN_RIGHT, Control::ANCHOR_END, -ttp->get_margin(MARGIN_RIGHT)); gui.tooltip_label->set_anchor_and_margin(MARGIN_BOTTOM, Control::ANCHOR_END, -ttp->get_margin(MARGIN_BOTTOM)); - gui.tooltip_label->set_text(tooltip.strip_edges()); + gui.tooltip_label->set_text(tooltip); } rp->add_child(gui.tooltip_popup); gui.tooltip_popup->force_parent_owned(); gui.tooltip_popup->set_as_toplevel(true); - //gui.tooltip_popup->hide(); + if (gui.tooltip) // Avoids crash when rapidly switching controls. + gui.tooltip_popup->set_scale(gui.tooltip->get_global_transform().get_scale()); Point2 tooltip_offset = ProjectSettings::get_singleton()->get("display/mouse_cursor/tooltip_position_offset"); Rect2 r(gui.tooltip_pos + tooltip_offset, gui.tooltip_popup->get_minimum_size()); Rect2 vr = gui.tooltip_popup->get_viewport_rect(); - if (r.size.x + r.position.x > vr.size.x) - r.position.x = vr.size.x - r.size.x; + if (r.size.x * gui.tooltip_popup->get_scale().x + r.position.x > vr.size.x) + r.position.x = vr.size.x - r.size.x * gui.tooltip_popup->get_scale().x; else if (r.position.x < 0) r.position.x = 0; - if (r.size.y + r.position.y > vr.size.y) - r.position.y = vr.size.y - r.size.y; + if (r.size.y * gui.tooltip_popup->get_scale().y + r.position.y > vr.size.y) + r.position.y = vr.size.y - r.size.y * gui.tooltip_popup->get_scale().y; else if (r.position.y < 0) r.position.y = 0; @@ -1701,6 +1810,8 @@ bool Viewport::_gui_drop(Control *p_at_control, Point2 p_at_pos, bool p_just_che void Viewport::_gui_input_event(Ref<InputEvent> p_event) { + ERR_FAIL_COND(p_event.is_null()) + //? /* if (!is_visible()) { @@ -1741,6 +1852,12 @@ void Viewport::_gui_input_event(Ref<InputEvent> p_event) { return; // no one gets the event if exclusive NO ONE } + if (mb->get_button_index() == BUTTON_WHEEL_UP || mb->get_button_index() == BUTTON_WHEEL_DOWN || mb->get_button_index() == BUTTON_WHEEL_LEFT || mb->get_button_index() == BUTTON_WHEEL_RIGHT) { + //cancel scroll wheel events, only clicks should trigger focus changes. + set_input_as_handled(); + return; + } + top->notification(Control::NOTIFICATION_MODAL_CLOSE); top->_modal_stack_remove(); top->hide(); @@ -2288,32 +2405,34 @@ void Viewport::_gui_input_event(Ref<InputEvent> p_event) { if (from && p_event->is_pressed()) { Control *next = NULL; - if (p_event->is_action_pressed("ui_focus_next")) { + Input *input = Input::get_singleton(); + + if (!mods && p_event->is_action_pressed("ui_focus_next") && input->is_action_just_pressed("ui_focus_next")) { next = from->find_next_valid_focus(); } - if (p_event->is_action_pressed("ui_focus_prev")) { + if (!mods && p_event->is_action_pressed("ui_focus_prev") && input->is_action_just_pressed("ui_focus_prev")) { next = from->find_prev_valid_focus(); } - if (!mods && p_event->is_action_pressed("ui_up")) { + if (!mods && p_event->is_action_pressed("ui_up") && input->is_action_just_pressed("ui_up")) { next = from->_get_focus_neighbour(MARGIN_TOP); } - if (!mods && p_event->is_action_pressed("ui_left")) { + if (!mods && p_event->is_action_pressed("ui_left") && input->is_action_just_pressed("ui_left")) { next = from->_get_focus_neighbour(MARGIN_LEFT); } - if (!mods && p_event->is_action_pressed("ui_right")) { + if (!mods && p_event->is_action_pressed("ui_right") && input->is_action_just_pressed("ui_right")) { next = from->_get_focus_neighbour(MARGIN_RIGHT); } - if (!mods && p_event->is_action_pressed("ui_down")) { + if (!mods && p_event->is_action_pressed("ui_down") && input->is_action_just_pressed("ui_down")) { next = from->_get_focus_neighbour(MARGIN_BOTTOM); } @@ -2364,7 +2483,6 @@ void Viewport::_gui_remove_from_modal_stack(List<Control *>::Element *MI, Object List<Control *>::Element *next = MI->next(); gui.modal_stack.erase(MI); - MI = NULL; if (p_prev_focus_owner) { @@ -2390,8 +2508,7 @@ void Viewport::_gui_remove_from_modal_stack(List<Control *>::Element *MI, Object void Viewport::_gui_force_drag(Control *p_base, const Variant &p_data, Control *p_control) { - ERR_EXPLAIN("Drag data must be a value"); - ERR_FAIL_COND(p_data.get_type() == Variant::NIL); + ERR_FAIL_COND_MSG(p_data.get_type() == Variant::NIL, "Drag data must be a value."); gui.dragging = true; gui.drag_data = p_data; @@ -2576,7 +2693,7 @@ void Viewport::_drop_physics_mouseover() { List<Control *>::Element *Viewport::_gui_show_modal(Control *p_control) { - gui.modal_stack.push_back(p_control); + List<Control *>::Element *node = gui.modal_stack.push_back(p_control); if (gui.key_focus) p_control->_modal_set_prev_focus_owner(gui.key_focus->get_instance_id()); else @@ -2587,7 +2704,7 @@ List<Control *>::Element *Viewport::_gui_show_modal(Control *p_control) { _drop_mouse_focus(); } - return gui.modal_stack.back(); + return node; } Control *Viewport::_gui_get_focus_owner() { @@ -3063,6 +3180,7 @@ void Viewport::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::BOOL, "arvr"), "set_use_arvr", "use_arvr"); ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "size"), "set_size", "get_size"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "size_override_stretch"), "set_size_override_stretch", "is_size_override_stretch_enabled"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "own_world"), "set_use_own_world", "is_using_own_world"); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "world", PROPERTY_HINT_RESOURCE_TYPE, "World"), "set_world", "get_world"); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "world_2d", PROPERTY_HINT_RESOURCE_TYPE, "World2D", 0), "set_world_2d", "get_world_2d"); @@ -3172,6 +3290,7 @@ Viewport::Viewport() { parent = NULL; listener = NULL; camera = NULL; + override_canvas_transform = false; canvas_layers.insert(NULL); // This eases picking code (interpreted as the canvas of the Viewport) arvr = false; size_override = false; |