Basic property editor

This commit is contained in:
2024-01-05 08:36:05 +01:00
parent 852dc43556
commit dbc0ce5981
5 changed files with 73 additions and 31 deletions

View File

@@ -77,3 +77,38 @@ void initComponentIDs(ecs_world_t *ecs) {
ECS_TAG_DEFINE(ecs, Attackable); ECS_TAG_DEFINE(ecs, Attackable);
} }
#include <rlImGui.h>
void igTagCheckbox(const char *label, ecs_world_t *ecs,
ecs_entity_t entity, ecs_entity_t tag) {
bool hasTag = ecs_has_id(ecs, entity, tag);
igCheckbox(label, &hasTag);
if (hasTag)
ecs_add_id(ecs, entity, tag);
else
ecs_remove_id(ecs, entity, tag);
}
void igResource(const char *label, ecs_world_t *ecs,
ecs_entity_t entity, ecs_entity_t comp) {
if (!ecs_has_id(ecs, entity, comp)) return;
igSeparatorText(label);
Resource *res = ecs_get_mut_id(ecs, entity, comp);
const char *resStrings[RES_COUNT];
for (i32 i = 0; i < RES_COUNT; i++) {
resStrings[i] = getResourceTypePrettyName(i);
}
int curType = res->type;
igCombo_Str_arr("Type", &curType, resStrings, RES_COUNT, -1);
res->type = curType;
igInputInt("Amount", &res->amount, 1, 10, 0);
}
void igVec2(const char *label, ecs_world_t *ecs,
ecs_entity_t entity, ecs_entity_t comp) {
if (!ecs_has_id(ecs, entity, comp)) return;
igSeparatorText(label);
Vector2 *vec = ecs_get_mut_id(ecs, entity, comp);
igPushID_Str(label);
igInputFloat("X", &vec->x, 1.0f, 10.0f, "%.2f", 0);
igInputFloat("Y", &vec->y, 1.0f, 10.0f, "%.2f", 0);
igPopID();
}

View File

@@ -205,4 +205,10 @@ extern ECS_TAG_DECLARE(Attackable);
void initComponentIDs(ecs_world_t *ecs); void initComponentIDs(ecs_world_t *ecs);
void igTagCheckbox(const char *label, ecs_world_t *ecs,
ecs_entity_t entity, ecs_entity_t tag);
void igResource(const char *label, ecs_world_t *ecs,
ecs_entity_t entity, ecs_entity_t comp);
void igVec2(const char *label, ecs_world_t *ecs,
ecs_entity_t entity, ecs_entity_t comp);
#endif //PIXELDEFENSE_COMPONENTS_H #endif //PIXELDEFENSE_COMPONENTS_H

View File

@@ -1,5 +1,4 @@
#include <rlImGui.h> #include <rlImGui.h>
#include <raygui.h>
#include <time.h> #include <time.h>
#include <raymath.h> #include <raymath.h>
#include <stdlib.h> #include <stdlib.h>
@@ -566,11 +565,38 @@ void render(float dt, void *userData) {
} }
static void igEntity(ecs_entity_t entity) {
char buf[64];
snprintf(buf, sizeof(buf), "Entity: %ld", entity);
if (igTreeNode_Str(buf)) {
igSeparatorText("Tags");
{
igTagCheckbox("GameEntity", ECS, entity, GameEntity);
igTagCheckbox("Selectable", ECS, entity, Selectable);
igTagCheckbox("Selected", ECS, entity, Selected);
igTagCheckbox("Storage", ECS, entity, Storage);
igTagCheckbox("Harvestable", ECS, entity, Harvestable);
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();
}
}
void imguiRender(float dt, void *userData) { void imguiRender(float dt, void *userData) {
BZ_UNUSED(userData); BZ_UNUSED(userData);
Game *game = ecs_singleton_get_mut(ECS, Game); Game *game = ecs_singleton_get_mut(ECS, Game);
InputState *input = ecs_singleton_get_mut(ECS, InputState); InputState *input = ecs_singleton_get_mut(ECS, InputState);
igShowDemoWindow(NULL);
igSetNextWindowSize((ImVec2){300, 400}, ImGuiCond_FirstUseEver); igSetNextWindowSize((ImVec2){300, 400}, ImGuiCond_FirstUseEver);
igBegin("Debug Menu", NULL, 0); igBegin("Debug Menu", NULL, 0);
igText("PathData pool available: %llu", bzObjectPoolGetNumFree(game->pools.pathData)); igText("PathData pool available: %llu", bzObjectPoolGetNumFree(game->pools.pathData));
@@ -596,41 +622,17 @@ void imguiRender(float dt, void *userData) {
igText("Input state: %s", inputState); igText("Input state: %s", inputState);
if (igCollapsingHeader_TreeNodeFlags("Selection", 0)) { if (igCollapsingHeader_TreeNodeFlags("Selection", 0)) {
switch (input->state) { switch (input->state) {
case INPUT_SELECTED_UNITS: { case INPUT_SELECTED_UNITS:
igText("Selected units:");
ecs_iter_t it = ecs_query_iter(ECS, input->queries.selected);
while (ecs_iter_next(&it)) {
for (i32 i = 0; i < it.count; i++) {
ecs_entity_t entity = it.entities[i];
igText("Entity %llu", entity);
igText("Actions:");
const Action *pAction = NULL;
if (ecs_has(ECS, entity, UnitAction)) {
pAction = ecs_get(ECS, entity, UnitAction)->first;
}
while (pAction) {
igText("\t%d: %s", pAction->type, actionTypeToPrettyStr(pAction->type));
pAction = pAction->next;
}
}
}
break;
}
case INPUT_SELECTED_OBJECT: { case INPUT_SELECTED_OBJECT: {
igText("Selected objects:"); ecs_defer_begin(ECS);
ecs_iter_t it = ecs_query_iter(ECS, input->queries.selected); ecs_iter_t it = ecs_query_iter(ECS, input->queries.selected);
while (ecs_iter_next(&it)) { while (ecs_iter_next(&it)) {
for (i32 i = 0; i < it.count; i++) { for (i32 i = 0; i < it.count; i++) {
ecs_entity_t entity = it.entities[i]; ecs_entity_t entity = it.entities[i];
if (ecs_has(ECS, entity, Harvestable) && igEntity(entity);
ecs_has(ECS, entity, Resource)) {
Resource *res = ecs_get_mut(ECS, entity, Resource);
const char *resName = getResourceTypePrettyName(res->type);
igText("\tEntity %llu:", entity);
igSliderInt("\t\twood", &res->amount, 0, 20, NULL, 0);
}
} }
} }
ecs_defer_end(ECS);
break; break;
} }
default: default:

View File

@@ -42,7 +42,6 @@ bool initEntityObjectsLayer(BzTileMap *map, BzTileObjectGroup *objectGroup) {
.elapsed=i * 0.1f, .elapsed=i * 0.1f,
}); });
*/ */
bzLogInfo("%d %.2f %.2f", object.gid, object.shape.sizeX, object.shape.sizeY);
//EntityArms arms = { //EntityArms arms = {
// .left=createEntity(ECS), // .left=createEntity(ECS),
// .right=createEntity(ECS), // .right=createEntity(ECS),

View File

@@ -340,7 +340,7 @@ void drawPlayerInputUIGround() {
break; break;
} }
case INPUT_SELECTED_UNITS: { case INPUT_SELECTED_UNITS: {
BZ_ASSERT(selectedUnitCount); //BZ_ASSERT(selectedUnitCount);
/* /*
if (selectedUnitCount > 1 && isInputBtnDragged(input, primaryBtn)) { if (selectedUnitCount > 1 && isInputBtnDragged(input, primaryBtn)) {
i32 numUnits = selectedUnitCount; i32 numUnits = selectedUnitCount;