using Godot; namespace Cirno.Scripts.Actors; [Tool] public partial class PreviewMarker3D : Marker3D { protected Texture2D _texture; protected Texture2D Texture { get => _texture; set { _texture = value; if (Engine.IsEditorHint()) { QueueRedraw(); } } } protected bool _fixedSize; protected bool _billboard; protected float _pixelSize = 0.05f; [Export] protected bool FixedSize { get => _fixedSize; set { _fixedSize = value; if (Engine.IsEditorHint()) { QueueRedraw(); } } } [Export] protected bool Billboard { get => _billboard; set { _billboard = value; if (Engine.IsEditorHint()) { QueueRedraw(); } } } [Export] protected float PixelSize { get => _pixelSize; set { _pixelSize = value; if (Engine.IsEditorHint()) { QueueRedraw(); } } } protected float _alpha = 1.0f; protected float Alpha { get => _alpha; set { _alpha = value; if (Engine.IsEditorHint()) { QueueRedraw(); } } } [ExportToolButton("Update Icon")] public Callable RedrawButton => Callable.From(Redraw); [ExportToolButton("Clear Children")] public Callable ClearChildrenButton => Callable.From(ClearChildren); private Sprite3D _sprite; public override void _Ready() { _sprite = GetNodeOrNull("Sprite3D"); ClearChildren(); if (Engine.IsEditorHint()) { QueueRedraw(); return; } } private void Redraw() { QueueRedraw(); } private void ClearChildren() { var children = GetChildren(); foreach (var child in children) { if (child is Sprite3D) { child.QueueFree(); } } _sprite = null; } protected virtual void QueueRedraw() { if (!Engine.IsEditorHint()) return; if (_texture is null) return; if (_sprite is null) { //GD.Print("Remaking sprite"); _sprite = new EditorSprite3D(); this.AddChild(_sprite); //_sprite.Owner = GetTree().EditedSceneRoot; } _sprite.Modulate = new Color(_sprite.Modulate.R, _sprite.Modulate.G, _sprite.Modulate.B, Alpha); _sprite.Texture = _texture; //_sprite.SetRotationDegrees(new Vector3(-45, 45, 0)); _sprite.FixedSize = FixedSize; _sprite.SetBillboardMode(Billboard ? BaseMaterial3D.BillboardModeEnum.Enabled : BaseMaterial3D.BillboardModeEnum.Disabled); _sprite.TextureFilter = BaseMaterial3D.TextureFilterEnum.Nearest; _sprite.PixelSize = PixelSize; //GD.Print($"Modulating alpha: {Alpha}"); //_sprite.SetModulate(new Color(_sprite.Modulate.R, _sprite.Modulate.G, _sprite.Modulate.B, Alpha)); } protected void SetSpriteAlpha(float alpha) { if (!Engine.IsEditorHint()) return; if (_sprite is not null) { //GD.Print($"Modulating sprite to {alpha}"); _sprite.SetModulate(new Color(_sprite.Modulate.R, _sprite.Modulate.G, _sprite.Modulate.B, alpha)); } else { //GD.Print("Sprite was null"); } } }