From 5af8e3e2a4676c38439d1e0d29a8f08163d85143 Mon Sep 17 00:00:00 2001 From: jpbruyere Date: Tue, 9 Feb 2016 18:48:58 +0100 Subject: [PATCH] LogicalParent use in all bindings; Resolved boolean in Binding class to avoid multiple event registration; resoved all tree in templated control, to allow complex bindings outside template content --- src/CompilerServices/CompilerServices.cs | 9 ++++++-- src/GraphicObjects/GraphicObject.cs | 28 ++++++++++++++++-------- src/GraphicObjects/ILayoutable.cs | 5 +++++ src/GraphicObjects/TemplatedControl.cs | 6 +---- src/OpenTKGameWindow.cs | 19 ++++++++-------- 5 files changed, 41 insertions(+), 26 deletions(-) diff --git a/src/CompilerServices/CompilerServices.cs b/src/CompilerServices/CompilerServices.cs index b640a628..4c5da03d 100644 --- a/src/CompilerServices/CompilerServices.cs +++ b/src/CompilerServices/CompilerServices.cs @@ -60,7 +60,7 @@ namespace Crow public string DynMethodId { get { return dynMethodId; } } - + public bool Resolved = false; public MemberReference Source; public MemberReference Target; @@ -103,7 +103,7 @@ namespace Crow if (tmp == null) return false; if (bindingExp [ptr] == "..") - tmp = tmp.Parent as ILayoutable; + tmp = tmp.LogicalParent; else if (bindingExp [ptr] == ".") { if (ptr > 0) throw new Exception ("Syntax error in binding, './' may only appear in first position"); @@ -143,6 +143,11 @@ namespace Crow { Target = null; dynMethodId = ""; + Resolved = false; + } + public override string ToString () + { + return string.Format ("[Binding: {0}.{1} <= {2}]", Source.Instance, Source.Member.Name, Expression); } } diff --git a/src/GraphicObjects/GraphicObject.cs b/src/GraphicObjects/GraphicObject.cs index 09b1776d..93fa85bb 100644 --- a/src/GraphicObjects/GraphicObject.cs +++ b/src/GraphicObjects/GraphicObject.cs @@ -66,6 +66,7 @@ namespace Crow #region private fields LayoutingType registeredLayoutings = LayoutingType.None; + ILayoutable logicalParent; ILayoutable _parent; string _name = "unamed"; Fill _background = Color.Transparent; @@ -86,10 +87,6 @@ namespace Crow #region public fields /// - /// The logical parent (used mainly for bindings) as opposed to the parent in the graphic tree - /// - public GraphicObject LogicalParent; - /// /// Original size and position 0=Stretched; -1=Fit /// public Rectangle Bounds; @@ -135,6 +132,10 @@ namespace Crow } } + public ILayoutable LogicalParent { + get { return logicalParent == null ? Parent : logicalParent; } + set { logicalParent = value; } + } [XmlIgnore]public virtual Rectangle ClientRectangle { get { @@ -422,8 +423,7 @@ namespace Crow } get { return dataSource == null ? - LogicalParent == null ? - Parent is GraphicObject ? (Parent as GraphicObject).DataSource : null : LogicalParent.DataSource : dataSource; + (LogicalParent as GraphicObject).DataSource : dataSource; } } #endregion @@ -905,10 +905,11 @@ namespace Crow #if DEBUG_BINDING Debug.WriteLine ("ResolveBinding => " + this.ToString ()); #endif - + if (Bindings.Count == 0) + return; Dictionary> resolved = new Dictionary>(); foreach (Binding b in Bindings) { - if (!string.IsNullOrEmpty (b.DynMethodId)) + if (b.Resolved) continue; if (b.Source.Member.MemberType == MemberTypes.Event) { if (b.Expression.StartsWith("{")){ @@ -928,6 +929,7 @@ namespace Crow MethodInfo addHandler = b.Source.Event.GetAddMethod (); Delegate del = Delegate.CreateDelegate (b.Source.Event.EventHandlerType, b.Target.Instance, b.Target.Method); addHandler.Invoke (this, new object[] { del }); + b.Resolved = true; continue; } List bindings = null; @@ -936,6 +938,7 @@ namespace Crow resolved [b.Target.Instance] = bindings; } bindings.Add (b); + b.Resolved = true; } MethodInfo stringEquals = typeof(string).GetMethod @@ -1072,7 +1075,12 @@ namespace Crow throw new Exception ("unhandle target member type in binding"); } - if (!targetValueType.IsValueType) + if (b.Source.Property.PropertyType == typeof(string)) { + MemberReference tostring = new MemberReference (b.Source.Instance); + if (!tostring.FindMember ("ToString")) + throw new Exception ("ToString method not found"); + il.Emit (OpCodes.Callvirt, tostring.Method); + }else if (!targetValueType.IsValueType) il.Emit(OpCodes.Castclass, targetValueType); else if (b.Source.Property.PropertyType != targetValueType) il.Emit(OpCodes.Callvirt, CompilerServices.GetConvertMethod( b.Source.Property.PropertyType )); @@ -1215,6 +1223,8 @@ namespace Crow Delegate del = dm.CreateDelegate(binding.Source.Event.EventHandlerType,this); MethodInfo addHandler = binding.Source.Event.GetAddMethod (); addHandler.Invoke(this, new object[] {del}); + + binding.Resolved = true; } /// /// Remove dynamic delegates by ids from dataSource diff --git a/src/GraphicObjects/ILayoutable.cs b/src/GraphicObjects/ILayoutable.cs index edddedd7..54e54f03 100644 --- a/src/GraphicObjects/ILayoutable.cs +++ b/src/GraphicObjects/ILayoutable.cs @@ -5,8 +5,13 @@ namespace Crow { public interface ILayoutable { + /// Unsuccessfull UpdateLayout and requeueing count int LayoutingTries { get; set; } + /// Parent in the graphic tree ILayoutable Parent { get; set; } + /// The logical parent (used mainly for bindings) as opposed + /// to the parent in the graphic tree + ILayoutable LogicalParent { get; set; } Rectangle ClientRectangle { get; } Rectangle getSlot(); diff --git a/src/GraphicObjects/TemplatedControl.cs b/src/GraphicObjects/TemplatedControl.cs index 1f527bc3..9dacd466 100644 --- a/src/GraphicObjects/TemplatedControl.cs +++ b/src/GraphicObjects/TemplatedControl.cs @@ -103,10 +103,6 @@ namespace Crow //prevent name searching in template return nameToFind == this.Name ? this : null; } - public override void ResolveBindings () - { - base.ResolveBindingsWithNoRecurse (); - } #endregion protected virtual void loadTemplate(GraphicObject template = null) @@ -117,7 +113,7 @@ namespace Crow }else this.SetChild (template); - this.child.ResolveBindings (); + this.ResolveBindings (); } #region IXmlSerializable diff --git a/src/OpenTKGameWindow.cs b/src/OpenTKGameWindow.cs index 3b70d92e..ac3cd75c 100644 --- a/src/OpenTKGameWindow.cs +++ b/src/OpenTKGameWindow.cs @@ -562,19 +562,18 @@ namespace Crow { return r; } - public Rectangle ScreenCoordinates (Rectangle r) - { - return r; - } + public Rectangle ScreenCoordinates (Rectangle r) => r; public ILayoutable Parent { - get { - return null; - } - set { - throw new NotImplementedException (); - } + get { return null; } + set { throw new NotImplementedException (); } + } + + public ILayoutable LogicalParent { + get { return null; } + set { throw new NotImplementedException (); } } + Rectangle ILayoutable.ClientRectangle { get { return new Size(this.ClientRectangle.Size.Width,this.ClientRectangle.Size.Height); } } -- 2.47.3