rokojori_action_library/Runtime/UI/Nodes/UIInputInfo.cs

226 lines
5.3 KiB
C#
Raw Normal View History

using Godot;
using System.Collections.Generic;
2026-05-22 12:25:02 +00:00
using Rokojori.Extensions;
namespace Rokojori;
2026-05-22 12:25:02 +00:00
[Tool]
[GlobalClass, Icon("res://addons/rokojori_action_library/Icons/UIInputIcon.svg")]
public partial class UIInputInfo: UIRegion
{
InputIcon[] _inputIcons = [];
[Export]
public InputIcon[] inputIcons
{
get => _inputIcons;
set
{
2026-05-22 12:25:02 +00:00
_inputIcons = value;
// UpdateInfo();
}
2026-05-22 12:25:02 +00:00
}
2026-05-22 12:25:02 +00:00
[Export]
public LocalizedString locale;
2026-05-22 12:25:02 +00:00
[Export]
public bool upperCase;
2026-05-22 12:25:02 +00:00
[Export]
public bool updateInfo
{
get => false;
set { if ( value ){ UpdateInfo(); }}
}
2026-05-22 12:25:02 +00:00
[Export]
public DeviceFilter deviceFilter;
2025-06-12 14:04:15 +00:00
2026-05-22 12:25:02 +00:00
public override void _Ready()
{
UpdateInfo();
}
2026-05-22 12:25:02 +00:00
public void UpdateInfo()
{
Nodes.RemoveAndDeleteChildren( this );
2026-05-22 12:25:02 +00:00
if ( inputIcons == null )
{
return;
}
2026-05-22 12:25:02 +00:00
var iiLib = GetInputIconsLibrary();
2026-05-22 12:25:02 +00:00
if ( iiLib == null )
{
// this.LogInfo( "No Input lib!" );
return;
}
2026-05-22 12:25:02 +00:00
var icons = iiLib.ResolveIcons( inputIcons );
2026-05-22 12:25:02 +00:00
this.LogInfo( "Resolved Icons:\n", inputIcons, "\n>>\n", icons );
2026-05-22 12:25:02 +00:00
if ( deviceFilter != null )
{
var iconsBefore = ListExtensions.From( icons );
icons = ListExtensions.Filter( icons, i => deviceFilter.BelongsToDevice( i ) );
this.LogInfo( "Device Filtered Icons:\n", iconsBefore, "\n>>\n", icons );
}
2026-05-22 12:25:02 +00:00
icons = iiLib.ResolveIcons( icons.ToArray() );
2026-05-22 12:25:02 +00:00
icons.ForEach( i => AddIcon( i ) );
2026-05-22 12:25:02 +00:00
2026-05-22 12:25:02 +00:00
if ( locale != null )
{
var localeText = this.CreateChild<UIText>( "Locale" );
localeText.locale = locale;
2026-05-22 12:25:02 +00:00
localeText.font = iiLib.font;
localeText.fontSize = this.fontSize ?? iiLib.fontSize;
2026-05-22 12:25:02 +00:00
localeText.Uppercase = upperCase;
localeText.marginLeft = UINumber.EM( 0.25f );
}
2026-05-22 12:25:02 +00:00
}
2025-06-19 17:22:25 +00:00
2026-05-22 12:25:02 +00:00
UI ui;
2025-06-19 17:22:25 +00:00
2026-05-22 12:25:02 +00:00
public void SetUI( UI ui )
{
this.ui = ui;
}
InputIconsLibrary GetInputIconsLibrary()
{
var ui = this.ui != null ? this.ui : Unique<UI>.Get();
2025-06-19 17:22:25 +00:00
2026-05-22 12:25:02 +00:00
if ( ui == null )
{
2026-05-22 12:25:02 +00:00
ui = this.FindParentThatIs<UI>();
if ( ui == null )
{
2026-05-22 12:25:02 +00:00
this.LogInfo( "No UI in parents >", ui );
2026-05-22 12:25:02 +00:00
return null;
}
}
2026-05-22 12:25:02 +00:00
var lib = ui.settings.inputIconsLibrary;
2026-05-22 12:25:02 +00:00
return lib;
}
void AddIcon( InputIcon inputIcon )
{
if ( inputIcon == null )
{
2026-05-22 12:25:02 +00:00
return;
}
var lib = GetInputIconsLibrary();
2026-05-22 12:25:02 +00:00
var id = lib.GetInputIconDefinition( inputIcon );
2026-05-22 12:25:02 +00:00
if ( id == null )
{
// this.LogInfo( "No definition found >> ", inputIcon );
return;
}
2026-05-22 12:25:02 +00:00
// this.LogInfo( "Definition found >> ", inputIcon );
2026-05-22 12:25:02 +00:00
var iconRegion = this.CreateChild<UIRegion>();
var elements = id.GetIconElementsForIcon( inputIcon );
2026-05-22 12:25:02 +00:00
elements.ForEach( e => AddElement( iconRegion, e, e == elements[ 0 ] ) );
}
2026-05-22 12:25:02 +00:00
void AddElement( UIRegion iconRegion, IconElement element, bool isBG )
{
var lib = GetInputIconsLibrary();
2026-05-22 12:25:02 +00:00
if ( element is TextureIconElement textureIconElement )
{
var uiImage = iconRegion.CreateChild<UIImage>();
uiImage.Texture = textureIconElement.texture;
uiImage.ExpandMode = TextureRect.ExpandModeEnum.IgnoreSize;
uiImage.StretchMode = TextureRect.StretchModeEnum.Scale;
2026-05-22 12:25:02 +00:00
uiImage.height = UINumber.EM( lib.iconHeightInEm );
2026-05-22 12:25:02 +00:00
if ( isBG )
{
uiImage.width = UINumber.EM( lib.iconHeightInEm * textureIconElement.widthScale );
if ( textureIconElement.hasBorders )
{
2026-05-22 12:25:02 +00:00
var ninePatchType = new NinePatchUIImageType();
ninePatchType.leftPixelOffset = textureIconElement.borderLeft;
ninePatchType.rightPixelOffset = textureIconElement.borderRight;
ninePatchType.topPixelOffset = textureIconElement.borderTop;
ninePatchType.bottomPixelOffset = textureIconElement.borderBottom;
uiImage.imageType = ninePatchType;
}
2026-05-22 12:25:02 +00:00
}
else
{
var size = textureIconElement.texture.GetSize();
var ratio = size.X / size.Y;
uiImage.width = UINumber.EM( lib.iconHeightInEm ).GetMultiplied( ratio );
uiImage.top = UINumber.EM( 0f );
uiImage.position = UIPosition.Parent_Anchor;
if ( TextureIconOverlayAlignment.Left == textureIconElement.alignment )
{
2026-05-22 12:25:02 +00:00
uiImage.left = UINumber.EM( 0f );
}
2026-05-22 12:25:02 +00:00
if ( TextureIconOverlayAlignment.Right == textureIconElement.alignment )
{
2026-05-22 12:25:02 +00:00
uiImage.right = UINumber.EM( 0f );
}
}
2026-05-22 12:25:02 +00:00
if ( textureIconElement.rotation != 0 )
{
2026-05-22 12:25:02 +00:00
uiImage.rotation = UINumber.Create( textureIconElement.rotation );
}
}
else if ( element is LabelIconElement labelIconElement )
{
var uiText = iconRegion.CreateChild<UIText>();
2026-05-22 12:25:02 +00:00
uiText.font = lib.font;
uiText.fontSize = labelIconElement.fontScale == 1 ? lib.fontSize : lib.fontSize.GetMultiplied( labelIconElement.fontScale );
uiText.locale = labelIconElement.locale;
2026-05-22 12:25:02 +00:00
uiText.width = UINumber.PW( 100 );
uiText.height = UINumber.PH( 100 );
uiText.left = UINumber.EM( 0f );
uiText.top = UINumber.EM( 0f );
2026-05-22 12:25:02 +00:00
uiText.position = UIPosition.Parent_Anchor;
uiText.alwaysMinimumSize = false;
uiText.HorizontalAlignment = HorizontalAlignment.Center;
uiText.VerticalAlignment = VerticalAlignment.Center;
2026-05-22 12:25:02 +00:00
uiText.Uppercase = labelIconElement.fontIsUpperCase;
}
}
2026-05-22 12:25:02 +00:00
}