示例#1
0
class IntegralSlider(Slider):
    '''
    Identical to a regular slider, except the values are integers, and the
    marker snaps between integral chunks on the slide.
    '''
    def __init__(self):
        super(IntegralSlider, self).__init__()
        self.on_choice_observable = Observable()
        self._choices = [0, 1]
        self._index = 0

    @property
    def index(self):
        return self._index

    @index.setter
    def index(self, index):
        if not self.index == index:
            self._index = index
            self.value = index / (len(self.choices) - 1)
            self.on_choice_observable.on_event(self)

    @property
    def choice(self):
        return self.choices[self._index]

    @choice.setter
    def choice(self, choice):
        if choice in self.choices:
            self.index = self.choices.index(choice)

    @property
    def choices(self):
        return self._choices

    @choices.setter
    def choices(self, choices):
        if choices and isinstance(choices, list):
            self._choices = choices
            self._index = -1  # This is a hack to activate the
            self.index = 0    # on_choice_observable event when setting index

    def on_mouse_move(self, x, y, dx, dy, buttons, modifiers):
        if self.actively_dragging:
            # Get position of mouse from where we start measuring after the
            # middle of the marker
            x = x - self.absolute_position.x - (self.marker.size.x / 2)
            total_distance = self.size.x - self.marker.size.x
            length_of_segment = total_distance / (len(self.choices) - 1)
            closest_segment = round(x / length_of_segment)

            # Clamp choice
            closest_choice = min(len(self.choices)-1, max(0, closest_segment))
            # logging.debug('Closest to {}'.format(closest_choice))
            if not self.index == closest_choice:
                self.index = closest_choice
示例#2
0
class Checkbox(ColoredRectangle):
    def __init__(self):
        super(Checkbox, self).__init__()
        self.fill_color = [255, 255, 255, 255]
        self.fill = ColoredRectangle()
        self.fill.color = self.fill_color
        self.fill.layout.add(
            partial(center_inside, item=self.fill, base_object=self)
        )
        self._checked = True
        self.checked_observable = Observable()

    def on_resize(self):
        self.fill.size.x = self.size.x - 10
        self.fill.size.y = self.size.y - 10

    def on_added_as_child(self):
        super(Checkbox, self).on_added_as_child()
        self.add_child(self.fill)

    def on_mouse_pressed(self, x, y, buttons, modifiers):
        rectangle = AABox()
        rectangle.position = self.absolute_position
        rectangle.size = self.size
        if rectangle.contains(x,y):
            self.checked = not self.checked

    @property
    def checked(self):
        return self._checked

    @checked.setter
    def checked(self, state):
        self._checked = state
        logging.debug(
            'Box is {}'.format('checked' if self.checked else 'unchecked')
        )
        if self.checked:
            self.fill.color = self.fill_color
        else:
            tmp_color = list(self.fill_color)
            tmp_color[-1] = 0
            self.fill.color = tmp_color
        self.checked_observable.on_event(self)
示例#3
0
 def __init__(self):
     super(Checkbox, self).__init__()
     self.fill_color = [255, 255, 255, 255]
     self.fill = ColoredRectangle()
     self.fill.color = self.fill_color
     self.fill.layout.add(
         partial(center_inside, item=self.fill, base_object=self)
     )
     self._checked = True
     self.checked_observable = Observable()
示例#4
0
 def __init__(self):
     super(IntegralSlider, self).__init__()
     self.on_choice_observable = Observable()
     self._choices = [0, 1]
     self._index = 0