-
Notifications
You must be signed in to change notification settings - Fork 11
/
menus.py
390 lines (287 loc) · 10.5 KB
/
menus.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
from globals import *
import libtcodpy as tcod
import alife
import life
def create_menu(menu=[], position=[0,0], title='Untitled', format_str='$k: $v', padding=MENU_PADDING,
on_select=None, on_change=None, on_close=None, on_move=None, dim=True, alignment='', action=None,
close_on_select=False):
_menu = {'settings': {'position': list(position),'title': title,'padding': padding,'dim': dim,'format': format_str},
'on_select': on_select,
'on_change': on_change,
'on_move': on_move,
'on_close': on_close,
'close_on_select': close_on_select,
'alignment': alignment,
'index': 0,
'values':{},
'action':action}
#TODO: Does this need to be copied?
_menu['menu'] = menu[:]
_size = [len(title),len(_menu['menu'])+2+(_menu['settings']['padding'][1]*2)]
_uid = 0
for entry in _menu['menu']:
entry['uid'] = _uid
_uid+=1
for value in range(len(entry['values'])):
_line = format_entry(_menu['settings']['format'], entry, value=value)
if len(_line) > _size[0]:
_size[0] = len(_line)
_menu['settings']['size'] = (_size[0]+(_menu['settings']['padding'][0]*2),_size[1])
_menu['settings']['console'] = tcod.console_new(_menu['settings']['size'][0],_menu['settings']['size'][1])
MENUS.append(_menu)
return MENUS.index(_menu)
def create_item(item_type,key,values,icon=' ',enabled=True, color=(tcod.gray, tcod.white), **kwargs):
if not isinstance(values,list):
values = [values]
_item = {'type': item_type,
'key': key,
'enabled': enabled,
'icon': icon,
'color': color,
'values': values,
'value': 0}
if _item['type'] in ['title','spacer']:
_item['enabled'] = False
_item.update(kwargs)
return _item
def remove_item_from_menus(matching):
for menu in MENUS:
for item in menu['menu'][:]:
_match = True
for key in matching:
#print item.keys()
if not key in item or not matching[key] == item[key]:
_match = False
break
if _match:
menu['menu'].remove(item)
def format_entry(format_str, entry, value=-1):
if value == -1:
value = entry['value']
return format_str.replace('$k', str(entry['key']))\
.replace('$v', str(entry['values'][value]))\
.replace('$i', str(entry['icon']))
def redraw_menu(menu):
tcod.console_clear(menu['settings']['console'])
def draw_menus():
for menu in MENUS:
_y_offset = menu['settings']['padding'][1]
tcod.console_set_default_foreground(menu['settings']['console'], tcod.white)
tcod.console_print(menu['settings']['console'],
menu['settings']['padding'][0],
_y_offset,
menu['settings']['title'])
_y_offset += 2
for item in menu['menu']:
if item['type'] == 'title':
tcod.console_set_default_foreground(menu['settings']['console'], tcod.white)
_line = format_entry('- $k',item)
elif item['type'] == 'spacer':
tcod.console_set_default_foreground(menu['settings']['console'], tcod.white)
_line = item['key']*(menu['settings']['size'][0]-menu['settings']['padding'][0])
elif item['type'] == 'input':
#TODO: Input check?
if MENUS.index(menu) == ACTIVE_MENU['menu'] and menu['menu'].index(item) == menu['index'] and item['enabled']:
#TODO: Colors
tcod.console_set_default_foreground(menu['settings']['console'], item['color'][1])
elif not item['enabled']:
tcod.console_set_default_foreground(menu['settings']['console'], tcod.dark_sepia)
elif menu['settings']['dim']:
tcod.console_set_default_foreground(menu['settings']['console'], item['color'][0])
_line = format_entry(menu['settings']['format'],item)
else:
if MENUS.index(menu) == ACTIVE_MENU['menu'] and menu['menu'].index(item) == menu['index'] and item['enabled']:
#TODO: Colors
tcod.console_set_default_foreground(menu['settings']['console'], item['color'][1])
elif not item['enabled']:
tcod.console_set_default_foreground(menu['settings']['console'], tcod.dark_sepia)
elif menu['settings']['dim']:
tcod.console_set_default_foreground(menu['settings']['console'], item['color'][0])
#TODO: Per-item formats here
_line = format_entry(menu['settings']['format'],item)
tcod.console_print(menu['settings']['console'],
menu['settings']['padding'][0],
_y_offset,
_line)
_y_offset += 1
def align_menus():
for menu in MENUS:
if not MENUS.index(menu):
continue
if not menu['alignment'] and menu['settings']['position'][1] > 1:
continue
if not 'position_mod' in menu['settings']:
menu['settings']['position_mod'] = menu['settings']['position'][:]
if menu['alignment'] == 'botleft':
menu['settings']['position'][0] = 1
menu['settings']['position'][1] = WINDOW_SIZE[1]-menu['settings']['size'][1]-1
if menu['alignment']:
menu['settings']['position'][0] += menu['settings']['position_mod'][0]
menu['settings']['position'][1] += menu['settings']['position_mod'][1]
continue
_prev_menu = MENUS[MENUS.index(menu)-1]
_y_mod = _prev_menu['settings']['position'][1]+_prev_menu['settings']['size'][1]
menu['settings']['position'][1] = _y_mod+1
def delete_menu(id, abort=False):
_menu = get_menu(id)
if _menu['on_close'] and abort:
_entry = get_selected_item(id, _menu['index'])
_menu['on_close'](_entry)
if ACTIVE_MENU['menu'] == id:
ACTIVE_MENU['menu'] -= 1
MENUS.pop(id)
def delete_active_menu(abort=True):
if MENUS:
delete_menu(ACTIVE_MENU['menu'], abort=abort)
return True
return False
def get_menu(id):
return MENUS[id]
def get_menu_by_name(name):
for _menu in MENUS:
if _menu['settings']['title'] == name:
return MENUS.index(_menu)
return -1
def activate_menu(id):
ACTIVE_MENU['menu'] = id
MENUS[id]['index'] = find_item_after(MENUS[id])
if MENUS[id]['on_move']:
_entry = get_selected_item(id, MENUS[id]['index'])
return MENUS[id]['on_move'](_entry)
def activate_menu_by_name(name):
ACTIVE_MENU['menu'] = get_menu_by_name(name)
def find_item_before(menu,index=0):
_items = menu['menu'][:index][:]
_items.reverse()
for item in _items:
if item['enabled']:
return menu['menu'].index(item)
return find_item_before(menu,index=len(menu['menu']))
def find_item_after(menu,index=-1):
for item in menu['menu'][index+1:]:
if item['enabled']:
return menu['menu'].index(item)
return find_item_after(menu)
def get_menu_index_by_key(menu, key):
menu = get_menu(menu)
_i = 0
for entry in menu['menu']:
if entry['key'] == key:
return _i
_i += 1
return -1
def get_menu_index_by_flag(menu, flag, value):
menu = get_menu(menu)
_i = 0
for entry in menu['menu']:
if entry[flag] == value:
return _i
_i += 1
return -1
def go_to_menu_index(menu, index):
get_menu(menu)['index'] = index
if get_menu(menu)['on_move']:
_entry = get_selected_item(menu, index)
return get_menu(menu)['on_move'](_entry)
def move_up(menu, index):
menu['index'] = find_item_before(menu, index=index)
if menu['on_move']:
_entry = get_selected_item(MENUS.index(menu), menu['index'])
return menu['on_move'](_entry)
def move_down(menu, index):
menu['index'] = find_item_after(menu, index=index)
if menu['on_move']:
_entry = get_selected_item(MENUS.index(menu), menu['index'])
return menu['on_move'](_entry)
def previous_item(menu,index):
if menu['menu'][index]['value']:
menu['menu'][index]['value']-=1
redraw_menu(menu)
def next_item(menu,index):
if menu['menu'][index]['value']<len(menu['menu'][index]['values'])-1:
menu['menu'][index]['value']+=1
redraw_menu(menu)
def get_selected_item(menu,index):
menu = get_menu(menu)
_entry = menu['menu'][index]
return _entry
def item_selected(menu_id, index):
_entry = get_selected_item(menu_id, index)
_menu = get_menu(menu_id)
if _menu['close_on_select']:
delete_menu(menu_id)
if _menu['on_select']:
return _menu['on_select'](_entry)
return False
def item_changed(menu,index):
_entry = get_selected_item(menu,index)
menu = get_menu(menu)
if menu['on_change']:
return menu['on_change'](_entry)
else:
return False
def is_getting_input(menu_id):
_item = get_selected_item(menu_id, MENUS[menu_id]['index'])
if _item['type'] == 'input':
return _item
return False
def is_any_menu_getting_input():
for menu_id in [MENUS.index(m) for m in MENUS]:
_item = is_getting_input(menu_id)
if _item:
return _item
return False
def _create_target_list(target_list):
_menu_items = []
_near_targets = []
_group_targets = []
if LIFE[SETTINGS['controlling']]['group']:
_group = alife.groups.get_group(LIFE[SETTINGS['controlling']], LIFE[SETTINGS['controlling']]['group'])
else:
_group = None
for target_id in target_list:
if LIFE[target_id]['dead'] or target_id == SETTINGS['controlling']:
continue
if target_id in LIFE[SETTINGS['controlling']]['seen']:
_near_targets.append(target_id)
if _group and target_id in _group['members']:
_group_targets.append(target_id)
if _near_targets:
_menu_items.append(create_item('title', 'Near', None))
for target_id in _near_targets:
if not _menu_items:
SETTINGS['following'] = target_id
_color = life.draw_life_icon(LIFE[target_id])[1]
_menu_items.append(create_item('single',
' '.join(LIFE[target_id]['name']),
None,
target=target_id,
color=(_color, tcod.color_lerp(_color, tcod.white, 0.5))))
if _group_targets:
_menu_items.append(create_item('title', 'Group', None))
for target_id in _group_targets:
if not _menu_items:
SETTINGS['following'] = target_id
_color = life.draw_life_icon(LIFE[target_id])[1]
_menu_items.append(create_item('single',
' '.join(LIFE[target_id]['name']),
None,
target=target_id,
color=(_color, tcod.color_lerp(_color, tcod.white, 0.5))))
if not target_list:
return []
_menu_items.append(create_item('title', 'All', None))
for target_id in target_list:
if target_id == SETTINGS['controlling']:
continue
if not _menu_items:
SETTINGS['following'] = target_id
_color = life.draw_life_icon(LIFE[target_id])[1]
_menu_items.append(create_item('single',
' '.join(LIFE[target_id]['name']),
None,
target=target_id,
color=(_color, tcod.color_lerp(_color, tcod.white, 0.5))))
return _menu_items
def create_target_list():
return _create_target_list(LIFE[SETTINGS['controlling']]['seen'])