Maybe mouse clicks may use the game

This commit is contained in:
Yohann D'ANELLO 2020-12-11 17:43:46 +01:00
parent d50f6701f4
commit 089a247b2f
3 changed files with 10 additions and 6 deletions

View File

@ -5,6 +5,7 @@ import curses
from typing import Any, Optional, Union from typing import Any, Optional, Union
from squirrelbattle.display.texturepack import TexturePack from squirrelbattle.display.texturepack import TexturePack
from squirrelbattle.game import Game
from squirrelbattle.tests.screen import FakePad from squirrelbattle.tests.screen import FakePad
@ -86,7 +87,7 @@ class Display:
def display(self) -> None: def display(self) -> None:
raise NotImplementedError raise NotImplementedError
def handle_click(self, y: int, x: int) -> None: def handle_click(self, y: int, x: 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 can do something. Maybe it can do something.

View File

@ -65,7 +65,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) display.handle_click(y - display.y, x - display.x, self.game)
def refresh(self) -> List[Display]: def refresh(self) -> List[Display]:
displays = [] displays = []

View File

@ -1,10 +1,13 @@
# 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 curses
from typing import List from typing import List
from squirrelbattle.menus import Menu, MainMenu from squirrelbattle.menus import Menu, MainMenu
from .display import Display, Box from .display import Display, Box
from ..enums import KeyValues
from ..game import Game
from ..resources import ResourceManager from ..resources import ResourceManager
from ..translations import gettext as _ from ..translations import gettext as _
@ -41,12 +44,12 @@ 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) -> None: def handle_click(self, y: int, x: int, game: Game) -> None:
""" """
We can select a menu item with the mouse. We can select a menu item with the mouse.
""" """
self.menu.position = y - 1 self.menu.position = y - 1
curses.ungetch('\n') game.handle_key_pressed(KeyValues.ENTER)
@property @property
def truewidth(self) -> int: def truewidth(self) -> int:
@ -106,13 +109,13 @@ class MainMenuDisplay(Display):
menuy, menux, min(self.menudisplay.preferred_height, menuy, menux, min(self.menudisplay.preferred_height,
self.height - menuy), menuwidth) self.height - menuy), menuwidth)
def handle_click(self, y: int, x: int) -> None: def handle_click(self, y: int, x: 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) self.menudisplay.handle_click(y - menuy, x - menux, game)
class InventoryDisplay(MenuDisplay): class InventoryDisplay(MenuDisplay):