[Supertux-Commit] r5762 - trunk/supertux-editor/supertux-editor/PropertyEditors

mmlosh at millhouse.dreamhost.com mmlosh at millhouse.dreamhost.com
Mon Sep 1 07:23:40 PDT 2008


Author: mmlosh
Date: 2008-09-01 07:23:39 -0700 (Mon, 01 Sep 2008)
New Revision: 5762

Modified:
   trunk/supertux-editor/supertux-editor/PropertyEditors/ChooseLicenseWidget.cs
   trunk/supertux-editor/supertux-editor/PropertyEditors/ChooseResourceWidget.cs
   trunk/supertux-editor/supertux-editor/PropertyEditors/ChooseSectorWidget.cs
   trunk/supertux-editor/supertux-editor/PropertyEditors/EditScriptWidget.cs
   trunk/supertux-editor/supertux-editor/PropertyEditors/ScriptEditor.cs
Log:
auto-update feature for last custom widgets: ScriptEditor, ChooseResource and ChooseSector


Modified: trunk/supertux-editor/supertux-editor/PropertyEditors/ChooseLicenseWidget.cs
===================================================================
--- trunk/supertux-editor/supertux-editor/PropertyEditors/ChooseLicenseWidget.cs	2008-08-31 19:32:56 UTC (rev 5761)
+++ trunk/supertux-editor/supertux-editor/PropertyEditors/ChooseLicenseWidget.cs	2008-09-01 14:23:39 UTC (rev 5762)
@@ -35,9 +35,7 @@
 
 		comboBox = new ComboBoxEntry(licenseTemplateTexts.ToArray());
 
-		// set current value
-		string val = (string)Field.GetValue(Object);
-		comboBox.Entry.Text = val;
+		OnFieldChanged(Field);	//same code for initialization
 
 		comboBox.Changed += OnComboBoxChanged;
 
@@ -59,17 +57,26 @@
 			if (s == "non-redistributable (forbid sharing and modification of this level)") s = s.Substring(0, s.IndexOf(" ("));
 			if (s == "GPL 2+ / CC-by-sa 3.0 (allow sharing and modification of this level)") s = s.Substring(0, s.IndexOf(" ("));
 
-			PropertyChangeCommand command = new PropertyChangeCommand(
-				"Changed value of " + Field.Name,
-				Field,
-				Object,
-				s);
-			command.Do();
-			UndoManager.AddCommand(command);
+			if (s != (string) Field.GetValue(Object)) {	//no change => no undo item
+				PropertyChangeCommand command = new PropertyChangeCommand(
+					"Changed value of " + Field.Name,
+					Field,
+					Object,
+					s);
+				command.Do();
+				UndoManager.AddCommand(command);
+			}
 		} catch (Exception e) {
 			ErrorDialog.Exception(e);
 		}
 	}
+
+	/// <summary> Called when our data changes, use this for re-loading. </summary>
+	protected override void OnFieldChanged(FieldOrProperty field) {
+		// set current value
+		string val = (string)Field.GetValue(Object);
+		comboBox.Entry.Text = val;		
+	}
 }
 
 [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]

Modified: trunk/supertux-editor/supertux-editor/PropertyEditors/ChooseResourceWidget.cs
===================================================================
--- trunk/supertux-editor/supertux-editor/PropertyEditors/ChooseResourceWidget.cs	2008-08-31 19:32:56 UTC (rev 5761)
+++ trunk/supertux-editor/supertux-editor/PropertyEditors/ChooseResourceWidget.cs	2008-09-01 14:23:39 UTC (rev 5762)
@@ -12,9 +12,7 @@
 	public override Widget Create(object caller) {
 		HBox box = new HBox();
 		entry = new Entry();
-		string val = (string) Field.GetValue(Object);
-		if (val != null)
-			entry.Text = val;
+		OnFieldChanged(Field); //same code for initialization
 
 		entry.FocusOutEvent += OnEntryChangeDone;
 		box.PackStart(entry, true, true, 0);
@@ -74,6 +72,15 @@
 				entry.Text = val;
 		}
 	}
+
+	/// <summary> Called when our data changes, use this for re-loading. </summary>
+	protected override void OnFieldChanged(FieldOrProperty field) {
+		string val = (string) Field.GetValue(Object);
+		if (val != null)
+			entry.Text = val;
+		else
+			entry.Text = "";
+	}
 }
 
 [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property,

Modified: trunk/supertux-editor/supertux-editor/PropertyEditors/ChooseSectorWidget.cs
===================================================================
--- trunk/supertux-editor/supertux-editor/PropertyEditors/ChooseSectorWidget.cs	2008-08-31 19:32:56 UTC (rev 5761)
+++ trunk/supertux-editor/supertux-editor/PropertyEditors/ChooseSectorWidget.cs	2008-09-01 14:23:39 UTC (rev 5762)
@@ -26,6 +26,7 @@
 
 public sealed class ChooseSectorWidget : CustomSettingsWidget {
 	private ComboBox comboBox;
+	private List<String> sectorNames;
 
 	public override Widget Create(object caller) {
 		HBox box = new HBox();
@@ -33,7 +34,7 @@
 		PropertiesView propview = (PropertiesView)caller;
 
 		// Add the names of the sectors to a list
-		List<String> sectorNames = new List<String>(propview.application.CurrentLevel.Sectors.Count);
+		sectorNames = new List<String>(propview.application.CurrentLevel.Sectors.Count);
 		foreach(Sector sector in propview.application.CurrentLevel.Sectors) {
 			sectorNames.Add(sector.Name);
 		}
@@ -41,11 +42,7 @@
 		// Populate a combo box with the sector names
 		comboBox = new ComboBox(sectorNames.ToArray());
 
-		// Get the index of the current value from the original list
-		// due to limitations in ComboBox
-		string val = (string)Field.GetValue(Object);
-		if (val != null)
-			comboBox.Active = sectorNames.IndexOf(val);
+		OnFieldChanged(Field); //same code for initialization
 
 		comboBox.Changed += OnComboBoxChanged;
 		box.PackStart(comboBox, true, true, 0);
@@ -60,17 +57,30 @@
 	private void OnComboBoxChanged(object o, EventArgs args) {
 		try {
 			ComboBox comboBox = (ComboBox)o;
-			PropertyChangeCommand command = new PropertyChangeCommand(
-				"Changed value of " + Field.Name,
-				Field,
-				Object,
-				comboBox.ActiveText);
-			command.Do();
-			UndoManager.AddCommand(command);
+			string newText = comboBox.ActiveText;
+			string oldText = (string) Field.GetValue(Object);
+			if (newText != oldText) {
+				PropertyChangeCommand command = new PropertyChangeCommand(
+					"Changed value of " + Field.Name,
+					Field,
+					Object,
+					newText);
+				command.Do();
+				UndoManager.AddCommand(command);
+			}
 		} catch (Exception e) {
 			ErrorDialog.Exception(e);
 		}
 	}
+
+	/// <summary> Called when our data changes, use this for re-loading. </summary>
+	protected override void OnFieldChanged(FieldOrProperty field) {
+		// Get the index of the current value from the original list
+		// due to limitations in ComboBox
+		string val = (string)Field.GetValue(Object);
+		if (val != null)
+			comboBox.Active = sectorNames.IndexOf(val);
+	}
 }
 
 [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property,

Modified: trunk/supertux-editor/supertux-editor/PropertyEditors/EditScriptWidget.cs
===================================================================
--- trunk/supertux-editor/supertux-editor/PropertyEditors/EditScriptWidget.cs	2008-08-31 19:32:56 UTC (rev 5761)
+++ trunk/supertux-editor/supertux-editor/PropertyEditors/EditScriptWidget.cs	2008-09-01 14:23:39 UTC (rev 5762)
@@ -22,7 +22,7 @@
 
 	private void OnEdit(object sender, EventArgs args)
 	{
-		new ScriptEditor(Object.GetType() + ":" + Field.Name, Field, Object);
+		new ScriptEditor(Object.GetType() + " : " + Field.Name, Field, Object);
 	}
 }
 

Modified: trunk/supertux-editor/supertux-editor/PropertyEditors/ScriptEditor.cs
===================================================================
--- trunk/supertux-editor/supertux-editor/PropertyEditors/ScriptEditor.cs	2008-08-31 19:32:56 UTC (rev 5761)
+++ trunk/supertux-editor/supertux-editor/PropertyEditors/ScriptEditor.cs	2008-09-01 14:23:39 UTC (rev 5762)
@@ -41,8 +41,9 @@
 		window.SetSizeRequest(640, 500);
 		*/
 
-		object val = field.GetValue(object_);
-		scriptEditor.Buffer.Text = val != null ? val.ToString() : String.Empty;
+		scriptDialog.Title = title + "  -  " + scriptDialog.Title;
+		OnFieldChanged(object_, field, null);	//same code as for initialization
+		field.Changed += OnFieldChanged;
 
 		scriptDialog.ShowAll();
 	}
@@ -61,12 +62,21 @@
 			ErrorDialog.Exception(e);
 		}
 
+		field.Changed -= OnFieldChanged;
 		scriptDialog.Hide();
 	}
 
 	protected void OnCancel(object o, EventArgs args)
 	{
+		field.Changed -= OnFieldChanged;
 		scriptDialog.Hide();
 	}
 
+	/// <summary> Called when our field changes on any instance of same type as our Object. </summary>
+	private void OnFieldChanged(object Object, FieldOrProperty field, object oldValue) {
+		if (object_ == Object) {
+			object val = field.GetValue(object_);
+			scriptEditor.Buffer.Text = val != null ? val.ToString() : String.Empty;
+		}
+	}
 }



More information about the Supertux-Commit mailing list