[gtk/wip/matthiasc/lottie-stroke: 1/3] Add line width in the curve demo




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

    Add line width in the curve demo
    
    This is getting a bit confusing, maybe.
    We have stroke width, which is the size of the
    stroke shape, and line width, which is the thickness
    with which we draw its outline.
    
    Also, use a fill node instead of a stroke node,
    to see how well it works to replace stroking paths
    with fill stroke paths.

 tests/curve2.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 72 insertions(+), 8 deletions(-)
---
diff --git a/tests/curve2.c b/tests/curve2.c
index c3178d11e7..29b464743a 100644
--- a/tests/curve2.c
+++ b/tests/curve2.c
@@ -14,6 +14,7 @@ struct _DemoWidget
   graphene_point_t point2;
   graphene_vec2_t tangent;
   double start, end;
+  float line_width;
 
   gboolean track;
   gboolean show_bounding_box;
@@ -97,6 +98,50 @@ demo_widget_init (DemoWidget *self)
   gtk_widget_add_controller (GTK_WIDGET (self), controller);
 }
 
+static  gboolean
+offset_segment (GskPathOperation        op,
+                const graphene_point_t *pts,
+                gsize                   n_pts,
+                float                   weight,
+                gpointer                user_data)
+{
+  GskPathBuilder *builder = user_data;
+
+  switch (op)
+    {
+    case GSK_PATH_MOVE:
+      gsk_path_builder_move_to (builder, pts[0].x + 0.5, pts[0].y + 0.5);
+      break;
+    case GSK_PATH_CLOSE:
+      gsk_path_builder_close (builder);
+      break;
+    case GSK_PATH_LINE:
+      gsk_path_builder_line_to (builder, pts[1].x + 0.5, pts[1].y + 0.5);
+      break;
+    case GSK_PATH_CURVE:
+      gsk_path_builder_curve_to (builder,
+                                 pts[1].x + 0.5, pts[1].y + 0.5,
+                                 pts[2].x + 0.5, pts[2].y + 0.5,
+                                 pts[3].x + 0.5, pts[3].y + 0.5);
+      break;
+    case GSK_PATH_CONIC:
+    default:
+      g_assert_not_reached ();
+    }
+
+  return TRUE;
+}
+
+static GskPath *
+offset_path (GskPath *path)
+{
+  GskPathBuilder *builder = gsk_path_builder_new ();
+
+  gsk_path_foreach (path, offset_segment, builder);
+
+  return gsk_path_builder_free_to_path (builder);
+}
+
 static void
 demo_widget_snapshot (GtkWidget   *widget,
                       GtkSnapshot *snapshot)
@@ -106,6 +151,7 @@ demo_widget_snapshot (GtkWidget   *widget,
   GskStroke *stroke;
   GskPathBuilder *builder;
   GskPath *path;
+  GskPath *path2;
 
   if (!self->path)
     return;
@@ -126,15 +172,19 @@ demo_widget_snapshot (GtkWidget   *widget,
            gtk_snapshot_pop (snapshot);
         }
 
-      stroke = gsk_stroke_new (1.0);
-      gtk_snapshot_push_stroke (snapshot, self->stroke_path, stroke);
+      stroke = gsk_stroke_new (self->line_width);
+      path = gsk_path_to_stroke (self->stroke_path, stroke);
+      gtk_snapshot_push_fill (snapshot, path, GSK_FILL_RULE_WINDING);
+      gsk_path_unref (path);
 
       gtk_snapshot_append_color (snapshot,
-                                 &(GdkRGBA){ 0, 0, 0, 1},
+                                 &(GdkRGBA){ 0, 0, 0, 0.2},
                                  &GRAPHENE_RECT_INIT (0, 0, width, height ));
 
       gtk_snapshot_pop (snapshot);
 
+      gsk_stroke_free (stroke);
+      stroke = gsk_stroke_new (1);
       gtk_snapshot_push_stroke (snapshot, self->path, stroke);
 
       gtk_snapshot_append_color (snapshot,
@@ -146,7 +196,7 @@ demo_widget_snapshot (GtkWidget   *widget,
     }
   else
     {
-      stroke = gsk_stroke_new (1.0);
+      stroke = gsk_stroke_new (self->line_width);
       gtk_snapshot_push_stroke (snapshot, self->path, stroke);
       gsk_stroke_free (stroke);
 
@@ -365,6 +415,7 @@ init_demo (DemoWidget  *demo,
   gsk_path_unref (path);
 
   demo->stroke = gsk_stroke_new (20);
+  demo->line_width = 1;
 }
 
 static void
@@ -456,13 +507,21 @@ limit_changed (GtkSpinButton *spin,
 }
 
 static void
-width_changed (GtkSpinButton *spin,
-               DemoWidget    *self)
+stroke_width_changed (GtkSpinButton *spin,
+                      DemoWidget    *self)
 {
   gsk_stroke_set_line_width (self->stroke, gtk_spin_button_get_value (spin));
   update_stroke_path (self);
 }
 
+static void
+line_width_changed (GtkSpinButton *spin,
+                    DemoWidget    *self)
+{
+  self->line_width = gtk_spin_button_get_value (spin);
+  gtk_widget_queue_draw (GTK_WIDGET (self));
+}
+
 int
 main (int argc, char *argv[])
 {
@@ -530,12 +589,17 @@ main (int argc, char *argv[])
   g_signal_connect (spin, "value-changed", G_CALLBACK (limit_changed), demo);
   gtk_grid_attach (GTK_GRID (grid), spin, 1, 6, 1, 1);
 
-  gtk_grid_attach (GTK_GRID (grid), gtk_label_new ("Line width:"), 0, 7, 1, 1);
+  gtk_grid_attach (GTK_GRID (grid), gtk_label_new ("Stroke width:"), 0, 7, 1, 1);
   spin = gtk_spin_button_new_with_range (1, 40, 1);
   gtk_spin_button_set_value (GTK_SPIN_BUTTON (spin), 20);
-  g_signal_connect (spin, "value-changed", G_CALLBACK (width_changed), demo);
+  g_signal_connect (spin, "value-changed", G_CALLBACK (stroke_width_changed), demo);
   gtk_grid_attach (GTK_GRID (grid), spin, 1, 7, 1, 1);
 
+  gtk_grid_attach (GTK_GRID (grid), gtk_label_new ("Line width:"), 0, 8, 1, 1);
+  spin = gtk_spin_button_new_with_range (1, 20, 1);
+  gtk_spin_button_set_value (GTK_SPIN_BUTTON (spin), 1);
+  g_signal_connect (spin, "value-changed", G_CALLBACK (line_width_changed), demo);
+  gtk_grid_attach (GTK_GRID (grid), spin, 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]