2025-04-14 21:54:29 +08:00

193 lines
5.2 KiB
C#

using System;
using System.Collections.Generic;
using Godot;
using Milimoe.FunGame.Core.Api.Utility;
using Milimoe.FunGame.Core.Entity;
using Milimoe.FunGame.Core.Library.Constant;
namespace Milimoe.GodotGame;
public partial class SavedNode : PanelContainer
{
[Export]
public int Index = 1;
[Export]
public bool IsAuto = false;
[Export]
public Button Button;
[Export]
public ConfirmationDialog SaveDialog;
[Export]
public ConfirmationDialog LoadDialog;
[Export]
public Label SavedName;
[Export]
public Label SavedTime;
[Export]
public Label ChapterInGame;
[Export]
public Label AreaInGame;
public Node Parent { get; set; }
public bool IsSave { get; set; } = false;
public bool HasValue { get; set; } = false;
public PluginConfig Config { get; set; } = null;
public override void _Ready()
{
if (IsAuto)
{
Config ??= new("Saved", $"auto{Index}");
}
else
{
Config ??= new("Saved", Index.ToString());
}
Config.LoadConfig();
Button ??= GetNode<Button>("Button");
Button.Pressed += Button_Pressed;
if (SaveDialog != null)
{
SaveDialog.GetOkButton().Pressed += SaveDialogConfirmed;
}
if (LoadDialog != null)
{
LoadDialog.GetOkButton().Pressed += LoadDialogConfirmed;
}
RefreshValue();
}
public void RefreshValue()
{
if (IsAuto) SavedName.Text = $"自动存档";
else SavedName.Text = $"存档位置 {Index}";
string savedTime = "";
string chapterInGame = "";
string areaInGame = "";
if (Config.Count > 0)
{
savedTime = Config["SavedTime"].ToString();
chapterInGame = Config["ChapterInGame"].ToString();
areaInGame = Config["AreaInGame"].ToString();
if (savedTime != "" && chapterInGame != "" && areaInGame != "")
{
HasValue = true;
}
}
if (!HasValue)
{
SavedTime.Text = "<空>";
ChapterInGame.Text = "";
AreaInGame.Text = "";
}
else
{
SavedTime.Text = savedTime;
ChapterInGame.Text = chapterInGame;
AreaInGame.Text = areaInGame;
}
}
private void Button_Pressed()
{
if (IsSave)
{
if (IsAuto)
{
ShowAlert("不能保存到自动存档位。");
return;
}
GD.Print("Save");
SaveDialog?.PopupCentered();
}
else
{
if (!HasValue)
{
ShowAlert("这个存档位是空的,请选择其他位置。");
return;
}
GD.Print("Load");
LoadDialog?.PopupCentered();
}
}
private void SaveDialogConfirmed()
{
if (HasValue)
{
ShowConfirmation("这个存档位已经有存档了,是否覆盖?", Save);
return;
}
Save();
}
private void LoadDialogConfirmed()
{
GD.Print("LoadDialogConfirmed");
if (Parent is ISaveSelectionEvent e)
{
e.OnSaveSelectionClosed(true, Index, IsSave);
}
}
private void Save()
{
GD.Print("SaveDialogConfirmed");
if (Parent is ISaveSelectionEvent e)
{
Config.Add("SavedTime", $"保存时间:{DateTime.Now.ToString(General.GeneralDateTimeFormatChinese)}");
Config.Add("ChapterInGame", GameConstant.ChapterInGame);
Config.Add("AreaInGame", GameConstant.AreaInGame);
Config.Add("NovelName", GameConstant.NovelName);
Config.Add("SceneName", GameConstant.SceneName);
Config.Add("NovelNodeKey", GameConstant.NovelNodeKey);
Config.Add("Characters", new List<Character>(GameConstant.Characters.Values));
Config.SaveConfig();
e.OnSaveSelectionClosed(true, Index, IsSave);
}
}
public void ShowAlert(string message)
{
// 创建并配置 AcceptDialog
AcceptDialog dialog = new()
{
Title = "提示",
OkButtonText = "好",
DialogText = message,
Theme = GD.Load<Theme>("res://resources/themes/dialog.tres")
};
GetViewport().GetWindow().AddChild(dialog);
dialog.PopupCentered();
dialog.Connect("confirmed", new Callable(dialog, nameof(dialog.QueueFree)));
}
public void ShowConfirmation(string message, Action ok)
{
// 创建并配置 AcceptDialog
ConfirmationDialog dialog = new()
{
Title = "提示",
OkButtonText = "是",
CancelButtonText = "否",
DialogText = message,
Theme = GD.Load<Theme>("res://resources/themes/dialog.tres")
};
dialog.GetOkButton().Pressed += () => ok();
GetViewport().GetWindow().AddChild(dialog);
dialog.PopupCentered();
dialog.Connect("confirmed", new Callable(dialog, nameof(dialog.QueueFree)));
}
}