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 SetPositionInParentAnchor( UIStylePropertyContainer region ) { var control = (Control) region; 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; if ( ! UINumber.IsNullOrNone( UIStyle.Left( region ) )) { var left = UINumber.Compute( control, UIStyle.Left( region ), 0 ); x = left; } else if ( ! UINumber.IsNullOrNone( UIStyle.Right( region ) ) ) { var right = UINumber.Compute( control, UIStyle.Right( region ), 0 ); x = ( pWidth - UILayouting.GetWidth( control ) ) - right; } if ( ! UINumber.IsNullOrNone( UIStyle.Top( region ) )) { var top = UINumber.Compute( control, UIStyle.Top( region ), 0 ); y = top; } else if ( ! UINumber.IsNullOrNone( UIStyle.Bottom( region ) ) ) { var bottom = UINumber.Compute( control, UIStyle.Bottom( region ), 0 ); y = ( pHeight - UILayouting.GetHeight( control ) ) - bottom; } UILayouting.SetPosition( control, new Vector2( x, y ) ); } 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.5f * c.Size.X, c.Size.X ); var pivotY = UINumber.Compute( c, UIStyle.PivotY( container ), 0.5f * c.Size.Y, 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; } } }