summaryrefslogtreecommitdiff
path: root/scene/gui
diff options
context:
space:
mode:
Diffstat (limited to 'scene/gui')
-rw-r--r--scene/gui/graph_edit.cpp517
-rw-r--r--scene/gui/graph_edit.h93
-rw-r--r--scene/gui/graph_node.cpp272
-rw-r--r--scene/gui/graph_node.h46
-rw-r--r--scene/gui/menu_button.cpp1
-rw-r--r--scene/gui/popup_menu.cpp19
-rw-r--r--scene/gui/popup_menu.h4
7 files changed, 932 insertions, 20 deletions
diff --git a/scene/gui/graph_edit.cpp b/scene/gui/graph_edit.cpp
new file mode 100644
index 0000000000..957e63e3ce
--- /dev/null
+++ b/scene/gui/graph_edit.cpp
@@ -0,0 +1,517 @@
+#include "graph_edit.h"
+
+bool GraphEditFilter::has_point(const Point2& p_point) const {
+
+ return ge->_filter_input(p_point);
+}
+
+
+GraphEditFilter::GraphEditFilter(GraphEdit *p_edit) {
+
+ ge=p_edit;
+}
+
+
+Error GraphEdit::connect_node(const StringName& p_from, int p_from_port,const StringName& p_to,int p_to_port) {
+
+ if (is_node_connected(p_from,p_from_port,p_to,p_to_port))
+ return OK;
+ Connection c;
+ c.from=p_from;
+ c.from_port=p_from_port;
+ c.to=p_to;
+ c.to_port=p_to_port;
+ connections.push_back(c);
+ top_layer->update();
+
+ return OK;
+}
+
+bool GraphEdit::is_node_connected(const StringName& p_from, int p_from_port,const StringName& p_to,int p_to_port) {
+
+ for(List<Connection>::Element *E=connections.front();E;E=E->next()) {
+
+ if (E->get().from==p_from && E->get().from_port==p_from_port && E->get().to==p_to && E->get().to_port==p_to_port)
+ return true;
+ }
+
+ return false;
+
+}
+
+void GraphEdit::disconnect_node(const StringName& p_from, int p_from_port,const StringName& p_to,int p_to_port){
+
+
+ for(List<Connection>::Element *E=connections.front();E;E=E->next()) {
+
+ if (E->get().from==p_from && E->get().from_port==p_from_port && E->get().to==p_to && E->get().to_port==p_to_port) {
+
+ connections.erase(E);
+ top_layer->update();
+ return;
+ }
+ }
+}
+
+void GraphEdit::get_connection_list(List<Connection> *r_connections) {
+
+ *r_connections=connections;
+}
+
+
+void GraphEdit::_scroll_moved(double) {
+
+
+ _update_scroll_offset();
+ top_layer->update();
+}
+
+void GraphEdit::_update_scroll_offset() {
+
+ for(int i=0;i<get_child_count();i++) {
+
+ GraphNode *gn=get_child(i)->cast_to<GraphNode>();
+ if (!gn)
+ continue;
+
+ Point2 pos=gn->get_offset();
+ pos-=Point2(h_scroll->get_val(),v_scroll->get_val());
+ gn->set_pos(pos);
+ }
+
+}
+
+void GraphEdit::_update_scroll() {
+
+ if (updating)
+ return;
+
+ updating=true;
+ Rect2 screen;
+ screen.size=get_size();
+ for(int i=0;i<get_child_count();i++) {
+
+ GraphNode *gn=get_child(i)->cast_to<GraphNode>();
+ if (!gn)
+ continue;
+
+ Rect2 r;
+ r.pos=gn->get_offset();
+ r.size=gn->get_size();
+ screen = screen.merge(r);
+ }
+
+ h_scroll->set_min(screen.pos.x);
+ h_scroll->set_max(screen.pos.x+screen.size.x);
+ h_scroll->set_page(get_size().x);
+ if (h_scroll->get_max() - h_scroll->get_min() <= h_scroll->get_page())
+ h_scroll->hide();
+ else
+ h_scroll->show();
+
+ v_scroll->set_min(screen.pos.y);
+ v_scroll->set_max(screen.pos.y+screen.size.y);
+ v_scroll->set_page(get_size().y);
+
+ if (v_scroll->get_max() - v_scroll->get_min() <= v_scroll->get_page())
+ v_scroll->hide();
+ else
+ v_scroll->show();
+
+ _update_scroll_offset();
+ updating=false;
+}
+
+
+void GraphEdit::_graph_node_raised(Node* p_gn) {
+
+ GraphNode *gn=p_gn->cast_to<GraphNode>();
+ ERR_FAIL_COND(!gn);
+ gn->raise();
+ top_layer->raise();
+
+}
+
+
+void GraphEdit::_graph_node_moved(Node *p_gn) {
+
+ GraphNode *gn=p_gn->cast_to<GraphNode>();
+ ERR_FAIL_COND(!gn);
+
+ //gn->set_pos(gn->get_offset()+scroll_offset);
+
+ top_layer->update();
+}
+
+void GraphEdit::add_child_notify(Node *p_child) {
+
+ top_layer->call_deferred("raise"); //top layer always on top!
+ GraphNode *gn = p_child->cast_to<GraphNode>();
+ if (gn) {
+ gn->connect("offset_changed",this,"_graph_node_moved",varray(gn));
+ gn->connect("raise_request",this,"_graph_node_raised",varray(gn));
+ _graph_node_moved(gn);
+ gn->set_stop_mouse(false);
+ }
+}
+
+void GraphEdit::remove_child_notify(Node *p_child) {
+
+ top_layer->call_deferred("raise"); //top layer always on top!
+ GraphNode *gn = p_child->cast_to<GraphNode>();
+ if (gn) {
+ gn->disconnect("offset_changed",this,"_graph_node_moved");
+ gn->disconnect("raise_request",this,"_graph_node_raised");
+ }
+}
+
+void GraphEdit::_notification(int p_what) {
+
+ if (p_what==NOTIFICATION_READY) {
+ Size2 size = top_layer->get_size();
+ Size2 hmin = h_scroll->get_combined_minimum_size();
+ Size2 vmin = v_scroll->get_combined_minimum_size();
+
+ v_scroll->set_anchor_and_margin(MARGIN_LEFT,ANCHOR_END,vmin.width);
+ v_scroll->set_anchor_and_margin(MARGIN_RIGHT,ANCHOR_END,0);
+ v_scroll->set_anchor_and_margin(MARGIN_TOP,ANCHOR_BEGIN,0);
+ v_scroll->set_anchor_and_margin(MARGIN_BOTTOM,ANCHOR_END,0);
+
+ h_scroll->set_anchor_and_margin(MARGIN_LEFT,ANCHOR_BEGIN,0);
+ h_scroll->set_anchor_and_margin(MARGIN_RIGHT,ANCHOR_END,0);
+ h_scroll->set_anchor_and_margin(MARGIN_TOP,ANCHOR_END,hmin.height);
+ h_scroll->set_anchor_and_margin(MARGIN_BOTTOM,ANCHOR_END,0);
+
+ }
+ if (p_what==NOTIFICATION_DRAW) {
+ VS::get_singleton()->canvas_item_set_clip(get_canvas_item(),true);
+
+ }
+
+ if (p_what==NOTIFICATION_RESIZED) {
+ _update_scroll();
+ top_layer->update();
+ }
+}
+
+bool GraphEdit::_filter_input(const Point2& p_point) {
+
+ Ref<Texture> port =get_icon("port","GraphNode");
+
+ float grab_r=port->get_width()*0.5;
+ for(int i=get_child_count()-1;i>=0;i--) {
+
+ GraphNode *gn=get_child(i)->cast_to<GraphNode>();
+ if (!gn)
+ continue;
+
+ for(int j=0;j<gn->get_connection_output_count();j++) {
+
+ Vector2 pos = gn->get_connection_output_pos(j)+gn->get_pos();
+ if (pos.distance_to(p_point)<grab_r)
+ return true;
+
+
+ }
+
+ for(int j=0;j<gn->get_connection_input_count();j++) {
+
+ Vector2 pos = gn->get_connection_input_pos(j)+gn->get_pos();
+ if (pos.distance_to(p_point)<grab_r)
+ return true;
+
+
+ }
+
+ }
+
+ return false;
+}
+
+void GraphEdit::_top_layer_input(const InputEvent& p_ev) {
+
+ if (p_ev.type==InputEvent::MOUSE_BUTTON && p_ev.mouse_button.button_index==BUTTON_LEFT && p_ev.mouse_button.pressed) {
+
+ Ref<Texture> port =get_icon("port","GraphNode");
+ Vector2 mpos(p_ev.mouse_button.x,p_ev.mouse_button.y);
+ float grab_r=port->get_width()*0.5;
+ for(int i=get_child_count()-1;i>=0;i--) {
+
+ GraphNode *gn=get_child(i)->cast_to<GraphNode>();
+ if (!gn)
+ continue;
+
+ for(int j=0;j<gn->get_connection_output_count();j++) {
+
+ Vector2 pos = gn->get_connection_output_pos(j)+gn->get_pos();
+ if (pos.distance_to(mpos)<grab_r) {
+
+ connecting=true;
+ connecting_from=gn->get_name();
+ connecting_index=j;
+ connecting_out=true;
+ connecting_type=gn->get_connection_output_type(j);
+ connecting_color=gn->get_connection_output_color(j);
+ connecting_target=false;
+ connecting_to=pos;
+ return;
+ }
+
+
+ }
+
+ for(int j=0;j<gn->get_connection_input_count();j++) {
+
+ Vector2 pos = gn->get_connection_input_pos(j)+gn->get_pos();
+
+ if (pos.distance_to(mpos)<grab_r) {
+ connecting=true;
+ connecting_from=gn->get_name();
+ connecting_index=j;
+ connecting_out=false;
+ connecting_type=gn->get_connection_input_type(j);
+ connecting_color=gn->get_connection_input_color(j);
+ connecting_target=false;
+ connecting_to=pos;
+ return;
+ }
+
+
+ }
+ }
+ }
+
+ if (p_ev.type==InputEvent::MOUSE_MOTION && connecting) {
+
+ connecting_to=Vector2(p_ev.mouse_motion.x,p_ev.mouse_motion.y);
+ connecting_target=false;
+ top_layer->update();
+
+ Ref<Texture> port =get_icon("port","GraphNode");
+ Vector2 mpos(p_ev.mouse_button.x,p_ev.mouse_button.y);
+ float grab_r=port->get_width()*0.5;
+ for(int i=get_child_count()-1;i>=0;i--) {
+
+ GraphNode *gn=get_child(i)->cast_to<GraphNode>();
+ if (!gn)
+ continue;
+
+ if (!connecting_out) {
+ for(int j=0;j<gn->get_connection_output_count();j++) {
+
+ Vector2 pos = gn->get_connection_output_pos(j)+gn->get_pos();
+ int type =gn->get_connection_output_type(j);
+ if (type==connecting_type && pos.distance_to(mpos)<grab_r) {
+
+ connecting_target=true;
+ connecting_to=pos;
+ connecting_target_to=gn->get_name();
+ connecting_target_index=j;
+ return;
+ }
+
+
+ }
+ } else {
+
+ for(int j=0;j<gn->get_connection_input_count();j++) {
+
+ Vector2 pos = gn->get_connection_input_pos(j)+gn->get_pos();
+ int type =gn->get_connection_input_type(j);
+ if (type==connecting_type && pos.distance_to(mpos)<grab_r) {
+ connecting_target=true;
+ connecting_to=pos;
+ connecting_target_to=gn->get_name();
+ connecting_target_index=j;
+ return;
+ }
+ }
+ }
+ }
+ }
+
+ if (p_ev.type==InputEvent::MOUSE_BUTTON && p_ev.mouse_button.button_index==BUTTON_LEFT && !p_ev.mouse_button.pressed) {
+
+ if (connecting && connecting_target) {
+
+ String from = connecting_from;
+ int from_slot = connecting_index;
+ String to =connecting_target_to;
+ int to_slot = connecting_target_index;
+
+ if (!connecting_out) {
+ SWAP(from,to);
+ SWAP(from_slot,to_slot);
+ }
+ emit_signal("connection_request",from,from_slot,to,to_slot);
+
+ }
+ connecting=false;
+ top_layer->update();
+
+ }
+
+
+
+}
+
+void GraphEdit::_draw_cos_line(const Vector2& p_from, const Vector2& p_to,const Color& p_color) {
+
+ static const int steps = 20;
+
+ Rect2 r;
+ r.pos=p_from;
+ r.expand_to(p_to);
+ Vector2 sign=Vector2((p_from.x < p_to.x) ? 1 : -1,(p_from.y < p_to.y) ? 1 : -1);
+ bool flip = sign.x * sign.y < 0;
+
+ Vector2 prev;
+ for(int i=0;i<=steps;i++) {
+
+ float d = i/float(steps);
+ float c=-Math::cos(d*Math_PI) * 0.5+0.5;
+ if (flip)
+ c=1.0-c;
+ Vector2 p = r.pos+Vector2(d*r.size.width,c*r.size.height);
+
+ if (i>0) {
+
+ top_layer->draw_line(prev,p,p_color,2);
+ }
+
+ prev=p;
+ }
+}
+
+void GraphEdit::_top_layer_draw() {
+
+ _update_scroll();
+
+ if (connecting) {
+
+ Node *fromn = get_node(connecting_from);
+ ERR_FAIL_COND(!fromn);
+ GraphNode *from = fromn->cast_to<GraphNode>();
+ ERR_FAIL_COND(!from);
+ Vector2 pos;
+ if (connecting_out)
+ pos=from->get_connection_output_pos(connecting_index);
+ else
+ pos=from->get_connection_input_pos(connecting_index);
+ pos+=from->get_pos();
+
+ Vector2 topos;
+ topos=connecting_to;
+
+ Color col=connecting_color;
+
+ if (connecting_target) {
+ col.r+=0.4;
+ col.g+=0.4;
+ col.b+=0.4;
+ }
+ _draw_cos_line(pos,topos,col);
+ }
+
+ List<List<Connection>::Element* > to_erase;
+ for(List<Connection>::Element *E=connections.front();E;E=E->next()) {
+
+ NodePath fromnp(E->get().from);
+
+ Node * from = get_node(fromnp);
+ if (!from) {
+ to_erase.push_back(E);
+ continue;
+ }
+
+ GraphNode *gfrom = from->cast_to<GraphNode>();
+
+ if (!gfrom) {
+ to_erase.push_back(E);
+ continue;
+ }
+
+ NodePath tonp(E->get().to);
+ Node * to = get_node(tonp);
+ if (!to) {
+ to_erase.push_back(E);
+ continue;
+ }
+
+ GraphNode *gto = to->cast_to<GraphNode>();
+
+ if (!gto) {
+ to_erase.push_back(E);
+ continue;
+ }
+
+ Vector2 frompos=gfrom->get_connection_output_pos(E->get().from_port)+gfrom->get_pos();
+ Color color = gfrom->get_connection_output_color(E->get().from_port);
+ Vector2 topos=gto->get_connection_input_pos(E->get().to_port)+gto->get_pos();
+ _draw_cos_line(frompos,topos,color);
+
+ }
+
+ while(to_erase.size()) {
+ connections.erase(to_erase.front()->get());
+ to_erase.pop_front();
+ }
+ //draw connections
+}
+
+void GraphEdit::_input_event(const InputEvent& p_ev) {
+
+ if (p_ev.type==InputEvent::MOUSE_MOTION && p_ev.mouse_motion.button_mask&BUTTON_MASK_MIDDLE) {
+ h_scroll->set_val( h_scroll->get_val() - p_ev.mouse_motion.relative_x );
+ v_scroll->set_val( v_scroll->get_val() - p_ev.mouse_motion.relative_y );
+ }
+}
+
+void GraphEdit::clear_connections() {
+
+ connections.clear();
+ update();
+}
+
+
+void GraphEdit::_bind_methods() {
+
+ ObjectTypeDB::bind_method(_MD("connect_node:Error","from","from_port","to","to_port"),&GraphEdit::connect_node);
+ ObjectTypeDB::bind_method(_MD("is_node_connected","from","from_port","to","to_port"),&GraphEdit::is_node_connected);
+ ObjectTypeDB::bind_method(_MD("disconnect_node","from","from_port","to","to_port"),&GraphEdit::disconnect_node);
+ ObjectTypeDB::bind_method(_MD("_graph_node_moved"),&GraphEdit::_graph_node_moved);
+ ObjectTypeDB::bind_method(_MD("_graph_node_raised"),&GraphEdit::_graph_node_raised);
+
+ ObjectTypeDB::bind_method(_MD("_top_layer_input"),&GraphEdit::_top_layer_input);
+ ObjectTypeDB::bind_method(_MD("_top_layer_draw"),&GraphEdit::_top_layer_draw);
+ ObjectTypeDB::bind_method(_MD("_scroll_moved"),&GraphEdit::_scroll_moved);
+
+ ObjectTypeDB::bind_method(_MD("_input_event"),&GraphEdit::_input_event);
+
+ ADD_SIGNAL(MethodInfo("connection_request",PropertyInfo(Variant::STRING,"from"),PropertyInfo(Variant::INT,"from_slot"),PropertyInfo(Variant::STRING,"to"),PropertyInfo(Variant::INT,"to_slot")));
+
+}
+
+GraphEdit::GraphEdit() {
+ top_layer=NULL;
+ top_layer=memnew(GraphEditFilter(this));
+ add_child(top_layer);
+ top_layer->set_stop_mouse(false);
+ top_layer->set_area_as_parent_rect();
+ top_layer->connect("draw",this,"_top_layer_draw");
+ top_layer->set_stop_mouse(false);
+ top_layer->connect("input_event",this,"_top_layer_input");
+
+ h_scroll = memnew(HScrollBar);
+ h_scroll->set_name("_h_scroll");
+ top_layer->add_child(h_scroll);
+
+ v_scroll = memnew(VScrollBar);
+ v_scroll->set_name("_v_scroll");
+ top_layer->add_child(v_scroll);
+ updating=false;
+ connecting=false;
+
+ h_scroll->connect("value_changed", this,"_scroll_moved");
+ v_scroll->connect("value_changed", this,"_scroll_moved");
+}
diff --git a/scene/gui/graph_edit.h b/scene/gui/graph_edit.h
new file mode 100644
index 0000000000..f8a2f3fee7
--- /dev/null
+++ b/scene/gui/graph_edit.h
@@ -0,0 +1,93 @@
+#ifndef GRAPH_EDIT_H
+#define GRAPH_EDIT_H
+
+#include "scene/gui/graph_node.h"
+#include "scene/gui/scroll_bar.h"
+
+class GraphEdit;
+
+class GraphEditFilter : public Control {
+
+ OBJ_TYPE(GraphEditFilter,Control);
+
+friend class GraphEdit;
+ GraphEdit *ge;
+ virtual bool has_point(const Point2& p_point) const;
+
+public:
+
+
+ GraphEditFilter(GraphEdit *p_edit);
+};
+
+class GraphEdit : public Control {
+
+ OBJ_TYPE(GraphEdit,Control);
+public:
+
+ struct Connection {
+ StringName from;
+ StringName to;
+ int from_port;
+ int to_port;
+
+ };
+private:
+
+ HScrollBar* h_scroll;
+ VScrollBar* v_scroll;
+
+
+ bool connecting;
+ String connecting_from;
+ bool connecting_out;
+ int connecting_index;
+ int connecting_type;
+ Color connecting_color;
+ bool connecting_target;
+ Vector2 connecting_to;
+ String connecting_target_to;
+ int connecting_target_index;
+
+
+
+ bool updating;
+ List<Connection> connections;
+
+ void _draw_cos_line(const Vector2& p_from, const Vector2& p_to,const Color& p_color);
+
+ void _graph_node_raised(Node* p_gn);
+ void _graph_node_moved(Node *p_gn);
+
+ void _update_scroll();
+ void _scroll_moved(double);
+ void _input_event(const InputEvent& p_ev);
+
+ GraphEditFilter *top_layer;
+ void _top_layer_input(const InputEvent& p_ev);
+ void _top_layer_draw();
+ void _update_scroll_offset();
+
+friend class GraphEditFilter;
+ bool _filter_input(const Point2& p_point);
+protected:
+
+ static void _bind_methods();
+ virtual void add_child_notify(Node *p_child);
+ virtual void remove_child_notify(Node *p_child);
+ void _notification(int p_what);
+
+public:
+
+ Error connect_node(const StringName& p_from, int p_from_port,const StringName& p_to,int p_to_port);
+ bool is_node_connected(const StringName& p_from, int p_from_port,const StringName& p_to,int p_to_port);
+ void disconnect_node(const StringName& p_from, int p_from_port,const StringName& p_to,int p_to_port);
+ void clear_connections();
+
+ void get_connection_list(List<Connection> *r_connections);
+
+
+ GraphEdit();
+};
+
+#endif // GRAPHEdit_H
diff --git a/scene/gui/graph_node.cpp b/scene/gui/graph_node.cpp
index 6e3afeefd0..50ee9abcf8 100644
--- a/scene/gui/graph_node.cpp
+++ b/scene/gui/graph_node.cpp
@@ -76,7 +76,7 @@ void GraphNode::_get_property_list( List<PropertyInfo> *p_list) const{
int idx=0;
for(int i=0;i<get_child_count();i++) {
Control *c=get_child(i)->cast_to<Control>();
- if (!c || c->is_set_as_toplevel() || !c->get_owner())
+ if (!c || c->is_set_as_toplevel() )
continue;
String base="slot/"+itos(idx)+"/";
@@ -122,6 +122,7 @@ void GraphNode::_resort() {
}
+
int vofs=0;
int w = get_size().x - sb->get_minimum_size().x;
@@ -131,7 +132,7 @@ void GraphNode::_resort() {
Control *c=get_child(i)->cast_to<Control>();
if (!c)
continue;
- if (c->is_set_as_toplevel() || !c->get_owner())
+ if (c->is_set_as_toplevel())
continue;
Size2i size=c->get_combined_minimum_size();
@@ -150,6 +151,8 @@ void GraphNode::_resort() {
_change_notify();
update();
+ connpos_dirty=true;
+
}
@@ -160,14 +163,34 @@ void GraphNode::_notification(int p_what) {
Ref<StyleBox> sb=get_stylebox("frame");
Ref<Texture> port =get_icon("port");
+ Ref<Texture> close =get_icon("close");
+ int close_offset = get_constant("close_offset");
+ Ref<Font> title_font = get_font("title_font");
+ int title_offset = get_constant("title_offset");
+ Color title_color = get_color("title_color");
Point2i icofs = -port->get_size()*0.5;
- int edgeofs=3;
+ int edgeofs=get_constant("port_offset");
icofs.y+=sb->get_margin(MARGIN_TOP);
draw_style_box(sb,Rect2(Point2(),get_size()));
+ int w = get_size().width-sb->get_minimum_size().x;
+
+ if (show_close)
+ w-=close->get_width();
+
+ draw_string(title_font,Point2(sb->get_margin(MARGIN_LEFT),-title_font->get_height()+title_font->get_ascent()+title_offset),title,title_color,w);
+ if (show_close) {
+ Vector2 cpos = Point2(w+sb->get_margin(MARGIN_LEFT),-close->get_height()+close_offset);
+ draw_texture(close,cpos);
+ close_rect.pos=cpos;
+ close_rect.size=close->get_size();
+ } else {
+ close_rect=Rect2();
+ }
+
for (Map<int,Slot>::Element *E=slot_info.front();E;E=E->next()) {
- if (E->key()>cache_y.size())
+ if (E->key() < 0 || E->key()>=cache_y.size())
continue;
if (!slot_info.has(E->key()))
continue;
@@ -180,6 +203,7 @@ void GraphNode::_notification(int p_what) {
}
}
+
if (p_what==NOTIFICATION_SORT_CHILDREN) {
_resort();
@@ -187,16 +211,6 @@ void GraphNode::_notification(int p_what) {
}
-void GraphNode::set_title(const String& p_title) {
-
- title=p_title;
- update();
-}
-
-String GraphNode::get_title() const {
-
- return title;
-}
void GraphNode::set_slot(int p_idx,bool p_enable_left,int p_type_left,const Color& p_color_left, bool p_enable_right,int p_type_right,const Color& p_color_right) {
@@ -216,17 +230,23 @@ void GraphNode::set_slot(int p_idx,bool p_enable_left,int p_type_left,const Colo
s.color_right=p_color_right;
slot_info[p_idx]=s;
update();
+ connpos_dirty=true;
+
}
void GraphNode::clear_slot(int p_idx){
slot_info.erase(p_idx);
update();
+ connpos_dirty=true;
+
}
void GraphNode::clear_all_slots(){
slot_info.clear();
update();
+ connpos_dirty=true;
+
}
bool GraphNode::is_slot_enabled_left(int p_idx) const{
@@ -280,18 +300,26 @@ Color GraphNode::get_slot_color_right(int p_idx) const{
Size2 GraphNode::get_minimum_size() const {
+ Ref<Font> title_font = get_font("title_font");
+
int sep=get_constant("separation");
Ref<StyleBox> sb=get_stylebox("frame");
bool first=true;
Size2 minsize;
+ minsize.x=title_font->get_string_size(title).x;
+ if (show_close) {
+ Ref<Texture> close =get_icon("close");
+ minsize.x+=sep+close->get_width();
+ }
+
for(int i=0;i<get_child_count();i++) {
Control *c=get_child(i)->cast_to<Control>();
if (!c)
continue;
- if (c->is_set_as_toplevel() || !c->get_owner())
+ if (c->is_set_as_toplevel())
continue;
Size2i size=c->get_combined_minimum_size();
@@ -308,13 +336,225 @@ Size2 GraphNode::get_minimum_size() const {
return minsize+sb->get_minimum_size();
}
+void GraphNode::set_title(const String& p_title) {
+
+ title=p_title;
+ minimum_size_changed();
+ update();
+
+}
+
+String GraphNode::get_title() const{
+
+ return title;
+}
+
+void GraphNode::set_offset(const Vector2& p_offset) {
+
+ offset=p_offset;
+ emit_signal("offset_changed");
+ update();
+}
+
+Vector2 GraphNode::get_offset() const {
+
+ return offset;
+}
+
+
+
+void GraphNode::set_show_close_button(bool p_enable){
+
+ show_close=p_enable;
+ update();
+}
+bool GraphNode::is_close_button_visible() const{
+
+ return show_close;
+}
+
+void GraphNode::_connpos_update() {
+
+
+ int edgeofs=get_constant("port_offset");
+ int sep=get_constant("separation");
+
+ Ref<StyleBox> sb=get_stylebox("frame");
+ Ref<Texture> port =get_icon("port");
+ conn_input_cache.clear();
+ conn_output_cache.clear();
+ int vofs=0;
+
+ int idx=0;
+
+ for(int i=0;i<get_child_count();i++) {
+ Control *c=get_child(i)->cast_to<Control>();
+ if (!c)
+ continue;
+ if (c->is_set_as_toplevel())
+ continue;
+
+ Size2i size=c->get_combined_minimum_size();
+
+ int y = sb->get_margin(MARGIN_TOP)+vofs;
+ int h = size.y;
+
+
+ if (slot_info.has(idx)) {
+
+ if (slot_info[idx].enable_left) {
+ ConnCache cc;
+ cc.pos=Point2i(edgeofs,y+h/2);
+ cc.type=slot_info[idx].type_left;
+ cc.color=slot_info[idx].color_left;
+ conn_input_cache.push_back(cc);
+ }
+ if (slot_info[idx].enable_right) {
+ ConnCache cc;
+ cc.pos=Point2i(get_size().width-edgeofs,y+h/2);
+ cc.type=slot_info[idx].type_right;
+ cc.color=slot_info[idx].color_right;
+ conn_output_cache.push_back(cc);
+ }
+ }
+
+ if (vofs>0)
+ vofs+=sep;
+ vofs+=size.y;
+ idx++;
+
+ }
+
+
+ connpos_dirty=false;
+}
+
+int GraphNode::get_connection_input_count() {
+
+ if (connpos_dirty)
+ _connpos_update();
+
+ return conn_input_cache.size();
+
+}
+int GraphNode::get_connection_output_count() {
+
+ if (connpos_dirty)
+ _connpos_update();
+
+ return conn_output_cache.size();
+
+}
+
+
+Vector2 GraphNode::get_connection_input_pos(int p_idx) {
+
+ if (connpos_dirty)
+ _connpos_update();
+
+ ERR_FAIL_INDEX_V(p_idx,conn_input_cache.size(),Vector2());
+ return conn_input_cache[p_idx].pos;
+}
+
+int GraphNode::get_connection_input_type(int p_idx) {
+
+ if (connpos_dirty)
+ _connpos_update();
+
+ ERR_FAIL_INDEX_V(p_idx,conn_input_cache.size(),0);
+ return conn_input_cache[p_idx].type;
+}
+
+Color GraphNode::get_connection_input_color(int p_idx) {
+
+ if (connpos_dirty)
+ _connpos_update();
+
+ ERR_FAIL_INDEX_V(p_idx,conn_input_cache.size(),Color());
+ return conn_input_cache[p_idx].color;
+}
+
+Vector2 GraphNode::get_connection_output_pos(int p_idx){
+
+ if (connpos_dirty)
+ _connpos_update();
+
+ ERR_FAIL_INDEX_V(p_idx,conn_output_cache.size(),Vector2());
+ return conn_output_cache[p_idx].pos;
+
+}
+
+int GraphNode::get_connection_output_type(int p_idx) {
+
+ if (connpos_dirty)
+ _connpos_update();
+
+ ERR_FAIL_INDEX_V(p_idx,conn_output_cache.size(),0);
+ return conn_output_cache[p_idx].type;
+}
+
+Color GraphNode::get_connection_output_color(int p_idx) {
+
+ if (connpos_dirty)
+ _connpos_update();
+
+ ERR_FAIL_INDEX_V(p_idx,conn_output_cache.size(),Color());
+ return conn_output_cache[p_idx].color;
+}
+
+void GraphNode::_input_event(const InputEvent& p_ev) {
+
+ if (p_ev.type==InputEvent::MOUSE_BUTTON && p_ev.mouse_button.pressed && p_ev.mouse_button.button_index==BUTTON_LEFT) {
+
+ Vector2 mpos = Vector2(p_ev.mouse_button.x,p_ev.mouse_button.y);
+ if (close_rect.size!=Size2() && close_rect.has_point(mpos)) {
+ emit_signal("close_request");
+ return;
+ }
+
+ drag_from=get_offset();
+ drag_accum=Vector2();
+ dragging=true;
+ emit_signal("raise_request");
+
+ }
+
+ if (p_ev.type==InputEvent::MOUSE_BUTTON && !p_ev.mouse_button.pressed && p_ev.mouse_button.button_index==BUTTON_LEFT) {
+
+ dragging=false;
+ emit_signal("dragged",drag_from,get_offset()); //useful for undo/redo
+ }
+
+ if (p_ev.type==InputEvent::MOUSE_MOTION && dragging) {
+
+ drag_accum+=Vector2(p_ev.mouse_motion.relative_x,p_ev.mouse_motion.relative_y);
+ set_offset(drag_from+drag_accum);
+ }
+
+}
+
void GraphNode::_bind_methods() {
+ ObjectTypeDB::bind_method(_MD("set_title","title"),&GraphNode::set_title);
+ ObjectTypeDB::bind_method(_MD("get_title"),&GraphNode::get_title);
+ ObjectTypeDB::bind_method(_MD("_input_event"),&GraphNode::_input_event);
+
+ ObjectTypeDB::bind_method(_MD("set_show_close_button","show"),&GraphNode::set_show_close_button);
+ ObjectTypeDB::bind_method(_MD("is_close_button_visible"),&GraphNode::is_close_button_visible);
+ ADD_PROPERTY( PropertyInfo(Variant::STRING,"title"),_SCS("set_title"),_SCS("get_title"));
+ ADD_PROPERTY( PropertyInfo(Variant::BOOL,"show_close"),_SCS("set_show_close_button"),_SCS("is_close_button_visible"));
+
+ ADD_SIGNAL(MethodInfo("offset_changed"));
+ ADD_SIGNAL(MethodInfo("dragged",PropertyInfo(Variant::VECTOR2,"from"),PropertyInfo(Variant::VECTOR2,"to")));
+ ADD_SIGNAL(MethodInfo("raise_request"));
+ ADD_SIGNAL(MethodInfo("close_request"));
}
GraphNode::GraphNode() {
-
+ dragging=false;
+ show_close=false;
+ connpos_dirty=true;
}
diff --git a/scene/gui/graph_node.h b/scene/gui/graph_node.h
index 3b89da9f0f..0d5cbf8dd3 100644
--- a/scene/gui/graph_node.h
+++ b/scene/gui/graph_node.h
@@ -8,7 +8,7 @@ class GraphNode : public Container {
OBJ_TYPE(GraphNode,Container);
- String title;
+
struct Slot {
bool enable_left;
int type_left;
@@ -21,13 +21,36 @@ class GraphNode : public Container {
Slot() { enable_left=false; type_left=0; color_left=Color(1,1,1,1); enable_right=false; type_right=0; color_right=Color(1,1,1,1); };
};
+ String title;
+ bool show_close;
+ Vector2 offset;
+
+ Rect2 close_rect;
+
Vector<int> cache_y;
+ struct ConnCache {
+ Vector2 pos;
+ int type;
+ Color color;
+ };
+
+ Vector<ConnCache> conn_input_cache;
+ Vector<ConnCache> conn_output_cache;
+
Map<int,Slot> slot_info;
+ bool connpos_dirty;
+
+ void _connpos_update();
void _resort();
+
+ Vector2 drag_from;
+ Vector2 drag_accum;
+ bool dragging;
protected:
+ void _input_event(const InputEvent& p_ev);
void _notification(int p_what);
static void _bind_methods();
@@ -39,8 +62,6 @@ public:
- void set_title(const String& p_title);
- String get_title() const;
void set_slot(int p_idx,bool p_enable_left,int p_type_left,const Color& p_color_left, bool p_enable_right,int p_type_right,const Color& p_color_right);
void clear_slot(int p_idx);
@@ -52,6 +73,25 @@ public:
int get_slot_type_right(int p_idx) const;
Color get_slot_color_right(int p_idx) const;
+ void set_title(const String& p_title);
+ String get_title() const;
+
+ void set_offset(const Vector2& p_offset);
+ Vector2 get_offset() const;
+
+ void set_show_close_button(bool p_enable);
+ bool is_close_button_visible() const;
+
+ int get_connection_input_count() ;
+ int get_connection_output_count() ;
+ Vector2 get_connection_input_pos(int p_idx);
+ int get_connection_input_type(int p_idx);
+ Color get_connection_input_color(int p_idx);
+ Vector2 get_connection_output_pos(int p_idx);
+ int get_connection_output_type(int p_idx);
+ Color get_connection_output_color(int p_idx);
+
+
virtual Size2 get_minimum_size() const;
GraphNode();
diff --git a/scene/gui/menu_button.cpp b/scene/gui/menu_button.cpp
index 7353744d07..d10ca20fc3 100644
--- a/scene/gui/menu_button.cpp
+++ b/scene/gui/menu_button.cpp
@@ -84,6 +84,7 @@ void MenuButton::pressed() {
popup->set_parent_rect( Rect2(Point2(gp-popup->get_global_pos()),get_size()));
popup->popup();
popup->call_deferred("grab_click_focus");
+ popup->set_invalidate_click_until_motion();
}
diff --git a/scene/gui/popup_menu.cpp b/scene/gui/popup_menu.cpp
index 0ba3bdb7c6..1fd1d8adc8 100644
--- a/scene/gui/popup_menu.cpp
+++ b/scene/gui/popup_menu.cpp
@@ -30,6 +30,7 @@
#include "print_string.h"
#include "os/keyboard.h"
#include "translation.h"
+#include "os/input.h"
String PopupMenu::_get_accel_text(uint32_t p_accel) const {
@@ -318,6 +319,10 @@ void PopupMenu::_input_event(const InputEvent &p_event) {
int over=_get_mouse_over(Point2(b.x,b.y));
+ if (invalidated_click) {
+ invalidated_click=false;
+ break;
+ }
if (over<0 || items[over].separator || items[over].disabled)
break; //non-activable
@@ -336,6 +341,13 @@ void PopupMenu::_input_event(const InputEvent &p_event) {
case InputEvent::MOUSE_MOTION: {
+ if (invalidated_click) {
+ moved+=Vector2(p_event.mouse_motion.relative_x,p_event.mouse_motion.relative_y);
+ if (moved.length()>4)
+ invalidated_click=false;
+
+ }
+
const InputEventMouseMotion &m=p_event.mouse_motion;
for(List<Rect2>::Element *E=autohide_areas.front();E;E=E->next()) {
@@ -893,12 +905,17 @@ void PopupMenu::_bind_methods() {
}
+
+void PopupMenu::set_invalidate_click_until_motion() {
+ moved=Vector2();
+ invalidated_click=true;
+}
+
PopupMenu::PopupMenu() {
idcount=0;
mouse_over=-1;
-
set_focus_mode(FOCUS_ALL);
set_as_toplevel(true);
diff --git a/scene/gui/popup_menu.h b/scene/gui/popup_menu.h
index b150be1008..c2e988de95 100644
--- a/scene/gui/popup_menu.h
+++ b/scene/gui/popup_menu.h
@@ -70,6 +70,8 @@ class PopupMenu : public Popup {
void _activate_submenu(int over);
void _submenu_timeout();
+ bool invalidated_click;
+ Vector2 moved;
Array _get_items() const;
void _set_items(const Array& p_items);
@@ -134,6 +136,8 @@ public:
void add_autohide_area(const Rect2& p_area);
void clear_autohide_areas();
+ void set_invalidate_click_until_motion();
+
PopupMenu();
~PopupMenu();