1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
//! # Built-in viewport sync and input handling systems
//!
//! These two systems bridge Bevy's window and input events to the
//! engine's framework-agnostic [`InputEvent`] protocol, so the host
//! application gets working pan / rotate / zoom out of the box.
//!
//! ## Data flow
//!
//! ```text
//! Bevy Window Bevy Input Events Engine
//! ----------- ----------------- ------
//! Window.resolution -----> sync_viewport ----------> Camera.viewport_*
//!
//! CursorMoved (left) --+
//! MouseMotion (right) --+--> handle_default_input --> MapState.handle_input()
//! MouseWheel ---+ |
//! KeyCode::Space ---+ v
//! CameraController
//! ```
//!
//! ## Input mapping
//!
//! | Bevy input | Engine event | Effect |
//! |------------|--------------|--------|
//! | Left-drag (`CursorMoved` delta) | `InputEvent::Pan` | Translate the map |
//! | Right-drag (`MouseMotion`) | `InputEvent::Rotate` | Yaw + pitch the camera |
//! | Mouse wheel (`MouseWheel`) | `InputEvent::Zoom` | Zoom in / out |
//! | Space (just pressed) | direct `camera.mode` toggle | Perspective / orthographic |
//!
//! ## Coordinate and unit conventions
//!
//! Viewport dimensions and pan deltas both use **logical** pixels.
//! This makes the pipeline DPI-independent:
//!
//! - `sync_viewport` reads `Window::resolution.width()` / `.height()`
//! (logical size).
//! - Pan deltas are derived from `CursorMoved.position` differences,
//! which Bevy reports in logical pixels.
//! - `Camera::meters_per_pixel()` divides the visible ground-plane
//! height by the logical viewport height, giving logical meters per
//! logical pixel.
//!
//! Rotation uses `MouseMotion.delta` (raw device motion) because
//! rotation sensitivity should be the same regardless of DPI -- one
//! physical centimetre of mouse travel should produce the same bearing
//! change on any display.
//!
//! ## Sensitivity constants
//!
//! The rotation and zoom sensitivities are tuned for a standard
//! desktop mouse. They are exposed as module-level constants so
//! they can be adjusted centrally if the mapping needs recalibrating.
//!
//! ## Scheduling
//!
//! Both systems run in [`PreUpdate`](bevy::prelude::PreUpdate), before
//! `update_map_state`. This ensures that input from the current frame
//! is applied before the engine ticks and before any `Update`-phase
//! sync system reads derived state.
// ---------------------------------------------------------------------------
use crateMapStateResource;
use MessageReader;
use ;
use TouchInput;
use *;
use ;
// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------
/// Radians per physical pixel of mouse drag for rotation.
///
/// At 0.005 rad/px a full 1280 px sweep rotates the camera ~6.4 rad
/// (roughly one full turn), giving fine-grained control while still
/// allowing fast rotation with a flick.
const ROTATE_SENSITIVITY: f64 = 0.005;
/// Zoom speed factor per scroll-wheel notch.
///
/// A single notch (`wheel.y = 1.0`) produces a 10 % zoom step.
/// The formula is asymmetric by design:
///
/// - Scroll up: `factor = 1 + y * ZOOM_SENSITIVITY` (linear growth)
/// - Scroll down: `factor = 1 / (1 + |y| * ZOOM_SENSITIVITY)` (reciprocal)
///
/// The reciprocal ensures that scrolling up and then down by the same
/// amount returns to *approximately* the same zoom level, while
/// preventing the factor from reaching zero or going negative.
const ZOOM_SENSITIVITY: f64 = 0.1;
/// Lower clamp for the computed zoom factor.
///
/// Prevents extreme zoom-out from a single high-velocity scroll event.
const ZOOM_FACTOR_MIN: f64 = 0.1;
/// Upper clamp for the computed zoom factor.
///
/// Prevents extreme zoom-in from a single high-velocity scroll event.
const ZOOM_FACTOR_MAX: f64 = 10.0;
// ---------------------------------------------------------------------------
// MapInputEnabled -- public gate for host applications
// ---------------------------------------------------------------------------
/// When set to `false`, [`handle_default_input`] is a no-op for that frame.
///
/// Host applications that own their own input routing (e.g. via an egui
/// scene widget) should insert this resource as `MapInputEnabled(false)` at
/// startup. The renderer plugin initialises it to `true` so existing
/// stand-alone users are unaffected.
;
// ---------------------------------------------------------------------------
// PrevCursorPos -- local resource for tracking cursor delta
// ---------------------------------------------------------------------------
/// Local resource that tracks the previous frame's cursor position in
/// logical pixels.
///
/// `CursorMoved` gives absolute positions, so we derive a delta by
/// subtracting the previous position. Stored as an `Option` to handle
/// the first frame (and re-entry after the cursor leaves the window).
pub ;
// ---------------------------------------------------------------------------
// sync_viewport
// ---------------------------------------------------------------------------
/// Sync the engine viewport size to the Bevy primary window each frame.
///
/// Reads the window's **logical** resolution and writes it to
/// [`Camera::viewport_width`](rustial_engine::Camera::viewport_width) /
/// [`Camera::viewport_height`](rustial_engine::Camera::viewport_height).
///
/// Logical pixels are used because:
///
/// - `CursorMoved.position` is in logical pixels.
/// - Bevy's `PerspectiveProjection::update()` internally handles the
/// physical-to-logical mapping for the actual GPU projection.
/// - Keeping viewport and pan deltas in the same unit system makes
/// `meters_per_pixel()` correct regardless of DPI scale factor.
///
/// The write is skipped when the values have not changed, avoiding a
/// needless mutation of the `Res<MapStateResource>` (which would
/// trigger Bevy's change-detection).
pub
// ---------------------------------------------------------------------------
// handle_default_input
// ---------------------------------------------------------------------------
/// Default map input handling: left-drag pan, right-drag rotate, wheel
/// zoom, Space toggles projection mode.
///
/// ## Pan (left-drag)
///
/// Pan deltas are derived from `CursorMoved` position differences
/// (logical pixels), not from `MouseMotion`. `CursorMoved` is
/// reported in logical pixels on every platform, which matches the
/// logical viewport stored by [`sync_viewport`]. This makes
/// `meters_per_pixel * delta` produce the exact screen-space
/// displacement regardless of DPI, keeping the cursor locked to the
/// map point under it during drag.
///
pub