Skip to content

Commit

Permalink
Select tile color #475
Browse files Browse the repository at this point in the history
  • Loading branch information
cxong committed Mar 3, 2020
1 parent 9373157 commit 7bf4f8b
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 79 deletions.
54 changes: 20 additions & 34 deletions src/cdogsed/char_editor.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,6 @@ static int DrawClassSelection(
const GLuint *texids, const char *items, const int selected,
const size_t len);
static int HairIndex(const char *hair);
static void DrawCharColor(
struct nk_context *ctx, EditorContext *ec, const char *label, color_t *c);
static void DrawFlag(
struct nk_context *ctx, EditorContext *ec, const char *label,
const int flag, const char *tooltip);
Expand Down Expand Up @@ -387,11 +385,26 @@ static bool Draw(SDL_Window *win, struct nk_context *ctx, void *data)

// Character colours
nk_layout_row(ctx, NK_DYNAMIC, ROW_HEIGHT, 2, colRatios);
DrawCharColor(ctx, ec, "Skin:", &ec->Char->Colors.Skin);
DrawCharColor(ctx, ec, "Hair:", &ec->Char->Colors.Hair);
DrawCharColor(ctx, ec, "Arms:", &ec->Char->Colors.Arms);
DrawCharColor(ctx, ec, "Body:", &ec->Char->Colors.Body);
DrawCharColor(ctx, ec, "Legs:", &ec->Char->Colors.Legs);
if (ColorPicker(ctx, ROW_HEIGHT, "Skin:", &ec->Char->Colors.Skin))
{
*ec->FileChanged = true;
}
if (ColorPicker(ctx, ROW_HEIGHT, "Hair:", &ec->Char->Colors.Hair))
{
*ec->FileChanged = true;
}
if (ColorPicker(ctx, ROW_HEIGHT, "Arms:", &ec->Char->Colors.Arms))
{
*ec->FileChanged = true;
}
if (ColorPicker(ctx, ROW_HEIGHT, "Body:", &ec->Char->Colors.Body))
{
*ec->FileChanged = true;
}
if (ColorPicker(ctx, ROW_HEIGHT, "Legs:", &ec->Char->Colors.Legs))
{
*ec->FileChanged = true;
}
}
nk_end(ctx);

Expand Down Expand Up @@ -592,33 +605,6 @@ static int HairIndex(const char *hair)
return -1;
}

static void DrawCharColor(
struct nk_context *ctx, EditorContext *ec, const char *label, color_t *c)
{
nk_label(ctx, label, NK_TEXT_LEFT);
struct nk_color color = { c->r, c->g, c->b, 255 };
const struct nk_color colorOriginal = color;
if (nk_combo_begin_color(ctx, color, nk_vec2(nk_widget_width(ctx), 400)))
{
nk_layout_row_dynamic(ctx, 110, 1);
struct nk_colorf colorf = nk_color_cf(color);
colorf = nk_color_picker(ctx, colorf, NK_RGB);
color = nk_rgb_cf(colorf);
nk_layout_row_dynamic(ctx, ROW_HEIGHT, 1);
color.r = (nk_byte)nk_propertyi(ctx, "#R:", 0, color.r, 255, 1, 1);
color.g = (nk_byte)nk_propertyi(ctx, "#G:", 0, color.g, 255, 1, 1);
color.b = (nk_byte)nk_propertyi(ctx, "#B:", 0, color.b, 255, 1, 1);
nk_combo_end(ctx);
c->r = color.r;
c->g = color.g;
c->b = color.b;
if (memcmp(&color, &colorOriginal, sizeof color))
{
*ec->FileChanged = true;
}
}
}

static void DrawFlag(
struct nk_context *ctx, EditorContext *ec, const char *label, const int flag,
const char *tooltip)
Expand Down
4 changes: 2 additions & 2 deletions src/cdogsed/editor_ui_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,5 @@ void TileClassGetBrushName(char *buf, const TileClass *tc);
// Get a null-separated string; for nk_combo_separator
char *GetClassNames(const int len, const char *(*indexNameFunc)(const int));

void TexArrayInit(CArray* arr, const int count);
void TexArrayTerminate(CArray* arr);
void TexArrayInit(CArray *arr, const int count);
void TexArrayTerminate(CArray *arr);
29 changes: 29 additions & 0 deletions src/cdogsed/nk_window.c
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,35 @@ void DrawPic(
nk_draw_image(&ctx->current->buffer, bounds, &tex, nk_white);
}

bool ColorPicker(
struct nk_context *ctx, const float height, const char *label, color_t *c)
{
bool changed = false;
nk_label(ctx, label, NK_TEXT_LEFT);
struct nk_color color = { c->r, c->g, c->b, 255 };
const struct nk_color colorOriginal = color;
if (nk_combo_begin_color(ctx, color, nk_vec2(nk_widget_width(ctx), 400)))
{
nk_layout_row_dynamic(ctx, 110, 1);
struct nk_colorf colorf = nk_color_cf(color);
colorf = nk_color_picker(ctx, colorf, NK_RGB);
color = nk_rgb_cf(colorf);
nk_layout_row_dynamic(ctx, height, 1);
color.r = (nk_byte)nk_propertyi(ctx, "#R:", 0, color.r, 255, 1, 1);
color.g = (nk_byte)nk_propertyi(ctx, "#G:", 0, color.g, 255, 1, 1);
color.b = (nk_byte)nk_propertyi(ctx, "#B:", 0, color.b, 255, 1, 1);
nk_combo_end(ctx);
c->r = color.r;
c->g = color.g;
c->b = color.b;
if (memcmp(&color, &colorOriginal, sizeof color))
{
changed = true;
}
}
return changed;
}

static void BeforeDrawTex(const GLuint texid)
{
glBindTexture(GL_TEXTURE_2D, texid);
Expand Down
2 changes: 2 additions & 0 deletions src/cdogsed/nk_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,5 @@ void LoadTexFromPic(const GLuint texid, const Pic *pic);
void DrawPic(
struct nk_context *ctx, const Pic *pic, const GLuint texid,
const struct vec2i pos, const float scale);
bool ColorPicker(
struct nk_context *ctx, const float height, const char *label, color_t *c);
104 changes: 61 additions & 43 deletions src/cdogsed/tile_brush.c
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,27 @@ static void DrawTileOpsRow(
}
}
}
static void DrawTileTypeSelect(
struct nk_context *ctx, TileBrushData *tbData, TileClass *selectedTC);
static void DrawTileStyleSelect(
struct nk_context *ctx, TileBrushData *tbData, TileClass *selectedTC);
static void DrawTilePropsSidebar(
struct nk_context *ctx, TileBrushData *tbData, TileClass *selectedTC)
{
nk_layout_row_dynamic(ctx, ROW_HEIGHT, 1);

DrawTileTypeSelect(ctx, tbData, selectedTC);

if (selectedTC->Type != TILE_CLASS_NOTHING)
{
DrawTileStyleSelect(ctx, tbData, selectedTC);
ColorPicker(ctx, ROW_HEIGHT, "Primary Color", &selectedTC->Mask);
ColorPicker(ctx, ROW_HEIGHT, "Alt Color", &selectedTC->MaskAlt);
}
}
static void DrawTileTypeSelect(
struct nk_context *ctx, TileBrushData *tbData, TileClass *selectedTC)
{
nk_label(ctx, "Type:", NK_TEXT_LEFT);
const TileClassType newType = nk_combo_separator(
ctx, tbData->tcTypes, '\0', selectedTC->Type,
Expand Down Expand Up @@ -262,51 +279,52 @@ static void DrawTilePropsSidebar(
CASSERT(false, "unknown tile class");
break;
}
TileClassInitDefault(selectedTC, tbData->pm, base, NULL, &selectedTC->Mask);
TileClassInitDefault(
selectedTC, tbData->pm, base, NULL, &selectedTC->Mask);
}

if (selectedTC->Type != TILE_CLASS_NOTHING)
}
static void DrawTileStyleSelect(
struct nk_context *ctx, TileBrushData *tbData, TileClass *selectedTC)
{
nk_label(ctx, "Style:", NK_TEXT_LEFT);
const GLuint* styleTexIds = NULL;
const char* styles = "";
int styleSelected = -1;
int styleCount = 0;
switch (selectedTC->Type)
{
nk_label(ctx, "Style:", NK_TEXT_LEFT);
const GLuint* styleTexIds = NULL;
const char* styles = "";
int styleSelected = -1;
int styleCount = 0;
switch (selectedTC->Type)
{
case TILE_CLASS_FLOOR:
styles = tbData->floorStyles;
styleSelected = FloorStyleIndex(selectedTC->Style);
styleTexIds = tbData->texIdsFloorStyles.data;
styleCount = tbData->texIdsFloorStyles.size;
break;
case TILE_CLASS_WALL:
styles = tbData->wallStyles;
styleSelected = WallStyleIndex(selectedTC->Style);
styleTexIds = tbData->texIdsWallStyles.data;
styleCount = tbData->texIdsWallStyles.size;
break;
case TILE_CLASS_DOOR:
styles = tbData->doorStyles;
styleSelected = DoorStyleIndex(selectedTC->Style);
styleTexIds = tbData->texIdsDoorStyles.data;
styleCount = tbData->texIdsDoorStyles.size;
break;
default:
break;
}
const int newStyle = nk_combo_separator_image(
ctx, styleTexIds, styles, '\0', styleSelected,
styleCount, ROW_HEIGHT,
nk_vec2(nk_widget_width(ctx), 8 * ROW_HEIGHT)
);
if (newStyle != styleSelected)
{
CFREE(selectedTC->Style);
CSTRDUP(
selectedTC->Style, IndexTileStyleName(selectedTC->Type, newStyle));
TileClassReloadPic(selectedTC, tbData->pm);
}
case TILE_CLASS_FLOOR:
styles = tbData->floorStyles;
styleSelected = FloorStyleIndex(selectedTC->Style);
styleTexIds = tbData->texIdsFloorStyles.data;
styleCount = tbData->texIdsFloorStyles.size;
break;
case TILE_CLASS_WALL:
styles = tbData->wallStyles;
styleSelected = WallStyleIndex(selectedTC->Style);
styleTexIds = tbData->texIdsWallStyles.data;
styleCount = tbData->texIdsWallStyles.size;
break;
case TILE_CLASS_DOOR:
styles = tbData->doorStyles;
styleSelected = DoorStyleIndex(selectedTC->Style);
styleTexIds = tbData->texIdsDoorStyles.data;
styleCount = tbData->texIdsDoorStyles.size;
break;
default:
break;
}
const int newStyle = nk_combo_separator_image(
ctx, styleTexIds, styles, '\0', styleSelected,
styleCount, ROW_HEIGHT,
nk_vec2(nk_widget_width(ctx), 8 * ROW_HEIGHT)
);
if (newStyle != styleSelected)
{
CFREE(selectedTC->Style);
CSTRDUP(
selectedTC->Style, IndexTileStyleName(selectedTC->Type, newStyle));
TileClassReloadPic(selectedTC, tbData->pm);
}
}
static void DrawTileClass(
Expand Down

0 comments on commit 7bf4f8b

Please sign in to comment.