From d01745e8bffd559d124c359e976e9a7eed446a41 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jean-Philippe=20Bruy=C3=A8re?= Date: Sat, 13 Apr 2019 22:41:14 +0200 Subject: [PATCH] replace cairo_region with Rectangles class to prepare for vkvg test --- Crow/Crow.csproj | 2 +- Crow/Default.style | 4 +- Crow/src/GraphicObjects/Group.cs | 20 +-- Crow/src/GraphicObjects/PrivateContainer.cs | 10 +- Crow/src/GraphicObjects/Slider.cs | 2 +- Crow/src/GraphicObjects/Widget.cs | 11 +- Crow/src/Interface.cs | 25 ++-- Crow/src/Rectangles.cs | 133 ++++++++++++++++++++ 8 files changed, 157 insertions(+), 50 deletions(-) create mode 100644 Crow/src/Rectangles.cs diff --git a/Crow/Crow.csproj b/Crow/Crow.csproj index dd3c9881..cb2902f8 100644 --- a/Crow/Crow.csproj +++ b/Crow/Crow.csproj @@ -31,7 +31,7 @@ true false $(SolutionDir)build\Debug - TRACE0;DEBUG;DEBUG_BINDING_FUNC_CALLS0;DEBUG_DRAGNDROP0;DEBUG_LOG0;XLIB_BACKEND0;DESIGN_MODE;DEBUG_UPDATE0;DEBUG_FOCUS;DEBUG_DISPOSE0;MEASURE_TIME;DEBUG_LOAD0;DEBUG_BINDING0;DEBUG_CLIP_RECTANGLE0 + TRACE0;DEBUG;DEBUG_BINDING_FUNC_CALLS0;DEBUG_DRAGNDROP0;DEBUG_LOG0;XLIB_BACKEND0;DESIGN_MODE;DEBUG_UPDATE0;DEBUG_FOCUS0;DEBUG_DISPOSE0;MEASURE_TIME;DEBUG_LOAD0;DEBUG_BINDING0;DEBUG_CLIP_RECTANGLE0 diff --git a/Crow/Default.style b/Crow/Default.style index 61678fdf..da26c243 100644 --- a/Crow/Default.style +++ b/Crow/Default.style @@ -38,7 +38,7 @@ Button { Label { Height = "Fit"; Width = "Fit"; - Margin = "0"; + Margin = "1"; } Menu { Margin = "1"; @@ -73,7 +73,7 @@ MessageBox { Slider { Background = "vgradient|0:Black|0.1:Grey|0.9:Grey|1:LightGrey"; Foreground = "Grey"; - Height = "10"; + Height = "8"; Value="5"; } Splitter { diff --git a/Crow/src/GraphicObjects/Group.cs b/Crow/src/GraphicObjects/Group.cs index a0ae35b2..fdd078ab 100644 --- a/Crow/src/GraphicObjects/Group.cs +++ b/Crow/src/GraphicObjects/Group.cs @@ -312,12 +312,7 @@ namespace Crow Context gr = new Context (bmp); if (!Clipping.IsEmpty) { - for (int i = 0; i < Clipping.NumRectangles; i++) - gr.Rectangle(Clipping.GetRectangle(i)); - gr.ClipPreserve(); - gr.Operator = Operator.Clear; - gr.Fill(); - gr.Operator = Operator.Over; + Clipping.clearAndClip (gr); base.onDraw (gr); @@ -331,7 +326,7 @@ namespace Crow foreach (Widget c in Children) { if (!c.Visible) continue; - if (Clipping.Contains (c.Slot + ClientRectangle.Position) == RegionOverlap.Out) + if (!Clipping.intersect (c.Slot + ClientRectangle.Position)) continue; c.Paint (ref gr); } @@ -339,11 +334,9 @@ namespace Crow childrenRWLock.ExitReadLock (); #if DEBUG_CLIP_RECTANGLE - /*gr.LineWidth = 1; - gr.SetSourceColor(Color.DarkMagenta.AdjustAlpha (0.8)); - for (int i = 0; i < Clipping.NumRectangles; i++) - gr.Rectangle(Clipping.GetRectangle(i)); - gr.Stroke ();*/ + gr.LineWidth = 1; + Clipping.stroke(gr,Color.DarkMagenta.AdjustAlpha (0.8)); + gr.Stroke (); #endif } gr.Dispose (); @@ -351,8 +344,7 @@ namespace Crow ctx.SetSourceSurface (bmp, rb.X, rb.Y); ctx.Paint (); - Clipping.Dispose(); - Clipping = new Region (); + Clipping.Reset(); } #endregion diff --git a/Crow/src/GraphicObjects/PrivateContainer.cs b/Crow/src/GraphicObjects/PrivateContainer.cs index 1225cbb3..1f86a034 100644 --- a/Crow/src/GraphicObjects/PrivateContainer.cs +++ b/Crow/src/GraphicObjects/PrivateContainer.cs @@ -203,12 +203,7 @@ namespace Crow Context gr = new Context (bmp); if (!Clipping.IsEmpty) { - for (int i = 0; i < Clipping.NumRectangles; i++) - gr.Rectangle(Clipping.GetRectangle(i)); - gr.ClipPreserve(); - gr.Operator = Operator.Clear; - gr.Fill(); - gr.Operator = Operator.Over; + Clipping.clearAndClip (gr); onDraw (gr); } @@ -217,8 +212,7 @@ namespace Crow ctx.SetSourceSurface (bmp, rb.X, rb.Y); ctx.Paint (); - Clipping.Dispose(); - Clipping = new Region (); + Clipping.Reset (); } #endregion diff --git a/Crow/src/GraphicObjects/Slider.cs b/Crow/src/GraphicObjects/Slider.cs index bd46b051..891e393e 100644 --- a/Crow/src/GraphicObjects/Slider.cs +++ b/Crow/src/GraphicObjects/Slider.cs @@ -79,7 +79,7 @@ namespace Crow NotifyValueChanged ("CursorColor", _cursorColor); } } - [DefaultValue(20)] + [DefaultValue(10)] public virtual int CursorSize { get { return _cursorSize; } set { diff --git a/Crow/src/GraphicObjects/Widget.cs b/Crow/src/GraphicObjects/Widget.cs index f047e67a..c6efabeb 100644 --- a/Crow/src/GraphicObjects/Widget.cs +++ b/Crow/src/GraphicObjects/Widget.cs @@ -191,7 +191,6 @@ namespace Crow parentRWLock.ExitWriteLock(); } else Debug.WriteLine ("!!! Finalized by GC: {0}", this.ToString ()); - Clipping?.Dispose (); bmp?.Dispose (); disposed = true; } @@ -216,7 +215,7 @@ namespace Crow /// are repeated at each cached levels of the tree with correspondig coordinate system. This is done /// in a dedicated step of the update between layouting and drawing. /// - public Region Clipping; + public Rectangles Clipping = new Rectangles(); #region IValueChange implementation /// @@ -243,7 +242,6 @@ namespace Crow /// action. /// protected Widget () { - Clipping = new Region (); #if DEBUG_LOG GraphicObjects.Add (this); DebugLog.AddEvent(DbgEvtType.GOClassCreation, this); @@ -831,7 +829,7 @@ namespace Crow /// /// Font being used in many controls, it is defined in the base GraphicObject class. /// - [DesignCategory ("Appearance")][DefaultValue("sans, 12")] + [DesignCategory ("Appearance")][DefaultValue("droid, 10")] public virtual Font Font { get { return font; } set { @@ -1394,7 +1392,7 @@ namespace Crow if (r.Bottom > cb.Bottom) r.Height -= r.Bottom - cb.Bottom; if (cacheEnabled && !IsDirty) - Clipping.UnionRectangle (r); + Clipping.AddRectangle (r); if (Parent == null) return; Widget p = Parent as Widget; @@ -1710,8 +1708,7 @@ namespace Crow ctx.SetSourceSurface (bmp, rb.X, rb.Y); ctx.Paint (); - Clipping.Dispose (); - Clipping = new Region (); + Clipping.Reset (); #if DEBUG_LOG dbgEvt.end = DebugLog.chrono.ElapsedTicks; #endif diff --git a/Crow/src/Interface.cs b/Crow/src/Interface.cs index 2eab589d..11221364 100644 --- a/Crow/src/Interface.cs +++ b/Crow/src/Interface.cs @@ -310,7 +310,7 @@ namespace Crow public int DragImageWidth, DragImageHeight, DragImageX, DragImageY; public void ClearDragImage () { lock (UpdateMutex) { - clipping.UnionRectangle(new Rectangle (DragImageX, DragImageY, DragImageWidth, DragImageHeight)); + clipping.AddRectangle(new Rectangle (DragImageX, DragImageY, DragImageWidth, DragImageHeight)); DragImage.Dispose(); DragImage = null; } @@ -321,7 +321,7 @@ namespace Crow /// Client rectangle in the host context protected Rectangle clientRectangle; /// Clipping rectangles on the root context - Region clipping = new Region(); + Rectangles clipping = new Rectangles(); /// Main Cairo context Context ctx; #endregion @@ -744,25 +744,19 @@ namespace Crow DebugLog.AddEvent (DbgEvtType.IFaceStartDrawing); #endif if (DragImage != null) - clipping.UnionRectangle(new Rectangle (DragImageX, DragImageY, DragImageWidth, DragImageHeight)); + clipping.AddRectangle(new Rectangle (DragImageX, DragImageY, DragImageWidth, DragImageHeight)); using (surf = new ImageSurface (bmp, Format.Argb32, ClientRectangle.Width, ClientRectangle.Height, ClientRectangle.Width * 4)) { using (ctx = new Context (surf)) { if (!clipping.IsEmpty) { IsDirty = true; - for (int i = 0; i < clipping.NumRectangles; i++) - ctx.Rectangle (clipping.GetRectangle (i)); - - ctx.ClipPreserve (); - ctx.Operator = Operator.Clear; - ctx.Fill (); - ctx.Operator = Operator.Over; + clipping.clearAndClip (ctx); for (int i = GraphicTree.Count - 1; i >= 0; i--) { Widget p = GraphicTree[i]; if (!p.Visible) continue; - if (clipping.Contains (p.Slot) == RegionOverlap.Out) + if (!clipping.intersect (p.Slot)) continue; ctx.Save (); @@ -788,14 +782,11 @@ namespace Crow #if DEBUG_CLIP_RECTANGLE ctx.LineWidth = 1; - ctx.SetSourceColor(Color.Magenta.AdjustAlpha (0.5)); - for (int i = 0; i < clipping.NumRectangles; i++) - ctx.Rectangle(clipping.GetRectangle(i)); + clipping.stroke (ctx, Color.Magenta.AdjustAlpha (0.5)); ctx.Stroke (); #endif - clipping.Dispose (); - clipping = new Region (); + clipping.Reset (); //} //surf.WriteToPng (@"/mnt/data/test.png"); @@ -1284,7 +1275,7 @@ namespace Crow return true; } public void RegisterClip(Rectangle r){ - clipping.UnionRectangle (r); + clipping.AddRectangle (r); } public bool ArrangeChildren { get { return false; }} public int LayoutingTries { diff --git a/Crow/src/Rectangles.cs b/Crow/src/Rectangles.cs new file mode 100644 index 00000000..c915ac44 --- /dev/null +++ b/Crow/src/Rectangles.cs @@ -0,0 +1,133 @@ +// +// Rectangles.cs +// +// Author: +// Jean-Philippe Bruyère +// +// Copyright (c) 2013-2017 Jean-Philippe Bruyère +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +using System.Collections.Generic; +using Crow.Cairo; + +namespace Crow { + public class Rectangles + { + public List list = new List(); + public int count + { + get { return list.Count; } + } + public bool IsEmpty => list.Count == 0; + + public void AddRectangle(Rectangle r) + { + if (doesNotContain (r)) { + list.Add (r); + boundsUpToDate = false; + } + } + public void Reset() + { + list = new List(); + _bounds = Rectangle.Empty; + boundsUpToDate = true; + } + bool doesNotContain(Rectangle r) + { + foreach (Rectangle rInList in list) + if (rInList.ContainsOrIsEqual(r)) + return false; + return true; + } + + public bool intersect(Rectangle r) + { + foreach (Rectangle rInList in list) + if (rInList.Intersect(r)) + return true; + return false; + } + public void stroke(Context ctx, Color c) + { + foreach (Rectangle r in list) + ctx.Rectangle(r); + + ctx.SetSourceColor(c); + + ctx.LineWidth = 2; + ctx.Stroke (); + } + public void clearAndClip(Context ctx) + { + if (list.Count == 0) + return; + foreach (Rectangle r in list) + ctx.Rectangle(r); + + ctx.ClipPreserve(); + ctx.Operator = Operator.Clear; + ctx.Fill(); + ctx.Operator = Operator.Over; + } + + public void clip(Context ctx) + { + foreach (Rectangle r in list) + ctx.Rectangle(r); + + ctx.Clip(); + } + + Rectangle _bounds; + bool boundsUpToDate = true; + public Rectangle Bounds { + get { + if (!boundsUpToDate) { + if (list.Count > 0) { + _bounds = list [0]; + for (int i = 1; i < list.Count; i++) { + _bounds += list [i]; + } + } else + _bounds = Rectangle.Empty; + boundsUpToDate = true; + } + return _bounds; + } + } + public void clear(Context ctx) + { + foreach (Rectangle r in list) + ctx.Rectangle(r); + ctx.Operator = Operator.Clear; + ctx.Fill(); + ctx.Operator = Operator.Over; + } + public override string ToString () + { + string tmp = ""; + foreach (Rectangle r in list) { + tmp += r.ToString (); + } + return tmp; + } + } +} -- 2.47.3