From df7953c97fe6b6e4628d80c3d1535575ebcb76cd Mon Sep 17 00:00:00 2001 From: "C. McEnroe" Date: Tue, 28 Sep 2021 13:13:55 -0400 Subject: Add mouse support to freecell Works much like WEP FreeCell. --- freecell.c | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/freecell.c b/freecell.c index 872009a..9185870 100644 --- a/freecell.c +++ b/freecell.c @@ -216,7 +216,8 @@ static void curse(void) { initscr(); cbreak(); noecho(); - keypad(stdscr, false); + keypad(stdscr, true); + mousemask(BUTTON1_CLICKED | BUTTON1_DOUBLE_CLICKED, NULL); struct termios term; tcgetattr(STDOUT_FILENO, &term); term.c_iflag &= ~IXON; @@ -266,10 +267,13 @@ enum { CardHeight = 1, CellX = Padding, CellY = 2*CardHeight, - FoundationX = CellX + 4*(CardWidth+Padding), + CellWidth = 4*(CardWidth+Padding), + FoundationX = CellX + CellWidth, FoundationY = CellY, + FoundationWidth = 4*(CardWidth+Padding), TableauX = CellX, TableauY = CellY + 2*CardHeight, + TableauWidth = 8*(CardWidth+Padding), }; static uint game; @@ -319,8 +323,32 @@ static void draw(void) { static bool quit; static void input(void) { - char ch = getch(); + MEVENT m; + int ch = getch(); uint stack = Stacks; + + if (ch == KEY_MOUSE && getmouse(&m) == OK) { + if (m.y == CellY && m.x >= CellX && m.x < CellX+CellWidth) { + stack = Cell + (m.x-CellX) / (CardWidth+Padding); + } else if ( + m.y == FoundationY && + m.x >= FoundationX && m.x < FoundationX+FoundationWidth + ) { + stack = Foundation+3 - (m.x-FoundationX) / (CardWidth+Padding); + } else if ( + m.y >= TableauY && m.x >= TableauX && m.x < TableauX+TableauWidth + ) { + stack = Tableau + (m.x-TableauX) / (CardWidth+Padding); + } else { + srcStack = Stacks; + } + if (m.bstate == BUTTON1_DOUBLE_CLICKED) { + srcStack = stack; + } else if (stack == srcStack) { + srcStack = stack = Stacks; + } + } + switch (tolower(ch)) { break; case 'Q'^'@': quit = true; break; case '\33': srcStack = Stacks; @@ -384,5 +412,6 @@ uint playFreeCell(void) { draw(); input(); } + mousemask(0, NULL); return win(); } -- cgit 1.4.1