[gnome-mines/wip/libgames-scores] Start using libgames-scores



commit bdea1d28814b5c678c495b2861b198572057319b
Author: Michael Catanzaro <mcatanzaro gnome org>
Date:   Wed Apr 16 21:12:41 2014 -0500

    Start using libgames-scores
    
    There are a couple of FIXMEs here:
    
    * We need to figure out how to handle scores in custom games. Mines is
      currently flexible enough to add categories as needed, but
      libgames-scores expects them all up front.
    * We should be using a system-provided vapi, but libgames-scores does
      not yet provide one.
    * The libgames-scores API is not very good yet, since it was designed
      ten years ago for the needs of our C games.

 configure.ac             |    1 +
 src/Makefile.am          |    4 +-
 src/gnome-mines.vala     |   28 ++++----
 src/libgames-scores.vapi |   66 +++++++++++++++++
 src/score-dialog.vala    |  174 ----------------------------------------------
 5 files changed, 84 insertions(+), 189 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index f3a2d95..17adcd0 100644
--- a/configure.ac
+++ b/configure.ac
@@ -23,6 +23,7 @@ PKG_CHECK_MODULES(GNOME_MINES, [
   glib-2.0 >= $GLIB_REQUIRED
   gtk+-3.0 >= $GTK_REQUIRED
   librsvg-2.0 >= $RSVG_REQUIRED
+  libgames-scores
 ])
 
 AC_PATH_PROG([APPDATA_VALIDATE], [appdata-validate], [/bin/true])
diff --git a/src/Makefile.am b/src/Makefile.am
index 166c088..3d0f2b4 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -2,11 +2,11 @@ bin_PROGRAMS = gnome-mines
 
 gnome_mines_SOURCES =  \
        config.vapi     \
+       libgames-scores.vapi \
        gnome-mines.vala \
        history.vala \
        minefield.vala \
-       minefield-view.vala \
-       score-dialog.vala
+       minefield-view.vala
 
 gnome_mines_CFLAGS = \
        -DVERSION=\"$(VERSION)\" \
diff --git a/src/gnome-mines.vala b/src/gnome-mines.vala
index 8422816..3be9a3e 100644
--- a/src/gnome-mines.vala
+++ b/src/gnome-mines.vala
@@ -49,7 +49,14 @@ public class Mines : Gtk.Application
     private bool window_skip_configure;
     
     /* Game history */
-    private History history;
+    private Scores.Scores highscores;
+
+    const Scores.ScoresCategory[] score_categories = {
+      {"8x8", N_("8×8 (10 mines)")},
+      {"16x16", N_("16×16 (40 mines)")},
+      {"30x16", N_("30×16 (99 mines)")},
+      {"custom", N_("FIXME FIXME")},
+    };
 
     /* Minefield being played */
     private Minefield minefield;
@@ -170,8 +177,8 @@ public class Mines : Gtk.Application
         startup_custom_game_screen ();
         view_box.pack_start (custom_game_screen, false, false);
 
-        history = new History (Path.build_filename (Environment.get_user_data_dir (), "gnome-mines", 
"history"));
-        history.load ();
+        Scores.scores_startup ();
+        highscores = new Scores.Scores ("gnome-mines", score_categories, null, null, 0, 
Scores.ScoreStyle.PLAIN_DESCENDING);
 
         buttons_box = new Gtk.Box (Gtk.Orientation.VERTICAL, 4);
         buttons_box.margin_right = 6;
@@ -434,11 +441,10 @@ public class Mines : Gtk.Application
         flag_label.set_text ("%u/%u".printf (minefield.n_flags, minefield.n_mines));
     }
 
-    private int show_scores (HistoryEntry? selected_entry = null, bool show_close = false)
+    private int show_scores ()
     {
-        var dialog = new ScoreDialog (history, selected_entry, show_close);
-        dialog.modal = true;
-        dialog.transient_for = window;
+        var dialog = new Scores.ScoresDialog (window, highscores, _("Best Times"));
+        dialog.set_category_description (_("Size:"));
 
         var result = dialog.run ();
         dialog.destroy ();
@@ -622,13 +628,9 @@ public class Mines : Gtk.Application
 
     private void cleared_cb (Minefield minefield)
     {
-        var date = new DateTime.now_local ();
-        var duration = (uint) (minefield.elapsed + 0.5);
-        var entry = new HistoryEntry (date, minefield.width, minefield.height, minefield.n_mines, duration);
-        history.add (entry);
-        history.save ();
+        highscores.add_time_score (minefield.elapsed / 60.0);
 
-        if (show_scores (entry, true) == Gtk.ResponseType.OK)
+        if (show_scores () == Gtk.ResponseType.OK)
             show_new_game_screen ();
         else
         {
diff --git a/src/libgames-scores.vapi b/src/libgames-scores.vapi
new file mode 100644
index 0000000..07b1ca3
--- /dev/null
+++ b/src/libgames-scores.vapi
@@ -0,0 +1,66 @@
+/* We should probably be using the GIR files, but I can't get them to work in
+ * Vala.  This works for now but means it needs to be updated when the library
+ * changes -- Robert Ancell */
+
+[CCode (cprefix = "Games", lower_case_cprefix = "games_")]
+namespace Scores
+{
+    [CCode (cheader_filename = "games-scores.h")]
+    public void scores_startup ();
+
+    [CCode (cprefix = "GAMES_SCORES_STYLE_", cheader_filename = "games-score.h")]
+    public enum ScoreStyle
+    {
+        PLAIN_DESCENDING,
+        PLAIN_ASCENDING,
+        TIME_DESCENDING,
+        TIME_ASCENDING
+    }
+
+    [CCode (cheader_filename = "games-scores.h", destroy_function = "")]
+    public struct ScoresCategory
+    {
+        string key;
+        string name;
+    }
+
+    [CCode (cheader_filename = "games-score.h")]
+    public class Score : GLib.Object
+    {
+        public Score ();
+    }
+
+    [CCode (cheader_filename = "games-scores.h")]
+    public class Scores : GLib.Object
+    {
+        public Scores (string app_name, ScoresCategory[] categories, string? categories_context, string? 
categories_domain, int default_category_index, ScoreStyle style);
+        public void set_category (string category);
+        public int add_score (Score score);
+        public int add_plain_score (uint32 value);
+        public int add_time_score (double value);
+        public void update_score (string new_name);
+        public void update_score_name (string new_name, string old_name);
+        public ScoreStyle get_style ();
+        public unowned string get_category ();
+        public void add_category (string key, string name);
+    }
+
+    [CCode (cprefix = "GAMES_SCORES_", cheader_filename = "games-scores-dialog.h")]
+    public enum ScoresButtons
+    {
+        CLOSE_BUTTON,
+        NEW_GAME_BUTTON,
+        UNDO_BUTTON,
+        QUIT_BUTTON
+    }
+
+    [CCode (cheader_filename = "games-scores-dialog.h")]
+    public class ScoresDialog : Gtk.Dialog
+    {
+        public ScoresDialog (Gtk.Window parent_window, Scores scores, string title);
+        public void set_category_description (string description);
+        public void set_hilight (uint pos);
+        public void set_message (string? message);
+        public void set_buttons (uint buttons);
+    }
+}


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