summaryrefslogtreecommitdiff
path: root/asciifarm/client/display
diff options
context:
space:
mode:
authorWango Fett <wangofett@tilde.town>2017-10-26 17:05:49 +0000
committerWango Fett <wangofett@tilde.town>2017-10-26 17:05:49 +0000
commit3a8afeb369d4ef66c9b991e64febe66a35338177 (patch)
tree460a2b88f6d5de2258ad17a319a4672afcde434d /asciifarm/client/display
Pip installable!
Diffstat (limited to 'asciifarm/client/display')
-rw-r--r--asciifarm/client/display/__init__.py59
-rw-r--r--asciifarm/client/display/fieldpad.py44
-rw-r--r--asciifarm/client/display/healthpad.py25
-rw-r--r--asciifarm/client/display/infopad.py25
-rw-r--r--asciifarm/client/display/screen.py44
5 files changed, 197 insertions, 0 deletions
diff --git a/asciifarm/client/display/__init__.py b/asciifarm/client/display/__init__.py
new file mode 100644
index 0000000..41383d6
--- /dev/null
+++ b/asciifarm/client/display/__init__.py
@@ -0,0 +1,59 @@
+
+
+import curses
+from .fieldpad import FieldPad
+from .infopad import InfoPad
+from .healthpad import HealthPad
+from .screen import Screen
+
+class Display:
+
+ def __init__(self, stdscr, charMap, colours=False):
+
+ self.screen = Screen(stdscr)
+ self.fieldPad = FieldPad((64, 32), charMap.get("charwidth", 1), colours)
+ self.characters = charMap["mapping"]
+ self.defaultChar = charMap.get("default", "?")
+ self.infoPad = InfoPad((100, 100))
+ self.healthPad = HealthPad((20, 1))
+ self.lastinfostring = None
+ self.colours = colours
+ if colours:
+ curses.use_default_colors()
+ for i in range(0, min(256, curses.COLORS, curses.COLOR_PAIRS)):
+ curses.init_pair(i, i%16, i//16)
+
+
+ def resizeField(self, size):
+ self.fieldPad.resize(*size)
+
+ def drawFieldCells(self, cells):
+ for cell in cells:
+ (x, y), spriteName = cell
+ sprite = self.getChar(spriteName)
+ self.fieldPad.changeCell(x, y, *sprite)
+ self.screen.change()
+
+ def setFieldCenter(self, pos):
+ self.fieldPad.setCenter(pos)
+
+ def showHealth(self, health, maxHealth):
+ self.healthPad.setHealth(health, maxHealth)
+ self.screen.change()
+
+ def showInfo(self, infostring):
+ if infostring != self.lastinfostring:
+ self.infoPad.showString(infostring)
+ self.screen.change()
+ self.lastinfostring = infostring
+
+ def getChar(self, sprite):
+ char = self.characters.get(sprite, self.defaultChar)
+ if isinstance(char, str):
+ return [char]
+ return char
+
+ def update(self):
+ self.screen.update(self.fieldPad, self.infoPad, self.healthPad)
+
+
diff --git a/asciifarm/client/display/fieldpad.py b/asciifarm/client/display/fieldpad.py
new file mode 100644
index 0000000..ac69678
--- /dev/null
+++ b/asciifarm/client/display/fieldpad.py
@@ -0,0 +1,44 @@
+
+import curses
+
+
+class FieldPad:
+
+
+
+ def __init__(self, size=(1,1), charSize=1, colours=False):
+ self.pad = curses.newpad(size[1]+1, (size[0]+1)*charSize)
+ self.size = size
+ self.charSize = charSize
+ self.center = (0, 0)
+ self.colours = colours
+
+ def resize(self, width, height):
+ self.size = (width, height)
+ self.pad.resize(height+1, width*self.charSize1)
+
+ def changeCell(self, x, y, char, colour=None):
+ if colour != None and self.colours:
+ self.pad.addstr(y, x*self.charSize, char, curses.color_pair(colour))
+ else:
+ self.pad.addstr(y, x*self.charSize, char)
+
+ def setCenter(self, pos):
+ self.center = pos
+
+ def getWidth(self):
+ return self.size[0]*self.charSize
+
+ def getHeight(self):
+ return self.size[1]
+
+ def update(self, screen, x, y, xmax, ymax):
+ width = xmax-x
+ height = ymax-y
+ self.pad.noutrefresh(
+ max(0, min(self.getHeight()-height, self.center[1] - int(height/2))),
+ max(0, min(self.getWidth()-width, self.center[0]*self.charSize - int(width/2))),
+ y,
+ x,
+ ymax-1,
+ xmax-1)
diff --git a/asciifarm/client/display/healthpad.py b/asciifarm/client/display/healthpad.py
new file mode 100644
index 0000000..17baf35
--- /dev/null
+++ b/asciifarm/client/display/healthpad.py
@@ -0,0 +1,25 @@
+
+import curses
+
+
+
+class HealthPad:
+
+
+
+ def __init__(self, size=(1,1), *args):
+ self.pad = curses.newpad(size[1], size[0])
+ self.size = size
+
+ def setHealth(self, health, maxHealth):
+ self.pad.erase()
+ self.pad.addstr(0,0,"Health: {}/{}".format(health, maxHealth))
+
+ def update(self, screen, x, y, xmax, ymax):
+ self.pad.noutrefresh(
+ 0,
+ 0,
+ y,
+ x,
+ ymax-1,
+ xmax-1)
diff --git a/asciifarm/client/display/infopad.py b/asciifarm/client/display/infopad.py
new file mode 100644
index 0000000..6fd8655
--- /dev/null
+++ b/asciifarm/client/display/infopad.py
@@ -0,0 +1,25 @@
+
+import curses
+
+
+
+class InfoPad:
+
+
+
+ def __init__(self, size=(1,1), *args):
+ self.pad = curses.newpad(size[1], size[0])
+ self.size = size
+
+ def showString(self, string):
+ self.pad.clear()
+ self.pad.addstr(0,0,string)
+
+ def update(self, screen, x, y, xmax, ymax):
+ self.pad.noutrefresh(
+ 0,
+ 0,
+ y,
+ x,
+ ymax-1,
+ xmax-1)
diff --git a/asciifarm/client/display/screen.py b/asciifarm/client/display/screen.py
new file mode 100644
index 0000000..ec49d34
--- /dev/null
+++ b/asciifarm/client/display/screen.py
@@ -0,0 +1,44 @@
+
+import curses
+from .fieldpad import FieldPad
+
+import signal
+
+SIDEWIDTH = 20
+HEALTHHEIGHT = 1
+
+class Screen:
+
+
+ def __init__(self, stdscr, maxSize=(float("inf"),float("inf")), charSize=1):
+ curses.curs_set(0)
+ self.stdscr = stdscr
+ self.height, self.width = self.stdscr.getmaxyx()
+ self.changed = False
+ signal.signal(signal.SIGWINCH, self.updateSize)
+
+ def updateSize(self, *args):
+ curses.endwin()
+ curses.initscr()
+ self.height, self.width = self.stdscr.getmaxyx()
+ self.stdscr.clear()
+ self.change()
+
+ def getWidth(self):
+ return self.width
+
+ def getHeight(self):
+ return self.height
+
+ def change(self):
+ self.changed = True
+
+ def update(self, fieldPad, infoPad, healthPad):
+ if self.changed:
+ fieldEnd = min(fieldPad.getWidth(), self.getWidth()-SIDEWIDTH-1)
+ fieldPad.update(self, 0,0,fieldEnd, min(fieldPad.getHeight(), self.getHeight()))
+ healthPad.update(self, fieldEnd+1,0, self.getWidth(), HEALTHHEIGHT)
+ infoPad.update(self, fieldEnd+1,HEALTHHEIGHT, self.getWidth(), self.getHeight())
+ curses.doupdate()
+ self.changed = False
+