diff options
| author | troido <troido@hotmail.com> | 2017-11-13 13:37:05 +0100 |
|---|---|---|
| committer | troido <troido@hotmail.com> | 2017-11-13 13:37:05 +0100 |
| commit | 227f266df78b143b0c5dd2c7f498b755199707eb (patch) | |
| tree | 9af5dac95f684e79d04fcc96b03dc81689dff78a | |
| parent | 336f6f873c52ef62e2f304436b381c166eac872f (diff) | |
display parts now use windows as arguments
| -rw-r--r-- | asciifarm/client/display/__init__.py | 25 | ||||
| -rw-r--r-- | asciifarm/client/display/fieldpad.py | 26 | ||||
| -rw-r--r-- | asciifarm/client/display/healthpad.py | 59 | ||||
| -rw-r--r-- | asciifarm/client/display/infopad.py | 37 | ||||
| -rw-r--r-- | asciifarm/client/display/inventorypad.py | 39 | ||||
| -rw-r--r-- | asciifarm/client/display/messagepad.py | 35 | ||||
| -rw-r--r-- | asciifarm/client/display/screen.py | 52 |
7 files changed, 170 insertions, 103 deletions
diff --git a/asciifarm/client/display/__init__.py b/asciifarm/client/display/__init__.py index ea86557..fc48d55 100644 --- a/asciifarm/client/display/__init__.py +++ b/asciifarm/client/display/__init__.py @@ -21,7 +21,7 @@ class Display: self.colours = Colours() else: self.colours = None - self.screen = Screen(stdscr) + self.screen = Screen(self, stdscr) self.fieldPad = FieldPad((1, 1), charMap.get("charwidth", 1), self.colours) self.characters = charMap["mapping"] self.defaultChar = charMap.get("default", "?") @@ -83,17 +83,18 @@ class Display: def update(self): if self.changed: - fieldRight = min(self.fieldPad.getWidth(), self.screen.getWidth()-SIDEWIDTH-1) - fieldBottom = min(self.fieldPad.getHeight(), self.screen.getHeight()-self.messagePad.getHeight()) - healthBottom = self.healthPad.getHeight() - groundBottom = healthBottom + self.groundPad.getHeight() - inventoryBottom = groundBottom + self.inventoryPad.getHeight() - self.fieldPad.update(self, 0,0,fieldRight, fieldBottom) - self.messagePad.update(self, 0,fieldBottom, fieldRight, min(self.screen.getHeight(), fieldBottom+self.messagePad.getHeight())) - self.healthPad.update(self, fieldRight+1,0, self.screen.getWidth(), healthBottom) - self.groundPad.update(self, fieldRight+1, healthBottom, self.screen.getWidth(), min(self.screen.getHeight(), groundBottom)) - self.inventoryPad.update(self, fieldRight+1, groundBottom, self.screen.getWidth(), min(self.screen.getHeight(), inventoryBottom)) - self.infoPad.update(self, fieldRight+1,inventoryBottom+1, self.screen.getWidth(), self.screen.getHeight()) + self.screen.update() + #fieldRight = min(self.fieldPad.getWidth(), self.screen.getWidth()-SIDEWIDTH-1) + #fieldBottom = min(self.fieldPad.getHeight(), self.screen.getHeight()-self.messagePad.getHeight()) + #healthBottom = self.healthPad.getHeight() + #groundBottom = healthBottom + self.groundPad.getHeight() + #inventoryBottom = groundBottom + self.inventoryPad.getHeight() + #self.fieldPad.update(self, 0,0,fieldRight, fieldBottom) + #self.messagePad.update(self, 0,fieldBottom, fieldRight, min(self.screen.getHeight(), fieldBottom+self.messagePad.getHeight())) + #self.healthPad.update(self, fieldRight+1,0, self.screen.getWidth(), healthBottom) + #self.groundPad.update(self, fieldRight+1, healthBottom, self.screen.getWidth(), min(self.screen.getHeight(), groundBottom)) + #self.inventoryPad.update(self, fieldRight+1, groundBottom, self.screen.getWidth(), min(self.screen.getHeight(), inventoryBottom)) + #self.infoPad.update(self, fieldRight+1,inventoryBottom+1, self.screen.getWidth(), self.screen.getHeight()) curses.doupdate() self.changed = False diff --git a/asciifarm/client/display/fieldpad.py b/asciifarm/client/display/fieldpad.py index 9be20e5..1966611 100644 --- a/asciifarm/client/display/fieldpad.py +++ b/asciifarm/client/display/fieldpad.py @@ -13,7 +13,7 @@ class FieldPad: self.center = (0, 0) self.colours = colours self.changed = False - self.lastView = None + #self.lastView = None def resize(self, width, height): self.size = (width, height) @@ -35,22 +35,24 @@ class FieldPad: def getHeight(self): return self.size[1] - def roundWidth(self, x): + def _roundWidth(self, x): return x // self.charSize * self.charSize - def update(self, screen, x, y, xmax, ymax, force=False): - if not self.changed and (x, y, xmax, ymax) == self.lastView or xmax <= x or ymax <= y and not force: + def update(self, win, force=False): + if not self.changed and not force: return - self.lastView = (x, y, xmax, ymax) - self.changed = False - width = xmax-x - height = ymax-y + #self.lastView = (x, y, xmax, ymax) + height, width = win.getmaxyx() + y, x = win.getparyx() + xmax = x + width + ymax = y + height self.pad.noutrefresh( max(0, min(self.getHeight()-height, self.center[1] - int(height/2))), max(0, min( - self.roundWidth(self.getWidth()-width), - self.roundWidth(self.center[0]*self.charSize - int(width/2)))), + self._roundWidth(self.getWidth()-width), + self._roundWidth(self.center[0]*self.charSize - int(width/2)))), y, x, - ymax-1, - xmax-1) + ymax, + xmax) + self.changed = False diff --git a/asciifarm/client/display/healthpad.py b/asciifarm/client/display/healthpad.py index 502559a..2ca0a89 100644 --- a/asciifarm/client/display/healthpad.py +++ b/asciifarm/client/display/healthpad.py @@ -6,10 +6,10 @@ class HealthPad: def __init__(self, width=1, char=('@',7,0), emptyChar=('-',7,0), colours=False): self.char = char self.emptyChar = emptyChar - self.pad = curses.newpad(2, width+1) - self.width = width + #self.pad = curses.newpad(2, width+1) + #self.width = width self.changed = False - self.lastView = None + #self.lastView = None self.colours = colours self.health = 0 self.maxHealth = 0 @@ -19,30 +19,41 @@ class HealthPad: self.maxHealth = maxHealth - self.pad.erase() - barEnd = round(health/maxHealth * self.width) - self.pad.addstr(0,0,"Health: {}/{}".format(health, maxHealth)[:self.width]) - if self.colours: - self.pad.addstr(1,0, self.char[0]*barEnd, self.colours.get(*self.char[1:])) - self.pad.addstr(1,barEnd, self.emptyChar[0]*(self.width-barEnd), self.colours.get(*self.emptyChar[1:])) - else: - self.pad.addstr(1,0, self.char[0]*barEnd) - self.pad.addstr(1,barEnd, self.emptyChar[0]*(self.width-barEnd)) + #self.pad.erase() + #barEnd = round(health/maxHealth * self.width) + #self.pad.addstr(0,0,"Health: {}/{}".format(health, maxHealth)[:self.width]) + #if self.colours: + #self.pad.addstr(1,0, self.char[0]*barEnd, self.colours.get(*self.char[1:])) + #self.pad.addstr(1,barEnd, self.emptyChar[0]*(self.width-barEnd), self.colours.get(*self.emptyChar[1:])) + #else: + #self.pad.addstr(1,0, self.char[0]*barEnd) + #self.pad.addstr(1,barEnd, self.emptyChar[0]*(self.width-barEnd)) self.changed = True - def getHeight(self): - return 2 + #def getHeight(self): + #return 2 - def update(self, screen, x, y, xmax, ymax, force=False): - if not self.changed and (x, y, xmax, ymax) == self.lastView or xmax <= x or ymax <= y and not force: + def update(self, win, force=False): + if not self.changed and not force: return - self.lastView = (x, y, xmax, ymax) + #self.lastView = (x, y, xmax, ymax) self.changed = False - self.pad.noutrefresh( - 0, - 0, - y, - x, - ymax-1, - xmax-1) + height, width = win.getmaxyx() + width -= 1 + barEnd = round(self.health/self.maxHealth * width) + win.erase() + win.addstr(0,0,"Health: {}/{}".format(self.health, self.maxHealth)[:width]) + if self.colours: + win.addstr(1,0, self.char[0]*barEnd, self.colours.get(*self.char[1:])) + win.addstr(1,barEnd, self.emptyChar[0]*(width-barEnd), self.colours.get(*self.emptyChar[1:])) + else: + win.addstr(1,0, self.char[0]*barEnd) + win.addstr(1,barEnd, self.emptyChar[0]*(width-barEnd)) + win.noutrefresh() + #0, + #0, + #y, + #x, + #ymax-1, + #xmax-1) diff --git a/asciifarm/client/display/infopad.py b/asciifarm/client/display/infopad.py index 564d6d4..7c9fef0 100644 --- a/asciifarm/client/display/infopad.py +++ b/asciifarm/client/display/infopad.py @@ -8,25 +8,32 @@ class InfoPad: def __init__(self, size=(1,1), *args): - self.pad = curses.newpad(size[1], size[0]) - self.size = size + #self.pad = curses.newpad(size[1], size[0]) + #self.size = size self.changed = False - self.lastView = None + self.lines = [] + #self.lastView = None def showString(self, string): - self.pad.clear() - self.pad.addstr(0,0,string) + self.lines = string.split('\n') + #self.pad.clear() + #self.pad.addstr(0,0,string) self.changed = True - def update(self, screen, x, y, xmax, ymax, force=False): - if not self.changed and (x, y, xmax, ymax) == self.lastView or xmax <= x or ymax <= y and not force: + def update(self, win, force=False): + if not self.changed and not force or not win: return - self.lastView = (x, y, xmax, ymax) + #self.lastView = (x, y, xmax, ymax) + height, width = win.getmaxyx() + lines = [line[:width] for line in self.lines][:height] + text = '\n'.join(lines) + win.erase() + win.addstr(0, 0, text) self.changed = False - self.pad.noutrefresh( - 0, - 0, - y, - x, - ymax-1, - xmax-1) + win.noutrefresh() + #0, + #0, + #y, + #x, + #ymax-1, + #xmax-1) diff --git a/asciifarm/client/display/inventorypad.py b/asciifarm/client/display/inventorypad.py index f10b138..8ef3296 100644 --- a/asciifarm/client/display/inventorypad.py +++ b/asciifarm/client/display/inventorypad.py @@ -5,32 +5,37 @@ class InventoryPad: def __init__(self, title, maxItems=20): self.title = title - self.maxItems = maxItems - self.pad = curses.newpad(maxItems+2, 100) + #self.maxItems = maxItems + #self.pad = curses.newpad(maxItems+2, 100) self.setInventory([]) self.changed = False - self.lastView = None + #self.lastView = None def setInventory(self, items): self.items = items - self.pad.erase() - self.pad.addstr(0,0, self.title + ":\n") - for i, item in enumerate(items[:self.maxItems]): - self.pad.addstr(i+1, 2, item) + #self.pad.erase() + #self.pad.addstr(0,0, self.title + ":\n") + #for i, item in enumerate(items[:self.maxItems]): + #self.pad.addstr(i+1, 2, item) self.changed = True def getHeight(self): return self.maxItems+2 - def update(self, screen, x, y, xmax, ymax, force=False): - if not self.changed and (x, y, xmax, ymax) == self.lastView or xmax <= x or ymax <= y and not force: + def update(self, win, force=False): + if not self.changed and not force or not win: return - self.lastView = (x, y, xmax, ymax) + #self.lastView = (x, y, xmax, ymax) self.changed = False - self.pad.noutrefresh( - 0, - 0, - y, - x, - ymax-1, - xmax-1) + height, width = win.getmaxyx() + win.erase() + win.addstr(0,0, self.title + ":\n") + for i, item in enumerate(self.items[:height-1]): + win.addstr(i+1, 2, item) + win.noutrefresh() + #0, + #0, + #y, + #x, + #ymax-1, + #xmax-1) diff --git a/asciifarm/client/display/messagepad.py b/asciifarm/client/display/messagepad.py index 7d22f2f..f447e44 100644 --- a/asciifarm/client/display/messagepad.py +++ b/asciifarm/client/display/messagepad.py @@ -5,24 +5,23 @@ import textwrap class MessagePad(): def __init__(self, maxLines=10): - self.maxLines = maxLines - self.pad = curses.newpad(maxLines+2, 200) + #self.maxLines = maxLines + #self.pad = curses.newpad(maxLines+2, 200) self.changed = False - self.lastView = None + #self.lastView = None self.messages = [] def addMessage(self, message): self.messages.append(message) self.changed = True - def getHeight(self): - return self.maxLines + #def getHeight(self): + #return self.maxLines - def update(self, screen, x, y, xmax, ymax, force=False): - if not self.changed and (x, y, xmax, ymax) == self.lastView or xmax <= x or ymax <= y and not force: + def update(self, win, force=False): + if not self.changed and not force or not win: return - width = xmax - x - height = ymax - y # should equal self.getHeight() + height, width = win.getmaxyx() if height < 1: return lines = [] @@ -30,13 +29,13 @@ class MessagePad(): lines += textwrap.wrap(message, width) if len(lines) > height: lines = lines[len(lines)-height:] - self.pad.erase() - self.pad.addstr(0,0,'\n'.join(lines)) + win.erase() + win.addstr(0,0,'\n'.join(lines)) self.changed = False - self.pad.noutrefresh( - 0, - 0, - y, - x, - ymax-1, - xmax-1) + win.noutrefresh() + #0, + #0, + #y, + #x, + #ymax-1, + #xmax-1) diff --git a/asciifarm/client/display/screen.py b/asciifarm/client/display/screen.py index 9924f9c..d9e9266 100644 --- a/asciifarm/client/display/screen.py +++ b/asciifarm/client/display/screen.py @@ -4,25 +4,67 @@ from .fieldpad import FieldPad import signal - class Screen: - def __init__(self, stdscr, maxSize=(float("inf"),float("inf")), charSize=1): + def __init__(self, display, stdscr): + self.display = display curses.curs_set(0) self.stdscr = stdscr - self.height, self.width = self.stdscr.getmaxyx() + #self.height, self.width = self.stdscr.getmaxyx() + self.setWins() signal.signal(signal.SIGWINCH, self.updateSize) + def _limitHeight(self, h, y): + return min(h + y, self.height) - y + + def setWins(self): + height, width = self.height, self.width = self.stdscr.getmaxyx() + + sideW = 20 + sideX = width-sideW + msgH = max(3, min(height // 5, 6)) + msgY = height - msgH + healthY = 0 + healthH = self._limitHeight(2, healthY) + groundY = healthY + healthH + groundH = self._limitHeight(7, groundY) + invY = groundY + groundH + invH = self._limitHeight(12, invY) + infoY = invY + invH + infoH = self._limitHeight(20, infoY) + + + self.fieldWin = curses.newwin(msgY, sideX - 1, 0, 0) + self.msgWin = curses.newwin(msgH, sideX - 1, msgY, 0) + self.healthWin = curses.newwin(healthH, sideW, healthY, sideX) + self.groundWin = curses.newwin(groundH, sideW, groundY, sideX) + self.inventoryWin = curses.newwin(invH, sideW, invY, sideX) + self.infoWin = curses.newwin(infoH, sideW, infoY, sideX) + + + def updateSize(self, *args): curses.endwin() curses.initscr() - self.height, self.width = self.stdscr.getmaxyx() + self.setWins() + #self.height, self.width = self.stdscr.getmaxyx() self.stdscr.clear() + self.update(True) + + def update(self, force=False): + d = self.display + d.fieldPad.update(self.fieldWin, force) + d.messagePad.update(self.msgWin, force) + d.healthPad.update(self.healthWin, force) + d.groundPad.update(self.groundWin, force) + d.inventoryPad.update(self.inventoryWin, force) + d.infoPad.update(self.infoWin, force) def getWidth(self): return self.width def getHeight(self): return self.height - + + #def update(self |
