From 2361b6f12f566fdea9b177f9ee84887273fd6c81 Mon Sep 17 00:00:00 2001 From: jpbruyere Date: Sat, 23 Apr 2016 16:21:08 +0200 Subject: [PATCH] Group basic sizing ok --- .gitignore | 2 +- OTKCrow/OpenTKGameWindow.cs | 5 +- Tests/GOLIBTestsOTK.cs | 8 +-- Tests/Interfaces/Container/3.crow | 4 ++ Tests/Interfaces/Group/0.crow | 11 +++ Tests/Interfaces/Group/1.crow | 33 +++++++++ Tests/Interfaces/Group/2.crow | 33 +++++++++ Tests/Interfaces/Group/3.crow | 33 +++++++++ Tests/Interfaces/Group/4.crow | 7 ++ Tests/Tests.csproj | 19 +++++ src/GraphicObjects/GraphicObject.cs | 4 +- src/GraphicObjects/Group.cs | 96 +++++++++++--------------- src/GraphicObjects/PrivateContainer.cs | 76 ++++++++++---------- src/LayoutingQueueItem.cs | 1 + src/Measure.cs | 6 +- 15 files changed, 236 insertions(+), 102 deletions(-) create mode 100755 Tests/Interfaces/Container/3.crow create mode 100755 Tests/Interfaces/Group/0.crow create mode 100755 Tests/Interfaces/Group/1.crow create mode 100755 Tests/Interfaces/Group/2.crow create mode 100755 Tests/Interfaces/Group/3.crow create mode 100755 Tests/Interfaces/Group/4.crow diff --git a/.gitignore b/.gitignore index 7aa58178..ce290fc4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ Win_x86 - +.nuget Crow.userprefs Debug packages diff --git a/OTKCrow/OpenTKGameWindow.cs b/OTKCrow/OpenTKGameWindow.cs index 0b5c8543..329f83fb 100644 --- a/OTKCrow/OpenTKGameWindow.cs +++ b/OTKCrow/OpenTKGameWindow.cs @@ -115,6 +115,7 @@ namespace Crow 3,3,OpenTK.Graphics.GraphicsContextFlags.Default) { CrowInterface = new Interface (); + Thread t = new Thread (interfaceThread); t.IsBackground = true; t.Start (); @@ -125,7 +126,9 @@ namespace Crow { CrowInterface.Quit += Quit; CrowInterface.MouseCursorChanged += CrowInterface_MouseCursorChanged; - + while (CrowInterface.ClientRectangle.Size.Width == 0) + Thread.Sleep (5); + while (true) { CrowInterface.Update (); Thread.Sleep (5); diff --git a/Tests/GOLIBTestsOTK.cs b/Tests/GOLIBTestsOTK.cs index ec8fd690..bc0d4e03 100644 --- a/Tests/GOLIBTestsOTK.cs +++ b/Tests/GOLIBTestsOTK.cs @@ -91,13 +91,13 @@ namespace testOTK //this.AddWidget(new test4()); KeyboardKeyDown += GOLIBTests_KeyboardKeyDown1;; + testFiles = Directory.GetFiles(@"Interfaces/Group", "*.crow").ToArray(); + //testFiles = Directory.GetFiles(@"Interfaces/Stack", "*.crow").ToArray(); + //testFiles = Directory.GetFiles(@"Interfaces/GraphicObject", "*.crow").Concat(testFiles).ToArray(); - testFiles = Directory.GetFiles(@"Interfaces/Stack", "*.crow").ToArray(); - testFiles = Directory.GetFiles(@"Interfaces/GraphicObject", "*.crow").Concat(testFiles).ToArray(); - testFiles = Directory.GetFiles(@"Interfaces/Container", "*.crow").Concat(testFiles).ToArray(); //testFiles = Directory.GetFiles(@"Interfaces", "*.crow").Concat(testFiles).ToArray(); - GraphicObject obj = CrowInterface.LoadInterface(testFiles[5]); + GraphicObject obj = CrowInterface.LoadInterface(testFiles[idx]); obj.DataSource = this; } diff --git a/Tests/Interfaces/Container/3.crow b/Tests/Interfaces/Container/3.crow new file mode 100755 index 00000000..6f21416c --- /dev/null +++ b/Tests/Interfaces/Container/3.crow @@ -0,0 +1,4 @@ + + + \ No newline at end of file diff --git a/Tests/Interfaces/Group/0.crow b/Tests/Interfaces/Group/0.crow new file mode 100755 index 00000000..c3cf308f --- /dev/null +++ b/Tests/Interfaces/Group/0.crow @@ -0,0 +1,11 @@ + + + + + + \ No newline at end of file diff --git a/Tests/Interfaces/Group/1.crow b/Tests/Interfaces/Group/1.crow new file mode 100755 index 00000000..6950a8d8 --- /dev/null +++ b/Tests/Interfaces/Group/1.crow @@ -0,0 +1,33 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/Tests/Interfaces/Group/2.crow b/Tests/Interfaces/Group/2.crow new file mode 100755 index 00000000..693e1b05 --- /dev/null +++ b/Tests/Interfaces/Group/2.crow @@ -0,0 +1,33 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/Tests/Interfaces/Group/3.crow b/Tests/Interfaces/Group/3.crow new file mode 100755 index 00000000..321e219d --- /dev/null +++ b/Tests/Interfaces/Group/3.crow @@ -0,0 +1,33 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/Tests/Interfaces/Group/4.crow b/Tests/Interfaces/Group/4.crow new file mode 100755 index 00000000..25e03bd8 --- /dev/null +++ b/Tests/Interfaces/Group/4.crow @@ -0,0 +1,7 @@ + + + \ No newline at end of file diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index 143f80be..24042c9f 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -307,12 +307,31 @@ PreserveNewest + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + diff --git a/src/GraphicObjects/GraphicObject.cs b/src/GraphicObjects/GraphicObject.cs index e4d17351..30aa4a95 100644 --- a/src/GraphicObjects/GraphicObject.cs +++ b/src/GraphicObjects/GraphicObject.cs @@ -664,11 +664,11 @@ namespace Crow get { return layoutingTries; } set { layoutingTries = value; } } + protected Size contentSize; /// return size of content + margins protected virtual int measureRawSize (LayoutingType lt) { return lt == LayoutingType.Width ? - Width == Measure.Fit ? MinimumSize.Width : (int)Width : - Height == Measure.Fit ? MinimumSize.Height : (int)Height; + contentSize.Width + 2 * Margin: contentSize.Height + 2 * Margin; } /// By default in groups, LayoutingType.ArrangeChildren is reset public virtual void ChildrenLayoutingConstraints(ref LayoutingType layoutType){ diff --git a/src/GraphicObjects/Group.cs b/src/GraphicObjects/Group.cs index 78c5bca3..2b440ac1 100644 --- a/src/GraphicObjects/Group.cs +++ b/src/GraphicObjects/Group.cs @@ -21,8 +21,6 @@ namespace Crow public event EventHandler ChildrenCleared; #endregion - internal int maxChildrenWidth = 0; - internal int maxChildrenHeight = 0; internal GraphicObject largestChild = null; internal GraphicObject tallestChild = null; @@ -59,9 +57,9 @@ namespace Crow //child.Parent = null; Children.Remove(child); - if (child == largestChild) + if (child == largestChild && Width == Measure.Fit) searchLargestChild (); - if (child == tallestChild) + if (child == tallestChild && Height == Measure.Fit) searchTallestChild (); this.RegisterForLayouting (LayoutingType.Sizing | LayoutingType.ArrangeChildren); @@ -130,28 +128,26 @@ namespace Crow } protected override int measureRawSize (LayoutingType lt) { - if (Children.Count == 0) - return base.measureRawSize (lt); - - if (lt == LayoutingType.Width) { - if (largestChild == null) - searchLargestChild (); - if (largestChild == null){ - //if still null, not possible to determine a width - //because all children are stretched, force first one to fit - Children[0].Width = Measure.Fit; - return -1;//cancel actual sizing to let child computation take place - } - return maxChildrenWidth + 2 * Margin; - }else{ - if (tallestChild == null) - searchTallestChild (); - if (tallestChild == null) { - Children[0].Height = Measure.Fit; - return -1; + if (Children.Count > 0) { + if (lt == LayoutingType.Width) { + if (largestChild == null) + searchLargestChild (); + if (largestChild == null) { + //if still null, not possible to determine a width + //because all children are stretched, force first one to fit + Children [0].Width = Measure.Fit; + return -1;//cancel actual sizing to let child computation take place + } + } else { + if (tallestChild == null) + searchTallestChild (); + if (tallestChild == null) { + Children [0].Height = Measure.Fit; + return -1; + } } - return maxChildrenHeight + 2 * Margin; } + return base.measureRawSize (lt); } public override void OnLayoutChanges (LayoutingType layoutType) @@ -173,36 +169,29 @@ namespace Crow public virtual void OnChildLayoutChanges (object sender, LayoutingEventArgs arg) { GraphicObject g = sender as GraphicObject; + switch (arg.LayoutType) { case LayoutingType.Width: - if (g.Slot.Width > maxChildrenWidth) { - maxChildrenWidth = g.Slot.Width; + if (Width != Measure.Fit) + return; + if (g.Slot.Width > contentSize.Width) { largestChild = g; - if (Width == Measure.Fit) - this.RegisterForLayouting (LayoutingType.Width); - } else if (g == largestChild) { - - largestChild = null; - maxChildrenWidth = 0; + contentSize.Width = g.Slot.Width; + } else if (g == largestChild) + searchLargestChild (); - if (Width == Measure.Fit) - this.RegisterForLayouting (LayoutingType.Width); - } + this.RegisterForLayouting (LayoutingType.Width); break; case LayoutingType.Height: - if (g.Slot.Height > maxChildrenHeight) { - maxChildrenHeight = g.Slot.Height; + if (Height != Measure.Fit) + return; + if (g.Slot.Height > contentSize.Height) { tallestChild = g; - if (Height == Measure.Fit) - this.RegisterForLayouting (LayoutingType.Height); - } else if (g == tallestChild) { - - tallestChild = null; - maxChildrenHeight = 0; + contentSize.Height = g.Slot.Height; + } else if (g == tallestChild) + searchTallestChild (); - if (Height == Measure.Fit) - this.RegisterForLayouting (LayoutingType.Height); - } + this.RegisterForLayouting (LayoutingType.Height); break; } } @@ -211,22 +200,21 @@ namespace Crow void resetChildrenMaxSize(){ largestChild = null; tallestChild = null; - maxChildrenWidth = 0; - maxChildrenHeight = 0; + contentSize = 0; } void searchLargestChild(){ #if DEBUG_LAYOUTING Debug.WriteLine("\tSearch largest child"); #endif largestChild = null; - maxChildrenWidth = 0; + contentSize.Width = 0; for (int i = 0; i < Children.Count; i++) { if (!Children [i].Visible) continue; if (children [i].RegisteredLayoutings.HasFlag (LayoutingType.Width)) continue; - if (Children [i].Slot.Width > maxChildrenWidth) { - maxChildrenWidth = Children [i].Slot.Width; + if (Children [i].Slot.Width > contentSize.Width) { + contentSize.Width = Children [i].Slot.Width; largestChild = Children [i]; } } @@ -236,14 +224,14 @@ namespace Crow Debug.WriteLine("\tSearch tallest child"); #endif tallestChild = null; - maxChildrenHeight = 0; + contentSize.Height = 0; for (int i = 0; i < Children.Count; i++) { if (!Children [i].Visible) continue; if (children [i].RegisteredLayoutings.HasFlag (LayoutingType.Height)) continue; - if (Children [i].Slot.Height > maxChildrenHeight) { - maxChildrenHeight = Children [i].Slot.Height; + if (Children [i].Slot.Height > contentSize.Height) { + contentSize.Height = Children [i].Slot.Height; tallestChild = Children [i]; } } diff --git a/src/GraphicObjects/PrivateContainer.cs b/src/GraphicObjects/PrivateContainer.cs index 99b85dd2..0ff4beca 100644 --- a/src/GraphicObjects/PrivateContainer.cs +++ b/src/GraphicObjects/PrivateContainer.cs @@ -51,6 +51,7 @@ namespace Crow { if (child != null) { + contentSize = new Size (0, 0); child.LayoutChanged -= OnChildLayoutChanges; this.RegisterForLayouting (LayoutingType.Sizing); child.Parent = null; @@ -61,6 +62,7 @@ namespace Crow if (child != null) { child.Parent = this; child.LayoutChanged += OnChildLayoutChanges; + contentSize = child.Slot.Size; child.RegisterForLayouting (LayoutingType.Sizing); } @@ -90,22 +92,10 @@ namespace Crow return child == goToFind ? true : child == null ? false : child.Contains(goToFind); } - protected override int measureRawSize (LayoutingType lt) - { - if (child == null) - return base.measureRawSize (lt); - if (lt == LayoutingType.Width) - return child.RegisteredLayoutings.HasFlag(LayoutingType.Width) ? - -1 : child.Slot.Size.Width + 2 * Margin; - else - return child.RegisteredLayoutings.HasFlag(LayoutingType.Height) ? - -1 : child.Slot.Size.Height + 2 * Margin; - } public override bool UpdateLayout (LayoutingType layoutType) { if (child != null) { - //force sizing to fit if sizing on children and - //child has stretched size + //force sizing to fit if sizing on children and child has stretched size switch (layoutType) { case LayoutingType.Width: if (Width == Measure.Fit && child.Width.Units == Unit.Percent) @@ -123,34 +113,46 @@ namespace Crow { base.OnLayoutChanges (layoutType); - switch (layoutType) { - case LayoutingType.Width: - if (child != null) { - child.RegisterForLayouting (LayoutingType.X | LayoutingType.Width); + if (child == null) + return; + + LayoutingType ltChild = LayoutingType.None; + + if (layoutType == LayoutingType.Width) { + if (Width == Measure.Fit) + return; + if (child.Width.Units == Unit.Percent) { + ltChild |= LayoutingType.Width; + if (child.Width.Value < 100 && child.Left == 0) + ltChild |= LayoutingType.X; } - break; - case LayoutingType.Height: - if (child != null) { - child.RegisterForLayouting (LayoutingType.Y | LayoutingType.Height); + } else if (layoutType == LayoutingType.Height) { + if (Height == Measure.Fit) + return; + if (child.Height.Units == Unit.Percent) { + ltChild |= LayoutingType.Height; + if (child.Height.Value < 100 && child.Top == 0) + ltChild |= LayoutingType.Y; } - break; - } + } + if (ltChild == LayoutingType.None) + return; + child.RegisterForLayouting (ltChild); } public virtual void OnChildLayoutChanges (object sender, LayoutingEventArgs arg) - { - switch (arg.LayoutType) { - case LayoutingType.X: - break; - case LayoutingType.Y: - break; - case LayoutingType.Width: - if (Width < 0) - this.RegisterForLayouting (LayoutingType.Width); - break; - case LayoutingType.Height: - if (Height < 0) - this.RegisterForLayouting (LayoutingType.Height); - break; + { + GraphicObject g = sender as GraphicObject; + + if (arg.LayoutType == LayoutingType.Width) { + if (Width != Measure.Fit) + return; + contentSize.Width = g.Slot.Width; + this.RegisterForLayouting (LayoutingType.Width); + }else if (arg.LayoutType == LayoutingType.Height){ + if (Height != Measure.Fit) + return; + contentSize.Height = g.Slot.Height; + this.RegisterForLayouting (LayoutingType.Height); } } protected override void onDraw (Context gr) diff --git a/src/LayoutingQueueItem.cs b/src/LayoutingQueueItem.cs index 5fbde097..933fc57c 100644 --- a/src/LayoutingQueueItem.cs +++ b/src/LayoutingQueueItem.cs @@ -24,6 +24,7 @@ using System.Collections.Generic; namespace Crow { + [Flags] public enum LayoutingType : byte { None = 0x00, diff --git a/src/Measure.cs b/src/Measure.cs index 4fbac421..64e5cc98 100644 --- a/src/Measure.cs +++ b/src/Measure.cs @@ -30,7 +30,7 @@ namespace Crow public static Measure Fit = new Measure(-1); //TODO:implement stretching as 100%, not 0% - public static Measure Stretched = new Measure(0, Unit.Percent); + public static Measure Stretched = new Measure(100, Unit.Percent); public Measure (int _value, Unit _units = Unit.Pixel) { @@ -72,8 +72,8 @@ namespace Crow public override string ToString () { return Value == -1 ? "Fit" : - Value == 0 ? "Stretched" : - Units == Unit.Percent ? Value.ToString () + "%" : Value.ToString (); + Units == Unit.Percent ? Value == 100 ? "Stretched" : + Value.ToString () + "%" : Value.ToString (); } public static Measure Parse(string s){ if (string.IsNullOrEmpty (s)) -- 2.47.3