summary refs log tree commit diff homepage
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2021-09-28 13:13:55 -0400
committerJune McEnroe <june@causal.agency>2021-09-28 13:13:55 -0400
commitdf7953c97fe6b6e4628d80c3d1535575ebcb76cd (patch)
treef4dd8e8138beff52e284f61daac3bff9cb1c2224
parentAdd direct game selection tip to menu (diff)
downloadplay-df7953c97fe6b6e4628d80c3d1535575ebcb76cd.tar.gz
play-df7953c97fe6b6e4628d80c3d1535575ebcb76cd.zip
Add mouse support to freecell
Works much like WEP FreeCell.
Diffstat (limited to '')
-rw-r--r--freecell.c35
1 files 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();
 }