annotate pyntnclick/menuscreen.py @ 813:3a875256f795 pyntnclick

better visible handling
author Neil Muller <neil@dip.sun.ac.za>
date Sun, 27 Jan 2013 17:33:04 +0200
parents bcc9277a23e6
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
793
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
1 # menu.py
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
2 # Copyright Boomslang team, 2010 (see COPYING File)
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
3 # Main menu for the game
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
4
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
5 import pygame.event
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
6 from pygame.locals import QUIT
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
7 from pyntnclick.engine import Screen
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
8 from pyntnclick.widgets.imagebutton import ImageButtonWidget
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
9 from pyntnclick.widgets.text import TextButton
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
10
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
11
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
12 class MenuScreen(Screen):
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
13 BACKGROUND_IMAGE = None
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
14
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
15 def setup(self):
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
16 self._background = None
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
17 if self.BACKGROUND_IMAGE is not None:
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
18 self._background = self.resource.get_image(self.BACKGROUND_IMAGE)
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
19
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
20 self._add_new_game_button()
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
21 self._add_load_game_button()
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
22 self._add_save_game_button()
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
23 self._add_resume_game_button()
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
24 self._add_quit_button()
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
25
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
26 def on_enter(self):
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
27 super(MenuScreen, self).on_enter()
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
28 running = self.check_running()
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
29 self.set_button_state(self._resume_game_button, running)
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
30 self.set_button_state(self._load_game_button, self.check_has_saves())
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
31 self.set_button_state(self._save_game_button, running)
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
32
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
33 def set_button_state(self, button, enabled):
813
3a875256f795 better visible handling
Neil Muller <neil@dip.sun.ac.za>
parents: 803
diff changeset
34 button.set_visible(enabled)
793
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
35 if enabled:
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
36 button.enable()
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
37 else:
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
38 button.disable()
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
39
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
40 def make_new_game_button(self):
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
41 "Override this to customise the new game button."
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
42 return self.make_text_button((200, 100), 'New game')
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
43
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
44 def make_load_game_button(self):
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
45 "Override this to customise the load game button."
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
46 return self.make_text_button((200, 200), 'Load game')
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
47
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
48 def make_save_game_button(self):
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
49 "Override this to customise the save game button."
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
50 return self.make_text_button((200, 300), 'Save game')
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
51
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
52 def make_resume_button(self):
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
53 "Override this to customise the resume game button."
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
54 return self.make_text_button((200, 400), 'Resume')
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
55
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
56 def make_quit_button(self):
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
57 "Override this to customise the quit button."
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
58 return self.make_text_button((200, 500), 'Quit')
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
59
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
60 def _add_new_game_button(self):
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
61 self._new_game_button = self.make_new_game_button()
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
62 self._new_game_button.add_callback('clicked', self.new_game)
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
63
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
64 def _add_load_game_button(self):
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
65 self._load_game_button = self.make_load_game_button()
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
66 self._load_game_button.add_callback('clicked', self.load_game)
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
67
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
68 def _add_save_game_button(self):
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
69 self._save_game_button = self.make_save_game_button()
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
70 self._save_game_button.add_callback('clicked', self.save_game)
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
71
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
72 def _add_resume_game_button(self):
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
73 self._resume_game_button = self.make_resume_game_button()
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
74 self._resume_game_button.add_callback('clicked', self.resume_game)
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
75
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
76 def _add_quit_button(self):
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
77 self._quit_button = self.make_quit_button()
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
78 self._quit_button.add_callback('clicked', self.quit)
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
79
803
bcc9277a23e6 Refactor widget positioning API. Remove unused widgets
Stefano Rivera <stefano@rivera.za.net>
parents: 793
diff changeset
80 def make_text_button(self, pos, text):
bcc9277a23e6 Refactor widget positioning API. Remove unused widgets
Stefano Rivera <stefano@rivera.za.net>
parents: 793
diff changeset
81 widget = TextButton(pos, self.gd, text)
793
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
82 self.container.add(widget)
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
83 return widget
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
84
803
bcc9277a23e6 Refactor widget positioning API. Remove unused widgets
Stefano Rivera <stefano@rivera.za.net>
parents: 793
diff changeset
85 def make_image_button(self, pos, image_name):
793
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
86 image = self.resource.get_image(image_name)
803
bcc9277a23e6 Refactor widget positioning API. Remove unused widgets
Stefano Rivera <stefano@rivera.za.net>
parents: 793
diff changeset
87 widget = ImageButtonWidget(pos, self.gd, image)
793
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
88 self.container.add(widget)
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
89 return widget
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
90
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
91 def draw_background(self):
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
92 if self._background is not None:
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
93 self.surface.blit(self._background, self.surface.get_rect())
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
94
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
95 def new_game(self, ev, widget):
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
96 self.screen_event('game', 'restart')
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
97 self.change_screen('game')
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
98
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
99 def load_game(self, ev, widget):
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
100 self.screen_event('game', 'load')
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
101 self.change_screen('game')
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
102
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
103 def save_game(self, ev, widget):
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
104 self.screen_event('game', 'save')
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
105
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
106 def check_running(self):
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
107 return self.gd.running
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
108
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
109 def check_has_saves(self):
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
110 import os.path
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
111 save_dir = self.gd.get_default_save_location()
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
112 return os.path.exists(
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
113 self.gd.game_state_class().get_save_fn(save_dir, 'savegame'))
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
114
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
115 def resume_game(self, ev, widget):
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
116 self.change_screen('game')
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
117
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
118 def quit(self, ev, widget):
0e5b80b3128c Putting the new MenuScreen in the repo might be a good idea. I should totally do that.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
119 pygame.event.post(pygame.event.Event(QUIT))