fix ctx.selected_op on swap

isolation_bkp/dynres
Dominik Madarász 2021-05-15 18:41:30 +02:00
parent 8623c18127
commit 276bd21b65
9 changed files with 1031 additions and 994 deletions

Binary file not shown.

File diff suppressed because it is too large Load Diff

Binary file not shown.

Before

Width:  |  Height:  |  Size: 12 KiB

After

Width:  |  Height:  |  Size: 9.1 KiB

View File

@ -13,6 +13,9 @@
#define GUI_FILE_DIALOG_IMPLEMENTATION #define GUI_FILE_DIALOG_IMPLEMENTATION
#include "gui_file_dialog.h" #include "gui_file_dialog.h"
#define GUI_TEXTBOX_EXTENDED_IMPLEMENTATION
#include "gui_textbox_extended.h"
static uint16_t screenWidth = 1280; static uint16_t screenWidth = 1280;
static uint16_t screenHeight = 720; static uint16_t screenHeight = 720;
static float zoom = 4.0f; static float zoom = 4.0f;
@ -24,6 +27,7 @@ static float zoom = 4.0f;
typedef enum { typedef enum {
TPARAM_FLOAT, TPARAM_FLOAT,
TPARAM_COORD,
TPARAM_INT, TPARAM_INT,
TPARAM_COLOR, TPARAM_COLOR,
TPARAM_STRING, TPARAM_STRING,
@ -52,6 +56,7 @@ typedef enum {
TOP_DRAW_LINE, TOP_DRAW_LINE,
TOP_DITHER, TOP_DITHER,
TOP_LOAD_IMAGE, TOP_LOAD_IMAGE,
TOP_DRAW_TEXT,
TOP_FORCE_UINT8 = UINT8_MAX TOP_FORCE_UINT8 = UINT8_MAX
} td_op_kind; } td_op_kind;
@ -126,6 +131,8 @@ void texed_run(void) {
texed_new(TD_DEFAULT_IMG_WIDTH, TD_DEFAULT_IMG_HEIGHT); texed_new(TD_DEFAULT_IMG_WIDTH, TD_DEFAULT_IMG_HEIGHT);
GuiSetStyle(TEXTBOX, TEXT_COLOR_NORMAL, ColorToInt(RAYWHITE));
zpl_aabb2 screen = { zpl_aabb2 screen = {
.min = (zpl_vec2) {.x = 0.0f, .y = 0.0f}, .min = (zpl_vec2) {.x = 0.0f, .y = 0.0f},
.max = (zpl_vec2) {.x = screenWidth, .y = screenHeight}, .max = (zpl_vec2) {.x = screenWidth, .y = screenHeight},
@ -222,7 +229,7 @@ void texed_swp_op(int idx, int idx2) {
td_op tmp = ctx.ops[idx2]; td_op tmp = ctx.ops[idx2];
ctx.ops[idx2] = ctx.ops[idx]; ctx.ops[idx2] = ctx.ops[idx];
ctx.ops[idx] = tmp; ctx.ops[idx] = tmp;
ctx.selected_op = idx2;
texed_repaint_preview(); texed_repaint_preview();
} }

View File

@ -54,6 +54,14 @@ void texed_process_ops(void) {
zpl_printf("TOP_LOAD_IMAGE: src %s not found!\n", op->params[0].str); zpl_printf("TOP_LOAD_IMAGE: src %s not found!\n", op->params[0].str);
} }
}break; }break;
case TOP_DRAW_TEXT: {
char const *str = op->params[0].str;
int x = op->params[1].i32;
int y = op->params[2].i32;
int size = op->params[3].i32;
Color color = op->params[4].color;
ImageDrawText(&ctx.img, str, x, y, size, color);
}break;
default: { default: {
zpl_printf("%s\n", "unsupported op!"); zpl_printf("%s\n", "unsupported op!");
}break; }break;
@ -74,7 +82,10 @@ void texed_process_params(void) {
p->flt = (float)zpl_str_to_f64(p->str, NULL); p->flt = (float)zpl_str_to_f64(p->str, NULL);
}break; }break;
case TPARAM_INT: { case TPARAM_INT: {
p->u32 = (int)zpl_str_to_i64(p->str, NULL, 10); p->u32 = (uint32_t)zpl_str_to_i64(p->str, NULL, 10);
}break;
case TPARAM_COORD: {
p->i32 = (int32_t)zpl_str_to_i64(p->str, NULL, 10);
}break; }break;
case TPARAM_COLOR: { case TPARAM_COLOR: {
uint32_t color = (uint32_t)zpl_str_to_u64(p->str, NULL, 16); uint32_t color = (uint32_t)zpl_str_to_u64(p->str, NULL, 16);

View File

@ -1,5 +1,6 @@
#define PARAM(k,n,v) { .kind = k, .name = n, .str = v } #define PARAM(k,n,v) { .kind = k, .name = n, .str = v }
#define PARAM_DEF_COLOR "000000ff"
static td_op default_ops[] = { static td_op default_ops[] = {
{ {
@ -13,22 +14,22 @@ static td_op default_ops[] = {
OP(TOP_DRAW_RECT), OP(TOP_DRAW_RECT),
.num_params = 5, .num_params = 5,
.params = (td_param[]) { .params = (td_param[]) {
PARAM(TPARAM_INT, "x", "0"), PARAM(TPARAM_COORD, "x", "0"),
PARAM(TPARAM_INT, "y", "0"), PARAM(TPARAM_COORD, "y", "0"),
PARAM(TPARAM_INT, "w", "10"), PARAM(TPARAM_COORD, "w", "10"),
PARAM(TPARAM_INT, "h", "10"), PARAM(TPARAM_COORD, "h", "10"),
PARAM(TPARAM_COLOR, "color", "0"), PARAM(TPARAM_COLOR, "color", PARAM_DEF_COLOR),
} }
}, },
{ {
OP(TOP_DRAW_LINE), OP(TOP_DRAW_LINE),
.num_params = 5, .num_params = 5,
.params = (td_param[]) { .params = (td_param[]) {
PARAM(TPARAM_INT, "x1", "0"), PARAM(TPARAM_COORD, "x1", "0"),
PARAM(TPARAM_INT, "y1", "0"), PARAM(TPARAM_COORD, "y1", "0"),
PARAM(TPARAM_INT, "x2", "64"), PARAM(TPARAM_COORD, "x2", "64"),
PARAM(TPARAM_INT, "y2", "64"), PARAM(TPARAM_COORD, "y2", "64"),
PARAM(TPARAM_COLOR, "color", "0"), PARAM(TPARAM_COLOR, "color", PARAM_DEF_COLOR),
} }
}, },
{ {
@ -45,17 +46,28 @@ static td_op default_ops[] = {
OP(TOP_LOAD_IMAGE), OP(TOP_LOAD_IMAGE),
.num_params = 6, .num_params = 6,
.params = (td_param[]) { .params = (td_param[]) {
PARAM(TPARAM_STRING, "src", "art/natives/test.png"), PARAM(TPARAM_STRING, "src", "art/samples/test.png"),
PARAM(TPARAM_INT, "x", "0"), PARAM(TPARAM_COORD, "x", "0"),
PARAM(TPARAM_INT, "y", "0"), PARAM(TPARAM_COORD, "y", "0"),
PARAM(TPARAM_INT, "w", "-1"), PARAM(TPARAM_COORD, "w", "-1"),
PARAM(TPARAM_INT, "h", "-1"), PARAM(TPARAM_COORD, "h", "-1"),
PARAM(TPARAM_COLOR, "tint", "ffffffff"), PARAM(TPARAM_COLOR, "tint", "ffffffff"),
} }
},
{
OP(TOP_DRAW_TEXT),
.num_params = 5,
.params = (td_param[]) {
PARAM(TPARAM_STRING, "text", "hello world"),
PARAM(TPARAM_COORD, "x", "0"),
PARAM(TPARAM_COORD, "y", "0"),
PARAM(TPARAM_INT, "size", "16"),
PARAM(TPARAM_COLOR, "color", "ffffffff"),
}
} }
}; };
// NOTE(zaklaus): IMPORTANT !! keep these in sync // NOTE(zaklaus): IMPORTANT !! keep these in sync
static char const *add_op_list = "CLEAR SOLID;DRAW RECTANGLE;PLOT LINE;DITHER;LOAD IMAGE"; static char const *add_op_list = "CLEAR SOLID;DRAW RECTANGLE;PLOT LINE;DITHER;LOAD IMAGE;DRAW TEXT";
#define DEF_OPS_LEN (int)(sizeof(default_ops) / (sizeof(default_ops[0]))) #define DEF_OPS_LEN (int)(sizeof(default_ops) / (sizeof(default_ops[0])))

View File

@ -38,13 +38,12 @@ void texed_load(void) {
int parmarrsize = (int)uc.item.as.array.size; int parmarrsize = (int)uc.item.as.array.size;
for (int j = 0; j < parmarrsize; j += 1) { for (int j = 0; j < parmarrsize; j += 1) {
td_param *p = &op->params[j]; td_param *p = &op->params[j];
UNPACK(CWP_ITEM_POSITIVE_INTEGER);
p->kind = (td_param_kind)uc.item.as.u64;
UNPACK(CWP_ITEM_STR); UNPACK(CWP_ITEM_STR);
zpl_memcopy(p->str, uc.item.as.str.start, uc.item.as.str.length); zpl_memcopy(p->str, uc.item.as.str.start, uc.item.as.str.length);
// NOTE(zaklaus): fix up other metadata // NOTE(zaklaus): fix up other metadata
p->name = default_ops[kind].params[j].name; p->name = default_ops[kind].params[j].name;
p->kind = default_ops[kind].params[j].kind;;
} }
} }
@ -77,7 +76,6 @@ void texed_save(void) {
cw_pack_array_size(&pc, op->num_params); cw_pack_array_size(&pc, op->num_params);
for (int j = 0; j < op->num_params; j += 1) { for (int j = 0; j < op->num_params; j += 1) {
td_param *p = &op->params[j]; td_param *p = &op->params[j];
cw_pack_unsigned(&pc, p->kind);
cw_pack_str(&pc, p->str, zpl_strlen(p->str)); cw_pack_str(&pc, p->str, zpl_strlen(p->str));
} }
} }

View File

@ -182,7 +182,7 @@ void texed_draw_props_pane(zpl_aabb2 r) {
switch (p->kind) { switch (p->kind) {
case TPARAM_COLOR: { case TPARAM_COLOR: {
if (GuiTextBox(aabb2_ray(tbox_r), p->str, 64, p->edit_mode)) { if (GuiTextBoxEx(aabb2_ray(tbox_r), p->str, 64, p->edit_mode)) {
p->edit_mode = true; p->edit_mode = true;
} }
@ -198,9 +198,18 @@ void texed_draw_props_pane(zpl_aabb2 r) {
} }
} }
}break; }break;
case TPARAM_COORD: {
if (GuiValueBox(aabb2_ray(tbox_r), NULL, &p->i32, INT32_MIN, INT32_MAX, p->edit_mode)) {
p->edit_mode = !p->edit_mode;
if (!p->edit_mode) {
sprintf(p->str, "%d", p->i32);
texed_repaint_preview();
}
};
}break;
default: { default: {
if (GuiTextBox(aabb2_ray(tbox_r), p->str, 64, p->edit_mode)) { if (GuiTextBoxEx(aabb2_ray(tbox_r), p->str, 64, p->edit_mode)) {
p->edit_mode = !p->edit_mode; p->edit_mode = !p->edit_mode;
if (!p->edit_mode) if (!p->edit_mode)

View File

@ -256,7 +256,7 @@ RAYGUIDEF int GuiTextBoxDelete(char *text, int length, bool before)
else else
{ {
// Delete character after cursor // Delete character after cursor
if (guiTextBoxState.cursor + 1 <= GuiCountCodepointsUntilNewline(text)) if (guiTextBoxState.cursor + 1 <= (int)GuiCountCodepointsUntilNewline(text))
{ {
startIdx = GuiTextBoxGetByteIndex(text, 0, 0, guiTextBoxState.cursor); startIdx = GuiTextBoxGetByteIndex(text, 0, 0, guiTextBoxState.cursor);
endIdx = GuiTextBoxGetByteIndex(text, 0, 0, guiTextBoxState.cursor+1); endIdx = GuiTextBoxGetByteIndex(text, 0, 0, guiTextBoxState.cursor+1);