summaryrefslogtreecommitdiff
path: root/asciifarm/client/display/fieldpad.py
blob: 1966611d86b159804a3d4ad5c49bda7af5cc594c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

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
        self.changed = False
        #self.lastView = None
    
    def resize(self, width, height):
        self.size = (width, height)
        self.pad.resize(height+1, width*self.charSize+1)
    
    def changeCell(self, x, y, char, colour=None, bgcolour=0):
        if colour != None and self.colours:
            self.pad.addstr(y, x*self.charSize, char, self.colours.get(colour, bgcolour))
        else:
            self.pad.addstr(y, x*self.charSize, char)
        self.changed = True
    
    def setCenter(self, pos):
        self.center = pos
    
    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, force=False):
        if not self.changed and not force:
            return
        #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)))),
            y,
            x,
            ymax,
            xmax)
        self.changed = False