Use custom function to render a string message onto a pad

This commit is contained in:
Yohann D'ANELLO 2020-11-26 12:35:52 +01:00
parent b6f5fe9364
commit 8b187ec2b3
5 changed files with 27 additions and 23 deletions

View File

@ -19,6 +19,9 @@ class Display:
def newpad(self, height: int, width: int) -> Union[FakePad, Any]: def newpad(self, height: int, width: int) -> Union[FakePad, Any]:
return curses.newpad(height, width) if self.screen else FakePad() return curses.newpad(height, width) if self.screen else FakePad()
def addstr(self, pad: Any, y: int, x: int, msg: str, *options) -> None:
return pad.addstr(y, x, msg, *options)
def init_pair(self, number: int, foreground: int, background: int) -> None: def init_pair(self, number: int, foreground: int, background: int) -> None:
return curses.init_pair(number, foreground, background) \ return curses.init_pair(number, foreground, background) \
if self.screen else None if self.screen else None
@ -68,7 +71,7 @@ class VerticalSplit(Display):
def display(self) -> None: def display(self) -> None:
for i in range(self.height): for i in range(self.height):
self.pad.addstr(i, 0, "") self.addstr(self.pad, i, 0, "")
self.pad.refresh(0, 0, self.y, self.x, self.y + self.height - 1, self.x) self.pad.refresh(0, 0, self.y, self.x, self.y + self.height - 1, self.x)
@ -88,7 +91,7 @@ class HorizontalSplit(Display):
def display(self) -> None: def display(self) -> None:
for i in range(self.width): for i in range(self.width):
self.pad.addstr(0, i, "") self.addstr(self.pad, 0, i, "")
self.pad.refresh(0, 0, self.y, self.x, self.y, self.x + self.width - 1) self.pad.refresh(0, 0, self.y, self.x, self.y, self.x + self.width - 1)
@ -99,10 +102,11 @@ class Box(Display):
self.pad = self.newpad(self.rows, self.cols) self.pad = self.newpad(self.rows, self.cols)
def display(self) -> None: def display(self) -> None:
self.pad.addstr(0, 0, "" + "" * (self.width - 2) + "") self.addstr(self.pad, 0, 0, "" + "" * (self.width - 2) + "")
for i in range(1, self.height - 1): for i in range(1, self.height - 1):
self.pad.addstr(i, 0, "") self.addstr(self.pad, i, 0, "")
self.pad.addstr(i, self.width - 1, "") self.addstr(self.pad, i, self.width - 1, "")
self.pad.addstr(self.height - 1, 0, "" + "" * (self.width - 2) + "") 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.pad.refresh(0, 0, self.y, self.x, self.y + self.height - 1,
self.x + self.width - 1) self.x + self.width - 1)

View File

@ -17,7 +17,7 @@ class LogsDisplay(Display):
messages = messages[::-1] messages = messages[::-1]
self.pad.clear() self.pad.clear()
for i in range(min(self.height, len(messages))): for i in range(min(self.height, len(messages))):
self.pad.addstr(self.height - i - 1, self.x, self.addstr(self.pad, self.height - i - 1, self.x,
messages[i][:self.width]) messages[i][:self.width])
self.pad.refresh(0, 0, self.y, self.x, self.y + self.height - 1, self.pad.refresh(0, 0, self.y, self.x, self.y + self.height - 1,
self.x + self.width - 1) self.x + self.width - 1)

View File

@ -15,11 +15,11 @@ class MapDisplay(Display):
def update_pad(self) -> None: def update_pad(self) -> None:
self.init_pair(1, self.pack.tile_fg_color, self.pack.tile_bg_color) self.init_pair(1, self.pack.tile_fg_color, self.pack.tile_bg_color)
self.init_pair(2, self.pack.entity_fg_color, self.pack.entity_bg_color) self.init_pair(2, self.pack.entity_fg_color, self.pack.entity_bg_color)
self.pad.addstr(0, 0, self.map.draw_string(self.pack), self.addstr(self.pad, 0, 0, self.map.draw_string(self.pack),
self.color_pair(1)) self.color_pair(1))
for e in self.map.entities: for e in self.map.entities:
self.pad.addstr(e.y, self.pack.tile_width * e.x, self.addstr(self.pad, e.y, self.pack.tile_width * e.x,
self.pack[e.name.upper()], self.color_pair(2)) self.pack[e.name.upper()], self.color_pair(2))
def display(self) -> None: def display(self) -> None:
y, x = self.map.currenty, self.pack.tile_width * self.map.currentx y, x = self.map.currenty, self.pack.tile_width * self.map.currentx

View File

@ -20,13 +20,13 @@ class MenuDisplay(Display):
# Menu values are printed in pad # Menu values are printed in pad
self.pad = self.newpad(self.trueheight, self.truewidth + 2) self.pad = self.newpad(self.trueheight, self.truewidth + 2)
for i in range(self.trueheight): for i in range(self.trueheight):
self.pad.addstr(i, 0, " " + self.values[i]) self.addstr(self.pad, i, 0, " " + self.values[i])
def update_pad(self) -> None: def update_pad(self) -> None:
for i in range(self.trueheight): for i in range(self.trueheight):
self.pad.addstr(i, 0, " " + self.values[i]) self.addstr(self.pad, i, 0, " " + self.values[i])
# set a marker on the selected line # set a marker on the selected line
self.pad.addstr(self.menu.position, 0, ">") self.addstr(self.pad, self.menu.position, 0, ">")
def display(self) -> None: def display(self) -> None:
cornery = 0 if self.height - 2 >= self.menu.position - 1 \ cornery = 0 if self.height - 2 >= self.menu.position - 1 \
@ -79,8 +79,8 @@ class MainMenuDisplay(Display):
def display(self) -> None: def display(self) -> None:
for i in range(len(self.title)): for i in range(len(self.title)):
self.pad.addstr(4 + i, max(self.width // 2 self.addstr(self.pad, 4 + i, max(self.width // 2
- len(self.title[0]) // 2 - 1, 0), self.title[i]) - 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.pad.refresh(0, 0, self.y, self.x, self.height + self.y - 1,
self.width + self.x - 1) self.width + self.x - 1)
menuwidth = min(self.menudisplay.preferred_width, self.width) menuwidth = min(self.menudisplay.preferred_width, self.width)

View File

@ -21,21 +21,21 @@ class StatsDisplay(Display):
.format(self.player.level, self.player.current_xp, .format(self.player.level, self.player.current_xp,
self.player.max_xp, self.player.health, self.player.max_xp, self.player.health,
self.player.maxhealth) self.player.maxhealth)
self.pad.addstr(0, 0, string2) self.addstr(self.pad, 0, 0, string2)
string3 = "STR {}\nINT {}\nCHR {}\nDEX {}\nCON {}"\ string3 = "STR {}\nINT {}\nCHR {}\nDEX {}\nCON {}"\
.format(self.player.strength, .format(self.player.strength,
self.player.intelligence, self.player.charisma, self.player.intelligence, self.player.charisma,
self.player.dexterity, self.player.constitution) self.player.dexterity, self.player.constitution)
self.pad.addstr(3, 0, string3) self.addstr(self.pad, 3, 0, string3)
inventory_str = "Inventaire : " + "".join( inventory_str = "Inventaire : " + "".join(
self.pack[item.name.upper()] for item in self.player.inventory) self.pack[item.name.upper()] for item in self.player.inventory)
self.pad.addstr(8, 0, inventory_str) self.addstr(self.pad, 8, 0, inventory_str)
if self.player.dead: if self.player.dead:
self.pad.addstr(10, 0, "VOUS ÊTES MORT", self.addstr(self.pad, 10, 0, "VOUS ÊTES MORT",
curses.A_BOLD | curses.A_BLINK | curses.A_STANDOUT curses.A_BOLD | curses.A_BLINK | curses.A_STANDOUT
| self.color_pair(3)) | self.color_pair(3))
def display(self) -> None: def display(self) -> None:
self.pad.clear() self.pad.clear()