summaryrefslogtreecommitdiff
path: root/interface/chaserview.py
blob: 56a59d56257eef5ec6ef443e272cbe34724e13a4 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/env python3

import curses
import math
import threading 

from blc2.constants import INFTY, MANUAL, JOIN, CHASERSTEP

CURSES_LOCK = threading.RLock()

def format_time(n):
    if n == INFTY:
        return "∞s"
    elif n == 0:
        return "0s"
    postfixes = "mcisahkegtp"
    idx = 0
    multiple = 10
    while n >= multiple:
        idx += 1
        multiple *= 10

    return str(n)[0]+postfixes[idx]

def format_long(n):
    if n == INFTY:
        return "  ∞s"
    elif n == 0:
        return "  0s"
    elif n < 10000:
        return "%4d" % n
    else:
        n = str(n/1000)[:3]
        if n[-1] == '.':
            n = n[:-1]
        return "%4s" % n

class ChaserView:
    def set_dim(self, height, width):
        if height < 5 or width < 10:
            raise ValueError("Size too small")

        with self._lock:
            if (height, width) != (self._height, self._width):
                self.win.erase()
                self.win.noutrefresh()

                self.win.resize(height, width)
                self.win.redrawwin()

            self._height = height
            self._width = width
            self._redraw()

    @staticmethod
    def fit(s, l, pad=False):
        ## TODO: Try shortening by words first?
        if len(s) > l:
            return s[:l-1] + '…'
        elif len(s) < l and pad:
            return s + ' '*(l-len(s))
        return s
        
    def set_pos(self, y, x):
        with self._lock:
            if (y, x) != (self._y, self._x):
                self.win.mvwin(y, x)
                self._y = y
                self._x = x
                self.win.noutrefresh()

    @property
    def highlight(self):
        return self._highlight

    @highlight.setter
    def highlight(self, value):
        with self._lock:
            if self._highlight != value:
                self._highlight = value
                self._redraw()

    def _redraw(self):
        self.win.erase()
        self.win.border()
        self.win.hline(2, 1, curses.ACS_HLINE, self._width-2)
        self.win.addch(2, 0, curses.ACS_LTEE)
        self.win.addch(2, self._width-1, curses.ACS_RTEE)

        if self._chaser is None:
            self.win.refresh()
            return

        c = self._chaser
        w = self._width - 2
        self.win.addstr(1, 1, self.fit(("%d: "% c.id) + c.name + " (%s)" % ("Join" if c.type == JOIN else c.advance_mode), w, True), curses.A_REVERSE if self._highlight else 0)

        maxsteps = self._height - 4
        first = 0
        if maxsteps < len(c.steps):
            if self._selected is None:
                first = 0
                last = maxsteps
            else:
                last = min(self._selected + (maxsteps // 2), len(c.steps))
                first = last - maxsteps
                if first < 0:
                    last -= first
                    first = 0
        else:
            first = 0
            last = len(c.steps)
        steps = c.steps[first:last]
        for n, s in enumerate(steps, 1):
            if first+n-1 == self._selected:
                attrs = curses.A_REVERSE
            else: 
                attrs = 0
            if s.type == CHASERSTEP and s.function is not None:
                ft = s.function.type[0].upper()
                fid = str(s.function.id)
            elif s.type != CHASERSTEP:
                ft = s.type[0].upper()
                fid = str(s.id)
            else:
                ft = "-"
                fid = "---"

            t = "%s%3s%s|%s:%s:%s" % (ft, fid, '*' if (s.type == CHASERSTEP and s.duration_mode == MANUAL) else ' ', format_long(s.fade_in), format_long(s.duration if s.type != CHASERSTEP else s.length), format_long(s.fade_out))
            self.win.addstr(n+2, 1, self.fit((self._numformat % (first+n)) + ": " + s.name, w-len(t), pad=True)+t, attrs)

        if first > 0:
            self.win.addch(2, self._width//2, '⯅')
        if last < len(c.steps):
            self.win.addch(self._height-1, self._width//2, '⯆')
        
        self.win.refresh()

    @property
    def selected(self):
        with self._lock:
            return self._selected

    @selected.setter
    def selected(self, value):
        with self._lock:
            if value != self._selected:
                self._selected = value
                ## TODO: Clean this up if possible?
                self._redraw()

    def set_chaser(self, chaser, selected=None):
        with self._lock:
            self._chaser = chaser
            self._selected = selected
            if chaser is not None and chaser.steps:
                self._numformat = "%%%dd" % math.ceil(math.log10(len(chaser.steps)))
            self._redraw()

    @property
    def chaser(self):
        with self._lock:
            return self._chaser

    def __init__(self, y, x, height, width):
        with CURSES_LOCK:
            self.win = curses.newwin(height, width, y, x)
            self.win.leaveok(True)
            self.win.keypad(True)
        self._lock = threading.RLock()
        self._height = height
        self._width = width
        self._y = -1
        self._x = -1

        self._chaser = None
        self._highlight = False
        self._numformat = ""
        self._selected = -1

        self.set_pos(y, x)
        self.set_dim(height, width)