diff --git a/src/cursor.c b/src/cursor.c index b23fc3a..ab1df19 100644 --- a/src/cursor.c +++ b/src/cursor.c @@ -3,6 +3,7 @@ #include #include #include +#include #include "cursor.h" #include "game.h" @@ -90,3 +91,24 @@ void cursor_move(struct cursor *cursor, enum movement movement) { break; } } + +enum movement cursor_direction(int key) { + switch (key) { + case 'h': + case KEY_LEFT: + return(LEFT); + case 'j': + case KEY_DOWN: + return(DOWN); + case 'k': + case KEY_UP: + return(UP); + case 'l': + case KEY_RIGHT: + return(RIGHT); + default: + endwin(); + game_end(); + assert(false && "invalid cursor direction"); + } +} diff --git a/src/cursor.h b/src/cursor.h index 39664b1..6b7c706 100644 --- a/src/cursor.h +++ b/src/cursor.h @@ -41,5 +41,6 @@ void cursor_free(struct cursor *); void cursor_mark(struct cursor *); void cursor_unmark(struct cursor *); void cursor_move(struct cursor *, enum movement); +enum movement cursor_direction(int); #endif diff --git a/src/keyboard.c b/src/keyboard.c index a5d07ce..cb3b47d 100644 --- a/src/keyboard.c +++ b/src/keyboard.c @@ -105,27 +105,15 @@ static void handle_card_movement(struct cursor *cursor) { while (1) { switch (option = getch()) { case 'h': - case KEY_LEFT: - erase_cursor(cursor); - cursor_move(cursor, LEFT); - draw_cursor(cursor); - break; case 'j': - case KEY_DOWN: - erase_cursor(cursor); - cursor_move(cursor, DOWN); - draw_cursor(cursor); - break; case 'k': - case KEY_UP: - erase_cursor(cursor); - cursor_move(cursor, UP); - draw_cursor(cursor); - break; case 'l': + case KEY_LEFT: + case KEY_DOWN: + case KEY_UP: case KEY_RIGHT: erase_cursor(cursor); - cursor_move(cursor, RIGHT); + cursor_move(cursor, cursor_direction(option)); draw_cursor(cursor); break; case 'm': @@ -227,27 +215,15 @@ static void handle_card_movement(struct cursor *cursor) { void handle_keyboard_event(int key) { switch (key) { case 'h': - case KEY_LEFT: - erase_cursor(cursor); - cursor_move(cursor, LEFT); - draw_cursor(cursor); - break; case 'j': - case KEY_DOWN: - erase_cursor(cursor); - cursor_move(cursor, DOWN); - draw_cursor(cursor); - break; case 'k': - case KEY_UP: - erase_cursor(cursor); - cursor_move(cursor, UP); - draw_cursor(cursor); - break; case 'l': + case KEY_LEFT: + case KEY_DOWN: + case KEY_UP: case KEY_RIGHT: erase_cursor(cursor); - cursor_move(cursor, RIGHT); + cursor_move(cursor, cursor_direction(key)); draw_cursor(cursor); break; case KEY_SPACEBAR: