Encapsulated behaviour into cursor_direction().

This commit is contained in:
Murilo Pereira 2011-06-06 22:06:10 -03:00
parent 977c80b91e
commit 6588525e8e
3 changed files with 31 additions and 32 deletions

View File

@ -3,6 +3,7 @@
#include <malloc.h>
#include <errno.h>
#include <ncurses.h>
#include <assert.h>
#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");
}
}

View File

@ -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

View File

@ -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: