This commit is contained in:
CortexCore
2023-06-19 00:41:44 +08:00
parent 073996ce6c
commit bf122c66dc
48 changed files with 683 additions and 84 deletions

View File

@@ -0,0 +1,80 @@
using System.Threading;
using Godot;
using Microsoft.Extensions.DependencyInjection;
using HttpClient = System.Net.Http.HttpClient;
namespace BITKit;
/// <summary>
/// 为Godot提供的BITApp加载服务
/// </summary>
public partial class BITAppForGodot : Node
{
public static readonly ValidHandle AllowCursor = new();
/// <summary>
/// 在构造函数中注册Logger
/// </summary>
public BITAppForGodot()
{
BIT4Log.OnLog += GD.Print;
BIT4Log.OnWarning += GD.PushWarning;
BIT4Log.OnNextLine += () => GD.Print();
BIT4Log.OnException += x=>GD.PrintErr(x.ToString());
BIT4Log.UseLogTime();
//启动BITApp
BITApp.Start(ProjectSettings.GetSetting("application/config/name").AsString());
BIT4Log.Log<BITAppForGodot>("已创建BITApp");
}
public override void _Ready()
{
BIT4Log.Log<BITAppForGodot>("正在创建BITWebApp");
//添加测试用HttpClient
BITApp.ServiceCollection.AddSingleton<HttpClient>();
//构造依赖服务提供接口
BITApp.BuildService();
//设置光标状态
AllowCursor.AddListener(SetCursor);
AllowCursor.AddElement(this);
// AllowCursor.AddElement(this);
// Input.MouseMode = Input.MouseModeEnum.Hidden;
// DisplayServer.MouseSetMode(DisplayServer.MouseMode.Hidden);
}
protected override void Dispose(bool disposing)
{
#pragma warning disable CS4014
//停止BITApp
BITApp.Stop();
#pragma warning restore CS4014
BIT4Log.Log<BITAppForGodot>("已安全退出App");
}
private void Exit()
{
GetTree().Quit();
}
private void WindowSetMaxSize()
{
var max = DisplayServer.WindowGetMaxSize();
//DisplayServer.WindowSetMaxSize(max);
var nextMode = DisplayServer.WindowGetMode() switch
{
DisplayServer.WindowMode.Fullscreen=>DisplayServer.WindowMode.Windowed,
DisplayServer.WindowMode.Windowed=>DisplayServer.WindowMode.Fullscreen,
_ => DisplayServer.WindowMode.Fullscreen,
};
DisplayServer.WindowSetMode(nextMode);
}
private void WindowSetMinSize()
{
DisplayServer.WindowSetMode(DisplayServer.WindowMode.Minimized);
}
private static void SetCursor(bool allow)
{
Input.MouseMode = allow ? Input.MouseModeEnum.Visible : Input.MouseModeEnum.Captured;
}
}

View File

@@ -0,0 +1,11 @@
using Godot;
using System;
namespace BITKit;
public partial class BITAppProxy : Node
{
private void Exit()
{
GetTree().Quit();
}
}

View File

@@ -0,0 +1,23 @@
using Godot;
using System.IO;
using Environment = System.Environment;
namespace BITKit;
public partial class Exec : Node
{
[Export] private string path;
public override void _Ready()
{
var filePath = Path.Combine(Environment.CurrentDirectory, path);
if (PathHelper.TryGetText(filePath,out var json))
{
json = File.ReadAllText(filePath);
DataParser.Set(json);
BIT4Log.Log<Exec>($"已加载:{path}");
}
else
{
BIT4Log.Warnning<Exec>($"未加载:{path}");
}
}
}

View File

@@ -0,0 +1,56 @@
using Godot;
namespace BITKit;
/// <summary>
/// 固定间隔工具,用于控制执行速率
/// </summary>
[System.Serializable]
public class IntervalTimer
{
public IntervalTimer()
{
}
/// <summary>
/// 在构造函数中声明间隔时间
/// </summary>
/// <param name="interval">间隔(秒)</param>
public IntervalTimer(ulong interval)
{
this.interval = interval;
}
/// <summary>
/// 间隔时间
/// </summary>
private readonly ulong interval;
/// <summary>
/// 可以执行的事件
/// </summary>
private ulong allowTime;
/// <summary>
/// 是否可以执行(执行重置间隔)
/// </summary>
public bool Allow
{
get
{
if (allowTime >= Time.GetTicksMsec()) return false;
Release();
return true;
}
}
/// <summary>
/// 是否可以执行(不会执行重置间隔)
/// </summary>
public bool AllowWithoutRelease => allowTime >= Time.GetTicksMsec();
/// <summary>
/// 重置执行间隔
/// </summary>
/// <param name="immediately">是否可以立即执行</param>
public void Release(bool immediately=false)
{
var currentTime = Time.GetTicksMsec();
allowTime = immediately ? currentTime : currentTime + interval*1000;
}
}