preamble
[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, linebreaks=True):
22 config = self.game.configuration
23 if option is None and section is None:
24 return config
25 if option and section:
26 rvalue = config.get(section, option)
27 if not linebreaks and isinstance(rvalue, str):
28 rvalue = rvalue.replace("\n", " ")
29 return rvalue
30 return config.get_section(section)
31
32 def get_input(self):
33 return self.game.input
34
35 def get_screen(self):
36 return self.game.display.get_screen()
37
38 def get_display_surface(self):
39 current = self
40 attribute = "display_surface"
41 while not isinstance(current, Game.Game):
42 if hasattr(current, attribute):
43 return getattr(current, attribute)
44 current = current.parent
45 return current.display.get_screen()
46
47 def get_audio(self):
48 return self.game.audio
49
50 def get_delegate(self):
51 return self.game.delegate
52
53 def get_resource(self, path_or_section, option=None):
54 config = self.get_configuration()
55 rel_path = path_or_section
56 if option is not None:
57 rel_path = config.get(path_or_section, option)
58 if rel_path:
59 for root in config.get("setup", "resource-search-path"):
60 if self.is_shared_mode() and not self.is_absolute_path(root):
61 continue
62 path = join(root, rel_path)
63 if exists(path):
64 return path
65 self.print_debug("Couldn't find resource: {0} {1}".\
66 format(path_or_section, option))
67
68 def is_shared_mode(self):
69 return self.check_command_line("s")
70
71 def check_command_line(self, flag):
72 return "-" + flag in argv
73
74 def print_debug(self, statement):
75 if self.is_debug_mode():
76 print statement
77
78 def is_debug_mode(self):
79 return self.check_command_line("d")
80
81 def is_absolute_path(self, path):
82 return normpath(path) == abspath(path)
83
84 def subscribe(self, callback, kind=None):
85 self.game.delegate.add_subscriber(callback, kind)
86
87 def unsubscribe(self, callback, kind=None):
88 self.game.delegate.remove_subscriber(callback, kind)