Source code for tuxemon.menu.interface

# SPDX-License-Identifier: GPL-3.0
# Copyright (c) 2014-2023 William Edwards <shadowapex@gmail.com>, Benjamin Bean <superman2k5@gmail.com>
from __future__ import annotations

from typing import ClassVar, Generic, Optional, TypeVar

import pygame

from tuxemon import graphics, tools
from tuxemon.sprite import Sprite
from tuxemon.ui.draw import GraphicBox


[docs]class Bar: """Common bar class for UI elements.""" border_filename: ClassVar[str] border = None # type: ClassVar[GraphicBox] fg_color: ClassVar[graphics.ColorLike] = (255, 255, 255) bg_color: ClassVar[Optional[graphics.ColorLike]] = (0, 0, 0) def __init__(self, value: float = 1.0) -> None: if self.border is None: self.load_graphics() self.value = value
[docs] def load_graphics(self) -> None: """ Load the border image. Image become class attribute, so is shared. Eventually, implement some game-wide image caching. """ image = graphics.load_and_scale(self.border_filename) type(self).border = GraphicBox(image)
[docs] @staticmethod def calc_inner_rect(rect: pygame.rect.Rect) -> pygame.rect.Rect: """ Calculate inner rectangle. Calculate the inner rect to draw fg_color that fills bar The values here are calculated based on game scale and the content of the border image file. Parameters: rect: Outside rectangle. Returns: Inner rectangle. """ inner = rect.copy() inner.top += tools.scale(2) inner.height -= tools.scale(4) inner.left += tools.scale(9) inner.width -= tools.scale(11) return inner
[docs] def draw( self, surface: pygame.surface.Surface, rect: pygame.rect.Rect, ) -> None: """ Draws the bar. Parameters: surface: Surface where to draw the bar. rect: Location and size of the bar. """ inner = self.calc_inner_rect(rect) if self.bg_color is not None: pygame.draw.rect(surface, self.bg_color, inner) if self.value > 0: inner.width = int(inner.width * self.value) pygame.draw.rect(surface, self.fg_color, inner) self.border.draw(surface, rect)
[docs]class HpBar(Bar): """HP bar for UI elements.""" border_filename = "gfx/ui/monster/hp_bar.png" border = None # type: ClassVar[GraphicBox] fg_color = (10, 240, 25) bg_color = (245, 10, 25)
[docs]class ExpBar(Bar): """EXP bar for UI elements.""" border_filename = "gfx/ui/monster/exp_bar.png" border = None # type: ClassVar[GraphicBox] fg_color = (31, 239, 255) bg_color = None
T = TypeVar("T", covariant=True)