-
Notifications
You must be signed in to change notification settings - Fork 4
Generic canvas events (WIP) #61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
gselzer
wants to merge
8
commits into
pyapp-kit:main
Choose a base branch
from
gselzer:canvas-events
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3f692a7
First cut: Canvas events
gselzer 8c21b09
canvas_pos -> pos
gselzer e74145c
Fix Jupyter/Wx tests
gselzer 2f1e4fc
Merge remote-tracking branch 'upstream/main' into canvas-events
gselzer cb1d879
Fix examples
gselzer c72de14
Jupyter and wx key events
gselzer 798bbf5
Fix wx tests
gselzer 4730ef9
Merge remote-tracking branch 'upstream/main' into canvas-events
gselzer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| """Demonstrates keyboard-driven pan and zoom on top of the PanZoom controller. | ||
|
|
||
| Arrow keys pan the view; + and - zoom in and out. Mouse drag and scroll | ||
| continue to work as normal via the PanZoom controller. | ||
| """ | ||
|
|
||
| import numpy as np | ||
| from app_model.types import KeyBinding, KeyCode | ||
|
|
||
| import scenex as snx | ||
| from scenex.app.events import Event, KeyPressEvent | ||
|
|
||
| # Build a recognisable test image: a grid of bright dots on a dark background. | ||
| rng = np.random.default_rng(0) | ||
| data = np.zeros((512, 512), dtype=np.uint8) | ||
| coords = rng.integers(8, 504, size=(200, 2)) | ||
| for y, x in coords: | ||
| data[y - 3 : y + 3, x - 3 : x + 3] = 255 | ||
|
|
||
| view = snx.View( | ||
| scene=snx.Scene(children=[snx.Image(data=data)]), | ||
| camera=snx.Camera(controller=snx.PanZoom(), interactive=True), | ||
| ) | ||
| canvas = snx.Canvas(views=[view]) | ||
|
|
||
| _PAN_STEP = 20.0 # world units per arrow-key press | ||
| _ZOOM_STEP = 1.25 # multiplicative factor per +/- press | ||
|
|
||
| LEFT = KeyBinding.validate(KeyCode.LeftArrow) | ||
| RIGHT = KeyBinding.validate(KeyCode.RightArrow) | ||
| UP = KeyBinding.validate(KeyCode.UpArrow) | ||
| DOWN = KeyBinding.validate(KeyCode.DownArrow) | ||
| ZOOM_IN = KeyBinding.validate(KeyCode.NumpadAdd) # + key | ||
| ZOOM_OUT = KeyBinding.validate(KeyCode.NumpadSubtract) # - key | ||
|
|
||
|
|
||
| def _key_filter(event: Event) -> bool: | ||
| """Pan with arrow keys; zoom with + / -.""" | ||
| if not isinstance(event, KeyPressEvent): | ||
| return False | ||
|
|
||
| key = event.key # KeyCode (or KeyCombo for modified keys) | ||
| print(f"key_down: {key}") | ||
| cam = view.camera | ||
|
|
||
| if key == UP: | ||
| cam.transform = cam.transform.translated((0, -_PAN_STEP)) | ||
| elif key == DOWN: | ||
| cam.transform = cam.transform.translated((0, _PAN_STEP)) | ||
| elif key == LEFT: | ||
| cam.transform = cam.transform.translated((_PAN_STEP, 0)) | ||
| elif key == RIGHT: | ||
| cam.transform = cam.transform.translated((-_PAN_STEP, 0)) | ||
| elif key == ZOOM_IN: | ||
| s = _ZOOM_STEP | ||
| cam.projection = cam.projection.scaled((s, s, 1.0)) | ||
| elif key == ZOOM_OUT: | ||
| s = 1.0 / _ZOOM_STEP | ||
| cam.projection = cam.projection.scaled((s, s, 1.0)) | ||
|
|
||
| return True | ||
|
|
||
|
|
||
| canvas.set_event_filter(_key_filter) | ||
|
|
||
| snx.show(canvas) | ||
| snx.run() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm really left wanting for something better than this. We need a way to go from "here's an event" to "here are all of the nodes that pertain to this event"
Will keep thinking on it. Suggestions appreciated too by any passers-by!