tools/atlas-editor.c

changeset 313
e8119155c099
parent 312
330fc71ad3c2
equal deleted inserted replaced
312:330fc71ad3c2 313:e8119155c099
25 * POSSIBILITY OF SUCH DAMAGE. 25 * POSSIBILITY OF SUCH DAMAGE.
26 */ 26 */
27 27
28 #include <ui/ui.h> 28 #include <ui/ui.h>
29 29
30 #define VAR_TILE_WIDTH "tile_width" 30 typedef struct atlas_document {
31 #define VAR_TILE_HEIGHT "tile_height" 31 UiInteger *tile_width;
32 #define VAR_SPACING "spacing" 32 UiInteger *tile_height;
33 UiInteger *spacing;
34 } AtlasDocument;
33 35
34 static void *create_document(void) { 36 typedef struct viewmodel {
35 void *doc = ui_document_new(1); // TODO: actually we have zero additional data, but that probably does not work 37 UIWIDGET drawingarea;
38 } Viewmodel;
39
40 static AtlasDocument *create_document(void) {
41 AtlasDocument *doc = ui_document_new(sizeof(AtlasDocument));
36 UiContext *ctx = ui_document_context(doc); 42 UiContext *ctx = ui_document_context(doc);
37 ui_set(ui_int_new(ctx, VAR_TILE_WIDTH), 32); 43
38 ui_set(ui_int_new(ctx, VAR_TILE_HEIGHT), 32); 44 doc->tile_width = ui_int_new(ctx, NULL);
39 ui_set(ui_int_new(ctx, VAR_SPACING), 0); 45 ui_set(doc->tile_width, 32);
46
47 doc->tile_height = ui_int_new(ctx, NULL);
48 ui_set(doc->tile_height, 32);
49
50 doc->spacing = ui_int_new(ctx, NULL);
51 ui_set(doc->spacing, 0);
52
40 return doc; 53 return doc;
41 } 54 }
42 55
43 static void draw_atlas(UiEvent* evt, UiGraphics* g, void* data) { 56 static void draw_atlas(UiEvent* evt, UiGraphics* g, [[maybe_unused]] void* data) {
44 UiContext *doc_ctx = ui_document_context(evt->document); 57 AtlasDocument *doc = evt->document;
45 int spacing = (int) ui_var_get_int(doc_ctx, VAR_SPACING); 58 int spacing = ui_get(doc->spacing);
46 int tile_width = (int) ui_var_get_int(doc_ctx, VAR_TILE_WIDTH); 59 int tile_width = ui_get(doc->tile_width);
47 int tile_height = (int) ui_var_get_int(doc_ctx, VAR_TILE_HEIGHT); 60 int tile_height = ui_get(doc->tile_height);
48 61
49 ui_graphics_color(g, 255, 128, 0); 62 ui_graphics_color(g, 255, 128, 0);
50 ui_draw_rect(g, 0, 0, g->width, g->height, true); 63 ui_draw_rect(g, 0, 0, g->width, g->height, true);
51 ui_graphics_color(g, 128, 128, 128); 64 ui_graphics_color(g, 128, 128, 128);
52 for (int x = spacing ; x < g->width ; x += tile_width + spacing) { 65 for (int x = spacing ; x < g->width ; x += tile_width + spacing) {
54 ui_draw_rect(g, x, y, tile_width, tile_height, false); 67 ui_draw_rect(g, x, y, tile_width, tile_height, false);
55 } 68 }
56 } 69 }
57 } 70 }
58 71
59 static void application_startup([[maybe_unused]] UiEvent *event, void *data) { 72 static void on_grid_params_changed(UiEvent *event, [[maybe_unused]] void *data) {
73 Viewmodel *viewmodel = event->window;
74 ui_drawingarea_redraw(viewmodel->drawingarea);
75 }
76
77 static void application_startup([[maybe_unused]] UiEvent *event, [[maybe_unused]] void *data) {
60 UiObject *window = ui_window("Ascension - Texture Atlas Editor"); 78 UiObject *window = ui_window("Ascension - Texture Atlas Editor");
79 AtlasDocument* doc = create_document();
80 ui_attach_document(window->ctx, doc);
81
82 Viewmodel *viewmodel = ui_malloc(window->ctx, sizeof(Viewmodel));
83 window->window = viewmodel;
61 84
62 ui_grid(window, .columnspacing = 10, .margin = 10, .fill = true) { 85 ui_grid(window, .columnspacing = 10, .margin = 10, .fill = true) {
63 ui_frame(window, .hexpand = true, .hfill = true, .vexpand = true, .vfill = true, 86 ui_frame(window, .hexpand = true, .hfill = true, .vexpand = true, .vfill = true,
64 .subcontainer = UI_CONTAINER_NO_SUB) { 87 .subcontainer = UI_CONTAINER_NO_SUB) {
65 ui_drawingarea(window, .draw = draw_atlas); 88 viewmodel->drawingarea = ui_drawingarea(window, .draw = draw_atlas);
66 } 89 }
67 ui_vbox(window, .spacing = 5) { 90 ui_vbox(window, .spacing = 5) {
68 ui_grid(window, .columnspacing = 10, .def_hexpand = true, .def_hfill = true) { 91 ui_grid(window, .columnspacing = 10, .def_hexpand = true, .def_hfill = true) {
69 ui_button(window, .label = "Load Atlas"); 92 ui_button(window, .label = "Load Atlas");
70 ui_button(window, .label = "Save Atlas"); 93 ui_button(window, .label = "Save Atlas");
71 } 94 }
72 ui_button(window, .label = "Load Texture"); 95 ui_button(window, .label = "Load Texture");
73 ui_separator(window); 96 ui_separator(window);
74 ui_grid(window, .columnspacing = 10, .rowspacing = 2, .def_vfill = true) { 97 ui_grid(window, .columnspacing = 10, .rowspacing = 2, .def_vfill = true) {
75 ui_label(window, .label = "Tile Width:"); 98 ui_label(window, .label = "Tile Width:");
76 ui_spinbox(window, .varname = VAR_TILE_WIDTH, .min = 8, .step = 8); 99 ui_spinbox(window, .intvalue = doc->tile_width, .min = 8, .step = 8, .onchange = on_grid_params_changed);
77 ui_label(window, .label = "Tile Height:"); 100 ui_label(window, .label = "Tile Height:");
78 ui_spinbox(window, .varname = VAR_TILE_HEIGHT, .min = 8, .step = 8); 101 ui_spinbox(window, .intvalue = doc->tile_height, .min = 8, .step = 8, .onchange = on_grid_params_changed);
79 ui_newline(window); 102 ui_newline(window);
80 ui_label(window, .varname = VAR_SPACING, .label = "Spacing:"); 103 ui_label(window, .label = "Spacing:");
81 ui_spinbox(window); 104 ui_spinbox(window, .intvalue = doc->spacing, .onchange = on_grid_params_changed);
82 } 105 }
83 } 106 }
84 } 107 }
85
86 ui_attach_document(window->ctx, create_document());
87 108
88 ui_show(window); 109 ui_show(window);
89 } 110 }
90 111
91 int main(int argc, char** argv) { 112 int main(int argc, char** argv) {

mercurial