Exemplo n.º 1
0
    def draw_game_flow(self, model: GameModel):
        self.log.debug('drawing game flow for model {0}'.format(model.name))
        TMPUTILS.clear_container(self.game_flow_panel)

        all_phases = [
            phase for pt in [model.table_type] + model.player_types
            for phase in pt.phases
        ]
        phase_phase = {}
        for phase in all_phases:
            phase_phase[phase] = list(TMPUTILS.end_phases(phase))

        graph = Graph()
        graph.vp.name = graph.new_vertex_property('string')
        graph.vp.color = graph.new_vertex_property('string')
        graph.vp.text_pos = graph.new_vertex_property('float')
        graph.vp.text_off = graph.new_vertex_property('vector<float>')
        phase_vertex = {}
        for phase in all_phases:
            vertex = graph.add_vertex()
            phase_vertex[phase] = vertex
            text = phase.name
            graph.vp.name[vertex] = text
            if phase is model.start_phase:
                color = self.config.start_game_color()
            elif phase is model.end_phase:
                color = self.config.end_game_color()
            elif phase in model.table_type.phases:
                color = self.config.table_color()
            else:
                number = model.player_types.index(
                    model.get_player_type_for_phase(phase))
                color = self.config.player_color(number)
            graph.vp.color[vertex] = color
            graph.vp.text_pos[vertex] = 0
            graph.vp.text_off[vertex] = [0, 0]

        for phase in all_phases:
            for other in phase_phase[phase]:
                graph.add_edge(phase_vertex[phase], phase_vertex[other])

        pos = sfdp_layout(graph)

        vprops = {
            'text': graph.vp.name,
            'fill_color': graph.vp.color,
            'text_position': graph.vp.text_pos,
            'text_offset': graph.vp.text_off
        }

        graph_widget = GraphWidget(graph, pos, vprops=vprops, vertex_size=50)
        self.game_flow_panel.pack_start(Gtk.Label('gameflow'), False, False, 0)
        self.game_flow_panel.pack_start(graph_widget, True, True, 0)
        self.show_all()
Exemplo n.º 2
0
    def draw_chart(self):
        self.graph = Graph()
        self.graph.vp.pos = self.graph.new_vertex_property('vector<double>')
        self.graph.vp.name = self.graph.new_vertex_property('string')

        self.vertexes = []
        for player in self.players:
            for phase in self.phases:
                index = len(self.vertexes)
                self.vertexes.append(self.graph.add_vertex())
                self.graph.vp.pos[index] = [index, index]
                self.graph.vp.name[index] = player.name + "-" + phase.name[-1]
        self.vprops = {'text': self.graph.vp.name}

        if 'graph_widget' in vars(self):
            self.remove(self.graph_widget)

        self.graph_widget = GraphWidget(self.graph,
                                        self.graph.vp.pos,
                                        vprops=self.vprops)
        self.pack_start(self.graph_widget, True, True, 0)
        self.graph_widget.show()
Exemplo n.º 3
0
    def draw_for(self, phase: Phase, model: GameModel):
        self.log.debug('drawing phase flow {0}'.format(phase.name))
        TMPUTILS.clear_container(self.main_panel)

        top = Gtk.HBox()

        name = Gtk.Label(phase.name)
        top.pack_start(name, True, True, 0)
        refresh_button = Gtk.Button('Refresh')
        refresh_button.connect('clicked', lambda w: self.draw_for(phase, model))
        top.pack_start(refresh_button, True, True, 0)
        self.main_panel.pack_start(top, False, False, 0)

        #TMPUTILS.start_rule_color = TMPUTILS.rule_color
        #start = phase.rules[0]
        #start = Rule('Początek {0}'.format(phase.name))
        start = Rule('Start {0}'.format(phase.name))
        start.next = phase.rules
        phase.rules = [start]

        rules_set = phase.all_rules_set()
        phase.rules = start.next

        graph = Graph()
        graph.vp.name = graph.new_vertex_property('string')
        graph.vp.fullname = graph.new_vertex_property('string')
        graph.vp.color = graph.new_vertex_property('string')
        graph.vp.shape = graph.new_vertex_property('string')
        graph.vp.rotation = graph.new_vertex_property('float')
        graph.vp.text_pos = graph.new_vertex_property('float')
        graph.vp.text_rotation = graph.new_vertex_property('float')

        graph.ep.text = graph.new_edge_property('string')
        graph.ep.text_color = graph.new_edge_property('string')

        rule_vertex = {}
        self.vertex_rule = {}

        for rule in rules_set:
            vertex = graph.add_vertex()
            rule_vertex[rule] = vertex
            self.vertex_rule[vertex] = rule
            graph.vp.name[vertex] = rule.verticle_name()
            graph.vp.fullname[vertex] = rule.name
            if rule is start:
                color = self.config.start_rule_color()
            elif issubclass(rule.__class__, ChangePhase):
                color = TMPUTILS.end_rule_color(rule, model)
            elif len([r for k, v in rule.rules_dict().items() for r in v]) == 0:
                color = self.config.wrong_rule_color()
            else:
                color = TMPUTILS.rule_color()
            graph.vp.color[vertex] = color
            #graph.vp.shape[vertex] = 'square' if issubclass(rule.__class__, If) else 'circle'
            graph.vp.shape[vertex] = self.config.rule_shape(rule)
            #graph.vp.rotation[vertex] = pi / 4 if issubclass(rule.__class__, If) else 0
            graph.vp.rotation[vertex] = self.config.rule_rotation(rule)
            graph.vp.text_pos[vertex] = 0
            #graph.vp.text_rotation[vertex] = - pi / 4 if issubclass(rule.__class__, If) else 0
            graph.vp.text_rotation[vertex] = self.config.rule_text_rotation(rule)

        for rule in rules_set:
            for next_text, next_rule_list in rule.rules_dict().items():
                for next_rule in next_rule_list:
                    edge = graph.add_edge(rule_vertex[rule], rule_vertex[next_rule])
                    #as_polish = {'No': "Nie", 'Yes': "Tak"}
                    #graph.ep.text[edge] = as_polish[next_text] if next_text in as_polish else next_text
                    graph.ep.text[edge] = next_text
                    graph.ep.text_color[edge] = TMPUTILS.text_color(next_text)

        pos = sfdp_layout(graph)

        vprops = {
            'text': graph.vp.name,
            'fill_color': graph.vp.color,
            'shape': graph.vp.shape,
            'rotation': graph.vp.rotation,
            'text_position': graph.vp.text_pos,
            'text_rotation': graph.vp.text_rotation
        }
        eprops = {
            'text': graph.ep.text,
            'text_color': graph.ep.text_color
        }
        self.graph_widget = GraphWidget(graph, pos, display_props=[graph.vp.fullname], vprops=vprops, eprops=eprops,
                                   vertex_size=50)
        #jest cos takiego jak GraphWidget.key_press_callback ale u mnie nie dziala...
        self.graph_widget.connect('button-release-event', self.on_vertex_clicked)

        self.main_panel.pack_start(self.graph_widget, True, True, 0)
        self.show_all()
Exemplo n.º 4
0
class PhaseFlowWindow(Gtk.ApplicationWindow):
    def __init__(self, app, mediator: Mediator):
        super().__init__(title='Phase flow', application=app)
        self.log = logging.getLogger(self.__class__.__name__)
        self.config = EditorConfig()
        self.set_size_request(600, 400)
        self.move(0, 0)
        self.main_panel = Gtk.VBox()
        self.add(self.main_panel)
        self.mediator = mediator
        self.mediator.phase_select.register(self.on_phase_select)
        self.mediator.model_select.register(self.on_model_select)
        self.model = None
        self.graph_widget = None

    def on_model_select(self, sender, model):
        self.model = model
        self.draw_for(model.start_phase, model)

    def on_phase_select(self, sender, phase):
        self.draw_for(phase, self.model)

    def draw_for(self, phase: Phase, model: GameModel):
        self.log.debug('drawing phase flow {0}'.format(phase.name))
        TMPUTILS.clear_container(self.main_panel)

        top = Gtk.HBox()

        name = Gtk.Label(phase.name)
        top.pack_start(name, True, True, 0)
        refresh_button = Gtk.Button('Refresh')
        refresh_button.connect('clicked', lambda w: self.draw_for(phase, model))
        top.pack_start(refresh_button, True, True, 0)
        self.main_panel.pack_start(top, False, False, 0)

        #TMPUTILS.start_rule_color = TMPUTILS.rule_color
        #start = phase.rules[0]
        #start = Rule('Początek {0}'.format(phase.name))
        start = Rule('Start {0}'.format(phase.name))
        start.next = phase.rules
        phase.rules = [start]

        rules_set = phase.all_rules_set()
        phase.rules = start.next

        graph = Graph()
        graph.vp.name = graph.new_vertex_property('string')
        graph.vp.fullname = graph.new_vertex_property('string')
        graph.vp.color = graph.new_vertex_property('string')
        graph.vp.shape = graph.new_vertex_property('string')
        graph.vp.rotation = graph.new_vertex_property('float')
        graph.vp.text_pos = graph.new_vertex_property('float')
        graph.vp.text_rotation = graph.new_vertex_property('float')

        graph.ep.text = graph.new_edge_property('string')
        graph.ep.text_color = graph.new_edge_property('string')

        rule_vertex = {}
        self.vertex_rule = {}

        for rule in rules_set:
            vertex = graph.add_vertex()
            rule_vertex[rule] = vertex
            self.vertex_rule[vertex] = rule
            graph.vp.name[vertex] = rule.verticle_name()
            graph.vp.fullname[vertex] = rule.name
            if rule is start:
                color = self.config.start_rule_color()
            elif issubclass(rule.__class__, ChangePhase):
                color = TMPUTILS.end_rule_color(rule, model)
            elif len([r for k, v in rule.rules_dict().items() for r in v]) == 0:
                color = self.config.wrong_rule_color()
            else:
                color = TMPUTILS.rule_color()
            graph.vp.color[vertex] = color
            #graph.vp.shape[vertex] = 'square' if issubclass(rule.__class__, If) else 'circle'
            graph.vp.shape[vertex] = self.config.rule_shape(rule)
            #graph.vp.rotation[vertex] = pi / 4 if issubclass(rule.__class__, If) else 0
            graph.vp.rotation[vertex] = self.config.rule_rotation(rule)
            graph.vp.text_pos[vertex] = 0
            #graph.vp.text_rotation[vertex] = - pi / 4 if issubclass(rule.__class__, If) else 0
            graph.vp.text_rotation[vertex] = self.config.rule_text_rotation(rule)

        for rule in rules_set:
            for next_text, next_rule_list in rule.rules_dict().items():
                for next_rule in next_rule_list:
                    edge = graph.add_edge(rule_vertex[rule], rule_vertex[next_rule])
                    #as_polish = {'No': "Nie", 'Yes': "Tak"}
                    #graph.ep.text[edge] = as_polish[next_text] if next_text in as_polish else next_text
                    graph.ep.text[edge] = next_text
                    graph.ep.text_color[edge] = TMPUTILS.text_color(next_text)

        pos = sfdp_layout(graph)

        vprops = {
            'text': graph.vp.name,
            'fill_color': graph.vp.color,
            'shape': graph.vp.shape,
            'rotation': graph.vp.rotation,
            'text_position': graph.vp.text_pos,
            'text_rotation': graph.vp.text_rotation
        }
        eprops = {
            'text': graph.ep.text,
            'text_color': graph.ep.text_color
        }
        self.graph_widget = GraphWidget(graph, pos, display_props=[graph.vp.fullname], vprops=vprops, eprops=eprops,
                                   vertex_size=50)
        #jest cos takiego jak GraphWidget.key_press_callback ale u mnie nie dziala...
        self.graph_widget.connect('button-release-event', self.on_vertex_clicked)

        self.main_panel.pack_start(self.graph_widget, True, True, 0)
        self.show_all()

    def on_vertex_clicked(self, widget, event):
        if widget.picked is not None:
            if type(widget.picked) == PropertyMap:
                rules = [self.vertex_rule[v] for v in self.vertex_rule.keys() if widget.picked[v]]
            else:
                rules = [self.vertex_rule[widget.picked]]
            self.mediator.rule_selects.fire(self, rules)
Exemplo n.º 5
0
class GeneralChartWidget(Gtk.VBox):
    def __init__(self, mediator: Mediator):
        Gtk.VBox.__init__(self)

        self.players = []
        self.phases = []

        self.header = PanelHeader('General view')

        self.pack_start(self.header, False, False, 0)

        mediator.player_types.add.register(self.on_player_add)
        mediator.phases.add.register(self.on_phase_add)
        mediator.player_types.remove.register(self.on_player_remove)
        mediator.phases.remove.register(self.on_phase_remove)
        mediator.clear_state.register(self.clear_state)

        self.draw_chart()

    def on_player_add(self, sender, player):
        self.players.append(player)
        self.draw_chart()

    def on_phase_add(self, sender, phase):
        self.phases.append(phase)
        self.draw_chart()

    def on_player_remove(self, sender, player):
        self.players.remove(player)
        self.draw_chart()

    def on_phase_remove(self, sender, phase):
        self.phases.remove(phase)
        self.draw_chart()

    def draw_chart(self):
        self.graph = Graph()
        self.graph.vp.pos = self.graph.new_vertex_property('vector<double>')
        self.graph.vp.name = self.graph.new_vertex_property('string')

        self.vertexes = []
        for player in self.players:
            for phase in self.phases:
                index = len(self.vertexes)
                self.vertexes.append(self.graph.add_vertex())
                self.graph.vp.pos[index] = [index, index]
                self.graph.vp.name[index] = player.name + "-" + phase.name[-1]
        self.vprops = {'text': self.graph.vp.name}

        if 'graph_widget' in vars(self):
            self.remove(self.graph_widget)

        self.graph_widget = GraphWidget(self.graph,
                                        self.graph.vp.pos,
                                        vprops=self.vprops)
        self.pack_start(self.graph_widget, True, True, 0)
        self.graph_widget.show()

    def clear_state(self, sender, value):
        self.players.clear()
        self.phases.clear()
        self.draw_chart()
Exemplo n.º 6
0
    def draw_for(self, sender, model: GameModel):
        self.log.debug('drawing place map for model {0}'.format(model.name))
        TMPUTILS.clear_container(self.main_panel)

        refresh_button = Gtk.Button('Refresh')
        refresh_button.connect('clicked',
                               lambda w: self.draw_for(sender, model))
        self.main_panel.pack_start(refresh_button, False, False, 0)

        all_places = set()
        for player_type in model.player_types + [model.table_type]:
            for place in player_type.places:
                all_places.add(place)

        place_vertex = {}

        graph = Graph()
        graph.vp.name = graph.new_vertex_property('string')
        graph.vp.color = graph.new_vertex_property('string')
        graph.vp.text_pos = graph.new_vertex_property('float')
        graph.ep.color = graph.new_edge_property('vector<float>')

        for place in all_places:
            vertex = graph.add_vertex()
            place_vertex[place] = vertex

            graph.vp.name[vertex] = place.name
            graph.vp.color[vertex] = TMPUTILS.table_color(
            ) if place in model.table_type.places else TMPUTILS.player_color()
            graph.vp.text_pos[vertex] = 0

        self.graph = graph
        self.place_vertex = place_vertex

        self.rule_edge = {}
        for phase in model.all_phases():
            for source, target, rule in self.edge_info_for_phase(phase):
                edge = graph.add_edge(place_vertex[source],
                                      place_vertex[target])
                if rule not in self.rule_edge:
                    self.rule_edge[rule] = []
                self.rule_edge[rule].append(edge)
                graph.ep.color[edge] = [0.179, 0.203, 0.210, 0.8]

        pos = sfdp_layout(graph)

        vprops = {
            'text': graph.vp.name,
            'fill_color': graph.vp.color,
            'text_position': graph.vp.text_pos,
        }

        eprops = {'color': graph.ep.color}

        self.graph_widget = GraphWidget(graph,
                                        pos,
                                        vprops=vprops,
                                        eprops=eprops,
                                        vertex_size=50)
        self.main_panel.pack_start(self.graph_widget, True, True, 0)
        self.show_all()
Exemplo n.º 7
0
class PlaceMapWindow(Gtk.ApplicationWindow):
    def __init__(self, app, mediator: Mediator):
        super().__init__(title='Place map', application=app)
        self.log = logging.getLogger(self.__class__.__name__)
        self.set_size_request(600, 400)
        self.move(700, 0)
        self.main_panel = Gtk.VBox()
        self.add(self.main_panel)

        self.mediator = mediator
        self.mediator.model_select.register(self.draw_for)
        self.mediator.phase_select.register(self.on_phase_select)
        self.mediator.rule_selects.register(self.on_rules_select)

        self.rule_edge = None
        self.last_highlighted_edges = []
        self.graph = None
        self.place_vertex = None
        self.graph_widget = None

    def draw_for(self, sender, model: GameModel):
        self.log.debug('drawing place map for model {0}'.format(model.name))
        TMPUTILS.clear_container(self.main_panel)

        refresh_button = Gtk.Button('Refresh')
        refresh_button.connect('clicked',
                               lambda w: self.draw_for(sender, model))
        self.main_panel.pack_start(refresh_button, False, False, 0)

        all_places = set()
        for player_type in model.player_types + [model.table_type]:
            for place in player_type.places:
                all_places.add(place)

        place_vertex = {}

        graph = Graph()
        graph.vp.name = graph.new_vertex_property('string')
        graph.vp.color = graph.new_vertex_property('string')
        graph.vp.text_pos = graph.new_vertex_property('float')
        graph.ep.color = graph.new_edge_property('vector<float>')

        for place in all_places:
            vertex = graph.add_vertex()
            place_vertex[place] = vertex

            graph.vp.name[vertex] = place.name
            graph.vp.color[vertex] = TMPUTILS.table_color(
            ) if place in model.table_type.places else TMPUTILS.player_color()
            graph.vp.text_pos[vertex] = 0

        self.graph = graph
        self.place_vertex = place_vertex

        self.rule_edge = {}
        for phase in model.all_phases():
            for source, target, rule in self.edge_info_for_phase(phase):
                edge = graph.add_edge(place_vertex[source],
                                      place_vertex[target])
                if rule not in self.rule_edge:
                    self.rule_edge[rule] = []
                self.rule_edge[rule].append(edge)
                graph.ep.color[edge] = [0.179, 0.203, 0.210, 0.8]

        pos = sfdp_layout(graph)

        vprops = {
            'text': graph.vp.name,
            'fill_color': graph.vp.color,
            'text_position': graph.vp.text_pos,
        }

        eprops = {'color': graph.ep.color}

        self.graph_widget = GraphWidget(graph,
                                        pos,
                                        vprops=vprops,
                                        eprops=eprops,
                                        vertex_size=50)
        self.main_panel.pack_start(self.graph_widget, True, True, 0)
        self.show_all()

    def on_phase_select(self, sender, phase):
        edges = []
        for source, target, rule in self.edge_info_for_phase(phase):
            edges += self.rule_edge[rule]

        self.highlight_edges(edges)

    def on_rules_select(self, sender, rules):
        edges = []
        for rule in rules:
            if rule in self.rule_edge:
                edges += self.rule_edge[rule]
        #edges = [self.rule_edge[rule] if rule in self.rule_edge else [] for rule in rules]
        self.highlight_edges(edges)

    def highlight_edges(self, edges):
        self.log.debug('highlighting edges')
        for edge in self.last_highlighted_edges:
            self.graph.ep.color[edge] = [0.179, 0.203, 0.210, 0.8]

        self.last_highlighted_edges = []
        for edge in edges:
            self.graph.ep.color[edge] = [0, 0, 255, 0.8]
            self.last_highlighted_edges.append(edge)

        self.graph_widget.regenerate_surface()
        self.graph_widget.queue_draw()

    def edge_info_for_phase(self, phase):
        ret = []
        for rule in self.rules_for_phase(phase):
            if issubclass(rule.__class__, Move):
                source = rule.card_picker.source_place_picker.place
                target = rule.card_picker.target_place_picker.place
                ret.append((source, target, rule))
        return ret

    def rules_for_phase(self, phase):
        # TODO poprawilem w domu ajestem wpracy, pozniej zmienie
        start = Rule('start {0}'.format(phase.name))
        start.next = phase.rules
        rules = dict()
        rules[''] = [start]
        TMPUTILS.append_rules(rules, start)
        rules_set = set()
        for text, rule_list in rules.items():
            for rule in rule_list:
                rules_set.add(rule)
        return rules_set