[gtk/matthiasc/for-master] gtk-demo: Add a little extra in peg solitaire




commit aa1b9531c10f6a5652eb6c760852d802e1c2cb46
Author: Matthias Clasen <mclasen redhat com>
Date:   Sun Aug 9 18:59:29 2020 -0400

    gtk-demo: Add a little extra in peg solitaire
    
    A win needs to be celebrated!

 demos/gtk-demo/peg_solitaire.c | 55 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 55 insertions(+)
---
diff --git a/demos/gtk-demo/peg_solitaire.c b/demos/gtk-demo/peg_solitaire.c
index 81f9a5c4c5..379a336e47 100644
--- a/demos/gtk-demo/peg_solitaire.c
+++ b/demos/gtk-demo/peg_solitaire.c
@@ -4,6 +4,7 @@
  *
  */
 
+#include "config.h"
 #include <gtk/gtk.h>
 
 /* Create an object for the pegs that get moved around in the game.
@@ -115,6 +116,57 @@ solitaire_peg_new (void)
   return g_object_new (SOLITAIRE_TYPE_PEG, NULL);
 }
 
+/*** Helper for finding a win ***/
+
+static void
+ended (GObject *object)
+{
+  g_object_unref (object);
+}
+
+static void
+celebrate (void)
+{
+  char *path;
+  GtkMediaStream *stream;
+
+  path = g_build_filename (GTK_DATADIR, "sounds", "freedesktop", "stereo", "complete.oga", NULL);
+  stream = gtk_media_file_new_for_filename (path);
+  gtk_media_stream_set_volume (stream, 1.0);
+  gtk_media_stream_play (stream);
+
+  g_signal_connect (stream, "notify::ended", G_CALLBACK (ended), NULL);
+  g_free (path);
+}
+
+static void
+check_for_win (GtkGrid *grid)
+{
+  GtkWidget *image;
+  int x, y;
+  int pegs;
+
+  pegs = 0;
+  for (x = 0; x < 7; x++)
+    {
+      for (y = 0; y < 7; y++)
+        {
+          image = gtk_grid_get_child_at (grid, x, y);
+          if (GTK_IS_IMAGE (image) &&
+              SOLITAIRE_IS_PEG (gtk_image_get_paintable (GTK_IMAGE (image))))
+            pegs++;
+        }
+    }
+
+  if (pegs > 1)
+    return;
+
+  image = gtk_grid_get_child_at (grid, 3, 3);
+  if (SOLITAIRE_IS_PEG (gtk_image_get_paintable (GTK_IMAGE (image))))
+    celebrate ();
+}
+
+
 /*** DRAG AND DROP ***/
 
 /* The user tries to start a drag operation.
@@ -267,6 +319,9 @@ drop_drop (GtkDropTarget *target,
   solitaire_peg_set_position (peg, image_x, image_y);
   gtk_image_set_from_paintable (GTK_IMAGE (image), GDK_PAINTABLE (peg));
 
+  /* Maybe we have something to celebrate */
+  check_for_win (grid);
+
   /* Success! */
   return TRUE;
 }


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