aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKirill Petrashin <kirill8201@yandex.ru>2026-04-13 16:10:12 +0300
committerKirill Petrashin <kirill8201@yandex.ru>2026-04-13 16:10:12 +0300
commit734f6379f0978088c7051ec8645cf1e2962fee49 (patch)
treed92cfbe473abd33af372d0a1ddb92dfad97e62a1
parent41bfefc9e08b0ae4a8de6e96e33feb3e17110011 (diff)
downloadastar-734f6379f0978088c7051ec8645cf1e2962fee49.tar.xz
Improve the map editor
-rw-r--r--map.c64
1 files 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);
}
}