예제 #1
0
    def __init__(self, game, settings, map_settings):
        super(ViewWidget, self).__init__()

        self.game = game

        #self.map_widget = GameWidget(game, map_settings)
        #self.map_widget.setParentItem(self)
        #self.map_widget.hide()

        self.game_widget = GameWidget(game, settings)
        self.game_widget.setParentItem(self)
        self.active = self.game_widget
        self.levels = [self.game_widget.level]#, self.map_widget.level]

        self._info = InfoWidget()
        self.setWidget('top_right', self._info)
        self._info.resize_event.connect(self._onInfoResizeEvent)
        self._info.gained_focus.connect(self._onWidgetGainedFocus)
        self._info.lost_focus.connect(self._onWidgetLostFocus)

        self._question = InputWidget(self)
        self.setWidget('middle_left', self._question)
        self._question.finished.connect(self._onAnswerRecieved)
        self._question.canceled.connect(self._onAnswerRecieved)

        self._stats = StatsWidget()
        self.setWidget('bottom_left', self._stats)

        self._log = LogWidget()
        self.setWidget('top_left', self._log)
        game.events['action_happened_in_game'].connect(self._log.appendDungeonMessage)

        game.events['game_started'].connect(self._onGameStarted)
        game.events['turn_finished'].connect(self._onTurnFinished)

        for group, commands in game.commands.items():
            for command in commands.values():
                parent = self if group in self.game_widget.menu_names else self.game_widget.level
                action = Action(
                    parent, 
                    command.desc, 
                    [command.key], 
                    self.game_widget._onAction,
                    args=(group, command.name)
                )
                parent.addAction(action)

        for name in self.game_widget.settings.keys(self._settings_group):
            action = Action(self, name, ['Ctrl+' + name[0]], self._onSettingsChanged, args=('settings', name))
            action.setCheckable(True)
            action.setChecked(settings[self._settings_group, name])
            self.addAction(action)
            self._onSettingsChanged('settings', name)
예제 #2
0
class ViewWidget(ScalingWidget):

    _settings_group = 'model'

    def __init__(self, game, settings, map_settings):
        super(ViewWidget, self).__init__()

        self.game = game

        #self.map_widget = GameWidget(game, map_settings)
        #self.map_widget.setParentItem(self)
        #self.map_widget.hide()

        self.game_widget = GameWidget(game, settings)
        self.game_widget.setParentItem(self)
        self.active = self.game_widget
        self.levels = [self.game_widget.level]#, self.map_widget.level]

        self._info = InfoWidget()
        self.setWidget('top_right', self._info)
        self._info.resize_event.connect(self._onInfoResizeEvent)
        self._info.gained_focus.connect(self._onWidgetGainedFocus)
        self._info.lost_focus.connect(self._onWidgetLostFocus)

        self._question = InputWidget(self)
        self.setWidget('middle_left', self._question)
        self._question.finished.connect(self._onAnswerRecieved)
        self._question.canceled.connect(self._onAnswerRecieved)

        self._stats = StatsWidget()
        self.setWidget('bottom_left', self._stats)

        self._log = LogWidget()
        self.setWidget('top_left', self._log)
        game.events['action_happened_in_game'].connect(self._log.appendDungeonMessage)

        game.events['game_started'].connect(self._onGameStarted)
        game.events['turn_finished'].connect(self._onTurnFinished)

        for group, commands in game.commands.items():
            for command in commands.values():
                parent = self if group in self.game_widget.menu_names else self.game_widget.level
                action = Action(
                    parent, 
                    command.desc, 
                    [command.key], 
                    self.game_widget._onAction,
                    args=(group, command.name)
                )
                parent.addAction(action)

        for name in self.game_widget.settings.keys(self._settings_group):
            action = Action(self, name, ['Ctrl+' + name[0]], self._onSettingsChanged, args=('settings', name))
            action.setCheckable(True)
            action.setChecked(settings[self._settings_group, name])
            self.addAction(action)
            self._onSettingsChanged('settings', name)


    def toggleMap(self):
        view = self.map_widget.scene().views()[0]
        if self.active is self.game_widget:
            self.game_widget.hide()
            self.map_widget.show()
            self._last_scale = view.scale

            view.setScale(1)
            view.centerOn(self.map_widget.level.centerTile())
            self.active = self.map_widget

        else:
            self.game_widget.show()
            self.map_widget.hide()
            view.setScale(self._last_scale)
            view.centerOn(self.game_widget.level.player_tile)
            self.active = self.game_widget

    def advanceFocus(self):
        self._info.advanceFocus()

    def _onGameStarted(self, level):
        player = self.game.player
        self._info.setPlayer(player)
        self._stats.setPlayer(player)
        player.events['action_happened_to_player'].connect(self._log.appendPlayerMessage)
        player.events['using_updated'].connect(self.game_widget.level._onUsingUpdated)
        player.events['answer_requested'].connect(self._onAnswerRequested)
        self.game.player.emit_info()

    def _onInfoResizeEvent(self):
        self._onViewportChanged(self._last_viewport_rect, self._last_viewport_scale)

    def _onWidgetGainedFocus(self, widget):
        for level in self.levels:
            if widget.selectable:
                level.setEnabled(False)
            else:
                level.setEnabled(True)

    def _onWidgetLostFocus(self, widget):
        for level in self.levels:
            if widget.selectable:
                level.setEnabled(True)
                level.setFocus()

    def _onAnswerRequested(self, question, callback):
        self._question.ask(question, callback)
        for level in self.levels:
            level.setEnabled(False)

    def _onAnswerRecieved(self):
        for level in self.levels:
            level.setEnabled(True)

    def _onFinishedLoading(self):
        self.active.level.setFocus()

    def _onTurnFinished(self, number, player_can_move):
        self._log.onTurnFinished(number, player_can_move)

    def _onSettingsChanged(self, _nothing, setting):

        setting = str(setting)
        for action in self.actions():
            if action.name == setting:
                checked = action.isChecked()  
                self.game.set_setting(setting, checked)
                return
        raise ValueError(setting)