Add ControlChildSize option to disable autosizing child controls in UI panel

#2511
This commit is contained in:
Wojtek Figat
2024-09-18 23:19:45 +02:00
parent c9817b25a3
commit a8354720df
3 changed files with 19 additions and 5 deletions

View File

@@ -22,6 +22,8 @@ namespace FlaxEngine.GUI
base.PerformLayoutBeforeChildren();
// Pre-set height of all controls
if (!ControlChildSize)
return;
float h = Height - _margin.Height;
for (int i = 0; i < _children.Count; i++)
{
@@ -47,16 +49,17 @@ namespace FlaxEngine.GUI
if (c.Visible)
{
var w = c.Width;
var ch = ControlChildSize ? h : c.Height;
if (Mathf.IsZero(c.AnchorMin.X) && Mathf.IsZero(c.AnchorMax.X))
{
c.Bounds = new Rectangle(left + _offset.X, _margin.Top + _offset.Y, w, h);
c.Bounds = new Rectangle(left + _offset.X, _margin.Top + _offset.Y, w, ch);
left = c.Right + _spacing;
hasAnyLeft = true;
}
else if (Mathf.IsOne(c.AnchorMin.X) && Mathf.IsOne(c.AnchorMax.X))
{
right += w + _spacing;
c.Bounds = new Rectangle(Width - right + _offset.X, _margin.Top + _offset.Y, w, h);
c.Bounds = new Rectangle(Width - right + _offset.X, _margin.Top + _offset.Y, w, ch);
hasAnyRight = true;
}
}

View File

@@ -1,5 +1,7 @@
// Copyright (c) 2012-2024 Wojciech Figat. All rights reserved.
using System.ComponentModel;
namespace FlaxEngine.GUI
{
/// <summary>
@@ -133,7 +135,7 @@ namespace FlaxEngine.GUI
/// <summary>
/// Gets or sets the value indicating whenever the panel size will be based on a children dimensions.
/// </summary>
[EditorOrder(30), Tooltip("If checked, the panel size will be based on a children dimensions.")]
[EditorOrder(30), DefaultValue(true), Tooltip("If checked, the panel size will be based on a children dimensions.")]
public bool AutoSize
{
get => _autoSize;
@@ -147,6 +149,12 @@ namespace FlaxEngine.GUI
}
}
/// <summary>
/// Gets or sets the value indicating whenever the panel can resize children controls (eg. auto-fit width/height).
/// </summary>
[EditorOrder(35), DefaultValue(true), Tooltip("If checked, the panel can resize children controls (eg. auto-fit width/height).")]
public bool ControlChildSize { get; set; } = true;
/// <summary>
/// Gets or sets the panel area margin.
/// </summary>

View File

@@ -22,6 +22,8 @@ namespace FlaxEngine.GUI
base.PerformLayoutBeforeChildren();
// Pre-set width of all controls
if (!ControlChildSize)
return;
float w = Width - _margin.Width;
for (int i = 0; i < _children.Count; i++)
{
@@ -47,16 +49,17 @@ namespace FlaxEngine.GUI
if (c.Visible)
{
var h = c.Height;
var cw = ControlChildSize ? w : c.Width;
if (Mathf.IsZero(c.AnchorMin.Y) && Mathf.IsZero(c.AnchorMax.Y))
{
c.Bounds = new Rectangle(_margin.Left + _offset.X, top + _offset.Y, w, h);
c.Bounds = new Rectangle(_margin.Left + _offset.X, top + _offset.Y, cw, h);
top = c.Bottom + _spacing;
hasAnyTop = true;
}
else if (Mathf.IsOne(c.AnchorMin.Y) && Mathf.IsOne(c.AnchorMax.Y))
{
bottom += h + _spacing;
c.Bounds = new Rectangle(_margin.Left + _offset.X, Height - bottom + _offset.Y, w, h);
c.Bounds = new Rectangle(_margin.Left + _offset.X, Height - bottom + _offset.Y, cw, h);
hasAnyBottom = true;
}
}