Capture all mouse events and take into account mouse attributes, fixes #58
This commit is contained in:
		| @@ -92,6 +92,6 @@ class CreditsDisplay(Display): | |||||||
|                 self.addstr(self.pad, y_offset + i, x_offset + j, c, |                 self.addstr(self.pad, y_offset + i, x_offset + j, c, | ||||||
|                             fg_color, bg_color, bold=bold) |                             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(" "): |         if self.pad.inch(y - 1, x - 1) != ord(" "): | ||||||
|             self.ascii_art_displayed = True |             self.ascii_art_displayed = True | ||||||
|   | |||||||
| @@ -187,12 +187,11 @@ class Display: | |||||||
|         """ |         """ | ||||||
|         raise NotImplementedError |         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. |         A mouse click was performed on the coordinates (y, x) of the pad. | ||||||
|         Maybe it should do something. |         Maybe it should do something. | ||||||
|         """ |         """ | ||||||
|         pass |  | ||||||
|  |  | ||||||
|     @property |     @property | ||||||
|     def rows(self) -> int: |     def rows(self) -> int: | ||||||
|   | |||||||
| @@ -63,7 +63,7 @@ class DisplayManager: | |||||||
|             d.pack = TexturePack.get_pack(self.game.settings.TEXTURE_PACK) |             d.pack = TexturePack.get_pack(self.game.settings.TEXTURE_PACK) | ||||||
|             d.update(self.game) |             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. |         Handles the mouse clicks. | ||||||
|         """ |         """ | ||||||
| @@ -76,7 +76,7 @@ class DisplayManager: | |||||||
|                 # of that display |                 # of that display | ||||||
|                 display = d |                 display = d | ||||||
|         if display: |         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]: |     def refresh(self) -> List[Display]: | ||||||
|         """ |         """ | ||||||
|   | |||||||
| @@ -50,7 +50,7 @@ class MenuDisplay(Display): | |||||||
|                          self.height - 2 + self.y, |                          self.height - 2 + self.y, | ||||||
|                          self.width - 2 + self.x) |                          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. |         We can select a menu item with the mouse. | ||||||
|         """ |         """ | ||||||
| @@ -134,13 +134,13 @@ class MainMenuDisplay(Display): | |||||||
|     def update(self, game: Game) -> None: |     def update(self, game: Game) -> None: | ||||||
|         self.menudisplay.update_menu(game.main_menu) |         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) |         menuwidth = min(self.menudisplay.preferred_width, self.width) | ||||||
|         menuy, menux = len(self.title) + 8, self.width // 2 - menuwidth // 2 - 1 |         menuy, menux = len(self.title) + 8, self.width // 2 - menuwidth // 2 - 1 | ||||||
|         menuheight = min(self.menudisplay.preferred_height, self.height - menuy) |         menuheight = min(self.menudisplay.preferred_height, self.height - menuy) | ||||||
|  |  | ||||||
|         if menuy <= y < menuy + menuheight and menux <= x < menux + menuwidth: |         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): |         if y <= len(self.title): | ||||||
|             self.fg_color = randint(0, 1000), randint(0, 1000), randint(0, 1000) |             self.fg_color = randint(0, 1000), randint(0, 1000), randint(0, 1000) | ||||||
| @@ -189,7 +189,7 @@ class PlayerInventoryDisplay(MenuDisplay): | |||||||
|     def trueheight(self) -> int: |     def trueheight(self) -> int: | ||||||
|         return 2 + super().trueheight |         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. |         We can select a menu item with the mouse. | ||||||
|         """ |         """ | ||||||
| @@ -232,7 +232,7 @@ class StoreInventoryDisplay(MenuDisplay): | |||||||
|     def trueheight(self) -> int: |     def trueheight(self) -> int: | ||||||
|         return 2 + super().trueheight |         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. |         We can select a menu item with the mouse. | ||||||
|         """ |         """ | ||||||
|   | |||||||
| @@ -100,8 +100,8 @@ class Game: | |||||||
|                 exit(0) |                 exit(0) | ||||||
|                 return |                 return | ||||||
|             if key == "KEY_MOUSE": |             if key == "KEY_MOUSE": | ||||||
|                 _ignored1, x, y, _ignored2, _ignored3 = curses.getmouse() |                 _ignored1, x, y, _ignored2, attr = curses.getmouse() | ||||||
|                 self.display_actions(DisplayActions.MOUSE, y, x) |                 self.display_actions(DisplayActions.MOUSE, y, x, attr) | ||||||
|             else: |             else: | ||||||
|                 self.handle_key_pressed( |                 self.handle_key_pressed( | ||||||
|                     KeyValues.translate_key(key, self.settings), key) |                     KeyValues.translate_key(key, self.settings), key) | ||||||
|   | |||||||
| @@ -21,7 +21,7 @@ class TermManager:  # pragma: no cover | |||||||
|         # make cursor invisible |         # make cursor invisible | ||||||
|         curses.curs_set(False) |         curses.curs_set(False) | ||||||
|         # Catch mouse events |         # Catch mouse events | ||||||
|         curses.mousemask(True) |         curses.mousemask(curses.ALL_MOUSE_EVENTS | curses.REPORT_MOUSE_POSITION) | ||||||
|         # Enable colors |         # Enable colors | ||||||
|         curses.start_color() |         curses.start_color() | ||||||
|  |  | ||||||
|   | |||||||
| @@ -1,6 +1,6 @@ | |||||||
| # Copyright (C) 2020 by ÿnérant, eichhornchen, nicomarg, charlse | # Copyright (C) 2020 by ÿnérant, eichhornchen, nicomarg, charlse | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  | import curses | ||||||
| import os | import os | ||||||
| import random | import random | ||||||
| import unittest | import unittest | ||||||
| @@ -257,10 +257,12 @@ class TestGame(unittest.TestCase): | |||||||
|         self.game.state = GameMode.MAINMENU |         self.game.state = GameMode.MAINMENU | ||||||
|  |  | ||||||
|         # Change the color of the artwork |         # 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 |         # 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.main_menu.position, 4) | ||||||
|         self.assertEqual(self.game.state, GameMode.SETTINGS) |         self.assertEqual(self.game.state, GameMode.SETTINGS) | ||||||
|  |  | ||||||
| @@ -272,11 +274,13 @@ class TestGame(unittest.TestCase): | |||||||
|         self.game.state = GameMode.INVENTORY |         self.game.state = GameMode.INVENTORY | ||||||
|  |  | ||||||
|         # Click nowhere |         # 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) |         self.assertEqual(self.game.state, GameMode.INVENTORY) | ||||||
|  |  | ||||||
|         # Click on the second item |         # 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.state, GameMode.INVENTORY) | ||||||
|         self.assertEqual(self.game.inventory_menu.position, 1) |         self.assertEqual(self.game.inventory_menu.position, 1) | ||||||
|  |  | ||||||
| @@ -572,7 +576,8 @@ class TestGame(unittest.TestCase): | |||||||
|         # Buy the second item by clicking on it |         # Buy the second item by clicking on it | ||||||
|         item = self.game.store_menu.validate() |         item = self.game.store_menu.validate() | ||||||
|         self.assertIn(item, merchant.inventory) |         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.assertIn(item, self.game.player.inventory) | ||||||
|         self.assertNotIn(item, merchant.inventory) |         self.assertNotIn(item, merchant.inventory) | ||||||
|  |  | ||||||
| @@ -747,9 +752,11 @@ class TestGame(unittest.TestCase): | |||||||
|         """ |         """ | ||||||
|         self.game.state = GameMode.MAINMENU |         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.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.display_actions(DisplayActions.REFRESH) | ||||||
|  |  | ||||||
|         self.game.state = GameMode.CREDITS |         self.game.state = GameMode.CREDITS | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user