]> O.S.I.I.S - jp/crow.git/commitdiff
Style attribute in xml, add default value loader style key.
authorjpbruyere <jp.bruyere@hotmail.com>
Sun, 31 Jul 2016 10:02:14 +0000 (12:02 +0200)
committerjpbruyere <jp.bruyere@hotmail.com>
Sun, 31 Jul 2016 10:02:14 +0000 (12:02 +0200)
modifié :         src/GraphicObjects/GraphicObject.cs
modifié :         src/Interface.cs

src/GraphicObjects/GraphicObject.cs
src/Interface.cs

index a24b5763d4f3c5d886854c0f6f6e11173ef902e2..a053e700ec1813a24296486c24ea317622e2de6a 100644 (file)
@@ -45,17 +45,14 @@ namespace Crow
                        uid = currentUid;
                        currentUid++;
 
-                       if (Interface.XmlSerializerInit)
+                       if (Interface.CurrentInterface.XmlLoading)
                                return;
-
-                       loadDefaultValues ();
                }
                public GraphicObject (Rectangle _bounds)
                {
-                       if (Interface.XmlSerializerInit)
-                               return;
-
-                       loadDefaultValues ();
+                       if (!Interface.CurrentInterface.XmlLoading)
+                               loadDefaultValues ();
+                       
                        Left = _bounds.Left;
                        Top = _bounds.Top;
                        Width = _bounds.Width;
@@ -485,17 +482,6 @@ namespace Crow
                }
                #endregion
 
-
-               /// <summary>
-               /// Add 'Styles' to full class name between assembly name and the rest of the full name space
-               /// </summary>
-               string getDefaultStyleResId (Type t) {
-                       string [] tmp = t.FullName.Split ('.');
-                       string res = tmp [0] + ".Styles";
-                       for (int i = 1; i < tmp.Length; i++)
-                               res += "." + tmp [i];
-                       return res;
-               }
                /// <summary> Loads the default values from XML attributes default </summary>
                protected virtual void loadDefaultValues()
                {
@@ -504,10 +490,21 @@ namespace Crow
                        #endif
 
                        Type thisType = this.GetType ();
+
+                       if (!string.IsNullOrEmpty (Style)) {
+                               if (Interface.DefaultValuesLoader.ContainsKey (Style)) {
+                                       Interface.DefaultValuesLoader [Style] (this);
+                                       return;
+                               }
+                       }
                        if (Interface.DefaultValuesLoader.ContainsKey (thisType.FullName)) {
                                Interface.DefaultValuesLoader [thisType.FullName] (this);
                                return;
                        }
+                       if (Interface.DefaultValuesLoader.ContainsKey (thisType.Name)) {
+                               Interface.DefaultValuesLoader [thisType.Name] (this);
+                               return;
+                       }
 
                        List<Dictionary<string, object>> styling = new List<Dictionary<string, object>>();
 
@@ -516,15 +513,26 @@ namespace Crow
                        //2: class name
                        //3: style may have been registered with their ressource ID minus .style extention
                        //   those files being placed in a Styles folder
-
-                       if (Interface.CurrentInterface.Styling.ContainsKey (thisType.FullName))
+                       string styleKey = Style;
+                       if (!string.IsNullOrEmpty (Style)) {
+                               if (Interface.CurrentInterface.Styling.ContainsKey (Style)) {
+                                       styling.Add (Interface.CurrentInterface.Styling [Style]);
+                               }
+                       }
+                       if (Interface.CurrentInterface.Styling.ContainsKey (thisType.FullName)) {
                                styling.Add (Interface.CurrentInterface.Styling [thisType.FullName]);
-                       if (Interface.CurrentInterface.Styling.ContainsKey (thisType.Name))
+                               if (string.IsNullOrEmpty (styleKey))
+                                       styleKey = thisType.FullName;
+                       }
+                       if (Interface.CurrentInterface.Styling.ContainsKey (thisType.Name)) {
                                styling.Add (Interface.CurrentInterface.Styling [thisType.Name]);
-                       
-                       string styleFullName = getDefaultStyleResId (thisType);
-                       if (Interface.CurrentInterface.Styling.ContainsKey (styleFullName))
-                               styling.Add (Interface.CurrentInterface.Styling [styleFullName]);
+                               if (string.IsNullOrEmpty (styleKey))
+                                       styleKey = thisType.Name;
+                       }
+
+                       if (string.IsNullOrEmpty (styleKey))
+                               styleKey = thisType.FullName;
+
                        
                        //Reflexion being very slow compared to dyn method or delegates,
                        //I compile the initial values coded in the CustomAttribs of the class,
@@ -676,8 +684,8 @@ namespace Crow
                        il.Emit(OpCodes.Ret);
                        #endregion
 
-                       Interface.DefaultValuesLoader[thisType.FullName] = (Interface.loadDefaultInvoker)dm.CreateDelegate(typeof(Interface.loadDefaultInvoker));
-                       Interface.DefaultValuesLoader[thisType.FullName] (this);
+                       Interface.DefaultValuesLoader[styleKey] = (Interface.loadDefaultInvoker)dm.CreateDelegate(typeof(Interface.loadDefaultInvoker));
+                       Interface.DefaultValuesLoader[styleKey] (this);
                }
 
                public virtual GraphicObject FindByName(string nameToFind){
@@ -1283,13 +1291,21 @@ namespace Crow
                }
                public virtual void ReadXml (System.Xml.XmlReader reader)
                {
-                       if (!reader.HasAttributes)
-                               return;
+                       if (reader.HasAttributes) {                             
+
+                               style = reader.GetAttribute ("Style");
 
-                       while (reader.MoveToNextAttribute ())
-                               affectMember (reader.Name, reader.Value);
+                               loadDefaultValues ();
 
-                       reader.MoveToElement();
+                               while (reader.MoveToNextAttribute ()) {
+                                       if (reader.Name == "Style")
+                                               continue;
+
+                                       affectMember (reader.Name, reader.Value);
+                               }
+                               reader.MoveToElement ();
+                       }else
+                               loadDefaultValues ();
                }
                public virtual void WriteXml (System.Xml.XmlWriter writer)
                {
index a83a5305f29ba9fccb9ad2f3504062cb890f7583..f67849a7f41e4e098f056f64666a1d5d71b9af86 100644 (file)
@@ -59,8 +59,7 @@ namespace Crow
                #endregion
 
                #region Static and constants
-               /// <summary> Used to prevent spurious loading of templates </summary>
-               internal static bool XmlSerializerInit = false;
+               internal bool XmlLoading = false;
                /// <summary> keep ressource path for debug msg </summary>
                internal static string CurrentGOMLPath = "";
                //used in templatedControl
@@ -216,7 +215,7 @@ namespace Crow
 
                        return tmp;
                }
-               public static GraphicObject Load (Stream stream, Type type, object hostClass = null)
+               internal static GraphicObject Load (Stream stream, Type type, object hostClass = null)
                {
                        #if DEBUG_LOAD
                        Stopwatch loadingTime = new Stopwatch ();
@@ -225,16 +224,16 @@ namespace Crow
 
                        GraphicObject result;
 
+                       CurrentInterface.XmlLoading = true;
 
                        XmlSerializerNamespaces xn = new XmlSerializerNamespaces ();
                        xn.Add ("", "");
 
-                       XmlSerializerInit = true;
                        XmlSerializer xs = new XmlSerializer (type);
-                       XmlSerializerInit = false;
 
                        result = (GraphicObject)xs.Deserialize (stream);
                        //result.DataSource = hostClass;
+                       CurrentInterface.XmlLoading = false;
 
                        #if DEBUG_LOAD
                        FileStream fs = stream as FileStream;
@@ -250,11 +249,6 @@ namespace Crow
                        return result;
                }
 
-               public void InterfaceLoad(string path){
-                       GraphicObject tmp = Interface.Load (path, this);
-                       AddWidget (tmp);
-               }
-
                public GraphicObject LoadInterface (string path)
                {
                        lock (UpdateMutex) {