From 734f6379f0978088c7051ec8645cf1e2962fee49 Mon Sep 17 00:00:00 2001 From: Kirill Petrashin Date: Mon, 13 Apr 2026 16:10:12 +0300 Subject: Improve the map editor --- map.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 59 insertions(+), 5 deletions(-) diff --git a/map.c b/map.c index 5dc2782..ce8f949 100644 --- a/map.c +++ b/map.c @@ -452,6 +452,7 @@ void map_editor(Map *map, size_t *width, size_t *height, Position *start, Positi int ch = getch(); if (ch == 'q') break; + /* Handle keyboard keys */ switch (ch) { case 'h': map_offset_x += 2; break; case 'l': map_offset_x -= 2; break; @@ -498,8 +499,8 @@ void map_editor(Map *map, size_t *width, size_t *height, Position *start, Positi /* TODO: keybindings to resize the map */ } - draw_map(*map, *width, *height, *start, *goal, NULL, NULL, NULL, NULL); + /* Handle mouse */ if (ch == KEY_MOUSE) { if (getmouse(&event) == OK) { if (event.bstate & BUTTON1_PRESSED) { @@ -528,40 +529,91 @@ void map_editor(Map *map, size_t *width, size_t *height, Position *start, Positi } } } else if (row == *height) { /* Resize vertically */ - while ((ch = getch()) == KEY_MOUSE && getmouse(&event) == OK && !(event.bstate & BUTTON1_RELEASED)) {/* TODO: draw a line */}; + /* Draw the line when we just click the border */ + for (size_t i = map_offset_x; i < *width*2 + map_offset_x; i++) { + mvaddch(event.y, i, '='); + } + + /* Wait for BUTTON1_RELEASED */ + size_t old_event_y = event.y; + while ((ch = getch()) == KEY_MOUSE && getmouse(&event) == OK && !(event.bstate & BUTTON1_RELEASED)) { + if (event.y <= map_offset_y) continue; + draw_map(*map, *width, *height, *start, *goal, NULL, NULL, NULL, NULL); + for (size_t i = map_offset_x; i < *width*2 + map_offset_x; i++) { + if (old_event_y > *height + map_offset_y + 1) + mvaddch(old_event_y, i, ' '); + mvaddch(event.y, i, '='); + } + old_event_y = event.y; + }; + size_t new_height = event.y - map_offset_y; if (event.y <= map_offset_y) continue; + Map new_map = empty_map(*width, new_height); map_copy(*map, *width, *height, new_map, *width, new_height); map_free(*map, *height); + *height = new_height; *map = new_map; + + /* Make sure start and goal aren't outside of the map */ if (start->y >= *height) { start->y = *height - 1; } if (goal->y >= *height) { goal->y = *height - 1; } + clear(); draw_map(*map, *width, *height, *start, *goal, NULL, NULL, NULL, NULL); } else if (col == *width) { /* Resize horizontally */ - while ((ch = getch()) == KEY_MOUSE && getmouse(&event) == OK && !(event.bstate & BUTTON1_RELEASED)) {/* TODO: draw a line */}; size_t new_width = (event.x - map_offset_x) / 2; - if (event.x <= map_offset_x) continue; + + /* Draw the line when we just click the border */ + for (size_t i = map_offset_y; i < *height + map_offset_y; i++) { + mvaddch(i, new_width * 2 + map_offset_x, '|'); + mvaddch(i, new_width * 2 + map_offset_x + 1, '|'); + } + + /* Wait for BUTTON1_RELEASED */ + size_t old_new_width = (event.x - map_offset_x) / 2; /* Used to erase the old line */ + while ((ch = getch()) == KEY_MOUSE && getmouse(&event) == OK && !(event.bstate & BUTTON1_RELEASED)) { + size_t new_width = (event.x - map_offset_x) / 2; + if (event.x <= map_offset_x || new_width == 0) continue; + draw_map(*map, *width, *height, *start, *goal, NULL, NULL, NULL, NULL); + for (size_t i = map_offset_y; i < *height + map_offset_y; i++) { + if (old_new_width > *width + map_offset_x/2) { + mvaddch(i, old_new_width * 2 + map_offset_x, ' '); + mvaddch(i, old_new_width * 2 + map_offset_x + 1, ' '); + } + mvaddch(i, new_width * 2 + map_offset_x, '|'); + mvaddch(i, new_width * 2 + map_offset_x + 1, '|'); + } + old_new_width = new_width; + }; + + new_width = (event.x - map_offset_x) / 2; + if (event.x <= map_offset_x || new_width == 0) continue; + Map new_map = empty_map(new_width, *height); map_copy(*map, *width, *height, new_map, new_width, *height); map_free(*map, *height); + *width = new_width; *map = new_map; + + /* Make sure start and goal aren't outside of the map */ if (start->x >= *width) { start->x = *width - 1; } if (goal->x >= *width) { goal->x = *width - 1; } + clear(); draw_map(*map, *width, *height, *start, *goal, NULL, NULL, NULL, NULL); - } else { /* Draw */ + } else { /* Start drawing */ m1down = 1; if (row < *height && col < *width) { if ((*map)[row][col] == WALL) @@ -583,6 +635,8 @@ void map_editor(Map *map, size_t *width, size_t *height, Position *start, Positi } } } + } else { /* Only update the map if not a mouse event */ + draw_map(*map, *width, *height, *start, *goal, NULL, NULL, NULL, NULL); } } -- cgit v1.2.3