def do_prepare(self, args): super(JagsatApp, self).do_prepare(args) args.add('m', 'map', self._arg_map) args.add('s', 'state', self._arg_state) args.add(None, 'ratio-hack', self._arg_rhack) args.add(None, 'music-on', OptionConfFlag(GlobalConf().child('global-music'), True)) args.add(None, 'music-off', OptionConfFlag(GlobalConf().child('global-music'), False)) self.add_state('sandbox', Sandbox) self.add_state('root', RootState) self.add_state('game', GameState) self.add_state('init_game', InitGameState) self.add_state('game_round', GameRoundState) self.add_state('ingame_menu', IngameMenuState) self.add_state('main_menu', MainMenuState) self.add_state('reinforce', ReinforcementState) self.add_state('attack', AttackState) self.add_state('move', MovementState) self.add_state('risk_attack', RiskAttackState) self.add_state('message', RootMessageState) self.add_state('dialog', RootDialogState) self.add_state('yes_no_dialog', RootYesNoDialogState) self.add_state('input_dialog', RootInputDialogState) self.add_state('test_reinforce', partial(GameState, test_phase='reinforce')) self.add_state('test_attack', partial(GameState, test_phase='attack')) self.add_state('test_move', partial(GameState, test_phase='move'))
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 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)
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 __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 setup2 (self): self.events.event ('panda-escape').connect (self.kill) m = loader.loadModel ('../data/mesh/pigeon.x') m.reparentTo(render) m.setPos (0, 30, -10) m.setScale (-1, -1, -1) m2 = loader.loadModel ('../data/mesh/cloud.x') m2.reparentTo(render) m2.setPos (0, 0, -100) m2.setScale (3, 3, 3) def rotate_task (t): m.setHpr (t.elapsed * 10, 0, 0) return Task.RUNNING self.tasks.add (rotate_task) plightnode = PointLight("point light") plightnode.setAttenuation(Vec3(1,0.0000001,0.0000001)) plight = render.attachNewNode(plightnode) plight.setPos(100, -100, 1000) alightnode = AmbientLight("ambient light") alightnode.setColor(Vec4(0.4,0.4,0.4,1)) alight = render.attachNewNode(alightnode) render.setLight(alight) render.setLight(plight) base.setBackgroundColor (Vec4 (.4, .6, .9, 1)) # light ramp #tempnode1 = NodePath(PandaNode("temp-node1")) #render.setAttrib (LightRampAttrib.makeDoubleThreshold(0.4, 0.5, 0.5, 0.9)) #render.setAttrib (LightRampAttrib.makeHdr0()) #render.setAttrib (LightRampAttrib.makeHdr1()) #render.setAttrib (LightRampAttrib.makeHdr2()) #render.setShaderAuto() #base.cam.node().setInitialState (tempnode1.getState ()) # ink # self.separation = 1.3 # Pixels # self.filters = CommonFilters (base.win, base.cam) # filterok = self.filters.setCartoonInk (separation=self.separation) self.events.event ('panda-f').connect ( lambda: GlobalConf ().path ('panda.frame-meter').set_value ( not GlobalConf ().path ('panda.frame-meter').value))
def setup_panda(self): self.glow_filter = shader.GlowFilter() panda = self.manager.panda if GlobalConf().path('game.shader').value: self.glow_filter.enable(panda) panda.relative_mouse() panda.loop_music(self.level.music)
def setup_input (self): self.player_input = GameInput () self.camera_input = GameInput (CAMERA_INPUT_MAP) self.events.connect (self.player_input) self.events.connect (self.camera_input) self.tasks.add (self.player_input) self.tasks.add (self.camera_input) self.player_input.assoc ('on_steer', 'panda-mouse-move') cfg = GlobalConf ().path ('game.player0.keys') for c in cfg.childs (): if c.value: self.player_input.assoc (c.name, c.value) c.on_conf_change += self.on_control_change GlobalConf ().path ('game.shader').on_conf_change += \ self.on_shader_change
def setup_input(self): self.player_input = GameInput() self.camera_input = GameInput(CAMERA_INPUT_MAP) self.events.connect(self.player_input) self.events.connect(self.camera_input) self.tasks.add(self.player_input) self.tasks.add(self.camera_input) self.player_input.assoc('on_steer', 'panda-mouse-move') cfg = GlobalConf().path('game.player0.keys') for c in cfg.childs(): if c.value: self.player_input.assoc(c.name, c.value) c.on_conf_change += self.on_control_change GlobalConf ().path ('game.shader').on_conf_change += \ self.on_shader_change
def do_execute(self, freeargs): GlobalConf().child('global-music').default(True) if self._arg_state.value: self.root_state = self._arg_state.value if self._arg_rhack.value: uihelp.xratio = 1366. / 1024 super(JagsatApp, self).do_execute(freeargs)
def __init__(self, parent=None, config=create_default_profile()): super(GameOptions, self).__init__(parent) self.on_start_game = signal.Signal() self.on_quit_program = signal.Signal() self._config = config self._box_left = widget.VBox(self) self._box_left.separation = 15 title_text(self._box_left, '> Player options') self._box_players = widget.VBox(self._box_left) self._player_options = \ [ PlayerOptions (self._box_players, config.child ('player-%i' % i)) for i in range (6) ] self._box_players.separation = 10 txt = title_text(self._box_left, '> Game options') txt.padding_top = 10 self._box_extra = widget.HBox(self._box_left) self._box_extra.separation = 10 self.rules = widget.SmallButton(self._box_extra, 'Rules', 'data/icon/world-small.png', vertical=False) self._but_music = widget.SelectButton( self._box_extra, text='Music', vertical=False, selected_img='data/icon/music-small.png', unselected_img='data/icon/nomusic-small.png') # HACK if GlobalConf().child('global-music').value: self._but_music.select() self._but_music.on_select += self._update_music self._but_music.on_unselect += self._update_music self._box_map = widget.VBox(self) title_text(self._box_map, '> War map') self._box_map.separation = 10 self._map_selector = MapSelector(parent=self._box_map) self._map_selector.on_change_select += self._update_map try: self._map_selector.select(config.child('map').value) except Exception: try: self._map_selector.select(self._map_selector._contents[0][2]) except Exception: pass self.separation = 50
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 __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 do_prepare(self, args): self._video_cfg = GlobalConf().child('video') cfg = self._video_cfg args.add('F', 'fps', OptionConfWith(cfg.child('fps'), int)) args.add('f', 'fullscreen', OptionConfFlag(cfg.child('full'))) args.add('w', 'window', OptionConfFlag(cfg.child('full'), False)) args.add('s', 'vsync', OptionConfFlag(cfg.child('vsync'))) args.add('S', 'no-vsync', OptionConfFlag(cfg.child('vsync'), False)) args.add('W', 'width', OptionConfWith(cfg.child('width'), int)) args.add('H', 'height', OptionConfWith(cfg.child('height'), int)) args.add(None, 'show-mouse', OptionConfFlag(cfg.child('showmouse'), True)) args.add(None, 'hide-mouse', OptionConfFlag(cfg.child('showmouse'), False))
def loop_music(self, file): if self._music: self._music.setLoop(False) self.tasks.add( task.sequence( task.linear(self._music.setVolume, self._music.getVolume(), 0.0))) volume = GlobalConf().path('panda.music-volume').value self._music = loader.loadSfx(file) self._music.setLoop(True) self.tasks.add( task.sequence( task.linear(self._music.setVolume, 0.0, volume, init=True))) self._music.play()
def __init__(self, parent=None, config=GlobalConf(), *a, **k): super(ProfileChangerDialog, self).__init__(parent) self.separation = 15 title_text(self, "> Choose a profile") self._prof_list = widget.List( parent=self, contents=[('data/icon/credits-tiny.png', p.name, p.name) for p in config.child('profiles').childs()], num_slots=8, button_size=(400, 40), *a, **k) self._prof_list.select(config.child('current-profile').value) self._prof_list.on_change_select += lambda x: self.on_dialog_exit( ProfileChangerReturn(self._prof_list.selected))
def __init__(self, parent=None, config=GlobalConf(), *a, **k): super(ProfileManager, self).__init__(parent, center=True, *a, **k) self._cfg_root = config self._cfg_profiles = config.child('profiles') profile_name = config.child('current-profile').value if profile_name is None: profile_name = 'default' config.child('current-profile').value = profile_name if self._cfg_profiles.has_child(profile_name): self._cfg_current = self._cfg_profiles.child(profile_name) else: self._cfg_current = self._cfg_root.child('profiles').adopt( create_default_profile(), profile_name) title_text(self, '> Profile ') self._edit_profile = widget.LineEdit(self, text=profile_name, theme=theme.small_button_green) self._edit_profile.activate() self._edit_profile.on_edit += self._on_profile_rename self.add_profile = widget.Button(self, 'Add', 'data/icon/add-tiny.png', vertical=False, theme=theme.small_button_green) self.del_profile = widget.Button(self, 'Delete', 'data/icon/cancel-tiny.png', vertical=False, theme=theme.small_button_green) self.change_profile = widget.Button(self, 'Change', 'data/icon/retreat-tiny.png', vertical=False, theme=theme.small_button_green) self.separation = 15
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 start(self, title): cfg = GlobalConf().child('panda') self.set_defaults(cfg) self.base = ShowBase() self.base.disableMouse() self.audio = self.base.sfxManagerList[0] self.audio3d = Audio3DManager(self.audio, camera) self.audio3d.setListenerVelocityAuto() self.audio3d.setDropOffFactor(0.1) # HACK self.create_properties(title) self.update_properties(cfg) self.listen_conf(cfg) loadPrcFileData("", "interpolate-frames 1") loadPrcFileData("", "support-threads #f") path = getModelPath() path.prependPath('./data') self.base.enableParticles()
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'])
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 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 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')
def _update_music(self, but): GlobalConf().child('global-music').value = but.is_selected
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')
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)