Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/games/gomoku gomoku: add mouse support



details:   https://anonhg.NetBSD.org/src/rev/af976143bd00
branches:  trunk
changeset: 366348:af976143bd00
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sun May 22 13:38:08 2022 +0000

description:
gomoku: add mouse support

NetBSD 10 provides the mouse handling functions from <curses.h> but does
not actually implement the mouse handling.  For the benefit of other
platforms, add mouse support; when linked with ncurses instead of
curses, it works.

Depending on the input device, mouse clicks are either reported as
"button 1 clicked" (mouse) or "button 1 pressed/released" (touchpad);
support both.

Be strict about the X coordinate when clicking.  Since the coordinates
are integer numbers, getting the location between two spots is
ambiguous, as it could be just one pixel away or right in the middle of
the space between the spots.

diffstat:

 games/gomoku/bdisp.c |  45 +++++++++++++++++++++++----------------------
 1 files changed, 23 insertions(+), 22 deletions(-)

diffs (82 lines):

diff -r 7906462fd71a -r af976143bd00 games/gomoku/bdisp.c
--- a/games/gomoku/bdisp.c      Sun May 22 13:20:44 2022 +0000
+++ b/games/gomoku/bdisp.c      Sun May 22 13:38:08 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: bdisp.c,v 1.45 2022/05/22 12:42:54 rillig Exp $        */
+/*     $NetBSD: bdisp.c,v 1.46 2022/05/22 13:38:08 rillig Exp $        */
 
 /*
  * Copyright (c) 1994
@@ -34,7 +34,7 @@
 
 #include <sys/cdefs.h>
 /*     @(#)bdisp.c     8.2 (Berkeley) 5/3/95   */
-__RCSID("$NetBSD: bdisp.c,v 1.45 2022/05/22 12:42:54 rillig Exp $");
+__RCSID("$NetBSD: bdisp.c,v 1.46 2022/05/22 13:38:08 rillig Exp $");
 
 #include <curses.h>
 #include <string.h>
@@ -73,9 +73,7 @@
        cbreak();
        leaveok(stdscr, false);
 
-#if 0 /* no mouse support in netbsd curses yet */
        mousemask(BUTTON1_CLICKED, NULL);
-#endif
 }
 
 /*
@@ -323,6 +321,23 @@
        return c != EOF;
 }
 
+static bool
+get_coord_mouse(int *x, int *y)
+{
+       MEVENT ev;
+
+       if (getmouse(&ev) == OK &&
+           (ev.bstate & (BUTTON1_RELEASED | BUTTON1_CLICKED)) != 0 &&
+           ev.y >= scr_y(BSZ) && ev.y <= scr_y(1) &&
+           ev.x >= scr_x(1) && ev.x <= scr_x(BSZ) &&
+           (ev.x - scr_x(1)) % (scr_x(2) - scr_x(1)) == 0) {
+               *x = 1 + (ev.x - scr_x(1)) / (scr_x(2) - scr_x(1));
+               *y = 1 + (scr_y(1) - ev.y) / (scr_y(1) - scr_y(2));
+               return true;
+       }
+       return false;
+}
+
 /*
  * Ask the user for the coordinate of a move, or return RESIGN or SAVE.
  *
@@ -419,25 +434,11 @@
                        (void)clearok(stdscr, true);
                        (void)refresh();
                        break;
-#if 0 /* notyet */
                case KEY_MOUSE:
-               {
-                       MEVENT  myevent;
-
-                       getmouse(&myevent);
-                       /* XXX: 'y <= BSZ + 1' should probably be '<'. */
-                       /* TODO: use scr_x and scr_y. */
-                       if (myevent.y >= 1 && myevent.y <= BSZ + 1 &&
-                           myevent.x >= 3 && myevent.x <= 2 * BSZ + 1) {
-                               curx0 = (myevent.x - 3) / 2;
-                               cury0 = BSZ - myevent.y;
-                               return PT(curx0,cury0);
-                       } else {
-                               beep();
-                       }
-               }
-               break;
-#endif
+                       if (get_coord_mouse(&x, &y))
+                               return PT(x, y);
+                       beep();
+                       break;
                case 'Q':
                case 'q':
                        return RESIGN;



Home | Main Index | Thread Index | Old Index