More generic display update

This commit is contained in:
Yohann D'ANELLO 2020-12-18 15:07:09 +01:00
parent b8d32b29c8
commit 85870494a0
8 changed files with 46 additions and 18 deletions

View File

@ -162,6 +162,16 @@ class Display:
pad.refresh(top_y, top_x, window_y, window_x, last_y, last_x) pad.refresh(top_y, top_x, window_y, window_x, last_y, last_x)
def display(self) -> None: def display(self) -> None:
"""
Draw the content of the display and refresh pads.
"""
raise NotImplementedError
def update(self, game: Game) -> None:
"""
The game state was updated.
Indicate what to do with the new state.
"""
raise NotImplementedError raise NotImplementedError
def handle_click(self, y: int, x: int, game: Game) -> None: def handle_click(self, y: int, x: int, game: Game) -> None:

View File

@ -49,16 +49,13 @@ class DisplayManager:
self.handle_mouse_click(*params) self.handle_mouse_click(*params)
def update_game_components(self) -> None: def update_game_components(self) -> None:
"""
The game state was updated.
Trigger all displays of these modifications.
"""
for d in self.displays: for d in self.displays:
d.pack = TexturePack.get_pack(self.game.settings.TEXTURE_PACK) d.pack = TexturePack.get_pack(self.game.settings.TEXTURE_PACK)
self.mapdisplay.update_map(self.game.map) d.update(self.game)
self.statsdisplay.update_player(self.game.player)
self.game.inventory_menu.update_player(self.game.player)
self.playerinventorydisplay.update_menu(self.game.inventory_menu)
self.storeinventorydisplay.update_menu(self.game.store_menu)
self.settingsmenudisplay.update_menu(self.game.settings_menu)
self.logsdisplay.update_logs(self.game.logs)
self.messagedisplay.update_message(self.game.message)
def handle_mouse_click(self, y: int, x: int) -> None: def handle_mouse_click(self, y: int, x: int) -> None:
displays = self.refresh() displays = self.refresh()

View File

@ -2,6 +2,7 @@
# SPDX-License-Identifier: GPL-3.0-or-later # SPDX-License-Identifier: GPL-3.0-or-later
from squirrelbattle.display.display import Display from squirrelbattle.display.display import Display
from squirrelbattle.game import Game
from squirrelbattle.interfaces import Logs from squirrelbattle.interfaces import Logs
@ -11,8 +12,8 @@ class LogsDisplay(Display):
super().__init__(*args) super().__init__(*args)
self.pad = self.newpad(self.rows, self.cols) self.pad = self.newpad(self.rows, self.cols)
def update_logs(self, logs: Logs) -> None: def update(self, game: Game) -> None:
self.logs = logs self.logs = game.logs
def display(self) -> None: def display(self) -> None:
messages = self.logs.messages[-self.height:] messages = self.logs.messages[-self.height:]

View File

@ -3,6 +3,7 @@
from squirrelbattle.interfaces import Map from squirrelbattle.interfaces import Map
from .display import Display from .display import Display
from ..game import Game
class MapDisplay(Display): class MapDisplay(Display):
@ -10,9 +11,10 @@ class MapDisplay(Display):
def __init__(self, *args): def __init__(self, *args):
super().__init__(*args) super().__init__(*args)
def update_map(self, m: Map) -> None: def update(self, game: Game) -> None:
self.map = m self.map = game.map
self.pad = self.newpad(m.height, self.pack.tile_width * m.width + 1) self.pad = self.newpad(self.map.height,
self.pack.tile_width * self.map.width + 1)
def update_pad(self) -> None: def update_pad(self) -> None:
self.addstr(self.pad, 0, 0, self.map.draw_string(self.pack), self.addstr(self.pad, 0, 0, self.map.draw_string(self.pack),

View File

@ -5,7 +5,7 @@ import curses
from random import randint from random import randint
from typing import List from typing import List
from squirrelbattle.menus import Menu, MainMenu from squirrelbattle.menus import Menu, MainMenu, SettingsMenu
from .display import Box, Display from .display import Box, Display
from ..enums import KeyValues, GameMode from ..enums import KeyValues, GameMode
from ..game import Game from ..game import Game
@ -17,6 +17,7 @@ class MenuDisplay(Display):
""" """
A class to display the menu objects A class to display the menu objects
""" """
menu: Menu
position: int position: int
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
@ -80,6 +81,11 @@ class SettingsMenuDisplay(MenuDisplay):
""" """
A class to display specifically a settingsmenu object A class to display specifically a settingsmenu object
""" """
menu: SettingsMenu
def update(self, game: Game) -> None:
self.update_menu(game.settings_menu)
@property @property
def values(self) -> List[str]: def values(self) -> List[str]:
return [_(a[1][1]) + (" : " return [_(a[1][1]) + (" : "
@ -122,6 +128,9 @@ 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 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, 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
@ -135,6 +144,9 @@ class MainMenuDisplay(Display):
class PlayerInventoryDisplay(MenuDisplay): class PlayerInventoryDisplay(MenuDisplay):
def update(self, game: Game) -> None:
self.update_menu(game.inventory_menu)
def update_pad(self) -> None: def update_pad(self) -> None:
self.menubox.update_title(_("INVENTORY")) self.menubox.update_title(_("INVENTORY"))
for i, item in enumerate(self.menu.values): for i, item in enumerate(self.menu.values):
@ -166,6 +178,9 @@ class PlayerInventoryDisplay(MenuDisplay):
class StoreInventoryDisplay(MenuDisplay): class StoreInventoryDisplay(MenuDisplay):
def update(self, game: Game) -> None:
self.update_menu(game.store_menu)
def update_pad(self) -> None: def update_pad(self) -> None:
self.menubox.update_title(_("STALL")) self.menubox.update_title(_("STALL"))
for i, item in enumerate(self.menu.values): for i, item in enumerate(self.menu.values):

View File

@ -3,6 +3,7 @@
import curses import curses
from squirrelbattle.display.display import Box, Display from squirrelbattle.display.display import Box, Display
from squirrelbattle.game import Game
class MessageDisplay(Display): class MessageDisplay(Display):
@ -17,8 +18,8 @@ class MessageDisplay(Display):
self.message = "" self.message = ""
self.pad = self.newpad(1, 1) self.pad = self.newpad(1, 1)
def update_message(self, msg: str) -> None: def update(self, game: Game) -> None:
self.message = msg self.message = game.message
def display(self) -> None: def display(self) -> None:
self.box.refresh(self.y - 1, self.x - 2, self.box.refresh(self.y - 1, self.x - 2,

View File

@ -4,6 +4,7 @@
import curses import curses
from ..entities.player import Player from ..entities.player import Player
from ..game import Game
from ..translations import gettext as _ from ..translations import gettext as _
from .display import Display from .display import Display
@ -15,8 +16,8 @@ class StatsDisplay(Display):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.pad = self.newpad(self.rows, self.cols) self.pad = self.newpad(self.rows, self.cols)
def update_player(self, p: Player) -> None: def update(self, game: Game) -> None:
self.player = p self.player = game.player
def update_pad(self) -> None: def update_pad(self) -> None:
string2 = "Player -- LVL {}\nEXP {}/{}\nHP {}/{}"\ string2 = "Player -- LVL {}\nEXP {}/{}\nHP {}/{}"\

View File

@ -404,6 +404,7 @@ class TestGame(unittest.TestCase):
Check that some functions are not implemented, only for coverage. Check that some functions are not implemented, only for coverage.
""" """
self.assertRaises(NotImplementedError, Display.display, None) self.assertRaises(NotImplementedError, Display.display, None)
self.assertRaises(NotImplementedError, Display.update, None, self.game)
def test_messages(self) -> None: def test_messages(self) -> None:
""" """