-
Notifications
You must be signed in to change notification settings - Fork 0
/
easycurses.py
343 lines (277 loc) · 10.4 KB
/
easycurses.py
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import curses
from collections import defaultdict
color_map = {
'white':curses.COLOR_WHITE,
'black':curses.COLOR_BLACK,
'magenta':curses.COLOR_MAGENTA,
'cyan':curses.COLOR_CYAN,
'red':curses.COLOR_RED,
'green':curses.COLOR_GREEN,
'yellow':curses.COLOR_YELLOW,
'blue':curses.COLOR_BLUE,
-1:-1
}
UP, RIGHT, DOWN, LEFT = 0, 1, 2, 3
def sign(x):
if x < 0:
return -1
if x > 0:
return 1
if x == 0:
return 0
class Pair():
'''
Used for convenience. It isn't required to use this object for positioning (a (y,x) tuple can be used instead), but it does provide some nice functionality.
'''
def __init__(self, y, x):
self.x=x
self.y=y
def __add__(self, other):
return Pair(self.y+other.y, self.x + other.x)
def __sub__(self, other):
return Pair(self.y-other.y, self.x - other.x)
def __eq__(self, other):
return (self.y==other.y and self.x==other.x)
def __getitem__(self, idx):
if idx == 0:
return self.y
if idx == 1:
return self.x
raise IndexError("No.")
def __hash__(self):
return ((self.x+self.y)*(self.x+self.y+1)<<2)+self.y
def __iter__(self):
yield self.y
yield self.x
def __tuple__(self):
return (self.y,self.x)
def __str__(self):
return str((self.y,self.x))
@staticmethod
def pair_from_direction(v, ortho=True):
if ortho:
return Pair(*[(-1,0),(0,1),(1,0),(0,-1)] [v])
else:
return Pair(*[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)] [v])
def get_neighbors(self, ortho=True):
for d in range(4 if ortho else 8):
yield self + Pair.pair_from_direction(d, ortho=ortho)
def rounded(self):
return Pair(int(self.y+.5), int(self.x+.5))
def euclidean(self, other):
return ((self.y-other.y)**2 + (self.x-other.x)**2)**.5
def direction_to(self, other):
diff = other - self
if abs(diff.y) > abs(diff.x):
if sign(diff.y) == -1:
return UP
else:
return DOWN
else:
if sign(diff.x) == -1:
return LEFT
else:
return RIGHT
class Char():
'''This is the class that gets sent to draw controller to render a single character'''
def __init__(self, pos, character, color=1):
self.pos = Pair(pos[0], pos[1])
self.char = character
self.color = color
@staticmethod
def from_string(st, pos, color=1, direction=RIGHT):
'''convenience for drawing whole strings.'''
p = Pair(pos[0],pos[1])
ls = []
for i in range(len(st)):
ls.append(Char(p, st[i], color=color))
p += Pair._directions[direction]
return ls
class ColorController():
'''Handles curses\'s color handling. Don\'t need to use this if you manually use curses\'s color stuff'''
def __init__(self):
if hasattr(ColorController, "_instance"):
raise Exception('This is a singleton.')
self.pairs = {}
self.counter = 1
@staticmethod
def get_instance():
if not hasattr(ColorController, '_instance'):
ColorController._instance = ColorController()
return ColorController._instance
@staticmethod
def get_color(text, bg):
'''specify two colors that exist in the color map. First is the text color, second is background color. Returns an int, use it when defining '''
assert text in color_map
assert bg in color_map
self = ColorController.get_instance()
if (text, bg) in self.pairs:
return self.pairs[(text, bg)]
else:
self.pairs[(text, bg)] = self.counter
curses.init_pair(self.counter, color_map[text], color_map[bg])
self.counter += 1
return self.counter - 1
class DrawController():
def __init__(self):
self.draw_buffer = defaultdict(list)
self.default_char = ' '
self.rules = {}
self.rule_assignments = defaultdict(list)
self.drawn = set()
self.to_restore = set()
def init_screen(self):
stdscr = curses.initscr()
curses.noecho()
curses.cbreak()
curses.curs_set(0)
stdscr.nodelay(1)
stdscr.keypad(1)
curses.start_color()
curses.use_default_colors()
self.screen = stdscr
self.height, self.width = stdscr.getmaxyx()
self.size = stdscr.getmaxyx()
self.default_color = ColorController.get_color('black', 'black')
return stdscr
def get_screen(self):
return self.screen
def set_default_char(self, c):
self.default_char = c
def set_default_color(self, co):
self.default_color = co
def update(self, modified):
'''Explicitly update some cells'''
m = []
for e in modified:
m.append(Pair(e[0],e[1]))
self.to_restore.update(modified)
def add_rule(self, rule_id, rule, ch, color=1, modified=None):
''' Adds a rule, if modified is not none it will only update those cells, if modified is none all cells will be iterated over. rule_id must be unique '''
assert rule_id not in self.rules
self.rules[rule_id] = (rule,ch,color)
if modified is None:
for i in range(self.height):
for j in range(self.width):
p = Pair(i,j)
if rule((i,j)):
self.to_restore.add(p)
else:
self.to_restore.update(modified)
def update_rule(self, rule_id, rule, ch, color=1, modified=None):
self.remove_rule(rule_id)
self.add_rule(rule_id, rule, ch, color=color, modified=modified)
def remove_rule(self, rule_id):
if rule_id in self.rules:
self.rules.pop(rule_id)
self.to_restore.update(self.rule_assignments[rule_id])
self.rule_assignments.pop(rule_id)
def _draw_char(self, y, x, ch, co):
if y < self.height and y >= 0 and x < self.width and x >= 0:
try:
draw_y, draw_x = Pair(y,x).rounded()
self.screen.addch(draw_y, draw_x, ch, curses.color_pair(co))
except curses.error:
pass
def full_draw(self):
'''Function should be called after initialization. Prepares to redraw every cell, the subsequent render (restore) will be expensive.'''
for i in range(self.height):
for j in range(self.width):
p = Pair(i,j)
self.to_restore.add(p)
def _restore(self):
'''Each iteration, anything not explicitly drawn that was previously drawn is redrawn to the value specified in the rules, or the default.'''
self.rule_assignments = defaultdict(list)
for pix in self.to_restore:
for k in self.rules:
rule = self.rules[k]
if rule[0](tuple(pix)):
self._draw_char(pix.y, pix.x, rule[1], rule[2])
self.rule_assignments[k].append(pix)
break
else:
self._draw_char(pix.y, pix.x, self.default_char, self.default_color)
self.to_restore = set()
def draw(self, chars):
'''Use this function to draw characters to the screen. Receives a list of Char instances.'''
for bc in chars:
assert isinstance(bc, Char)
y,x = bc.pos
color, char = bc.color, bc.char
self._draw_char(y, x, char, color)
p = bc.pos.rounded()
if p in self.to_restore:
self.to_restore.remove(p)
self.drawn.add(p)
def render(self):
'''Actually draw buffered draws.'''
self._restore()
self.to_restore = self.drawn
self.drawn = set()
self.screen.refresh()
def end(self):
'''Call to restore terminal to normal.'''
curses.endwin()
'''Really simple keyboard/mouse controller. Provided bare minimum.'''
class InputController():
def __init__(self, screen):
'''Needs a reference to a screen, grab one from the draw controller via get_screen'''
curses.mousemask(1)
self.use_mouse = True
try:
curses.getmouse()
except:
self.use_mouse = False
self.screen = screen
self.callbacks = defaultdict(set)
if self.use_mouse:
self.mouse_callbacks = set()
self.mouse_state = curses.getmouse()
def register_keyset(self, keyset, callback, ident=None):
'''
Provided an iterable of characters and a single callback function, assigns the callback function to those keys.
Callback should receive a single argument for which key was pressed.
ident can be used to remove callbacks via remove_callback.
'''
for k in keyset:
if isinstance(k, str):
self.callbacks[ord(k)].add((ident, callback))
def can_use_mouse(self):
return self.use_mouse
def register_mouse(self, callback, ident=None):
'''
After any mouse click we send the mouse position and state to all callbacks.
'''
self.mouse_callbacks.add((ident, callback))
def getkeys(self): #order of callbacks not guaranteed
pressed = set()
char = self.screen.getch()
while True:
if char != -1:
pressed.add(char)
else:
break
char = self.screen.getch()
for k in pressed:
if k == curses.KEY_MOUSE and self.use_mouse:
s = curses.getmouse()
_, x, y, _, state = s
if s != self.mouse_state:
for c in self.mouse_callbacks:
c[1](Pair(y, x), state)
self.mouse_state = s
for h in self.callbacks[k]:
h[1](k)
def remove_keyset_callback(self, ident):
for c in self.callbacks:
to_remove = set()
for e in self.callbacks[c]:
if e[0] == ident:
to_remove.add(e)
self.callbacks[c] -= to_remove
def remove_mouse_callback(self, ident):
to_remove = set()
for c in self.mouse_callbacks:
if c[0] == ident:
to_remove.add(c)
self.mouse_callbacks -= to_remove