Touch Gestures as Controls.¶
A map you pinch, twist and pan with two fingers, or drive with a mouse and keys. Every gesture is a control, bound in the node’s input_context exactly as a key is, and read with Input.value or Input.just_pressed.
Tags: input touch gestures mouse mobile
Touch.PINCHandTouch.ROTATEare axes holding the frame’s motion: the fractional change of scale and the turn in radians. Both rest at zero.Mouse.WHEEL * 0.1is a scaled binding: it puts a wheel notch into the pinch’s unit, so onezoomaction serves both.Touch.PANis a stick, the frame’s two-finger translation, read as aVec2from onepanaction.Touch.TWO_FINGER_TAPis a button, bound besideKey.Rto reset the view.Input.touch_secondary = Touch.LONG_PRESSmakes a held finger the touch screen’s right click, sodrop, bound toPointer.SECONDARY, drops a pin under a right click and under a long press alike.
A gesture is recognised only while something names it, so a scene that binds none pays nothing for recognition.
The map is a child Node2D whose position, rotation and scale the
gestures drive, so it draws in its own space and to_local turns the pointer
into the map point a pin belongs at. A left mouse drag pans too, through a
PointerEvent handler, so a mouse reaches everything a hand does.
Controls: pinch or the mouse wheel zooms; twisting two fingers, or Q and E, turns; a two-finger drag or a left drag pans; a long press or a right click drops a pin; a two-finger tap or R resets the view.
Source¶
1# /// simvx
2# tags = ["input", "touch", "gestures", "mouse", "mobile"]
3# web = { width = 960, height = 600, responsive = true }
4# ///
5"""Touch Gestures as Controls.
6
7A map you pinch, twist and pan with two fingers, or drive with a mouse and keys.
8Every gesture is a control, bound in the node's ``input_context`` exactly as a
9key is, and read with ``Input.value`` or ``Input.just_pressed``.
10
11- ``Touch.PINCH`` and ``Touch.ROTATE`` are axes holding the frame's motion: the
12 fractional change of scale and the turn in radians. Both rest at zero.
13- ``Mouse.WHEEL * 0.1`` is a scaled binding: it puts a wheel notch into the
14 pinch's unit, so one ``zoom`` action serves both.
15- ``Touch.PAN`` is a stick, the frame's two-finger translation, read as a
16 ``Vec2`` from one ``pan`` action.
17- ``Touch.TWO_FINGER_TAP`` is a button, bound beside ``Key.R`` to reset the view.
18- ``Input.touch_secondary = Touch.LONG_PRESS`` makes a held finger the touch
19 screen's right click, so ``drop``, bound to ``Pointer.SECONDARY``, drops a pin
20 under a right click and under a long press alike.
21
22A gesture is recognised only while something names it, so a scene that binds
23none pays nothing for recognition.
24
25The map is a child ``Node2D`` whose ``position``, ``rotation`` and ``scale`` the
26gestures drive, so it draws in its own space and ``to_local`` turns the pointer
27into the map point a pin belongs at. A left mouse drag pans too, through a
28``PointerEvent`` handler, so a mouse reaches everything a hand does.
29
30Controls: pinch or the mouse wheel zooms; twisting two fingers, or Q and E,
31turns; a two-finger drag or a left drag pans; a long press or a right click drops
32a pin; a two-finger tap or R resets the view.
33"""
34
35from simvx.core import (
36 Anchor,
37 Axis1D,
38 Input,
39 Key,
40 Label,
41 Mouse,
42 Node2D,
43 Pointer,
44 PointerEvent,
45 PointerPhase,
46 PointerType,
47 Touch,
48 Vec2,
49 on_input,
50)
51from simvx.graphics import App
52
53TILE = 80.0
54TILES = 12
55
56
57class MapView(Node2D):
58 """A tiled grid and its pins, drawn in the map's own space."""
59
60 def __init__(self, **kwargs):
61 super().__init__(**kwargs)
62 self.pins: list[Vec2] = []
63
64 def drop_pin(self, point: Vec2):
65 self.pins.append(point)
66 self.queue_redraw()
67
68 def on_draw(self, renderer):
69 half = TILES * TILE / 2
70 for i in range(TILES + 1):
71 t = -half + i * TILE
72 renderer.line((t, -half), (t, half), stroke=(0.3, 0.45, 0.6, 1.0))
73 renderer.line((-half, t), (half, t), stroke=(0.3, 0.45, 0.6, 1.0))
74 for pin in self.pins:
75 renderer.circle(pin, 10.0, fill=(1.0, 0.45, 0.3, 1.0))
76
77
78class GestureMap(Node2D):
79 """Reads the gesture actions and moves the map with them."""
80
81 input_context = {
82 "zoom": [Touch.PINCH, Mouse.WHEEL * 0.1],
83 "twist": [Touch.ROTATE],
84 "turn": [Axis1D(low=Key.Q, high=Key.E)],
85 "pan": [Touch.PAN],
86 "drop": [Pointer.SECONDARY],
87 "reset": [Touch.TWO_FINGER_TAP, Key.R],
88 }
89
90 def on_ready(self):
91 Input.touch_secondary = Touch.LONG_PRESS
92 self.offset = Vec2(0.0, 0.0)
93 self.map = self.add_child(MapView())
94 self.readout = self.add_child(Label(anchor=Anchor.TOP_LEFT, inset=10))
95
96 def on_update(self, dt):
97 zoom = self.map.scale.x * (1.0 + Input.value("zoom"))
98 self.map.scale = (min(max(zoom, 0.25), 4.0),) * 2
99 self.map.rotation += Input.value("twist") + Input.value("turn") * 2.0 * dt
100 self.offset += Input.value("pan")
101 if Input.just_pressed("reset"):
102 self.map.scale, self.map.rotation, self.offset = (1.0, 1.0), 0.0, Vec2(0.0, 0.0)
103 self.map.position = self.viewport_size / 2 + self.offset
104 if Input.just_pressed("drop"):
105 self.map.drop_pin(self.map.to_local(Input.mouse_position))
106 self.readout.text = (
107 f"zoom {self.map.scale.x:.2f} turn {self.map.rotation_degrees:.0f} deg pins {len(self.map.pins)}"
108 )
109
110 @on_input(PointerEvent)
111 def _mouse_drag(self, event):
112 if event.pointer_type is PointerType.MOUSE and event.phase is PointerPhase.MOVE and event.contact:
113 self.offset += event.delta
114
115
116if __name__ == "__main__":
117 App(title="Touch Gestures", width=960, height=600, bg_colour=(0.08, 0.1, 0.14, 1.0)).run(GestureMap())