using System.Numerics; using ImGuiNET; using MoonTools.ECS; using Nerfed.Runtime.Components; namespace Nerfed.Runtime.Serialization; public static class ComponentHelper { // Auto generate this. public static readonly Dictionary> GetComponentByType = new() { { typeof(LocalTransform), (world, entity) => world.Get(entity) }, { typeof(Root), (world, entity) => world.Get(entity) }, }; // Auto generate this. 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) }, }; // Auto generate this, but it should only contain user assignable components (so something like 'root' should be excluded). // Maybe use an attribute for this. public static readonly Dictionary> AddComponentByType = new() { { typeof(LocalTransform), (world, entity) => world.Set(entity, LocalTransform.Identity) }, }; // Auto generate this, but also keep the option for 'custom inspectors'. // Maybe via attribute? 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(); } }, }; }