iFactory.Godot/Mods/教育平台/Scripts/CourseElement.cs

67 lines
1.5 KiB
C#

using System.Diagnostics;
using System.Security;
using Godot;
using BITKit;
namespace BITFactory;
public partial class CourseElement : Node
{
private static Node CurrentScene;
private static string CurrentScenePath=string.Empty;
[Export]
public PackedScene CourseScene;
[Export] private Label courseLabel;
[Export] private Button entryButton;
[Export]
public string CourseName
{
get => _courseName;
set
{
if (courseLabel is not null)
courseLabel.Text = _courseName = value;
}
}
private string _courseName;
public override void _Ready()
{
//entryButton.Connect(nameof(Button.press))
entryButton.Pressed += EntryCourse;
}
private void EntryCourse()
{
if (CourseScene is null)
{
return;
}
Stopwatch stopwatch = new();
stopwatch.Start();
if (CurrentScenePath == CourseScene?.ResourcePath)
{
BIT4Log.Log<CourseElement>($"已返回当前课程");
UXService.Open(CurrentScene as Control);
return;
}
if (CurrentScene is not null)
{
BIT4Log.Log<CourseElement>($"正在释放课程:\t{CurrentScene.Name}");
CurrentScene.QueueFree();
BIT4Log.Log<CourseElement>($"已释放当前课程:\t{CurrentScene.Name}");
}
CurrentScene = CourseScene!.Instantiate();
CurrentScenePath = CourseScene.ResourcePath;
GetTree().Root.AddChild(CurrentScene);
UXService.Open(CurrentScene as Control);
BIT4Log.Log<CourseElement>($"已加载新的课程:\t{CourseScene.ResourceName}");
stopwatch.Stop();
BIT4Log.Log<CourseElement>($"加载课程耗时:\t{stopwatch.ElapsedMilliseconds}ms");
}
}