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
use super::ScaleMode;
use amethyst_core::specs::prelude::{Component, DenseVecStorage, FlaggedStorage};
use std::marker::PhantomData;
#[derive(Clone, Debug)]
pub struct UiTransform {
pub id: String,
pub local_x: f32,
pub local_y: f32,
pub local_z: f32,
pub width: f32,
pub height: f32,
pub tab_order: i32,
pub global_x: f32,
pub global_y: f32,
pub global_z: f32,
pub scale_mode: ScaleMode,
pub opaque: bool,
pd: PhantomData<u8>,
}
impl UiTransform {
pub fn new(
id: String,
x: f32,
y: f32,
z: f32,
width: f32,
height: f32,
tab_order: i32,
) -> UiTransform {
UiTransform {
id,
local_x: x,
local_y: y,
local_z: z,
width,
height,
tab_order,
global_x: x,
global_y: y,
global_z: z,
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.global_x - self.width / 2.0 && y > self.global_y - self.height / 2.0
&& x < self.global_x + self.width / 2.0 && y < self.global_y + self.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
}
}
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(), 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(), 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));
}
}