using Godot; using Rokojori; namespace Rokojori { public class UILayouting { public static void UpdateChild( Control control ) { if ( control is UIRegion ) { var childUIRegion = (UIRegion) control; childUIRegion.Layout(); } else if ( control is UIImage ) { var uiImage = (UIImage) control; if ( uiImage.Texture == null ) { uiImage.Size = new Vector2( 0, 0 ); return; } var tw = uiImage.Texture.GetWidth(); var th = uiImage.Texture.GetHeight(); var w = UINumber.Compute( control, uiImage.width, tw, tw / 100f ); var h = UINumber.Compute( control, uiImage.height, th, th / 100f ); uiImage.Size = new Vector2( w, h ); } else { control.UpdateMinimumSize(); control.Size = control.GetMinimumSize(); } UILayouting.UpdatePivot( control ); } public static void UpdatePivot( Control c ) { if ( ! ( c is UIImage || c is UIRegion || c is UIText ) ) { return; } var container = c as UIStylePropertyContainer; var pivotX = UINumber.Compute( c, UIStyle.PivotX( container ), 0, c.Size.X ); var pivotY = UINumber.Compute( c, UIStyle.PivotY( container ), 0, c.Size.Y ); c.PivotOffset = new Vector2( pivotX, pivotY ); c.Rotation = UINumber.Compute( c, UIStyle.Rotation( container ), 0 ); var scale = UINumber.Compute( c, UIStyle.Scale( container ), 1, 1 ); c.Scale = new Vector2( UINumber.Compute( c, UIStyle.ScaleX( container ), 1, 1 ) , UINumber.Compute( c, UIStyle.ScaleY( container ), 1, 1 ) ) * scale; } public static void SetPosition( Control c, Vector2 position ) { if ( UIStyling.HasInnerMargins( c ) ) { var container = c as UIStylePropertyContainer; var margin = UINumber.Compute( c, UIStyle.Margin( container ), 0 ); var marginLeft = margin + UINumber.Compute( c, UIStyle.MarginLeft( container ), 0 ); var marginTop = margin + UINumber.Compute( c, UIStyle.MarginTop( container ), 0 ); position.X += marginLeft; position.Y += marginTop; } c.Position = position; } public static float GetWidth( Control c ) { if ( UIStyling.HasInnerMargins( c ) ) { var container = c as UIStylePropertyContainer; var margin = UINumber.Compute( c, UIStyle.Margin( container ), 0 ); var marginLeft = margin + UINumber.Compute( c, UIStyle.MarginLeft( container ), 0 ); var marginRight = margin + UINumber.Compute( c, UIStyle.MarginRight( container ), 0 ); 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; var margin = UINumber.Compute( c, UIStyle.Margin( container ), 0 ); var marginTop = margin + UINumber.Compute( c, UIStyle.MarginTop( container ), 0 ); var marginBottom = margin + UINumber.Compute( c, UIStyle.MarginBottom( container ), 0 ); return c.Size.Y + marginTop + marginBottom; } return c.Size.Y; } } }