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

326 lines
11 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;
}
2024-08-09 13:52:49 +00:00
if ( control is UIRegion )
{
var childUIRegion = (UIRegion) control;
childUIRegion.Layout();
}
else if ( control is UIImage )
2024-08-09 13:52:49 +00:00
{
2024-09-14 06:41:52 +00:00
var tw = 0;
var th = 0;
if ( control is UIImage )
{
var uiImage = (UIImage) control;
2024-08-09 13:52:49 +00:00
2024-09-14 06:41:52 +00:00
if ( uiImage.Texture == null )
{
uiImage.Size = new Vector2( 0, 0 );
return;
}
tw = uiImage.Texture.GetWidth();
th = uiImage.Texture.GetHeight();
}
var container = (UIStylePropertyContainer) control;
2024-08-09 13:52:49 +00:00
2024-09-14 06:41:52 +00:00
var w = UINumber.Compute( control, UIStyleNumberProperty.Width, tw, tw / 100f );
var h = UINumber.Compute( control, UIStyleNumberProperty.Height, th, th / 100f );
2024-08-09 13:52:49 +00:00
// RJLog.Log( "Set Image Size", w, h );
2024-09-14 06:41:52 +00:00
control.Size = new Vector2( w, h );
2024-08-09 13:52:49 +00:00
2024-09-14 06:41:52 +00:00
if ( control is UIImage )
{
var uiImage = (UIImage) control;
if ( uiImage.Material != null )
{
2025-06-19 17:22:25 +00:00
var ui = uiImage.GetUI();
2024-09-14 06:41:52 +00:00
if ( ui == null )
{
2025-06-19 17:22:25 +00:00
RJLog.Log( "No UI Found", HierarchyName.Of( uiImage ) );
2024-09-14 06:41:52 +00:00
return;
}
2024-11-13 11:57:10 +00:00
if ( ui.settings == null )
{
2025-06-19 17:22:25 +00:00
RJLog.Log( "No UI settings Found", HierarchyName.Of( uiImage ) );
2024-11-13 11:57:10 +00:00
return;
}
if ( ui.settings.sizePropertyName == null )
{
2025-06-19 17:22:25 +00:00
// RJLog.Log( "No UI.settings.sizePropertyName Found" );
2024-11-13 11:57:10 +00:00
return;
}
2024-09-14 06:41:52 +00:00
//RJLog.Log( "Setting Size", ui.settings.sizePropertyName.propertyName, HierarchyName.Of( uiImage ) );
ui.settings.sizePropertyName.Set( uiImage.Material, uiImage.Size );
2025-06-19 17:22:25 +00:00
// UIShaderProperties.UpdatePropertiesInHierarchy( uiImage, uiImage.Material );
var colorProperties = uiImage.imageType != null ? uiImage.imageType.GetColorShaderProperties() : [];
var colorPropertyName = new ColorPropertyName();
foreach ( var c in colorProperties )
{
// uiImage.LogInfo( c );
var color = UIColor.Compute( control, UIStyleColorProperty.ColorShaderProperty, c, Colors.White );
colorPropertyName.propertyName = c;
colorPropertyName.Set( uiImage.Material, color );
}
var numberProperties = uiImage.imageType != null ? uiImage.imageType.GetNumberShaderProperties() : [];
var numberPropertyName = new FloatPropertyName();
foreach ( var n in numberProperties )
{
// uiImage.LogInfo( c );
var value = UINumber.Compute( control, UIStyleNumberProperty.FloatShaderProperty, n );
numberPropertyName.propertyName = n;
numberPropertyName.Set( uiImage.Material, value );
}
2024-09-14 06:41:52 +00:00
return;
}
}
2024-08-09 13:52:49 +00:00
}
2024-09-14 06:41:52 +00:00
else if ( control is UIText )
{
var text = (UIText) control;
var container = (UIStylePropertyContainer) control;
2025-06-19 17:22:25 +00:00
text.uiTextLabelSettings.FontSize = Mathf.Max( 1,
UINumber.ComputeInt( control, UIStyleNumberProperty.FontSize, UINumber.em( control ), UINumber.em( control ) / 100f ) );
2024-09-14 06:41:52 +00:00
text.uiTextLabelSettings.FontColor =
2025-06-19 17:22:25 +00:00
UIColor.Compute( control, UIStyleColorProperty.FontColor, "", Colors.White );
2024-09-14 06:41:52 +00:00
text.uiTextLabelSettings.OutlineSize =
UINumber.ComputeInt( control, UIStyleNumberProperty.FontOutlineSize, 0 );
text.uiTextLabelSettings.OutlineColor =
2025-06-19 17:22:25 +00:00
UIColor.Compute( control, UIStyleColorProperty.FontOutlineColor, "", Colors.Transparent );
2024-09-14 06:41:52 +00:00
text.uiTextLabelSettings.ShadowSize =
UINumber.ComputeInt( control, UIStyleNumberProperty.FontShadowSize, 0 );
text.uiTextLabelSettings.ShadowColor =
2025-06-19 17:22:25 +00:00
UIColor.Compute( control, UIStyleColorProperty.FontShadowColor, "", Colors.Black );
2024-09-14 06:41:52 +00:00
text.uiTextLabelSettings.ShadowOffset = new Vector2(
UINumber.Compute( control, UIStyleNumberProperty.FontShadowOffsetX, 0 ),
UINumber.Compute( control, UIStyleNumberProperty.FontShadowOffsetY, 0 )
);
2024-09-14 06:41:52 +00:00
control.UpdateMinimumSize();
if ( text.alwaysMinimumSize )
2025-06-19 17:22:25 +00:00
{
if ( text.AutowrapMode == TextServer.AutowrapMode.Word )
{
text.AutowrapMode = TextServer.AutowrapMode.Off;
var minSize = text.GetMinimumSize();
text.AutowrapMode = TextServer.AutowrapMode.Word;
var w = UINumber.Compute( control, UIStyleNumberProperty.Width, minSize.X, minSize.X / 100f );
var h = UINumber.Compute( control, UIStyleNumberProperty.Height, minSize.Y, minSize.Y / 100f );
control.CustomMinimumSize = new Vector2( Mathf.Min( minSize.X, w ), 0 );
}
control.Size = control.GetMinimumSize();
}
else
{
var minSize = control.GetMinimumSize();
var w = UINumber.Compute( control, UIStyleNumberProperty.Width, minSize.X, minSize.X / 100f );
var h = UINumber.Compute( control, UIStyleNumberProperty.Height, minSize.Y, minSize.Y / 100f );
// RJLog.Log( "Set Image Size", w, h );
control.Size = new Vector2( w, h );
}
2024-09-14 06:41:52 +00:00
}
2024-08-09 13:52:49 +00:00
else
{
2024-09-14 06:41:52 +00:00
// control.UpdateMinimumSize();
// control.Size = control.GetMinimumSize();
2024-08-09 13:52:49 +00:00
}
UILayouting.UpdatePivot( control );
}
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;
}
}
}