Skip to content
Snippets Groups Projects
Commit 48b33096 authored by schneider's avatar schneider
Browse files

ui: add a viewmanager field to ViewWithInputState

parent 5b9744cb
No related branches found
No related tags found
No related merge requests found
......@@ -305,11 +305,9 @@ there is a special view which helps you with that: :py:class:`ViewWithInputState
class SecondScreen(ViewWithInputState):
def __init__(self) -> None:
super().__init__()
self._vm = None
def on_enter(self, vm: Optional[ViewManager]) -> None:
super().on_enter(vm) # Let ViewWithInputState do its thing
self._vm = vm
def draw(self, ctx: Context) -> None:
# Paint the background black
......@@ -321,13 +319,12 @@ there is a special view which helps you with that: :py:class:`ViewWithInputState
super().think(ins, delta_ms) # Let ViewWithInputState do its thing
if self.input.right_shoulder.middle.pressed:
self._vm.pop()
self.vm.pop()
class Example(ViewWithInputState):
def __init__(self) -> None:
super().__init__()
self._vm = None
def draw(self, ctx: Context) -> None:
# Paint the background black
......@@ -337,13 +334,13 @@ there is a special view which helps you with that: :py:class:`ViewWithInputState
def on_enter(self, vm: Optional[ViewManager]) -> None:
self._vm = vm
super().on_enter(vm) # Let ViewWithInputState do its thing
def think(self, ins: InputState, delta_ms: int) -> None:
super().think(ins, delta_ms) # Let ViewWithInputState do its thing
if self.input.right_shoulder.middle.pressed:
self._vm.push(SecondScreen())
self.vm.push(SecondScreen())
st3m.run.run_view(Example())
......@@ -366,11 +363,9 @@ Let's introduce the final class you should actually be using for application dev
class SecondScreen(ViewWithInputState):
def __init__(self) -> None:
super().__init__()
self._vm = None
def on_enter(self, vm: Optional[ViewManager]) -> None:
super().on_enter(vm) # Let ViewWithInputState do its thing
self._vm = vm
def draw(self, ctx: Context) -> None:
# Paint the background black
......@@ -382,7 +377,7 @@ Let's introduce the final class you should actually be using for application dev
super().think(ins, delta_ms) # Let ViewWithInputState do its thing
if self.input.right_shoulder.middle.pressed:
self._vm.pop()
self.vm.pop()
class MyDemo(Application):
......
......@@ -21,23 +21,21 @@ log = Log(__name__)
class Application(ViewWithInputState):
def __init__(self, name: str = __name__) -> None:
self._name = name
self._view_manager: Optional[ViewManager] = None
super().__init__()
def on_exit(self) -> None:
pass
def on_enter(self, vm: Optional[ViewManager]) -> None:
self._view_manager = vm
super().on_enter(vm)
def think(self, ins: InputState, delta_ms: int) -> None:
super().think(ins, delta_ms)
if self.input.left_shoulder.middle.pressed:
if self._view_manager is not None:
if self.vm is not None:
self.on_exit()
self._view_manager.pop(ViewTransitionSwipeRight())
self.vm.pop(ViewTransitionSwipeRight())
class BundleLoadException(BaseException):
......
......@@ -120,19 +120,16 @@ class MenuController(ViewWithInputState):
__slots__ = (
"_items",
"_scroll_controller",
"_view_manager",
)
def __init__(self, items: List[MenuItem]) -> None:
self._items = items
self._scroll_controller = ScrollController()
self._scroll_controller.set_item_count(len(items))
self._view_manager: Optional[ViewManager] = None
super().__init__()
def on_enter(self, vm: Optional["ViewManager"]) -> None:
self._view_manager = vm
super().on_enter(vm)
def _parse_state(self) -> None:
......@@ -174,4 +171,4 @@ class MenuController(ViewWithInputState):
Automatically called on canonical user input.
"""
self._items[self._scroll_controller.target_position()].press(self._view_manager)
self._items[self._scroll_controller.target_position()].press(self.vm)
......@@ -35,9 +35,11 @@ class ViewWithInputState(View):
def __init__(self) -> None:
self.input = InputController()
self.vm: Optional["ViewManager"] = None
def on_enter(self, vm: Optional["ViewManager"]) -> None:
self.input._ignore_pressed()
self.vm = vm
def think(self, ins: InputState, delta_ms: int) -> None:
self.input.think(ins, delta_ms)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment