From a5eec67305d20d7112743f649e2a327ba717ceec Mon Sep 17 00:00:00 2001 From: jpbruyere Date: Mon, 8 Feb 2016 17:22:23 +0100 Subject: [PATCH] MaxLayoutingTries; CachingEnabled in GraphicObject + MaxCacheSize --- src/GraphicObjects/GraphicObject.cs | 57 ++++++++++++++++++++++------- src/GraphicObjects/ILayoutable.cs | 1 + src/Interface.cs | 2 + src/LayoutingQueueItem.cs | 5 ++- src/OpenTKGameWindow.cs | 4 ++ 5 files changed, 54 insertions(+), 15 deletions(-) diff --git a/src/GraphicObjects/GraphicObject.cs b/src/GraphicObjects/GraphicObject.cs index 43b9a725..27f1fe5d 100644 --- a/src/GraphicObjects/GraphicObject.cs +++ b/src/GraphicObjects/GraphicObject.cs @@ -29,6 +29,7 @@ namespace Crow #endif internal List Bindings = new List (); + internal int layoutingTries = 0; #region IValueChange implementation public event EventHandler ValueChanged; @@ -80,6 +81,7 @@ namespace Crow HorizontalAlignment _horizontalAlignment = HorizontalAlignment.Center; Size _maximumSize = "0;0"; Size _minimumSize = "0;0"; + bool cacheEnabled = false; #endregion #region public fields @@ -178,6 +180,16 @@ namespace Crow #endregion #region public properties + [XmlAttributeAttribute()][DefaultValue(true)] + public virtual bool CacheEnabled { + get { return cacheEnabled; } + set { + if (cacheEnabled == value) + return; + cacheEnabled = value; + NotifyValueChanged ("CacheEnabled", cacheEnabled); + } + } [XmlAttributeAttribute()][DefaultValue("unamed")] public virtual string Name { get { return _name; } @@ -499,6 +511,11 @@ namespace Crow } #region Layouting + public int LayoutingTries { + get { return layoutingTries; } + set { layoutingTries = value; } + } + /// return size of content + margins protected virtual Size measureRawSize () { return Bounds.Size; @@ -740,25 +757,37 @@ namespace Crow if (!Visible) return; - if (bmp == null) - UpdateGraphic (); + Rectangle rb = Parent.ContextCoordinates (Slot); - Rectangle rb = Parent.ContextCoordinates(Slot); + if (cacheEnabled) { + if (Slot.Width > Interface.MaxCacheSize || Slot.Height > Interface.MaxCacheSize) + cacheEnabled = false; + } - using (ImageSurface source = new ImageSurface(bmp, Format.Argb32, Slot.Width, Slot.Height, 4 * Slot.Width)) { - //TODO:improve equality test for basic color and Fill - if (this.Background is SolidColor) { - if ((this.Background as SolidColor).Equals(Color.Clear)) { - ctx.Save (); - ctx.Operator = Operator.Clear; - ctx.Rectangle (rb); - ctx.Fill (); - ctx.Restore (); + if (cacheEnabled) { + if (bmp == null) + UpdateGraphic (); + + using (ImageSurface source = new ImageSurface (bmp, Format.Argb32, Slot.Width, Slot.Height, 4 * Slot.Width)) { + //TODO:improve equality test for basic color and Fill + if (this.Background is SolidColor) { + if ((this.Background as SolidColor).Equals (Color.Clear)) { + ctx.Save (); + ctx.Operator = Operator.Clear; + ctx.Rectangle (rb); + ctx.Fill (); + ctx.Restore (); + } } + ctx.SetSourceSurface (source, rb.X, rb.Y); + ctx.Paint (); } - ctx.SetSourceSurface (source, rb.X, rb.Y); - ctx.Paint (); + return; } + ctx.Save (); + ctx.Translate (rb.X, rb.Y); + onDraw (ctx); + ctx.Restore (); } #endregion diff --git a/src/GraphicObjects/ILayoutable.cs b/src/GraphicObjects/ILayoutable.cs index b9a97b0e..edddedd7 100644 --- a/src/GraphicObjects/ILayoutable.cs +++ b/src/GraphicObjects/ILayoutable.cs @@ -5,6 +5,7 @@ namespace Crow { public interface ILayoutable { + int LayoutingTries { get; set; } ILayoutable Parent { get; set; } Rectangle ClientRectangle { get; } diff --git a/src/Interface.cs b/src/Interface.cs index 5ecd7a88..5a67751d 100644 --- a/src/Interface.cs +++ b/src/Interface.cs @@ -52,6 +52,8 @@ namespace Crow public static bool DesignerMode = false; /// Threshold to catch borders for sizing public static int BorderThreshold = 5; + public const int MaxCacheSize = 2048; + public const int MaxLayoutingTries = 50; public static Queue LayoutingQueue = new Queue(); diff --git a/src/LayoutingQueueItem.cs b/src/LayoutingQueueItem.cs index a558efd2..03a39c5c 100644 --- a/src/LayoutingQueueItem.cs +++ b/src/LayoutingQueueItem.cs @@ -61,8 +61,11 @@ namespace Crow #if DEBUG_LAYOUTING Debug.WriteLine ("Requeuing => " + this.ToString ()); #endif - Interface.LayoutingQueue.Enqueue (this); + GraphicObject.LayoutingTries ++; + if (GraphicObject.LayoutingTries < Interface.MaxLayoutingTries) + Interface.LayoutingQueue.Enqueue (this); } else { + GraphicObject.LayoutingTries = 0; #if DEBUG_LAYOUTING Debug.WriteLine ("Layouting => " + this.ToString ()); #endif diff --git a/src/OpenTKGameWindow.cs b/src/OpenTKGameWindow.cs index 2083346c..0892ec47 100644 --- a/src/OpenTKGameWindow.cs +++ b/src/OpenTKGameWindow.cs @@ -544,6 +544,10 @@ namespace Crow #endregion #region ILayoutable implementation + public int LayoutingTries { + get { throw new NotImplementedException (); } + set { throw new NotImplementedException (); } + } public LayoutingType RegisteredLayoutings { get { return LayoutingType.None; } set { throw new NotImplementedException (); } -- 2.47.3