[five-or-more/vala-port] add files missing from last commit
- From: Thomas Hindoe Paaboel Andersen <thomashpa src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [five-or-more/vala-port] add files missing from last commit
- Date: Sun, 4 Nov 2012 22:49:33 +0000 (UTC)
commit 5623b2d81fc5aa3bf5730c4d57a14d1dd88e8dbf
Author: Thomas Hindoe Paaboel Andersen <phomes gmail com>
Date: Sun Nov 4 23:49:27 2012 +0100
add files missing from last commit
src/history.vala | 112 +++++++++++++++++++++++++++++++++++
src/score-dialog.vala | 155 +++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 267 insertions(+), 0 deletions(-)
---
diff --git a/src/history.vala b/src/history.vala
new file mode 100644
index 0000000..c3c1c78
--- /dev/null
+++ b/src/history.vala
@@ -0,0 +1,112 @@
+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)
+ {
+ if (!(e is FileError.NOENT))
+ warning ("Failed to load history: %s", e.message);
+ return;
+ }
+
+ 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 n_pieces = int.parse (tokens[4]);
+ var score = int.parse (tokens[5]);
+
+ add (new HistoryEntry (date, width, height, n_colors, n_pieces, score));
+ }
+ }
+
+ public void save ()
+ {
+ var contents = "";
+
+ foreach (var entry in entries)
+ {
+ var line = "%s %u %u %u %u %u\n".printf (entry.date.to_string (), entry.width, entry.height, entry.n_colors, entry.n_pieces, 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 n_pieces;
+ public uint score;
+
+ public HistoryEntry (DateTime date, uint width, uint height, uint n_colors, uint n_pieces, uint score)
+ {
+ this.date = date;
+ this.width = width;
+ this.height = height;
+ this.n_colors = n_colors;
+ this.n_pieces = n_pieces;
+ this.score = score;
+ }
+}
+
diff --git a/src/score-dialog.vala b/src/score-dialog.vala
new file mode 100644
index 0000000..f0cef22
--- /dev/null
+++ b/src/score-dialog.vala
@@ -0,0 +1,155 @@
+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 = ngettext ("%u à %u, %u color", "%u à %u, %u colors", entry.n_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]