Exemple #1
0
class GUIList(GUIComponent):
    def __init__(self):
        GUIComponent.__init__(self)
        self.child = GUIStackLayout()
        self.selected_item = None

    # Sets the selected item
    def set_selected_item(self, item):
        self.selected_item = item
        for child in self.child.children:
            if child is not item:
                child.deselect()

    # Adds an item to the list
    def add_item(self, item, index=-1):
        item.list_container = self
        self.child.add_child(item, index)
        GUISystem.update_all()

    # Removes an item from the list
    def remove_item(self, item):
        if type(item) == GUIListDropdown and item.expanded:
            item.collapse()
        item.deselect()
        self.child.remove_child(item)
        GUISystem.update_all()

    # Adds the parent's sub-list to the list
    def add_sub_list(self, parent):
        p_index = self.child.children.index(parent)
        for list_item in parent.sub_list:
            self.add_item(list_item, p_index + 1)

    # Removes the parent's sub-list from the list
    def remove_sub_list(self, parent):
        # Find the parent in the list
        for list_item in parent.sub_list:
            self.remove_item(list_item)

    def update(self):
        largest_width = 0
        if self.child is not None:
            for child in self.child.children:
                if largest_width < child.child.bbox.width:
                    largest_width = child.child.bbox.width
        self.bbox.width = max(largest_width, self.clip_region.width)

        self.child.bbox.x = self.bbox.x
        self.child.bbox.y = self.bbox.y
        self.child.bbox.width = self.bbox.width
        self.child.set_clip_region(self.clip_region.get_intersection(
            self.bbox))
        self.child.update()
        self.bbox.height = self.child.bbox.height
Exemple #2
0
class ComponentViewer(GUIFrame):

    def __init__(self):
        GUIFrame.__init__(self)
        self.components = None
        self.envedit_data = None

        # GUI settings
        self.bg_color = (0, 0, 0, 0.8)
        self.bbox.width = 300
        self.padding = 10

        master_layout = GUIDockLayout()
        self.set_child(master_layout)

        self.layout = GUIStackLayout()
        master_layout.set_child_dock(self.layout, GUIDockLayout.TOP)

        title = GUILabel()
        title.text_size = 30
        title.set_font(GUISystem.get_font("default_light"))
        title.set_text("Components")
        self.layout.add_child(title)

        spacer = GUIComponent()
        spacer.bbox.height = 20
        self.layout.add_child(spacer)

        self.add_component_dropdown = GUIDropdown()
        self.add_component_dropdown.child.set_text("Add Component...")

        scroll_container = GUIScrollContainer(scroll_v=True, scroll_h=True)
        master_layout.set_child_dock(scroll_container, GUIDockLayout.CENTER)

        self.component_layout = GUIStackLayout()
        scroll_container.set_child(self.component_layout)

    # Sets up the components for the viewer
    def setup_components(self):
        if self.envedit_data is not None and self.envedit_data.target_node is not None:
            # Remove drawers that don't exist
            remove_list = []
            for i in range(len(self.component_layout.children)):
                drawer = self.component_layout.children[i]
                if i >= len(self.envedit_data.target_node.data) or drawer.component is not self.envedit_data.target_node.data[i]:
                    remove_list.append(drawer)
            for drawer in remove_list:
                self.component_layout.remove_child(drawer)

            # Add new drawers
            for i in range(len(self.envedit_data.target_node.data)):
                component = self.envedit_data.target_node.data[i]
                if i >= len(self.component_layout.children) or component is not self.component_layout.children[i].component:
                    drawer = ComponentDrawer(component)
                    drawer.set_envedit_data(self.envedit_data)
                    self.component_layout.add_child(drawer)

    # Clears the component viewer
    def clear_viewer(self):
        self.component_layout.clear()

    # Sets the data model
    def set_envedit_data(self, envedit_data):
        self.envedit_data = envedit_data

    # Updates the viewer
    def update_viewer(self):
        if self.envedit_data is not None and self.envedit_data.target_node is not None:
            # Only add "add component" button if scene root isn't selected
            self.layout.remove_child(self.add_component_dropdown)
            if self.envedit_data.target_node is not self.envedit_data.scene_root:
                self.layout.add_child(self.add_component_dropdown, 2)

            # Set up the node's components
            self.setup_components()
            self.update()

    # Sets the list of components from configuration file
    def set_components(self, components):
        self.add_component_dropdown.menu.child.clear()
        self.components = components
        # Add "add component" menu items
        for component in self.components:
            component_item = GUIMenuItem()
            component_item.child.set_text(EComponent.from_script(component).name)
            component_item.on_release = self.add_component_handler
            component_item.data = component
            self.add_component_dropdown.menu.child.add_child(component_item)

    # Handles a component to add being selected
    def add_component_handler(self, item):
        component = EComponent.from_script(item.data)
        self.envedit_data.target_node.add_component(component)
        self.envedit_data.target_node.component_property_changed_selected()
        self.envedit_data.modify()
        self.envedit_data.update()