def load_game_defaults(): cfg = GlobalConf().path('game.player0.keys') for act, key in DEFAULT_PLAYER0_KEYS: if check_duplicate_key(cfg, act, key): cfg.child(act).default(key) GlobalConf().path('game.shader').default(True)
class Keyboard(object): def __init__(self, state=None, *a, **k): super(Keyboard, self).__init__(*a, **k) if state: self.state = state self.active = False self.cfg = GlobalConf().path('game.player0.keys') self.keys_txt = { 'on_move_forward': (1, "Forward"), 'on_move_backward': (2, "Backward"), 'on_strafe_left': (3, "Strafe left"), 'on_strafe_right': (4, "Strafe right"), 'on_steer_left': (5, "Steer left"), 'on_steer_right': (6, "Steer right"), 'on_throw_weapon': (7, "Throw weapon"), 'on_place_stick': (8, "Place stick"), 'on_feed': (9, "Feed"), 'on_jump': (10, "Jump"), 'on_run': (11, "Run"), 'on_hit': (12, "Hit") } def get_key_name(self, event): if event is None or event == 'no-event': return "?" else: return event[6:] def do_paint(self): tx_scale = (0.6, 0.6) init = -0.15 dif = -0.06 self.keys_btn = {} self.keys_lab = {} for func, (i, bt_text) in self.keys_txt.iteritems(): self.keys_btn[func] = DirectButton( text=bt_text, text_font=self.state.font, text_scale=tx_scale, text_align=TextNode.ARight, scale=.1, pos=(0.3, 0, init + dif * i), relief=None, command=lambda func=func: self.det_key(func)) self.keys_lab[func] = OnscreenText(text=self.get_key_name( self.cfg.child(func).value), font=self.state.font, align=TextNode.ALeft, pos=(0.7, init + dif * i), scale=0.07) self.cfg.child(func).on_conf_change += (self.on_key_change) self.info_txt = OnscreenText(text='Select action', font=self.state.font, pos=(0.4, init + dif * 13 - 0.02), scale=0.04) self.active = True def do_enable(self): if self.active: for n in self.keys_btn.itervalues(): n.setProp('state', DGG.NORMAL) def do_disable(self): if self.active: for n in self.keys_btn.itervalues(): n.setProp('state', DGG.DISABLED) def do_destroy(self): for n in self.keys_btn.itervalues(): n.destroy() for n in self.keys_lab.itervalues(): n.destroy() self.info_txt.destroy() self.active = False self.on_key_change.disconnect_sources() def det_key(self, key): # Deactivate all buttons self.state.do_disable() self.keys_lab[key].setText('?') self.info_txt.setText('Click any key to config') self.slot = self.state.events.on_any_event.connect( lambda ev, *a, **k: self.get_key(ev, key)) def get_key(self, ev, key): if is_key_event(ev): self.state.do_enable() self.state.events.on_any_event -= self.slot for c in self.cfg.childs(): if c.value == ev: c.set_value('no-event') self.cfg.child(key).set_value(ev) @slot def on_key_change(self, cfg): if self.keys_lab[cfg.name]: self.keys_lab[cfg.name].setText(self.get_key_name(cfg.value)) self.info_txt.setText('Select button')
class Screen (object): def __init__(self, state = None, *a, **k): super (Screen, self).__init__ (*a, **k) if state: self.state = state self.active = False self.cfg_p = GlobalConf().child ('panda') self.cfg_g = GlobalConf().child ('game') def do_paint (self): # Get possible screen resolutions NOT AVAILABLE IN OSX # di = base.pipe.getDisplayInformation() # for index in range(di.getTotalDisplayModes()): # sizes += ( str(di.getDisplayModeWidth(index)), # str(di.getDisplayModeHeight(index)) # ) actual_full = self.cfg_p.child ('fullscreen').value actual_width = self.cfg_p.child ('width').value actual_height = self.cfg_p.child ('height').value self.active = True self.sizes = [ (640, 480), (800, 600), (1024, 768), (1280, 800), (1280, 1024), (1440, 900) ] self.sizes_tx = [] for h, w in self.sizes: self.sizes_tx += [str (h)+"x"+str (w)] # Gets the position of the actual config from self.sizes init_item = self.sizes.index ((actual_width, actual_height)) # # TO DO: add images to check buttos # # Create a frame tx_scale = (0.9, 0.9) self.res_txt = OnscreenText (text = "Resolution", font = self.state.font, align = TextNode.ALeft, pos = (-0.1, -0.4) ) self.res_menu = DirectOptionMenu (text = "Resolution", highlightScale = tx_scale, text_font = self.state.font, text_scale = tx_scale, item_text_font = self.state.font, item_text_scale = tx_scale, scale = 0.08, items = self.sizes_tx, initialitem = init_item, highlightColor = (0.65,0.65,0.65,1), command = self.change_res, pos = (0.5, 0, -0.4) ) #checked = loader.loadModel ('menu/checked.egg') #unchecked = loader.loadModel ('menu/unchecked.egg') self.full_screen = DirectCheckButton (text = "Full screen", text_font = self.state.font, text_scale = tx_scale, #text_pos = (0.5, 0, -0.7), text_align = TextNode.ALeft, indicatorValue = self.cfg_p.child ('fullscreen').value, scale = .08, relief = None, boxPlacement = 'right', boxRelief = None, #boxImage = (unchecked, checked , None), #boxImageScale = 0.15, command = self.change_full, pos = (-0.1, 0, -0.6) ) self.fps_display = DirectCheckButton (text = "FPS display", text_font = self.state.font, text_scale = tx_scale, text_align = TextNode.ALeft, indicatorValue = self.cfg_p.child ('frame-meter').value, scale = .08, relief = None, boxPlacement = 'right', boxRelief = None, command = self.change_fps, pos = (-0.1, 0, -0.75) ) self.sha_display = DirectCheckButton (text = "Shaders", text_font = self.state.font, text_scale = tx_scale, text_align = TextNode.ALeft, indicatorValue = self.cfg_g.child ('shader').value, scale = .08, relief = None, boxPlacement = 'right', boxRelief = None, command = self.change_shad, pos = (-0.1, 0, -0.9) ) def do_enable (self): pass def do_disable (self): pass def do_destroy (self): self.res_txt.destroy () self.res_menu.destroy () self.fps_display.destroy () self.full_screen.destroy () self.sha_display.destroy () self.active = False def change_shad (self, status): self.cfg_g.child ('shader').set_value (bool(status)) def change_fps (self, status): self.cfg_p.child ('frame-meter').set_value (bool(status)) def change_full (self, status): self.cfg_p.child ('fullscreen').set_value (bool(status)) self.cfg_p.nudge () def change_res (self, arg): i = self.sizes_tx.index(arg) self.cfg_p.child ('width').set_value (self.sizes[i][0]) self.cfg_p.child ('height').set_value (self.sizes[i][1]) self.cfg_p.nudge ()
class Keyboard (object): def __init__(self, state = None, *a, **k): super (Keyboard, self).__init__ (*a, **k) if state: self.state = state self.active = False self.cfg = GlobalConf ().path ('game.player0.keys') self.keys_txt = { 'on_move_forward' : (1, "Forward"), 'on_move_backward' : (2, "Backward"), 'on_strafe_left' : (3, "Strafe left"), 'on_strafe_right' : (4, "Strafe right"), 'on_steer_left' : (5, "Steer left"), 'on_steer_right' : (6, "Steer right"), 'on_throw_weapon' : (7, "Throw weapon"), 'on_place_stick' : (8, "Place stick"), 'on_feed' : (9, "Feed"), 'on_jump' : (10, "Jump"), 'on_run' : (11, "Run"), 'on_hit' : (12, "Hit") } def get_key_name (self, event): if event is None or event == 'no-event': return "?" else: return event [6:] def do_paint (self): tx_scale = (0.6, 0.6) init = -0.15 dif = -0.06 self.keys_btn = {} self.keys_lab = {} for func, (i, bt_text) in self.keys_txt.iteritems (): self.keys_btn [func] = DirectButton( text = bt_text, text_font = self.state.font, text_scale = tx_scale, text_align = TextNode.ARight, scale = .1, pos = (0.3, 0, init + dif*i), relief = None, command = lambda func=func: self.det_key (func) ) self.keys_lab [func] = OnscreenText(text = self.get_key_name ( self.cfg.child (func).value), font = self.state.font, align = TextNode.ALeft, pos = (0.7, init+dif*i), scale = 0.07 ) self.cfg.child (func).on_conf_change += (self.on_key_change) self.info_txt = OnscreenText (text = 'Select action', font = self.state.font, pos = (0.4, init+dif*13-0.02), scale = 0.04 ) self.active = True def do_enable (self): if self.active: for n in self.keys_btn.itervalues (): n.setProp ('state', DGG.NORMAL) def do_disable (self): if self.active: for n in self.keys_btn.itervalues (): n.setProp ('state', DGG.DISABLED) def do_destroy (self): for n in self.keys_btn.itervalues (): n.destroy () for n in self.keys_lab.itervalues (): n.destroy () self.info_txt.destroy() self.active = False self.on_key_change.disconnect_sources () def det_key (self, key): # Deactivate all buttons self.state.do_disable () self.keys_lab [key].setText ('?') self.info_txt.setText ('Click any key to config') self.slot = self.state.events.on_any_event.connect ( lambda ev, *a, **k: self.get_key (ev, key)) def get_key (self, ev, key): if is_key_event (ev): self.state.do_enable () self.state.events.on_any_event -= self.slot for c in self.cfg.childs (): if c.value == ev: c.set_value ('no-event') self.cfg.child (key).set_value (ev) @slot def on_key_change (self, cfg): if self.keys_lab [cfg.name]: self.keys_lab [cfg.name].setText (self.get_key_name (cfg.value)) self.info_txt.setText ('Select button')
class TfController (Tracker): DEFAULT_WIDTH = 1024 DEFAULT_HEIGHT = 768 DEFAULT_FPS = 60 DEFAULT_BPP = 32 DEFAULT_ANTIALIAS = 4 DEFAULT_VSYNCH = False DEFAULT_FULLSCREEN = False DEFAULT_SHOWMOUSE = False def __init__ (self, conf = None, window_title = '', *a, **k): super (TfController, self).__init__ (*a, **k) self._conf = GlobalConf ().child ('video') if conf is None else conf self._window_title = window_title self._is_setup = False self._music = None self.on_sfml_event = Signal () # TODO: abstract sfml here self._setup_conf_defaults () self._setup_conf_signals () self._setup_window () self._setup_logic () self._setup_input () def dispose (self): if self._is_setup: self.on_sfml_event.clear () self._event_loop.signal_event.remove (self.on_sfml_event) self.disconnect_all () self._window.Close () self._is_setup = False def play_music (self, file): if self._music: self._music.Stop () self._music = sf.Music () self._music.OpenFromFile (file) self._music.Play () def loop (self): self._timer.reset () self._timer.loop (self._loop_fn) def _loop_fn (self, timer): if self._tasks.count > 1: return self._tasks.update (timer) return False @property def audio (self): return self._game_loop.get_audiomanager () @property def keys (self): return self._keyboard @property def game (self): return self._game_loop @property def timer (self): return self._timer @property def tasks (self): return self._tasks @property def sfml_window (self): return self._window @property def window (self): return self._tf_window @property def view (self): return self._tf_view def _setup_input (self): self._keyboard = KeyboardManager () self.on_sfml_event += self._keyboard.on_sfml_event def _setup_logic (self): self._timer = Timer () self._tasks = task.TaskGroup () self._game_loop = get_game_loop () self._event_loop = EventLoop ('screenshot', self._tf_window, self._game_loop, None) self._event_loop.signal_event.add (self.on_sfml_event) self._tasks.add (task.repeat (task.run (self._event_loop.loop_once))) def _setup_window (self): self._window = sf.RenderWindow ( sf.VideoMode (self._conf.child ('width').value, self._conf.child ('height').value, self._conf.child ('bpp').value), self._window_title, sf.Style.Fullscreen if self._conf.child ('full').value else 0, sf.WindowSettings (AntialiasingLevel = self._conf.child ('antialias').value)) self._window.ShowMouseCursor (self._conf.child ('showmouse').value) self._window.SetFramerateLimit (self._conf.child ('fps').value) self._window.UseVerticalSync (self._conf.child ('vsync').value) self._tf_window = Window (self._window) self._tf_view = View (self._tf_window, self._window.GetDefaultView ()) def _setup_conf_defaults (self): self._conf.child ('width').default (self.DEFAULT_WIDTH) self._conf.child ('height').default (self.DEFAULT_HEIGHT) self._conf.child ('full').default (self.DEFAULT_FULLSCREEN) self._conf.child ('fps').default (self.DEFAULT_FPS) self._conf.child ('bpp').default (self.DEFAULT_BPP) self._conf.child ('vsync').default (self.DEFAULT_VSYNCH) self._conf.child ('antialias').default (self.DEFAULT_ANTIALIAS) self._conf.child ('showmouse').default (self.DEFAULT_SHOWMOUSE) def _setup_conf_signals (self): self._conf.on_conf_nudge += self._on_nudge_video self._conf.child ('fps').on_conf_change += self._on_change_fps self._conf.child ('vsync').on_conf_change += self._on_change_vsync @slot def _on_change_fps (self, node): self._window.SetFrameLimit (node.value) @slot def _on_change_vsync (self, node): self._window.UseVerticalSync (node.value) @slot def _on_nudge_video (self, node): self._window.Create ( sf.VideoMode (self._conf.child ('width').value, self._conf.child ('height').value, self._conf.child ('bpp').value), self._window_title, sf.Style.Fullscreen if self._conf.child ('full').value else 0)
class TfController(Tracker): DEFAULT_WIDTH = 1024 DEFAULT_HEIGHT = 768 DEFAULT_FPS = 60 DEFAULT_BPP = 32 DEFAULT_ANTIALIAS = 4 DEFAULT_VSYNCH = False DEFAULT_FULLSCREEN = False DEFAULT_SHOWMOUSE = False def __init__(self, conf=None, window_title='', *a, **k): super(TfController, self).__init__(*a, **k) self._conf = GlobalConf().child('video') if conf is None else conf self._window_title = window_title self._is_setup = False self._music = None self.on_sfml_event = Signal() # TODO: abstract sfml here self._setup_conf_defaults() self._setup_conf_signals() self._setup_window() self._setup_logic() self._setup_input() def dispose(self): if self._is_setup: self.on_sfml_event.clear() self._event_loop.signal_event.remove(self.on_sfml_event) self.disconnect_all() self._window.Close() self._is_setup = False def play_music(self, file): if self._music: self._music.Stop() self._music = sf.Music() self._music.OpenFromFile(file) self._music.Play() def loop(self): self._timer.reset() self._timer.loop(self._loop_fn) def _loop_fn(self, timer): if self._tasks.count > 1: return self._tasks.update(timer) return False @property def audio(self): return self._game_loop.get_audiomanager() @property def keys(self): return self._keyboard @property def game(self): return self._game_loop @property def timer(self): return self._timer @property def tasks(self): return self._tasks @property def sfml_window(self): return self._window @property def window(self): return self._tf_window @property def view(self): return self._tf_view def _setup_input(self): self._keyboard = KeyboardManager() self.on_sfml_event += self._keyboard.on_sfml_event def _setup_logic(self): self._timer = Timer() self._tasks = task.TaskGroup() self._game_loop = get_game_loop() self._event_loop = EventLoop('screenshot', self._tf_window, self._game_loop, None) self._event_loop.signal_event.add(self.on_sfml_event) self._tasks.add(task.repeat(task.run(self._event_loop.loop_once))) def _setup_window(self): self._window = sf.RenderWindow( sf.VideoMode( self._conf.child('width').value, self._conf.child('height').value, self._conf.child('bpp').value), self._window_title, sf.Style.Fullscreen if self._conf.child('full').value else 0, sf.WindowSettings( AntialiasingLevel=self._conf.child('antialias').value)) self._window.ShowMouseCursor(self._conf.child('showmouse').value) self._window.SetFramerateLimit(self._conf.child('fps').value) self._window.UseVerticalSync(self._conf.child('vsync').value) self._tf_window = Window(self._window) self._tf_view = View(self._tf_window, self._window.GetDefaultView()) def _setup_conf_defaults(self): self._conf.child('width').default(self.DEFAULT_WIDTH) self._conf.child('height').default(self.DEFAULT_HEIGHT) self._conf.child('full').default(self.DEFAULT_FULLSCREEN) self._conf.child('fps').default(self.DEFAULT_FPS) self._conf.child('bpp').default(self.DEFAULT_BPP) self._conf.child('vsync').default(self.DEFAULT_VSYNCH) self._conf.child('antialias').default(self.DEFAULT_ANTIALIAS) self._conf.child('showmouse').default(self.DEFAULT_SHOWMOUSE) def _setup_conf_signals(self): self._conf.on_conf_nudge += self._on_nudge_video self._conf.child('fps').on_conf_change += self._on_change_fps self._conf.child('vsync').on_conf_change += self._on_change_vsync @slot def _on_change_fps(self, node): self._window.SetFrameLimit(node.value) @slot def _on_change_vsync(self, node): self._window.UseVerticalSync(node.value) @slot def _on_nudge_video(self, node): self._window.Create( sf.VideoMode( self._conf.child('width').value, self._conf.child('height').value, self._conf.child('bpp').value), self._window_title, sf.Style.Fullscreen if self._conf.child('full').value else 0)
class Screen(object): def __init__(self, state=None, *a, **k): super(Screen, self).__init__(*a, **k) if state: self.state = state self.active = False self.cfg_p = GlobalConf().child('panda') self.cfg_g = GlobalConf().child('game') def do_paint(self): # Get possible screen resolutions NOT AVAILABLE IN OSX # di = base.pipe.getDisplayInformation() # for index in range(di.getTotalDisplayModes()): # sizes += ( str(di.getDisplayModeWidth(index)), # str(di.getDisplayModeHeight(index)) # ) actual_full = self.cfg_p.child('fullscreen').value actual_width = self.cfg_p.child('width').value actual_height = self.cfg_p.child('height').value self.active = True self.sizes = [(640, 480), (800, 600), (1024, 768), (1280, 800), (1280, 1024), (1440, 900)] self.sizes_tx = [] for h, w in self.sizes: self.sizes_tx += [str(h) + "x" + str(w)] # Gets the position of the actual config from self.sizes init_item = self.sizes.index((actual_width, actual_height)) # # TO DO: add images to check buttos # # Create a frame tx_scale = (0.9, 0.9) self.res_txt = OnscreenText(text="Resolution", font=self.state.font, align=TextNode.ALeft, pos=(-0.1, -0.4)) self.res_menu = DirectOptionMenu(text="Resolution", highlightScale=tx_scale, text_font=self.state.font, text_scale=tx_scale, item_text_font=self.state.font, item_text_scale=tx_scale, scale=0.08, items=self.sizes_tx, initialitem=init_item, highlightColor=(0.65, 0.65, 0.65, 1), command=self.change_res, pos=(0.5, 0, -0.4)) #checked = loader.loadModel ('menu/checked.egg') #unchecked = loader.loadModel ('menu/unchecked.egg') self.full_screen = DirectCheckButton( text="Full screen", text_font=self.state.font, text_scale=tx_scale, #text_pos = (0.5, 0, -0.7), text_align=TextNode.ALeft, indicatorValue=self.cfg_p.child('fullscreen').value, scale=.08, relief=None, boxPlacement='right', boxRelief=None, #boxImage = (unchecked, checked , None), #boxImageScale = 0.15, command=self.change_full, pos=(-0.1, 0, -0.6)) self.fps_display = DirectCheckButton( text="FPS display", text_font=self.state.font, text_scale=tx_scale, text_align=TextNode.ALeft, indicatorValue=self.cfg_p.child('frame-meter').value, scale=.08, relief=None, boxPlacement='right', boxRelief=None, command=self.change_fps, pos=(-0.1, 0, -0.75)) self.sha_display = DirectCheckButton( text="Shaders", text_font=self.state.font, text_scale=tx_scale, text_align=TextNode.ALeft, indicatorValue=self.cfg_g.child('shader').value, scale=.08, relief=None, boxPlacement='right', boxRelief=None, command=self.change_shad, pos=(-0.1, 0, -0.9)) def do_enable(self): pass def do_disable(self): pass def do_destroy(self): self.res_txt.destroy() self.res_menu.destroy() self.fps_display.destroy() self.full_screen.destroy() self.sha_display.destroy() self.active = False def change_shad(self, status): self.cfg_g.child('shader').set_value(bool(status)) def change_fps(self, status): self.cfg_p.child('frame-meter').set_value(bool(status)) def change_full(self, status): self.cfg_p.child('fullscreen').set_value(bool(status)) self.cfg_p.nudge() def change_res(self, arg): i = self.sizes_tx.index(arg) self.cfg_p.child('width').set_value(self.sizes[i][0]) self.cfg_p.child('height').set_value(self.sizes[i][1]) self.cfg_p.nudge()
class Sound (object): def __init__(self, state = None, *a, **k): super (Sound, self).__init__ (*a, **k) if state: self.state = state self.active = False self.cfg = GlobalConf().child ('panda') def do_paint (self): # # TO DO: - Place the sliders and label in correct place. # - Test colors and style # self.active = True scale = 0.4 y = -0.4 # Creates volume label and slider self.vol_lab = OnscreenText(text = 'SFX', font = self.state.font, pos = (0, y-0.02), scale = 0.07, ) self.vol_slider = DirectSlider(range=(0, 1), value = self.cfg.child ('sound-volume').value, pageSize = 0.03, command = self.change_snd, scale = scale, pos = (0.6, 0, y) ) # Creates music label and slider self.mus_lab = OnscreenText(text = 'Music', font = self.state.font, pos = (0, y-0.22), scale = 0.07 ) self.mus_slider = DirectSlider(range=(0, 1), value = self.cfg.child ('music-volume').value, pageSize = 0.03, command = self.change_snd, scale = scale, pos = (0.6, 0, y-0.2) ) #self.mus_slider.setPos (0.3, 0, -0.4) def do_enable (self): pass def do_disable (self): pass def do_destroy (self): self.mus_lab.destroy () self.mus_slider.destroy () self.vol_lab.destroy () self.vol_slider.destroy () self.active = False def change_snd (self): # Modifies config acording to sliders values self.cfg.child ('sound-volume').set_value (self.vol_slider['value']) self.cfg.child ('music-volume').set_value (self.mus_slider['value'])