summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortroido <troido@hotmail.com>2018-01-03 17:07:15 +0100
committertroido <troido@hotmail.com>2018-01-03 17:07:15 +0100
commit99d7920cb3daf3fc5cb6f824eb4a639542d47974 (patch)
tree62b2cbafbe2caf462fab7b7f4766c1d5eb63a8d8
parent03d45d60b84c07c29a304301fb290aecee9cfa24 (diff)
added intermediary window wrapper for safety. also server now appends logs
-rw-r--r--asciifarm/client/display/display.py2
-rw-r--r--asciifarm/client/display/fieldpad.py4
-rw-r--r--asciifarm/client/display/healthpad.py12
-rw-r--r--asciifarm/client/display/infopad.py4
-rw-r--r--asciifarm/client/display/inventorypad.py20
-rw-r--r--asciifarm/client/display/messagepad.py4
-rw-r--r--asciifarm/client/display/screen.py10
-rw-r--r--asciifarm/client/display/textinput.py4
-rw-r--r--asciifarm/client/display/window.py71
9 files changed, 98 insertions, 33 deletions
diff --git a/asciifarm/client/display/display.py b/asciifarm/client/display/display.py
index 2ebbd02..7c5384d 100644
--- a/asciifarm/client/display/display.py
+++ b/asciifarm/client/display/display.py
@@ -35,7 +35,7 @@ class Display:
bg = get(sprite, 2)
self.characters[name] = (char, fg, bg)
self.defaultChar = charMap.get("default", "?")
- self.screen = Screen(self, stdscr)
+ self.screen = Screen(self, stdscr, self.colours)
self.widgets = {}
diff --git a/asciifarm/client/display/fieldpad.py b/asciifarm/client/display/fieldpad.py
index 24baa81..6619c1f 100644
--- a/asciifarm/client/display/fieldpad.py
+++ b/asciifarm/client/display/fieldpad.py
@@ -57,8 +57,8 @@ class FieldPad:
def update(self):
win = self.widget.getWin()
- height, width = win.getmaxyx()
- y, x = win.getparyx()
+ width, height = win.getSize()
+ x, y = win.getPos()
xmax = x + width
ymax = y + height
self.pad.noutrefresh(
diff --git a/asciifarm/client/display/healthpad.py b/asciifarm/client/display/healthpad.py
index 559906d..7b756a5 100644
--- a/asciifarm/client/display/healthpad.py
+++ b/asciifarm/client/display/healthpad.py
@@ -22,15 +22,15 @@ class HealthPad:
def update(self):
win = self.widget.getWin()
- height, width = win.getmaxyx()
+ width, height = win.getSize()
width -= 1
barEnd = round(self.health/self.maxHealth * width) if self.maxHealth > 0 else 0
win.erase()
- win.addstr(0,0,"Health: {}/{}".format(self.health, self.maxHealth)[:width])
+ win.addLine((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:]))
+ win.addLine((0, 1), self.char[0]*barEnd, self.char[1:])
+ win.addLine((barEnd, 1), self.emptyChar[0]*(width-barEnd), self.emptyChar[1:])
else:
- win.addstr(1,0, self.char[0]*barEnd)
- win.addstr(1,barEnd, self.emptyChar[0]*(width-barEnd))
+ win.addLine((0, 1), self.char[0]*barEnd)
+ win.addLine((barEnd, 1), self.emptyChar[0]*(width-barEnd))
win.noutrefresh()
diff --git a/asciifarm/client/display/infopad.py b/asciifarm/client/display/infopad.py
index dc543bf..a45a03e 100644
--- a/asciifarm/client/display/infopad.py
+++ b/asciifarm/client/display/infopad.py
@@ -21,9 +21,9 @@ class InfoPad:
def update(self):
win = self.widget.getWin()
- height, width = win.getmaxyx()
+ width, height = win.getSize()
lines = [line[:width-1] for line in self.lines][:height]
text = '\n'.join(lines)
win.erase()
- win.addstr(0, 0, text)
+ win.addLine((0, 0), text)
win.noutrefresh()
diff --git a/asciifarm/client/display/inventorypad.py b/asciifarm/client/display/inventorypad.py
index 595313c..cbfeff1 100644
--- a/asciifarm/client/display/inventorypad.py
+++ b/asciifarm/client/display/inventorypad.py
@@ -28,28 +28,20 @@ class InventoryPad:
def update(self):
win = self.widget.getWin()
- height, width = win.getmaxyx()
+ width, height = win.getSize()
height -= 1
selected = self.selector.getValue()
start = min(selected - height//2, len(self.items)-height)
start = max(start, 0)
end = start + height
win.erase()
- win.addstr(0,0, (self.title + ":")[:width])
+ win.addLine((0,0), (self.title + ":")[:width])
for i, item in enumerate(self.items[start:end]):
if i + start == selected:
- win.addstr(i+1, 0, '*')
- win.addstr(i+1, 1, item)
+ win.addLine((0, i+1), '*')
+ win.addLine((1, i+1), item)
if end < len(self.items):
- try:
- win.addstr(height, width-1, "+")
- except curses.error:
- # ncurses has a weird problem:
- # it always raises an error when drawing to the last character in the window
- # it draws first and then raises the error
- # therefore to draw in the last place of the window the last character needs to be ingored
- # other solutions might be possible, but are more hacky
- pass
+ win.addLine((width-1, height), "+")
if start > 0:
- win.addstr(1, width-1, "-")
+ win.addLine((width-1, 1), "-")
win.noutrefresh()
diff --git a/asciifarm/client/display/messagepad.py b/asciifarm/client/display/messagepad.py
index c9eaf85..cae66a4 100644
--- a/asciifarm/client/display/messagepad.py
+++ b/asciifarm/client/display/messagepad.py
@@ -18,7 +18,7 @@ class MessagePad():
def update(self):
win = self.widget.getWin()
- height, width = win.getmaxyx()
+ width, height = win.getSize()
if height < 1:
return
lines = []
@@ -27,5 +27,5 @@ class MessagePad():
if len(lines) > height:
lines = lines[len(lines)-height:]
win.erase()
- win.addstr(0,0,'\n'.join(lines))
+ win.addLine((0,0),'\n'.join(lines))
win.noutrefresh()
diff --git a/asciifarm/client/display/screen.py b/asciifarm/client/display/screen.py
index e3c9831..dd80044 100644
--- a/asciifarm/client/display/screen.py
+++ b/asciifarm/client/display/screen.py
@@ -2,15 +2,17 @@
import curses
from .fieldpad import FieldPad
from asciifarm.common.utils import clamp
+from .window import Window
import signal
class Screen:
- def __init__(self, display, stdscr):
+ def __init__(self, display, stdscr, colours):
self.display = display
curses.curs_set(0)
self.stdscr = stdscr
+ self.colours = colours
self.setWins()
signal.signal(signal.SIGWINCH, self.updateSize)
@@ -50,13 +52,13 @@ class Screen:
def makeWin(self, x, y, width, height):
if width < 1 or height < 1:
- return None
- return curses.newwin(height, width, y, x)
+ win = None
+ win = curses.newwin(height, width, y, x)
+ return Window(win, self.colours)
def getWin(self, name):
return self.windows.get(name, None)
-
def updateSize(self, *args):
curses.endwin()
curses.initscr()
diff --git a/asciifarm/client/display/textinput.py b/asciifarm/client/display/textinput.py
index a32993f..621214e 100644
--- a/asciifarm/client/display/textinput.py
+++ b/asciifarm/client/display/textinput.py
@@ -17,8 +17,8 @@ class TextInput:
self.reading = True
curses.echo()
curses.nocbreak()
- win.addstr(0, 0, ">")
- string = win.getstr(0,2)
+ win.addLine((0, 0), ">")
+ string = win.getStr((2,0))
curses.noecho()
curses.cbreak()
self.reading = False
diff --git a/asciifarm/client/display/window.py b/asciifarm/client/display/window.py
new file mode 100644
index 0000000..a8e0696
--- /dev/null
+++ b/asciifarm/client/display/window.py
@@ -0,0 +1,71 @@
+
+import curses
+
+class Window:
+ """ Small wrapper around curses windows """
+
+ def __init__(self, win, colours=None):
+
+ self.setWin(win)
+ self.colours = colours
+
+
+
+ def setWin(self, win):
+ self.win = win
+
+ def getSize(self):
+ if not self.win:
+ return (0, 0)
+ height, width = self.win.getmaxyx()
+ return (width, height)
+
+ def getPos(self):
+ if not self.win:
+ return (0, 0)
+ y, x = self.win.getparyx()
+ return (x, y)
+
+ def addLine(self, pos, string, colour=(0,0)):
+ """Draw a string that does not contain newlines or characters with larger width
+
+ long lines are cropped to fit in the window"""
+ x, y = pos
+ width, height = self.getSize()
+ string = string[:width-x]
+ drawLast = None
+ if self.colours:
+ self._addstr(y, x, string, self.colours.get(*colour))
+ else:
+ self._addstr(y, x, string)
+
+
+ def _addstr(self, y, x, string, *args):
+ width, height = self.getSize()
+ if y == height-1 and x+len(string) == width:
+ if len(string) > 1:
+ self.win.addstr(y, x, string[:-1], *args)
+ try:
+ self.win.addstr(height-1, width-1, string[-1], *args)
+ except curses.error:
+ # ncurses has a weird problem:
+ # it always raises an error when drawing to the last character in the window
+ # it draws first and then raises the error
+ # therefore to draw in the last place of the window the last character needs to be ingored
+ # other solutions might be possible, but are more hacky
+ pass
+ else:
+ self.win.addstr(y, x, string, *args)
+
+ def erase(self):
+ self.win.erase()
+
+ def noutrefresh(self):
+ self.win.noutrefresh()
+
+ def getStr(self, pos):
+ x, y = pos
+ return self.win.getstr(y, x)
+
+
+