Example #1
0
    def _find_components(self):
        # set the currently selected entity, if any
        entity = self.context['selected_entity']
        if not entity:
            return

        # repopulate the tree view
        entity_components = self.context['entities'][entity]['components']
        for comp_type in entity_components.keys():
            components = entity_components[comp_type]
            tl_item = TreeWidgetItemComponent(comp_type, components)
            self.component_tree_view.addTopLevelItem(tl_item)
            for component in components:
                # first add this component as a child item to the
                # component type top level item
                component_item = TreeWidgetItemComponent(component.text, component)
                tl_item.addChild(component_item)
                # then add each property of this component as
                # an additional nested level of child items
                for name in (x for x in dir(component.component) if not x.startswith('_')):
                    val = LambdaDef(component, name)
                    component_attr_item = TreeWidgetItemComponent(name, val)
                    component_item.addChild(component_attr_item)
    def _find_bodies(self):
        entity = self.context['selected_entity']

        # first do a soft clear of the parent list view
        for i in range(self.parent_list_view.count()-1,-1,-1):
            item = self.parent_list_view.takeItem(i)

        # clear the body tree
        for i in range(self.body_tree_view.topLevelItemCount()-1,-1,-1):
            item = self.body_tree_view.takeTopLevelItem(i)
            # I think this is unnecessary since taking the top level item should clear its children
            #for j in range(item.childCount()-1,-1,-1):
            #    child_item = item.takeChild(j)

        # populate parent tree with existing movement components
        available_parents = self.context['entities'][entity]['components']['movement']
        available_parents += self.context['entities'][entity]['components']['body']
        for component in available_parents:
            widget_item = WidgetItemComponent(component.text, component)
            self.parent_list_view.addItem(widget_item)

        # populate body tree with Vector2 components
        for comp_type, components in self.context['entities'][entity]['components'].iteritems():
            if comp_type == 'vector':
                for component in components:
                    tl_tree_item = TreeWidgetItemComponent(component.text, component)
                    self.body_tree_view.addTopLevelItem(tl_tree_item)
            for component in components:
                inner_comp = component.component
                for name in [x for x in dir(inner_comp) if not x.startswith('_')]:
                    attr = inner_comp.__dict__[name]
                    if type(attr).__name__ == 'Component':
                        if attr.type_name == 'Vector2':
                            tl_tree_item = TreeWidgetItemComponent(component.text, inner_comp)
                            self.body_tree_view.addTopLevelItem(tl_tree_item)
                            tree_item = TreeWidgetItemComponent(name, attr)
                            tl_tree_item.addChild(tree_item)
                    if type(attr).__name__ == 'Vector2':
                        tl_tree_item = TreeWidgetItemComponent(component.text, inner_comp)
                        self.body_tree_view.addTopLevelItem(tl_tree_item)
                        tree_item = TreeWidgetItemComponent(name, attr)
                        tl_tree_item.addChild(tree_item)
    def set_entity(self, event):
        entity = event.entity

        # first do a soft clear of the tree view
        for i in range(self.activation_component_tree_view.topLevelItemCount()-1,-1,-1):
            item = self.activation_component_tree_view.takeTopLevelItem(i)
            for j in range(item.childCount()-1,-1,-1):
                child_item = item.takeChild(j)

        # repopulate the tree with the current entity's components
        tl_items = self.context['entities'][entity]['components'].keys()
        # component types represent the top level tree items
        for item in tl_items:
            components = self.context['entities'][entity]['components'][item]
            tl_tree_item = TreeWidgetItemComponent(item, components)
            self.activation_component_tree_view.addTopLevelItem(tl_tree_item)
            # the components should all be listed under their respective 
            # component type
            for component in components:
                tree_item = TreeWidgetItemComponent(component.text, component)
                tl_tree_item.addChild(tree_item)

        # now do this same stuff for the rule value component tree
        rule_value_tree = self.rule_value_component_tree_view
        # clear it out first
        for i in range(rule_value_tree.topLevelItemCount()-1,-1,-1):
            item = rule_value_tree.takeTopLevelItem(i)
            for j in range(item.childCount()-1,-1,-1):
                child_item = item.takeChild(j)
                for k in range(child_item.childCount()-1,-1,-1):
                    grand_child_item = child_item.takeChild(k)

        # then repopulate the tree with components and component properties
        tl_items = self.context['entities'][entity]['components'].keys()
        for item in tl_items:
            components = self.context['entities'][entity]['components'][item]
            tl_tree_item = TreeWidgetItemComponent(item, components)
            rule_value_tree.addTopLevelItem(tl_tree_item)
            for component in components:
                component_item = TreeWidgetItemComponent(component.text, component)
                tl_tree_item.addChild(component_item)
                # then add a child item for each non-private attribute of 
                # this component
                for name in (x for x in dir(component.component) if not x.startswith('_')):
                    # still using outer component
                    val = LambdaDef(component, name)
                    component_attr_item = TreeWidgetItemComponent(name, val)
                    component_item.addChild(component_attr_item)

        # finally, repopulate the state component list
        self.state_list_view.clear()
        for state in self.context['entities'][entity]['components']['state']:
            widget_component = WidgetItemComponent(state.text, state)
            self.state_list_view.addItem(widget_component)