squirrel-battle/dungeonbattle/display/display_manager.py

70 lines
2.8 KiB
Python
Raw Normal View History

import curses
from dungeonbattle.display.mapdisplay import MapDisplay
from dungeonbattle.display.statsdisplay import StatsDisplay
from dungeonbattle.display.menudisplay import MenuDisplay, MainMenuDisplay
from dungeonbattle.display.texturepack import TexturePack
from typing import Any
from dungeonbattle.game import Game, GameMode
2020-11-11 23:12:30 +00:00
from dungeonbattle.enums import DisplayActions
class DisplayManager:
2020-11-10 19:34:22 +00:00
def __init__(self, screen: Any, g: Game):
self.game = g
self.screen = screen
pack = TexturePack.get_pack(self.game.settings.TEXTURE_PACK)
self.mapdisplay = MapDisplay(screen, pack)
self.statsdisplay = StatsDisplay(screen, pack)
2020-11-10 19:34:22 +00:00
self.mainmenudisplay = MainMenuDisplay(self.game.main_menu,
screen, pack)
self.settingsmenudisplay = MenuDisplay(screen, pack)
2020-11-10 19:34:22 +00:00
self.displays = [self.statsdisplay, self.mapdisplay,
self.mainmenudisplay, self.settingsmenudisplay]
self.update_game_components()
2020-11-12 00:58:10 +00:00
def handle_display_action(self, action: DisplayActions) -> None:
2020-11-11 23:12:30 +00:00
if action == DisplayActions.REFRESH:
self.refresh()
2020-11-11 23:12:30 +00:00
elif action == DisplayActions.UPDATE:
self.update_game_components()
2020-11-10 19:34:22 +00:00
def update_game_components(self) -> None:
for d in self.displays:
d.pack = TexturePack.get_pack(self.game.settings.TEXTURE_PACK)
self.mapdisplay.update_map(self.game.map)
self.statsdisplay.update_player(self.game.player)
2020-11-11 23:12:30 +00:00
self.settingsmenudisplay.update_menu(self.game.settings_menu)
def refresh(self) -> None:
if self.game.state == GameMode.PLAY:
# The map pad has already the good size
self.mapdisplay.refresh(0, 0, self.rows * 4 // 5, self.cols,
resize_pad=False)
2020-11-10 19:34:22 +00:00
self.statsdisplay.refresh(self.rows * 4 // 5, 0,
self.rows // 5, self.cols)
if self.game.state == GameMode.MAINMENU:
2020-11-10 19:34:22 +00:00
self.mainmenudisplay.refresh(0, 0, self.rows, self.cols)
if self.game.state == GameMode.SETTINGS:
2020-11-11 19:36:43 +00:00
self.settingsmenudisplay.refresh(0, 0, self.rows, self.cols - 1)
self.resize_window()
def resize_window(self) -> bool:
"""
If the window got resized, ensure that the screen size got updated.
"""
y, x = self.screen.getmaxyx() if self.screen else (0, 0)
if self.screen and curses.is_term_resized(self.rows,
self.cols): # pragma: nocover
curses.resizeterm(y, x)
return True
return False
@property
def rows(self) -> int:
return curses.LINES if self.screen else 42
@property
def cols(self) -> int:
2020-11-10 19:34:22 +00:00
return curses.COLS if self.screen else 42