Add component button

This commit is contained in:
max
2024-11-13 21:11:50 +01:00
parent 87ee6df46f
commit cf6cd080c6
2 changed files with 57 additions and 25 deletions

View File

@ -33,6 +33,36 @@ namespace Nerfed.Editor.Systems
{
World.ComponentTypeEnumerator componentTypes = World.Debug_GetAllComponentTypes(entity);
// Add button of all types that we can add. Also filter out types we already have.
List<Type> componentTypesToAdd = ComponentHelper.AddComponentByType.Keys.ToList();
foreach (Type componentType in componentTypes)
{
componentTypesToAdd.Remove(componentType);
}
const string popupId = "AddComponentPopup";
if (ImGui.Button("Add Component"))
{
ImGui.OpenPopup(popupId);
}
if (ImGui.BeginPopup(popupId))
{
foreach (Type componentType in componentTypesToAdd)
{
if (ImGui.Selectable(componentType.Name))
{
if (ComponentHelper.AddComponentByType.TryGetValue(componentType, out Action<World, Entity> componentSetter))
{
componentSetter.Invoke(World, entity);
}
}
}
ImGui.EndPopup();
}
ImGui.Dummy(new Vector2(16, 16));
ImGui.Text("ComponentInspectorByType");
foreach (Type componentType in componentTypes)
{
@ -40,6 +70,11 @@ namespace Nerfed.Editor.Systems
{
componentInspector(World, entity);
}
else if (ComponentHelper.GetComponentByType.TryGetValue(componentType, out Func<World, Entity, ValueType> componentGetter))
{
ValueType component = componentGetter.Invoke(World, entity);
ImGui.Text(component.ToString());
}
else
{
ImGui.Text(componentType.Name);
@ -47,32 +82,18 @@ namespace Nerfed.Editor.Systems
ImGui.Separator();
}
ImGui.Dummy(new Vector2(16, 16));
ImGui.Text("GetComponentByType");
foreach (Type componentType in componentTypes)
{
if (!ComponentHelper.GetComponentByType.TryGetValue(componentType, out Func<World, Entity, ValueType> componentGetter)) continue;
ValueType component = componentGetter.Invoke(World, entity);
ImGui.Text(component.ToString());
ImGui.Separator();
}
ImGui.Dummy(new Vector2(16, 16));
ImGui.Text("Reflection");
// TODO: explore something without reflection.
// Maybe generate some look up dictionary<type, Action<world, entity>> for 'custom editors'.
// Look into serializing of entities and components.
foreach (Type component in componentTypes)
{
System.Reflection.MethodInfo getMethodInfo = typeof(World).GetMethod("Get");
System.Reflection.MethodInfo getComponentMethod = getMethodInfo.MakeGenericMethod(component);
object result = getComponentMethod.Invoke(World, [entity]);
// process here
ImGui.Text(result.ToString());
}
// ImGui.Text("Reflection");
// foreach (Type component in componentTypes)
// {
// System.Reflection.MethodInfo getMethodInfo = typeof(World).GetMethod("Get");
// System.Reflection.MethodInfo getComponentMethod = getMethodInfo.MakeGenericMethod(component);
// object result = getComponentMethod.Invoke(World, [entity]);
//
// // process here
// ImGui.Text(result.ToString());
// }
}
}
}