Capture all mouse events and take into account mouse attributes, fixes #58

This commit is contained in:
Yohann D'ANELLO 2021-01-08 11:21:40 +01:00
parent b42f1277b1
commit 75e93611c3
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
7 changed files with 27 additions and 21 deletions

View File

@ -92,6 +92,6 @@ class CreditsDisplay(Display):
self.addstr(self.pad, y_offset + i, x_offset + j, c,
fg_color, bg_color, bold=bold)
def handle_click(self, y: int, x: int, game: Game) -> None:
def handle_click(self, y: int, x: int, attr: int, game: Game) -> None:
if self.pad.inch(y - 1, x - 1) != ord(" "):
self.ascii_art_displayed = True

View File

@ -187,12 +187,11 @@ class Display:
"""
raise NotImplementedError
def handle_click(self, y: int, x: int, game: Game) -> None:
def handle_click(self, y: int, x: int, attr: int, game: Game) -> None:
"""
A mouse click was performed on the coordinates (y, x) of the pad.
Maybe it should do something.
"""
pass
@property
def rows(self) -> int:

View File

@ -63,7 +63,7 @@ class DisplayManager:
d.pack = TexturePack.get_pack(self.game.settings.TEXTURE_PACK)
d.update(self.game)
def handle_mouse_click(self, y: int, x: int) -> None:
def handle_mouse_click(self, y: int, x: int, attr: int) -> None:
"""
Handles the mouse clicks.
"""
@ -76,7 +76,7 @@ class DisplayManager:
# of that display
display = d
if display:
display.handle_click(y - display.y, x - display.x, self.game)
display.handle_click(y - display.y, x - display.x, attr, self.game)
def refresh(self) -> List[Display]:
"""

View File

@ -50,7 +50,7 @@ class MenuDisplay(Display):
self.height - 2 + self.y,
self.width - 2 + self.x)
def handle_click(self, y: int, x: int, game: Game) -> None:
def handle_click(self, y: int, x: int, attr: int, game: Game) -> None:
"""
We can select a menu item with the mouse.
"""
@ -134,13 +134,13 @@ class MainMenuDisplay(Display):
def update(self, game: Game) -> None:
self.menudisplay.update_menu(game.main_menu)
def handle_click(self, y: int, x: int, game: Game) -> None:
def handle_click(self, y: int, x: int, attr: int, game: Game) -> None:
menuwidth = min(self.menudisplay.preferred_width, self.width)
menuy, menux = len(self.title) + 8, self.width // 2 - menuwidth // 2 - 1
menuheight = min(self.menudisplay.preferred_height, self.height - menuy)
if menuy <= y < menuy + menuheight and menux <= x < menux + menuwidth:
self.menudisplay.handle_click(y - menuy, x - menux, game)
self.menudisplay.handle_click(y - menuy, x - menux, attr, game)
if y <= len(self.title):
self.fg_color = randint(0, 1000), randint(0, 1000), randint(0, 1000)
@ -189,7 +189,7 @@ class PlayerInventoryDisplay(MenuDisplay):
def trueheight(self) -> int:
return 2 + super().trueheight
def handle_click(self, y: int, x: int, game: Game) -> None:
def handle_click(self, y: int, x: int, attr: int, game: Game) -> None:
"""
We can select a menu item with the mouse.
"""
@ -232,7 +232,7 @@ class StoreInventoryDisplay(MenuDisplay):
def trueheight(self) -> int:
return 2 + super().trueheight
def handle_click(self, y: int, x: int, game: Game) -> None:
def handle_click(self, y: int, x: int, attr: int, game: Game) -> None:
"""
We can select a menu item with the mouse.
"""

View File

@ -100,8 +100,8 @@ class Game:
exit(0)
return
if key == "KEY_MOUSE":
_ignored1, x, y, _ignored2, _ignored3 = curses.getmouse()
self.display_actions(DisplayActions.MOUSE, y, x)
_ignored1, x, y, _ignored2, attr = curses.getmouse()
self.display_actions(DisplayActions.MOUSE, y, x, attr)
else:
self.handle_key_pressed(
KeyValues.translate_key(key, self.settings), key)

View File

@ -21,7 +21,7 @@ class TermManager: # pragma: no cover
# make cursor invisible
curses.curs_set(False)
# Catch mouse events
curses.mousemask(True)
curses.mousemask(curses.ALL_MOUSE_EVENTS | curses.REPORT_MOUSE_POSITION)
# Enable colors
curses.start_color()

View File

@ -1,6 +1,6 @@
# Copyright (C) 2020 by ÿnérant, eichhornchen, nicomarg, charlse
# SPDX-License-Identifier: GPL-3.0-or-later
import curses
import os
import random
import unittest
@ -257,10 +257,12 @@ class TestGame(unittest.TestCase):
self.game.state = GameMode.MAINMENU
# Change the color of the artwork
self.game.display_actions(DisplayActions.MOUSE, 0, 10)
self.game.display_actions(DisplayActions.MOUSE, 0, 10,
curses.BUTTON1_CLICKED)
# Settings menu
self.game.display_actions(DisplayActions.MOUSE, 25, 21)
self.game.display_actions(DisplayActions.MOUSE, 25, 21,
curses.BUTTON1_CLICKED)
self.assertEqual(self.game.main_menu.position, 4)
self.assertEqual(self.game.state, GameMode.SETTINGS)
@ -272,11 +274,13 @@ class TestGame(unittest.TestCase):
self.game.state = GameMode.INVENTORY
# Click nowhere
self.game.display_actions(DisplayActions.MOUSE, 0, 0)
self.game.display_actions(DisplayActions.MOUSE, 0, 0,
curses.BUTTON1_CLICKED)
self.assertEqual(self.game.state, GameMode.INVENTORY)
# Click on the second item
self.game.display_actions(DisplayActions.MOUSE, 8, 25)
self.game.display_actions(DisplayActions.MOUSE, 8, 25,
curses.BUTTON1_CLICKED)
self.assertEqual(self.game.state, GameMode.INVENTORY)
self.assertEqual(self.game.inventory_menu.position, 1)
@ -572,7 +576,8 @@ class TestGame(unittest.TestCase):
# Buy the second item by clicking on it
item = self.game.store_menu.validate()
self.assertIn(item, merchant.inventory)
self.game.display_actions(DisplayActions.MOUSE, 7, 25)
self.game.display_actions(DisplayActions.MOUSE, 7, 25,
curses.BUTTON1_CLICKED)
self.assertIn(item, self.game.player.inventory)
self.assertNotIn(item, merchant.inventory)
@ -747,9 +752,11 @@ class TestGame(unittest.TestCase):
"""
self.game.state = GameMode.MAINMENU
self.game.display_actions(DisplayActions.MOUSE, 41, 41)
self.game.display_actions(DisplayActions.MOUSE, 41, 41,
curses.BUTTON1_CLICKED)
self.assertEqual(self.game.state, GameMode.CREDITS)
self.game.display_actions(DisplayActions.MOUSE, 21, 21)
self.game.display_actions(DisplayActions.MOUSE, 21, 21,
curses.BUTTON1_CLICKED)
self.game.display_actions(DisplayActions.REFRESH)
self.game.state = GameMode.CREDITS