[graph-gtk] Nodes move with the mouse while being dragged



commit 42d6d76bc427e207ed6e9daf0cd8829c275fdf97
Author: Isaac Wagner <isaacbw src gnome org>
Date:   Fri Jul 27 09:23:00 2012 -0400

    Nodes move with the mouse while being dragged

 graph-gtk/graph-gtk-node.c |   58 ++++++++++++++++++++++----------------------
 graph-gtk/graph-gtk-node.h |    5 ++-
 graph-gtk/graph-gtk-pad.c  |    4 +-
 graph-gtk/graph-gtk-view.c |   18 +++++++++++++-
 4 files changed, 51 insertions(+), 34 deletions(-)
---
diff --git a/graph-gtk/graph-gtk-node.c b/graph-gtk/graph-gtk-node.c
index 2620a97..ee93bb2 100644
--- a/graph-gtk/graph-gtk-node.c
+++ b/graph-gtk/graph-gtk-node.c
@@ -71,7 +71,7 @@ void cairo_image_surface_blur( cairo_surface_t* surface, double radius )
 
 static void graph_gtk_node_dispose (GObject *object);
 static void graph_gtk_node_finalize (GObject *object);
-static void graph_gtk_node_render_default(GraphGtkNode* self, cairo_t* cairo, int x, int y);
+static void graph_gtk_node_render_default(GraphGtkNode* self, cairo_t* cairo);
 
 G_DEFINE_TYPE (GraphGtkNode, graph_gtk_node, G_TYPE_OBJECT);
 
@@ -116,7 +116,7 @@ graph_gtk_node_finalize (GObject *object)
 }
 
 static void
-graph_gtk_node_render_default(GraphGtkNode* self, cairo_t* cr, int offset_x, int offset_y)
+graph_gtk_node_render_default(GraphGtkNode* self, cairo_t* cr)
 {
   double M_PI = 3.14159;
   //Draw the node with cairo
@@ -150,68 +150,68 @@ graph_gtk_node_render_default(GraphGtkNode* self, cairo_t* cr, int offset_x, int
       cairo_image_surface_blur(shadow, 2);
 
       int offset = 3.5;
-      cairo_set_source_surface(cr, shadow, (self->x+offset_x)-25+offset, (self->y+offset_y)-25+offset);
-      //cairo_rectangle(cr, (self->x+offset_x)-10, (self->y+offset_y)-10, (self->x+offset_x)+20, (self->y+offset_y)+20);
+      cairo_set_source_surface(cr, shadow, (self->x+self->offset_x)-25+offset, (self->y+self->offset_y)-25+offset);
+      //cairo_rectangle(cr, (self->x+self->offset_x)-10, (self->y+self->offset_y)-10, (self->x+self->offset_x)+20, (self->y+self->offset_y)+20);
       cairo_paint(cr);
 
       cairo_surface_finish(shadow);
       cairo_destroy(shadow_context);
     }
 
-  cairo_move_to(cr, (self->x+offset_x)+corner_radius, (self->y+offset_y));
-  cairo_line_to(cr, (self->x+offset_x)+self->width-corner_radius, (self->y+offset_y));
-  cairo_arc(cr, (self->x+offset_x)+self->width-corner_radius, (self->y+offset_y)+corner_radius,
+  cairo_move_to(cr, (self->x+self->offset_x)+corner_radius, (self->y+self->offset_y));
+  cairo_line_to(cr, (self->x+self->offset_x)+self->width-corner_radius, (self->y+self->offset_y));
+  cairo_arc(cr, (self->x+self->offset_x)+self->width-corner_radius, (self->y+self->offset_y)+corner_radius,
 	    corner_radius,
 	    -M_PI/2.0, 0.0);
-  cairo_line_to(cr, (self->x+offset_x)+self->width, (self->y+offset_y)+15);
-  cairo_line_to(cr, (self->x+offset_x), (self->y+offset_y)+15);
-  cairo_line_to(cr, (self->x+offset_x), (self->y+offset_y)+corner_radius);
-  cairo_arc(cr, (self->x+offset_x)+corner_radius, (self->y+offset_y)+corner_radius, 
+  cairo_line_to(cr, (self->x+self->offset_x)+self->width, (self->y+self->offset_y)+15);
+  cairo_line_to(cr, (self->x+self->offset_x), (self->y+self->offset_y)+15);
+  cairo_line_to(cr, (self->x+self->offset_x), (self->y+self->offset_y)+corner_radius);
+  cairo_arc(cr, (self->x+self->offset_x)+corner_radius, (self->y+self->offset_y)+corner_radius, 
 	    corner_radius,
 	    M_PI, -M_PI/2.0);
   cairo_set_source_rgb(cr, 80.0/256.0, 80.0/256.0, 80.0/256.0);
   cairo_fill(cr);
 
-  cairo_move_to(cr, (self->x+offset_x)+self->width, (self->y+offset_y)+15);
-  cairo_line_to(cr, (self->x+offset_x)+self->width, (self->y+offset_y)+self->height-corner_radius);
-  cairo_arc(cr, (self->x+offset_x)+self->width-corner_radius, (self->y+offset_y)+self->height-corner_radius, corner_radius, 0.0, M_PI/2.0);
-  cairo_line_to(cr, (self->x+offset_x)+corner_radius, (self->y+offset_y)+self->height);
-  cairo_arc(cr, (self->x+offset_x)+corner_radius, (self->y+offset_y)+self->height-corner_radius,
+  cairo_move_to(cr, (self->x+self->offset_x)+self->width, (self->y+self->offset_y)+15);
+  cairo_line_to(cr, (self->x+self->offset_x)+self->width, (self->y+self->offset_y)+self->height-corner_radius);
+  cairo_arc(cr, (self->x+self->offset_x)+self->width-corner_radius, (self->y+self->offset_y)+self->height-corner_radius, corner_radius, 0.0, M_PI/2.0);
+  cairo_line_to(cr, (self->x+self->offset_x)+corner_radius, (self->y+self->offset_y)+self->height);
+  cairo_arc(cr, (self->x+self->offset_x)+corner_radius, (self->y+self->offset_y)+self->height-corner_radius,
 	    corner_radius,
 	    M_PI/2.0, M_PI);
-  cairo_line_to(cr, (self->x+offset_x), (self->y+offset_y)+15);
+  cairo_line_to(cr, (self->x+self->offset_x), (self->y+self->offset_y)+15);
   cairo_close_path(cr);
   cairo_set_source_rgb(cr, 44.0/256.0, 44.0/256.0, 44.0/256.0);
   cairo_fill(cr);
 
-  cairo_move_to(cr, (self->x+offset_x)+corner_radius, (self->y+offset_y));
+  cairo_move_to(cr, (self->x+self->offset_x)+corner_radius, (self->y+self->offset_y));
 
   //top
-  cairo_line_to(cr, (self->x+offset_x)+self->width-corner_radius, (self->y+offset_y));
+  cairo_line_to(cr, (self->x+self->offset_x)+self->width-corner_radius, (self->y+self->offset_y));
 
   //top-right
-  cairo_arc(cr, (self->x+offset_x)+self->width-corner_radius, (self->y+offset_y)+corner_radius,
+  cairo_arc(cr, (self->x+self->offset_x)+self->width-corner_radius, (self->y+self->offset_y)+corner_radius,
 	    corner_radius,
 	    -M_PI/2.0, 0.0);
 
   //right
-  cairo_line_to(cr, (self->x+offset_x)+self->width, (self->y+offset_y)+self->height-corner_radius);
+  cairo_line_to(cr, (self->x+self->offset_x)+self->width, (self->y+self->offset_y)+self->height-corner_radius);
 
   //right-bottom
-  cairo_arc(cr, (self->x+offset_x)+self->width-corner_radius, (self->y+offset_y)+self->height-corner_radius, corner_radius, 0.0, M_PI/2.0);
+  cairo_arc(cr, (self->x+self->offset_x)+self->width-corner_radius, (self->y+self->offset_y)+self->height-corner_radius, corner_radius, 0.0, M_PI/2.0);
 
   //bottom
-  cairo_line_to(cr, (self->x+offset_x)+corner_radius, (self->y+offset_y)+self->height);
+  cairo_line_to(cr, (self->x+self->offset_x)+corner_radius, (self->y+self->offset_y)+self->height);
 
   //bottom-left
-  cairo_arc(cr, (self->x+offset_x)+corner_radius, (self->y+offset_y)+self->height-corner_radius,
+  cairo_arc(cr, (self->x+self->offset_x)+corner_radius, (self->y+self->offset_y)+self->height-corner_radius,
 	    corner_radius,
 	    M_PI/2.0, M_PI);
 
   //left
-  cairo_line_to(cr, (self->x+offset_x), (self->y+offset_y)+corner_radius);
+  cairo_line_to(cr, (self->x+self->offset_x), (self->y+self->offset_y)+corner_radius);
   //top-left
-  cairo_arc(cr, (self->x+offset_x)+corner_radius, (self->y+offset_y)+corner_radius, 
+  cairo_arc(cr, (self->x+self->offset_x)+corner_radius, (self->y+self->offset_y)+corner_radius, 
 	    corner_radius,
 	    M_PI, -M_PI/2.0);
   cairo_close_path(cr); //probably unecessary
@@ -236,7 +236,7 @@ graph_gtk_node_render_default(GraphGtkNode* self, cairo_t* cr, int offset_x, int
   cairo_select_font_face (cr, "FreeSerif", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
   cairo_set_font_size(cr, 14);
   cairo_set_source_rgb(cr, 200.0/256.0, 200.0/256.0, 200.0/256.0);
-  cairo_move_to(cr, (self->x+offset_x)+4, (self->y+offset_y)+13);
+  cairo_move_to(cr, (self->x+self->offset_x)+4, (self->y+self->offset_y)+13);
   cairo_show_text(cr, self->name);
 
   GSList *pad;
@@ -258,13 +258,13 @@ graph_gtk_node_new()
 }
 
 void
-graph_gtk_node_render(GraphGtkNode* self, cairo_t* cairo, int x, int y)
+graph_gtk_node_render(GraphGtkNode* self, cairo_t* cairo)
 {
   g_return_if_fail(IS_GRAPH_GTK_NODE(self));
 
   graph_gtk_node_recalculate_size(self);
 
-  GRAPH_GTK_NODE_GET_CLASS(self)->render_node(self, cairo, x, y);
+  GRAPH_GTK_NODE_GET_CLASS(self)->render_node(self, cairo);
 }
 
 GSList*
diff --git a/graph-gtk/graph-gtk-node.h b/graph-gtk/graph-gtk-node.h
index 237eb56..862553f 100644
--- a/graph-gtk/graph-gtk-node.h
+++ b/graph-gtk/graph-gtk-node.h
@@ -38,7 +38,7 @@ typedef struct _GraphGtkNodeClass GraphGtkNodeClass;
 struct _GraphGtkNodeClass
 {
   GObjectClass parent_class;
-  void (*render_node) (GraphGtkNode* self, cairo_t* cairo, int x, int y);
+  void (*render_node) (GraphGtkNode* self, cairo_t* cairo);
 };
 
 struct _GraphGtkNode
@@ -58,6 +58,7 @@ struct _GraphGtkNode
 
   /* Rendering state */
   gint x, y; //coordinate position
+  gint offset_x, offset_y; //drag offset
   gint width, height;
   gboolean is_selected; //set and unset by the GraphGtkView
 };
@@ -67,7 +68,7 @@ GType graph_gtk_node_get_type (void) G_GNUC_CONST;
 GraphGtkNode*	graph_gtk_node_new();
 void		graph_gtk_node_set_name(GraphGtkNode* self, const gchar* name);
 const gchar*	graph_gtk_node_get_name(GraphGtkNode* self);
-void		graph_gtk_node_render(GraphGtkNode* self, cairo_t* cairo, int x, int y);
+void		graph_gtk_node_render(GraphGtkNode* self, cairo_t* cairo);
 void		graph_gtk_node_add_pad(GraphGtkNode* self, const gchar* pad_name, gboolean output);
 GSList*		graph_gtk_node_get_pads(GraphGtkNode* self);
 GSList*		graph_gtk_node_get_input_pads(GraphGtkNode* self);
diff --git a/graph-gtk/graph-gtk-pad.c b/graph-gtk/graph-gtk-pad.c
index 489e097..a4720a6 100644
--- a/graph-gtk/graph-gtk-pad.c
+++ b/graph-gtk/graph-gtk-pad.c
@@ -141,8 +141,8 @@ graph_gtk_pad_get_position(GraphGtkPad* self, int *x, int *y)
 {
   g_return_if_fail(IS_GRAPH_GTK_PAD(self));
 
-  *x = self->rel_x+self->node->x;
-  *y = self->rel_y+self->node->y;
+  *x = self->rel_x+self->node->x+self->node->offset_x;
+  *y = self->rel_y+self->node->y+self->node->offset_y;
 }
 
 gboolean
diff --git a/graph-gtk/graph-gtk-view.c b/graph-gtk/graph-gtk-view.c
index c1bfd92..6911500 100644
--- a/graph-gtk/graph-gtk-view.c
+++ b/graph-gtk/graph-gtk-view.c
@@ -187,7 +187,7 @@ graph_gtk_view_draw(GtkWidget *widget, cairo_t* cr)
   for(list = view->nodes; list != NULL; list = list->next)
     {
       GraphGtkNode* node = (GraphGtkNode*)list->data;
-      graph_gtk_node_render(node, cr, 0, 0);
+      graph_gtk_node_render(node, cr);
     }
 
   return FALSE;
@@ -207,6 +207,7 @@ graph_gtk_view_button_pressed(GtkWidget* widget, GdkEventButton* event)
       for(nodes = self->selected_nodes; nodes != NULL; nodes = nodes->next)
 	{
 	  GraphGtkNode *node = nodes->data;
+	  //Todo: don't emit signal if if is just going to be selected again
 	  g_signal_emit_by_name(widget, "node-deselected", node);
 	  node->is_selected = FALSE;
 	}
@@ -263,6 +264,8 @@ graph_gtk_view_button_released(GtkWidget* widget, GdkEventButton* event)
 	    {
 	      node->x += event->x-self->drag_begin_x;
 	      node->y += event->y-self->drag_begin_y;
+	      node->offset_x = 0;
+	      node->offset_y = 0;
 	    }
 	}
     }
@@ -305,6 +308,19 @@ graph_gtk_view_mouse_moved(GtkWidget* widget, GdkEventMotion* event)
 {
   GraphGtkView *self = GRAPH_GTK_VIEW(widget);
 
+  if(self->is_mouse_dragging)
+    {
+      GSList* nodes;
+      for(nodes = self->selected_nodes; nodes != NULL; nodes = nodes->next)
+	{
+	  GraphGtkNode *node = nodes->data;
+	  node->offset_x = event->x-self->drag_begin_x;
+	  node->offset_y = event->y-self->drag_begin_y;
+	}
+
+      REDRAW();
+    }
+
   return FALSE;
 }
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]