#include "keraforge/editor.h" #include "keraforge/graphics.h" #include "keraforge/world.h" #include static void _kf_modal_edit_init(void) { struct kf_editor *editor = malloc(sizeof(struct kf_editor)); editor->selected_tile = 0; editor->dirty = false; kf_window.modal->data = editor; } static void _kf_modal_edit_exit(void) { bool d = ((struct kf_editor *)kf_window.modal->data)->dirty; kf_logdbg("exiting editor, world is %s", d ? "dirty" : "not dirty"); if (d) kf_timeit("save world", kf_world_save(kf_window.room, true, NULL)); free(kf_window.modal->data); kf_window.modal->data = NULL; } static void _kf_modal_edit_update(void) { /* Inherit from kf_modal_play. */ kf_modal_play.update(); if (kf_checkinputpress(kf_inputbind_palette) && kf_window.modal == &kf_modal_edit) kf_setmenu(&kf_menu_palette); } static void _kf_modal_edit_render_world(void) { /* Inherit from kf_modal_play. */ kf_modal_play.render_world(); if (kf_window.menu) return; struct kf_vec2(u32) select = kf_window.select; if (select.x >= kf_window.room->width || select.y >= kf_window.room->height) return; struct kf_editor *data = (struct kf_editor *)kf_window.modal->data; struct kf_tile *t = kf_world_gettile(kf_window.room, select.x, select.y); if (IsMouseButtonDown(MOUSE_BUTTON_LEFT)) { t->id = data->selected_tile; data->dirty = true; kf_world_updatetile(kf_window.room, select.x, select.y, true); } else if (IsMouseButtonDown(MOUSE_BUTTON_RIGHT)) { t->subid = data->selected_tile; data->dirty = true; } else if (IsMouseButtonPressed(MOUSE_BUTTON_MIDDLE)) { data->selected_tile = t->id; } DrawRectangleLines(select.x * KF_TILE_SIZE_PX, select.y * KF_TILE_SIZE_PX, KF_TILE_SIZE_PX, KF_TILE_SIZE_PX, WHITE); char *text = (char *)TextFormat("%d [%d] (%d,%d)", t->id, t->data, select.x, select.y); int s = kf_window.fontsize / kf_window.cam.zoom; kf_drawtext( BLACK, select.x * KF_TILE_SIZE_PX, select.y * KF_TILE_SIZE_PX - s, s, text ); } static void _kf_modal_edit_render_ui() { /* Inherit from kf_modal_play. */ kf_modal_play.render_ui(); } struct kf_modal kf_modal_edit = { .name = "map editor", .init = _kf_modal_edit_init, .exit = _kf_modal_edit_exit, .update = _kf_modal_edit_update, .render_world = _kf_modal_edit_render_world, .render_ui = _kf_modal_edit_render_ui, .data = NULL, }; static void _kf_menu_palette_render_ui(void) { kf_tileid_t *selected = &((struct kf_editor *)kf_window.modal->data)->selected_tile; int px = 80, py = 80; DrawRectangle(px, py, 400, 400, BLACK); kf_drawtextshadowed(WHITE, px + 10, py + 10, kf_window.fontsize, "tiles :3"); py += 40; int x = 0, y = 0; int s = KF_TILE_SIZE_PX * 2; for (int i = 1 ; i <= kf_tiles.count ; i++) { Rectangle r = {px + x*s, py + y*s, s, s}; kf_drawsprite_wh(kf_tiles.sheet[i], r.x, r.y, r.width, r.height, kf_tiles.sprite[i].x + 0, kf_tiles.sprite[i].y + 3); if (*selected == i) DrawRectangleLinesEx(r, 1, GOLD); if (CheckCollisionPointRec(GetMousePosition(), r)) { DrawRectangleLinesEx(r, 1, WHITE); if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) *selected = i; } x += 1; if (x >= 8) { x = 0; y++; } } if (kf_checkinputpress(kf_inputbind_cancel)) kf_setmenu(NULL); }; struct kf_modal kf_menu_palette = { .name = "tile palette", .render_ui = _kf_menu_palette_render_ui, };