-
Notifications
You must be signed in to change notification settings - Fork 22
/
ui.py
368 lines (272 loc) · 9.48 KB
/
ui.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
from nbinput import BlockingInput, UP, DOWN, RIGHT, LEFT
from console import CLS, REDRAW, WIDTH, HEIGHT, SHOW_CUR, HIDE_CUR
from colours import *
from console import *
from data import help_data
import saves
back = ('Back...', lambda: False)
def menu(name, options, selection=0):
"""
Executes the users selection from the menu, and returns the result.
Parameters:
- name: menu title
- options: a tuple of option name and option function
"""
STAR = colour_str('*', TERM_YELLOW)
options = [x for x in options if x is not None]
print_map = {}
i = 0
for j, option in enumerate(options):
if len(option) > 1:
print_map[i] = j
i += 1
padding = 5
max_height = HEIGHT - padding
char = None
with BlockingInput() as bi:
while not str(char) in ' \n':
selection %= len(print_map)
offset = selection - max(
min(selection, max_height - padding),
selection + min(0, max_height - len(options))
)
# Print menu
out = ''
for i, option in enumerate(options[offset:offset+max_height]):
if option == ():
pass
elif offset + i == print_map.get(selection):
out += STAR + colour_str(option[0], style=BOLD) + STAR
else:
out += ' ' + option[0] + ' '
out += '\n'
print(REDRAW + title(name) + out)
# Wait for useful input
while True:
char = str(bi.escape_code())
if char in ' \n':
break
if char in 'Ww'+UP:
selection -= 1
break
if char in 'Ss'+DOWN:
selection += 1
break
print(CLS)
# Execute function of selection
return options[print_map[selection]][1](), selection
def loop_menu(title, generator):
"""
Parameters:
- generator: Function which generates the `options` argument
for menu()
- top: Is the top level menu. Meaning False stays at current level too
Return values from option functions have these meanings:
- None: Stays at current level
- False, other: Drops a level
"""
data = None
selection = 0
while data is None:
data, selection = menu(title, generator(), selection)
if data is False:
# Stop at next level
data = None
return data
def main(meta, settings):
""" Loops the main menu until the user loads a save. """
print(CLS, end='')
return loop_menu('Main menu', lambda: (
[('Saves', load_save)] +
[('Multiplayer', lambda: servers(meta))] +
[('Settings', lambda: edit_settings(settings))] +
[('Help', help_)] +
[('Exit', lambda: False)]
))
def lambda_gen(func, *args, **kwargs):
""" Creates a lambda to call a function. """
return lambda: func(*args, **kwargs)
def title(name):
""" Returns a padded coloured string containing the title. """
return ' {title}\n\n'.format(
title = colour_str('{name}\n {_}'.format(
name = name,
_ = ('=' * len(name))
), style=BOLD)
)
def saves_list(func):
saves_list = saves.list_saves()
return ([(save[1]['name'], lambda_gen(func, save[0])) for save in saves_list] +
[() if saves_list else None])
def load_save():
""" A menu for selecting a save to load. """
return loop_menu('Load save', lambda: (
saves_list(lambda s: {'local': True,
'save': s}) +
[('Add new save', add_save)] +
[('Rename save', rename_save)] +
[('Delete save', delete_save)] +
[back])
)
def delete_save():
""" A menu for selecting a save to delete. """
loop_menu('Delete Save', lambda: (
saves_list(saves.delete_save) +
[back])
)
return None
def rename_save():
""" A menu for selecting a save to rename. """
save = loop_menu('Rename Save', lambda: (
saves_list(lambda s: s) +
[back])
)
if not save:
return None
print(REDRAW + title('Rename save'), end='')
meta = saves.get_meta(save)
meta['name'] = input(colour_str(' Save name', style=BOLD)
+ ' (leave blank to cancel): ' + SHOW_CUR)
print(HIDE_CUR + CLS, end='')
if meta['name']:
saves.save_meta(save, meta)
def add_save():
""" Lets the user enter a save name, then it creates and loads the save. """
print(REDRAW + title('New save'), end='')
meta = {}
meta['name'] = input(colour_str(' Save name', style=BOLD)
+ ' (leave blank to cancel): ' + SHOW_CUR)
print(HIDE_CUR, end='')
if not meta['name']:
print(CLS, end='')
return None
seed = input(colour_str(' Map seed', style=BOLD)
+ ' (leave blank to randomise): ' + SHOW_CUR)
print(HIDE_CUR, end='')
if seed:
meta['seed'] = seed
save = saves.new_save(meta)
if save is None:
error('Error creating save')
else:
return {'local': True, 'save': save}
def server_list(meta, func):
servers_list = meta.get('servers', [])
return ([('{}:{}'.format(*server), lambda_gen(func, server)) for server in servers_list] +
[() if servers_list else None])
def servers(meta):
""" A menu for selecting a server to join. """
return loop_menu('Join server', lambda: (
server_list(meta, lambda s: {'local': False,
'ip': s[0],
'port': s[1]}) +
[('Add new server', lambda: add_server(meta))] +
[('Delete server', lambda: delete_server(meta))] +
[back])
)
def title_case(s):
for t in '-_':
s = ' '.join(s.split(t))
return s.title()
def set_setting(settings, setting, value):
default_value = saves.default_settings.get(setting)
if isinstance(default_value, bool):
settings[setting] = not settings[setting]
else:
print(REDRAW + title('Edit Setting'), end='')
new = input(colour_str(' ' + title_case(setting), style=BOLD)
+ ' (leave blank to leave unchanged): ' + SHOW_CUR)
print(HIDE_CUR, end='')
if new:
if isinstance(default_value, int):
new = int(new)
elif isinstance(default_value, float):
new = float(new)
settings[setting] = new
print(CLS, end='')
saves.save_settings(settings)
def edit_settings(settings):
result = loop_menu('Settings', lambda: (
[('{}: {}'.format(title_case(setting), value),
lambda_gen(set_setting, settings, setting, value)) for setting, value in settings.items()] +
[back])
)
return result
def delete_server(meta):
""" A menu for selecting a server to delete. """
loop_menu('Delete Server', lambda: (
server_list(meta, lambda s: saves.delete_server(meta, s)) +
[back])
)
return None
def add_server(meta):
""" Get ip and port of server to connect to, then load world from server. """
print(REDRAW + title('New server'), end='')
ip = input(colour_str(' Server IP', style=BOLD)
+ ' (leave blank to cancel): ' + SHOW_CUR)
print(HIDE_CUR, end='')
if not ip:
print(CLS, end='')
return None
port = input(colour_str(' Server port', style=BOLD)
+ ' (leave blank to cancel): ' + SHOW_CUR)
print(HIDE_CUR, end='')
if not port:
print(CLS, end='')
return None
saves.add_server(meta, (ip, port))
return {'local': False,
'ip': ip,
'port': port}
def pause(server, settings):
print(CLS, end='')
return loop_menu('Paused', lambda: (
('Resume', lambda: False),
('Settings', lambda: edit_settings(settings)),
('Help', help_),
(None if server.serving is None else (
('Disable Multiplayer', server.kill_server)
if server.serving else
('Enable Multiplayer', server.init_server))),
((' Port: {}'.format(server.port),) if server.serving else None),
('Main Menu', lambda: 'exit')
))
def respawn():
print(CLS, end='')
return loop_menu('You Died!', lambda: (
('Respawn', lambda: False),
('Main Menu', lambda: 'exit')
))
def help_():
""" Displays the help stored in the help_data list. """
out = REDRAW + title('Help')
max_len = max(len(item[0]) for (label, section) in help_data for item in section)
for (label, section) in help_data:
out += label + '\n'
for name, key in section:
out += ' {name:{max_len}} - {key}\n'.format(
name = name, key = key, max_len = max_len
)
out += 'Back...\n'
print(out)
wait_for_input()
print(CLS, end='')
return None
def name(settings):
print(REDRAW, end='')
name = None
while not name:
name = input('Type your name: ' + SHOW_CUR)
print(HIDE_CUR + REDRAW, end='')
print(CLS, end='')
settings['name'] = name
saves.save_settings(settings)
return name
def error(message):
print(CLS + REDRAW + '\n' + colour_str(message, fg=TERM_RED) + '\n\nBack...\n')
wait_for_input()
print(CLS, end='')
def wait_for_input():
with BlockingInput() as bi:
while not str(bi.char()) in ' \n':
pass