rj-action-library/Runtime/UI/Layouts/UILayouting.cs

190 lines
5.6 KiB
C#
Raw Normal View History

2024-08-09 13:52:49 +00:00
2025-06-19 17:22:25 +00:00
using System.Linq;
2024-08-09 13:52:49 +00:00
using Godot;
using Rokojori;
namespace Rokojori
{
public class UILayouting
{
2024-09-14 06:41:52 +00:00
public static Vector2 GetContentSize( Control control )
{
if ( control is UIRegion )
{
return ( (UIRegion) control ).contentSize;
}
return control.Size;
}
public static Vector2 GetContentOffset( Control control )
{
if ( control is UIRegion )
{
return ( (UIRegion) control ).contentOffset;
}
return control.Size;
}
2024-08-09 13:52:49 +00:00
public static void UpdateChild( Control control )
{
2024-09-14 06:41:52 +00:00
if ( ! control.Visible )
{
return;
}
2025-06-23 11:16:01 +00:00
if ( control is UIRegion region )
2024-08-09 13:52:49 +00:00
{
2025-06-23 11:16:01 +00:00
region.Layout();
2024-08-09 13:52:49 +00:00
}
2025-06-23 11:16:01 +00:00
else if ( control is UIImage uiImage )
2024-08-09 13:52:49 +00:00
{
2025-06-23 11:16:01 +00:00
uiImage.Update();
uiImage.CommitUpdateInfo();
2024-08-09 13:52:49 +00:00
}
2025-06-23 11:16:01 +00:00
else if ( control is UIText uiText )
2024-09-14 06:41:52 +00:00
{
2025-06-23 11:16:01 +00:00
uiText.Update();
uiText.CommitUpdateInfo();
2024-09-14 06:41:52 +00:00
}
2024-08-09 13:52:49 +00:00
else
{
2025-06-23 11:16:01 +00:00
2024-08-09 13:52:49 +00:00
}
2025-06-23 11:16:01 +00:00
UpdatePivot( control );
2024-08-09 13:52:49 +00:00
}
2025-06-23 11:16:01 +00:00
2024-09-14 06:41:52 +00:00
public static void SetPositionInParentAnchor( UIStylePropertyContainer container )
2024-08-11 17:38:06 +00:00
{
2024-09-14 06:41:52 +00:00
var control = (Control) container;
2024-08-11 17:38:06 +00:00
var p = NodesWalker.Get().Parent( control ) as Control;
var pWidth = p == null ? UI.GetWindowWidth( control ) : UILayouting.GetWidth( p );
var pHeight = p == null ? UI.GetWindowHeight( control ) : UILayouting.GetHeight( p );
var x = p.Position.X;
var y = p.Position.Y;
2024-09-14 06:41:52 +00:00
if ( ! UINumber.IsNullOrNone( container, UIStyleNumberProperty.Left ) )
2024-08-11 17:38:06 +00:00
{
2024-09-14 06:41:52 +00:00
var left = UINumber.Compute( control, UIStyleNumberProperty.Left, 0 );
2024-08-11 17:38:06 +00:00
x = left;
}
2024-09-14 06:41:52 +00:00
else if ( ! UINumber.IsNullOrNone( container, UIStyleNumberProperty.Right ) )
2024-08-11 17:38:06 +00:00
{
2024-09-14 06:41:52 +00:00
var right = UINumber.Compute( control, UIStyleNumberProperty.Right, 0 );
2024-08-11 17:38:06 +00:00
x = ( pWidth - UILayouting.GetWidth( control ) ) - right;
}
2024-09-14 06:41:52 +00:00
if ( ! UINumber.IsNullOrNone( container, UIStyleNumberProperty.Top ))
2024-08-11 17:38:06 +00:00
{
2024-09-14 06:41:52 +00:00
var top = UINumber.Compute( control, UIStyleNumberProperty.Top, 0 );
2024-08-11 17:38:06 +00:00
y = top;
}
2024-09-14 06:41:52 +00:00
else if ( ! UINumber.IsNullOrNone( container, UIStyleNumberProperty.Bottom ) )
2024-08-11 17:38:06 +00:00
{
2024-09-14 06:41:52 +00:00
var bottom = UINumber.Compute( control, UIStyleNumberProperty.Bottom, 0 );
2024-08-11 17:38:06 +00:00
y = ( pHeight - UILayouting.GetHeight( control ) ) - bottom;
}
2024-09-14 06:41:52 +00:00
// var margin = UINumber.Compute( control, UIStyle.Margin( container ), 0 );
// var marginLeft = margin + UINumber.Compute( control, UIStyle.MarginLeft( container ), 0 );
// var marginTop = margin + UINumber.Compute( control, UIStyle.MarginRight( container ), 0 );
// var marginRight = margin + UINumber.Compute( control, UIStyle.MarginRight( container ), 0 );
// var marginBottom = margin + UINumber.Compute( control, UIStyle.MarginBottom( container ), 0 );
2024-08-11 17:38:06 +00:00
2024-09-14 06:41:52 +00:00
// UILayouting.SetPosition( control, new Vector2( x - ( marginLeft + marginRight ), y - ( marginTop + marginBottom ) ) );
UILayouting.SetPosition( control, new Vector2( x, y ) );
2024-08-11 17:38:06 +00:00
}
2024-08-09 13:52:49 +00:00
public static void UpdatePivot( Control c )
{
if ( ! ( c is UIImage || c is UIRegion || c is UIText ) )
{
return;
}
var container = c as UIStylePropertyContainer;
2024-09-14 06:41:52 +00:00
var pivotX = UINumber.Compute( c, UIStyleNumberProperty.PivotX, 0.5f * c.Size.X, c.Size.X );
var pivotY = UINumber.Compute( c, UIStyleNumberProperty.PivotY, 0.5f * c.Size.Y, c.Size.Y );
2024-08-09 13:52:49 +00:00
if ( c is UIText text && ! text.alwaysMinimumSize )
{
pivotX = 0;
pivotY = 0;
}
2024-08-09 13:52:49 +00:00
c.PivotOffset = new Vector2( pivotX, pivotY );
2024-08-09 13:52:49 +00:00
c.Rotation = MathX.DegreesToRadians * UINumber.Compute( c, UIStyleNumberProperty.Rotation, 0 );
2024-08-09 13:52:49 +00:00
2024-09-14 06:41:52 +00:00
var scale = UINumber.Compute( c, UIStyleNumberProperty.Scale, 1, 1 );
2024-08-09 13:52:49 +00:00
c.Scale = new Vector2(
2024-09-14 06:41:52 +00:00
UINumber.Compute( c, UIStyleNumberProperty.ScaleX, 1, 1 ) ,
UINumber.Compute( c, UIStyleNumberProperty.ScaleY, 1, 1 )
) * scale;
2024-08-09 13:52:49 +00:00
}
public static void SetPosition( Control c, Vector2 position )
{
if ( UIStyling.HasInnerMargins( c ) )
{
var container = c as UIStylePropertyContainer;
2024-09-14 06:41:52 +00:00
var margin = UINumber.Compute( c, UIStyleNumberProperty.Margin, 0 );
var marginLeft = margin + UINumber.Compute( c, UIStyleNumberProperty.MarginLeft, 0 );
var marginTop = margin + UINumber.Compute( c, UIStyleNumberProperty.MarginTop, 0 );
2024-08-09 13:52:49 +00:00
position.X += marginLeft;
position.Y += marginTop;
}
c.Position = position;
}
public static float GetWidth( Control c )
{
if ( UIStyling.HasInnerMargins( c ) )
{
var container = c as UIStylePropertyContainer;
2024-09-14 06:41:52 +00:00
var margin = UINumber.Compute( c, UIStyleNumberProperty.Margin, 0 );
var marginLeft = margin + UINumber.Compute( c, UIStyleNumberProperty.MarginLeft, 0 );
var marginRight = margin + UINumber.Compute( c, UIStyleNumberProperty.MarginRight, 0 );
2024-08-09 13:52:49 +00:00
return c.Size.X + marginLeft + marginRight;
}
return c.Size.X;
}
public static float GetHeight( Control c )
{
if ( UIStyling.HasInnerMargins( c ) )
{
var container = c as UIStylePropertyContainer;
2024-09-14 06:41:52 +00:00
var margin = UINumber.Compute( c, UIStyleNumberProperty.Margin, 0 );
var marginTop = margin + UINumber.Compute( c, UIStyleNumberProperty.MarginTop, 0 );
var marginBottom = margin + UINumber.Compute( c, UIStyleNumberProperty.MarginBottom, 0 );
2024-08-09 13:52:49 +00:00
return c.Size.Y + marginTop + marginBottom;
}
return c.Size.Y;
}
}
}