gbrainy r318 - trunk/src



Author: jmas
Date: Thu May  8 18:21:59 2008
New Revision: 318
URL: http://svn.gnome.org/viewvc/gbrainy?rev=318&view=rev

Log:
2008-05-08 Jordi Mas <jmas softcatala org>

	* gbrainy.cs: Use new Preference object
	* Preferences.cs: Preferences serialization
	* PreferencesDialog.cs: Use new preferences object
	* Makefile.am: New preferences object
	* gbrainy.glade: OnOk handler



Added:
   trunk/src/Preferences.cs
Modified:
   trunk/src/ChangeLog
   trunk/src/Makefile.am
   trunk/src/Memory.cs
   trunk/src/PreferencesDialog.cs
   trunk/src/gbrainy.cs
   trunk/src/gbrainy.glade

Modified: trunk/src/Makefile.am
==============================================================================
--- trunk/src/Makefile.am	(original)
+++ trunk/src/Makefile.am	Thu May  8 18:21:59 2008
@@ -64,6 +64,7 @@
 	$(srcdir)/PlayerHistory.cs		\
 	$(srcdir)/PlayerHistoryDialog.cs	\
 	$(srcdir)/GtkDialog.cs			\
+	$(srcdir)/Preferences.cs		\
 	$(srcdir)/gbrainy.cs			
 
 ASSEMBLIES = \

Modified: trunk/src/Memory.cs
==============================================================================
--- trunk/src/Memory.cs	(original)
+++ trunk/src/Memory.cs	Thu May  8 18:21:59 2008
@@ -66,7 +66,7 @@
 		draw_timer = false;
 
 		if (App != null)
-			time_left = total_time = App.MemQuestionTime * 10; // Seconds
+			time_left = total_time = gbrainy.preferences.GetIntValue (Preferences.MemQuestionTimeKey) * 10; // Seconds
 	}
 
 	public void StartTimer ()

Added: trunk/src/Preferences.cs
==============================================================================
--- (empty file)
+++ trunk/src/Preferences.cs	Thu May  8 18:21:59 2008
@@ -0,0 +1,171 @@
+/*
+ * Copyright (C) 2008 Jordi Mas i HernÃndez <jmas softcatala org>
+ *
+ * This program 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.
+ *
+ * This program 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.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Xml;
+using System.Xml.Serialization;
+using System.Text;
+
+public class Preferences
+{
+	private string file, config_path;
+	private SerializableDictionary <string, string > properties;
+
+	static public string MemQuestionWarnKey = "MemQuestionWarn";
+	static public string MemQuestionTimeKey = "MemQuestionTime";
+	static public string DifficultyKey = "Difficulty";
+
+	static private string element_item = "item";
+	static private string element_key = "key";
+	static private string element_value = "value";
+	static private string element_collection = "collection";
+
+
+    	public class SerializableDictionary <TKey, TValue> : Dictionary<TKey, TValue>, IXmlSerializable
+	{
+		public System.Xml.Schema.XmlSchema GetSchema ()
+		{
+			return null;
+		}
+
+		public void ReadXml (System.Xml.XmlReader reader)
+		{
+			XmlSerializer key_serializer = new XmlSerializer (typeof (TKey));
+			XmlSerializer value_serializer = new XmlSerializer (typeof (TValue));
+	 		bool wasEmpty = reader.IsEmptyElement;
+
+		    	reader.Read ();
+
+			if (wasEmpty)
+				return;
+			
+			reader.ReadStartElement (element_collection);
+			while (reader.NodeType != System.Xml.XmlNodeType.EndElement)
+			{
+				reader.ReadStartElement (element_item);
+
+				reader.ReadStartElement (element_key);
+				TKey key = (TKey) key_serializer.Deserialize (reader);
+				reader.ReadEndElement ();
+
+				reader.ReadStartElement (element_value);
+				TValue value = (TValue) value_serializer.Deserialize (reader);
+				reader.ReadEndElement();
+
+				this.Add(key, value);
+				reader.ReadEndElement();
+
+				reader.MoveToContent();
+			}
+			reader.ReadEndElement();
+		}
+
+		public void WriteXml (System.Xml.XmlWriter writer)
+		{
+			XmlSerializer key_serializer = new XmlSerializer (typeof(TKey));
+			XmlSerializer value_serializer = new XmlSerializer (typeof(TValue));
+
+			writer.WriteStartElement (element_collection);
+			foreach (TKey key in this.Keys)
+			{
+				writer.WriteStartElement (element_item);
+				writer.WriteStartElement (element_key);
+
+				key_serializer.Serialize (writer, key);
+				writer.WriteEndElement ();
+				writer.WriteStartElement (element_value);
+
+				TValue value = this[key];
+				value_serializer.Serialize (writer, value);
+				writer.WriteEndElement ();
+				writer.WriteEndElement ();
+			}
+			writer.WriteEndElement ();
+		}
+
+    	}
+
+	public Preferences ()
+	{
+		properties = new SerializableDictionary <string, string> ();
+		config_path = Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData);
+		config_path = Path.Combine (config_path, "gbrainy");
+		file = Path.Combine (config_path, "Preferences.xml");
+		Load ();
+	}
+
+	public void Save ()
+	{
+		try {
+			XmlTextWriter writer = new XmlTextWriter (file, Encoding.UTF8);
+			writer.Formatting = Formatting.Indented;
+
+			properties.WriteXml (writer);
+			writer.Close ();
+		}		
+		catch (Exception)
+		{
+		}
+	}
+	
+	public int GetIntValue (string key)
+	{
+		return Int32.Parse (properties [key]);
+	}
+
+	public bool GetBoolValue (string key)
+	{
+		return Boolean.Parse (properties [key]);
+	}
+
+	public void SetIntValue (string key, int value)
+	{
+		properties[key] = value.ToString ();
+	}
+
+	public void SetBoolValue (string key, bool value)
+	{
+		properties [key] = value.ToString ();
+	}
+
+	private void LoadDefaultValues ()
+	{
+		properties.Add (MemQuestionWarnKey, true.ToString ());
+		properties.Add (MemQuestionTimeKey, "4");
+		properties.Add (DifficultyKey, ((int)(Game.Difficulty.Medium)).ToString ());		
+	}
+
+	private void Load ()
+	{
+		try {
+			XmlTextReader reader = new XmlTextReader (file);
+			properties.ReadXml (reader);
+			reader.Close ();
+		}
+		catch (Exception)
+		{
+			LoadDefaultValues ();
+		}
+	}
+	
+}
+
+

Modified: trunk/src/PreferencesDialog.cs
==============================================================================
--- trunk/src/PreferencesDialog.cs	(original)
+++ trunk/src/PreferencesDialog.cs	Thu May  8 18:21:59 2008
@@ -33,19 +33,24 @@
 
 	public PreferencesDialog () : base ("preferences")
 	{
-	}
-	
-	public virtual int MemQuestionTime {
-		get { return prefspinbutton.ValueAsInt;}
-		set { prefspinbutton.Value = value; }
-	}
+		prefspinbutton.Value = gbrainy.preferences.GetIntValue (Preferences.MemQuestionTimeKey);
+		prefcheckbutton.Active = gbrainy.preferences.GetBoolValue (Preferences.MemQuestionWarnKey);
+			
+		switch ((Game.Difficulty) gbrainy.preferences.GetIntValue (Preferences.DifficultyKey)) {
+		case Game.Difficulty.Easy:
+			rb_easy.Active = rb_easy.HasFocus = true;
+			break;		
+		case Game.Difficulty.Medium:
+			rb_medium.Active = rb_medium.HasFocus = true;
+			break;
+		case Game.Difficulty.Master:
+			rb_master.Active = rb_master.HasFocus = true;
+			break;
+		}
 
-	public virtual bool MemQuestionWarn {
-		get { return prefcheckbutton.Active;}
-		set { prefcheckbutton.Active = value;}
 	}
 
-	public virtual Game.Difficulty Difficulty {
+	private Game.Difficulty Difficulty {
 		get {
 			if (rb_easy.Active)
 				return Game.Difficulty.Easy;
@@ -55,18 +60,14 @@
 
 			return Game.Difficulty.Medium;			
 		}
-		set {
-			switch (value) {
-			case Game.Difficulty.Easy:
-				rb_easy.Active = rb_easy.HasFocus = true;
-				break;		
-			case Game.Difficulty.Medium:
-				rb_medium.Active = rb_medium.HasFocus = true;
-				break;
-			case Game.Difficulty.Master:
-				rb_master.Active = rb_master.HasFocus = true;
-				break;
-			}
-		}
+	}
+
+	private void OnOK (object sender, EventArgs args)
+	{
+		gbrainy.preferences.SetIntValue (Preferences.MemQuestionTimeKey, (int) prefspinbutton.Value);
+		gbrainy.preferences.SetBoolValue (Preferences.MemQuestionWarnKey, prefcheckbutton.Active);
+		gbrainy.preferences.SetIntValue (Preferences.DifficultyKey, (int) Difficulty);
+		gbrainy.preferences.Save ();
 	}
 }
+

Modified: trunk/src/gbrainy.cs
==============================================================================
--- trunk/src/gbrainy.cs	(original)
+++ trunk/src/gbrainy.cs	Thu May  8 18:21:59 2008
@@ -44,10 +44,8 @@
 	GameSession session;
 	const int ok_buttonid = -5;
 	ToolButton pause_tbbutton;
-	int memquestion_time = 4;
-	bool memquestion_warn = true;
-	Game.Difficulty difficulty = Game.Difficulty.Medium;
 	public static PlayerHistory history = null;
+	public static Preferences preferences = null;
  
 	public gbrainy (string [] args, params object [] props)
 	: base ("gbrainy", Defines.VERSION, Modules.UI,  args, props)
@@ -112,6 +110,9 @@
 		if (history == null)
 			history = new PlayerHistory ();
 
+		if (preferences == null)
+			preferences = new Preferences ();
+
 		drawing_area = new GameDrawingArea ();
 		drawing_vbox.Add (drawing_area);
 		//app_window.Resize (500, 700);
@@ -123,18 +124,6 @@
 		ActiveInputControls (false);
 		//OnMemoryOnly (this, EventArgs.Empty); // temp
 	}
-	
-	public int MemQuestionTime {
-		get { return memquestion_time;}
-	}
-
-	public bool MemQuestionWarn {
-		get { return memquestion_warn;}
-	}
-	
-	public Game.Difficulty Difficulty {
-		get { return difficulty;}
-	}
 
 	public void UpdateStatusBar ()
 	{
@@ -208,7 +197,7 @@
 		UpdateQuestion (String.Empty);
 		session.NextGame ();
 		
-		if (MemQuestionWarn && session.Type != GameSession.Types.MemoryTrainers && ((session.CurrentGame as Memory)  != null)) {
+		if (preferences.GetBoolValue (Preferences.MemQuestionWarnKey) && session.Type != GameSession.Types.MemoryTrainers && ((session.CurrentGame as Memory)  != null)) {
 			ActiveInputControls (false);
 			drawing_area.OnDrawCountDown (OnNextGameAfterCountDown);
 		}
@@ -342,7 +331,7 @@
 		solution_label.Text = string.Empty;
 		session.Type = GameSession.Types.MemoryTrainers;
 
-		if (MemQuestionWarn)
+		if (preferences.GetBoolValue (Preferences.MemQuestionWarnKey))
 			drawing_area.OnDrawCountDown (OnMemoryOnlyAfterCountDown);
 		else
 			OnNewGame ();
@@ -353,14 +342,8 @@
 		PreferencesDialog dialog;
 
 		dialog = new PreferencesDialog ();
-		dialog.MemQuestionTime = MemQuestionTime;
-		dialog.MemQuestionWarn = MemQuestionWarn;
-		dialog.Difficulty = Difficulty;
 		if (dialog.Run () == ok_buttonid) {
-			memquestion_warn = dialog.MemQuestionWarn;
-			memquestion_time = dialog.MemQuestionTime;
-			difficulty = dialog.Difficulty;
-			session.GameManager.Difficulty = difficulty;
+			session.GameManager.Difficulty = (Game.Difficulty) preferences.GetIntValue (Preferences.DifficultyKey);
 		}
 		dialog.Dialog.Destroy ();
 	}

Modified: trunk/src/gbrainy.glade
==============================================================================
--- trunk/src/gbrainy.glade	(original)
+++ trunk/src/gbrainy.glade	Thu May  8 18:21:59 2008
@@ -881,7 +881,7 @@
 	  </child>
 
 	  <child>
-	    <widget class="GtkButton" id="okbutton1">
+	    <widget class="GtkButton" id="okbutton">
 	      <property name="visible">True</property>
 	      <property name="can_default">True</property>
 	      <property name="can_focus">True</property>
@@ -890,6 +890,7 @@
 	      <property name="relief">GTK_RELIEF_NORMAL</property>
 	      <property name="focus_on_click">True</property>
 	      <property name="response_id">-5</property>
+	      <signal name="clicked" handler="OnOK" last_modification_time="Thu, 08 May 2008 14:12:40 GMT"/>
 	    </widget>
 	  </child>
 	</widget>



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