def on_touch(self, xy, action): handle_size = self.get_handle_size() if action == "down": handle_pos = _topleft_from_aligned_xy(self.get_handle_position(), "center", handle_size, self.parent.size) handle_rect = pygame.Rect(handle_pos, handle_size) if handle_rect.collidepoint(xy): self.pressed = True if action in ("move", "up"): if self.vertical: pos = 1.0 - float(xy[1] - handle_size[1] / 2) / (self.size[1] - handle_size[1]) else: pos = float(xy[0] - handle_size[0] / 2) / (self.size[0] - handle_size[0]) new_pos = self.min_val + pos * (self.max_val - self.min_val) new_pos = clamp(self.min_val, self.max_val, new_pos) if self.pressed: self.value = new_pos if self.callback: self.callback(self.value) self.update() elif action == "up": if new_pos > (self.value + self.step): self.value += self.step elif new_pos < (self.value - self.step): self.value -= self.step else: self.value = new_pos if self.callback: self.callback(self.value) self.update() if action in ("up", "drag_up"): self.pressed = False if self.release_callback: self.release_callback(self.value)
def __init__(self, xy, size, align="center", parent=None, style=None): """Initialise this widget. It creates it's own subsurface for drawing on xy, size and align specify the position of the widget if this widget will live in a sub-container, such as ScrollArea, specify this with parent otherwise it will be attached to the main screen xy is relative to the parent widget (or screen) style is an instance of Style to specify the appearance of the widget """ if parent: self.parent = parent else: from .container import get_root_widget self.parent = get_root_widget() if style: self.style = style else: self.style = get_default_style() self.xy = _topleft_from_aligned_xy(xy, align, size, self.parent.size) self.visible = True self.init_size = size self.parent.add_child(self)
def __init__(self, xy, size, align="center", parent=None, style=None): """Initialise this widget. It creates it's own subsurface for drawing on xy, size and align specify the position of the widget if this widget will live in a sub-container, such as ScrollArea, specify this with parent otherwise it will be attached to the main screen xy is relative to the parent widget (or screen) style is an instance of Style to specify the appearance of the widget """ if parent: self.parent = parent else: from .container import get_root_widget self.parent = get_root_widget() if style: self.style = style else: self.style = get_default_style() self.xy = _topleft_from_aligned_xy(xy, align, size, self.parent.size) self.visible = True self.init_size = size self.parent.add_child(self)
def on_touch(self, xy, action): handle_size = self.get_handle_size() if action == "down": handle_pos = _topleft_from_aligned_xy(self.get_handle_position(), "center", handle_size, self.parent.size) handle_rect = pygame.Rect(handle_pos, handle_size) if handle_rect.collidepoint(xy): self.pressed = True if action in ("move", "up"): if self.vertical: pos = 1.0 - \ float(xy[1] - handle_size[1] / 2) / ( self.size[1] - handle_size[1]) else: pos = float(xy[0] - handle_size[0] / 2) / (self.size[0] - handle_size[0]) new_pos = self.min_val + pos * (self.max_val - self.min_val) new_pos = clamp(self.min_val, self.max_val, new_pos) if self.pressed: self.value = new_pos if self.callback: self.callback(self.value) self.update() elif action == "up": if new_pos > (self.value + self.step): self.value += self.step elif new_pos < (self.value - self.step): self.value -= self.step else: self.value = new_pos if self.callback: self.callback(self.value) self.update() if action in ("up", "drag_up"): self.pressed = False if self.release_callback: self.release_callback(self.value)