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
/* Original code Copyright (c) 2024 Shane Celis[1]
Licensed under the MIT License[2] or Apache License v2.0[3]
[1]: https://mastodon.gamedev.place/@shanecelis
[2]: https://opensource.org/licenses/MIT
[3]: https://www.apache.org/licenses/LICENSE-2.0
*/
//! Click event for Bevy
//!
//! [Interaction::Pressed] is not the same as a click. A click is a press and
//! release of a mouse button on the same UI element. A press that leaves a UI
//! element and releases elsewhere is not considered a click.
//!
//! # Setup
//!
//! Add the plugin.
//!
//! ```
//! # use bevy::prelude::*;
//! # use bevy_asky::view::click;
//! let mut app = App::new();
//! app.add_plugins(click::plugin);
//! ```
//!
//! # Usage
//!
//! Add an observer on a button.
//!
//! ```
//! # use bevy::prelude::*;
//! # use bevy_asky::view::click::{self, Click};
//! fn setup(mut commands: Commands) {
//! commands.spawn(Button::default())
//! .observe(|trigger: On<Click>|
//! eprintln!("Clicked on {}", trigger.event_target()));
//! }
//! ```
use EntityHashMap;
use *;
/// Adds a system that triggers a [Click] event when [Button] with an
/// [Interaction] component is pressed and released.
///
/// ```
/// # use bevy::prelude::*;
/// # use bevy_asky::view::click;
/// let mut app = App::new();
/// app.add_plugins(click::plugin);
/// ```
/// Excerpt from this gist[1].
///
/// * * *
///
/// A trigger event that signifies a click on a button.
///
/// ```
/// # use bevy::prelude::*;
/// # use bevy_asky::view::click::{self, Click};
/// fn setup(mut commands: Commands) {
/// commands.spawn(Button::default())
/// .observe(|trigger: On<Click>|
/// eprintln!("Clicked on {}", trigger.event_target()));
/// }
/// ```
/// [1]: https://gist.github.com/shanecelis/06b2d1a598e1e06d0a00671596e9f74f
/// This system looks at [Button] [Interaction] changes. If that state changes
/// from [Interaction::Pressed] to [Interaction::Hovered] then it will trigger a
/// [Click] event targeting the Entity with the [Interaction] component.
///
/// TODO: The `Local<EntityHashMap>` should be reset or drop elements that are stale
/// so it doesn't grow unbounded.