From 0076f23c462ec4fceceb0ab3508e61fad8285330 Mon Sep 17 00:00:00 2001 From: Serge Bazanski <q3k@q3k.org> Date: Thu, 17 Aug 2023 13:09:37 +0200 Subject: [PATCH] py/gr33nhouse: switch to ScrollController --- python_payload/apps/gr33nhouse/__init__.py | 24 +++++++++++----------- python_payload/apps/gr33nhouse/applist.py | 19 ++++++++--------- 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/python_payload/apps/gr33nhouse/__init__.py b/python_payload/apps/gr33nhouse/__init__.py index d6c5b9781d..dc47d69a3f 100644 --- a/python_payload/apps/gr33nhouse/__init__.py +++ b/python_payload/apps/gr33nhouse/__init__.py @@ -1,6 +1,7 @@ from st3m.goose import Enum from st3m.application import Application, ApplicationContext from st3m.input import InputController, InputState +from st3m.ui.interactions import ScrollController from st3m.ui import colours from st3m.ui.view import ViewManager from ctx import Context @@ -18,7 +19,6 @@ class ViewState(Enum): class Gr33nhouseApp(Application): items = ["Browse apps", "Record flow3r seed", "Enter flow3r seed"] - selection = 0 input: InputController background: Flow3rView @@ -29,6 +29,8 @@ class Gr33nhouseApp(Application): self.input = InputController() self.background = Flow3rView() + self._sc = ScrollController() + self._sc.set_item_count(3) self.state = ViewState.CONTENT @@ -76,7 +78,7 @@ class Gr33nhouseApp(Application): 30.0, ).fill() - ctx.translate(0, -30 * self.selection) + ctx.translate(0, -30 * self._sc.current_position()) offset = 0 @@ -86,7 +88,7 @@ class Gr33nhouseApp(Application): ctx.text_baseline = ctx.MIDDLE for idx, item in enumerate(self.items): - if idx == self.selection: + if idx == self._sc.target_position(): ctx.gray(0.0) else: ctx.gray(1.0) @@ -99,6 +101,7 @@ class Gr33nhouseApp(Application): def think(self, ins: InputState, delta_ms: int) -> None: self.input.think(ins, delta_ms) + self._sc.think(ins, delta_ms) if self.vm is None: raise RuntimeError("vm is None") @@ -112,17 +115,14 @@ class Gr33nhouseApp(Application): self.background.think(ins, delta_ms) if self.input.buttons.app.left.pressed: - if self.selection > 0: - self.selection -= 1 - + self._sc.scroll_left() elif self.input.buttons.app.right.pressed: - if self.selection < len(self.items) - 1: - self.selection += 1 - + self._sc.scroll_right() elif self.input.buttons.app.middle.pressed: - if self.selection == 0: + pos = self._sc.target_position() + if pos == 0: self.vm.push(AppList()) - elif self.selection == 1: + elif pos == 1: self.vm.push(RecordView()) - elif self.selection == 2: + elif pos == 2: self.vm.push(ManualInputView()) diff --git a/python_payload/apps/gr33nhouse/applist.py b/python_payload/apps/gr33nhouse/applist.py index 21137f5ae8..917bc6f1da 100644 --- a/python_payload/apps/gr33nhouse/applist.py +++ b/python_payload/apps/gr33nhouse/applist.py @@ -2,6 +2,7 @@ from st3m.goose import Optional, Enum, Any from st3m.input import InputController, InputState from st3m.ui import colours from st3m.ui.view import BaseView, ViewManager +from st3m.ui.interactions import ScrollController from ctx import Context import urequests import time @@ -21,7 +22,6 @@ class AppList(BaseView): _state: ViewState = ViewState.INITIAL apps: list[Any] = [] - selection: int = 0 input: InputController background: Flow3rView @@ -30,6 +30,7 @@ class AppList(BaseView): self.input = InputController() self.vm = None self.background = Flow3rView() + self._sc = ScrollController() def on_enter(self, vm: Optional[ViewManager]) -> None: self.vm = vm @@ -89,7 +90,7 @@ class AppList(BaseView): 30.0, ).fill() - ctx.translate(0, -30 * self.selection) + ctx.translate(0, -30 * self._sc.current_position()) offset = 0 @@ -100,7 +101,7 @@ class AppList(BaseView): ctx.move_to(0, 0) for idx, app in enumerate(self.apps): - if idx == self.selection: + if idx == self._sc.target_position(): ctx.gray(0.0) else: ctx.gray(1.0) @@ -114,6 +115,7 @@ class AppList(BaseView): raise RuntimeError(f"Invalid view state {self._state}") def think(self, ins: InputState, delta_ms: int) -> None: + self._sc.think(ins, delta_ms) if self.initial_ticks == 0 or time.ticks_ms() < self.initial_ticks + 300: return @@ -132,6 +134,7 @@ class AppList(BaseView): return self._state = ViewState.LOADED + self._sc.set_item_count(len(self.apps)) print("App list loaded") except Exception as e: print(f"Load failed: {e}") @@ -145,18 +148,14 @@ class AppList(BaseView): self.background.think(ins, delta_ms) if self.input.buttons.app.left.pressed: - if self.selection > 0: - self.selection -= 1 - + self._sc.scroll_left() elif self.input.buttons.app.right.pressed: - if self.selection < len(self.apps) - 1: - self.selection += 1 - + self._sc.scroll_right() elif self.input.buttons.app.middle.pressed: if self.vm is None: raise RuntimeError("vm is None") - app = self.apps[self.selection] + app = self.apps[self._sc.target_position()] url = app["tarDownloadUrl"] name = app["name"] author = app["author"] -- GitLab