Exemplo n.º 1
0
class DemoFramework:
    """ This is a small helper class to setup common stuff for the demos """
    def __init__(self):
        """ Constructs the demo framework """

        if False:
            self._skin = LUIMetroSkin()
            base.win.set_clear_color(Vec4(1))
        else:
            self._skin = LUIDefaultSkin()
            base.win.set_clear_color(Vec4(0.1, 0.0, 0.0, 1))
        self._skin.load()

        # Construct the LUIRegion
        region = LUIRegion.make("LUI", base.win)
        handler = LUIInputHandler()
        base.mouseWatcher.attach_new_node(handler)
        region.set_input_handler(handler)

        self._root = region.root
        self._constructor_params = []

    def prepare_demo(self, demo_title=u"Some Demo"):

        # Background
        self._background = LUISprite(self._root, "res/DemoBackground.png")
        # Make the background solid and recieve events
        self._background.solid = True

        # Logo
        self._logo = LUISprite(self._root, "res/LUILogo.png")
        self._logo.top_left = 15, 20

        # Title
        self._title_label = LUILabel(parent=self._root,
                                     text=demo_title,
                                     font_size=40,
                                     font="header",
                                     pos=(120, 27))
        self._subtitle_label = LUILabel(parent=self._root,
                                        text="Widget Demo",
                                        font_size=14,
                                        font="default",
                                        pos=(121, 70),
                                        alpha=0.3)

        # Right bar

        self._right_bar = LUIVerticalLayout(parent=self._root)
        self._left_bar = LUIVerticalLayout(parent=self._root)
        self._right_bar.width = 350
        self._right_bar.pos = (410, 120)
        self._right_bar.spacing = 10
        self._left_bar.width = 350
        self._left_bar.pos = (20, 120)
        self._left_bar.spacing = 10

        # Public functions
        self._public_functions = LUIFrame(width=340, style=LUIFrame.FS_sunken)
        self._functions_label = LUILabel(text=U"Additional Public functions")
        self._functions_layout = LUIVerticalLayout(
            parent=self._public_functions)
        self._functions_layout.add(self._functions_label, 30)

        # Events
        self._events = LUIFrame(width=340, style=LUIFrame.FS_sunken)
        self._events_label = LUILabel(text=U"Additional Events")
        self._events_layout = LUIVerticalLayout(parent=self._events)
        self._events_layout.add(self._events_label, 30)

        # Actions
        self._actions = LUIFrame(width=340, style=LUIFrame.FS_sunken)
        self._actions_label = LUILabel(parent=self._actions,
                                       text=U"Demo-Actions")
        self._actions_select = LUISelectbox(parent=self._actions,
                                            width=225,
                                            top=30)
        self._actions_btn = LUIButton(parent=self._actions,
                                      right=0,
                                      top=30,
                                      text=u"Execute",
                                      template="ButtonGreen")
        self._actions_btn.bind("click", self._exec_action)

        # Properties
        self._properties = LUIFrame(width=340, style=LUIFrame.FS_sunken)
        self._properties_label = LUILabel(text=u"Additional Properties")
        self._properties_layout = LUIVerticalLayout(parent=self._properties)
        self._properties_layout.add(self._properties_label, 30)

        self._right_bar.add(self._actions)
        self._right_bar.add(self._public_functions)
        self._right_bar.add(self._properties)
        self._right_bar.add(self._events)

        # Widget
        self._widget_container = LUIFrame(width=360,
                                          height=250,
                                          style=LUIFrame.FS_sunken)
        self._widget_label = LUILabel(parent=self._widget_container,
                                      text=u"Widget Demo")
        self._left_bar.add(self._widget_container)

        # Source Code
        self._source_container = LUIFrame(width=360,
                                          height=190,
                                          style=LUIFrame.FS_sunken)
        self._source_label = LUILabel(parent=self._source_container,
                                      text=u"Default Constructor")
        self._copy_code_button = LUIButton(parent=self._source_container,
                                           text=u"Copy to Clipboard",
                                           template="ButtonGreen",
                                           bottom_right=(0, 0))
        self._source_content = LUIObject(self._source_container)
        self._source_content.top = 40
        self._left_bar.add(self._source_container)

        self._widget_node = LUIObject(self._widget_container, x=0, y=40)

    def _exec_action(self, event):
        selected = self._actions_select.get_selected_option()
        if selected is not None:
            selected()

    def set_actions(self, actions):
        opts = []

        for name, action in actions.items():
            opts.append((action, name))

        self._actions_select.set_options(opts)

    def add_public_function(self, name, parameters=None, return_type="void"):
        label = LUIFormattedLabel()
        label.add(text=return_type + " ",
                  color=(102 / 255.0, 217 / 255.0, 239 / 255.0))
        label.add(text=name + " ",
                  color=(166 / 255.0, 226 / 255.0, 46 / 255.0))

        label.add(text="( ", color=(0.9, 0.9, 0.9))

        if parameters is not None:
            for index, (pname, ptype) in enumerate(parameters):
                label.add(text=pname,
                          color=(255 / 255.0, 151 / 255.0, 31 / 255.0))
                label.add(text=" : ", color=(0.9, 0.9, 0.9))
                label.add(text=ptype,
                          color=(102 / 255.0, 217 / 255.0, 239 / 255.0))

                if index < len(parameters) - 1:
                    label.add(text=",", color=(0.9, 0.9, 0.9))
        label.add(text=" )", color=(0.9, 0.9, 0.9))
        self._functions_layout.add(label)
        self.update_layouts()

    def add_constructor_parameter(self, name, default):
        self._constructor_params.append((name, default))
        self.update_layouts()

    def add_event(self, event_name):
        label = LUILabel(text=event_name)
        label.color = (1, 1, 1, 0.5)
        self._events_layout.add(label)
        self.update_layouts()

    def add_property(self, property_name, property_type):
        label = LUIFormattedLabel()
        label.add(text=property_name,
                  color=(255 / 255.0, 151 / 255.0, 31 / 255.0))
        label.add(" : ", color=(0.9, 0.9, 0.9))
        label.add(text=property_type + " ",
                  color=(102 / 255.0, 217 / 255.0, 239 / 255.0))
        self._properties_layout.add(label)
        self.update_layouts()

    def update_layouts(self):
        pass

    def construct_sourcecode(self, classname):
        self._source_content.remove_all_children()
        label = LUIFormattedLabel(parent=self._source_content)
        label.add(text="element ", color=(0.9, 0.9, 0.9))
        label.add(text="= ", color=(249 / 255.0, 38 / 255.0, 114 / 255.0))
        label.add(text=classname, color=(166 / 255.0, 226 / 255.0, 46 / 255.0))
        label.add(text="(", color=(0.9, 0.9, 0.9))

        for index, (pname, pvalue) in enumerate(self._constructor_params):
            label.newline()
            label.add(text=" " * 15)
            label.add(text=pname, color=(255 / 255.0, 151 / 255.0, 31 / 255.0))
            label.add(text=" = ")
            label.add(text=pvalue,
                      color=(153 / 255.0, 129 / 255.0, 255 / 255.0))

            if index < len(self._constructor_params) - 1:
                label.add(text=",")

        label.add(text=")")

        copy_text = "element = " + classname + "("

        for index, (pname, pvalue) in enumerate(self._constructor_params):
            copy_text += pname + "=" + pvalue

            if index < len(self._constructor_params) - 1:
                copy_text += ", "
        copy_text += ")"

        def copy_code(event):
            # Copies the source code to clipboard
            from Tkinter import Tk
            r = Tk()
            r.withdraw()
            r.clipboard_clear()
            r.clipboard_append(copy_text)
            r.destroy()

        self._copy_code_button.bind("click", copy_code)

        # self._source_content.fit_height_to_children()
        # self._source_container.fit_height_to_children()
        self._source_container.height += 40

    def get_widget_node(self):
        return self._widget_node
Exemplo n.º 2
0
class UISelectdrop(LUIObject):

    def __init__(self, parent, width=200):
        LUIObject.__init__(self, x=0, y=0, w=width, h=1, solid=True)

        self.layout = LUICornerLayout(parent=self, image_prefix="Selectdrop_", width=width + 10, height=100)
        self.layout.margin_left = -3

        self.opener = LUISprite(self, "SelectboxOpen_Right", "skin")
        self.opener.right = -4
        self.opener.top = -25
        self.opener.z_offset = 3

        self.container = LUIObject(self.layout, 0, 0, 0, 0)
        self.container.width = self.width
        self.container.clip_bounds = (0,0,0,0)
        self.container.left = 5

        self.selectbox = parent
        self.parent = self.selectbox

    def _on_opt_over(self, event):
        event.sender.color = (0,0,0,0.1)

    def _on_opt_out(self, event):
        event.sender.color = (0,0,0,0)

    def _on_opt_click(self, optid, event):
        self.selectbox._on_option_selected(optid)

    def _render_options(self, options):
        visible = min(4, len(options))
        offsetTop = 6
        self.layout.height = visible * 30 + offsetTop + 11
        self.container.height = visible * 30 + offsetTop + 1
        self.layout.update_layout()
        self.container.remove_all_children()
        
        currentY = offsetTop
        for optId, optVal in options:
            optContainer = LUIObject(self.container, x=0, y=currentY, w=self.container.width - 30, h=30)

            optBg = LUISprite(optContainer, "blank", "skin")
            optBg.width = self.container.width
            optBg.height = optContainer.height
            optBg.color = (0,0,0,0)
            optBg.bind("mouseover", self._on_opt_over)
            optBg.bind("mouseout", self._on_opt_out)
            optBg.bind("click", partial(self._on_opt_click, optId))
            optBg.solid = True

            optLabel = LUILabel(parent=optContainer, text=unicode(optVal), shadow=True)
            optLabel.top = 5
            optLabel.left = 8

            if optId == self.selectbox.get_selected_option():
                optLabel.color = (0.6, 0.9, 0.4, 1.0)

            divider = LUISprite(optContainer, "SelectdropDivider", "skin")
            divider.top = 30 - divider.height / 2
            divider.width = self.container.width

            currentY += 30
Exemplo n.º 3
0
class DemoFramework:

    """ This is a small helper class to setup common stuff for the demos """

    def __init__(self):
        base.win.set_clear_color(Vec4(0, 0, 0, 1))
        self.skin = LUIDefaultSkin()
        self.skin.load()

        # Construct the LUIRegion
        region = LUIRegion.make("LUI", base.win)
        handler = LUIInputHandler()
        base.mouseWatcher.attach_new_node(handler)
        region.set_input_handler(handler)

        self.root = region.root()
        self.constructorParams = []

    def prepare_demo(self, demo_title=u"Some Demo"):

        # Background
        self.background = LUISprite(self.root, "res/DemoBackground.png")

        
        # Make the background solid and recieve events
        self.background.bind("click", lambda event: self.background.request_focus())
        self.background.solid = True

        # Logo
        self.logo = LUISprite(self.root, "res/LUILogo.png")
        self.logo.top = 15
        self.logo.left = 20

        # Title
        self.titleLabel = LUILabel(parent=self.root, text=demo_title, font_size=40, font="header")
        self.titleLabel.pos = (120, 20)
        self.subtitleLabel = LUILabel(parent=self.root, text="Widget Demo", font_size=14, font="default")
        self.subtitleLabel.pos = (121, 65)
        self.subtitleLabel.color = (1,1,1,0.5)

        # Right bar
        self.rightBar = LUIVerticalLayout(parent=self.root, width=350, spacing=20)
        self.rightBar.pos = (410, 120)

        # Constructor parameters
        # self.constructorParameters = LUIFrame(width=340, style=LUIFrame.Sunken)
        # self.constructorLabel = LUILabel(parent=self.constructorParameters, text=u"Additional Constructor Parameters")
        # self.constructorLayout = UIVerticalLayout(parent=self.constructorParameters, spacing=10, use_dividers=True)
        # self.constructorLayout.top = 30

        # Public functions
        self.publicFunctions = LUIFrame(width=340, style=LUIFrame.Sunken)
        self.functionsLabel = LUILabel(parent=self.publicFunctions, text=U"Additional Public functions")
        self.functionsLayout = LUIVerticalLayout(parent=self.publicFunctions,spacing=10, use_dividers=True)
        self.functionsLayout.top = 30

        # Events
        self.events = LUIFrame(width=340,style=LUIFrame.Sunken)
        self.eventsLabel = LUILabel(parent=self.events, text=U"Additional Events")
        self.eventsLayout = LUIVerticalLayout(parent=self.events, spacing=10, use_dividers=True)
        self.eventsLayout.top = 30

        # Actions
        self.actions = LUIFrame(width=340,style=LUIFrame.Sunken, height=80)
        self.actionsLabel = LUILabel(parent=self.actions, text=U"Demo-Actions")
        self.actionsSelect = LUISelectbox(parent=self.actions, width=245, top=30)
        self.actionsBtn = LUIButton(parent=self.actions, right=0, top=30, text=u"Execute", template="ButtonMagic")
        self.actionsBtn.bind("click", self._exec_action)

        self.rightBar.add_row(self.actions)
        # self.rightBar.add_row(self.constructorParameters)
        self.rightBar.add_row(self.publicFunctions)
        self.rightBar.add_row(self.events)

        # Widget
        self.widgetContainer = LUIFrame(parent=self.root, width=360, height=250, style=LUIFrame.Sunken)
        self.widgetLabel = LUILabel(parent=self.widgetContainer, text=u"Widget Demo")
        self.widgetContainer.left = 26
        self.widgetContainer.top = 120

        # Source Code
        self.sourceContainer = LUIFrame(parent=self.root, width=360, height=200, style=LUIFrame.Sunken)
        self.sourceLabel = LUILabel(parent=self.sourceContainer, text=u"Default Constructor")
        self.copyCodeButton = LUIButton(parent=self.sourceContainer, 
                text=u"Copy to Clipboard", template="ButtonMagic", 
                right=-5, bottom=-5)
        self.sourceContainer.left = 26
        self.sourceContainer.top = 390
        self.sourceContent = LUIObject(self.sourceContainer)
        self.sourceContent.top = 40


        self.widgetNode = LUIObject(self.widgetContainer, x=0, y=40)


    def _exec_action(self, event):
        selected = self.actionsSelect.get_selected_option()
        if selected is not None:
            selected()

    def set_actions(self, actions):
        opts = []

        for name, action in actions.items():
            opts.append((action, name))

        self.actionsSelect.set_options(opts)

    def add_public_function(self, name, parameters=None, return_type="void"):
        label = LUIFormattedLabel()
        label.add_text(text=return_type + " ", color = (102/255.0, 217/255.0, 239/255.0))
        label.add_text(text=name + " ", color = (166/255.0, 226/255.0, 46/255.0))

        label.add_text(text="( ", color=(0.9,0.9,0.9))

        if parameters is not None:
            for index, (pname, ptype) in enumerate(parameters):
                label.add_text(text=pname, color=(255/255.0, 151/255.0, 31/255.0))
                label.add_text(text=" : ", color=(0.9,0.9,0.9))
                label.add_text(text=ptype, color=(102/255.0, 217/255.0, 239/255.0))

                if index < len(parameters) - 1:
                    label.add_text(text=",", color=(0.9,0.9,0.9))
        label.add_text(text=" )", color=(0.9,0.9,0.9))
        self.functionsLayout.add_row(label)
        self.update_layouts()

    def add_constructor_parameter(self, name, default):
        # label = UIFormattedLabel()
        # label.add_text(text=name, color=(255/255.0, 151/255.0, 31/255.0))
        # label.add_text(text=" = ", color=(249/255.0, 38/255.0, 114/255.0))
        # label.add_text(text=default, color=(153/255.0, 129/255.0, 255/255.0))
        # self.constructorLayout.add_row(label)
        self.constructorParams.append((name, default))
        self.update_layouts()

    def add_event(self, event_name):
        label = LUILabel(text=event_name)
        label.color = (1,1,1,0.5)
        self.eventsLayout.add_row(label)
        self.update_layouts()

    def update_layouts(self):
        self.publicFunctions.fit_height_to_children()
        # self.constructorParameters.fit_height_to_children()
        self.events.fit_height_to_children()
        self.rightBar.update()

    def construct_sourcecode(self, classname):
        self.sourceContent.remove_all_children()
        label = LUIFormattedLabel(parent=self.sourceContent)
        label.add_text(text="element ", color=(0.9,0.9,0.9))
        label.add_text(text="= ", color=(249/255.0, 38/255.0, 114/255.0))
        label.add_text(text=classname, color=(166/255.0, 226/255.0, 46/255.0))
        label.add_text(text="(", color=(0.9,0.9,0.9))

        for index, (pname, pvalue) in enumerate(self.constructorParams):
            label.br()
            label.add_text(text=" " * 15)
            label.add_text(text=pname, color=(255/255.0, 151/255.0, 31/255.0))
            label.add_text(text=" = ")
            label.add_text(text=pvalue, color=(153/255.0, 129/255.0, 255/255.0))

            if index < len(self.constructorParams) - 1:
                label.add_text(text=",")

        label.add_text(text=")")

        self.sourceContent.fit_height_to_children()
        self.sourceContainer.fit_height_to_children()
        self.sourceContainer.height += 40


    def get_widget_node(self):
        return self.widgetNode
Exemplo n.º 4
0
class LUISelectdrop(LUIObject):
    """ Internal class used by the selectbox, representing the dropdown menu """
    def __init__(self, parent, width=200):
        LUIObject.__init__(self, x=0, y=0, w=width, h=1, solid=True)

        self._layout = LUICornerLayout(parent=self,
                                       image_prefix="Selectdrop_",
                                       width=width + 10,
                                       height=100)
        self._layout.margin.left = -3

        self._opener = LUISprite(self, "SelectboxOpen_Right", "skin")
        self._opener.right = -4
        self._opener.top = -25
        self._opener.z_offset = 3

        self._container = LUIObject(self._layout, 0, 0, 0, 0)
        self._container.width = self.width
        self._container.clip_bounds = (0, 0, 0, 0)
        self._container.left = 5
        self._container.solid = True
        self._container.bind("mousedown", lambda *args: self.request_focus())

        self._selectbox = parent
        self._option_focus = False
        self.parent = self._selectbox

    def _on_opt_over(self, event):
        """ Inernal handler when an option got hovered """
        event.sender.color = (0, 0, 0, 0.1)

    def _on_opt_out(self, event):
        """ Inernal handler when an option got no longer hovered """
        event.sender.color = (0, 0, 0, 0)

    def _on_opt_click(self, opt_id, event):
        """ Internal handler when an option got clicked """
        self._selectbox._on_option_selected(opt_id)

    def _render_options(self, options):
        """ Internal method to update the options """
        num_visible_options = min(4, len(options))
        offset_top = 6
        self._layout.height = num_visible_options * 30 + offset_top + 11
        self._container.height = num_visible_options * 30 + offset_top + 1
        self._container.remove_all_children()

        current_y = offset_top
        for opt_id, opt_val in options:
            opt_container = LUIObject(self._container,
                                      x=0,
                                      y=current_y,
                                      w=self._container.width - 30,
                                      h=30)

            opt_bg = LUISprite(opt_container, "blank", "skin")
            opt_bg.width = self._container.width
            opt_bg.height = opt_container.height
            opt_bg.color = (0, 0, 0, 0)
            opt_bg.bind("mouseover", self._on_opt_over)
            opt_bg.bind("mouseout", self._on_opt_out)
            opt_bg.bind("mousedown", lambda *args: self.request_focus())
            opt_bg.bind("click", partial(self._on_opt_click, opt_id))
            opt_bg.solid = True

            opt_label = LUILabel(parent=opt_container, text=unicode(opt_val))
            opt_label.top = 8
            opt_label.left = 8

            if opt_id == self._selectbox.selected_option:
                opt_label.color = (0.6, 0.9, 0.4, 1.0)

            divider = LUISprite(opt_container, "SelectdropDivider", "skin")
            divider.top = 30 - divider.height / 2
            divider.width = self._container.width

            current_y += 30