cirnogodot/Scripts/UI/AudioNameVisualizer.cs

67 lines
1.5 KiB
C#
Raw Normal View History

2025-02-25 14:06:41 +01:00
using Godot;
using System;
using System.Linq;
using Cirno.Scripts;
using GTweens.Builders;
using GTweens.Easings;
using GTweens.Tweens;
using GTweensGodot.Extensions;
2025-02-27 09:14:00 +01:00
using Cirno.Scripts.Resources;
2025-02-25 14:06:41 +01:00
2025-07-09 10:41:44 +02:00
public partial class AudioNameVisualizer : AudioStreamPlayer
2025-02-25 14:06:41 +01:00
{
[Export]
2025-02-27 10:01:50 +01:00
public MusicResource MusicData { get; set; }
2025-02-25 14:06:41 +01:00
[Export] public PackedScene CanvasTemplate;
2025-02-27 09:14:00 +01:00
2025-02-25 14:06:41 +01:00
private MusicVisualizerCanvas _canvasLayer;
2025-02-27 09:14:00 +01:00
2025-02-25 14:06:41 +01:00
public override void _Ready()
{
Setup();
2025-02-27 09:14:00 +01:00
2025-02-25 14:06:41 +01:00
if (Autoplay)
{
2025-02-27 10:01:50 +01:00
PlayWithName();
2025-02-25 14:06:41 +01:00
}
}
private void Setup()
{
2025-02-27 09:14:00 +01:00
if (MusicData?.Track is not null)
{
this.Stream = MusicData.Track;
}
2025-02-25 14:06:41 +01:00
var existingCanvas = this.GetTree().Root.GetNodeOrNull<CanvasLayer>("AudioCanvas");
if (existingCanvas is null)
{
// _canvasLayer = new CanvasLayer();
// _canvasLayer.Name = "AudioCanvas";
_canvasLayer = CanvasTemplate.Instantiate<MusicVisualizerCanvas>();
GetTree().Root.CallDeferred("add_child", _canvasLayer);
}
else
{
_canvasLayer = (MusicVisualizerCanvas)existingCanvas;
}
}
public void PlayWithName()
{
ShowName();
Play();
}
public void ShowName()
{
2025-02-27 09:14:00 +01:00
if (MusicData is null)
{
GD.PushWarning("Music data was null");
return;
}
_canvasLayer.ShowName(MusicData.TrackName, MusicData.ShowAuthor ? MusicData.AuthorName : null);
2025-02-25 14:06:41 +01:00
}
}