Fix control Offsets updating for control bounds when changing anchors

#312
This commit is contained in:
Wojtek Figat
2021-03-18 13:16:17 +01:00
parent 6a6eb96793
commit 5c171c8b58

View File

@@ -223,53 +223,56 @@ namespace FlaxEngine.GUI
set
{
if (!_bounds.Equals(ref value))
{
// Calculate anchors based on the parent container client area
Margin anchors;
if (_parent != null)
{
_parent.GetDesireClientArea(out var parentBounds);
anchors = new Margin
(
_anchorMin.X * parentBounds.Size.X + parentBounds.Location.X,
_anchorMax.X * parentBounds.Size.X,
_anchorMin.Y * parentBounds.Size.Y + parentBounds.Location.Y,
_anchorMax.Y * parentBounds.Size.Y
);
}
else
{
anchors = Margin.Zero;
}
// Calculate offsets on X axis
_offsets.Left = value.Location.X - anchors.Left;
if (_anchorMin.X != _anchorMax.X)
{
_offsets.Right = anchors.Right - value.Location.X - value.Size.X;
}
else
{
_offsets.Right = value.Size.X;
}
// Calculate offsets on Y axis
_offsets.Top = value.Location.Y - anchors.Top;
if (_anchorMin.Y != _anchorMax.Y)
{
_offsets.Bottom = anchors.Bottom - value.Location.Y - value.Size.Y;
}
else
{
_offsets.Bottom = value.Size.Y;
}
// Flush the control bounds
UpdateBounds();
}
SetBounds(ref value);
}
}
private void SetBounds(ref Rectangle value)
{
// Calculate anchors based on the parent container client area
Margin anchors;
if (_parent != null)
{
_parent.GetDesireClientArea(out var parentBounds);
anchors = new Margin
(
_anchorMin.X * parentBounds.Size.X + parentBounds.Location.X,
_anchorMax.X * parentBounds.Size.X,
_anchorMin.Y * parentBounds.Size.Y + parentBounds.Location.Y,
_anchorMax.Y * parentBounds.Size.Y
);
}
else
{
anchors = Margin.Zero;
}
// Calculate offsets on X axis
_offsets.Left = value.Location.X - anchors.Left;
if (_anchorMin.X != _anchorMax.X)
{
_offsets.Right = anchors.Right - value.Location.X - value.Size.X;
}
else
{
_offsets.Right = value.Size.X;
}
// Calculate offsets on Y axis
_offsets.Top = value.Location.Y - anchors.Top;
if (_anchorMin.Y != _anchorMax.Y)
{
_offsets.Bottom = anchors.Bottom - value.Location.Y - value.Size.Y;
}
else
{
_offsets.Bottom = value.Size.Y;
}
// Flush the control bounds
UpdateBounds();
}
/// <summary>
/// Gets or sets the scale.
/// </summary>
@@ -553,7 +556,7 @@ namespace FlaxEngine.GUI
}
bounds.Location += parentBounds.Location;
}
Bounds = bounds;
SetBounds(ref bounds);
}
return;
}