Implemented logic for moving blocks of cards.

This commit is contained in:
Murilo Pereira 2011-06-05 17:34:02 -03:00
parent b7cce35315
commit 3b1dd0bcf6

View File

@ -58,6 +58,8 @@ static void handle_stock_event() {
} }
} }
/* FIXME: this function does not work on stacks with no marked cards.
* In that case it returns the stack's length. */
static int marked_cards_count(struct stack *stack) { static int marked_cards_count(struct stack *stack) {
if (length(stack) == 1) { if (length(stack) == 1) {
if (stack->card->frame->begin_y > MANEUVRE_BEGIN_Y) { if (stack->card->frame->begin_y > MANEUVRE_BEGIN_Y) {
@ -66,8 +68,7 @@ static int marked_cards_count(struct stack *stack) {
} else if (length(stack) > 1) { } else if (length(stack) > 1) {
for (int marked_cards_count = 0; stack; stack = stack->next) { for (int marked_cards_count = 0; stack; stack = stack->next) {
marked_cards_count++; marked_cards_count++;
if (!stack->next || (stack->card->frame->begin_y - if (!stack->next || (stack->card->frame->begin_y - stack->next->card->frame->begin_y) > 1) {
stack->next->card->frame->begin_y) > 1) {
return(marked_cards_count); return(marked_cards_count);
} }
} }
@ -163,22 +164,40 @@ static void handle_card_movement(struct cursor *cursor) {
break; break;
case KEY_SPACEBAR: case KEY_SPACEBAR:
destination = cursor_stack(cursor); destination = cursor_stack(cursor);
if (maneuvre_stack(*origin) && marked_cards_count(*origin) > 0) { int marked_cards = marked_cards_count(*origin);
if (maneuvre_stack(*origin) && marked_cards > 0) {
erase_stack(*origin); erase_stack(*origin);
unmark_cards(*origin); unmark_cards(*origin);
draw_stack(*origin);
} }
/* As 'destination' can be NULL if the cursor is at the invalid spot we if (marked_cards > 1 && maneuvre_stack(*origin) && maneuvre_stack(*destination)) {
* check it before dereferencing */ struct stack *block = *origin;
if (destination && valid_move(*origin, *destination)) { for (int i = 1; i < marked_cards; block = block->next, i++)
erase_stack(*origin); ;
move_card(origin, destination); if (destination && valid_move(block, *destination)) {
draw_stack(*origin); erase_stack(*origin);
draw_stack(*destination); struct stack *tmp;
if (maneuvre_stack(*destination) && length(*destination) > 1) { allocate_stack(&tmp);
erase_cursor(cursor); initialize_stack(tmp);
cursor->y++; for (int i = 0; i < marked_cards; i++) {
draw_cursor(cursor); push(&tmp, pop(origin));
}
for (int i = 0; i < marked_cards; i++) {
move_card(&tmp, destination);
}
if (length(*destination) > 1) {
erase_cursor(cursor);
cursor->y += marked_cards;
}
free_stack(tmp);
}
} else {
if (destination && valid_move(*origin, *destination)) {
erase_stack(*origin);
move_card(origin, destination);
if (maneuvre_stack(*destination) && length(*destination) > 1) {
erase_cursor(cursor);
cursor->y++;
}
} }
} }
if (destination && *origin == *destination) { if (destination && *origin == *destination) {
@ -187,6 +206,8 @@ static void handle_card_movement(struct cursor *cursor) {
} }
unmark_cursor(cursor); unmark_cursor(cursor);
draw_cursor(cursor); draw_cursor(cursor);
draw_stack(*origin);
draw_stack(*destination);
return; return;
case KEY_ESCAPE: case KEY_ESCAPE:
if (cursor_stack(cursor) == origin && maneuvre_stack(*origin)) { if (cursor_stack(cursor) == origin && maneuvre_stack(*origin)) {