]> O.S.I.I.S - jp/crow.git/commitdiff
allow goml loading from memory stream with single call to
authorjpbruyere <jp.bruyere@hotmail.com>
Sun, 13 Sep 2015 13:06:21 +0000 (15:06 +0200)
committerjpbruyere <jp.bruyere@hotmail.com>
Sun, 13 Sep 2015 13:06:21 +0000 (15:06 +0200)
getTopGraphicObject type when loading several time the same template.

src/GraphicObjects/ListBox.cs
src/Interface.cs

index 5811e5ac72fdaea4e02e14fbd6644f062a677266..ea7f9ec5d0ea2f5edb5fd68c9e83bcacf8366f1a 100644 (file)
@@ -23,6 +23,10 @@ using System.Collections;
 using System.Xml.Serialization;
 using System.ComponentModel;
 //TODO: implement ItemTemplate node in xml
+using System.IO;
+using System.Diagnostics;
+
+
 namespace go
 {
        [DefaultTemplate("#go.Templates.Listbox.goml")]
@@ -70,12 +74,35 @@ namespace go
                                _list.Children.Clear ();
                                if (data == null)
                                        return;
+
+                               #if DEBUG
+                               Stopwatch loadingTime = new Stopwatch ();
+                               loadingTime.Start ();
+                               #endif
+
+                               MemoryStream ms = new MemoryStream ();
+                               using (Stream stream = Interface.GetStreamFromPath (ItemTemplate)) {
+                                       
+                                       stream.CopyTo (ms);
+                               }
+                                       
+                               Type t = Interface.GetTopContainerOfGOMLStream (ms);
+
                                foreach (var item in data) {
-                                       GraphicObject g = Interface.Load (ItemTemplate, item);
+                                       ms.Seek(0,SeekOrigin.Begin);
+                                       GraphicObject g = Interface.Load (ms, t, item);
                                        g.MouseClick += itemClick;
                                        _list.addChild(g);
 
                                }
+                               ms.Dispose ();
+
+                               #if DEBUG
+                               loadingTime.Stop ();
+                               Debug.WriteLine("Listbox Loading: {0} ticks \t, {1} ms",
+                                       loadingTime.ElapsedTicks,
+                                       loadingTime.ElapsedMilliseconds);
+                               #endif
                        }
                }
                void itemClick(object sender, OpenTK.Input.MouseButtonEventArgs e){
index 54cd752a6c59e76a9419201d6c054d7cf1379aa5..c3ddc8fad009053f6e87ab24372e993e56ef957b 100644 (file)
@@ -87,33 +87,38 @@ namespace go
                        }
                        return stream;
                }
-
-               public static GraphicObject Load(string path, object hostClass = null)
-               {
+               /// <summary>
+               /// Pre-read first node to set GraphicObject class for loading
+               /// and reset stream position to 0
+               /// </summary>
+               public static Type GetTopContainerOfGOMLStream(Stream stream){
                        string root = "Object";
-
-                       using (Stream stream = GetStreamFromPath (path)) {
-
-                               #region Pre-read first node to set GraphicObject class for loading
-                               using (XmlReader reader = XmlReader.Create (stream)) {
-                                       while (reader.Read()) {
-                                               // first element is the root element
-                                               if (reader.NodeType == XmlNodeType.Element) {
-                                                       root = reader.Name;
-                                                       break;
-                                               }
+                       stream.Seek(0,SeekOrigin.Begin);
+                       using (XmlReader reader = XmlReader.Create (stream)) {
+                               while (reader.Read()) {
+                                       // first element is the root element
+                                       if (reader.NodeType == XmlNodeType.Element) {
+                                               root = reader.Name;
+                                               break;
                                        }
                                }
+                       }
 
-                               Type t = Type.GetType("go." + root);
-                               //var go = Activator.CreateInstance(t);
-                               stream.Seek(0,SeekOrigin.Begin);
-                               #endregion
-
-                               return Load(stream, t, hostClass);
+                       Type t = Type.GetType("go." + root);
+                       //var go = Activator.CreateInstance(t);
+                       stream.Seek(0,SeekOrigin.Begin);
+                       return t;
+               }
+               public static string CurrentGOMLPath;
+               public static GraphicObject Load(string path, object hostClass = null)
+               {                       
+                       CurrentGOMLPath = path;
+                       using (Stream stream = GetStreamFromPath (path)) {
+                               return Load(stream, GetTopContainerOfGOMLStream(stream), hostClass);
                        }
+                       CurrentGOMLPath = "";
                }
-               static GraphicObject Load(Stream stream, Type type, object hostClass = null)
+               public static GraphicObject Load(Stream stream, Type type, object hostClass = null, bool resolve = true)
                {
                        GraphicObject result;
                        GOMLResolutionStack.Push(new List<DynAttribute>());
@@ -129,31 +134,14 @@ namespace go
                                GOMLResolutionStack.Pop ();
                                return result;
                        }
-
-                       resolveGOML (hostClass);
-
-//                     while (Bindings.Count > 0) {
-//                             DynAttribute binding = Bindings [0];
-//                             Bindings.RemoveAt (0);
-//                             CompilerServices.ResolveBinding (binding, hostClass);
-//                     }
-
-//                     foreach (DynAttribute binding in Bindings) {
-//                             //                              Type tSource = binding.Source.GetType ();
-//                             //                              if (!tSource.GetInterfaces ().Any (i => i.Name == "IValueChange")){
-//                             //                                      Debug.WriteLine ("Binding source does not implement IValueChange.");
-//                             //                                      continue;
-//                             //                              }
-//                             //MemberInfo mi = binding.Source.GetType ().GetMember (binding.MemberName);
-//                             CompilerServices.CreateBinding (binding, hostClass);
-//                     }
-//                     Bindings.Clear ();
-
+                               
+                       if (resolve)
+                               resolveGOML (hostClass);
 
                        return result;
                }
 
-               static void resolveGOML(object hostClass)
+               public static void resolveGOML(object hostClass)
                {
                        foreach (DynAttribute es in GOMLResolver)
                        {