rj-action-library/Runtime/UI/Nodes/UIImage.cs

482 lines
13 KiB
C#
Raw 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;
2025-06-19 17:22:25 +00:00
using System.Linq;
2024-08-09 13:52:49 +00:00
namespace Rokojori
{
[Tool]
[GlobalClass,Icon("res://addons/rokojori_action_library/Icons/UIImage.svg")]
2025-06-19 17:22:25 +00:00
public partial class UIImage:TextureRect, UIStylePropertyContainer, UIHolderControl, IAssemblyReload
2024-08-09 13:52:49 +00:00
{
UIImageType _imageType;
[Export]
public UIImageType imageType
{
get => _imageType;
set
2025-06-19 17:22:25 +00:00
{
if ( _imageType != value )
{
if ( _imageType != null )
{
_imageType.Clear( this );
}
}
_imageType = value;
UpdateImageType();
}
}
2025-06-19 17:22:25 +00:00
UI ui;
public void SetUI( UI ui )
{
this.ui = ui;
}
public UI GetUI()
{
var ui = this.ui != null ? this.ui : Unique<UI>.Get();
if ( ui == null )
{
ui = this.FindParentThatIs<UI>();
if ( ui == null )
{
this.LogInfo( "No UI in parents >", ui );
return null;
}
}
return ui;
}
void UpdateImageType()
{
ResetImageType();
if ( _imageType != null )
{
2025-06-19 17:22:25 +00:00
this.LogInfo( "Assigning Image Type:", _imageType );
_imageType.Assign( this );
}
}
2025-06-19 17:22:25 +00:00
string hoverID = IDGenerator.GenerateID();
public override void _Ready()
{
MouseEntered += ()=>
{
AddUISelectorFlag( UISelectorFlag.Hover, hoverID );
};
MouseExited += ()=>
{
RemoveUISelectorFlag( UISelectorFlag.Hover, hoverID );
};
UpdateImageType();
}
public override void _EnterTree()
{
UpdateImageType();
}
public void OnAssemblyReloaded()
{
UpdateImageType();
}
void ResetImageType()
{
ExpandMode = TextureRect.ExpandModeEnum.IgnoreSize;
StretchMode = TextureRect.StretchModeEnum.Scale;
Material = null;
}
2025-06-19 17:22:25 +00:00
protected override void Dispose( bool disposing)
{
_selectorFlagReferenceCounter.Clear();
}
MapList<UISelectorFlag,string> _selectorFlagReferenceCounter = new MapList<UISelectorFlag, string>();
public void AddUISelectorFlag( UISelectorFlag flag, string reference = "" )
{
SetSelectorFlagReference( flag, reference, true );
}
public void RemoveUISelectorFlag( UISelectorFlag flag, string reference = "" )
{
SetSelectorFlagReference( flag, reference, false );
}
void SetSelectorFlagReference( UISelectorFlag flag, string reference, bool enable )
{
if ( enable )
{
_selectorFlagReferenceCounter.AddIfNotPresent( flag, reference );
}
else
{
_selectorFlagReferenceCounter.Remove( flag );
}
_selectorFlags = _selectorFlagReferenceCounter.Keys.ToList();
UISelector.UpdateParentUISelectorFlags( this );
}
List<UISelectorFlag> _selectorFlags = [];
List<UISelectorFlag> _parentSelectorFlags = [];
public List<UISelectorFlag> GetUISelectorFlags()
{
return _selectorFlags;
}
public List<UISelectorFlag> GetParentUISelectorFlags()
{
return _parentSelectorFlags;
}
public void CopyNumberShaderPropertyFrom( CustomMaterialProperty<float> name, UINumber number, TransitionSettings transition )
{
SetNumberShaderProperty( name.GetPropertyName<FloatPropertyName>(), number, transition );
}
public void SetNumberShaderProperty( FloatPropertyName name, UINumber number, TransitionSettings transition )
{
if ( name == null || name.propertyName == null )
{
this.LogInfo( "Name is null" );
return;
}
for ( int i = 0; i < numberProperties.Length; i++ )
{
if ( numberProperties[ i ] == null || numberProperties[ i ].floatPropertyName == null )
{
return;
}
}
if ( number == null )
{
var index = Arrays.FindIndex( numberProperties, p => p.floatPropertyName.propertyName == name.propertyName );
numberProperties = Arrays.RemoveIndex( numberProperties, index );
return;
}
var shaderUINumber = Arrays.Find( numberProperties, p => p.floatPropertyName.propertyName == name.propertyName );
if ( shaderUINumber == null )
{
shaderUINumber = new ShaderUINumber();
shaderUINumber.floatPropertyName = name;
numberProperties = Arrays.AddEntry( numberProperties, shaderUINumber );
}
shaderUINumber.number = number;
}
public void CopyColorShaderPropertyFrom( CustomMaterialProperty<Color> name, UIColor color )
{
SetColorShaderProperty( name.GetPropertyName<ColorPropertyName>(), color );
}
public void SetColorShaderProperty( ColorPropertyName name, UIColor color )
{
if ( name == null || name.propertyName == null )
{
this.LogInfo( "Name is null" );
return;
}
for ( int i = 0; i < colorProperties.Length; i++ )
{
if ( colorProperties[ i ] == null || colorProperties[ i ].colorPropertyName == null )
{
return;
}
}
if ( color == null )
{
var index = Arrays.FindIndex( colorProperties, p => p != null && p.colorPropertyName.propertyName == name.propertyName );
colorProperties = Arrays.RemoveIndex( colorProperties, index );
return;
}
var shaderUIColor = Arrays.Find( colorProperties, p => p != null && p.colorPropertyName.propertyName == name.propertyName );
if ( shaderUIColor == null )
{
shaderUIColor = new ShaderUIColor();
shaderUIColor.colorPropertyName = name;
colorProperties = Arrays.AddEntry( colorProperties, shaderUIColor );
}
shaderUIColor.color = color;
}
2024-08-11 17:38:06 +00:00
[Export]
public UIStyle parentStyle;
2024-09-14 06:41:52 +00:00
[ExportGroup("Size & Margins")]
2024-08-09 13:52:49 +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( "Position" )]
2024-08-09 13:52:49 +00:00
[Export]
public UIPosition position;
[Export]
2024-09-14 06:41:52 +00:00
public UILineWrap lineWrap;
[Export]
2024-08-09 13:52:49 +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" )]
2024-08-09 13:52:49 +00:00
[Export]
public UINumber pivotX;
[Export]
public UINumber pivotY;
[Export]
public UINumber rotation;
[Export]
public UINumber scale;
[Export]
public UINumber scaleX;
[Export]
public UINumber scaleY;
2024-09-14 06:41:52 +00:00
[ExportGroup( "Shader Properties" )]
[Export]
public ShaderUIColor[] colorProperties = new ShaderUIColor[ 0 ];
2025-06-19 17:22:25 +00:00
// public List<ActiveStyleTransition<UIColor,ColorPropertyName>> activeShaderColorTransitions = new List<ActiveStyleTransition<UIColor,ColorPropertyName>>();
// public List<ActiveStyleTransition<UIColor,ColorPropertyName>> GetActiveShaderUIColorTransitions()
// {
// return activeShaderColorTransitions;
// }
2024-09-14 06:41:52 +00:00
[Export]
public ShaderUINumber[] numberProperties = new ShaderUINumber[ 0 ];
2025-06-19 17:22:25 +00:00
// public List<ActiveStyleTransition<UINumber,FloatPropertyName>> activeShaderNumberTransitions = new List<ActiveStyleTransition<UINumber,FloatPropertyName>>();
// public List<ActiveStyleTransition<UINumber,FloatPropertyName>> GetActiveShaderUINumberTransitions()
// {
// return activeShaderNumberTransitions;
// }
2024-09-14 06:41:52 +00:00
[ExportGroup("Transitions")]
[Export]
public TransitionSettingsAll transitionSettings;
public TransitionSettingsAll GetTransitionSettingsAll()
{
return transitionSettings;
}
[Export]
public UINumberTransition[] numberTransitions = new UINumberTransition[0];
2024-09-14 06:41:52 +00:00
public UINumberTransition[] GetNumberTransitions()
{
return numberTransitions;
}
2025-06-19 17:22:25 +00:00
public List<ActiveStyleTransition<UINumber,UIStyleNumberPropertyAndName>> activeNumberTransitions = new List<ActiveStyleTransition<UINumber,UIStyleNumberPropertyAndName>>();
public List<ActiveStyleTransition<UINumber,UIStyleNumberPropertyAndName>> GetActiveUINumberTransitions()
2024-09-14 06:41:52 +00:00
{
return activeNumberTransitions;
}
[Export]
public UIColorTransition[] colorTransitions = new UIColorTransition[ 0 ];
2024-09-14 06:41:52 +00:00
public UIColorTransition[] GetColorTransitions()
{
return colorTransitions;
}
2025-06-19 17:22:25 +00:00
public List<ActiveStyleTransition<UIColor,UIStyleColorPropertyAndName>> activeColorTransitions = new List<ActiveStyleTransition<UIColor,UIStyleColorPropertyAndName>>();
public List<ActiveStyleTransition<UIColor,UIStyleColorPropertyAndName>> GetActiveUIColorTransitions()
2024-09-14 06:41:52 +00:00
{
return activeColorTransitions;
}
2024-08-09 13:52:49 +00:00
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 UILayout.___;
}
public ShaderUIColor[] GetShaderUIColors()
{
return colorProperties;
}
public ShaderUINumber[] GetShaderUINumbers()
{
return numberProperties;
}
2025-06-19 17:22:25 +00:00
public UINumber GetUIStyleNumberProperty( UIStyleNumberProperty property, string shaderPropertyName, UIStylePropertyContainer source )
2024-08-09 13:52:49 +00:00
{
2025-06-19 17:22:25 +00:00
2024-08-09 13:52:49 +00:00
switch ( property )
{
case UIStyleNumberProperty.Width: return width;
case UIStyleNumberProperty.Height: return height;
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;
case UIStyleNumberProperty.Left: return left;
case UIStyleNumberProperty.Right: return right;
case UIStyleNumberProperty.Top: return top;
case UIStyleNumberProperty.Bottom: return bottom;
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;
}
2025-06-19 17:22:25 +00:00
if ( UIStyleNumberProperty.FloatShaderProperty == property )
{
var numberProperty = numberProperties.Find( n => n.floatPropertyName.propertyName == shaderPropertyName );
if ( numberProperty != null )
{
return numberProperty.number;
}
}
2024-08-09 13:52:49 +00:00
return null;
}
2024-09-14 06:41:52 +00:00
2025-06-19 17:22:25 +00:00
public void SetUIStyleNumberProperty( UIStyleNumberProperty property, UINumber number )
2024-09-14 06:41:52 +00:00
{
2025-06-19 17:22:25 +00:00
switch ( property )
{
case UIStyleNumberProperty.Left: { left = number; } break;
case UIStyleNumberProperty.Right: { right = number; } break;
case UIStyleNumberProperty.Top: { top = number; } break;
case UIStyleNumberProperty.Bottom: { bottom = number; } break;
case UIStyleNumberProperty.Width: { width = number; } break;
case UIStyleNumberProperty.Height: { height = number; } break;
case UIStyleNumberProperty.Margin: { margin = number; } break;
case UIStyleNumberProperty.MarginLeft: { marginLeft = number; } break;
case UIStyleNumberProperty.MarginRight: { marginRight = number; } break;
case UIStyleNumberProperty.MarginTop: { marginTop = number; } break;
case UIStyleNumberProperty.MarginBottom: { marginBottom = number; } break;
case UIStyleNumberProperty.PivotX: { pivotX = number; } break;
case UIStyleNumberProperty.PivotY: { pivotY = number; } break;
case UIStyleNumberProperty.Rotation: { rotation = number; } break;
case UIStyleNumberProperty.Scale: { scale = number; } break;
case UIStyleNumberProperty.ScaleX: { scaleX = number; } break;
case UIStyleNumberProperty.ScaleY: { scaleY = number; } break;
}
}
public UIColor GetUIStyleColorProperty( UIStyleColorProperty property, string shaderPropertyName, UIStylePropertyContainer source )
{
if ( property != UIStyleColorProperty.ColorShaderProperty )
{
return null;
}
if ( imageType != null )
{
var uiColor = imageType.GetUIStyleColorProperty( property, shaderPropertyName );
if ( uiColor != null )
{
return uiColor;
}
}
var shaderUIColor = colorProperties.Find( c => c.colorPropertyName.propertyName == shaderPropertyName );
return shaderUIColor != null ? shaderUIColor.color : null;
2024-09-14 06:41:52 +00:00
}
public Font GetFont()
{
return null;
}
public Vector2 GetUISize()
{
return GetSize();
}
2024-08-09 13:52:49 +00:00
}
}