[gegl/soc-2012-editor: 5/36] Added pads and connections between pads are rendered with a bezier curve



commit 60cbad622ad339cb1bec250256572646adac0443
Author: Isaac Wagner <isaacbw src gnome org>
Date:   Tue Jun 5 23:58:33 2012 -0400

    Added pads and connections between pads are rendered with a bezier curve

 bin/editor/build              |    2 +-
 bin/editor/gegl-editor.c      |    2 +
 bin/editor/gegl-node-widget.c |  183 +++++++++++++++++++++++++++++++++++++++--
 bin/editor/gegl-node-widget.h |   33 +++++---
 4 files changed, 201 insertions(+), 19 deletions(-)
---
diff --git a/bin/editor/build b/bin/editor/build
index ed50483..ec9e060 100755
--- a/bin/editor/build
+++ b/bin/editor/build
@@ -1 +1 @@
-gcc gegl-editor.c gegl-node-widget.c -o editor `pkg-config --cflags --libs gtk+-2.0`
\ No newline at end of file
+gcc gegl-editor.c gegl-node-widget.c -o editor `pkg-config --cflags --libs gtk+-2.0` -I/usr/local/include/gegl-0.1/ -I/usr/local/include/babl-0.1/
\ No newline at end of file
diff --git a/bin/editor/gegl-editor.c b/bin/editor/gegl-editor.c
index 9b9125a..9877777 100644
--- a/bin/editor/gegl-editor.c
+++ b/bin/editor/gegl-editor.c
@@ -1,6 +1,8 @@
 #include <glib.h>
 #include <gtk/gtk.h>
 #include <cairo.h>
+#include <gegl.h>
+
 #include "gegl-node-widget.h"
 
 
diff --git a/bin/editor/gegl-node-widget.c b/bin/editor/gegl-node-widget.c
index 24d7d1e..53d0718 100644
--- a/bin/editor/gegl-node-widget.c
+++ b/bin/editor/gegl-node-widget.c
@@ -45,10 +45,14 @@ EditorNode* new_editor_node(EditorNode* prev) {
   EditorNode* node = malloc(sizeof(EditorNode));
   node->next = NULL;
   node->x = node->y = 0;
-  node->width = node->height = 75;
+  node->width = node->height = 100;
   if(prev != NULL)
     prev->next = node;
-  node->title = "Empty Node";
+  node->title = "New Node";
+
+  node->inputs = NULL;
+  node->outputs = NULL;
+
   return node;
 }
 
@@ -59,6 +63,50 @@ EditorNode* top_node(EditorNode* first)
   return node;
 }
 
+void
+connect_pads(NodePad* a, NodePad* b)
+{
+  a->connected = b;
+  b->connected = a;
+}
+
+void
+get_pad_position_input(NodePad* pad, gint* x, gint* y, cairo_t* cr, GeglNodeWidget* editor)
+{
+  cairo_select_font_face(cr, "Georgia",
+			 CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
+  cairo_set_font_size(cr, 12);
+
+  cairo_text_extents_t te;
+  cairo_text_extents(cr, pad->node->title, &te);
+  gint title_height = te.height+5;
+
+  int i = 0;
+  NodePad* _pad = pad->node->inputs;
+  for(;_pad!=NULL;_pad = _pad->next, i++)
+    {
+      if(_pad == pad)
+	{
+	  gint node_x, node_y;
+
+	  if(pad->node == editor->dragged_node)
+	    {
+	      node_x = pad->node->x+editor->px-editor->dx;
+	      node_y = pad->node->y+editor->py-editor->dy;
+	    }
+	  else
+	    {
+	      node_x = pad->node->x;
+	      node_y = pad->node->y;
+	    }
+
+	  *x = node_x+5;
+	  *y = node_y+(title_height)+15+20*i;
+	  break;
+	}
+    }
+}
+
 static void
 draw_node(EditorNode* node, cairo_t *cr, GeglNodeWidget* editor)
 {
@@ -76,6 +124,11 @@ draw_node(EditorNode* node, cairo_t *cr, GeglNodeWidget* editor)
       y = node->y;
     }
 
+  if(node->width < 100)
+    node->width = 100;
+  if(node->height < 50)
+    node->height = 50;
+
   if(node == editor->resized_node)
     {
       width = node->width+editor->px-editor->dx;
@@ -87,8 +140,8 @@ draw_node(EditorNode* node, cairo_t *cr, GeglNodeWidget* editor)
       height = node->height;
     }
 
-  if(width < 50)
-    width = 50;
+  if(width < 100)
+    width = 100;
   if(height < 50)
     height = 50;
 
@@ -111,6 +164,7 @@ draw_node(EditorNode* node, cairo_t *cr, GeglNodeWidget* editor)
 
   cairo_text_extents_t te;
   cairo_text_extents(cr, node->title, &te);
+  gint title_height = te.height+5;
 
   //draw the line separating the title
   cairo_move_to(cr, x, y+te.height+5);
@@ -132,6 +186,65 @@ draw_node(EditorNode* node, cairo_t *cr, GeglNodeWidget* editor)
   cairo_move_to(cr, x+width-15, y+height);
   cairo_line_to(cr, x+width, y+height-15);
   cairo_stroke(cr);
+
+
+  int i = 0;
+  NodePad* pad = node->inputs;
+  for(;pad!=NULL;pad = pad->next, i++)
+    {
+      cairo_text_extents(cr, pad->name, &te);
+
+      cairo_set_source_rgb(cr, 0, 0, 0);
+      cairo_rectangle(cr, x, y+(title_height)+10+20*i, 10, 10);
+      cairo_fill(cr);
+
+      cairo_set_source_rgb(cr, 0, 0, 0);
+      cairo_move_to(cr, x+12.5, y+(title_height)+10+20*i+te.height/2+5);
+      cairo_show_text(cr, pad->name);
+    }
+
+  i = 0;
+  pad = node->outputs;
+  for(;pad!=NULL;pad = pad->next, i++)
+    {
+      cairo_rectangle(cr, x, y, width, height);
+      cairo_clip(cr);
+
+      cairo_text_extents(cr, pad->name, &te);
+
+      cairo_set_source_rgb(cr, 0, 0, 0);
+      cairo_rectangle(cr, x+width-10, y+(title_height)+10+20*i, 10, 10);
+      cairo_fill(cr);
+
+      cairo_set_source_rgb(cr, 0, 0, 0);
+      cairo_move_to(cr, x+width-15-te.width, y+(title_height)+10+20*i+te.height/2+5);
+      cairo_show_text(cr, pad->name);
+
+      cairo_reset_clip(cr);
+      
+      if(pad->connected)
+	{
+	  gint fx, fy;
+	  fx = x+width-5;
+	  fy = y+(title_height)+15+20*i;
+
+	  gint tx, ty;
+	  get_pad_position_input(pad->connected, &tx, &ty, cr, editor);
+
+
+	  cairo_move_to(cr, fx, fy);
+	  if(tx - fx > 200)
+	    cairo_curve_to(cr, (fx+tx)/2, fy,
+			   (fx+tx)/2, ty,
+			   tx, ty);
+	  else
+	    cairo_curve_to(cr, fx+100, fy,
+			   tx-100, ty,
+			   tx, ty);
+	  cairo_stroke(cr);
+
+	}
+    }
 }
 
 static gboolean
@@ -288,12 +401,68 @@ gegl_node_widget_init(GeglNodeWidget* self)
   self->dragged_node = NULL;
   self->resized_node = NULL;
 
-  self->first_node = new_editor_node(NULL);
-  EditorNode* node = new_editor_node(self->first_node);
+  EditorNode* node = new_editor_node(NULL);
+
+  NodePad* input = malloc(sizeof(NodePad));
+  input->next = NULL;
+  input->connected = NULL;
+  input->name = "In";
+  input->node = node;
+
+  NodePad* input2 = malloc(sizeof(NodePad));
+  input2->next = NULL;
+  input2->connected = NULL;
+  input2->name = "Mask";
+  input2->node = node;
+
+  input->next = input2;
+  node->inputs = input;
+
+  NodePad* output = malloc(sizeof(NodePad));
+  output->next = NULL;
+  output->connected = NULL;
+  output->name = "Out";
+  output->node = node;
+
+  node->outputs = output;
+
+  self->first_node = node;
+
+  node = new_editor_node(NULL);
+
+  input = malloc(sizeof(NodePad));
+  input->next = NULL;
+  input->connected = NULL;
+  input->name = "In";
+  input->node = node;
+
+  input2 = malloc(sizeof(NodePad));
+  input2->next = NULL;
+  input2->connected = NULL;
+  input2->name = "Mask";
+  input2->node = node;
+
+  input->next = input2;
+  node->inputs = input;
+
+  output = malloc(sizeof(NodePad));
+  output->next = NULL;
+  output->connected = NULL;
+  output->name = "Out";
+  output->node = node;
+
+  node->outputs = output;
+
+  self->first_node->next = node;
+  node->x = 200;
+
+  connect_pads(self->first_node->inputs, node->outputs);
+
+  /*   = new_editor_node(self->first_node);
   node->x = 50;
   node = new_editor_node(node);
   node->x = 100;
-  node->y = 14;
+  node->y = 14;*/
 }
 
 GtkWidget* 
diff --git a/bin/editor/gegl-node-widget.h b/bin/editor/gegl-node-widget.h
index 17ce728..1b3d6b7 100644
--- a/bin/editor/gegl-node-widget.h
+++ b/bin/editor/gegl-node-widget.h
@@ -17,16 +17,27 @@
 typedef struct _GeglNodeWidget		GeglNodeWidget;
 typedef struct _GeglNodeWidgetClass	GeglNodeWidgetClass;
 
-typedef struct _EditorNode EditorNode;
+typedef struct _EditorNode	EditorNode;
+typedef struct _NodePad NodePad;
+
+struct _NodePad
+{
+  gchar* name;
+  NodePad *connected; //the pad that this is connected to. NULL if none
+  NodePad *next; //the next pad in the linked list
+  EditorNode* node;
+};
 
 struct _EditorNode
 {
-  gint x, y, width, height;
-  gchar* title;
-  EditorNode *next;
+  gint		 x, y, width, height;
+  gchar*	 title;
+  EditorNode	*next;
+  NodePad* inputs;
+  NodePad* outputs;
 };
 
-EditorNode* new_editor_node(EditorNode* prev);
+EditorNode*	new_editor_node(EditorNode* prev);
 
 
 struct _GeglNodeWidget
@@ -34,12 +45,12 @@ struct _GeglNodeWidget
   GtkDrawingArea	parent;
 
   /* private */
-  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;
-  EditorNode* resized_node;
+  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;
+  EditorNode*	resized_node;
 };
 
 struct _GeglNodeWidgetClass



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