This commit is contained in:
CortexCore
2023-11-02 20:58:36 +08:00
parent 3beceb1645
commit 7712c80804
75 changed files with 552 additions and 417 deletions

View File

@@ -0,0 +1,39 @@
using System;
using Godot;
namespace BITKit;
/// <summary>
/// 设备值组件,仅包括设备值的数据
/// </summary>
public partial class DeviceValueComponent : EntityComponent
{
/// <summary>
/// 获取角度的路径
/// </summary>
[Export] public string Path { get; private set; }
/// <summary>
/// 值
/// </summary>
[Export]
public string Value
{
get => _value;
set
{
_value = value;
if (Engine.IsEditorHint() is false)
{
OnValueChanged?.Invoke(value,LastUpdateTime);
}
}
}
public DateTime LastUpdateTime { get; set; }
private string _value;
/// <summary>
/// 当值改变时的回调
/// </summary>
public event Action<string,DateTime> OnValueChanged;
}

View File

@@ -0,0 +1,43 @@
using Godot;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using BITKit;
using BITKit.Entities;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion.Internal;
using Newtonsoft.Json.Linq;
#pragma warning disable CS0649 // 从未对字段赋值,字段将一直保持其默认值
namespace BITFactory;
public partial class IOTCloudService : EntityComponent
{
[Inject] private IEntitiesService _entitiesService;
private void Parse(string json)
{
var jObject = JObject.Parse(json);
foreach (var idComponent in _entitiesService.QueryComponents<IdComponent>())
{
if(jObject.TryGetValue(idComponent.Id,out var deviceToken) is false)continue;
var deviceValue = deviceToken.ToObject<JObject>();
foreach (var valueComponent in idComponent.Entity.Components.OfType<DeviceValueComponent>())
{
var lastUpdateTime = DateTime.Now;
if (deviceValue.TryGetValue("LastUpdateTime", out var _timeStr))
{
lastUpdateTime = DateTime.ParseExact(_timeStr.ToObject<string>(), "yyyy-MM-dd HH:mm:ss:fff", CultureInfo.InvariantCulture);
}
if (!deviceValue.TryGetValue(valueComponent.Path, out var value)) continue;
if (valueComponent.LastUpdateTime != lastUpdateTime)
valueComponent.Value = value.ToObject<string>();
valueComponent.LastUpdateTime = lastUpdateTime;
}
}
}
}

View File

@@ -0,0 +1,12 @@
using Godot;
namespace BITKit;
/// <summary>
/// ECS的Id组件用于提供Id
/// </summary>
public partial class IdComponent:EntityComponent
{
[Export]
public string Id;
}

View File

@@ -0,0 +1,105 @@
using Godot;
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using BITKit;
using BITKit.IO;
using Microsoft.Extensions.DependencyInjection;
using Cysharp.Threading.Tasks;
namespace BITKit.UX;
public partial class PlaybackService : UXPanel
{
[Export] private string path;
[Export] private Node root;
[Export] private Label label;
[Export] private PackedScene template;
[Export] private PackedScene labelTemplate;
[Export] private DataPlayer dataPlayer;
private CancellationTokenSource cancellationTokenSource;
public PlaybackService()
{
BITApp.ServiceCollection.AddSingleton(this);
}
public override void _Ready()
{
cancellationTokenSource = new CancellationTokenSource();
//OnExit();
}
public override void _ExitTree()
{
cancellationTokenSource.Cancel();
}
private void NoEntry()
//public override void OnEntry()
{
cancellationTokenSource.Token.ThrowIfCancellationRequested();
var files =
new DirectoryInfo(PathHelper.GetFolderPath("Demos", GetTree().CurrentScene.Name))
.GetFiles()
.Where(x => x.Extension is ".demo");
;
foreach (var x in files)
{
var playableInfo = BITAssets.ReadAs<PlayableInfo>(x.FullName);
var instance = template.Instantiate<Button>();
instance.Text = playableInfo.Name;
root.AddChild(instance);
instance.Pressed += OnClick;
async void OnClick()
{
BIT4Log.Log<PlaybackService>($"正在播放:{playableInfo.Name}");
Stopwatch stopwatch = new();
stopwatch.Start();
//OnExit();
label.Text = $"正在加载:{playableInfo.Name},创建时间:{playableInfo.CreateTime}";
label.Show();
await UniTask.SwitchToTaskPool();
var array = BITAssets.Read<string[]>(x.FullName, "base64");
try
{
await UniTask.SwitchToSynchronizationContext(BITApp.SynchronizationContext,
cancellationTokenSource.Token);
}
catch (OperationCanceledException)
{
stopwatch.Stop();
return;
}
stopwatch.Stop();
BIT4Log.Log<PlaybackService>($"已加载:{playableInfo.Name},耗时:{stopwatch.ElapsedMilliseconds}ms");
dataPlayer.Play(array);
label.Text = $"已加载,耗时{stopwatch.ElapsedMilliseconds}ms,点击任意位置返回";
BIT4Log.Log<PlaybackService>($"正在播放:{playableInfo.Name}");
}
}
}
private void NoExit()
//public override void OnExit()
{
foreach (var x in root.GetChildren())
{
x.QueueFree();
}
label.Hide();
}
}

View File

@@ -0,0 +1,33 @@
using Godot;
namespace BITKit;
/// <summary>
/// ECS中iFactory.Rotation的角度组件
/// </summary>
public partial class RotationComponent : DeviceValueComponent
{
/// <summary>
/// 角度的绝对权重例如90*0,0,1 = 0,0,90
/// </summary>
[Export] public Vector3 Weight { get; private set; }
/// <summary>
/// 角度的相对偏移
/// </summary>
[Export] public Vector3 Offset { get; private set; }
/// <summary>
/// 默认角度的缓存
/// </summary>
public Vector3 OriginalEuler { get; private set; }
/// <summary>
/// 可读可写的当前角度
/// </summary>
public float CurrentAngle { get; set; }
[ExportCategory("Nodes")]
[Export] public Node3D node3D;
public override void _Ready()
{
//保存默认角度
OriginalEuler = node3D.RotationDegrees;
}
}