[gnome-games] swell-foop: Stop using GamesScores
- From: Robert Ancell <rancell src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-games] swell-foop: Stop using GamesScores
- Date: Wed, 26 Sep 2012 23:41:41 +0000 (UTC)
commit 8ef493a0653d319ff78bb44c41c8eab3c034dc42
Author: Robert Ancell <robert ancell canonical com>
Date: Thu Sep 27 11:41:30 2012 +1200
swell-foop: Stop using GamesScores
swell-foop/data/Makefile.am | 10 --
swell-foop/src/Makefile.am | 12 +--
swell-foop/src/history.vala | 105 ++++++++++++++++++++
swell-foop/src/swell-foop.vala | 214 ++++++++++++++++++++++++++++++++++------
4 files changed, 291 insertions(+), 50 deletions(-)
---
diff --git a/swell-foop/data/Makefile.am b/swell-foop/data/Makefile.am
index 9245e58..ea57cba 100644
--- a/swell-foop/data/Makefile.am
+++ b/swell-foop/data/Makefile.am
@@ -29,14 +29,4 @@ DISTCLEANFILES = \
@INTLTOOL_SCHEMAS_RULE@
-install-scorefiles-local:
- -$(mkinstalldirs) $(DESTDIR)$(scoredir)
- -for i in small normal large; do \
- touch $(DESTDIR)$(scoredir)/swell-foop.$$i.scores; \
- chown $(scores_user):$(scores_group) $(DESTDIR)$(scoredir)/swell-foop.$$i.scores; \
- chmod 664 $(DESTDIR)$(scoredir)/swell-foop.$$i.scores; \
- done
-
-install-data-local: install-scorefiles-local
-
-include $(top_srcdir)/git.mk
diff --git a/swell-foop/src/Makefile.am b/swell-foop/src/Makefile.am
index a6d6f2a..385a54c 100644
--- a/swell-foop/src/Makefile.am
+++ b/swell-foop/src/Makefile.am
@@ -3,18 +3,16 @@ bin_PROGRAMS = swell-foop
swell_foop_SOURCES = \
game-view.vala \
game.vala \
+ history.vala \
config.vapi \
swell-foop.vala
swell_foop_VALAFLAGS = \
--pkg posix \
--pkg gmodule-2.0 \
- --pkg clutter-gtk-1.0 \
- --vapidir $(top_srcdir)/libgames-support \
- --pkg GnomeGamesSupport-1.0
+ --pkg clutter-gtk-1.0
swell_foop_CFLAGS = \
- -I$(top_srcdir)/libgames-support \
-DVERSION=\"$(VERSION)\" \
-DGETTEXT_PACKAGE=\"$(GETTEXT_PACKAGE)\" \
-DDATADIR=\""$(datadir)/swell-foop"\" \
@@ -25,16 +23,10 @@ swell_foop_CFLAGS = \
$(CLUTTER_GTK_CFLAGS)
swell_foop_LDADD = \
- $(top_builddir)/libgames-support/libgames-support.la \
$(GTHREAD_LIBS) \
$(GMODULE_LIBS) \
$(GTK_LIBS) \
$(CLUTTER_GTK_LIBS) \
$(INTLLIBS)
-install-exec-hook:
- -if test "$(setgid)" = "true"; then \
- chgrp $(scores_group) $(DESTDIR)$(bindir)/swell-foop && chmod 2555 $(DESTDIR)$(bindir)/swell-foop ;\
- fi
-
-include $(top_srcdir)/git.mk
diff --git a/swell-foop/src/history.vala b/swell-foop/src/history.vala
new file mode 100644
index 0000000..2f317d0
--- /dev/null
+++ b/swell-foop/src/history.vala
@@ -0,0 +1,105 @@
+public class History
+{
+ public string filename;
+ public List<HistoryEntry> entries;
+
+ public signal void entry_added (HistoryEntry entry);
+
+ public History (string filename)
+ {
+ this.filename = filename;
+ entries = new List<HistoryEntry> ();
+ }
+
+ public void add (HistoryEntry entry)
+ {
+ entries.append (entry);
+ entry_added (entry);
+ }
+
+ public void load ()
+ {
+ entries = new List<HistoryEntry> ();
+
+ var contents = "";
+ try
+ {
+ FileUtils.get_contents (filename, out contents);
+ }
+ catch (FileError e)
+ {
+ }
+
+ foreach (var line in contents.split ("\n"))
+ {
+ var tokens = line.split (" ");
+ if (tokens.length != 5)
+ continue;
+
+ var date = parse_date (tokens[0]);
+ if (date == null)
+ continue;
+ var width = int.parse (tokens[1]);
+ var height = int.parse (tokens[2]);
+ var n_colors = int.parse (tokens[3]);
+ var score = int.parse (tokens[4]);
+
+ add (new HistoryEntry (date, width, height, n_colors, score));
+ }
+ }
+
+ public void save ()
+ {
+ var contents = "";
+
+ foreach (var entry in entries)
+ {
+ var line = "%s %u %u %u %u\n".printf (entry.date.to_string (), entry.width, entry.height, entry.n_colors, entry.score);
+ contents += line;
+ }
+
+ try
+ {
+ DirUtils.create_with_parents (Path.get_dirname (filename), 0775);
+ FileUtils.set_contents (filename, contents);
+ }
+ catch (FileError e)
+ {
+ warning ("Failed to save history: %s", e.message);
+ }
+ }
+
+ private DateTime? parse_date (string date)
+ {
+ if (date.length < 19 || date[4] != '-' || date[7] != '-' || date[10] != 'T' || date[13] != ':' || date[16] != ':')
+ return null;
+
+ var year = int.parse (date.substring (0, 4));
+ var month = int.parse (date.substring (5, 2));
+ var day = int.parse (date.substring (8, 2));
+ var hour = int.parse (date.substring (11, 2));
+ var minute = int.parse (date.substring (14, 2));
+ var seconds = int.parse (date.substring (17, 2));
+ var timezone = date.substring (19);
+
+ return new DateTime (new TimeZone (timezone), year, month, day, hour, minute, seconds);
+ }
+}
+
+public class HistoryEntry
+{
+ public DateTime date;
+ public uint width;
+ public uint height;
+ public uint n_colors;
+ public uint score;
+
+ public HistoryEntry (DateTime date, uint width, uint height, uint n_colors, uint score)
+ {
+ this.date = date;
+ this.width = width;
+ this.height = height;
+ this.n_colors = n_colors;
+ this.score = score;
+ }
+}
diff --git a/swell-foop/src/swell-foop.vala b/swell-foop/src/swell-foop.vala
index b4bb76e..d957b29 100644
--- a/swell-foop/src/swell-foop.vala
+++ b/swell-foop/src/swell-foop.vala
@@ -4,7 +4,10 @@ public class SwellFoop : Gtk.Application
private Settings settings;
/* Main window */
- private Gtk.Window main_window;
+ private Gtk.Window window;
+
+ /* Game history */
+ private History history;
/* Game being played */
private Game? game = null;
@@ -15,11 +18,9 @@ public class SwellFoop : Gtk.Application
private Clutter.Stage stage;
private GtkClutter.Embed clutter_embed;
- private GnomeGamesSupport.Scores high_scores;
-
private Gtk.Dialog? preferences_dialog = null;
- private Gtk.Label current_score_label;
+ private Gtk.Label current_score_label;
/* Store size options */
public Size[] sizes;
@@ -50,13 +51,13 @@ public class SwellFoop : Gtk.Application
add_accelerator ("<Primary>n", "app.new-game", null);
/* Create the main window */
- main_window = new Gtk.ApplicationWindow (this);
- main_window.set_title (_("Swell Foop"));
- main_window.resizable = false;
+ window = new Gtk.ApplicationWindow (this);
+ window.set_title (_("Swell Foop"));
+ window.resizable = false;
var vbox = new Gtk.Box (Gtk.Orientation.VERTICAL, 0);
vbox.show ();
- main_window.add (vbox);
+ window.add (vbox);
/* Create the menus */
var menu = new Menu ();
@@ -142,16 +143,11 @@ public class SwellFoop : Gtk.Application
/* When the mouse leaves the window we need to update the view */
clutter_embed.leave_notify_event.connect (view.board_left_cb);
- high_scores = new GnomeGamesSupport.Scores ("swell-foop",
- new GnomeGamesSupport.ScoresCategory[0],
- null, null, 0,
- GnomeGamesSupport.ScoreStyle.PLAIN_DESCENDING);
- high_scores.set_category (settings.get_string ("size"));
- high_scores.add_category ("small", _("Small"));
- high_scores.add_category ("normal", _("Normal"));
- high_scores.add_category ("large", _("Large"));
stage.key_release_event.connect (key_release_event_cb);
+
+ history = new History (Path.build_filename (Environment.get_user_data_dir (), "swell-foop", "history"));
+ history.load ();
}
private bool key_release_event_cb (Clutter.Actor actor, Clutter.KeyEvent event)
@@ -207,7 +203,10 @@ public class SwellFoop : Gtk.Application
private void complete_cb ()
{
- high_scores.add_plain_score (game.score);
+ var date = new DateTime.now_local ();
+ var entry = new HistoryEntry (date, game.columns, game.rows, game.color_num, game.score);
+ history.add (entry);
+ history.save ();
}
protected override void shutdown ()
@@ -216,12 +215,12 @@ public class SwellFoop : Gtk.Application
/* Record the score if the game isn't over. */
if (game != null && !game.has_completed() && game.score > 0)
- high_scores.add_plain_score (game.score);
+ complete_cb ();
}
protected override void activate ()
{
- main_window.present ();
+ window.present ();
}
public void preferences_cb ()
@@ -317,7 +316,6 @@ public class SwellFoop : Gtk.Application
return;
settings.set_string ("size", new_size);
- high_scores.set_category (new_size);
new_game ();
}
@@ -341,7 +339,7 @@ public class SwellFoop : Gtk.Application
public void show ()
{
- main_window.show ();
+ window.show ();
}
private void new_game_cb ()
@@ -351,22 +349,24 @@ public class SwellFoop : Gtk.Application
private void scores_cb ()
{
- var scores_dialog = new GnomeGamesSupport.ScoresDialog (main_window, high_scores, _("Swell Foop Scores"));
- scores_dialog.set_category_description (_("Size:"));
- scores_dialog.run ();
- scores_dialog.destroy ();
+ var dialog = new ScoreDialog (history);
+ dialog.modal = true;
+ dialog.transient_for = window;
+
+ dialog.run ();
+ dialog.destroy ();
}
private void quit_cb ()
{
- main_window.destroy ();
+ window.destroy ();
}
private void help_cb ()
{
try
{
- Gtk.show_uri (main_window.get_screen (), "help:swell-foop", Gtk.get_current_event_time ());
+ Gtk.show_uri (window.get_screen (), "help:swell-foop", Gtk.get_current_event_time ());
}
catch (Error e)
{
@@ -382,7 +382,7 @@ public class SwellFoop : Gtk.Application
var license = "Swell Foop is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.\n\nSwell Foop 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 General Public License for more details.\n\nYou should have received a copy of the GNU General Public License along with Swell Foop; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA";
- Gtk.show_about_dialog (main_window,
+ Gtk.show_about_dialog (window,
"program-name", _("Swell Foop"),
"version", VERSION,
"comments",
@@ -424,8 +424,6 @@ public class SwellFoop : Gtk.Application
Intl.bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
Intl.textdomain (GETTEXT_PACKAGE);
- GnomeGamesSupport.scores_startup ();
-
if (GtkClutter.init (ref args) != Clutter.InitError.SUCCESS)
{
warning ("Failed to initialise Clutter");
@@ -465,3 +463,159 @@ public struct Size
public int columns;
public int rows;
}
+
+public class ScoreDialog : Gtk.Dialog
+{
+ private History history;
+ private HistoryEntry? selected_entry = null;
+ private Gtk.ListStore size_model;
+ private Gtk.ListStore score_model;
+ private Gtk.ComboBox size_combo;
+
+ public ScoreDialog (History history, HistoryEntry? selected_entry = null, bool show_quit = false)
+ {
+ this.history = history;
+ history.entry_added.connect (entry_added_cb);
+ this.selected_entry = selected_entry;
+
+ if (show_quit)
+ {
+ add_button (Gtk.Stock.QUIT, Gtk.ResponseType.CLOSE);
+ add_button (_("New Game"), Gtk.ResponseType.OK);
+ }
+ else
+ add_button (Gtk.Stock.OK, Gtk.ResponseType.DELETE_EVENT);
+ set_size_request (200, 300);
+
+ var vbox = new Gtk.Box (Gtk.Orientation.VERTICAL, 5);
+ vbox.border_width = 6;
+ vbox.show ();
+ get_content_area ().pack_start (vbox, true, true, 0);
+
+ var hbox = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 6);
+ hbox.show ();
+ vbox.pack_start (hbox, false, false, 0);
+
+ var label = new Gtk.Label (_("Size:"));
+ label.show ();
+ hbox.pack_start (label, false, false, 0);
+
+ size_model = new Gtk.ListStore (4, typeof (string), typeof (int), typeof (int), typeof (int));
+
+ size_combo = new Gtk.ComboBox ();
+ size_combo.changed.connect (size_changed_cb);
+ size_combo.model = size_model;
+ var renderer = new Gtk.CellRendererText ();
+ size_combo.pack_start (renderer, true);
+ size_combo.add_attribute (renderer, "text", 0);
+ size_combo.show ();
+ hbox.pack_start (size_combo, true, true, 0);
+
+ var scroll = new Gtk.ScrolledWindow (null, null);
+ scroll.shadow_type = Gtk.ShadowType.ETCHED_IN;
+ scroll.set_policy (Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC);
+ scroll.show ();
+ vbox.pack_start (scroll, true, true, 0);
+
+ score_model = new Gtk.ListStore (3, typeof (string), typeof (string), typeof (int));
+
+ var scores = new Gtk.TreeView ();
+ renderer = new Gtk.CellRendererText ();
+ scores.insert_column_with_attributes (-1, _("Date"), renderer, "text", 0, "weight", 2);
+ renderer = new Gtk.CellRendererText ();
+ renderer.xalign = 1.0f;
+ scores.insert_column_with_attributes (-1, _("Score"), renderer, "text", 1, "weight", 2);
+ scores.model = score_model;
+ scores.show ();
+ scroll.add (scores);
+
+ foreach (var entry in history.entries)
+ entry_added_cb (entry);
+ }
+
+ public void set_size (uint width, uint height, uint n_colors)
+ {
+ score_model.clear ();
+
+ var entries = history.entries.copy ();
+ entries.sort (compare_entries);
+
+ foreach (var entry in entries)
+ {
+ if (entry.width != width || entry.height != height || entry.n_colors != n_colors)
+ continue;
+
+ var date_label = entry.date.format ("%d/%m/%Y");
+
+ var score_label = "%u".printf (entry.score);
+
+ int weight = Pango.Weight.NORMAL;
+ if (entry == selected_entry)
+ weight = Pango.Weight.BOLD;
+
+ Gtk.TreeIter iter;
+ score_model.append (out iter);
+ score_model.set (iter, 0, date_label, 1, score_label, 2, weight);
+ }
+ }
+
+ private static int compare_entries (HistoryEntry a, HistoryEntry b)
+ {
+ if (a.width != b.width)
+ return (int) a.width - (int) b.width;
+ if (a.height != b.height)
+ return (int) a.height - (int) b.height;
+ if (a.n_colors != b.n_colors)
+ return (int) a.n_colors - (int) b.n_colors;
+ if (a.score != b.score)
+ return (int) a.score - (int) b.score;
+ return a.date.compare (b.date);
+ }
+
+ private void size_changed_cb (Gtk.ComboBox combo)
+ {
+ Gtk.TreeIter iter;
+ if (!combo.get_active_iter (out iter))
+ return;
+
+ int width, height, n_colors;
+ combo.model.get (iter, 1, out width, 2, out height, 3, out n_colors);
+ set_size ((uint) width, (uint) height, (uint) n_colors);
+ }
+
+ private void entry_added_cb (HistoryEntry entry)
+ {
+ /* Ignore if already have an entry for this */
+ Gtk.TreeIter iter;
+ var have_size_entry = false;
+ if (size_model.get_iter_first (out iter))
+ {
+ do
+ {
+ int width, height, n_colors;
+ size_model.get (iter, 1, out width, 2, out height, 3, out n_colors);
+ if (width == entry.width && height == entry.height && n_colors == entry.n_colors)
+ {
+ have_size_entry = true;
+ break;
+ }
+ } while (size_model.iter_next (ref iter));
+ }
+
+ if (!have_size_entry)
+ {
+ var label = _("%u à %u, %u colors").printf (entry.width, entry.height, entry.n_colors);
+
+ size_model.append (out iter);
+ size_model.set (iter, 0, label, 1, entry.width, 2, entry.height, 3, entry.n_colors);
+
+ /* Select this entry if don't have any */
+ if (size_combo.get_active () == -1)
+ size_combo.set_active_iter (iter);
+
+ /* Select this entry if the same category as the selected one */
+ if (selected_entry != null && entry.width == selected_entry.width && entry.height == selected_entry.height && entry.n_colors == selected_entry.n_colors)
+ size_combo.set_active_iter (iter);
+ }
+ }
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]