From d195b7756b87f4cdb46bcf905aac076e2253886a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Madar=C3=A1sz?= Date: Sun, 16 May 2021 15:38:30 +0200 Subject: [PATCH] make op list order independent --- code/game/source/editors/texed.c | 24 +++++++++++++++++------ code/game/source/editors/texed_ops.c | 2 +- code/game/source/editors/texed_ops_list.c | 22 ++++++++++----------- code/game/source/editors/texed_prj.c | 15 +++++++------- code/game/source/editors/texed_widgets.c | 6 +++--- 5 files changed, 41 insertions(+), 28 deletions(-) diff --git a/code/game/source/editors/texed.c b/code/game/source/editors/texed.c index 21565dc..5c62696 100644 --- a/code/game/source/editors/texed.c +++ b/code/game/source/editors/texed.c @@ -61,7 +61,7 @@ typedef enum { TOP_DRAW_RECT, TOP_DRAW_LINE, TOP_DITHER, - TOP_LOAD_IMAGE, + TOP_DRAW_IMAGE, TOP_DRAW_TEXT, TOP_RESIZE_IMAGE, @@ -117,9 +117,11 @@ void texed_compose_image(void); void texed_msgbox_init(char const *title, char const *message, char const *buttons); void texed_process_ops(void); void texed_process_params(void); -void texed_add_op(int idx); + +void texed_add_op(int kind); void texed_rem_op(int idx); void texed_swp_op(int idx, int idx2); +int texed_find_op(int kind); void texed_draw_oplist_pane(zpl_aabb2 r); void texed_draw_props_pane(zpl_aabb2 r); @@ -169,7 +171,7 @@ void texed_run(int argc, char **argv) { Image orig = LoadImage(zpl_bprintf("art/%s", path)); texed_new(orig.width, orig.height); is_repaint_locked = true; - texed_add_op(TOP_LOAD_IMAGE); + texed_add_op(TOP_DRAW_IMAGE); td_param *params = ctx.ops[1].params; zpl_strcpy(params[0].str, path); is_repaint_locked = false; @@ -336,9 +338,19 @@ void texed_msgbox_init(char const *title, char const *message, char const *butto ctx.msgbox.buttons = buttons; } -void texed_add_op(int idx) { - assert(idx >= 0 && idx < DEF_OPS_LEN); - td_op *dop = &default_ops[idx]; +int texed_find_op(int kind) { + assert(kind >= 0 && kind < DEF_OPS_LEN); + for (int i = 0; i < DEF_OPS_LEN; i += 1) { + if (default_ops[i].kind == kind) { + return i; + } + } + return -1; +} + +void texed_add_op(int kind) { + assert(kind >= 0 && kind < DEF_OPS_LEN); + td_op *dop = &default_ops[texed_find_op(kind)]; td_op op = { .kind = dop->kind, diff --git a/code/game/source/editors/texed_ops.c b/code/game/source/editors/texed_ops.c index 937b05e..4f3db65 100644 --- a/code/game/source/editors/texed_ops.c +++ b/code/game/source/editors/texed_ops.c @@ -33,7 +33,7 @@ void texed_process_ops(void) { op->params[2].i32, op->params[3].i32); }break; - case TOP_LOAD_IMAGE: { + case TOP_DRAW_IMAGE: { char const *str = zpl_bprintf("art/%s", op->params[0].str); if (FileExists(str)) { Image img = LoadImage(str); diff --git a/code/game/source/editors/texed_ops_list.c b/code/game/source/editors/texed_ops_list.c index cd1a8d2..dba125a 100644 --- a/code/game/source/editors/texed_ops_list.c +++ b/code/game/source/editors/texed_ops_list.c @@ -36,17 +36,7 @@ static td_op default_ops[] = { } }, { - OP(TOP_DITHER), - .num_params = 4, - .params = (td_param[]) { - PARAM(TPARAM_INT, "r_bpp", "8"), - PARAM(TPARAM_INT, "g_bpp", "8"), - PARAM(TPARAM_INT, "b_bpp", "8"), - PARAM(TPARAM_INT, "a_bpp", "8"), - } - }, - { - OP(TOP_LOAD_IMAGE), + OP(TOP_DRAW_IMAGE), .num_params = 6, .params = (td_param[]) { PARAM(TPARAM_STRING, "src", "samples/test.png"), @@ -68,6 +58,16 @@ static td_op default_ops[] = { PARAM(TPARAM_COLOR, "color", PARAM_DEF_COLOR), } }, + { + OP(TOP_DITHER), + .num_params = 4, + .params = (td_param[]) { + PARAM(TPARAM_INT, "r_bpp", "8"), + PARAM(TPARAM_INT, "g_bpp", "8"), + PARAM(TPARAM_INT, "b_bpp", "8"), + PARAM(TPARAM_INT, "a_bpp", "8"), + } + }, { OP(TOP_RESIZE_IMAGE), .num_params = 3, diff --git a/code/game/source/editors/texed_prj.c b/code/game/source/editors/texed_prj.c index 9914515..72087cb 100644 --- a/code/game/source/editors/texed_prj.c +++ b/code/game/source/editors/texed_prj.c @@ -39,7 +39,8 @@ void texed_load(void) { op->is_hidden = uc.item.as.boolean; UNPACK(CWP_ITEM_ARRAY); - op->num_params = default_ops[kind].num_params; + int idx = texed_find_op(kind); + op->num_params = default_ops[idx].num_params; op->params = zpl_malloc(sizeof(td_param)*op->num_params); int parmarrsize = (int)uc.item.as.array.size; for (int j = 0; j < parmarrsize; j += 1) { @@ -48,16 +49,16 @@ void texed_load(void) { zpl_memcopy(p->str, uc.item.as.str.start, uc.item.as.str.length); // NOTE(zaklaus): fix up other metadata - p->name = default_ops[kind].params[j].name; - p->kind = default_ops[kind].params[j].kind; + p->name = default_ops[idx].params[j].name; + p->kind = default_ops[idx].params[j].kind; } // NOTE(zaklaus): resolve missing params - for (int j = parmarrsize; j < default_ops[kind].num_params; j += 1) { + for (int j = parmarrsize; j < default_ops[idx].num_params; j += 1) { td_param *p = &op->params[j]; - p->name = default_ops[kind].params[j].name; - p->kind = default_ops[kind].params[j].kind; - zpl_strcpy(p->str, default_ops[kind].params[j].str); + p->name = default_ops[idx].params[j].name; + p->kind = default_ops[idx].params[j].kind; + zpl_strcpy(p->str, default_ops[idx].params[j].str); } } diff --git a/code/game/source/editors/texed_widgets.c b/code/game/source/editors/texed_widgets.c index 294f30d..fe19439 100644 --- a/code/game/source/editors/texed_widgets.c +++ b/code/game/source/editors/texed_widgets.c @@ -145,7 +145,7 @@ void texed_draw_oplist_pane(zpl_aabb2 r) { zpl_aabb2 add_op_btn_r = zpl_aabb2_cut_top(&add_op_r, 22.5f); zpl_aabb2_cut_bottom(&add_op_btn_r, 2.5f); if (GuiButton(aabb2_ray(add_op_btn_r), default_ops[i].name)) { - texed_add_op(i); + texed_add_op(default_ops[i].kind); } } } @@ -198,7 +198,7 @@ void texed_draw_oplist_pane(zpl_aabb2 r) { zpl_aabb2 hidden_r = zpl_aabb2_cut_right(&op_item_r, 20.0f); - if (!default_ops[ctx.ops[i].kind].is_locked) GuiSetState(GUI_STATE_NORMAL); + if (!default_ops[texed_find_op(ctx.ops[i].kind)].is_locked) GuiSetState(GUI_STATE_NORMAL); if (GuiButton(aabb2_ray(hidden_r), ctx.ops[i].is_hidden ? "#45#" : "#44#")) { ctx.ops[i].is_hidden = !ctx.ops[i].is_hidden; texed_repaint_preview(); @@ -207,7 +207,7 @@ void texed_draw_oplist_pane(zpl_aabb2 r) { zpl_aabb2 lock_r = zpl_aabb2_cut_right(&op_item_r, 20.0f); - if (default_ops[ctx.ops[i].kind].is_locked) GuiSetState(GUI_STATE_DISABLED); + if (default_ops[texed_find_op(ctx.ops[i].kind)].is_locked) GuiSetState(GUI_STATE_DISABLED); if (GuiButton(aabb2_ray(lock_r), ctx.ops[i].is_locked ? "#137#" : "#138#")) { ctx.ops[i].is_locked = !ctx.ops[i].is_locked; ctx.is_saved = false;