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
use crate::transform::UiTransform;
use amethyst_core::{
ecs::{
prelude::{
Component, Entities, Entity, Join, Read, ReadExpect, ReadStorage, System, Write,
},
storage::NullStorage,
},
math::Vector2,
shrev::EventChannel,
};
use amethyst_input::{BindingTypes, InputHandler};
use amethyst_window::ScreenDimensions;
use serde::{Deserialize, Serialize};
use std::marker::PhantomData;
use winit::MouseButton;
pub trait TargetedEvent {
fn get_target(&self) -> Entity;
}
#[derive(Debug, Clone, PartialEq)]
pub enum UiEventType {
Click,
ClickStart,
ClickStop,
HoverStart,
HoverStop,
Dragging {
element_offset: Vector2<f32>,
},
Dropped {
dropped_on: Entity,
},
ValueChange,
ValueCommit,
Focus,
Blur,
}
#[derive(Debug, Clone)]
pub struct UiEvent {
pub event_type: UiEventType,
pub target: Entity,
}
impl UiEvent {
pub fn new(event_type: UiEventType, target: Entity) -> Self {
UiEvent { event_type, target }
}
}
impl TargetedEvent for UiEvent {
fn get_target(&self) -> Entity {
self.target
}
}
#[derive(Default, Debug, Serialize, Deserialize, Clone)]
pub struct Interactable;
impl Component for Interactable {
type Storage = NullStorage<Interactable>;
}
#[derive(Default, Debug)]
pub struct UiMouseSystem<T: BindingTypes> {
was_down: bool,
click_started_on: Option<Entity>,
last_target: Option<Entity>,
_marker: PhantomData<T>,
}
impl<T: BindingTypes> UiMouseSystem<T> {
pub fn new() -> Self {
UiMouseSystem {
was_down: false,
click_started_on: None,
last_target: None,
_marker: PhantomData,
}
}
}
impl<'a, T: BindingTypes> System<'a> for UiMouseSystem<T> {
type SystemData = (
Entities<'a>,
ReadStorage<'a, UiTransform>,
ReadStorage<'a, Interactable>,
Read<'a, InputHandler<T>>,
ReadExpect<'a, ScreenDimensions>,
Write<'a, EventChannel<UiEvent>>,
);
fn run(
&mut self,
(entities, transform, react, input, screen_dimensions, mut events): Self::SystemData,
) {
let down = input.mouse_button_is_down(MouseButton::Left);
let click_started = down && !self.was_down;
let click_stopped = !down && self.was_down;
if let Some((pos_x, pos_y)) = input.mouse_position() {
let x = pos_x as f32;
let y = screen_dimensions.height() - pos_y as f32;
let target = targeted((x, y), (&*entities, &transform, react.maybe()).join());
if target != self.last_target {
if let Some(last_target) = self.last_target {
events.single_write(UiEvent::new(UiEventType::HoverStop, last_target));
}
if let Some(target) = target {
events.single_write(UiEvent::new(UiEventType::HoverStart, target));
}
}
if let Some(e) = target {
if click_started {
events.single_write(UiEvent::new(UiEventType::ClickStart, e));
self.click_started_on = Some(e);
} else if click_stopped {
if let Some(e2) = self.click_started_on {
if e2 == e {
events.single_write(UiEvent::new(UiEventType::Click, e2));
}
}
}
}
self.last_target = target;
}
if click_stopped {
if let Some(e) = self.click_started_on {
events.single_write(UiEvent::new(UiEventType::ClickStop, e));
self.click_started_on = None;
}
}
self.was_down = down;
}
}
pub fn targeted<'a, I>(pos: (f32, f32), transforms: I) -> Option<Entity>
where
I: Iterator<Item = (Entity, &'a UiTransform, Option<&'a Interactable>)> + 'a,
{
transforms
.filter(|(_e, t, _m)| t.opaque && t.position_inside(pos.0, pos.1))
.max_by(|(_e1, t1, _m1), (_e2, t2, _m2)| {
t1.global_z
.partial_cmp(&t2.global_z)
.expect("Unexpected NaN")
})
.and_then(|(e, _, m)| m.map(|_m| e))
}