rj-action-library/Runtime/UI/Styling/UIStyle.cs

723 lines
17 KiB
C#
Raw Permalink Normal View History

2024-08-09 13:52:49 +00:00
using Godot;
using Rokojori;
2024-09-14 06:41:52 +00:00
using System.Collections.Generic;
2024-08-09 13:52:49 +00:00
namespace Rokojori
{
[Tool]
[GlobalClass]
2024-08-11 17:38:06 +00:00
public partial class UIStyle:Resource, UIStylePropertyContainer
2024-08-09 13:52:49 +00:00
{
[Export]
2024-08-11 17:38:06 +00:00
public UIStyle parentStyle;
2024-09-14 06:41:52 +00:00
[ExportGroup( "Layout" )]
2024-08-11 17:38:06 +00:00
[Export]
public UILayout layout;
[Export]
public UINumber horizontalAlignment;
2024-08-09 13:52:49 +00:00
[Export]
2024-08-11 17:38:06 +00:00
public UINumber verticalAlignment;
2024-09-14 06:41:52 +00:00
[Export]
public UINumber verticalPlacement;
2024-08-09 13:52:49 +00:00
[Export]
2024-08-11 17:38:06 +00:00
public UINumber elementSpacing;
[Export]
public UINumber lineSpacing;
2024-09-14 06:41:52 +00:00
[ExportGroup( "Size & Margins" )]
2024-08-11 17:38:06 +00:00
[Export]
public UINumber width;
[Export]
public UINumber height;
[Export]
public UINumber margin;
[Export]
public UINumber marginLeft;
[Export]
public UINumber marginTop;
[Export]
public UINumber marginRight;
[Export]
public UINumber marginBottom;
2024-09-14 06:41:52 +00:00
[ExportGroup( "Font" )]
2024-08-11 17:38:06 +00:00
[Export]
public Font font;
[Export]
public UINumber fontSize;
2024-08-09 13:52:49 +00:00
[Export]
2024-08-11 17:38:06 +00:00
public UIColor fontColor;
2024-08-09 13:52:49 +00:00
[Export]
2024-08-11 17:38:06 +00:00
public UINumber outlineSize;
2024-08-09 13:52:49 +00:00
[Export]
2024-08-11 17:38:06 +00:00
public UIColor outlineColor;
2024-08-09 13:52:49 +00:00
[Export]
2024-08-11 17:38:06 +00:00
public UINumber shadowSize;
2024-08-09 13:52:49 +00:00
[Export]
2024-08-11 17:38:06 +00:00
public UIColor shadowColor;
2024-09-14 06:41:52 +00:00
[Export]
public UINumber shadowOffsetX;
[Export]
public UINumber shadowOffsetY;
[ExportGroup( "Position" )]
2024-08-11 17:38:06 +00:00
[Export]
public UIPosition position;
[Export]
2024-09-14 06:41:52 +00:00
public UILineWrap lineWrap;
[Export]
2024-08-11 17:38:06 +00:00
public UINumber left;
[Export]
public UINumber top;
[Export]
public UINumber right;
[Export]
public UINumber bottom;
2024-09-14 06:41:52 +00:00
[ExportGroup("Rotation & Scale")]
[Export]
public UINumber pivotX;
[Export]
public UINumber pivotY;
[Export]
public UINumber rotation;
[Export]
public UINumber scale;
[Export]
public UINumber scaleX;
[Export]
public UINumber scaleY;
[ExportGroup( "Shader Properties" )]
[Export]
public ShaderUIColor[] colorProperties;
[Export]
public ShaderUINumber[] numberProperties;
public List<ActiveStyleTransition<UIColor,ColorPropertyName>> GetActiveShaderUIColorTransitions()
{
return null;
}
public List<ActiveStyleTransition<UINumber,FloatPropertyName>> GetActiveShaderUINumberTransitions()
{
return null;
}
[ExportGroup("Transitions")]
[Export]
public TransitionSettingsAll transitionSettings;
public TransitionSettingsAll GetTransitionSettingsAll()
{
return transitionSettings;
}
[Export]
public UINumberTransition[] numberTransitions;
public UINumberTransition[] GetNumberTransitions()
{
return numberTransitions;
}
public List<ActiveStyleTransition<UINumber,UIStyleNumberProperty>> GetActiveUINumberTransitions()
{
return null;
}
[Export]
public UIColorTransition[] colorTransitions;
public UIColorTransition[] GetColorTransitions()
{
return colorTransitions;
}
public List<ActiveStyleTransition<UIColor,UIStyleColorProperty>> GetActiveUIColorTransitions()
{
return null;;
}
2024-08-11 17:38:06 +00:00
public UIStyle GetUIStyleParent()
{
return parentStyle;
}
public UIPosition GetUIPosition()
{
return position;
}
2024-09-14 06:41:52 +00:00
public UILineWrap GetUILineWrap()
{
return lineWrap;
}
public UILayout GetUILayout()
{
return layout;
}
public ShaderUIColor[] GetShaderUIColors()
{
return colorProperties;
}
public ShaderUINumber[] GetShaderUINumbers()
{
return numberProperties;
}
2024-08-11 17:38:06 +00:00
public UINumber GetUIStyleNumberProperty( UIStyleNumberProperty property )
{
switch ( property )
{
case UIStyleNumberProperty.Left: return left;
case UIStyleNumberProperty.Right: return right;
case UIStyleNumberProperty.Top: return top;
case UIStyleNumberProperty.Bottom: return bottom;
2024-09-14 06:41:52 +00:00
case UIStyleNumberProperty.HorizontalAlignment: return horizontalAlignment;
case UIStyleNumberProperty.VerticalAlignment: return verticalAlignment;
case UIStyleNumberProperty.VerticalPlacement: return verticalPlacement;
2024-08-11 17:38:06 +00:00
case UIStyleNumberProperty.ElementSpacing: return elementSpacing;
case UIStyleNumberProperty.LineSpacing: return lineSpacing;
case UIStyleNumberProperty.Width: return width;
case UIStyleNumberProperty.Height: return height;
2024-09-14 06:41:52 +00:00
2024-08-11 17:38:06 +00:00
case UIStyleNumberProperty.Margin: return margin;
case UIStyleNumberProperty.MarginLeft: return marginLeft;
case UIStyleNumberProperty.MarginRight: return marginRight;
case UIStyleNumberProperty.MarginTop: return marginTop;
case UIStyleNumberProperty.MarginBottom: return marginBottom;
2024-09-14 06:41:52 +00:00
case UIStyleNumberProperty.FontSize: return fontSize;
case UIStyleNumberProperty.FontOutlineSize: return outlineSize;
case UIStyleNumberProperty.FontShadowSize: return shadowSize;
case UIStyleNumberProperty.FontShadowOffsetX: return shadowOffsetX;
case UIStyleNumberProperty.FontShadowOffsetY: return shadowOffsetY;
case UIStyleNumberProperty.PivotX: return pivotX;
case UIStyleNumberProperty.PivotY: return pivotY;
case UIStyleNumberProperty.Rotation: return rotation;
case UIStyleNumberProperty.Scale: return scale;
case UIStyleNumberProperty.ScaleX: return scaleX;
case UIStyleNumberProperty.ScaleY: return scaleY;
2024-08-11 17:38:06 +00:00
}
return null;
}
2024-09-14 06:41:52 +00:00
public UIColor GetUIStyleColorProperty( UIStyleColorProperty property )
{
switch ( property )
{
case UIStyleColorProperty.FontColor: return fontColor;
case UIStyleColorProperty.FontOutlineColor: return outlineColor;
case UIStyleColorProperty.FontShadowColor: return shadowColor;
}
return null;
}
2024-08-11 17:38:06 +00:00
public static UINumber GetReferenceableNumberProperty( UIStylePropertyContainer container, UIStyleNumberProperty property )
{
if ( container == null )
{
return null;
}
var ownProperty = container.GetUIStyleNumberProperty( property );
if ( ownProperty != null )
{
return ownProperty;
}
var style = container.GetUIStyleParent();
return GetReferenceableNumberProperty( style, property );
}
2024-09-14 06:41:52 +00:00
public static UIColor GetReferenceableColorProperty( UIStylePropertyContainer container, UIStyleColorProperty property )
2024-08-11 17:38:06 +00:00
{
2024-09-14 06:41:52 +00:00
if ( container == null )
{
return null;
}
2024-08-11 17:38:06 +00:00
2024-09-14 06:41:52 +00:00
var ownProperty = container.GetUIStyleColorProperty( property );
if ( ownProperty != null )
2024-08-11 17:38:06 +00:00
{
return ownProperty;
}
2024-09-14 06:41:52 +00:00
var style = container.GetUIStyleParent();
2024-08-11 17:38:06 +00:00
2024-09-14 06:41:52 +00:00
return GetReferenceableColorProperty( style, property );
}
2024-08-11 17:38:06 +00:00
2024-09-14 06:41:52 +00:00
public static UINumberTransition GetTransition( UIStylePropertyContainer container, UIStyleNumberProperty property )
2024-08-11 17:38:06 +00:00
{
2024-09-14 06:41:52 +00:00
switch ( property )
{
case UIStyleNumberProperty.FontSize:
case UIStyleNumberProperty.FontOutlineSize:
case UIStyleNumberProperty.FontShadowSize:
case UIStyleNumberProperty.FontShadowOffsetX:
case UIStyleNumberProperty.FontShadowOffsetY:
{
return _GetTransition( true, container, property );
}
}
return _GetTransition( false, container, property );
2024-08-11 17:38:06 +00:00
}
2024-08-09 13:52:49 +00:00
2024-09-14 06:41:52 +00:00
static UINumberTransition _GetTransition( bool inheritable, UIStylePropertyContainer container, UIStyleNumberProperty property )
2024-08-09 13:52:49 +00:00
{
2024-09-14 06:41:52 +00:00
var transitions = container.GetNumberTransitions();
var index = transitions == null ? -1 : Arrays.FindIndex( transitions, t => t != null && t.property == property );
if ( index != -1 )
{
return transitions[ index ];
}
var styleParent = container.GetUIStyleParent();
if ( styleParent == null )
{
return null;
}
var styleParentTransition = _GetTransition( false, styleParent, property );
if ( styleParentTransition != null )
{
return styleParentTransition;
}
if ( ! ( container is Control ) || ! inheritable )
{
return null;
}
var control = container as Control;
UINumberTransition parentTransition = null;
NodesWalker.Get().GetInParents( control,
it =>
{
if ( ! ( it is UIStylePropertyContainer ) )
{
return false;
}
var container = (UIStylePropertyContainer) it;
parentTransition = _GetTransition( true, container, property );
return parentTransition != null;
}
);
return parentTransition;
2024-08-09 13:52:49 +00:00
}
2024-09-14 06:41:52 +00:00
public static TransitionSettingsAll GetTransitionSettingsAll( UIStylePropertyContainer container )
2024-08-09 13:52:49 +00:00
{
2024-09-14 06:41:52 +00:00
var settings = container.GetTransitionSettingsAll();
if ( settings != null )
{
return settings;
}
var styleParent = container.GetUIStyleParent();
if ( styleParent == null )
{
return null;
}
return GetTransitionSettingsAll( styleParent );
2024-08-09 13:52:49 +00:00
}
2024-09-14 06:41:52 +00:00
2024-08-09 13:52:49 +00:00
2024-09-14 06:41:52 +00:00
/*public static TransitionSettings GetTransitionSettings( UIStylePropertyContainer container, FloatPropertyName floatProperty )
2024-08-09 13:52:49 +00:00
{
2024-09-14 06:41:52 +00:00
var transition = GetTransition( container, floatProperty );
if ( transition != null && transition.settings != null )
{
return transition.settings;
}
var containerSettings = container.GetTransitionSettingsAll();
if ( containerSettings != null )
{
return containerSettings;
}
var styleParent = container.GetUIStyleParent();
2024-08-09 13:52:49 +00:00
2024-09-14 06:41:52 +00:00
if ( styleParent == null )
{
return null;
}
return GetTransitionSettings( styleParent, floatProperty );
}
*/
public static TransitionSettings GetTransitionSettings( UIStylePropertyContainer container, UIStyleColorProperty colorProperty )
2024-08-09 13:52:49 +00:00
{
2024-09-14 06:41:52 +00:00
var transition = GetTransition( container, colorProperty );
if ( transition != null && transition.settings != null )
{
return transition.settings;
}
var containerSettings = container.GetTransitionSettingsAll();
if ( containerSettings != null )
{
return containerSettings;
}
var styleParent = container.GetUIStyleParent();
if ( styleParent == null )
{
return null;
}
return GetTransitionSettings( styleParent, colorProperty );
2024-08-09 13:52:49 +00:00
}
2024-09-14 06:41:52 +00:00
public static TransitionSettings GetTransitionSettings( UIStylePropertyContainer container, UIStyleNumberProperty numberProperty )
2024-08-09 13:52:49 +00:00
{
2024-09-14 06:41:52 +00:00
var transition = GetTransition( container, numberProperty );
2024-08-09 13:52:49 +00:00
2024-09-14 06:41:52 +00:00
if ( transition != null && transition.settings != null )
{
return transition.settings;
}
2024-08-09 13:52:49 +00:00
2024-09-14 06:41:52 +00:00
var containerSettings = container.GetTransitionSettingsAll();
2024-08-09 13:52:49 +00:00
2024-09-14 06:41:52 +00:00
if ( containerSettings != null )
{
return containerSettings;
}
var styleParent = container.GetUIStyleParent();
if ( styleParent == null )
{
return null;
}
return GetTransitionSettings( styleParent, numberProperty );
2024-08-09 13:52:49 +00:00
}
2024-09-14 06:41:52 +00:00
public static UIColorTransition GetTransition( UIStylePropertyContainer container, UIStyleColorProperty property )
2024-08-09 13:52:49 +00:00
{
2024-09-14 06:41:52 +00:00
switch ( property )
{
case UIStyleColorProperty.FontColor:
case UIStyleColorProperty.FontOutlineColor:
case UIStyleColorProperty.FontShadowColor:
{
return _GetTransition( true, container, property );
}
}
return _GetTransition( false, container, property );
2024-08-09 13:52:49 +00:00
}
2024-09-14 06:41:52 +00:00
static UIColorTransition _GetTransition( bool inheritable, UIStylePropertyContainer container, UIStyleColorProperty property )
2024-08-09 13:52:49 +00:00
{
2024-09-14 06:41:52 +00:00
var transitions = container.GetColorTransitions();
var index = transitions == null ? -1 : Arrays.FindIndex( transitions, t => t != null && t.property == property );
if ( index != -1 )
{
return transitions[ index ];
}
var styleParent = container.GetUIStyleParent();
if ( styleParent == null )
{
return null;
}
var styleParentTransition = _GetTransition( false, styleParent, property );
if ( styleParentTransition != null )
{
return styleParentTransition;
}
if ( ! ( container is Control ) || ! inheritable )
{
return null;
}
var control = container as Control;
UIColorTransition parentTransition = null;
NodesWalker.Get().GetInParents( control,
it =>
{
if ( ! ( it is UIStylePropertyContainer ) )
{
return false;
}
var container = (UIStylePropertyContainer) it;
parentTransition = _GetTransition( true, container, property );
return parentTransition != null;
}
);
return parentTransition;
2024-08-09 13:52:49 +00:00
}
2024-09-14 06:41:52 +00:00
public static UINumber GetInheritableNumberProperty( UIStylePropertyContainer container, UIStyleNumberProperty property )
2024-08-09 13:52:49 +00:00
{
2024-09-14 06:41:52 +00:00
if ( container == null )
{
return null;
}
2024-08-09 13:52:49 +00:00
2024-09-14 06:41:52 +00:00
var ownProperty = container.GetUIStyleNumberProperty( property );
if ( ownProperty != null )
{
return ownProperty;
}
var parentStyle = container.GetUIStyleParent();
var parentStyleProperty = GetReferenceableNumberProperty( parentStyle, property );
if ( parentStyleProperty != null )
{
return parentStyleProperty;
}
if ( ! ( container is Control ) )
{
return null;
}
var control = container as Control;
UINumber parentNumber = null;
NodesWalker.Get().GetInParents( control,
it =>
{
if ( ! ( it is UIStylePropertyContainer ) )
{
return false;
}
var container = (UIStylePropertyContainer) it;
parentNumber = GetReferenceableNumberProperty( container, property );
return parentNumber != null;
}
);
return parentNumber;
}
public static UIColor GetInheritableColorProperty( UIStylePropertyContainer container, UIStyleColorProperty property )
2024-08-09 13:52:49 +00:00
{
2024-09-14 06:41:52 +00:00
if ( container == null )
{
return null;
}
var ownProperty = container.GetUIStyleColorProperty( property );
if ( ownProperty != null )
{
return ownProperty;
}
var parentStyle = container.GetUIStyleParent();
var parentStyleProperty = GetReferenceableColorProperty( parentStyle, property );
if ( parentStyleProperty != null )
{
return parentStyleProperty;
}
if ( ! ( container is Control ) )
{
return null;
}
var control = container as Control;
UIColor parentColor = null;
NodesWalker.Get().GetInParents( control,
it =>
{
if ( ! ( it is UIStylePropertyContainer ) )
{
return false;
}
var container = (UIStylePropertyContainer) it;
parentColor = GetReferenceableColorProperty( container, property );
return parentColor != null;
}
);
return parentColor;
}
public static UIPosition Position( UIStylePropertyContainer container )
2024-08-09 13:52:49 +00:00
{
2024-09-14 06:41:52 +00:00
if ( container == null )
{
return UIPosition.___;
}
var ownProperty = container.GetUIPosition();
if ( ownProperty != UIPosition.___ )
{
return ownProperty;
}
var parent = container.GetUIStyleParent();
if ( parent == null )
{
return UIPosition.___;
}
return parent.GetUIPosition();
2024-08-09 13:52:49 +00:00
}
2024-09-14 06:41:52 +00:00
public static UILayout Layout( UIStylePropertyContainer container )
2024-08-09 13:52:49 +00:00
{
2024-09-14 06:41:52 +00:00
if ( container == null )
{
return UILayout.___;
}
var ownProperty = container.GetUILayout();
if ( ownProperty != UILayout.___ )
{
return ownProperty;
}
var parent = container.GetUIStyleParent();
if ( parent == null )
{
return UILayout.___;
}
return parent.GetUILayout();
2024-08-09 13:52:49 +00:00
}
2024-09-14 06:41:52 +00:00
public static UILineWrap LineWrap( UIStylePropertyContainer container )
2024-08-09 13:52:49 +00:00
{
2024-09-14 06:41:52 +00:00
if ( container == null )
{
return UILineWrap.___;
}
var ownProperty = container.GetUILineWrap();
if ( ownProperty != UILineWrap.___ )
{
return ownProperty;
}
var parent = container.GetUIStyleParent();
if ( parent == null )
{
return UILineWrap.___;
}
return parent.GetUILineWrap();
2024-08-11 17:38:06 +00:00
}
2024-09-14 06:41:52 +00:00
public static UINumber GetUINumberProperty( UIStylePropertyContainer container, UIStyleNumberProperty property )
2024-08-11 17:38:06 +00:00
{
2024-09-14 06:41:52 +00:00
switch ( property )
{
case UIStyleNumberProperty.FontSize:
case UIStyleNumberProperty.FontOutlineSize:
case UIStyleNumberProperty.FontShadowSize:
case UIStyleNumberProperty.FontShadowOffsetX:
case UIStyleNumberProperty.FontShadowOffsetY:
{
return GetInheritableNumberProperty( container, property );
}
}
return GetReferenceableNumberProperty( container, property );
2024-08-11 17:38:06 +00:00
}
2024-09-14 06:41:52 +00:00
public static UIColor GetUIColorProperty( UIStylePropertyContainer container, UIStyleColorProperty property )
2024-08-11 17:38:06 +00:00
{
2024-09-14 06:41:52 +00:00
switch ( property )
{
case UIStyleColorProperty.FontColor:
case UIStyleColorProperty.FontOutlineColor:
case UIStyleColorProperty.FontShadowColor:
{
return GetInheritableColorProperty( container, property );
}
}
return GetReferenceableColorProperty( container, property );
2024-08-09 13:52:49 +00:00
}
}
}