Beispiel #1
0
class MyButton:
    MODE_ACTIVATED_COLOR = (242, 142, 48)
    MODE_DEACTIVATED_COLOR = (219, 185, 151)
    MODE_DISABLED_COLOR = (181, 176, 171)

    def __init__(self,
                 callback,
                 panel_pos,
                 name,
                 label,
                 x,
                 y,
                 w,
                 h,
                 activated_color=GREEN,
                 deactivated_color=GREY):
        self.callback = callback
        self.panel_pos = panel_pos
        self.name = name
        self.activated_color = activated_color
        self.deactivated_color = deactivated_color
        self.gui_element = Button(self._do_action,
                                  (x + panel_pos[0], y + panel_pos[1]), (w, h),
                                  label,
                                  self.activated_color,
                                  anchor=TOPLEFT)
        self.down = False
        self.activated = True
        self.disabled = False
        self.handle_mouse_events = True

    def on_mouse_up(self):
        if self.down and not self.disabled:
            self.gui_element.unfocus()
            self.gui_element.release()
            self.down = False

    def on_mouse_down(self):
        if not self.down and not self.disabled:
            self.down = True
            self.gui_element.focus()
            self.gui_element.click()

    def activate(self):
        if not self.activated:
            self.gui_element.color = self.activated_color
            self.activated = True

    def deactivate(self):
        if self.activated:
            self.gui_element.color = self.deactivated_color
            self.activated = False

    def set_disabled(self, value):
        if value != self.disabled:
            self.disabled = value
            if self.disabled:
                self.gui_element.color = self.MODE_DISABLED_COLOR
            else:
                self.gui_element.color = self.activated_color

    def _do_action(self):
        self.callback()