bg
[sso] / shaken_and_spit_out / pgfw / GameChild.py
1 from os.path import exists, join, basename, normpath, abspath
2 from sys import argv
3
4 from pygame import mixer, event, time
5 from pygame.locals import *
6
7 import Game
8
9 class GameChild:
10
11 def __init__(self, parent=None):
12 self.parent = parent
13 self.game = self.get_game()
14
15 def get_game(self):
16 current = self
17 while not isinstance(current, Game.Game):
18 current = current.parent
19 return current
20
21 def get_configuration(self, section=None, option=None):
22 config = self.game.configuration
23 if option and section:
24 return config.get(section, option)
25 if section:
26 return config.get_section(section)
27 return config
28
29 def get_input(self):
30 return self.game.input
31
32 def get_screen(self):
33 return self.game.display.get_screen()
34
35 def get_display_surface(self):
36 current = self
37 attribute = "display_surface"
38 while not isinstance(current, Game.Game):
39 if hasattr(current, attribute):
40 return getattr(current, attribute)
41 current = current.parent
42 return current.display.get_screen()
43
44 def get_audio(self):
45 return self.game.audio
46
47 def get_delegate(self):
48 return self.game.delegate
49
50 def get_resource(self, path_or_section, option=None):
51 config = self.get_configuration()
52 rel_path = path_or_section
53 if option is not None:
54 rel_path = config.get(path_or_section, option)
55 if rel_path:
56 for root in config.get("setup", "resource-search-path"):
57 if self.is_shared_mode() and not self.is_absolute_path(root):
58 continue
59 path = join(root, rel_path)
60 if exists(path):
61 return path
62 self.print_debug("Couldn't find resource: {0} {1}".\
63 format(path_or_section, option))
64
65 def is_shared_mode(self):
66 return self.check_command_line("s")
67
68 def check_command_line(self, flag):
69 return "-" + flag in argv
70
71 def print_debug(self, statement):
72 if self.is_debug_mode():
73 print statement
74
75 def is_debug_mode(self):
76 return self.check_command_line("d")
77
78 def is_absolute_path(self, path):
79 return normpath(path) == abspath(path)
80
81 def subscribe(self, callback, kind=None):
82 self.game.delegate.add_subscriber(callback, kind)
83
84 def unsubscribe(self, callback, kind=None):
85 self.game.delegate.remove_subscriber(callback, kind)