Partial entity inspector

This commit is contained in:
2024-01-07 13:49:13 +01:00
parent 5dbc5ba15e
commit 9c745d2857
9 changed files with 276 additions and 56 deletions

View File

@@ -233,9 +233,10 @@ bool init(void *userData) {
loadMap(game, "assets/maps/main_menu_01.tmj");
game->debugDraw.mapColliders = true;
game->debugDraw.spatialGrid = true;
game->debugDraw.path = true;
game->debug.drawMapColliders = true;
game->debug.drawSpatialGrid = true;
game->debug.drawPath = true;
game->debug.inspecting = bzArrayCreate(ecs_entity_t, 10);
return true;
}
@@ -263,6 +264,8 @@ void deinit(void *userData) {
input = &inputCopy;
sounds = &soundsCopy;
bzArrayDestroy(game->debug.inspecting);
bzTilesetDestroy(&game->tileset);
bzStackAllocDestroy(&game->stackAlloc);
@@ -396,11 +399,11 @@ static void renderGame(Game *game, float dt) {
}
ecs_progress(ECS, dt);
ecs_enable(ECS, renderDebugPathSystem, game->debugDraw.path);
ecs_enable(ECS, renderCollidersSystem, game->debugDraw.entityColliders);
if (game->debugDraw.mapColliders)
ecs_enable(ECS, renderDebugPathSystem, game->debug.drawPath);
ecs_enable(ECS, renderCollidersSystem, game->debug.drawEntityColliders);
if (game->debug.drawMapColliders)
bzTileMapDrawCollisions(&game->map);
if (game->debugDraw.spatialGrid)
if (game->debug.drawSpatialGrid)
bzSpatialGridDrawDebugGrid(game->entityGrid);
drawPlayerInputUI();
@@ -612,13 +615,32 @@ void render(float dt, void *userData) {
}
static void igEntity(ecs_entity_t entity) {
void igInspectComp(const char *label, ecs_entity_t entity, ecs_entity_t comp, ImGuiCompFn fn) {
igPushID_Int(comp);
igSeparatorText(label);
bool isAttached = ecs_has_id(ECS, entity, comp);
igCheckbox("Attached", &isAttached);
if (isAttached)
fn(ECS, entity, comp);
if (isAttached != ecs_has_id(ECS, entity, comp)) {
if (!isAttached) {
ecs_remove_id(ECS, entity, comp);
} else {
ecs_set_id(ECS, entity, comp, 0, NULL);
}
}
igPopID();
}
void igInspectWindow(ecs_entity_t entity, bool *open) {
igSetNextWindowSize((ImVec2) {300, 440}, ImGuiCond_FirstUseEver);
char buf[64];
snprintf(buf, sizeof(buf), "Entity: %ld", entity);
if (igTreeNode_Str(buf)) {
igSeparatorText("Tags");
{
igTagCheckbox("GameEntity", ECS, entity, GameEntity);
if (igBegin(buf, open, 0)) {
if (igCollapsingHeader_TreeNodeFlags("Tags", 0)) {
//igTagCheckbox("GameEntity", ECS, entity, GameEntity);
igTagCheckbox("Selectable", ECS, entity, Selectable);
igTagCheckbox("Selected", ECS, entity, Selected);
igTagCheckbox("Storage", ECS, entity, Storage);
@@ -626,17 +648,30 @@ static void igEntity(ecs_entity_t entity) {
igTagCheckbox("Workable", ECS, entity, Workable);
igTagCheckbox("Attackable", ECS, entity, Attackable);
}
igResource("Resource", ECS, entity, ecs_id(Resource));
igVec2("Position", ECS, entity, ecs_id(Position));
igVec2("Size", ECS, entity, ecs_id(Size));
igVec2("Velocity", ECS, entity, ecs_id(Velocity));
igVec2("Steering", ECS, entity, ecs_id(Steering));
igVec2("TargetPosition", ECS, entity, ecs_id(TargetPosition));
igTreePop();
igInspectComp("Resource", entity, ecs_id(Resource), igResource);
igInspectComp("TilePosition", entity, ecs_id(TilePosition), igTilePosition);
igInspectComp("TileSize", entity, ecs_id(TileSize), igTileSize);
igInspectComp("Owner", entity, ecs_id(Owner), igOwner);
igInspectComp("SpatialGridID", entity, ecs_id(SpatialGridID), igSpatialGridID);
igInspectComp("Position", entity, ecs_id(Position), igVec2Comp);
igInspectComp("Size", entity, ecs_id(Size), igVec2Comp);
igInspectComp("Velocity", entity, ecs_id(Velocity), igVec2Comp);
igInspectComp("TargetPosition", entity, ecs_id(TargetPosition), igVec2Comp);
igInspectComp("Steering", entity, ecs_id(Steering), igVec2Comp);
igInspectComp("Rotation", entity, ecs_id(Rotation), igFloat);
igInspectComp("Path", entity, ecs_id(Path), igPath);
igInspectComp("TextureRegion", entity, ecs_id(TextureRegion), igTextureRegion);
igInspectComp("Animation", entity, ecs_id(Animation), igAnimation);
igInspectComp("Easing", entity, ecs_id(Easing), igEasing);
igInspectComp("Arms", entity, ecs_id(Arms), igArms);
igInspectComp("Arm", entity, ecs_id(Arm), igArm);
igInspectComp("UnitAction", entity, ecs_id(UnitAction), igUnitAction);
igInspectComp("UnitAI", entity, ecs_id(UnitAI), igUnitAI);
igInspectComp("Worker", entity, ecs_id(Worker), igWorker);
igInspectComp("Unit", entity, ecs_id(Unit), igUnit);
}
igEnd();
}
void imguiRender(float dt, void *userData) {
BZ_UNUSED(userData);
Game *game = ecs_singleton_get_mut(ECS, Game);
@@ -676,7 +711,7 @@ void imguiRender(float dt, void *userData) {
while (ecs_iter_next(&it)) {
for (i32 i = 0; i < it.count; i++) {
ecs_entity_t entity = it.entities[i];
igEntity(entity);
igText("Entity: %ld", entity);
}
}
ecs_defer_end(ECS);
@@ -705,10 +740,10 @@ void imguiRender(float dt, void *userData) {
}
if (igCollapsingHeader_TreeNodeFlags("DebugDraw", 0)) {
igCheckbox("map colliders", &game->debugDraw.mapColliders);
igCheckbox("entity colliders", &game->debugDraw.entityColliders);
igCheckbox("spatial grid", &game->debugDraw.spatialGrid);
igCheckbox("path", &game->debugDraw.path);
igCheckbox("map colliders", &game->debug.drawMapColliders);
igCheckbox("entity colliders", &game->debug.drawEntityColliders);
igCheckbox("spatial grid", &game->debug.drawSpatialGrid);
igCheckbox("path", &game->debug.drawPath);
}
if (igCollapsingHeader_TreeNodeFlags("Entities", 0)) {
@@ -718,5 +753,14 @@ void imguiRender(float dt, void *userData) {
bzGameExit();
}
igEnd();
i32 inspectLen = bzArraySize(game->debug.inspecting);
for (i32 i = inspectLen - 1; i >= 0; i--) {
bool open = true;
igInspectWindow(game->debug.inspecting[i], &open);
if (!open) {
bzArrayDelSwap(game->debug.inspecting, i);
}
}
}