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
use std::marker::PhantomData;
use amethyst_core::specs::prelude::{Component, DenseVecStorage, Entities, Entity, FlaggedStorage,
Join, ReadStorage};
use super::{Anchor, ScaleMode, Stretch};
#[derive(SystemData)]
pub struct UiFinder<'a> {
entities: Entities<'a>,
storage: ReadStorage<'a, UiTransform>,
}
impl<'a> UiFinder<'a> {
pub fn find(&self, id: &str) -> Option<Entity> {
(&*self.entities, &self.storage)
.join()
.find(|(_, transform)| transform.id == id)
.map(|(entity, _)| entity)
}
}
#[derive(Clone, Debug)]
pub struct UiTransform {
pub id: String,
pub anchor: Anchor,
pub stretch: Stretch,
pub local_x: f32,
pub local_y: f32,
pub local_z: f32,
pub width: f32,
pub height: f32,
pub tab_order: i32,
pub(crate) pixel_x: f32,
pub(crate) pixel_y: f32,
pub(crate) global_z: f32,
pub(crate) pixel_width: f32,
pub(crate) pixel_height: f32,
pub scale_mode: ScaleMode,
pub opaque: bool,
pd: PhantomData<u8>,
}
impl UiTransform {
pub fn new(
id: String,
anchor: Anchor,
x: f32,
y: f32,
z: f32,
width: f32,
height: f32,
tab_order: i32,
) -> UiTransform {
UiTransform {
id,
anchor,
stretch: Stretch::NoStretch,
local_x: x,
local_y: y,
local_z: z,
width,
height,
tab_order,
pixel_x: x,
pixel_y: y,
global_z: z,
pixel_width: width,
pixel_height: height,
scale_mode: ScaleMode::Pixel,
opaque: true,
pd: PhantomData,
}
}
pub fn position_inside_local(&self, x: f32, y: f32) -> bool {
x > self.local_x - self.width / 2.0 && y > self.local_y - self.height / 2.0
&& x < self.local_x + self.width / 2.0 && y < self.local_y + self.height / 2.0
}
pub fn position_inside(&self, x: f32, y: f32) -> bool {
x > self.pixel_x - self.pixel_width / 2.0 && y > self.pixel_y - self.pixel_height / 2.0
&& x < self.pixel_x + self.pixel_width / 2.0
&& y < self.pixel_y + self.pixel_height / 2.0
}
pub fn as_percent(mut self) -> Self {
self.scale_mode = ScaleMode::Percent;
self
}
pub fn as_transparent(mut self) -> Self {
self.opaque = false;
self
}
pub fn with_stretch(mut self, stretch: Stretch) -> Self {
self.stretch = stretch;
self
}
pub fn pixel_x(&self) -> f32 {
self.pixel_x
}
pub fn pixel_y(&self) -> f32 {
self.pixel_y
}
pub fn global_z(&self) -> f32 {
self.global_z
}
}
impl Component for UiTransform {
type Storage = FlaggedStorage<Self, DenseVecStorage<Self>>;
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn inside_local() {
let tr = UiTransform::new("".to_string(), Anchor::TopLeft, 0.0, 0.0, 0.0, 1.0, 1.0, 0);
let pos = (-0.49, 0.20);
assert!(tr.position_inside_local(pos.0, pos.1));
let pos = (-1.49, 1.20);
assert!(!tr.position_inside_local(pos.0, pos.1));
}
#[test]
fn inside_global() {
let tr = UiTransform::new("".to_string(), Anchor::TopLeft, 0.0, 0.0, 0.0, 1.0, 1.0, 0);
let pos = (-0.49, 0.20);
assert!(tr.position_inside(pos.0, pos.1));
let pos = (-1.49, 1.20);
assert!(!tr.position_inside(pos.0, pos.1));
}
}