mikymod 11 лет назад
Родитель
Сommit
4b8adf035a

+ 22 - 26
tools/gui/UnitEditor/MainMenu.cs

@@ -15,14 +15,8 @@ namespace UnitEditor
 	{
 		public Gtk.UIManager uim = null;
 		public Gtk.MenuBar instance;
-		/*
-         The first required piece is the XML definition.
-
-         The following is a hard-coded definition of a Gtk.MenuBar control (widget).
-         However, automation can come into play here by constructing the inner-XElements and building out.
-         Consider using a string formatting function to take a name in a list like "Zoom In", using that as-is
-         for the name of the Gtk.MenuItem, and using it again (after removing spaces) for the action name.
-     	*/
+
+		//------------------------------------------------------------------------------
 		private XDocument UI = new XDocument (
 			new XElement ("ui",
 				new XElement ("menubar",
@@ -65,6 +59,7 @@ namespace UnitEditor
 			)
 		);
 
+		//------------------------------------------------------------------------------
 		private Dictionary<string, string> keys = new Dictionary<string, string> () {
 			{"New", "<Control>n"},
 			{"Open", "<Control>o"},
@@ -91,6 +86,7 @@ namespace UnitEditor
 			{"GoToLine", "<Control>g"}
 		};
 
+		//------------------------------------------------------------------------------
 		public Gtk.ActionEntry[] getActionEntries ()
 		{
 			return new Gtk.ActionEntry[] {
@@ -102,7 +98,7 @@ namespace UnitEditor
 				new Gtk.ActionEntry ("SaveAll", null, "Save All", (keys.ContainsKey ("SaveAll")) ? keys ["SaveAll"] : null, null, null),
 				new Gtk.ActionEntry ("Close", null, "Close", (keys.ContainsKey ("Close")) ? keys ["Close"] : null, null, null),
 				new Gtk.ActionEntry ("CloseAll", null, "Close All", (keys.ContainsKey ("CloseAll")) ? keys ["CloseAll"] : null, null, null),
-				new Gtk.ActionEntry ("Quit", null, "Quit", (keys.ContainsKey ("Quit")) ? keys ["Quit"] : null, null, exit_cb),
+				new Gtk.ActionEntry ("Quit", null, "Quit", (keys.ContainsKey ("Quit")) ? keys ["Quit"] : null, null, (EventHandler)quit_cb),
 
 				new Gtk.ActionEntry ("EditMenu", null, "_Edit", null, null, null),
 				new Gtk.ActionEntry ("Undo", null, "Undo", (keys.ContainsKey ("Undo")) ? keys ["Undo"] : null, null, null),
@@ -125,32 +121,27 @@ namespace UnitEditor
 			};
 		}
 
+		//------------------------------------------------------------------------------
 		public MainMenu ()
 		{
-			Build ();
-			// Create a random string to attach to our base action group name (see WARNING).
-			Guid guid = Guid.NewGuid ();
-			// Recreate the UIManager that links the action entries with controls (widgets) created from an XML definition.
 			uim = new Gtk.UIManager ();
 
-			Gtk.ActionGroup actions = new Gtk.ActionGroup ("MenuBarActions" + guid.ToString ());
-			// Get all the action entries (that is, actions for menu items, toggled menu items, radio menu items).
+			Gtk.ActionGroup actions = new Gtk.ActionGroup ("MenuBarActions" + Guid.NewGuid ());
+
 			actions.Add (getActionEntries ());
-			// Put the action entry definitions into the UIManager's buffer.
 			uim.InsertActionGroup (actions, 0);
+	
 			// Put the XML definition of the controls (widgets) into the UIManager's buffer -and- create controls (widgets).
 			uim.AddUiFromString (UI.ToString ());
-			// Get the menubar created in the previous space.
+
 			instance = (Gtk.MenuBar)uim.GetWidget ("/menubar");
-			// Mark everything on the menubar to be shown.
 			instance.ShowAll ();
 		}
 
-		static void exit_cb (object o, EventArgs args)
-		{
-			Application.Quit ();
-		}
+		//------------------------------------------------------------------------------
+		static void new_cb(object sender, DeleteEventArgs args) {}
 
+		//------------------------------------------------------------------------------
 		static void open_cb(object sender, System.EventArgs args)
 		{
 			Gtk.FileChooserDialog fc = new Gtk.FileChooserDialog("Open file", null, FileChooserAction.Open);
@@ -166,14 +157,19 @@ namespace UnitEditor
 			{
 				UnitEditor.MainClass.g_win.open_unit (fc.Filename);
 			}
-
-			//Don't forget to call Destroy() or the FileChooserDialog window won't get closed.
 			fc.Destroy();
 		}
 
-		static void save_cb(object sender, DeleteEventArgs args)
+		//------------------------------------------------------------------------------
+		static void save_cb(object sender, DeleteEventArgs args) {}
+		static void saveas_cb(object sender, DeleteEventArgs args) {}
+		static void saveall_cb(object sender, DeleteEventArgs args) {}
+		static void close_cb(object sender, DeleteEventArgs args) {}
+		static void closeall_cb(object sender, DeleteEventArgs args) {}
+	
+		static void quit_cb (object o, EventArgs args)
 		{
-
+			Application.Quit ();
 		}
 	}
 }

+ 9 - 6
tools/gui/UnitEditor/MainWindow.cs

@@ -7,7 +7,8 @@ using UnitEditor;
 public partial class MainWindow: Gtk.Window
 {
 	private VBox box;
-	private UnitForm unb = null;
+	private Notebook nb;
+	private UnitForm uf = null;
 
 	public MainWindow () : base (Gtk.WindowType.Toplevel)
 	{
@@ -20,16 +21,18 @@ public partial class MainWindow: Gtk.Window
 		this.AddAccelGroup (mb.uim.AccelGroup);
 		box.PackStart(mb.instance, false, false, 0);
 
-		MaterialFile mf = new MaterialFile ("/home/mikymod/samples/doodles/opensans.material");
-		mf.serialize ();
+		nb = new Notebook ();
+		box.PackStart (nb, false, false, 0);
+
 		ShowAll ();
 	}
 
 	public void open_unit(string file_name)
 	{
-		unb = new UnitEditor.UnitForm (file_name);
-		box.PackStart (unb.instance, false, false, 0);
-		box.ShowAll ();
+		uf = new UnitEditor.UnitForm (file_name);
+		nb.AppendPage (uf.instance, new Label (file_name));
+
+		ShowAll ();
 	}
 
 	protected void OnDeleteEvent (object sender, DeleteEventArgs a)

+ 63 - 39
tools/gui/UnitEditor/UnitFile.cs

@@ -5,78 +5,102 @@ using System.IO;
 using System.Text;
 using System.Collections.Generic;
 using System.Linq;
+using System.Diagnostics;
 
 namespace UnitEditor
 {
 	public class UnitFile
 	{
-		private JObject m_root;
-		private Dictionary<string, Renderable> m_renderables;
-		private Dictionary<string, Node> m_nodes;
+		private string m_filename;
 
+		private List<string> m_renderables_names;
+		private List<Renderable> m_renderables;
+
+		private List<string> m_nodes_names;
+		private List<Node> m_nodes;
+
+		//------------------------------------------------------------------------------
 		public UnitFile (string file_name)
 		{
-			string json_string = string.Empty;
+			Debug.Assert(File.Exists (file_name));
+			m_filename = file_name;
 
+			// Parse json file
+			string json_string = string.Empty;
+			JObject m_root;
 			using (StreamReader streamReader = new StreamReader(file_name))
 			{            
 				json_string = streamReader.ReadToEnd();
 				m_root = JObject.Parse (json_string);
 			}
 
-			deserialize ();
+			// Deserialize renderables
+			JToken renderables_token = m_root ["renderables"];
+			Dictionary<string, Renderable> renderables = JsonConvert.DeserializeObject<Dictionary<string, Renderable>>(renderables_token.ToString());
+			m_renderables_names = new List<string> (renderables.Keys.ToArray ());
+			m_renderables = new List<Renderable> (renderables.Values.ToArray ());
+
+			// Deserialize Nodes
+			JToken nodes_token = m_root ["nodes"];
+			Dictionary<string, Node> nodes = JsonConvert.DeserializeObject<Dictionary<string, Node>> (nodes_token.ToString ());
+			m_nodes_names = new List<string> (nodes.Keys.ToArray ());
+			m_nodes = new List<Node> (nodes.Values.ToArray ());
 		}
 
+		//------------------------------------------------------------------------------
 		public string[] renderables_names()
 		{
-			return m_renderables.Keys.ToArray ();
+			return m_renderables_names.ToArray ();
 		}
 
+		//------------------------------------------------------------------------------
 		public Renderable[] renderables()
 		{
-			return m_renderables.Values.ToArray ();
+			return m_renderables.ToArray ();
 		}
 
-		public void deserialize()
+		//------------------------------------------------------------------------------
+		public string[] nodes_name()
 		{
-			// Deserialize renderables
-			JToken renderables_token = m_root ["renderables"];
-			m_renderables = JsonConvert.DeserializeObject<Dictionary<string, Renderable>>(renderables_token.ToString());
-
-			// Deserialize Nodes
-			JToken nodes_token = m_root ["nodes"];
-			m_nodes = JsonConvert.DeserializeObject<Dictionary<string, Node>> (nodes_token.ToString ());
+			return m_nodes_names.ToArray ();
 		}
 
-		public void serialize()
+		//------------------------------------------------------------------------------
+		public Node[] nodes()
 		{
-			string json_string = "{";
-			json_string += "\"renderables\": {";
+			return m_nodes.ToArray ();
+		}
 
-			string last = m_renderables.Keys.Last ();
-			foreach (var r in m_renderables)
-			{
-				var k = r.Key;
-				var v = r.Value;
-				string renderables_token = string.Format ("\t\"{0}\" : {1}", k, JsonConvert.SerializeObject(v));
-				if (k != last)
-					json_string += ",";
-				json_string += renderables_token;
-			}
-			json_string += "},\n";
 
-			json_string += "\"nodes\": {";
-			foreach (var n in m_nodes)
+		//------------------------------------------------------------------------------
+		public void save()
+		{
+			using (StreamWriter writer = new StreamWriter (m_filename))
 			{
-				var k = n.Key;
-				var v = n.Value;
-				string nodes_token = string.Format ("\t\"{0}\" : {1}", k, JsonConvert.SerializeObject(v));
-				json_string += nodes_token;
-			}
-			json_string += "}";
-			json_string += "}";
+				string json_string = "{";
+				json_string += "\"renderables\": {";
+
+				for (int i = 0; i < m_renderables.Count; i++)
+				{
+					string renderables_token = string.Format ("\t\"{0}\" : {1}", m_renderables_names[i], JsonConvert.SerializeObject(m_renderables[i]));
+					if (i < m_renderables.Count - 1)
+						json_string += ",";
+
+					json_string += renderables_token;
+				}
+				json_string += "},\n";
 
-			Console.Write (json_string);
+				json_string += "\"nodes\": {";
+				for (int i = 0; i < m_nodes.Count; i++)
+				{
+					string nodes_token = string.Format ("\t\"{0}\" : {1}", m_nodes_names[i], JsonConvert.SerializeObject(m_nodes[i]));
+					json_string += nodes_token;
+				}
+				json_string += "}";
+				json_string += "}";
+
+				writer.Write (json_string);
+			}
 		}
 	}
 }

+ 3 - 3
tools/gui/UnitEditor/UnitForm.cs

@@ -9,12 +9,12 @@ namespace UnitEditor
 	{
 		public Notebook instance;
 
-
-		public UnitForm (string unit_file_name)
+		//------------------------------------------------------------------------------
+		public UnitForm (string file_name)
 		{
 			instance = new Notebook ();
 
-			UnitEditor.RenderablesList renderables_list = new UnitEditor.RenderablesList (unit_file_name);
+			UnitEditor.RenderablesList renderables_list = new UnitEditor.RenderablesList (file_name);
 			instance.AppendPage (renderables_list, new Label("Renderables"));
 		}
 	}