From ab9b64492d22317fdd29c7943f22621025cdf64b Mon Sep 17 00:00:00 2001 From: SuperNovaa41 Date: Wed, 15 Jan 2025 17:17:42 -0500 Subject: [PATCH] implements a falldown --- src/controls.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/controls.c b/src/controls.c index a9e1997..0e66acb 100644 --- a/src/controls.c +++ b/src/controls.c @@ -64,20 +64,37 @@ int parse_input_char(char input_char) return 0; } +#define DO_FALLDOWN() if (editor.cx > editor.rows[editor.cy - 1].len) {editor.cx = editor.rows[editor.cy - 1].len;move_cursor_pos(editor.cx, editor.cy);} + void move_cursor(movement_t dir) { switch(dir) { case LEFT: + if (editor.cx - 1 <= 0) + break; move_cursor_pos(--editor.cx, editor.cy); break; case RIGHT: + if (editor.cx + 1 > editor.rows[editor.cy - 1].len) + break; move_cursor_pos(++editor.cx, editor.cy); break; case UP: + if (editor.cy -1 <= 0) + break; move_cursor_pos(editor.cx, --editor.cy); + + // want to fall down to the end of the line if we're moving to a shorter one + DO_FALLDOWN(); + break; case DOWN: + if (editor.cy + 1 >= editor.num_rows) + break; move_cursor_pos(editor.cx, ++editor.cy); + + DO_FALLDOWN(); + break; case HOME: move_cursor_pos(1, 1); @@ -94,3 +111,4 @@ void move_cursor(movement_t dir) } } +#undef DO_FALLDOWN