summaryrefslogtreecommitdiff
path: root/asciifarm/client/display/field.py
diff options
context:
space:
mode:
authortroido <troido@tilde.town>2018-01-24 13:27:41 +0000
committertroido <troido@tilde.town>2018-01-24 13:27:41 +0000
commitb15f89a3576b98f57ff1ec083e9299c890c1f7fc (patch)
tree3e2772610d8d1a91ff2b8ad17d2cd23ad687a8cd /asciifarm/client/display/field.py
parent50321b57b146944399671b6a8b56c6b769d5ddeb (diff)
parentb1ea1bff79c5e9edf6aedbe8f4183c7e4f92f1e8 (diff)
Merge branch 'master' of https://github.com/jmdejong/rooms
Diffstat (limited to 'asciifarm/client/display/field.py')
-rw-r--r--asciifarm/client/display/field.py73
1 files changed, 73 insertions, 0 deletions
diff --git a/asciifarm/client/display/field.py b/asciifarm/client/display/field.py
new file mode 100644
index 0000000..2ebb1b0
--- /dev/null
+++ b/asciifarm/client/display/field.py
@@ -0,0 +1,73 @@
+
+import curses
+
+
+class Field:
+
+
+ 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
+ self.changed = False
+ self.widget = None
+
+ def setWidget(self, widget):
+ self.widget = widget
+
+ def resize(self, width, height):
+ self.size = (width, height)
+ self.pad.resize(height+1, width*self.charSize)
+ self.widget.change()
+ win = self.widget.getWin()
+ if win:
+ win.erase()
+ win.noutrefresh()
+
+ def changeCell(self, x, y, sprites):
+ """ sprites must always have at least one element """
+ char, colour, bgcolour = sprites[0]
+ if bgcolour is None:
+ for (ch, co, bg) in sprites:
+ if bg is not None:
+ bgcolour = bg
+ break
+ else:
+ bgcolour = 0
+ if colour is not None and self.colours:
+ self.pad.addstr(y, x*self.charSize, " "*self.charSize, self.colours.get(7, 0))
+ self.pad.addstr(y, x*self.charSize, char, self.colours.get(colour, bgcolour))
+ else:
+ self.pad.addstr(y, x*self.charSize, char)
+ self.widget.change()
+
+ def setCenter(self, pos):
+ self.center = pos
+ self.widget.change()
+
+ def getWidth(self):
+ return self.size[0]*self.charSize
+
+ def getHeight(self):
+ return self.size[1]
+
+ def _roundWidth(self, x):
+ return x // self.charSize * self.charSize
+
+ def update(self):
+ win = self.widget.getWin()
+ width, height = win.getSize()
+ x, y = win.getPos()
+ 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)))),
+ y,
+ x + max(0, (width - self.getWidth()) // 2),
+ ymax,
+ xmax)