| 1 |
--- |
| 2 |
title: "Flat Pointer Events" |
| 3 |
pull_requests: [25337] |
| 4 |
--- |
| 5 |
|
| 6 |
Pointer events are now "flattened". For example `Pointer<Press>` is now `PointerPress`. These events no longer use (or implement) `Deref` to access the inner `Press` fields. Instead they are stored directly on the `PointerPress` event. `Pointer` is now non-generic, and is a field stored on each pointer event. |
| 7 |
|
| 8 |
```rust |
| 9 |
// Before |
| 10 |
fn on_press(press: On<Pointer<Press>>) { |
| 11 |
info!("pressed {} {:?} {:?}", press.entity, press.pointer_id, press.pointer_location.position); |
| 12 |
} |
| 13 |
|
| 14 |
// After |
| 15 |
fn on_press(press: On<PointerPress>) { |
| 16 |
info!("pressed {} {:?} {:?}", press.entity, press.pointer.id, press.pointer.position); |
| 17 |
} |
| 18 |
``` |
| 19 |
|
| 20 |
`pointer.position` is just a `Vec2`, rather than a `Location`. `Location` isn't used much in practice. Consumers that need a `Location` can now use `pointer.location()`. |
| 21 |
|
| 22 |
Developers that want to write code that is generic on pointer events should now use the new `PointerEvent` trait: |
| 23 |
|
| 24 |
```rust |
| 25 |
// Before |
| 26 |
fn on_pointer_event<E: Debug + Clone + Reflect>(event: On<Pointer<E>>) { |
| 27 |
} |
| 28 |
|
| 29 |
// After |
| 30 |
fn on_pointer_event<E: PointerEvent>(event: On<E>) { |
| 31 |
} |
| 32 |
``` |