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

489 lines
12 KiB
C#

using Godot;
using Rokojori;
using System.Collections.Generic;
using System.Linq;
namespace Rokojori
{
[Tool]
[GlobalClass,Icon("res://addons/rokojori_action_library/Icons/UIText.svg")]
public partial class UIText:Label,UIStylePropertyContainer, iLocalizable, UIHolderControl, IAssemblyReload
{
LocalizedString _locale;
[Export]
public LocalizedString locale
{
get => _locale;
set { _locale = value; UpdateLocalization(); }
}
[Export]
public bool refreshText
{
get => false;
set { if ( value ) UpdateLocalization(); UpdateFont(); }
}
[Export]
public bool disableLocalization = false;
public void UpdateLocalization()
{
if ( disableLocalization )
{
return;
}
Text = LocalizedString.Get( _locale );
}
public void OnAssemblyReloaded()
{
UpdateLocalization();
UpdateFont();
}
[Export]
public bool alwaysMinimumSize = true;
UIStyle _parentStyle;
[Export]
public UIStyle parentStyle
{
get => _parentStyle;
set { _parentStyle = value; UpdateFont(); }
}
Font _font;
[ExportGroup( "Font" )]
[Export]
public Font font
{
get => _font;
set { _font = value; UpdateFont(); }
}
[Export]
public UINumber fontSize;
[Export]
public UIColor fontColor;
[Export]
public UINumber outlineSize;
[Export]
public UIColor outlineColor;
[Export]
public UINumber shadowSize;
[Export]
public UIColor shadowColor;
[Export]
public UINumber shadowOffsetX;
[Export]
public UINumber shadowOffsetY;
[ExportGroup("Size & Margins")]
[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;
[ExportGroup( "Position" )]
[Export]
public UIPosition position;
[Export]
public UILineWrap lineWrap;
[Export]
public UINumber left;
[Export]
public UINumber top;
[Export]
public UINumber right;
[Export]
public UINumber bottom;
[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;
public Font GetFont()
{
return font;
}
void UpdateFont()
{
var resolvedFont = UIStyle.Font( this );
if ( resolvedFont != null )
{
RemoveThemeFontOverride( "font" );
AddThemeFontOverride( "font", resolvedFont );
}
}
string hoverID = IDGenerator.GenerateID();
public override void _Ready()
{
MouseEntered += ()=>
{
AddUISelectorFlag( UISelectorFlag.Hover, hoverID );
};
MouseExited += ()=>
{
RemoveUISelectorFlag( UISelectorFlag.Hover, hoverID );
};
}
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 override void _GuiInput( InputEvent inputEvent )
{
if ( ! handleMouseEvents )
{
return;
}
if ( inputEvent is InputEventMouseButton mb )
{
if ( mb.Pressed )
{
if ( mb.ButtonIndex == MouseButton.Left )
{
Action.Trigger( onLeftClick );
}
if ( mb.ButtonIndex == MouseButton.Middle )
{
Action.Trigger( onMiddleClick );
}
if ( mb.ButtonIndex == MouseButton.Right )
{
Action.Trigger( onRightClick );
}
}
}
}
// 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 = new UINumberTransition[ 0 ];
public UINumberTransition[] GetNumberTransitions()
{
return numberTransitions;
}
public List<ActiveStyleTransition<UINumber,UIStyleNumberPropertyAndName>> activeNumberTransitions = new List<ActiveStyleTransition<UINumber,UIStyleNumberPropertyAndName>>();
public List<ActiveStyleTransition<UINumber,UIStyleNumberPropertyAndName>> GetActiveUINumberTransitions()
{
return activeNumberTransitions;
}
[Export]
public UIColorTransition[] colorTransitions = new UIColorTransition[ 0 ];
public UIColorTransition[] GetColorTransitions()
{
return colorTransitions;
}
public List<ActiveStyleTransition<UIColor,UIStyleColorPropertyAndName>> activeColorTransitions = new List<ActiveStyleTransition<UIColor,UIStyleColorPropertyAndName>>();
public List<ActiveStyleTransition<UIColor,UIStyleColorPropertyAndName>> GetActiveUIColorTransitions()
{
return activeColorTransitions;
}
LabelSettings _labelSettings;
public LabelSettings uiTextLabelSettings
{
get
{
if ( _labelSettings == null )
{
_labelSettings = new LabelSettings();
}
LabelSettings = _labelSettings;
return _labelSettings;
}
set
{
_labelSettings = value.Duplicate() as LabelSettings;
LabelSettings = _labelSettings;
}
}
[ExportGroup("Events")]
[Export]
public bool handleMouseEvents = false;
[Export]
public Action onLeftClick;
[Export]
public Action onMiddleClick;
[Export]
public Action onRightClick;
public UIStyle GetUIStyleParent()
{
return parentStyle;
}
public UIPosition GetUIPosition()
{
return position;
}
public UILineWrap GetUILineWrap()
{
return lineWrap;
}
public UILayout GetUILayout()
{
return UILayout.___;
}
public ShaderUIColor[] GetShaderUIColors()
{
return null;
}
public ShaderUINumber[] GetShaderUINumbers()
{
return null;
}
public UINumber GetUIStyleNumberProperty( UIStyleNumberProperty property, string shaderPropertyName, UIStylePropertyContainer source )
{
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;
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;
}
return null;
}
public void SetUIStyleNumberProperty( UIStyleNumberProperty property, UINumber number )
{
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.FontSize: { fontSize = number; } break;
case UIStyleNumberProperty.FontOutlineSize: { outlineSize = number; } break;
case UIStyleNumberProperty.FontShadowSize: { shadowSize = number; } break;
case UIStyleNumberProperty.FontShadowOffsetX: { shadowOffsetX = number; } break;
case UIStyleNumberProperty.FontShadowOffsetY: { shadowOffsetY = 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 Vector2 GetUISize()
{
return GetSize();
}
public UIColor GetUIStyleColorProperty( UIStyleColorProperty property, string shaderPropertyName, UIStylePropertyContainer source )
{
switch ( property )
{
case UIStyleColorProperty.FontColor: return fontColor;
case UIStyleColorProperty.FontOutlineColor: return outlineColor;
case UIStyleColorProperty.FontShadowColor: return shadowColor;
}
return null;
}
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 >", HierarchyName.Of( this ) );
return null;
}
}
return ui;
}
}
}