From aaffd4bc45845ccf21a9f8b247244d11012a3d29 Mon Sep 17 00:00:00 2001
From: iggy <iggy@muc.ccc.de>
Date: Sun, 16 Jul 2023 22:07:31 +0200
Subject: [PATCH] py: st4m: inertia scroll wip

---
 python_payload/st4m/ui/elements/menus.py |  2 +-
 python_payload/st4m/ui/interactions.py   | 87 ++++++++++++++++++------
 2 files changed, 69 insertions(+), 20 deletions(-)

diff --git a/python_payload/st4m/ui/elements/menus.py b/python_payload/st4m/ui/elements/menus.py
index 4868f18ad5..f3d8b7f93f 100644
--- a/python_payload/st4m/ui/elements/menus.py
+++ b/python_payload/st4m/ui/elements/menus.py
@@ -124,7 +124,7 @@ class FlowerMenu(MenuController):
             item.rotation_time = 10000
         current = self._scroll_controller.current_position()
         current_int = round(current) % len(self._items)
-        print("current", current, current_int)
+        # print("current", current, current_int)
         self.ui.items_ring[current_int].highlighted = True
         self.ui.items_ring[current_int].rotation_time = 3000
         self.ui.angle_offset = math.pi - (tau * current / len(self.ui.items_ring))
diff --git a/python_payload/st4m/ui/interactions.py b/python_payload/st4m/ui/interactions.py
index 8676636c3b..e610609171 100644
--- a/python_payload/st4m/ui/interactions.py
+++ b/python_payload/st4m/ui/interactions.py
@@ -131,7 +131,7 @@ class ScrollController(st4m.Responder):
                 diff = d
         # diff =
         # print(diff)
-        max_velocity = 500
+        max_velocity = 0.2
         velocity = self._velocity
 
         if abs(velocity) < 1:
@@ -189,16 +189,29 @@ class GestureScrollController(ScrollController):
 
         dphi = self._petal._input._dphi
         phase = self._petal._input.phase()
-        self._speedbuffer.append(self._velocity)
-        if len(self._speedbuffer) > 10:
+
+        if self._velocity == 30:
+            print("here")
+            # raise ValueError
+            self._speedbuffer = [1]
+            self._current_position += 0.3
+        elif self._velocity == -30:
+            self._speedbuffer = [-1]
+            self._current_position -= 0.3
+        else:
+            self._speedbuffer.append(self._velocity)
+        # print(self._speedbuffer)
+        while len(self._speedbuffer) > 5:
             self._speedbuffer.pop(0)
         speed = sum(self._speedbuffer) / len(self._speedbuffer)
+        speed = min(speed, 0.008)
+        speed = max(speed, -0.008)
 
         if phase == self._petal._input.ENDED:
-            self._speedbuffer = [0 if abs(speed) < 0.001 else speed]
+            self._speedbuffer = [0 if abs(speed) < 0.005 else speed]
         elif phase == self._petal._input.UP:
-            # pass
-            self._speedbuffer.append(speed * 0.85)
+            pass
+            # self._speedbuffer.append(speed * 0.85)
         elif phase == self._petal._input.BEGIN:
             self._ignore = 5
             self._speedbuffer = [0.0]
@@ -212,7 +225,7 @@ class GestureScrollController(ScrollController):
             #    self._target_position += 1
             self._speedbuffer.append(impulse)
 
-        if abs(speed) < 0.000001:
+        if abs(speed) < 0.0001:
             speed = 0
 
         self._velocity = speed
@@ -221,23 +234,59 @@ class GestureScrollController(ScrollController):
             self._current_position + self._velocity * delta_ms
         ) % self._nitems
 
+        if phase != self._petal._input.UP:
+            return
+
         microstep = round(self._current_position) - self._current_position
-        print("micro:", microstep)
-        print("v", self._velocity)
+        # print("micro:", microstep)
+        # print("v", self._velocity)
+        # print("pos", self._current_position)
+
+        if (
+            abs(microstep) < 0.1
+            and abs(self._velocity) < 0.001
+            # and abs(self._velocity)
+        ):
+            self._velocity = 0
+            # self._speedbuffer = []
+            self._speedbuffer.append(0)
+            self._current_position = round(self._current_position)
+            # print("LOCK")
+            return
+        # if abs(self._velocity) <= 0.0001:
+        #    self._velocity = 0
+
+        if abs(self._velocity) > 0.001:
+            # self._velocity *= 0.7
+            self._speedbuffer.append(-self._velocity)
+            # print("BREAKING")
+            return
 
         if self._velocity >= 0 and microstep > 0:
-            self._velocity -= microstep / 100
-            print("1")
-        elif self._velocity <= 0 and microstep > 0:
-            self._velocity += microstep / 100
-            print("2")
-        elif self._velocity >= 0 and microstep < 0:
-            self._velocity -= microstep / 50
-            print("3")
+            # self._velocity += microstep / 5
+            # self._speedbuffer.append(microstep / 10)
+            self._speedbuffer.append(max(self._velocity, 0.01) * microstep * 10)
+            # print("1")
+        elif self._velocity < 0 and microstep > 0:
+            # self._velocity += microstep / 100
+            # self._speedbuffer.append(microstep / 5)
+            self._speedbuffer.append(-self._velocity)
+            # print("2")
+        elif self._velocity > 0 and microstep < 0:
+            # self._speedbuffer.append(microstep / 5)
+            self._speedbuffer.append(-self._velocity * 0.5)
+            # self._velocity = max(self._velocity + microstep, 0)
+            # if self._velocity == 0:
+            #    self._speedbuffer = []
+            # print("3")
+
         elif self._velocity <= 0 and microstep < 0:
-            self._velocity += microstep / 50
-            print("4")
+            # self._velocity += microstep / 20
+            # self._speedbuffer.append(microstep / 10)
+            self._speedbuffer.append(min(self._velocity, -0.01) * abs(microstep) * 10)
+            # print("4")
 
+        # self._speedbuffer.append(self._velocity)
         # if self._velocity > 0:
         #    self._velocity -= microstep / 100
         # else:
-- 
GitLab