2021-01-10 09:46:17 +00:00
|
|
|
# Copyright (C) 2020-2021 by ÿnérant, eichhornchen, nicomarg, charlse
|
2020-11-27 15:33:17 +00:00
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2020-11-10 17:08:06 +00:00
|
|
|
import curses
|
2020-12-11 15:56:22 +00:00
|
|
|
from typing import Any, List
|
2021-01-10 10:25:53 +00:00
|
|
|
|
2021-01-10 11:35:50 +00:00
|
|
|
from .display import Display, HorizontalSplit, MessageDisplay, VerticalSplit
|
|
|
|
from .gamedisplay import LogsDisplay, MapDisplay, StatsDisplay
|
|
|
|
from .menudisplay import ChestInventoryDisplay, CreditsDisplay, \
|
|
|
|
MainMenuDisplay, PlayerInventoryDisplay, \
|
|
|
|
SettingsMenuDisplay, StoreInventoryDisplay
|
2021-01-10 10:25:53 +00:00
|
|
|
from .texturepack import TexturePack
|
|
|
|
from ..enums import DisplayActions
|
|
|
|
from ..game import Game, GameMode
|
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-04 13:41:59 +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)
|
2021-01-08 22:15:48 +00:00
|
|
|
self.chestinventorydisplay = ChestInventoryDisplay(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-12-18 21:24:41 +00:00
|
|
|
self.messagedisplay = MessageDisplay(screen, pack)
|
2020-11-20 17:07:09 +00:00
|
|
|
self.hbar = HorizontalSplit(screen, pack)
|
|
|
|
self.vbar = VerticalSplit(screen, pack)
|
2020-12-18 21:24:41 +00:00
|
|
|
self.creditsdisplay = CreditsDisplay(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-12-07 20:51:51 +00:00
|
|
|
self.logsdisplay, self.messagedisplay,
|
|
|
|
self.playerinventorydisplay,
|
2021-01-08 22:15:48 +00:00
|
|
|
self.storeinventorydisplay, self.creditsdisplay,
|
|
|
|
self.chestinventorydisplay]
|
2020-11-10 17:08:06 +00:00
|
|
|
self.update_game_components()
|
|
|
|
|
2020-12-11 15:56:22 +00:00
|
|
|
def handle_display_action(self, action: DisplayActions, *params) -> None:
|
2020-12-13 20:29:25 +00:00
|
|
|
"""
|
|
|
|
Handles the differents values of display action.
|
|
|
|
"""
|
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-12-11 15:56:22 +00:00
|
|
|
elif action == DisplayActions.MOUSE:
|
|
|
|
self.handle_mouse_click(*params)
|
2020-11-11 22:56:08 +00:00
|
|
|
|
2020-11-10 19:34:22 +00:00
|
|
|
def update_game_components(self) -> None:
|
2020-12-13 20:29:25 +00:00
|
|
|
"""
|
2020-12-18 14:07:09 +00:00
|
|
|
The game state was updated.
|
|
|
|
Trigger all displays of these modifications.
|
2020-12-13 20:29:25 +00:00
|
|
|
"""
|
2020-11-10 17:08:06 +00:00
|
|
|
for d in self.displays:
|
|
|
|
d.pack = TexturePack.get_pack(self.game.settings.TEXTURE_PACK)
|
2020-12-18 14:07:09 +00:00
|
|
|
d.update(self.game)
|
2020-11-10 17:08:06 +00:00
|
|
|
|
2021-01-08 10:21:40 +00:00
|
|
|
def handle_mouse_click(self, y: int, x: int, attr: int) -> None:
|
2020-12-13 20:29:25 +00:00
|
|
|
"""
|
|
|
|
Handles the mouse clicks.
|
|
|
|
"""
|
2020-12-11 15:56:22 +00:00
|
|
|
displays = self.refresh()
|
|
|
|
display = None
|
|
|
|
for d in displays:
|
|
|
|
top_y, top_x, height, width = d.y, d.x, d.height, d.width
|
|
|
|
if top_y <= y < top_y + height and top_x <= x < top_x + width:
|
|
|
|
# The click coordinates correspond to the coordinates
|
|
|
|
# of that display
|
|
|
|
display = d
|
2020-12-11 16:40:56 +00:00
|
|
|
if display:
|
2021-01-08 10:21:40 +00:00
|
|
|
display.handle_click(y - display.y, x - display.x, attr, self.game)
|
2020-12-11 15:56:22 +00:00
|
|
|
|
|
|
|
def refresh(self) -> List[Display]:
|
2020-12-13 20:29:25 +00:00
|
|
|
"""
|
|
|
|
Refreshes all components on the screen.
|
|
|
|
"""
|
2020-12-11 15:56:22 +00:00
|
|
|
displays = []
|
2020-12-18 15:40:52 +00:00
|
|
|
pack = TexturePack.get_pack(self.game.settings.TEXTURE_PACK)
|
2020-12-11 15:56:22 +00:00
|
|
|
|
2020-12-04 13:51:41 +00:00
|
|
|
if self.game.state == GameMode.PLAY \
|
2020-12-07 19:54:53 +00:00
|
|
|
or self.game.state == GameMode.INVENTORY \
|
2021-01-08 22:15:48 +00:00
|
|
|
or self.game.state == GameMode.STORE\
|
|
|
|
or self.game.state == GameMode.CHEST:
|
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-11 15:56:22 +00:00
|
|
|
|
|
|
|
displays += [self.mapdisplay, self.statsdisplay, self.logsdisplay,
|
|
|
|
self.hbar, self.vbar]
|
|
|
|
|
2020-12-04 13:51:41 +00:00
|
|
|
if self.game.state == GameMode.INVENTORY:
|
2020-12-07 20:22:06 +00:00
|
|
|
self.playerinventorydisplay.refresh(
|
2020-12-18 15:40:52 +00:00
|
|
|
self.rows // 10,
|
|
|
|
pack.tile_width * (self.cols // (2 * pack.tile_width)),
|
|
|
|
8 * self.rows // 10,
|
|
|
|
pack.tile_width * (2 * self.cols // (5 * pack.tile_width)))
|
2020-12-11 17:44:05 +00:00
|
|
|
displays.append(self.playerinventorydisplay)
|
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(
|
2020-12-18 15:40:52 +00:00
|
|
|
self.rows // 10,
|
|
|
|
pack.tile_width * (self.cols // (2 * pack.tile_width)),
|
|
|
|
8 * self.rows // 10,
|
|
|
|
pack.tile_width * (2 * self.cols // (5 * pack.tile_width)))
|
2020-12-17 22:46:20 +00:00
|
|
|
self.playerinventorydisplay.refresh(
|
2020-12-18 15:40:52 +00:00
|
|
|
self.rows // 10,
|
|
|
|
pack.tile_width * (self.cols // (10 * pack.tile_width)),
|
|
|
|
8 * self.rows // 10,
|
|
|
|
pack.tile_width * (2 * self.cols // (5 * pack.tile_width)))
|
2020-12-11 17:38:13 +00:00
|
|
|
displays.append(self.storeinventorydisplay)
|
2020-12-17 22:46:20 +00:00
|
|
|
displays.append(self.playerinventorydisplay)
|
2021-01-08 22:15:48 +00:00
|
|
|
elif self.game.state == GameMode.CHEST:
|
|
|
|
self.chestinventorydisplay.refresh(
|
|
|
|
self.rows // 10,
|
|
|
|
pack.tile_width * (self.cols // (2 * pack.tile_width)),
|
|
|
|
8 * self.rows // 10,
|
|
|
|
pack.tile_width * (2 * self.cols // (5 * pack.tile_width)))
|
|
|
|
self.playerinventorydisplay.refresh(
|
|
|
|
self.rows // 10,
|
|
|
|
pack.tile_width * (self.cols // (10 * pack.tile_width)),
|
|
|
|
8 * self.rows // 10,
|
|
|
|
pack.tile_width * (2 * self.cols // (5 * pack.tile_width)))
|
|
|
|
displays.append(self.chestinventorydisplay)
|
|
|
|
displays.append(self.playerinventorydisplay)
|
2020-12-04 13:41:59 +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-11 15:56:22 +00:00
|
|
|
displays.append(self.mainmenudisplay)
|
2020-12-04 13:41:59 +00:00
|
|
|
elif self.game.state == GameMode.SETTINGS:
|
|
|
|
self.settingsmenudisplay.refresh(0, 0, self.rows, self.cols)
|
2020-12-11 15:56:22 +00:00
|
|
|
displays.append(self.settingsmenudisplay)
|
2020-12-18 21:24:41 +00:00
|
|
|
elif self.game.state == GameMode.CREDITS:
|
|
|
|
self.creditsdisplay.refresh(0, 0, self.rows, self.cols)
|
|
|
|
displays.append(self.creditsdisplay)
|
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))
|
2020-12-18 15:40:52 +00:00
|
|
|
y = pack.tile_width * (self.rows - height) // (2 * pack.tile_width)
|
|
|
|
x = pack.tile_width * ((self.cols - width) // (2 * pack.tile_width))
|
2020-11-27 16:32:26 +00:00
|
|
|
self.messagedisplay.refresh(y, x, height, width)
|
2020-12-11 15:56:22 +00:00
|
|
|
displays.append(self.messagedisplay)
|
2020-11-27 16:32:26 +00:00
|
|
|
|
2020-11-10 20:20:11 +00:00
|
|
|
self.resize_window()
|
2020-11-10 17:08:06 +00:00
|
|
|
|
2020-12-11 15:56:22 +00:00
|
|
|
return displays
|
|
|
|
|
2020-11-10 20:20:11 +00:00
|
|
|
def resize_window(self) -> bool:
|
2020-11-10 17:08:06 +00:00
|
|
|
"""
|
2020-12-13 20:29:25 +00:00
|
|
|
When the window is resized, ensures that the screen size is 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:
|
2020-12-13 20:29:25 +00:00
|
|
|
"""
|
|
|
|
Overwrites the native curses attribute of the same name,
|
|
|
|
for testing purposes.
|
|
|
|
"""
|
2020-11-10 17:08:06 +00:00
|
|
|
return curses.LINES if self.screen else 42
|
|
|
|
|
|
|
|
@property
|
|
|
|
def cols(self) -> int:
|
2020-12-13 20:29:25 +00:00
|
|
|
"""
|
|
|
|
Overwrites the native curses attribute of the same name,
|
|
|
|
for testing purposes.
|
|
|
|
"""
|
2020-11-10 19:34:22 +00:00
|
|
|
return curses.COLS if self.screen else 42
|