[gtk/wip/matthiasc/lottie-stroke: 4/4] wip: color segments in curve2




commit 5e1ca1b5354ad705aed9e5e5aa75b7db149c6cc0
Author: Matthias Clasen <mclasen redhat com>
Date:   Mon Nov 30 18:55:47 2020 -0500

    wip: color segments in curve2
    
    Maybe this is a bit much, but it can be useful
    for debugging.

 tests/curve2.c | 125 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 125 insertions(+)
---
diff --git a/tests/curve2.c b/tests/curve2.c
index c3178d11e7..d61b6208e9 100644
--- a/tests/curve2.c
+++ b/tests/curve2.c
@@ -25,6 +25,8 @@ struct _DemoWidget
   gboolean inside;
   GskFillRule fill_rule;
   GskStroke *stroke;
+
+  gboolean color_segments;
 };
 
 struct _DemoWidgetClass
@@ -97,6 +99,69 @@ demo_widget_init (DemoWidget *self)
   gtk_widget_add_controller (GTK_WIDGET (self), controller);
 }
 
+typedef struct
+{
+  GskPathBuilder *builder;
+  int count;
+} OddSegmentData;
+
+static gboolean
+collect_odd_segments (GskPathOperation        op,
+                      const graphene_point_t *pts,
+                      gsize                   n_pts,
+                      float                   weight,
+                      gpointer                user_data)
+{
+  OddSegmentData *data = user_data;
+
+  data->count++;
+
+  g_print ("segment %d\n", data->count);
+  switch (op)
+    {
+    case GSK_PATH_MOVE:
+      gsk_path_builder_move_to (data->builder, pts[0].x, pts[0].y);
+      break;
+    case GSK_PATH_CLOSE:
+      if ((data->count % 2) == 1)
+        gsk_path_builder_close (data->builder);
+      break;
+    case GSK_PATH_LINE:
+      if ((data->count % 2) == 1)
+        gsk_path_builder_line_to (data->builder, pts[1].x, pts[1].y);
+      else
+        gsk_path_builder_move_to (data->builder, pts[1].x, pts[1].y);
+      break;
+    case GSK_PATH_CURVE:
+      if ((data->count % 2) == 1)
+        gsk_path_builder_curve_to (data->builder,
+                                   pts[1].x, pts[1].y,
+                                   pts[2].x, pts[2].y,
+                                   pts[3].x, pts[3].y);
+      else
+        gsk_path_builder_move_to (data->builder, pts[3].x, pts[3].y);
+      break;
+    case GSK_PATH_CONIC:
+    default:
+      g_assert_not_reached ();
+    }
+
+  return TRUE;
+}
+
+static GskPath *
+get_odd_segments (GskPath *path)
+{
+  OddSegmentData data;
+
+  data.builder = gsk_path_builder_new ();
+  data.count = 0;
+
+  gsk_path_foreach (path, collect_odd_segments, &data);
+
+  return gsk_path_builder_free_to_path (data.builder);
+}
+
 static void
 demo_widget_snapshot (GtkWidget   *widget,
                       GtkSnapshot *snapshot)
@@ -135,6 +200,22 @@ demo_widget_snapshot (GtkWidget   *widget,
 
       gtk_snapshot_pop (snapshot);
 
+      if (self->color_segments)
+        {
+          path = get_odd_segments (self->stroke_path);
+
+          stroke = gsk_stroke_new (2.0);
+          gtk_snapshot_push_stroke (snapshot, path, stroke);
+
+          gtk_snapshot_append_color (snapshot,
+                                     &(GdkRGBA){ 1, 0, 0, 1},
+                                     &GRAPHENE_RECT_INIT (0, 0, width, height ));
+
+          gtk_snapshot_pop (snapshot);
+
+          gsk_path_unref (path);
+        }
+
       gtk_snapshot_push_stroke (snapshot, self->path, stroke);
 
       gtk_snapshot_append_color (snapshot,
@@ -143,6 +224,22 @@ demo_widget_snapshot (GtkWidget   *widget,
 
       gtk_snapshot_pop (snapshot);
       gsk_stroke_free (stroke);
+
+      if (self->color_segments)
+        {
+          path = get_odd_segments (self->path);
+
+          stroke = gsk_stroke_new (2.0);
+          gtk_snapshot_push_stroke (snapshot, path, stroke);
+
+          gtk_snapshot_append_color (snapshot,
+                                     &(GdkRGBA){ 1, 0, 0, 1},
+                                     &GRAPHENE_RECT_INIT (0, 0, width, height ));
+
+          gtk_snapshot_pop (snapshot);
+
+          gsk_path_unref (path);
+        }
     }
   else
     {
@@ -155,6 +252,22 @@ demo_widget_snapshot (GtkWidget   *widget,
                                  &GRAPHENE_RECT_INIT (0, 0, width, height ));
 
       gtk_snapshot_pop (snapshot);
+
+      if (self->color_segments)
+        {
+          path = get_odd_segments (self->path);
+
+          stroke = gsk_stroke_new (2.0);
+          gtk_snapshot_push_stroke (snapshot, path, stroke);
+
+          gtk_snapshot_append_color (snapshot,
+                                     &(GdkRGBA){ 1, 0, 0, 1},
+                                     &GRAPHENE_RECT_INIT (0, 0, width, height ));
+
+          gtk_snapshot_pop (snapshot);
+
+          gsk_path_unref (path);
+        }
     }
 
   if (self->show_bounding_box)
@@ -392,6 +505,14 @@ stroke_toggled (GtkCheckButton *button,
   gtk_widget_queue_draw (GTK_WIDGET (self));
 }
 
+static void
+color_segments_toggled (GtkCheckButton *button,
+                        DemoWidget      *self)
+{
+  self->color_segments = gtk_check_button_get_active (button);
+  gtk_widget_queue_draw (GTK_WIDGET (self));
+}
+
 static GtkWidget *start_scale;
 static GtkWidget *end_scale;
 
@@ -536,6 +657,10 @@ main (int argc, char *argv[])
   g_signal_connect (spin, "value-changed", G_CALLBACK (width_changed), demo);
   gtk_grid_attach (GTK_GRID (grid), spin, 1, 7, 1, 1);
 
+  toggle = gtk_check_button_new_with_label ("Color segments");
+  g_signal_connect (toggle, "toggled", G_CALLBACK (color_segments_toggled), demo);
+  gtk_grid_attach (GTK_GRID (grid), toggle, 1, 8, 1, 1);
+
 
   entry = gtk_entry_new ();
   g_signal_connect (entry, "activate", G_CALLBACK (activate), demo);


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