Nerfed/Nerfed.Runtime/Serialization/ComponentHelper.cs

57 lines
2.3 KiB
C#
Raw Normal View History

2024-11-02 21:59:35 +01:00
using System.Numerics;
using ImGuiNET;
using MoonTools.ECS;
using Nerfed.Runtime.Components;
namespace Nerfed.Runtime.Serialization;
public static class ComponentHelper
{
public static readonly Dictionary<Type, Func<World, Entity, ValueType>> GetComponentByType = new()
{
{ typeof(LocalTransform), (world, entity) => world.Get<LocalTransform>(entity) },
{ typeof(Root), (world, entity) => world.Get<Root>(entity) },
};
public static readonly Dictionary<Type, Action<World, Entity, ValueType>> SetComponentByType = new()
{
{ typeof(LocalTransform), (world, entity, component) => world.Set(entity, (LocalTransform)component) },
{ typeof(Root), (world, entity, component) => world.Set(entity, (Root)component) },
};
public static readonly Dictionary<Type, Action<World, Entity>> ComponentInspectorByType = new()
{
{
typeof(LocalTransform), (world, entity) =>
{
(Vector3 position, Quaternion rotation, Vector3 scale) = world.Get<LocalTransform>(entity);
Vector3 eulerAngles = MathEx.ToEulerAngles(rotation);
eulerAngles = new Vector3(float.RadiansToDegrees(eulerAngles.X), float.RadiansToDegrees(eulerAngles.Y), float.RadiansToDegrees(eulerAngles.Z));
bool isDirty = false;
ImGui.BeginGroup();
ImGui.Text($"{nameof(LocalTransform)}");
isDirty |= ImGui.DragFloat3("Position", ref position, 0.2f, float.MinValue, float.MaxValue, "%f0 m"); // TODO: right format.
isDirty |= ImGui.DragFloat3("Rotation", ref eulerAngles);
isDirty |= ImGui.DragFloat3("Scale", ref scale);
ImGui.EndGroup();
if (!isDirty)
{
return;
}
eulerAngles = new Vector3(float.DegreesToRadians(eulerAngles.X), float.DegreesToRadians(eulerAngles.Y), float.DegreesToRadians(eulerAngles.Z));
world.Set(entity, new LocalTransform(position, MathEx.ToQuaternion(eulerAngles), scale));
}
},
{
typeof(Root), (world, entity) =>
{
ImGui.BeginGroup();
ImGui.Text($"{nameof(Root)}");
ImGui.EndGroup();
}
},
};
}