[gegl/soc-2012-editor: 3/36] Added dummy nodes which can be dragged by the mouse



commit 8a1e6902c82f7682c152b7c5fbcbb9f1fc842841
Author: Isaac Wagner <isaacbw src gnome org>
Date:   Tue Jun 5 21:52:37 2012 -0400

    Added dummy nodes which can be dragged by the mouse

 bin/editor/gegl-node-widget.c |   90 +++++++++++++++++++++++++++++++++++++---
 bin/editor/gegl-node-widget.h |   19 ++++++++-
 2 files changed, 101 insertions(+), 8 deletions(-)
---
diff --git a/bin/editor/gegl-node-widget.c b/bin/editor/gegl-node-widget.c
index d6b8c6f..fce95fb 100644
--- a/bin/editor/gegl-node-widget.c
+++ b/bin/editor/gegl-node-widget.c
@@ -49,9 +49,33 @@ gegl_node_widget_draw(GtkWidget *widget, cairo_t *cr)
   cairo_set_source_rgb(cr, 0, 0, 0);
   cairo_paint(cr);
 
-  cairo_arc(cr, editor->p_x, editor->p_y, 30, 0, 2*3.14159);
-  cairo_set_source_rgb(cr, 1, 1, 1);
-  cairo_stroke(cr);
+
+
+  EditorNode* node = editor->first_node;
+  for(;node != NULL; node = node->next)
+    {
+      cairo_set_source_rgb(cr, 1, 1, 1);
+
+      /*if(editor->px > node->x && editor->px < node->x+node->width &&
+	 editor->py > node->y && editor->py < node->y+node->height) 
+	{
+	  cairo_set_source_rgb(cr, 1, 0, 0);
+	  }*/
+
+      if(node == editor->dragged_node)
+	{
+	  cairo_rectangle(cr, node->x+editor->px-editor->dx, node->y+editor->py-editor->dy, node->width, node->height);
+	}
+      else
+	{
+	  cairo_rectangle(cr, node->x, node->y, node->width, node->height);
+	}
+
+      cairo_fill(cr);
+    }
+  /*  cairo_arc(cr, editor->px, editor->py, 30, 0, 2*3.14159);
+
+  cairo_stroke(cr);*/
 
   return FALSE;
 }
@@ -73,8 +97,8 @@ static gboolean
 gegl_node_widget_motion(GtkWidget* widget, GdkEventMotion* event)
 {
   GeglNodeWidget*	editor = GEGL_NODE_WIDGET(widget);
-  editor->p_x		       = (gint)event->x;
-  editor->p_y		       = (gint)event->y;
+  editor->px		       = (gint)event->x;
+  editor->py		       = (gint)event->y;
 
   /* redraw */
   gtk_widget_queue_draw(widget);
@@ -85,7 +109,40 @@ gegl_node_widget_motion(GtkWidget* widget, GdkEventMotion* event)
 static gboolean
 gegl_node_widget_button_press(GtkWidget* widget, GdkEventButton* event)
 {
-  g_print("button_press\n");
+  GeglNodeWidget*	editor = GEGL_NODE_WIDGET(widget);
+  //TODO: check which mouse button was pressed
+  editor->left_mouse_down = TRUE;
+  editor->dx = editor->px;
+  editor->dy = editor->py;
+
+  EditorNode* node = editor->first_node;
+  for(;node != NULL; node = node->next)
+    {
+      if(editor->px > node->x && editor->px < node->x+node->width &&
+	 editor->py > node->y && editor->py < node->y+node->height) 
+	{
+	  editor->dragged_node = node;
+	}
+    }
+
+  return FALSE;
+}
+
+static gboolean
+gegl_node_widget_button_release(GtkWidget* widget, GdkEventButton* event)
+{
+  GeglNodeWidget*	editor = GEGL_NODE_WIDGET(widget);
+  //TODO: check which mouse button was released
+  editor->left_mouse_down = FALSE;
+
+  if(editor->dragged_node)
+    {
+      editor->dragged_node->x += editor->px-editor->dx;
+      editor->dragged_node->y += editor->py-editor->dy;
+    }
+
+  editor->dragged_node = NULL;
+
   return FALSE;
 }
 
@@ -100,6 +157,7 @@ gegl_node_widget_class_init(GeglNodeWidgetClass *klass)
 #endif
   widget_class->motion_notify_event   = gegl_node_widget_motion;
   widget_class->button_press_event    = gegl_node_widget_button_press;
+  widget_class->button_release_event    = gegl_node_widget_button_release;
 }
 
 
@@ -111,6 +169,16 @@ gegl_node_widget_init(GeglNodeWidget* self)
 			 GDK_POINTER_MOTION_MASK |
 			 GDK_BUTTON_PRESS_MASK   |
 			 GDK_BUTTON_RELEASE_MASK );
+
+  self->first_node = NULL;
+  self->dragged_node = NULL;
+
+  self->first_node = new_editor_node(NULL);
+  EditorNode* node = new_editor_node(self->first_node);
+  node->x = 50;
+  node = new_editor_node(node);
+  node->x = 100;
+  node->y = 14;
 }
 
 GtkWidget* 
@@ -119,4 +187,12 @@ gegl_node_widget_new ( void )
   return g_object_new (GEGL_TYPE_NODE_WIDGET, NULL);
 }
 
-
+EditorNode* new_editor_node(EditorNode* prev) {
+  EditorNode* node = malloc(sizeof(EditorNode));
+  node->next = NULL;
+  node->x = node->y = 0;
+  node->width = node->height = 25;
+  if(prev != NULL)
+    prev->next = node;
+  return node;
+}
diff --git a/bin/editor/gegl-node-widget.h b/bin/editor/gegl-node-widget.h
index 10b7e4e..28dfd9a 100644
--- a/bin/editor/gegl-node-widget.h
+++ b/bin/editor/gegl-node-widget.h
@@ -5,6 +5,7 @@
 
 #include	<gtk/gtk.h>
 #include <glib-object.h>
+#include <stdlib.h>
 
 #define GEGL_TYPE_NODE_WIDGET			(gegl_node_widget_get_type())
 #define GEGL_NODE_WIDGET(obj)			(G_TYPE_CHECK_INSTANCE_CAST(obj, GEGL_TYPE_NODE_WIDGET, GeglNodeWidget))
@@ -16,12 +17,28 @@
 typedef struct _GeglNodeWidget		GeglNodeWidget;
 typedef struct _GeglNodeWidgetClass	GeglNodeWidgetClass;
 
+typedef struct _EditorNode EditorNode;
+
+struct _EditorNode
+{
+  gint x, y, width, height;
+  gboolean dragging;
+  EditorNode* next;
+};
+
+EditorNode* new_editor_node(EditorNode* prev);
+
+
 struct _GeglNodeWidget
 {
   GtkDrawingArea	parent;
 
   /* private */
-  gint	p_x, p_y;
+  gint	px, py; //current mouse coordinates
+  gint dx, dy;  //last mouse coordinates when mouse button pressed
+  gboolean left_mouse_down; //if left mouse button is pressed
+  EditorNode* first_node;
+  EditorNode* dragged_node;
 };
 
 struct _GeglNodeWidgetClass



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