Mercurial > pyntnclick
changeset 58:9048cf43f613
Popup menu
author | Neil Muller <neil@dip.sun.ac.za> |
---|---|
date | Mon, 23 Aug 2010 15:51:27 +0200 |
parents | 4f9d412d83db |
children | 23d2c58369fc |
files | gamelib/gamescreen.py gamelib/popupmenu.py |
diffstat | 2 files changed, 41 insertions(+), 3 deletions(-) [+] |
line wrap: on
line diff
--- a/gamelib/gamescreen.py Mon Aug 23 14:01:00 2010 +0200 +++ b/gamelib/gamescreen.py Mon Aug 23 15:51:27 2010 +0200 @@ -4,6 +4,7 @@ from state import initial_state, Item from hand import HandButton +from popupmenu import PopupMenu from constants import BUTTON_SIZE from pygame.color import Color @@ -72,7 +73,8 @@ Button('Use titanium_leg', action = lambda: self.state.scenes['cryo'].things['cryo.door'].interact(self.state.items['titanium_leg'])), ], align='l', spacing=20) self.add_centered(menu) - self.menubutton = Button('Menu', action=self.main_menu) + self.popup_menu = PopupMenu(shell) + self.menubutton = Button('Menu', action=self.popup_menu.show_menu) self.menubutton.font = get_font(16, 'Vera.ttf') self.menubutton.set_rect(Rect(0, 0, BUTTON_SIZE, BUTTON_SIZE)) self.menubutton.bottomleft = self.bottomleft @@ -92,8 +94,14 @@ self.state.add_inventory_item('triangle') self.state.add_inventory_item('titanium_leg') - def main_menu(self): - print 'Returning to menu' + # Albow uses magic method names (command + '_cmd'). Yay. + # Albow's search order means they need to be defined here, not in + # PopMenu, which is annoying. + def hide_cmd(self): + # This option does nothing, but the method needs to exist for albow + return + + def main_menu_cmd(self): self.shell.show_screen(self.shell.menu_screen) def add_item(self):
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gamelib/popupmenu.py Mon Aug 23 15:51:27 2010 +0200 @@ -0,0 +1,30 @@ +# popmenu.py +# Copyright Boomslang team (see COPYING file) +# Popup menu for the game screen + +from constants import BUTTON_SIZE + +from albow.menu import Menu +from albow.resource import get_font + +class PopupMenu(Menu): + + def __init__(self, shell): + self.shell = shell + items = [ + ('Resume Game', 'hide'), + ('Exit to Main Menu', 'main_menu'), + ] + # albow.menu.Menu ignores title string + Menu.__init__(self, None, items) + self.font = get_font(16, 'Vera.ttf') + + def show_menu(self): + """Call present, with the correct position""" + item_height = self.font.get_linesize() + menu_top = 600 - (len(self.items) * item_height + BUTTON_SIZE) + item = self.present(self.shell, (0, menu_top)) + if item > -1: + # A menu item needs to be invoked + self.invoke_item(item) +