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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
use super::UiTransform;
use amethyst_core::specs::prelude::{BitSet, Component, Entities, FlaggedStorage, InsertedFlag,
Join, ModifiedFlag, ReadExpect, ReadStorage, ReaderId,
Resources, System, VecStorage, WriteStorage};
use amethyst_core::{HierarchyEvent, Parent, ParentHierarchy};
use amethyst_renderer::ScreenDimensions;
#[derive(Debug, Clone)]
pub enum ScaleMode {
Pixel,
Percent,
}
#[derive(Debug, Clone)]
pub enum Anchor {
TopLeft,
TopMiddle,
TopRight,
MiddleLeft,
Middle,
MiddleRight,
BottomLeft,
BottomMiddle,
BottomRight,
}
#[derive(Debug, Clone)]
pub enum Stretch {
X,
Y,
XY,
}
#[derive(Debug, Clone)]
pub struct Anchored {
anchor: Anchor,
offset: Option<(f32, f32)>,
}
impl Anchored {
pub fn new(anchor: Anchor) -> Self {
Anchored {
anchor,
offset: None,
}
}
pub fn norm_offset(&self) -> (f32, f32) {
match self.anchor {
Anchor::TopLeft => (-0.5, -0.5),
Anchor::TopMiddle => (0.0, -0.5),
Anchor::TopRight => (0.5, -0.5),
Anchor::MiddleLeft => (-0.5, 0.0),
Anchor::Middle => (0.0, 0.0),
Anchor::MiddleRight => (0.5, 0.0),
Anchor::BottomLeft => (-0.5, 0.5),
Anchor::BottomMiddle => (0.0, 0.5),
Anchor::BottomRight => (0.5, 0.5),
}
}
}
impl Component for Anchored {
type Storage = VecStorage<Self>;
}
#[derive(Debug, Clone)]
pub struct Stretched {
stretch: Stretch,
margin: (f32, f32),
}
impl Stretched {
pub fn new(stretch: Stretch, margin_x: f32, margin_y: f32) -> Self {
Stretched {
stretch,
margin: (margin_x, margin_y),
}
}
}
impl Component for Stretched {
type Storage = FlaggedStorage<Self, VecStorage<Self>>;
}
pub struct UiLayoutSystem {
screen_size: (f32, f32),
}
impl UiLayoutSystem {
pub fn new() -> Self {
UiLayoutSystem {
screen_size: (0.0, 0.0),
}
}
}
impl<'a> System<'a> for UiLayoutSystem {
type SystemData = (
Entities<'a>,
WriteStorage<'a, UiTransform>,
WriteStorage<'a, Anchored>,
ReadStorage<'a, Parent>,
ReadExpect<'a, ScreenDimensions>,
);
fn run(&mut self, (entities, mut transform, mut anchor, parent, screen_dim): Self::SystemData) {
let cur_size = (screen_dim.width(), screen_dim.height());
let offset_override = self.screen_size != cur_size;
self.screen_size = cur_size;
for (entity, mut tr, mut anchor) in (&*entities, &mut transform, &mut anchor).join() {
if anchor.offset.is_none() || (offset_override && anchor.offset.is_some()) {
if offset_override && anchor.offset.is_some() {
tr.local_x = anchor.offset.unwrap().0;
tr.local_y = anchor.offset.unwrap().1;
}
anchor.offset = Some((tr.local_x, tr.local_y));
let norm_offset = anchor.norm_offset();
let user_offset = match tr.scale_mode {
ScaleMode::Pixel => anchor.offset.unwrap(),
ScaleMode::Percent => anchor.offset.unwrap(),
};
let middle = (screen_dim.width() / 2.0, screen_dim.height() / 2.0);
let new_pos_x = middle.0 + norm_offset.0 * screen_dim.width() + user_offset.0;
let new_pos_y = middle.1 + norm_offset.1 * screen_dim.height() + user_offset.1;
tr.local_x = new_pos_x;
tr.local_y = new_pos_y;
if !parent.contains(entity) {
tr.global_x = tr.local_x;
tr.global_y = tr.local_y;
tr.global_z = tr.local_z;
}
}
}
}
}
#[derive(Default)]
pub struct UiParentSystem {
local_modified: BitSet,
inserted_local_id: Option<ReaderId<InsertedFlag>>,
modified_local_id: Option<ReaderId<ModifiedFlag>>,
inserted_stretch_id: Option<ReaderId<InsertedFlag>>,
modified_stretch_id: Option<ReaderId<ModifiedFlag>>,
parent_events_id: Option<ReaderId<HierarchyEvent>>,
}
impl<'a> System<'a> for UiParentSystem {
type SystemData = (
Entities<'a>,
WriteStorage<'a, UiTransform>,
ReadStorage<'a, Parent>,
ReadStorage<'a, Anchored>,
ReadStorage<'a, Stretched>,
ReadExpect<'a, ScreenDimensions>,
ReadExpect<'a, ParentHierarchy>,
);
fn run(&mut self, data: Self::SystemData) {
let (entities, mut locals, parents, anchors, stretches, screen_dim, hierarchy) = data;
#[cfg(feature = "profiler")]
profile_scope!("ui_parent_system");
self.local_modified.clear();
locals.populate_inserted(
&mut self.inserted_local_id.as_mut().unwrap(),
&mut self.local_modified,
);
locals.populate_modified(
&mut self.modified_local_id.as_mut().unwrap(),
&mut self.local_modified,
);
stretches.populate_inserted(
&mut self.inserted_stretch_id.as_mut().unwrap(),
&mut self.local_modified,
);
stretches.populate_modified(
&mut self.modified_stretch_id.as_mut().unwrap(),
&mut self.local_modified,
);
for event in hierarchy
.changed()
.read(&mut self.parent_events_id.as_mut().unwrap())
{
if let HierarchyEvent::Modified(entity) = *event {
self.local_modified.add(entity.id());
}
}
for entity in hierarchy.all() {
let self_dirty = self.local_modified.contains(entity.id());
let mut combined_transform: Option<(f32, f32, f32)> = None;
let mut new_size: Option<(f32, f32)> = None;
match (parents.get(*entity), locals.get(*entity)) {
(Some(parent), Some(local)) => {
let parent_dirty = self.local_modified.contains(parent.entity.id());
if parent_dirty || self_dirty {
if let Some(parent_global) = locals.get(parent.entity) {
combined_transform = Some(match anchors.get(*entity) {
Some(anchor) => {
let norm = anchor.norm_offset();
(
parent_global.global_x + parent_global.width * norm.0
+ anchor.offset.unwrap().0,
parent_global.global_y + parent_global.height * norm.1
+ anchor.offset.unwrap().1,
parent_global.global_z + local.local_z,
)
}
None => (
parent_global.global_x + local.local_x,
parent_global.global_y + local.local_y,
parent_global.global_z + local.local_z,
),
});
if let Some(st) = stretches.get(*entity) {
new_size = Some(match st.stretch {
Stretch::X => {
(parent_global.width - st.margin.0 * 2.0, local.height)
}
Stretch::Y => {
(local.width, parent_global.height - st.margin.1 * 2.0)
}
Stretch::XY => (
parent_global.width - st.margin.0 * 2.0,
parent_global.height - st.margin.1 * 2.0,
),
});
}
}
}
}
_ => (),
}
if let Some(c) = combined_transform {
if let Some(local) = locals.get_mut(*entity) {
local.global_x = c.0;
local.global_y = c.1;
local.global_z = c.2;
}
}
if let Some(s) = new_size {
if let Some(local) = locals.get_mut(*entity) {
local.width = s.0;
local.height = s.1;
}
}
}
for (entity, mut local, stretch) in (&*entities, &mut locals, &stretches).join() {
if !parents.contains(entity) {
let new_size = match stretch.stretch {
Stretch::X => (screen_dim.width() - stretch.margin.0 * 2.0, local.height),
Stretch::Y => (local.width, screen_dim.height() - stretch.margin.1 * 2.0),
Stretch::XY => (
screen_dim.width() - stretch.margin.0 * 2.0,
screen_dim.height() - stretch.margin.1 * 2.0,
),
};
local.width = new_size.0;
local.height = new_size.1;
}
}
locals.populate_inserted(
&mut self.inserted_local_id.as_mut().unwrap(),
&mut self.local_modified,
);
locals.populate_modified(
&mut self.modified_local_id.as_mut().unwrap(),
&mut self.local_modified,
);
}
fn setup(&mut self, res: &mut Resources) {
use amethyst_core::specs::prelude::SystemData;
Self::SystemData::setup(res);
self.parent_events_id = Some(res.fetch_mut::<ParentHierarchy>().track());
let mut locals = WriteStorage::<UiTransform>::fetch(res);
let mut stretches = WriteStorage::<Stretched>::fetch(res);
self.inserted_local_id = Some(locals.track_inserted());
self.modified_local_id = Some(locals.track_modified());
self.inserted_stretch_id = Some(stretches.track_inserted());
self.modified_stretch_id = Some(stretches.track_modified());
}
}