예제 #1
0
class LUISpriteButton(LUIObject):

    """ Simple button that uses only two images: Default and focus. """
    def __init__(self, template="ButtonDefault", width = 50, height = 50, **kwargs):
        LUIObject.__init__(self, x=0, y=0, w=0, h=0, solid=True)
        LUIInitialState.init(self, kwargs)
        self.margin_left = -1
        self.template = template

        self.fit_to_children()

        self.button_sprite = LUISprite(self, template, "skin")
        self.fit_to_children()

        self.width = width
        self.height = height

        self.button_sprite.width = width
        self.button_sprite.height = height

    def on_mousedown(self, event):
        """ Internal on_mousedown handler. Do not override """
        self.button_sprite.set_texture(self.template + "Focus", "skin", resize=False)

    def on_mouseup(self, event):
        """ Internal on_mouseup handler. Do not override """
        self.button_sprite.set_texture(self.template, "skin", resize=False)

    def on_click(self, event):
        """ Internal onclick handler. Do not override """
        self.trigger_event("changed")
        self._update_sprite()
예제 #2
0
class LUICheckbox(LUIObject):

    """ This is a simple checkbox, including a Label. The checkbox can either
    be checked or unchecked. """

    def __init__(self, checked=False, label=u"Checkbox", **kwargs):
        """ Constructs a new checkbox with the given label and state. """
        LUIObject.__init__(self, x=0, y=0, w=0, h=0, solid=True)
        LUIInitialState.init(self, kwargs)
        self.checkboxSprite = LUISprite(self, "Checkbox_Default", "skin")
        self.label = LUILabel(parent=self, text=label, shadow=True, left=self.checkboxSprite.width + 6)
        self.label.top = self.label.height - self.checkboxSprite.height
        self.label.bind("resized", self._on_label_resized)
        self.fit_to_children()

        self.checked = checked
        self._update_sprite()

    def _on_label_resized(self, event):
        """ Internal handler when the text of the label got changed """
        self.fit_to_children()

    def on_click(self, event):
        """ Internal onclick handler. Do not override """
        self.checked = not self.checked
        self.trigger_event("changed")
        self._update_sprite()

    def on_mousedown(self, event):
        """ Internal mousedown handler. Do not override """
        self.checkboxSprite.color = (0.86,0.86,0.86,1.0)

    def on_mouseup(self, event):
        """ Internal on_mouseup handler. Do not override """
        self.checkboxSprite.color = (1,1,1,1)

    def get_checked(self):
        """ Returns a boolean wheter the checkbox is currently checked """
        return self.checked

    def toggle_checked(self):
        """ Toggles the checkbox state """
        self.set_checked(not self.get_checked())

    def set_checked(self, checked):
        """ Sets the checkbox state """
        self.checked = checked
        self._update_sprite()
        
    def get_label(self):
        """ Returns a handle to the label, so it can be modified (e.g. change 
            its text) """
        return self.label

    def _update_sprite(self):
        """ Internal method to update the sprites """
        img = "Checkbox_Checked" if self.checked else "Checkbox_Default"
        self.checkboxSprite.set_texture(img, "skin")
예제 #3
0
class LUIHorizontalStretchedLayout(LUIObject):
    """ A layout which takes 3 sprites, a left sprite, a right sprite, and a
    middle sprite. While the left and right sprites remain untouched, the middle
    one will be stretched to fit the layout """
    def __init__(self, parent=None, prefix="ButtonDefault", **kwargs):
        LUIObject.__init__(self)
        self._layout = LUIHorizontalLayout(self, spacing=0)
        self._layout.width = "100%"
        self._sprite_left = LUISprite(self._layout.cell(), "blank", "skin")
        self._sprite_mid = LUISprite(self._layout.cell('*'), "blank", "skin")
        self._sprite_right = LUISprite(self._layout.cell(), "blank", "skin")
        if parent is not None:
            self.parent = parent
        self.prefix = prefix
        LUIInitialState.init(self, kwargs)

    def set_prefix(self, prefix):
        """ Sets the layout prefix, this controls which sprites will be used """
        self._sprite_left.set_texture(prefix + "_Left", "skin")
        self._sprite_mid.set_texture(prefix, "skin")
        self._sprite_right.set_texture(prefix + "_Right", "skin")
        self._sprite_mid.width = "100%"
        self._prefix = prefix

    def get_prefix(self):
        """ Returns the layout prefix """
        return self._prefix

    prefix = property(get_prefix, set_prefix)
예제 #4
0
class LUISpriteButton(LUIObject):
    """ Simple button that uses only two images: Default and focus. """
    def __init__(self, template="ButtonDefault", **kwargs):
        LUIObject.__init__(self, x=0, y=0, solid=True)
        self._template = template
        self._button_sprite = LUISprite(self, template, "skin")
        self._button_sprite.width = width
        self._button_sprite.height = height
        LUIInitialState.init(self, kwargs)

    def on_mousedown(self, event):
        """ Internal on_mousedown handler. Do not override """
        self._button_sprite.set_texture(self.template + "Focus",
                                        "skin",
                                        resize=False)

    def on_mouseup(self, event):
        """ Internal on_mouseup handler. Do not override """
        self._button_sprite.set_texture(self.template, "skin", resize=False)

    def on_click(self, event):
        """ Internal onclick handler. Do not override """
        self.trigger_event("changed")
예제 #5
0
class LUIRadiobox(LUIObject):

    """ A radiobox which can be used in combination with a LUIRadioboxGroup """

    def __init__(self, group=None, value=None, active=False, label=u"Radiobox", **kwargs):
        LUIObject.__init__(self, x=0, y=0, w=0, h=0, solid=True)
        LUIInitialState.init(self, kwargs)

        self.sprite = LUISprite(self, "Radiobox_Default", "skin")
        self.label = LUILabel(parent=self, text=label, shadow=True, left=self.sprite.width+6)
        self.label.top = self.label.height - self.sprite.height
        self.label.bind("resized", self._on_label_resized)

        self.fit_to_children()
        self.group = group
        self.group.register_box(self)
        self.active = False
        self.value = value

        if active:
            self.set_active()

    def _on_label_resized(self, event):
        """ Internal handler when the text of the label got changed """
        self.fit_to_children()

    def on_click(self, event):
        """ Internal onclick handler. Do not override. """
        self.set_active()

    def set_active(self):
        """ Internal function to set the radiobox active """
        if self.group is not None:
            self.group.set_active(self)
        else:
            self._update_state(True)

    def get_value(self):
        """ Returns the value of the radiobox """
        return self.value

    def get_label(self):
        """ Returns a handle to the label, so it can be modified (e.g. change 
            its text) """
        return self.label

    def _update_state(self, active):
        """ Internal method to update the state of the radiobox. Called by the
        LUIRadioboxGroup """
        self.active = active
        self.trigger_event("changed")
        self._update_sprite()

    def on_mousedown(self, event):
        """ Internal onmousedown handler. Do not override. """
        self.color = (0.86,0.86,0.86,1.0)

    def on_mouseup(self, event):
        """ Internal onmouseup handler. Do not override. """
        self.color = (1,1,1,1)

    def _update_sprite(self):
        """ Internal function to update the sprite of the radiobox """
        img = "Radiobox_Active" if self.active else "Radiobox_Default"
        self.sprite.set_texture(img, "skin")
예제 #6
0
class LUICheckbox(LUIObject):
    """ This is a simple checkbox, including a Label. The checkbox can either
    be checked or unchecked. """
    def __init__(self, checked=False, label=u"Checkbox", **kwargs):
        """ Constructs a new checkbox with the given label and state. By default,
        the checkbox is not checked. """
        LUIObject.__init__(self, x=0, y=0, solid=True)
        self._checked = checked
        self._checkbox_sprite = LUISprite(self, "Checkbox_Default", "skin")
        self._label = LUILabel(parent=self,
                               text=label,
                               margin=(0, 0, 0, 25),
                               center_vertical=True,
                               alpha=0.4)
        self._hovered = False
        LUIInitialState.init(self, kwargs)

    def on_click(self, event):
        """ Internal onclick handler. Do not override """
        self._checked = not self._checked
        self.trigger_event("changed")
        self._update_sprite()

    def on_mousedown(self, event):
        """ Internal mousedown handler. """
        self._checkbox_sprite.color = (0.9, 0.9, 0.9, 1.0)

    def on_mouseup(self, event):
        """ Internal on_mouseup handler. """
        self._checkbox_sprite.color = (1, 1, 1, 1)

    def on_mouseover(self, event):
        """ Internal mouseover handler """
        self._hovered = True
        self._update_sprite()

    def on_mouseout(self, event):
        """ Internal mouseout handler """
        self._hovered = False
        self._update_sprite()

    def toggle_checked(self):
        """ Toggles the checkbox state """
        self.checked = not self.checked

    def set_checked(self, checked):
        """ Sets the checkbox state """
        self._checked = checked
        self._update_sprite()

    def get_checked(self):
        """ Returns a boolean whether the checkbox is currently checked """
        return self._checked

    checked = property(get_checked, set_checked)

    def get_label(self):
        """ Returns a handle to the label, so it can be modified (e.g. changing
            its text) """
        return self._label

    label = property(get_label)

    def _update_sprite(self):
        """ Internal method to update the sprites """
        img = "Checkbox_Checked" if self._checked else "Checkbox_Default"
        if self._hovered:
            img += "Hover"
        self._checkbox_sprite.set_texture(img, "skin")
예제 #7
0
class LUIRadiobox(LUIObject):
    """ A radiobox which can be used in combination with a LUIRadioboxGroup """
    def __init__(self,
                 parent=None,
                 group=None,
                 value=None,
                 active=False,
                 label=u"Radiobox",
                 **kwargs):
        """ Constructs a new radiobox. group should be a handle to a LUIRadioboxGroup.
        value will be the value returned by group.value, in case the box was
        selected. By default, the radiobox is not active. """
        assert group is not None, "LUIRadiobox needs a LUIRadioboxGroup!"
        LUIObject.__init__(self, x=0, y=0, solid=True)
        self._sprite = LUISprite(self, "Radiobox_Default", "skin")
        self._label = LUILabel(parent=self,
                               text=label,
                               margin=(0, 0, 0, 23),
                               center_vertical=True)
        self._value = value
        self._active = False
        self._hovered = False
        self._group = group
        self._group.register_box(self)
        if active:
            self.set_active()
        if parent:
            self.parent = parent
        LUIInitialState.init(self, kwargs)

    def on_click(self, event):
        """ Internal onclick handler. Do not override. """
        self.set_active()

    def on_mouseover(self, event):
        """ Internal mouseover handler """
        self._hovered = True
        self._update_sprite()

    def on_mouseout(self, event):
        """ Internal mouseout handler """
        self._hovered = False
        self._update_sprite()

    def set_active(self):
        """ Internal function to set the radiobox active """
        if self._group is not None:
            self._group.set_active_box(self)
        else:
            self._update_state(True)

    def get_value(self):
        """ Returns the value of the radiobox """
        return self._value

    def set_value(self, value):
        """ Sets the value of the radiobox """
        self._value = value

    value = property(get_value, set_value)

    def get_label(self):
        """ Returns a handle to the label, so it can be modified (e.g. change
            its text) """
        return self._label

    label = property(get_label)

    def _update_state(self, active):
        """ Internal method to update the state of the radiobox. Called by the
        LUIRadioboxGroup """
        self._active = active
        self.trigger_event("changed")
        self._update_sprite()

    def on_mousedown(self, event):
        """ Internal onmousedown handler. Do not override. """
        self._sprite.color = (0.86, 0.86, 0.86, 1.0)

    def on_mouseup(self, event):
        """ Internal onmouseup handler. Do not override. """
        self._sprite.color = (1, 1, 1, 1)

    def _update_sprite(self):
        """ Internal function to update the sprite of the radiobox """
        img = "Radiobox_Active" if self._active else "Radiobox_Default"
        if self._hovered:
            img += "Hover"
        self._sprite.set_texture(img, "skin")