[gnome-nibbles] Add NibblesGame, NibblesView classes. Make game texture resizable



commit 616cf1898fa0a0950d11e1a1ebaaacee83793317
Author: Iulian Radu <iulian radu67 gmail com>
Date:   Thu May 14 16:38:06 2015 +0300

    Add NibblesGame, NibblesView classes. Make game texture resizable

 configure.ac                       |   18 ++--
 data/gnome-nibbles.ui              |   42 ++++++-
 data/org.gnome.nibbles.gschema.xml |    8 +
 src/Makefile.am                    |   16 ++-
 src/config.vapi                    |    2 +-
 src/games-gridframe.c              |  278 ++++++++++++++++++++++++++++++++++++
 src/games-gridframe.h              |   63 ++++++++
 src/games-gridframe.vapi           |   25 ++++
 src/gnome-nibbles.vala             |  136 ++++++++++++++++++
 src/nibbles-game.vala              |   12 ++
 src/nibbles-view.vala              |   46 ++++++
 src/properties.vala                |   19 +++
 12 files changed, 651 insertions(+), 14 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 5329a2d..9df04a4 100644
--- a/configure.ac
+++ b/configure.ac
@@ -17,8 +17,6 @@ LT_INIT
 
 GLIB_GSETTINGS
 
-APPSTREAM_XML
-
 AC_CONFIG_MACRO_DIR([m4])
 
 dnl ###########################################################################
@@ -27,17 +25,19 @@ dnl ###########################################################################
 
 GLIB_REQUIRED=2.40.0
 GTK_REQUIRED=3.14.0
+RSVG_REQUIRED=2.32.0
+CANBERRA_GTK_REQUIRED=0.26
+CLUTTER_REQUIRED=1.0.0
+CLUTTER_GTK_REQUIRED=0.91.6
 
-PKG_CHECK_MODULES(GNOME_SUDOKU, [
+PKG_CHECK_MODULES(GNOME_NIBBLES, [
   glib-2.0 >= $GLIB_REQUIRED
   gio-2.0 >= $GLIB_REQUIRED
   gtk+-3.0 >= $GTK_REQUIRED
-  gee-0.8
-])
-
-PKG_CHECK_MODULES(LIBSUDOKU, [
-  glib-2.0 >= $GLIB_REQUIRED
-  gio-2.0 >= $GLIB_REQUIRED
+  librsvg-2.0 >= $RSVG_REQUIRED
+  libcanberra-gtk3 >= $CANBERRA_GTK_REQUIRED
+  clutter-1.0 >= $CLUTTER_REQUIRED
+  clutter-gtk-1.0 >= $CLUTTER_REQUIRED
   gee-0.8
 ])
 
diff --git a/data/gnome-nibbles.ui b/data/gnome-nibbles.ui
index 6daaed9..89b812d 100644
--- a/data/gnome-nibbles.ui
+++ b/data/gnome-nibbles.ui
@@ -5,5 +5,45 @@
     </object>
     <object class="GtkApplicationWindow" id="nibbles-window">
         <property name="title" translatable="yes">Nibbles</property>
-    </object>
+        <child>
+            <object class="GtkBox" id="vbox">
+                <property name="orientation">vertical</property>
+                <property name="visible">True</property>
+                <child>
+                    <object class="GtkStack" id="main_stack">
+                        <property name="visible">True</property>
+                        <property name="homogeneous">True</property>
+                        <property name="margin">25</property>
+                        <property name="expand">True</property>
+                        <child>
+                            <object class="GtkBox" id="start_box">
+                                <property name="orientation">vertical</property>
+                                <property name="visible">True</property>
+                                <property name="halign">center</property>
+                                <property name="valign">center</property>
+                                <property name="homogeneous">True</property>
+                                <property name="margin">0</property>
+                                <property name="width-request">350</property>
+                                <property name="height-request">350</property>
+                                <child>
+                                    <object class="GtkButton" id="new_game_button">
+                                        <property name="visible">True</property>
+                                        <property name="use-underline">True</property>
+                                        <property name="label" translatable="yes">_New Game</property>
+                                        <property name="action-name">app.start-game</property>
+                                    </object>
+                                </child>
+                            </object>
+                            <packing>
+                                <property name="name">start_box</property>
+                            </packing>
+                        </child>
+                    </object> <!-- End of main_stack -->
+                    <packing>
+                        <property name="pack-type">end</property>
+                    </packing>
+                </child>
+            </object> <!-- End of vbox -->
+        </child>
+    </object> <!-- End of nibbles_app -->
 </interface>
diff --git a/data/org.gnome.nibbles.gschema.xml b/data/org.gnome.nibbles.gschema.xml
index c8028b0..ce47207 100644
--- a/data/org.gnome.nibbles.gschema.xml
+++ b/data/org.gnome.nibbles.gschema.xml
@@ -3,14 +3,22 @@
     <key name="window-width" type="i">
       <default>730</default>
       <summary>Width of the window in pixels</summary>
+      <description>Width of the window in pixels.</description>
     </key>
     <key name="window-height" type="i">
       <default>630</default>
       <summary>Height of the window in pixels</summary>
+      <description>Height of the window in pixels.</description>
     </key>
     <key name="window-is-maximized" type="b">
       <default>false</default>
       <summary>true if the window is maximized</summary>
+      <summary>true if the window is maximized.</summary>
+    </key>
+    <key name="tile-size" type="i">
+      <default>7</default>
+      <summary>Size of game tiles</summary>
+      <description>Size of game tiles.</description>
     </key>
   </schema>
 </schemalist>
diff --git a/src/Makefile.am b/src/Makefile.am
index 53d7721..777c055 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -5,14 +5,19 @@ BUILT_SOURCES = gnome-nibbles-resources.c
 gnome_nibbles_SOURCES = \
        config.vapi \
        gnome-nibbles.vala \
+       nibbles-view.vala \
+       nibbles-game.vala \
+       properties.vala \
+       games-gridframe.h \
+       games-gridframe.c \
        $(BUILT_SOURCES)
 
-gnome_nibbles_CFLAGS = -w `pkg-config --cflags glib-2.0` `pkg-config --cflags gtk+-3.0` `pkg-config --libs 
glib-2.0` `pkg-config --libs gtk+-3.0`
+gnome_nibbles_CFLAGS = -w `pkg-config --cflags glib-2.0` `pkg-config --cflags gtk+-3.0` `pkg-config --libs 
glib-2.0` `pkg-config --libs gtk+-3.0` `pkg-config --cflags clutter-1.0` `pkg-config --cflags 
clutter-gtk-1.0` `pkg-config --libs clutter-1.0` `pkg-config --libs clutter-gtk-1.0`
 
 
 gnome_nibbles_CPPFLAGS = \
        -DVERSION=\"$(VERSION)\" \
-       -DPKGDATADIR=\"$(datadir)/gnome-nibbles\" \
+       -DDATADIR=\"$(datadir)/gnome-nibbles\" \
        -DGETTEXT_PACKAGE=\"$(GETTEXT_PACKAGE)\" \
        $(GNOME_NIBBLES_CFLAGS)
 
@@ -20,9 +25,14 @@ gnome_nibbles_VALAFLAGS = \
        --pkg posix \
        --pkg gio-2.0 \
        --pkg gtk+-3.0 \
+       --pkg clutter-1.0 \
+       --pkg clutter-gtk-1.0 \
+       --pkg libcanberra \
+       --pkg libcanberra-gtk \
        --pkg gee-0.8 \
        --target-glib=$(GLIB_REQUIRED) \
-       --gresources=$(builddir)/gnome-nibbles.gresource.xml
+       --gresources=$(builddir)/gnome-nibbles.gresource.xml \
+       games-gridframe.vapi
 
 gnome-nibbles-resources.c: gnome-nibbles.gresource.xml $(shell $(GLIB_COMPILE_RESOURCES) 
--generate-dependencies gnome-nibbles.gresource.xml)
        $(AM_V_GEN) $(GLIB_COMPILE_RESOURCES) --target=$@ --sourcedir=$(srcdir) --generate-source $<
diff --git a/src/config.vapi b/src/config.vapi
index aa09de5..e30421d 100644
--- a/src/config.vapi
+++ b/src/config.vapi
@@ -1,3 +1,3 @@
-public const string PKGDATADIR;
+public const string DATADIR;
 public const string GETTEXT_PACKAGE;
 public const string VERSION;
diff --git a/src/games-gridframe.c b/src/games-gridframe.c
new file mode 100644
index 0000000..56fe00a
--- /dev/null
+++ b/src/games-gridframe.c
@@ -0,0 +1,278 @@
+/* games-gridframe.c: Create a container that guarantees that the internal
+ *                    allocated space is a fixed multiple of an integer.
+ *
+ * Copyright 2004 by Callum McKenzie
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+/* A lot of this was written by following the sorce for GtkFrame and
+ * GtkAspectFrame. */
+
+// #include <config.h>
+
+#include <gtk/gtk.h>
+#include <glib/gi18n.h>
+
+#include "games-gridframe.h"
+
+enum {
+  PROP_0,
+  PROP_X_PADDING,
+  PROP_Y_PADDING,
+  PROP_WIDTH,
+  PROP_HEIGHT,
+  PROP_X_ALIGN,
+  PROP_Y_ALIGN
+};
+
+G_DEFINE_TYPE (GamesGridFrame, games_grid_frame,GTK_TYPE_BIN)
+
+struct GamesGridFramePrivate {
+  gint xmult;
+  gint ymult;
+
+  gint xpadding;
+  gint ypadding;
+
+  gfloat xalign;
+  gfloat yalign;
+
+  GtkAllocation old_allocation;
+};
+
+void
+games_grid_frame_set (GamesGridFrame * frame, gint newxmult, gint newymult)
+{
+  if (newxmult > 0)
+    frame->priv->xmult = newxmult;
+  if (newymult > 0)
+    frame->priv->ymult = newymult;
+
+  gtk_widget_queue_resize (GTK_WIDGET (frame));
+}
+
+void
+games_grid_frame_set_padding (GamesGridFrame * frame, gint newxpadding,
+                             gint newypadding)
+{
+  if (newxpadding >= 0)
+    frame->priv->xpadding = newxpadding;
+
+  if (newypadding >= 0)
+    frame->priv->ypadding = newypadding;
+
+  gtk_widget_queue_resize (GTK_WIDGET (frame));
+}
+
+
+void
+games_grid_frame_set_alignment (GamesGridFrame * frame, gfloat xalign,
+                               gfloat yalign)
+{
+  if (xalign < 0.0)
+    xalign = 0.0;
+  else if (xalign > 1.0)
+    xalign = 1.0;
+
+  if (yalign < 0.0)
+    yalign = 0.0;
+  else if (yalign > 1.0)
+    yalign = 1.0;
+
+  frame->priv->xalign = xalign;
+  frame->priv->yalign = yalign;
+
+  gtk_widget_queue_resize (GTK_WIDGET (frame));
+}
+
+static void
+games_grid_frame_set_property (GObject * object, guint prop_id,
+                              const GValue * value, GParamSpec * pspec)
+{
+  GamesGridFrame *frame = GAMES_GRID_FRAME (object);
+
+  switch (prop_id) {
+  case PROP_X_PADDING:
+    games_grid_frame_set_padding (frame, g_value_get_int (value), -1);
+    break;
+  case PROP_Y_PADDING:
+    games_grid_frame_set_padding (frame, -1, g_value_get_int (value));
+    break;
+  case PROP_X_ALIGN:
+    games_grid_frame_set_alignment (frame, g_value_get_float (value),
+                                   frame->priv->yalign);
+    break;
+  case PROP_Y_ALIGN:
+    games_grid_frame_set_alignment (frame, frame->priv->xalign,
+                                   g_value_get_float (value));
+    break;
+  case PROP_WIDTH:
+    games_grid_frame_set (frame, g_value_get_int (value), -1);
+    break;
+  case PROP_HEIGHT:
+    games_grid_frame_set (frame, -1, g_value_get_int (value));
+    break;
+  default:
+    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    break;
+  }
+}
+
+static void
+games_grid_frame_get_property (GObject * object, guint prop_id,
+                              GValue * value, GParamSpec * pspec)
+{
+  GamesGridFrame *frame = GAMES_GRID_FRAME (object);
+
+  switch (prop_id) {
+  case PROP_X_PADDING:
+    g_value_set_int (value, frame->priv->xpadding);
+    break;
+  case PROP_Y_PADDING:
+    g_value_set_int (value, frame->priv->ypadding);
+    break;
+  case PROP_X_ALIGN:
+    g_value_set_float (value, frame->priv->xalign);
+    break;
+  case PROP_Y_ALIGN:
+    g_value_set_float (value, frame->priv->yalign);
+    break;
+  case PROP_WIDTH:
+    g_value_set_int (value, frame->priv->xmult);
+    break;
+  case PROP_HEIGHT:
+    g_value_set_int (value, frame->priv->ymult);
+    break;
+  default:
+    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    break;
+  }
+}
+
+static void
+games_grid_frame_size_allocate (GtkWidget * widget,
+                               GtkAllocation * allocation)
+{
+  GamesGridFrame *frame = GAMES_GRID_FRAME (widget);
+  GtkWidget *child = gtk_bin_get_child (GTK_BIN (widget));
+  GtkAllocation child_allocation;
+  gint xsize, ysize, size;
+
+  gtk_widget_set_allocation (widget, allocation);
+
+  xsize = MAX (1, (allocation->width - frame->priv->xpadding) / frame->priv->xmult);
+  ysize = MAX (1, (allocation->height - frame->priv->ypadding) / frame->priv->ymult);
+
+  size = MIN (xsize, ysize);
+
+  child_allocation.width = size * frame->priv->xmult + frame->priv->xpadding;
+  child_allocation.height = size * frame->priv->ymult + frame->priv->ypadding;
+
+  child_allocation.x =
+    (allocation->width - child_allocation.width) * frame->priv->xalign +
+    allocation->x;
+  child_allocation.y =
+    (allocation->height - child_allocation.height) * frame->priv->yalign +
+    allocation->y;
+
+  if (gtk_widget_get_mapped (widget) &&
+      (child_allocation.x != frame->priv->old_allocation.x ||
+       child_allocation.y != frame->priv->old_allocation.y ||
+       child_allocation.width != frame->priv->old_allocation.width ||
+       child_allocation.height != frame->priv->old_allocation.height))
+    gdk_window_invalidate_rect (gtk_widget_get_window (widget), allocation, FALSE);
+
+  if (child && gtk_widget_get_visible (child))
+    gtk_widget_size_allocate (child, &child_allocation);
+
+  frame->priv->old_allocation = child_allocation;
+}
+
+static void
+games_grid_frame_class_init (GamesGridFrameClass * class)
+{
+  GObjectClass *object_class;
+  GtkWidgetClass *widget_class;
+
+  object_class = G_OBJECT_CLASS (class);
+  widget_class = GTK_WIDGET_CLASS (class);
+
+  object_class->set_property = games_grid_frame_set_property;
+  object_class->get_property = games_grid_frame_get_property;
+
+  widget_class->size_allocate = games_grid_frame_size_allocate;
+
+  g_type_class_add_private (object_class, sizeof (GamesGridFramePrivate));
+
+  g_object_class_install_property (object_class, PROP_X_PADDING,
+                                  g_param_spec_int ("x_padding", "X Padding", "X Padding",
+                                                    0, G_MAXINT, 0,
+                                                    G_PARAM_READABLE |
+                                                    G_PARAM_WRITABLE));
+  g_object_class_install_property (object_class, PROP_Y_PADDING,
+                                   g_param_spec_int ("y_padding", "Y Padding", "Y Padding",
+                                                    0, G_MAXINT, 0,
+                                                    G_PARAM_READABLE |
+                                                    G_PARAM_WRITABLE));
+  g_object_class_install_property (object_class, PROP_WIDTH,
+                                   g_param_spec_int ("width_multiple", "Width Multiple", "Width Multiple",
+                                                    1, G_MAXINT, 1,
+                                                    G_PARAM_READABLE |
+                                                    G_PARAM_WRITABLE));
+  g_object_class_install_property (object_class, PROP_HEIGHT,
+                                   g_param_spec_int ("height_multiple", "Height Multiple", "Height Multiple",
+                                                    1, G_MAXINT, 1,
+                                                    G_PARAM_READABLE |
+                                                    G_PARAM_WRITABLE));
+  g_object_class_install_property (object_class, PROP_X_ALIGN,
+                                   g_param_spec_float ("xalign", "X Alignment", "X Alignment",
+                                                      0.0, 1.0, 0.5,
+                                                      G_PARAM_READABLE |
+                                                      G_PARAM_WRITABLE));
+  g_object_class_install_property (object_class, PROP_Y_ALIGN,
+                                   g_param_spec_float ("yalign", "Y Alignment", "Y Alignment",
+                                                      0.0, 1.0, 0.5,
+                                                      G_PARAM_READWRITE |
+                                                      G_PARAM_WRITABLE));
+}
+
+static void
+games_grid_frame_init (GamesGridFrame * frame)
+{
+  frame->priv = G_TYPE_INSTANCE_GET_PRIVATE (frame, GAMES_TYPE_GRID_FRAME, GamesGridFramePrivate);
+
+  frame->priv->xmult = 1;
+  frame->priv->ymult = 1;
+
+  frame->priv->xalign = 0.5;
+  frame->priv->yalign = 0.5;
+}
+
+GtkWidget *
+games_grid_frame_new (gint width, gint height)
+{
+  GamesGridFrame *frame;
+
+  frame = g_object_new (GAMES_TYPE_GRID_FRAME, NULL);
+
+  frame->priv->xmult = MAX (width, 1);
+  frame->priv->ymult = MAX (height, 1);
+
+  return GTK_WIDGET (frame);
+}
+
+/* EOF */
diff --git a/src/games-gridframe.h b/src/games-gridframe.h
new file mode 100644
index 0000000..1a67a9c
--- /dev/null
+++ b/src/games-gridframe.h
@@ -0,0 +1,63 @@
+/* games-gridframe.h: Create a container that guarantees that the internal
+ *                    allocated space is a fixed multiple of an integer.
+ *
+ * Copyright 2004 by Callum McKenzie
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#ifndef GAMES_GRID_FRAME_H
+#define GAMES_GRID_FRAME_H
+
+#include <gtk/gtk.h>
+
+G_BEGIN_DECLS
+
+#define GAMES_TYPE_GRID_FRAME            (games_grid_frame_get_type ())
+#define GAMES_GRID_FRAME(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), GAMES_TYPE_GRID_FRAME, 
GamesGridFrame))
+#define GAMES_GRID_FRAME_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), GAMES_TYPE_GRID_FRAME, 
GamesGridFrameClass))
+#define GAMES_IS_GRID_FRAME(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GAMES_TYPE_GRID_FRAME))
+#define GAMES_IS_GRID_FRAME_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GAMES_TYPE_GRID_FRAME))
+#define GAMES_GRID_FRAME_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), GAMES_TYPE_GRID_FRAME))
+
+typedef struct GamesGridFramePrivate GamesGridFramePrivate;
+
+typedef struct {
+  GtkBin bin;
+  /*< private >*/
+  GamesGridFramePrivate *priv;
+} GamesGridFrame;
+
+typedef struct {
+  GtkBinClass parent;
+} GamesGridFrameClass;
+
+GType      games_grid_frame_get_type      (void);
+GtkWidget *games_grid_frame_new           (gint width,
+                                           gint height);
+void       games_grid_frame_set           (GamesGridFrame * frame,
+                                           gint width,
+                                           gint height);
+void       games_grid_frame_set_padding   (GamesGridFrame * frame,
+                                           gint xpadding,
+                                           gint ypadding);
+void       games_grid_frame_set_alignment (GamesGridFrame * frame,
+                                           gfloat xalign,
+                                           gfloat yalign);
+
+G_END_DECLS
+#endif /* GAMES_GRID_FRAME_H */
+/* EOF */
diff --git a/src/games-gridframe.vapi b/src/games-gridframe.vapi
new file mode 100644
index 0000000..e0e2fd1
--- /dev/null
+++ b/src/games-gridframe.vapi
@@ -0,0 +1,25 @@
+[CCode (cheader_filename = "games-gridframe.h")]
+public class GamesGridFrame : Gtk.Bin {
+    public int xmult;
+    public int ymult;
+    public int xpadding;
+    public int ypadding;
+    public float xalign;
+    public float yalign;
+    public Gtk.Allocation old_allocation;
+    [CCode (cname = "games_grid_frame_new")]
+    public GamesGridFrame (int width, int height);
+    public void set (int width, int height);
+    public void set_padding (int xpadding, int ypadding);
+    public void set_alignment (float xalign, float yalign);
+}
+
+[CCode (cheader_filename = "games-gridframe.h")]
+public enum Prop {
+    X_PADDING,
+    Y_PADDING,
+    WIDTH,
+    HEIGHT,
+    X_ALIGN,
+    Y_ALIGN
+}
diff --git a/src/gnome-nibbles.vala b/src/gnome-nibbles.vala
index 53d2bbb..f2b2ded 100644
--- a/src/gnome-nibbles.vala
+++ b/src/gnome-nibbles.vala
@@ -11,15 +11,44 @@ public class Nibbles : Gtk.Application
 
     private ApplicationWindow window;
     private HeaderBar headerbar;
+    private Stack main_stack;
+    private GamesGridFrame frame;
+
+    private NibblesView? view;
+    private NibblesGame? game = null;
 
     private const GLib.ActionEntry action_entries[] =
     {
+        {"start-game", start_game_cb},
         {"quit", quit}
     };
 
+    private static const OptionEntry[] option_entries =
+    {
+        { "version", 'v', 0, OptionArg.NONE, null,
+        /* Help string for command line --version flag */
+        N_("Show release version"), null},
+
+        { null }
+    };
+
     public Nibbles ()
     {
         Object (application_id: "org.gnome.nibbles", flags: ApplicationFlags.FLAGS_NONE);
+        add_main_option_entries (option_entries);
+    }
+
+    protected override int handle_local_options (GLib.VariantDict options)
+    {
+        if (options.contains ("version"))
+        {
+            /* Not translated so can be easily parsed */
+            stderr.printf ("gnome-nibbles %s\n", VERSION);
+            return Posix.EXIT_SUCCESS;
+        }
+
+        /* Activate */
+        return -1;
     }
 
     protected override void startup ()
@@ -41,6 +70,7 @@ public class Nibbles : Gtk.Application
             window.maximize ();
 
         headerbar = builder.get_object ("headerbar") as HeaderBar;
+        main_stack = builder.get_object ("main_stack") as Stack;
         window.set_titlebar (headerbar);
 
         add_window (window);
@@ -56,10 +86,15 @@ public class Nibbles : Gtk.Application
         settings.set_int ("window-width", window_width);
         settings.set_int ("window-height", window_height);
         settings.set_boolean ("window-is-maximized", is_maximized);
+        game.properties.update_settings (settings);
 
         base.shutdown ();
     }
 
+    /*\
+    * * Window events
+    \*/
+
     private void size_allocate_cb (Allocation allocation)
     {
         if (is_maximized || is_tiled)
@@ -78,8 +113,109 @@ public class Nibbles : Gtk.Application
         return false;
     }
 
+    public bool configure_event_cb (Gdk.EventConfigure event)
+    {
+        int tile_size, ts_x, ts_y;
+        int board_width, board_height;
+
+        /* Compute the new tile size based on the size of the
+         * drawing area, rounded down.
+         */
+        ts_x = event.width / game.width;
+        ts_y = event.height / game.height;
+        if (ts_x * game.width > event.width)
+            ts_x--;
+        if (ts_y * game.height > event.height)
+            ts_y--;
+        tile_size = int.min (ts_x, ts_y);
+
+        if (game.properties.tile_size != tile_size)
+        {
+            board_width = tile_size * game.width;
+            board_height = tile_size * game.height;
+
+            view.stage.set_size (board_width, board_height);
+            view.surface.set_size (board_width, board_height);
+
+            game.properties.tile_size = tile_size;
+        }
+
+        return false;
+    }
+
+    private void start_game_cb ()
+    {
+        start_game ();
+    }
+
+    private void show_new_game_screen ()
+    {
+        main_stack.set_visible_child_name ("start_box");
+    }
+
+    private void show_game_view ()
+    {
+        main_stack.set_visible_child_name ("frame");
+    }
+
+    private void start_game ()
+    {
+        if (game != null)
+        {
+            SignalHandler.disconnect_matched (game, SignalMatchType.DATA, 0, 0, null, null, this);
+        }
+
+        game = new NibblesGame ();
+        game.properties.update_properties (settings);
+
+        view = new NibblesView (game);
+        view.configure_event.connect (configure_event_cb);
+
+        frame = new GamesGridFrame (game.width, game.height);
+        main_stack.add_named (frame, "frame");
+
+        frame.add (view);
+        frame.show_all ();
+
+        show_game_view ();
+    }
+
     public static int main (string[] args)
     {
+        var context = new OptionContext ("");
+
+        context.add_group (Gtk.get_option_group (false));
+        context.add_group (Clutter.get_option_group_without_init ());
+
+        try
+        {
+            context.parse (ref args);
+        }
+        catch (Error e)
+        {
+            stderr.printf ("%s\n", e.message);
+            return Posix.EXIT_FAILURE;
+        }
+
+        Environment.set_application_name (_("Nibbles"));
+
+        try
+        {
+            GtkClutter.init_with_args (ref args, "", new OptionEntry[0], null);
+        }
+        catch (Error e)
+        {
+            var dialog = new Gtk.MessageDialog (null,
+                                                DialogFlags.MODAL,
+                                                MessageType.ERROR,
+                                                ButtonsType.NONE,
+                                                "Unable to initialize Clutter:\n%s", e.message);
+            dialog.set_title (Environment.get_application_name ());
+            dialog.run ();
+            dialog.destroy ();
+            return Posix.EXIT_FAILURE;
+        }
+
         return new Nibbles ().run (args);
     }
 }
diff --git a/src/nibbles-game.vala b/src/nibbles-game.vala
new file mode 100644
index 0000000..914aa92
--- /dev/null
+++ b/src/nibbles-game.vala
@@ -0,0 +1,12 @@
+public class NibblesGame : Object
+{
+    public Properties properties;
+
+    public int width = 92;
+    public int height = 66;
+
+    public NibblesGame ()
+    {
+        properties = new Properties ();
+    }
+}
diff --git a/src/nibbles-view.vala b/src/nibbles-view.vala
new file mode 100644
index 0000000..c66a314
--- /dev/null
+++ b/src/nibbles-view.vala
@@ -0,0 +1,46 @@
+public class NibblesView : GtkClutter.Embed
+{
+    /* Game being played */
+    public NibblesGame game;
+
+    public Clutter.Actor surface;
+    public Clutter.Stage stage;
+
+    public NibblesView (NibblesGame game)
+    {
+        this.game = game;
+
+        stage = get_stage () as Clutter.Stage;
+        Clutter.Color stage_color = { 0x00, 0x00, 0x00, 0xff };
+        stage.set_background_color (stage_color);
+
+        set_size_request (game.properties.tile_size * game.width, game.properties.tile_size * game.height);
+
+        try
+        {
+            var pixbuf = new Gdk.Pixbuf.from_file (Path.build_filename (DATADIR, "pixmaps", 
"wall-small-empty.svg"));
+            surface = new GtkClutter.Texture ();
+            (surface as GtkClutter.Texture).set_from_pixbuf (pixbuf);
+
+            var val = Value (typeof (bool));
+            val.set_boolean (true);
+            surface.set_opacity (100);
+
+            surface.set_property ("repeat-x", val);
+            surface.set_property ("repeat-y", val);
+
+            surface.set_position (0, 0);
+            surface.show ();
+        }
+        catch (Clutter.TextureError e)
+        {
+            warning ("Failed to load textures: %s", e.message);
+        }
+        catch (GLib.Error e)
+        {
+            warning ("Failed to load textures: %s", e.message);
+        }
+
+        stage.add_child (surface);
+    }
+}
diff --git a/src/properties.vala b/src/properties.vala
new file mode 100644
index 0000000..301f2c5
--- /dev/null
+++ b/src/properties.vala
@@ -0,0 +1,19 @@
+public class Properties : Object
+{
+    public int tile_size;
+
+    public Properties ()
+    {
+
+    }
+
+    public void update_settings (GLib.Settings settings)
+    {
+        settings.set_int ("tile-size", tile_size);
+    }
+
+    public void update_properties (GLib.Settings settings)
+    {
+        tile_size = settings.get_int ("tile-size");
+    }
+}


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