diff --git a/squirrelbattle/display/display.py b/squirrelbattle/display/display.py index d5e3078..1d70159 100644 --- a/squirrelbattle/display/display.py +++ b/squirrelbattle/display/display.py @@ -50,7 +50,8 @@ class Display: self.y = y self.width = width self.height = height - if hasattr(self, "pad") and resize_pad: + if hasattr(self, "pad") and resize_pad and \ + self.height >= 0 and self.width >= 0: self.pad.resize(self.height + 1, self.width + 1) def refresh(self, *args, resize_pad: bool = True) -> None: @@ -58,6 +59,26 @@ class Display: self.resize(*args, resize_pad) self.display() + def refresh_pad(self, pad: Any, top_y: int, top_x: int, + window_y: int, window_x: int, + last_y: int, last_x: int) -> None: + """ + Refresh a pad on a part of the window. + The refresh starts at coordinates (top_y, top_x) from the pad, + and is drawn from (window_y, window_x) to (last_y, last_x). + If coordinates are invalid (negative indexes/length..., then nothing + is drawn and no error is raised. + """ + top_y, top_x = max(0, top_y), max(0, top_x) + window_y, window_x = max(0, window_y), max(0, window_x) + screen_max_y, screen_max_x = self.screen.getmaxyx() + last_y, last_x = min(screen_max_y - 1, last_y), \ + min(screen_max_x - 1, last_x) + + if last_y >= window_y and last_x >= window_x: + # Refresh the pad only if coordinates are valid + pad.refresh(top_y, top_x, window_y, window_x, last_y, last_x) + def display(self) -> None: raise NotImplementedError @@ -87,7 +108,8 @@ class VerticalSplit(Display): def display(self) -> None: for i in range(self.height): self.addstr(self.pad, i, 0, "┃") - self.pad.refresh(0, 0, self.y, self.x, self.y + self.height - 1, self.x) + self.refresh_pad(self.pad, 0, 0, self.y, self.x, + self.y + self.height - 1, self.x) class HorizontalSplit(Display): @@ -107,7 +129,8 @@ class HorizontalSplit(Display): def display(self) -> None: for i in range(self.width): self.addstr(self.pad, 0, i, "━") - self.pad.refresh(0, 0, self.y, self.x, self.y, self.x + self.width - 1) + self.refresh_pad(self.pad, 0, 0, self.y, self.x, self.y, + self.x + self.width - 1) class Box(Display): @@ -123,5 +146,5 @@ class Box(Display): self.addstr(self.pad, i, self.width - 1, "┃") self.addstr(self.pad, self.height - 1, 0, "┗" + "━" * (self.width - 2) + "┛") - self.pad.refresh(0, 0, self.y, self.x, self.y + self.height - 1, - self.x + self.width - 1) + self.refresh_pad(self.pad, 0, 0, self.y, self.x, + self.y + self.height - 1, self.x + self.width - 1) diff --git a/squirrelbattle/display/logsdisplay.py b/squirrelbattle/display/logsdisplay.py index 304cf7b..d332b69 100644 --- a/squirrelbattle/display/logsdisplay.py +++ b/squirrelbattle/display/logsdisplay.py @@ -19,5 +19,5 @@ class LogsDisplay(Display): for i in range(min(self.height, len(messages))): self.addstr(self.pad, self.height - i - 1, self.x, messages[i][:self.width]) - self.pad.refresh(0, 0, self.y, self.x, self.y + self.height - 1, - self.x + self.width - 1) + self.refresh_pad(self.pad, 0, 0, self.y, self.x, + self.y + self.height - 1, self.x + self.width - 1) diff --git a/squirrelbattle/display/mapdisplay.py b/squirrelbattle/display/mapdisplay.py index 571c527..ce134a5 100644 --- a/squirrelbattle/display/mapdisplay.py +++ b/squirrelbattle/display/mapdisplay.py @@ -36,4 +36,5 @@ class MapDisplay(Display): pmincol = max(0, min(self.pack.tile_width * self.map.width, pmincol)) self.pad.clear() self.update_pad() - self.pad.refresh(pminrow, pmincol, sminrow, smincol, smaxrow, smaxcol) + self.refresh_pad(self.pad, pminrow, pmincol, sminrow, smincol, smaxrow, + smaxcol) diff --git a/squirrelbattle/display/menudisplay.py b/squirrelbattle/display/menudisplay.py index 8d03017..2338e0d 100644 --- a/squirrelbattle/display/menudisplay.py +++ b/squirrelbattle/display/menudisplay.py @@ -37,7 +37,7 @@ class MenuDisplay(Display): self.menubox.refresh(self.y, self.x, self.height, self.width) self.pad.clear() self.update_pad() - self.pad.refresh(cornery, 0, self.y + 1, self.x + 2, + self.refresh_pad(self.pad, cornery, 0, self.y + 1, self.x + 2, self.height - 2 + self.y, self.width - 2 + self.x) @@ -81,7 +81,8 @@ class MainMenuDisplay(Display): for i in range(len(self.title)): self.addstr(self.pad, 4 + i, max(self.width // 2 - len(self.title[0]) // 2 - 1, 0), self.title[i]) - self.pad.refresh(0, 0, self.y, self.x, self.height + self.y - 1, + self.refresh_pad(self.pad, 0, 0, self.y, self.x, + self.height + self.y - 1, self.width + self.x - 1) menuwidth = min(self.menudisplay.preferred_width, self.width) menuy, menux = len(self.title) + 8, self.width // 2 - menuwidth // 2 - 1 diff --git a/squirrelbattle/display/statsdisplay.py b/squirrelbattle/display/statsdisplay.py index bd20ede..988d99c 100644 --- a/squirrelbattle/display/statsdisplay.py +++ b/squirrelbattle/display/statsdisplay.py @@ -40,5 +40,5 @@ class StatsDisplay(Display): def display(self) -> None: self.pad.clear() self.update_pad() - self.pad.refresh(0, 0, self.y, self.x, + self.refresh_pad(self.pad, 0, 0, self.y, self.x, self.y + self.height - 1, self.width + self.x - 1)