-
Notifications
You must be signed in to change notification settings - Fork 0
/
runAppWithScreens.py
80 lines (68 loc) · 3.08 KB
/
runAppWithScreens.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
try: from cmu_cs3_graphics import *
except: from cmu_graphics import *
import inspect
##################################################################
# runAppWithScreens() and setActiveScreen(screen)
##################################################################
def runAppWithScreens(initialScreen, *args, **kwargs):
global _callerGlobals
_callerGlobals = inspect.stack()[1][0].f_globals
appFnNames = ['onAppStart',
'onKeyPress', 'onKeyHold', 'onKeyRelease',
'onMousePress', 'onMouseDrag', 'onMouseRelease',
'onMouseMove', 'onStep', 'redrawAll']
def checkForAppFns():
globalVars = _callerGlobals # globals()
for appFnName in appFnNames:
if appFnName in globalVars:
raise Exception(f'Do not define {appFnName} when using screens')
def getScreenFnNames(appFnName):
globalVars = _callerGlobals # globals()
screenFnNames = [ ]
for globalVarName in globalVars:
screenAppSuffix = f'_{appFnName}'
if globalVarName.endswith(screenAppSuffix):
screenFnNames.append(globalVarName)
return screenFnNames
def wrapScreenFns():
globalVars = _callerGlobals # globals()
for appFnName in appFnNames:
screenFnNames = getScreenFnNames(appFnName)
if (screenFnNames != [ ]) or (appFnName == 'onAppStart'):
globalVars[appFnName] = makeAppFnWrapper(appFnName)
def makeAppFnWrapper(appFnName):
if appFnName == 'onAppStart':
def onAppStartWrapper(app):
globalVars = _callerGlobals # globals()
for screenFnName in getScreenFnNames('onScreenStart'):
screenFn = globalVars[screenFnName]
screenFn(app)
return onAppStartWrapper
else:
def appFnWrapper(*args):
globalVars = _callerGlobals # globals()
screen = globalVars['_activeScreen']
wrappedFnName = ('onScreenStart'
if appFnName == 'onAppStart' else appFnName)
screenFnName = f'{screen}_{wrappedFnName}'
if screenFnName in globalVars:
screenFn = globalVars[screenFnName]
return screenFn(*args)
return appFnWrapper
def go():
checkForAppFns()
wrapScreenFns()
setActiveScreen(initialScreen)
runApp(*args, **kwargs)
go()
def setActiveScreen(screen):
globalVars = _callerGlobals # globals()
if (screen in [None, '']) or (not isinstance(screen, str)):
raise Exception(f'{repr(screen)} is not a valid screen')
redrawAllFnName = f'{screen}_redrawAll'
if redrawAllFnName not in globalVars:
raise Exception(f'Screen {screen} requires {redrawAllFnName}()')
globalVars['_activeScreen'] = screen
##################################################################
# end of runAppWithScreens() and setActiveScreen(screen)
##################################################################