def on_touch_move(self, x, y): if self.start_x is None: return # not started in our area dirs = self.directions pdx = x - self.start_x pdy = y - self.start_y pdxa = abs(pdx) pdya = abs(pdy) if pdxa > pdya and dirs & SWIPE_HORIZONTAL: # horizontal direction if (pdx > 0 and dirs & SWIPE_RIGHT) or (pdx < 0 and dirs & SWIPE_LEFT): ui.display.backlight( ui.lerpi( self.light_origin, self.light_target, min(pdxa / _SWIPE_DISTANCE, 1), )) elif pdxa < pdya and dirs & SWIPE_VERTICAL: # vertical direction if (pdy > 0 and dirs & SWIPE_DOWN) or (pdy < 0 and dirs & SWIPE_UP): ui.display.backlight( ui.lerpi( self.light_origin, self.light_target, min(pdya / _SWIPE_DISTANCE, 1), ))
def touch(self, event, pos): if not self.absolute: pos = rotate(pos) if event == io.TOUCH_MOVE and self.start_pos is not None: pdx = pos[0] - self.start_pos[0] pdy = pos[1] - self.start_pos[1] pdxa = abs(pdx) pdya = abs(pdy) if pdxa > pdya and self.directions & SWIPE_HORIZONTAL: # Horizontal direction if (pdx > 0 and self.directions & SWIPE_RIGHT) or ( pdx < 0 and self.directions & SWIPE_LEFT): ui.display.backlight( ui.lerpi( self.light_origin, self.light_target, pdxa / _SWIPE_DISTANCE if pdxa < _SWIPE_DISTANCE else 1)) elif pdxa < pdya and self.directions & SWIPE_VERTICAL: # Vertical direction if (pdy > 0 and self.directions & SWIPE_DOWN) or ( pdy < 0 and self.directions & SWIPE_UP): ui.display.backlight( ui.lerpi( self.light_origin, self.light_target, pdya / _SWIPE_DISTANCE if pdya < _SWIPE_DISTANCE else 1)) elif event == io.TOUCH_START and contains(self.area, pos): self.start_pos = pos self.light_origin = ui.BACKLIGHT_NORMAL elif event == io.TOUCH_END and self.start_pos is not None: pdx = pos[0] - self.start_pos[0] pdy = pos[1] - self.start_pos[1] pdxa = abs(pdx) pdya = abs(pdy) if pdxa > pdya and self.directions & SWIPE_HORIZONTAL: # Horizontal direction ratio = pdxa / _SWIPE_DISTANCE if pdxa < _SWIPE_DISTANCE else 1 if ratio * 100 >= self.treshold: if pdx > 0 and self.directions & SWIPE_RIGHT: return SWIPE_RIGHT elif pdx < 0 and self.directions & SWIPE_LEFT: return SWIPE_LEFT elif pdxa < pdya and self.directions & SWIPE_VERTICAL: # Vertical direction ratio = pdya / _SWIPE_DISTANCE if pdya < _SWIPE_DISTANCE else 1 if ratio * 100 >= self.treshold: if pdy > 0 and self.directions & SWIPE_DOWN: return SWIPE_DOWN elif pdy < 0 and self.directions & SWIPE_UP: return SWIPE_UP # No swipe, reset the state self.start_pos = None ui.display.backlight(self.light_origin)