pgfw/pgfw/GameChild.py

95 lines
2.9 KiB
Python
Raw Normal View History

from os.path import exists, join, basename, normpath, abspath, commonprefix, sep
2019-04-12 03:08:07 -04:00
from sys import argv, version_info
2012-07-05 04:21:49 -04:00
from pygame import mixer, event, time
2012-07-07 08:34:49 -04:00
from pygame.locals import *
2012-07-05 04:21:49 -04:00
2019-04-12 03:08:07 -04:00
if version_info[0] >= 3:
from . import Game
else:
import Game
2012-07-05 04:21:49 -04:00
class GameChild:
def __init__(self, parent=None):
self.parent = parent
2013-04-14 10:46:43 -04:00
self.game = self.get_game()
2012-07-05 04:21:49 -04:00
def get_game(self):
current = self
while not isinstance(current, Game.Game):
current = current.parent
return current
2014-09-13 01:05:42 -04:00
def get_configuration(self, section=None, option=None, linebreaks=True):
2013-04-14 10:46:43 -04:00
config = self.game.configuration
2014-09-13 01:05:42 -04:00
if option is None and section is None:
return config
elif option and section:
if config.has_option(section, option):
rvalue = config.get(section, option)
if not linebreaks and isinstance(rvalue, str):
rvalue = rvalue.replace("\n", " ")
return rvalue
elif option is None:
if config.has_section(section):
return config.get_section(section)
else:
return {}
2012-07-05 04:21:49 -04:00
def get_input(self):
2013-04-14 10:46:43 -04:00
return self.game.input
2012-07-05 04:21:49 -04:00
def get_screen(self):
2013-04-14 10:46:43 -04:00
return self.game.display.get_screen()
2012-07-05 04:21:49 -04:00
2013-03-30 06:18:04 -04:00
def get_display_surface(self):
current = self
attribute = "display_surface"
while not isinstance(current, Game.Game):
if hasattr(current, attribute):
return getattr(current, attribute)
current = current.parent
return current.display.get_screen()
2012-07-05 04:21:49 -04:00
def get_audio(self):
2013-04-14 10:46:43 -04:00
return self.game.audio
2012-07-05 04:21:49 -04:00
def get_delegate(self):
2013-04-14 10:46:43 -04:00
return self.game.delegate
2012-07-05 04:21:49 -04:00
2013-06-13 11:05:01 -04:00
def get_resource(self, path_or_section, option=None):
2012-07-05 04:21:49 -04:00
config = self.get_configuration()
2013-06-13 11:05:01 -04:00
rel_path = path_or_section
if option is not None:
rel_path = config.get(path_or_section, option)
2013-04-27 07:21:52 -04:00
if rel_path:
for root in config.get("setup", "resource-search-path"):
if self.is_shared_mode() and not self.is_absolute_path(root):
continue
path = join(root, rel_path)
if exists(path):
return normpath(path)
2013-03-05 08:23:17 -05:00
def is_shared_mode(self):
2013-04-14 05:19:43 -04:00
return self.check_command_line("s")
def check_command_line(self, flag):
return "-" + flag in argv
2013-03-05 08:23:17 -05:00
2012-07-07 08:34:49 -04:00
def print_debug(self, statement):
if self.is_debug_mode():
2019-04-12 03:08:07 -04:00
print(statement)
2013-03-05 08:23:17 -05:00
def is_debug_mode(self):
2013-04-14 05:19:43 -04:00
return self.check_command_line("d")
2013-03-05 08:23:17 -05:00
def is_absolute_path(self, path):
return normpath(path) == abspath(path)
2013-03-05 08:23:17 -05:00
def subscribe(self, callback, kind=None):
2013-04-14 10:46:43 -04:00
self.game.delegate.add_subscriber(callback, kind)
2012-07-05 04:21:49 -04:00
2013-03-05 08:23:17 -05:00
def unsubscribe(self, callback, kind=None):
2013-04-14 10:46:43 -04:00
self.game.delegate.remove_subscriber(callback, kind)