using System.Numerics; using ImGuiNET; using MoonTools.ECS; using Nerfed.Runtime.Components; namespace Nerfed.Runtime.Serialization; public static class ComponentHelper { public static readonly Dictionary> GetComponentByType = new() { { typeof(LocalTransform), (world, entity) => world.Get(entity) }, { typeof(Root), (world, entity) => world.Get(entity) }, }; public static readonly Dictionary> 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> ComponentInspectorByType = new() { { typeof(LocalTransform), (world, entity) => { (Vector3 position, Quaternion rotation, Vector3 scale) = world.Get(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(); } }, }; }