Skip to content

Commit

Permalink
Fix some issues related to tile editing #475
Browse files Browse the repository at this point in the history
  • Loading branch information
cxong committed Mar 2, 2020
1 parent fc94520 commit d3a9aba
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 25 deletions.
16 changes: 11 additions & 5 deletions src/cdogs/mission_static.c
Original file line number Diff line number Diff line change
Expand Up @@ -770,10 +770,11 @@ TileClass *MissionStaticIdTileClass(
{
char keyBuf[6];
sprintf(keyBuf, "%d", tile);
TileClass *tc;
if (hashmap_get(m->TileClasses, keyBuf, (any_t)&tc) != MAP_OK)
TileClass *tc = NULL;
const int error = hashmap_get(m->TileClasses, keyBuf, (any_t)&tc);
if (error != MAP_OK && error != MAP_MISSING)
{
CASSERT(false, "cannot find tile id");
CASSERT(false, "error getting tile id");
}
return tc;
}
Expand Down Expand Up @@ -853,8 +854,11 @@ TileClass *MissionStaticAddTileClass(MissionStatic *m, const TileClass *base)
TileClass *tc;
CMALLOC(tc, sizeof *tc);
TileClassCopy(tc, base);
// Try to find an empty slot to add the new tile class
int i;
for (i = 0; MissionStaticIdTileClass(m, i) != NULL; i++);
char buf[12];
snprintf(buf, sizeof buf - 1, "%d", hashmap_length(m->TileClasses));
snprintf(buf, sizeof buf - 1, "%d", i);
if (hashmap_put(m->TileClasses, buf, (any_t *)tc) != MAP_OK)
{
char tcName[256];
Expand All @@ -875,10 +879,12 @@ bool MissionStaticRemoveTileClass(MissionStatic *m, const int tile)
return false;
}
// Set all tiles using this to the first one
int i;
for (i = 0; MissionStaticIdTileClass(m, i) == NULL; i++);
CA_FOREACH(int, t, m->Tiles)
if (*t == tile)
{
*t = 0;
*t = i;
}
CA_FOREACH_END()
return true;
Expand Down
46 changes: 26 additions & 20 deletions src/cdogsed/tile_brush.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ static void DrawTileOpsRow(
TileClass *selectedTC, bool *result);
static void DrawTilePropsRow(
struct nk_context *ctx, TileBrushData *tbData, TileClass *selectedTC);
static int DrawTileType(any_t data, any_t key);
static int DrawTileType(TileBrushData *tbData, TileClass *tc, const int tileId);
static bool Draw(SDL_Window *win, struct nk_context *ctx, void *data)
{
UNUSED(win);
Expand All @@ -158,10 +158,17 @@ static bool Draw(SDL_Window *win, struct nk_context *ctx, void *data)

nk_layout_row_dynamic(ctx, 40 * PIC_SCALE, WIDTH / 120);
tbData->tileIdx = 0;
if (hashmap_iterate_keys(
tbData->m->u.Static.TileClasses, DrawTileType, tbData) != MAP_OK)
int tilesDrawn = 0;
for (int i = 0;
tilesDrawn < hashmap_length(tbData->m->u.Static.TileClasses);
i++)
{
CASSERT(false, "failed to draw static tile classes");
TileClass *tc = MissionStaticIdTileClass(&tbData->m->u.Static, i);
if (tc != NULL)
{
DrawTileType(tbData, tc, i);
tilesDrawn++;
}
}
}
nk_end(ctx);
Expand All @@ -185,7 +192,6 @@ static void DrawTileOpsRow(
if (MissionStaticAddTileClass(&tbData->m->u.Static, &tc) != NULL)
{
ResetTexIds(tbData);
*tbData->brushIdx = (int)tbData->texIdsTileClasses.size - 1;
}
TileClassTerminate(&tc);
}
Expand All @@ -195,7 +201,6 @@ static void DrawTileOpsRow(
&tbData->m->u.Static, selectedTC) != NULL)
{
ResetTexIds(tbData);
*tbData->brushIdx = (int)tbData->texIdsTileClasses.size - 1;
}
}
if (hashmap_length(tbData->m->u.Static.TileClasses) > 0 &&
Expand All @@ -205,15 +210,26 @@ static void DrawTileOpsRow(
&tbData->m->u.Static, *tbData->brushIdx))
{
ResetTexIds(tbData);
*tbData->brushIdx = MIN(
*tbData->brushIdx,
(int)tbData->texIdsTileClasses.size - 1);
// Set selected tile index to an existing tile
if (hashmap_length(tbData->m->u.Static.TileClasses) > 0)
{
for (*tbData->brushIdx = 0;
MissionStaticIdTileClass(
&tbData->m->u.Static,
*tbData->brushIdx
) == NULL;
*tbData->brushIdx++);
}
}
}
}
static void DrawTilePropsRow(
struct nk_context *ctx, TileBrushData *tbData, TileClass *selectedTC)
{
if (selectedTC == NULL)
{
return;
}
nk_layout_row_dynamic(ctx, ROW_HEIGHT, 6);

nk_label(ctx, "Type:", NK_TEXT_LEFT);
Expand Down Expand Up @@ -293,20 +309,10 @@ static void DrawTilePropsRow(
static void DrawTileClass(
struct nk_context *ctx, const PicManager *pm, const TileClass *tc,
const struct vec2i pos, const GLuint texid);
static int DrawTileType(any_t data, any_t key)
static int DrawTileType(TileBrushData *tbData, TileClass *tc, const int tileId)
{
TileBrushData *tbData = data;
TileClass *tc;
const int error = hashmap_get(
tbData->m->u.Static.TileClasses, (const char *)key, (any_t *)&tc);
if (error != MAP_OK)
{
CASSERT(false, "cannot find tile class");
return error;
}
char name[256];
TileClassGetBrushName(name, tc);
const int tileId = strtol((const char*)key, NULL, 10);
const int selected = *tbData->brushIdx == tileId;
if (nk_select_label(tbData->ctx, name,
NK_TEXT_ALIGN_BOTTOM | NK_TEXT_ALIGN_CENTERED, selected))
Expand Down

0 comments on commit d3a9aba

Please sign in to comment.