--- a/tools/atlas-editor.c Sun May 24 18:27:13 2026 +0200 +++ b/tools/atlas-editor.c Sun May 24 19:10:34 2026 +0200 @@ -27,24 +27,37 @@ #include <ui/ui.h> -#define VAR_TILE_WIDTH "tile_width" -#define VAR_TILE_HEIGHT "tile_height" -#define VAR_SPACING "spacing" +typedef struct atlas_document { + UiInteger *tile_width; + UiInteger *tile_height; + UiInteger *spacing; +} AtlasDocument; + +typedef struct viewmodel { + UIWIDGET drawingarea; +} Viewmodel; -static void *create_document(void) { - void *doc = ui_document_new(1); // TODO: actually we have zero additional data, but that probably does not work +static AtlasDocument *create_document(void) { + AtlasDocument *doc = ui_document_new(sizeof(AtlasDocument)); UiContext *ctx = ui_document_context(doc); - ui_set(ui_int_new(ctx, VAR_TILE_WIDTH), 32); - ui_set(ui_int_new(ctx, VAR_TILE_HEIGHT), 32); - ui_set(ui_int_new(ctx, VAR_SPACING), 0); + + doc->tile_width = ui_int_new(ctx, NULL); + ui_set(doc->tile_width, 32); + + doc->tile_height = ui_int_new(ctx, NULL); + ui_set(doc->tile_height, 32); + + doc->spacing = ui_int_new(ctx, NULL); + ui_set(doc->spacing, 0); + return doc; } -static void draw_atlas(UiEvent* evt, UiGraphics* g, void* data) { - UiContext *doc_ctx = ui_document_context(evt->document); - int spacing = (int) ui_var_get_int(doc_ctx, VAR_SPACING); - int tile_width = (int) ui_var_get_int(doc_ctx, VAR_TILE_WIDTH); - int tile_height = (int) ui_var_get_int(doc_ctx, VAR_TILE_HEIGHT); +static void draw_atlas(UiEvent* evt, UiGraphics* g, [[maybe_unused]] void* data) { + AtlasDocument *doc = evt->document; + int spacing = ui_get(doc->spacing); + int tile_width = ui_get(doc->tile_width); + int tile_height = ui_get(doc->tile_height); ui_graphics_color(g, 255, 128, 0); ui_draw_rect(g, 0, 0, g->width, g->height, true); @@ -56,13 +69,23 @@ } } -static void application_startup([[maybe_unused]] UiEvent *event, void *data) { +static void on_grid_params_changed(UiEvent *event, [[maybe_unused]] void *data) { + Viewmodel *viewmodel = event->window; + ui_drawingarea_redraw(viewmodel->drawingarea); +} + +static void application_startup([[maybe_unused]] UiEvent *event, [[maybe_unused]] void *data) { UiObject *window = ui_window("Ascension - Texture Atlas Editor"); + AtlasDocument* doc = create_document(); + ui_attach_document(window->ctx, doc); + + Viewmodel *viewmodel = ui_malloc(window->ctx, sizeof(Viewmodel)); + window->window = viewmodel; ui_grid(window, .columnspacing = 10, .margin = 10, .fill = true) { ui_frame(window, .hexpand = true, .hfill = true, .vexpand = true, .vfill = true, .subcontainer = UI_CONTAINER_NO_SUB) { - ui_drawingarea(window, .draw = draw_atlas); + viewmodel->drawingarea = ui_drawingarea(window, .draw = draw_atlas); } ui_vbox(window, .spacing = 5) { ui_grid(window, .columnspacing = 10, .def_hexpand = true, .def_hfill = true) { @@ -73,18 +96,16 @@ ui_separator(window); ui_grid(window, .columnspacing = 10, .rowspacing = 2, .def_vfill = true) { ui_label(window, .label = "Tile Width:"); - ui_spinbox(window, .varname = VAR_TILE_WIDTH, .min = 8, .step = 8); + ui_spinbox(window, .intvalue = doc->tile_width, .min = 8, .step = 8, .onchange = on_grid_params_changed); ui_label(window, .label = "Tile Height:"); - ui_spinbox(window, .varname = VAR_TILE_HEIGHT, .min = 8, .step = 8); + ui_spinbox(window, .intvalue = doc->tile_height, .min = 8, .step = 8, .onchange = on_grid_params_changed); ui_newline(window); - ui_label(window, .varname = VAR_SPACING, .label = "Spacing:"); - ui_spinbox(window); + ui_label(window, .label = "Spacing:"); + ui_spinbox(window, .intvalue = doc->spacing, .onchange = on_grid_params_changed); } } } - ui_attach_document(window->ctx, create_document()); - ui_show(window); }