From c2aaeaaf5afb612cde195075575a955fa0650f4f Mon Sep 17 00:00:00 2001 From: MarianoGNU Date: Mon, 18 Jan 2016 20:32:37 -0300 Subject: [PATCH 1/2] Add zoom to GraphEdit Also fix some responsiveness issues: -Box selection no longer visible when connecting. -Allow cancel connection with mouse's right button. --- scene/gui/graph_edit.cpp | 69 +++++++++++++++++++++++++++++++++++----- scene/gui/graph_edit.h | 5 +++ scene/gui/graph_node.cpp | 11 +++++-- 3 files changed, 74 insertions(+), 11 deletions(-) diff --git a/scene/gui/graph_edit.cpp b/scene/gui/graph_edit.cpp index 85f00f9d84c..884d24c8130 100644 --- a/scene/gui/graph_edit.cpp +++ b/scene/gui/graph_edit.cpp @@ -151,6 +151,7 @@ void GraphEdit::add_child_notify(Node *p_child) { top_layer->call_deferred("raise"); //top layer always on top! GraphNode *gn = p_child->cast_to(); if (gn) { + gn->set_scale(Vector2(zoom,zoom)); gn->connect("offset_changed",this,"_graph_node_moved",varray(gn)); gn->connect("raise_request",this,"_graph_node_raised",varray(gn)); _graph_node_moved(gn); @@ -384,8 +385,6 @@ void GraphEdit::_top_layer_input(const InputEvent& p_ev) { } - - } void GraphEdit::_draw_cos_line(const Vector2& p_from, const Vector2& p_to,const Color& p_color) { @@ -525,7 +524,9 @@ void GraphEdit::_input_event(const InputEvent& p_ev) { if (!gn) continue; - bool in_box = gn->get_rect().intersects(box_selecting_rect); + Rect2 r = gn->get_rect(); + r.size*=zoom; + bool in_box = r.intersects(box_selecting_rect); if (in_box) gn->set_selected(box_selection_mode_aditive); @@ -554,7 +555,12 @@ void GraphEdit::_input_event(const InputEvent& p_ev) { } top_layer->update(); } else { - emit_signal("popup_request", Vector2(b.global_x, b.global_y)); + if (connecting) { + connecting = false; + top_layer->update(); + } else { + emit_signal("popup_request", Vector2(b.global_x, b.global_y)); + } } } @@ -564,8 +570,12 @@ void GraphEdit::_input_event(const InputEvent& p_ev) { for(int i=get_child_count()-1;i>=0;i--) { GraphNode *gn=get_child(i)->cast_to(); - if (gn && gn->get_rect().has_point(get_local_mouse_pos())) - gn->set_selected(false); + if (gn) { + Rect2 r = gn->get_rect(); + r.size*=zoom; + if (r.has_point(get_local_mouse_pos())) + gn->set_selected(false); + } } } @@ -594,8 +604,12 @@ void GraphEdit::_input_event(const InputEvent& p_ev) { gn=get_child(i)->cast_to(); - if (gn && gn->get_rect().has_point(get_local_mouse_pos())) - break; + if (gn) { + Rect2 r = gn->get_rect(); + r.size*=zoom; + if (r.has_point(get_local_mouse_pos())) + break; + } } if (gn) { @@ -624,6 +638,9 @@ void GraphEdit::_input_event(const InputEvent& p_ev) { } } else { + if (_filter_input(Vector2(b.x,b.y))) + return; + box_selecting = true; box_selecting_from = get_local_mouse_pos(); if (b.mod.control) { @@ -668,6 +685,14 @@ void GraphEdit::_input_event(const InputEvent& p_ev) { previus_selected.clear(); top_layer->update(); } + + if (b.button_index==BUTTON_WHEEL_UP && b.pressed) { + set_zoom(zoom/0.9); + } + + if (b.button_index==BUTTON_WHEEL_DOWN && b.pressed) { + set_zoom(zoom*0.9); + } } if (p_ev.type==InputEvent::KEY && p_ev.key.scancode==KEY_D && p_ev.key.pressed && p_ev.key.mod.command) { @@ -688,6 +713,29 @@ void GraphEdit::clear_connections() { update(); } +void GraphEdit::set_zoom(float p_zoom) { + + if (p_zoom<0.01) p_zoom=0.01; + if (p_zoom>4) p_zoom=4; + if (zoom == p_zoom) + return; + + float prev_zoom = zoom; + zoom = p_zoom; + for (int i = 0; i < get_child_count(); i++) { + GraphNode *child = get_child(i)->cast_to(); + if (!child) + continue; + Point2 ofs = child->get_offset() / prev_zoom * zoom; + child->set_scale(Vector2(zoom,zoom)); + child->set_offset(ofs); + } + update(); +} + +float GraphEdit::get_zoom() const { + return zoom; +} void GraphEdit::set_right_disconnects(bool p_enable) { @@ -722,6 +770,9 @@ void GraphEdit::_bind_methods() { ObjectTypeDB::bind_method(_MD("get_connection_list"),&GraphEdit::_get_connection_list); ObjectTypeDB::bind_method(_MD("get_scroll_ofs"),&GraphEdit::get_scroll_ofs); + ObjectTypeDB::bind_method(_MD("set_zoom","p_zoom"),&GraphEdit::set_zoom); + ObjectTypeDB::bind_method(_MD("get_zoom"),&GraphEdit::get_zoom); + ObjectTypeDB::bind_method(_MD("set_right_disconnects","enable"),&GraphEdit::set_right_disconnects); ObjectTypeDB::bind_method(_MD("is_right_disconnects_enabled"),&GraphEdit::is_right_disconnects_enabled); @@ -773,4 +824,6 @@ GraphEdit::GraphEdit() { h_scroll->connect("value_changed", this,"_scroll_moved"); v_scroll->connect("value_changed", this,"_scroll_moved"); + + zoom = 1; } diff --git a/scene/gui/graph_edit.h b/scene/gui/graph_edit.h index 5e339e4e7ea..b63597afbac 100644 --- a/scene/gui/graph_edit.h +++ b/scene/gui/graph_edit.h @@ -53,6 +53,8 @@ private: bool just_selected; Vector2 drag_accum; + float zoom; + bool box_selecting; bool box_selection_mode_aditive; Point2 box_selecting_from; @@ -96,6 +98,9 @@ public: void disconnect_node(const StringName& p_from, int p_from_port,const StringName& p_to,int p_to_port); void clear_connections(); + void set_zoom(float p_zoom); + float get_zoom() const; + GraphEditFilter *get_top_layer() const { return top_layer; } void get_connection_list(List *r_connections) const; diff --git a/scene/gui/graph_node.cpp b/scene/gui/graph_node.cpp index 5efc9757b75..762afb158a7 100644 --- a/scene/gui/graph_node.cpp +++ b/scene/gui/graph_node.cpp @@ -474,7 +474,10 @@ Vector2 GraphNode::get_connection_input_pos(int p_idx) { _connpos_update(); ERR_FAIL_INDEX_V(p_idx,conn_input_cache.size(),Vector2()); - return conn_input_cache[p_idx].pos; + Vector2 pos = conn_input_cache[p_idx].pos; + pos.x *= get_scale().x; + pos.y *= get_scale().y; + return pos; } int GraphNode::get_connection_input_type(int p_idx) { @@ -501,8 +504,10 @@ Vector2 GraphNode::get_connection_output_pos(int p_idx){ _connpos_update(); ERR_FAIL_INDEX_V(p_idx,conn_output_cache.size(),Vector2()); - return conn_output_cache[p_idx].pos; - + Vector2 pos = conn_output_cache[p_idx].pos; + pos.x *= get_scale().x; + pos.y *= get_scale().y; + return pos; } int GraphNode::get_connection_output_type(int p_idx) { From 76ee2035b6726dbe9f2dc356e71069ff7b6c9d78 Mon Sep 17 00:00:00 2001 From: MarianoGNU Date: Mon, 18 Jan 2016 22:10:44 -0300 Subject: [PATCH 2/2] Add a slider to handle zoom --- scene/gui/graph_edit.cpp | 22 ++++++++++++++++++++-- scene/gui/graph_edit.h | 5 ++++- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/scene/gui/graph_edit.cpp b/scene/gui/graph_edit.cpp index 884d24c8130..d0c82c15b8b 100644 --- a/scene/gui/graph_edit.cpp +++ b/scene/gui/graph_edit.cpp @@ -1,6 +1,7 @@ #include "graph_edit.h" #include "os/input.h" #include "os/keyboard.h" +#include "scene/gui/box_container.h" bool GraphEditFilter::has_point(const Point2& p_point) const { return ge->_filter_input(p_point); @@ -185,6 +186,8 @@ void GraphEdit::_notification(int p_what) { h_scroll->set_anchor_and_margin(MARGIN_TOP,ANCHOR_END,hmin.height); h_scroll->set_anchor_and_margin(MARGIN_BOTTOM,ANCHOR_END,0); + zoom_icon->set_texture( get_icon("Zoom", "EditorIcons")); + } if (p_what==NOTIFICATION_DRAW) { VS::get_singleton()->canvas_item_set_clip(get_canvas_item(),true); @@ -687,11 +690,11 @@ void GraphEdit::_input_event(const InputEvent& p_ev) { } if (b.button_index==BUTTON_WHEEL_UP && b.pressed) { - set_zoom(zoom/0.9); + sl_zoom->set_val(zoom/0.9); } if (b.button_index==BUTTON_WHEEL_DOWN && b.pressed) { - set_zoom(zoom*0.9); + sl_zoom->set_val(zoom*0.9); } } @@ -826,4 +829,19 @@ GraphEdit::GraphEdit() { v_scroll->connect("value_changed", this,"_scroll_moved"); zoom = 1; + + HBoxContainer* tools = memnew( HBoxContainer ); + add_child(tools); + + zoom_icon = memnew( TextureFrame ); + tools->add_child(zoom_icon); + + sl_zoom = memnew( HSlider ); + sl_zoom->set_min(0.01); + sl_zoom->set_max(4); + sl_zoom->set_val(1); + sl_zoom->set_step(0.01); + sl_zoom->connect("value_changed", this, "set_zoom"); + tools->add_child(sl_zoom); + sl_zoom->set_custom_minimum_size(Size2(200,0)); } diff --git a/scene/gui/graph_edit.h b/scene/gui/graph_edit.h index b63597afbac..ec278827b15 100644 --- a/scene/gui/graph_edit.h +++ b/scene/gui/graph_edit.h @@ -3,7 +3,8 @@ #include "scene/gui/graph_node.h" #include "scene/gui/scroll_bar.h" - +#include "scene/gui/slider.h" +#include "texture_frame.h" class GraphEdit; class GraphEditFilter : public Control { @@ -34,6 +35,8 @@ public: }; private: + TextureFrame* zoom_icon; + HSlider* sl_zoom; HScrollBar* h_scroll; VScrollBar* v_scroll;