[gtk/wip/matthiasc/lottie-stroke: 20/24] Add an interactive test for stroking
- From: Matthias Clasen <matthiasc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtk/wip/matthiasc/lottie-stroke: 20/24] Add an interactive test for stroking
- Date: Sun, 29 Nov 2020 18:57:12 +0000 (UTC)
commit 65ca4bb336fc3031fa7215dbf146d1bc960afa95
Author: Matthias Clasen <mclasen redhat com>
Date: Sat Nov 28 13:30:35 2020 -0500
Add an interactive test for stroking
Give the curve3 test a full complement of stroke
parameters to play with.
tests/curve3.c | 142 ++++++++++++++++++++++++++++++++++++++++++++++++++++-----
1 file changed, 131 insertions(+), 11 deletions(-)
---
diff --git a/tests/curve3.c b/tests/curve3.c
index 827927b7fa..b34590db22 100644
--- a/tests/curve3.c
+++ b/tests/curve3.c
@@ -7,9 +7,11 @@ struct _DemoWidget
{
GtkWidget parent_instance;
GskPath *path;
- GskPathMeasure *measure;
+ GskPathMeasure *stroke_measure;
+ GskPath *stroke_path;
gboolean inside;
GskFillRule fill_rule;
+ GskStroke *stroke;
};
struct _DemoWidgetClass
@@ -27,7 +29,7 @@ motion (GtkEventControllerMotion *controller,
{
gboolean inside = TRUE;
- inside = gsk_path_measure_in_fill (self->measure, &GRAPHENE_POINT_INIT (x, y), self->fill_rule);
+ inside = gsk_path_measure_in_fill (self->stroke_measure, &GRAPHENE_POINT_INIT (x, y), self->fill_rule);
if (self->inside == inside)
return;
@@ -43,6 +45,8 @@ demo_widget_init (DemoWidget *self)
controller = gtk_event_controller_motion_new ();
g_signal_connect (controller, "motion", G_CALLBACK (motion), self);
gtk_widget_add_controller (GTK_WIDGET (self), controller);
+
+ self->stroke = gsk_stroke_new (20.f);
}
static void
@@ -61,24 +65,32 @@ demo_widget_snapshot (GtkWidget *widget,
if (self->inside)
{
- gtk_snapshot_push_fill (snapshot, self->path, self->fill_rule);
+ gtk_snapshot_push_fill (snapshot, self->stroke_path, self->fill_rule);
gtk_snapshot_append_color (snapshot,
- &(GdkRGBA){ 1, 0, 1, 1},
+ &(GdkRGBA){ 1, 0, 1, 0.3},
&GRAPHENE_RECT_INIT (0, 0, width, height ));
gtk_snapshot_pop (snapshot);
}
stroke = gsk_stroke_new (1.0);
- gtk_snapshot_push_stroke (snapshot, self->path, stroke);
- gsk_stroke_free (stroke);
+ gtk_snapshot_push_stroke (snapshot, self->stroke_path, stroke);
gtk_snapshot_append_color (snapshot,
&(GdkRGBA){ 0, 0, 0, 1},
&GRAPHENE_RECT_INIT (0, 0, width, height ));
gtk_snapshot_pop (snapshot);
+
+ gtk_snapshot_push_stroke (snapshot, self->path, stroke);
+
+ gtk_snapshot_append_color (snapshot,
+ &(GdkRGBA){ 0, 0, 0, 0.3},
+ &GRAPHENE_RECT_INIT (0, 0, width, height ));
+
+ gtk_snapshot_pop (snapshot);
+ gsk_stroke_free (stroke);
}
static void
@@ -87,7 +99,9 @@ demo_widget_dispose (GObject *object)
DemoWidget *self = DEMO_WIDGET (object);
g_clear_pointer (&self->path, gsk_path_unref);
- g_clear_pointer (&self->measure, gsk_path_measure_unref);
+ g_clear_pointer (&self->stroke_path, gsk_path_unref);
+ g_clear_pointer (&self->stroke, gsk_stroke_free);
+ g_clear_pointer (&self->stroke_measure, gsk_path_measure_unref);
G_OBJECT_CLASS (demo_widget_parent_class)->dispose (object);
}
@@ -111,15 +125,25 @@ demo_widget_new (void)
return g_object_new (DEMO_TYPE_WIDGET, NULL);
}
+static void
+update_stroke_path (DemoWidget *self)
+{
+ g_clear_pointer (&self->stroke_path, gsk_path_unref);
+ g_clear_pointer (&self->stroke_measure, gsk_path_measure_unref);
+
+ self->stroke_path = gsk_path_to_stroke (self->path, self->stroke);
+ self->stroke_measure = gsk_path_measure_new (self->stroke_path);
+
+ gtk_widget_queue_draw (GTK_WIDGET (self));
+}
+
static void
demo_widget_set_path (DemoWidget *self,
GskPath *path)
{
g_clear_pointer (&self->path, gsk_path_unref);
- g_clear_pointer (&self->measure, gsk_path_measure_unref);
self->path = gsk_path_ref (path);
- self->measure = gsk_path_measure_new (path);
- gtk_widget_queue_draw (GTK_WIDGET (self));
+ update_stroke_path (self);
}
static void
@@ -127,9 +151,25 @@ demo_widget_set_fill_rule (DemoWidget *self,
GskFillRule fill_rule)
{
self->fill_rule = fill_rule;
+
gtk_widget_queue_draw (GTK_WIDGET (self));
}
+static const GskStroke *
+demo_widget_get_stroke (DemoWidget *self)
+{
+ return self->stroke;
+}
+
+static void
+demo_widget_set_stroke (DemoWidget *self,
+ GskStroke *stroke)
+{
+ gsk_stroke_free (self->stroke);
+ self->stroke = gsk_stroke_copy (stroke);
+ update_stroke_path (self);
+}
+
static void
init_demo (DemoWidget *demo)
{
@@ -137,11 +177,36 @@ init_demo (DemoWidget *demo)
GskPath *path;
builder = gsk_path_builder_new ();
- gsk_path_builder_add_circle (builder, &GRAPHENE_POINT_INIT (150, 150), 100);
+
gsk_path_builder_add_rect (builder, &GRAPHENE_RECT_INIT (100, 100, 100, 100));
gsk_path_builder_move_to (builder, 300, 150);
gsk_path_builder_curve_to (builder, 300, 50, 400, 50, 400, 150);
gsk_path_builder_curve_to (builder, 400, 250, 500, 250, 500, 150);
+
+#if 0
+ gsk_path_builder_move_to (builder, 50, 50);
+ gsk_path_builder_line_to (builder, 100, 100);
+ gsk_path_builder_line_to (builder, 150, 50);
+ gsk_path_builder_line_to (builder, 200, 100);
+ gsk_path_builder_line_to (builder, 250, 50);
+
+#elif 0
+ gsk_path_builder_move_to (builder, 300, 300);
+ gsk_path_builder_curve_to (builder, 300, 400, 400, 400, 400, 300);
+ gsk_path_builder_curve_to (builder, 400, 200, 500, 200, 500, 300);
+
+ gsk_path_builder_add_rect (builder, 100, 300, 100, 100);
+#elif 0
+ gsk_path_builder_add_rect (builder, 100, 100, 300, 200);
+#elif 0
+ gsk_path_builder_add_circle (builder, &GRAPHENE_POINT_INIT (200, 200), 100);
+#elif 0
+ gsk_path_builder_move_to (builder, 100, 100);
+ gsk_path_builder_line_to (builder, 150, 150);
+ gsk_path_builder_line_to (builder, 200, 100);
+ gsk_path_builder_curve_to (builder, 250, 50, 300, 50, 350, 100);
+#endif
+
path = gsk_path_builder_free_to_path (builder);
demo_widget_set_path (demo, path);
@@ -158,12 +223,51 @@ fill_rule_changed (GtkDropDown *combo,
gtk_widget_queue_draw (GTK_WIDGET (self));
}
+static void
+cap_changed (GtkDropDown *combo,
+ GParamSpec *pspec,
+ DemoWidget *self)
+{
+ GskStroke *stroke;
+
+ stroke = gsk_stroke_copy (demo_widget_get_stroke (self));
+ gsk_stroke_set_line_cap (stroke, (GskLineCap)gtk_drop_down_get_selected (combo));
+ demo_widget_set_stroke (self, stroke);
+ gsk_stroke_free (stroke);
+}
+
+static void
+join_changed (GtkDropDown *combo,
+ GParamSpec *pspec,
+ DemoWidget *self)
+{
+ GskStroke *stroke;
+
+ stroke = gsk_stroke_copy (demo_widget_get_stroke (self));
+ gsk_stroke_set_line_join (stroke, (GskLineJoin)gtk_drop_down_get_selected (combo));
+ demo_widget_set_stroke (self, stroke);
+ gsk_stroke_free (stroke);
+}
+
+static void
+limit_changed (GtkSpinButton *spin,
+ DemoWidget *self)
+{
+ GskStroke *stroke;
+
+ stroke = gsk_stroke_copy (demo_widget_get_stroke (self));
+ gsk_stroke_set_miter_limit (stroke, gtk_spin_button_get_value (spin));
+ demo_widget_set_stroke (self, stroke);
+ gsk_stroke_free (stroke);
+}
+
int
main (int argc, char *argv[])
{
GtkWidget *window, *box, *demo;
GtkWidget *popover, *button, *grid;
GtkWidget *header, *combo;
+ GtkWidget *spin;
gtk_init ();
@@ -199,6 +303,22 @@ main (int argc, char *argv[])
init_demo (DEMO_WIDGET (demo));
+ gtk_grid_attach (GTK_GRID (grid), gtk_label_new ("Line cap:"), 0, 1, 1, 1);
+ combo = gtk_drop_down_new_from_strings ((const char *[]){"Butt", "Round", "Square", NULL});
+ g_signal_connect (combo, "notify::selected", G_CALLBACK (cap_changed), demo);
+ gtk_grid_attach (GTK_GRID (grid), combo, 1, 1, 1, 1);
+
+ gtk_grid_attach (GTK_GRID (grid), gtk_label_new ("Line join:"), 0, 2, 1, 1);
+ combo = gtk_drop_down_new_from_strings ((const char *[]){"Miter", "Miter-clip", "Round", "Bevel", NULL});
+ g_signal_connect (combo, "notify::selected", G_CALLBACK (join_changed), demo);
+ gtk_grid_attach (GTK_GRID (grid), combo, 1, 2, 1, 1);
+
+ gtk_grid_attach (GTK_GRID (grid), gtk_label_new ("Miter limit:"), 0, 3, 1, 1);
+ spin = gtk_spin_button_new_with_range (0, 10, 1);
+ gtk_spin_button_set_value (GTK_SPIN_BUTTON (spin), 4);
+ g_signal_connect (spin, "value-changed", G_CALLBACK (limit_changed), demo);
+ gtk_grid_attach (GTK_GRID (grid), spin, 1, 3, 1, 1);
+
gtk_window_present (GTK_WINDOW (window));
while (g_list_model_get_n_items (gtk_window_get_toplevels ()) > 0)
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]