summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Clasen <mclasen@redhat.com>2018-06-07 17:38:24 -0400
committerMatthias Clasen <mclasen@redhat.com>2018-06-07 20:45:19 -0400
commitca888e5809c8bb3cf03d4b081fbd58e5a749cc52 (patch)
tree30c124b2fd7279a3b6aa814fcb29e393c2c1d735
parentad246d8a28008084bd2f600c53286701b3f6fd12 (diff)
downloadgtk+-wip/otte/puzzle.tar.gz
puzzle: Add mouse supportwip/otte/puzzle
Make it so that clicking on a puzzle piece moves enough pieces to move the empty space there, if it is possible.
-rw-r--r--demos/gtk-demo/sliding_puzzle.c68
1 files changed, 68 insertions, 0 deletions
diff --git a/demos/gtk-demo/sliding_puzzle.c b/demos/gtk-demo/sliding_puzzle.c
index f21a84b8f1..a49e034c0c 100644
--- a/demos/gtk-demo/sliding_puzzle.c
+++ b/demos/gtk-demo/sliding_puzzle.c
@@ -220,6 +220,68 @@ puzzle_key_pressed (GtkEventControllerKey *controller,
}
static void
+puzzle_button_pressed (GtkGestureMultiPress *gesture,
+ int n_press,
+ double x,
+ double y,
+ GtkWidget *grid)
+{
+ GtkWidget *child;
+ int l, t, i;
+ int pos;
+
+ child = gtk_widget_pick (grid, x, y);
+
+ if (!child)
+ {
+ gtk_widget_error_bell (grid);
+ return;
+ }
+
+ gtk_container_child_get (GTK_CONTAINER (grid), child,
+ "left-attach", &l,
+ "top-attach", &t,
+ NULL);
+
+ if (l == pos_x && t == pos_y)
+ {
+ gtk_widget_error_bell (grid);
+ }
+ else if (l == pos_x)
+ {
+ pos = pos_y;
+ for (i = t; i < pos; i++)
+ {
+ if (!move_puzzle (grid, 0, -1))
+ gtk_widget_error_bell (grid);
+ }
+ for (i = pos; i < t; i++)
+ {
+ if (!move_puzzle (grid, 0, 1))
+ gtk_widget_error_bell (grid);
+ }
+ }
+ else if (t == pos_y)
+ {
+ pos = pos_x;
+ for (i = l; i < pos; i++)
+ {
+ if (!move_puzzle (grid, -1, 0))
+ gtk_widget_error_bell (grid);
+ }
+ for (i = pos; i < l; i++)
+ {
+ if (!move_puzzle (grid, 1, 0))
+ gtk_widget_error_bell (grid);
+ }
+ }
+ else
+ {
+ gtk_widget_error_bell (grid);
+ }
+}
+
+static void
start_puzzle (GdkPaintable *puzzle)
{
GtkWidget *image, *grid;
@@ -244,6 +306,12 @@ start_puzzle (GdkPaintable *puzzle)
grid);
gtk_widget_add_controller (GTK_WIDGET (grid), controller);
+ controller = gtk_gesture_multi_press_new ();
+ g_signal_connect (controller, "pressed",
+ G_CALLBACK (puzzle_button_pressed),
+ grid);
+ gtk_widget_add_controller (GTK_WIDGET (grid), controller);
+
/* Make sure the cells have equal size */
gtk_grid_set_row_homogeneous (GTK_GRID (grid), TRUE);
gtk_grid_set_column_homogeneous (GTK_GRID (grid), TRUE);