Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/setup/menus.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/setup/menus.py')
-rw-r--r--src/setup/menus.py191
1 files changed, 191 insertions, 0 deletions
diff --git a/src/setup/menus.py b/src/setup/menus.py
new file mode 100644
index 0000000..e229277
--- /dev/null
+++ b/src/setup/menus.py
@@ -0,0 +1,191 @@
+'''
+
+'''
+
+import curses
+import config
+from utils import strs
+
+# setup logging
+import logging
+logging.basicConfig(filename=strs.LoggingFile,level=logging.DEBUG)
+log = logging.getLogger('setup.menus')
+
+# -----------------------------------------------------------------------------
+#
+# -----------------------------------------------------------------------------
+
+class MenuData(object):
+ '''Holds data used by the menu system'''
+
+ def __init__(self):
+ '''
+ Constructor
+ '''
+ self.levels = []
+ self.profiles = []
+ self.profile = None
+ self.screen = None
+
+
+# -----------------------------------------------------------------------------
+# Menu display functions
+# -----------------------------------------------------------------------------
+
+# Warning: the menu system uses recursion to move about. Excessive bad input (tested to crash at 979 bad inputs to the main menu) or moving between menus will case a stack overflow. When the menu system exits (such as when actually playing a game), we need to make sure all of the menu's function calls unwind
+# TODO: improve the menu system so it doesn't use recursion
+
+def mainMenu(data):
+ ''' '''
+
+ # we use the current profile, so make sure there is one
+ if (data.profile == None) | (data.profile == ''):
+ changePlayerMenu(data)
+ return
+
+ # configure the screen for our needs
+ data.screen.erase()
+ curses.cbreak()
+ curses.echo()
+
+ # display the title
+ title = strs.Title_Main + strs.Title_Seperator + (data.profile or '[No Player]')
+ displayTitle(data.screen, title)
+
+ # display the menu options
+ lines = ('1) Play Game',
+ '2) Join Game - Stubbed',
+ '3) Options - Stubbed',
+ '',
+ 'c) Change Player',
+ 'e) Exit')
+ displayOptions(data.screen, lines, 1)
+
+ # display the input prompt
+ lines = ('', 'Pick [1,c,e]: ')
+ displayPrompt(data.screen, lines, 7)
+
+ # refresh the screen
+ data.screen.refresh()
+
+ # store the current cursor location, so we can keep it here when the user types
+ y, x = data.screen.getyx()
+ log.debug('Y,X are at: ' + str(y) + ' ' + str(x))
+
+ # wait for user input
+ c = data.screen.getch()
+
+ # keep the cursor in the same spot
+ data.screen.move(y, x)
+
+ # process input
+ if c == ord('1'): playGameMenu(data)
+ #elif c == ord('2'): curses.beep()
+ #elif c == ord('3'): curses.beep()
+ elif c == ord('c'): changePlayerMenu(data)
+ elif c == ord('e'): pass
+ else:
+ mainMenu(data)
+
+def changePlayerMenu(data):
+ ''' '''
+
+ # create a list of players
+ profiles = config.getProfiles()
+ players = []
+ for profile in profiles:
+ players.append(profile.getName())
+
+ # configure the screen for our needs
+ data.screen.erase()
+ curses.nocbreak()
+ curses.echo()
+
+ # display the title
+ title = strs.Title_ChangePlayer + strs.Title_Seperator + (data.profile or '[No Player]')
+ displayTitle(data.screen, title)
+
+ # display the menu options
+ lines = ['', 'Known Players:']
+ for player in players:
+ lines.append(' * ' + player)
+ displayOptions(data.screen, lines, 1)
+
+ # display the input prompt
+ offset = len(lines) + 1
+ lines = ('','Enter your player\'s name: ')
+ displayPrompt(data.screen, lines, offset)
+
+ # refresh and wait for valid user input
+ data.screen.refresh()
+ name = data.screen.getstr()
+
+ # process input
+ # Note: if the player's profile doesn't exist, it will be created whenever the player does something that needs to be saved to a profile
+ data.profile = name
+
+ # always head back to the main menu after changing the profile
+ mainMenu(data)
+
+def playGameMenu(data):
+ ''' '''
+
+ # check for required vars
+ # clear the screen and display the title
+ # display the menu
+ # refresh the screen
+ # wait for input
+ # process input
+
+ pass
+
+def levelOptionsMenu(data):
+ ''' '''
+ pass
+
+def skillLevelMenu(data):
+ ''' '''
+ pass
+
+def numberOfPlayersMenu(data):
+ ''' '''
+ pass
+
+# -----------------------------------------------------------------------------
+# Common functions
+# -----------------------------------------------------------------------------
+
+def displayTitle(screen, title):
+ '''Displays the given title as the title of the given stdscr'''
+ screen.addstr(0,strs.Indent_Title, title, curses.A_REVERSE)
+
+def displayOptions(screen, options, offset=0):
+ '''Displays the given list of options on the given stdscr with the given vertical offset'''
+ for index, line in enumerate(options):
+ screen.addstr(index+1+offset,strs.Indent_Options, line)
+
+def displayPrompt(screen, lines, offset=0):
+ '''Displays the given list of textual prompt lines on the given stdscr with the given vertical offset'''
+ for index, line in enumerate(lines):
+ screen.addstr(index+1+offset,strs.Indent_Prompt, line)
+
+# -----------------------------------------------------------------------------
+# Menu setup functions
+# -----------------------------------------------------------------------------
+
+def init(stdscr):
+ ''' '''
+
+ # create the menu's data
+ data = MenuData()
+ data.screen = stdscr
+
+ # set the last used values
+ data.profile = config.getLastProfileName()
+
+ mainMenu(data)
+
+def begin():
+ ''' '''
+ curses.wrapper(init)
+