From 4a35f4247fb2fa7746e3040f6efb1faff3c21174 Mon Sep 17 00:00:00 2001 From: Nikita Zlobin Date: Sun, 15 May 2011 17:49:13 +0600 Subject: [PATCH] Begining of wire implementation --- src/gtkgraph-demo.c | 89 +++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 76 insertions(+), 13 deletions(-) diff --git a/src/gtkgraph-demo.c b/src/gtkgraph-demo.c index e9eb97d..27c22d2 100644 --- a/src/gtkgraph-demo.c +++ b/src/gtkgraph-demo.c @@ -17,11 +17,14 @@ * with this program. If not, see . */ +#define _GNU_SOURCE + #include #include #include #include #include +#include static gboolean on_root_buttonPress_event (GooCanvasItem * item, GooCanvasItem * target, GdkEventButton * event, gpointer data); static gboolean on_root_buttonRelease_event (GooCanvasItem * item, GooCanvasItem * target, GdkEventButton * event, gpointer data); @@ -43,7 +46,6 @@ static gboolean on_port_text_notify (GooCanvasItem * item, GParamSpec * event, static gboolean wire_entered (GooCanvasItem * item, GooCanvasItem * target, GdkEventCrossing * event, gpointer data); static gboolean wire_left (GooCanvasItem * item, GooCanvasItem * target, GdkEventCrossing * event, gpointer data); -GooCanvasItem * wire; double border_threshold = 1.0; #define drag_button 1 @@ -121,6 +123,23 @@ struct { #define G_MODULAR_GRAPH_PORT(p) ((GModularGraph_Port *) (p)) +typedef +struct { + GooCanvasItem * path_b; + GooCanvasItem * path_t; + + //char ** cmd_v; + //unsigned * cmd_len; + + double x1, y1, cx1, cy1; + double x2, y2, cx2, cy2; + + double angle1, angle2; /* In degreens */ + unsigned color; +} GModularGraph_Wire; + +#define G_MODULAR_GRAPH_WIRE(p) ((GModularGraph_Wire *) (p)) + /* Canvas data */ GtkWidget * canvas; GooCanvasItem * root_item; @@ -336,6 +355,58 @@ void g_modular_graph_module_add_port (GModularGraph_Port ** ret, if (ret) *ret = port; } +typedef +union { + struct { + uint8_t alpha; + uint8_t blue; + uint8_t green; + uint8_t red; + }; + uint32_t value; +} Color; + +GModularGraph_Wire * g_modular_graph_wire_new (double x1, double y1, double angle1, double x2, double y2, double angle2, char * tooltip, unsigned color) +{ + char * wire_data; + GModularGraph_Wire * wire; + wire = calloc (1, sizeof (GModularGraph_Wire)); + wire->x1 = x1, wire->y1 = y1; + wire->x2 = x2, wire->y2 = y2; + wire->angle1 = angle1; + wire->angle2 = angle2; + + wire->cx1 = wire->x1 + cos (wire->angle1 * G_PI / 180) * 50; + wire->cy1 = wire->y1 + sin (wire->angle1 * G_PI / 180) * 50; + wire->cx2 = wire->x2 + cos (wire->angle2 * G_PI / 180) * 50; + wire->cy2 = wire->y2 + sin (wire->angle2 * G_PI / 180) * 50; + asprintf (& wire_data, "M%i,%i C%i,%i %i,%i %i,%i", + (int) wire->x1, (int) wire->y1, + (int) wire->cx1, (int) wire->cy1, + (int) wire->cx2, (int) wire->cy2, + (int) wire->x2, (int) wire->y2); + + wire->path_b = goo_canvas_path_new (root_item, + wire_data, + "line-width", 2.0, + "stroke-color-rgba", color, NULL); + + Color color_t = {.value = 0xff0000f0}; + color_t.red = color_t.red + (0xff - color_t.red) * 0.8; + color_t.green = color_t.green + (0xff - color_t.green) * 0.8; + color_t.blue = color_t.blue + (0xff - color_t.blue) * 0.8; + wire->path_t = goo_canvas_path_new (root_item, + wire_data, + "line-width", 1.0, + "stroke-color-rgba", color_t.value, NULL); + + g_signal_connect (G_OBJECT (wire->path_t), "enter-notify-event", G_CALLBACK (wire_entered), NULL); + g_signal_connect (G_OBJECT (wire->path_t), "leave-notify-event", G_CALLBACK (wire_left), NULL); + g_object_set (wire->path_t, "tooltip", tooltip, NULL); + + return wire; +} + int main( int argc, char ** argv ) { GtkWidget * win; @@ -377,20 +448,12 @@ int main( int argc, char ** argv ) g_modular_graph_module_add_port (NULL, module[1], G_MODULAR_GRAPH_PORT_DIRECTION_OUT, "out-2", "Out 2 tip"); /* 2. Connection */ - char * wire_data = "M50,100 C100,100 50,200 100,200"; - wire = goo_canvas_path_new (root_item, - wire_data, - "line-width", 3.0, - "stroke-color", "black", NULL); - wire = goo_canvas_path_new (root_item, - wire_data, - "line-width", 2.0, - "stroke-color", "violet", NULL); - g_signal_connect (G_OBJECT (wire), "enter-notify-event", G_CALLBACK (wire_entered), NULL); - g_signal_connect (G_OBJECT (wire), "leave-notify-event", G_CALLBACK (wire_left), NULL); + g_modular_graph_wire_new (50, 100, 0.0, 300, 300, 180, "Test wire", 0x5f0000ff); + g_modular_graph_wire_new (50, 300, 0.0, 300, 100, 180, "Test wire", 0x5f0000ff); + + /* * * * */ gtk_widget_set_has_tooltip (canvas, TRUE); - g_object_set (wire, "tooltip", "Wire Test", NULL); gtk_widget_show_all (win); gtk_main (); -- 2.11.4.GIT