-
Notifications
You must be signed in to change notification settings - Fork 0
/
Helper.py
60 lines (45 loc) · 1.54 KB
/
Helper.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
import curses
#-- Define the appearance of some interface elements
hotkey_attr = curses.A_BOLD | curses.A_UNDERLINE
menu_attr = curses.A_NORMAL
heightStatScreen=7
MINIMUM_HEIGHT=34
MINIMUM_WIDTH=52
def createMenu(screen, menus, startX, startY):
for menu in menus:
menu_name = menu[0]
menu_hotkey = menu_name[0]
menu_no_hot = menu_name[1:]
screen.addstr(startY, startX, menu_hotkey, hotkey_attr)
screen.addstr(startY, startX+1, menu_no_hot, menu_attr)
startY+=1
# Add key handlers for this hotkey
menu_handler(screen, (str.upper(menu_hotkey), menu[1]))
menu_handler(screen, (str.lower(menu_hotkey), menu[1]))
screen.refresh()
#-- Magic key handler both loads and processes keys strokes
def menu_handler(screen, key_assign=None, key_dict={}):
if key_assign:
key_dict[ord(key_assign[0])] = key_assign[1]
else:
c = screen.getch()
if c in (curses.KEY_END, ord('!')):
return 0
elif c not in key_dict.keys():
curses.beep()
return 1
else:
return key_dict[c]()
def checkIfEnoughSpaceForGame(stdscr, gameScreen = None):
maxY, maxX = stdscr.getmaxyx()
if (maxX < MINIMUM_WIDTH or maxY < MINIMUM_HEIGHT):
return True
if gameScreen==None:
return False
return maxY <= (gameScreen.sizeY + heightStatScreen + 1) or maxX <= (gameScreen.sizeX)
class NotEnoughSpace(Exception):
pass
class ExitTotal(Exception):
pass
class GoBackToMainScreen(Exception):
pass