proper dock panels with attached dock properties

This commit is contained in:
Dylan Wilson
2018-03-09 23:34:39 +10:00
parent a206ed4143
commit 6fe96395b8
5 changed files with 95 additions and 40 deletions
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using Microsoft.Xna.Framework;
using MonoGame.Extended.BitmapFonts;
@@ -211,5 +212,22 @@ namespace MonoGame.Extended.Gui.Controls
public Rectangle? ClippingRectangle;
public Vector2 Position;
}
public Dictionary<string, object> AttachedProperties { get; } = new Dictionary<string, object>();
public object GetAttachedProperty(string name)
{
object value;
if (AttachedProperties.TryGetValue(name, out value))
return value;
return null;
}
public void SetAttachedProperty(string name, object value)
{
AttachedProperties[name] = value;
}
}
}
@@ -0,0 +1,66 @@
using System;
namespace MonoGame.Extended.Gui.Controls
{
public enum Dock
{
Left, Right, Top, Bottom
}
public class DockPanel : LayoutControl
{
public override Size2 GetContentSize(IGuiContext context)
{
return new Size2();
}
public override void Layout(IGuiContext context, RectangleF rectangle)
{
for (var i = 0; i < Items.Count; i++)
{
var control = Items[i];
if (LastChildFill && i == Items.Count - 1)
{
PlaceControl(context, control, rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height);
}
else
{
var actualSize = control.GetActualSize(context);
var dock = control.GetAttachedProperty(DockProperty) as Dock? ?? Dock.Left;
switch (dock)
{
case Dock.Left:
PlaceControl(context, control, rectangle.Left, rectangle.Top, actualSize.Width,
rectangle.Height);
rectangle.X += actualSize.Width;
rectangle.Width -= actualSize.Width;
break;
case Dock.Right:
PlaceControl(context, control, rectangle.Right - actualSize.Width, rectangle.Top,
actualSize.Width, rectangle.Height);
rectangle.Width -= actualSize.Width;
break;
case Dock.Top:
PlaceControl(context, control, rectangle.Left, rectangle.Top, rectangle.Width,
actualSize.Height);
rectangle.Y += actualSize.Height;
rectangle.Height -= actualSize.Height;
break;
case Dock.Bottom:
PlaceControl(context, control, rectangle.Left, rectangle.Bottom - actualSize.Height,
rectangle.Width, actualSize.Height);
rectangle.Height -= actualSize.Height;
break;
default:
throw new ArgumentOutOfRangeException();
}
}
}
}
public const string DockProperty = "Dock";
public bool LastChildFill { get; set; } = true;
}
}
@@ -41,36 +41,6 @@ namespace MonoGame.Extended.Gui.Controls
return new Size2(width, height);
}
//protected override Size2 CalculateDesiredSize(IGuiContext context, Size2 availableSize)
//{
// var width = 0f;
// var height = 0f;
// foreach (var control in Items)
// {
// var desiredSize = LayoutHelper.GetSizeWithMargins(control, context, availableSize);
// switch (Orientation)
// {
// case Orientation.Horizontal:
// width += desiredSize.Width;
// height = desiredSize.Height > height ? desiredSize.Height : height;
// break;
// case Orientation.Vertical:
// width = desiredSize.Width > width ? desiredSize.Width : width;
// height += desiredSize.Height;
// break;
// default:
// throw new InvalidOperationException($"Unexpected orientation {Orientation}");
// }
// }
// width += Orientation == Orientation.Horizontal ? (Items.Count - 1) * Spacing : 0;
// height += Orientation == Orientation.Vertical ? (Items.Count - 1) * Spacing : 0;
// return new Size2(width, height);
//}
public override void Layout(IGuiContext context, RectangleF rectangle)
{
var x = 0f;
@@ -79,23 +49,23 @@ namespace MonoGame.Extended.Gui.Controls
foreach (var control in Items)
{
var desiredSize = control.GetActualSize(context);
var actualSize = control.GetActualSize(context);
switch (Orientation)
{
case Orientation.Vertical:
control.VerticalAlignment = VerticalAlignment.Top;
//control.VerticalAlignment = VerticalAlignment.Top;
PlaceControl(context, control, 0f, y, Width, desiredSize.Height);
y += desiredSize.Height + Spacing;
availableSize.Height -= desiredSize.Height;
PlaceControl(context, control, 0f, y, Width, actualSize.Height);
y += actualSize.Height + Spacing;
availableSize.Height -= actualSize.Height;
break;
case Orientation.Horizontal:
control.HorizontalAlignment = HorizontalAlignment.Left;
//control.HorizontalAlignment = HorizontalAlignment.Left;
PlaceControl(context, control, x, 0f, desiredSize.Width, Height);
x += desiredSize.Width + Spacing;
availableSize.Height -= desiredSize.Height;
PlaceControl(context, control, x, 0f, actualSize.Width, Height);
x += actualSize.Width + Spacing;
availableSize.Height -= actualSize.Height;
break;
default:
throw new InvalidOperationException($"Unexpected orientation {Orientation}");
@@ -46,6 +46,7 @@
<Compile Include="Controls\Control.cs" />
<Compile Include="Controls\ControlCollection.cs" />
<Compile Include="Controls\Dialog.cs" />
<Compile Include="Controls\DockPanel.cs" />
<Compile Include="Controls\Form.cs" />
<Compile Include="Controls\SelectorControl.cs" />
<Compile Include="Controls\Label.cs" />
+1 -1
View File
@@ -130,7 +130,7 @@ namespace MonoGame.Extended.Gui
{nameof(Control.BorderColor), new Color(67, 67, 70)},
{nameof(Control.BorderThickness), 1},
{nameof(Control.TextColor), new Color(241, 241, 241)},
{nameof(Control.Margin), new Thickness(5)},
//{nameof(Control.Margin), new Thickness(5)},
{nameof(Control.Padding), new Thickness(5)},
},
new ControlStyle(typeof(LayoutControl)) {