2020-11-27 15:33:17 +00:00
|
|
|
# Copyright (C) 2020 by ÿnérant, eichhornchen, nicomarg, charlse
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2020-11-10 17:08:06 +00:00
|
|
|
import curses
|
2020-11-20 17:07:09 +00:00
|
|
|
from squirrelbattle.display.display import VerticalSplit, HorizontalSplit
|
2020-11-19 01:18:08 +00:00
|
|
|
from squirrelbattle.display.mapdisplay import MapDisplay
|
2020-11-27 16:32:26 +00:00
|
|
|
from squirrelbattle.display.messagedisplay import MessageDisplay
|
2020-11-19 01:18:08 +00:00
|
|
|
from squirrelbattle.display.statsdisplay import StatsDisplay
|
2020-12-06 10:43:48 +00:00
|
|
|
from squirrelbattle.display.menudisplay import MainMenuDisplay, \
|
2020-12-07 19:54:53 +00:00
|
|
|
PlayerInventoryDisplay, StoreInventoryDisplay, SettingsMenuDisplay
|
2020-11-19 11:03:05 +00:00
|
|
|
from squirrelbattle.display.logsdisplay import LogsDisplay
|
2020-11-19 01:18:08 +00:00
|
|
|
from squirrelbattle.display.texturepack import TexturePack
|
2020-11-10 17:08:06 +00:00
|
|
|
from typing import Any
|
2020-11-19 01:18:08 +00:00
|
|
|
from squirrelbattle.game import Game, GameMode
|
|
|
|
from squirrelbattle.enums import DisplayActions
|
2020-11-10 17:08:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DisplayManager:
|
2020-11-10 19:34:22 +00:00
|
|
|
|
2020-11-10 17:08:06 +00:00
|
|
|
def __init__(self, screen: Any, g: Game):
|
|
|
|
self.game = g
|
|
|
|
self.screen = screen
|
2020-11-10 18:40:59 +00:00
|
|
|
pack = TexturePack.get_pack(self.game.settings.TEXTURE_PACK)
|
|
|
|
self.mapdisplay = MapDisplay(screen, pack)
|
|
|
|
self.statsdisplay = StatsDisplay(screen, pack)
|
2020-12-06 10:43:48 +00:00
|
|
|
self.logsdisplay = LogsDisplay(screen, pack)
|
2020-12-07 19:54:53 +00:00
|
|
|
self.playerinventorydisplay = PlayerInventoryDisplay(screen, pack)
|
|
|
|
self.storeinventorydisplay = StoreInventoryDisplay(screen, pack)
|
2020-11-10 19:34:22 +00:00
|
|
|
self.mainmenudisplay = MainMenuDisplay(self.game.main_menu,
|
|
|
|
screen, pack)
|
2020-11-13 14:40:44 +00:00
|
|
|
self.settingsmenudisplay = SettingsMenuDisplay(screen, pack)
|
2020-11-27 16:52:26 +00:00
|
|
|
self.messagedisplay = MessageDisplay(screen=screen, pack=None)
|
2020-11-20 17:07:09 +00:00
|
|
|
self.hbar = HorizontalSplit(screen, pack)
|
|
|
|
self.vbar = VerticalSplit(screen, pack)
|
2020-11-10 19:34:22 +00:00
|
|
|
self.displays = [self.statsdisplay, self.mapdisplay,
|
2020-11-19 11:03:05 +00:00
|
|
|
self.mainmenudisplay, self.settingsmenudisplay,
|
2020-11-27 16:32:26 +00:00
|
|
|
self.logsdisplay, self.messagedisplay]
|
2020-11-10 17:08:06 +00:00
|
|
|
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:
|
2020-11-11 22:56:08 +00:00
|
|
|
self.refresh()
|
2020-11-11 23:12:30 +00:00
|
|
|
elif action == DisplayActions.UPDATE:
|
2020-11-11 22:56:08 +00:00
|
|
|
self.update_game_components()
|
|
|
|
|
2020-11-10 19:34:22 +00:00
|
|
|
def update_game_components(self) -> None:
|
2020-11-10 17:08:06 +00:00
|
|
|
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-12-07 19:54:53 +00:00
|
|
|
self.game.inventory_menu.update_player(self.game.player)
|
|
|
|
self.game.store_menu.update_merchant(self.game.player)
|
|
|
|
self.playerinventorydisplay.update_menu(self.game.inventory_menu)
|
|
|
|
self.storeinventorydisplay.update_menu(self.game.store_menu)
|
2020-11-11 23:12:30 +00:00
|
|
|
self.settingsmenudisplay.update_menu(self.game.settings_menu)
|
2020-11-19 11:03:05 +00:00
|
|
|
self.logsdisplay.update_logs(self.game.logs)
|
2020-11-27 16:32:26 +00:00
|
|
|
self.messagedisplay.update_message(self.game.message)
|
2020-11-10 17:08:06 +00:00
|
|
|
|
|
|
|
def refresh(self) -> None:
|
2020-12-06 10:43:48 +00:00
|
|
|
if self.game.state == GameMode.PLAY \
|
2020-12-07 19:54:53 +00:00
|
|
|
or self.game.state == GameMode.INVENTORY \
|
2020-12-07 20:13:55 +00:00
|
|
|
or self.game.state == GameMode.STORE:
|
2020-11-11 21:12:05 +00:00
|
|
|
# The map pad has already the good size
|
2020-11-20 17:07:09 +00:00
|
|
|
self.mapdisplay.refresh(0, 0, self.rows * 4 // 5,
|
2020-11-26 20:59:48 +00:00
|
|
|
self.mapdisplay.pack.tile_width
|
|
|
|
* (self.cols * 4 // 5
|
|
|
|
// self.mapdisplay.pack.tile_width),
|
|
|
|
resize_pad=False)
|
2020-11-20 17:07:09 +00:00
|
|
|
self.statsdisplay.refresh(0, self.cols * 4 // 5 + 1,
|
|
|
|
self.rows, self.cols // 5 - 1)
|
|
|
|
self.logsdisplay.refresh(self.rows * 4 // 5 + 1, 0,
|
|
|
|
self.rows // 5 - 1, self.cols * 4 // 5)
|
|
|
|
self.hbar.refresh(self.rows * 4 // 5, 0, 1, self.cols * 4 // 5)
|
|
|
|
self.vbar.refresh(0, self.cols * 4 // 5, self.rows, 1)
|
2020-12-06 10:43:48 +00:00
|
|
|
if self.game.state == GameMode.INVENTORY:
|
2020-12-07 20:22:06 +00:00
|
|
|
self.playerinventorydisplay.refresh(
|
|
|
|
self.rows // 10, self.cols // 2,
|
|
|
|
8 * self.rows // 10, 2 * self.cols // 5)
|
2020-12-07 19:54:53 +00:00
|
|
|
elif self.game.state == GameMode.STORE:
|
2020-12-07 20:22:06 +00:00
|
|
|
self.storeinventorydisplay.refresh(
|
|
|
|
self.rows // 10, self.cols // 2,
|
|
|
|
8 * self.rows // 10, 2 * self.cols // 5)
|
2020-12-06 10:43:48 +00:00
|
|
|
elif self.game.state == GameMode.MAINMENU:
|
2020-11-10 19:34:22 +00:00
|
|
|
self.mainmenudisplay.refresh(0, 0, self.rows, self.cols)
|
2020-12-06 10:43:48 +00:00
|
|
|
elif self.game.state == GameMode.SETTINGS:
|
|
|
|
self.settingsmenudisplay.refresh(0, 0, self.rows, self.cols)
|
2020-11-27 16:32:26 +00:00
|
|
|
|
|
|
|
if self.game.message:
|
|
|
|
height, width = 0, 0
|
|
|
|
for line in self.game.message.split("\n"):
|
|
|
|
height += 1
|
|
|
|
width = max(width, len(line))
|
|
|
|
y, x = (self.rows - height) // 2, (self.cols - width) // 2
|
|
|
|
self.messagedisplay.refresh(y, x, height, width)
|
|
|
|
|
2020-11-10 20:20:11 +00:00
|
|
|
self.resize_window()
|
2020-11-10 17:08:06 +00:00
|
|
|
|
2020-11-10 20:20:11 +00:00
|
|
|
def resize_window(self) -> bool:
|
2020-11-10 17:08:06 +00:00
|
|
|
"""
|
2020-11-10 20:20:11 +00:00
|
|
|
If the window got resized, ensure that the screen size got updated.
|
2020-11-10 17:08:06 +00:00
|
|
|
"""
|
|
|
|
y, x = self.screen.getmaxyx() if self.screen else (0, 0)
|
2020-11-10 20:32:42 +00:00
|
|
|
if self.screen and curses.is_term_resized(self.rows,
|
|
|
|
self.cols): # pragma: nocover
|
2020-11-10 17:08:06 +00:00
|
|
|
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
|