|
| 1 | +from typing import Optional |
| 2 | +from PIL import Image, ImageDraw |
| 3 | +from renderer.base import LayerBase |
| 4 | +from renderer.render import Renderer |
| 5 | +from renderer.const import COLORS_NORMAL, RELATION_NORMAL_STR |
| 6 | +from renderer.utils import replace_color |
| 7 | +from renderer.data import ReplayData |
| 8 | +from functools import lru_cache |
| 9 | + |
| 10 | + |
| 11 | +class LayerCapture(LayerBase): |
| 12 | + """A class for handling/drawing capture points. |
| 13 | +
|
| 14 | + Args: |
| 15 | + LayerBase (_type_): _description_ |
| 16 | + """ |
| 17 | + |
| 18 | + def __init__( |
| 19 | + self, renderer: Renderer, replay_data: Optional[ReplayData] = None |
| 20 | + ): |
| 21 | + """Initiates this class. |
| 22 | +
|
| 23 | + Args: |
| 24 | + renderer (Renderer): The renderer. |
| 25 | + """ |
| 26 | + self._renderer = renderer |
| 27 | + self._replay_data = ( |
| 28 | + replay_data if replay_data else self._renderer.replay_data |
| 29 | + ) |
| 30 | + self._owner = self._replay_data.player_info[self._replay_data.owner_id] |
| 31 | + self._generated_caps: dict[ |
| 32 | + int, tuple[Image.Image, tuple[int, int], int] |
| 33 | + ] = {} |
| 34 | + |
| 35 | + def draw(self, game_time: int, image: Image.Image): |
| 36 | + """Draws the capture area on the minimap image. |
| 37 | +
|
| 38 | + Args: |
| 39 | + game_time (int): Game time. Used to sync. events. |
| 40 | + image (Image.Image): Image where the capture are will be pasted on. |
| 41 | + """ |
| 42 | + events = self._replay_data.events |
| 43 | + cps = events[game_time].evt_control.values() |
| 44 | + |
| 45 | + for count, cp in enumerate(cps): |
| 46 | + if not cp.is_visible: |
| 47 | + continue |
| 48 | + |
| 49 | + if count in self._generated_caps: |
| 50 | + cap_image, cap_pos, cap_hash = self._generated_caps[count] |
| 51 | + if hash(cp) == cap_hash: |
| 52 | + image.alpha_composite(cap_image, cap_pos) |
| 53 | + continue |
| 54 | + |
| 55 | + x, y = self._renderer.get_scaled(cp.position) |
| 56 | + radius = self._renderer.get_scaled_r(cp.radius) |
| 57 | + w = h = round(radius * 2) |
| 58 | + cp_area = self._get_capture_area(cp.relation, (w, h)) |
| 59 | + |
| 60 | + if cp.control_point_type == 5: |
| 61 | + icon_name = "flag.png" |
| 62 | + else: |
| 63 | + icon_name = f"lettered_{count}.png" |
| 64 | + |
| 65 | + relation_str = RELATION_NORMAL_STR[cp.relation] |
| 66 | + icon = self._renderer.resman.load_image( |
| 67 | + icon_name, path=f"cap_icons.{relation_str}" |
| 68 | + ) |
| 69 | + |
| 70 | + if cp.has_invaders and cp.invader_team != -1: |
| 71 | + if cp.invader_team == self._owner.team_id: |
| 72 | + from_color = COLORS_NORMAL[cp.relation] |
| 73 | + to_color = COLORS_NORMAL[0] |
| 74 | + else: |
| 75 | + from_color = COLORS_NORMAL[cp.relation] |
| 76 | + to_color = COLORS_NORMAL[1] |
| 77 | + progress = self._get_progress( |
| 78 | + from_color, to_color, cp.progress |
| 79 | + ) |
| 80 | + else: |
| 81 | + normal = self._renderer.resman.load_image("cap_normal.png") |
| 82 | + from_color = "#000000" |
| 83 | + to_color = COLORS_NORMAL[cp.relation] |
| 84 | + progress = replace_color(normal, from_color, to_color) |
| 85 | + |
| 86 | + px = round(cp_area.width / 2 - progress.width / 2) + 1 |
| 87 | + py = round(cp_area.height / 2 - progress.height / 2) + 1 |
| 88 | + |
| 89 | + cp_area.alpha_composite(progress, (px, py)) |
| 90 | + |
| 91 | + ix = round(cp_area.width / 2 - icon.width / 2) + 1 |
| 92 | + iy = round(cp_area.height / 2 - icon.height / 2) + 1 |
| 93 | + |
| 94 | + cp_area.alpha_composite(icon, (ix, iy)) |
| 95 | + |
| 96 | + cx = round(x - cp_area.width / 2) |
| 97 | + cy = round(y - cp_area.height / 2) |
| 98 | + |
| 99 | + self._generated_caps[count] = ( |
| 100 | + cp_area.copy(), |
| 101 | + (cx, cy), |
| 102 | + hash(cp) |
| 103 | + ) |
| 104 | + |
| 105 | + image.alpha_composite(cp_area, (cx, cy)) |
| 106 | + |
| 107 | + def _get_capture_area(self, relation: int, size: tuple) -> Image.Image: |
| 108 | + """Loads the proper capture area image from the resources. |
| 109 | +
|
| 110 | + Args: |
| 111 | + relation (int): relation |
| 112 | + size (tuple): size of the image. |
| 113 | +
|
| 114 | + Returns: |
| 115 | + Image.Image: Image of the capture area, resized. |
| 116 | + """ |
| 117 | + relation_to_str = {-1: "neutral", 0: "ally", 1: "enemy"} |
| 118 | + filename = f"cap_{relation_to_str[relation]}.png" |
| 119 | + return self._renderer.resman.load_image(filename, size=size) |
| 120 | + |
| 121 | + @lru_cache |
| 122 | + def _get_progress(self, from_color: str, to_color: str, percent: float): |
| 123 | + """Gets the diamond progress `bar` from the resources and properly |
| 124 | + color it depending from the colors and percentage provided. |
| 125 | +
|
| 126 | + Args: |
| 127 | + from_color (str): From color. |
| 128 | + to_color (str): To color. |
| 129 | + percent (float): Percentage of the progress. 0.0 to 1.0 |
| 130 | +
|
| 131 | + Returns: |
| 132 | + Image.Image: Diamond progress `bar` image. |
| 133 | + """ |
| 134 | + pd = self._renderer.resman.load_image("cap_invaded.png") |
| 135 | + |
| 136 | + bg_diamond = replace_color(pd, "#000000", from_color) |
| 137 | + fg_diamond = replace_color(pd, "#000000", to_color) |
| 138 | + mask = Image.new("RGBA", pd.size) |
| 139 | + mask_draw = ImageDraw.Draw(mask, "RGBA") |
| 140 | + mask_draw.pieslice( |
| 141 | + ( |
| 142 | + (0, 0), |
| 143 | + (pd.width - 1, pd.height - 1), |
| 144 | + ), |
| 145 | + start=-90, |
| 146 | + end=(-90 + 360 * percent), |
| 147 | + fill="black", |
| 148 | + ) |
| 149 | + bg_diamond.paste(fg_diamond, mask=mask) |
| 150 | + return bg_diamond |
0 commit comments