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
use crate::math::{Mat2d, Rect, Vec2};
use core::ecs::{storage::UnprotectedStorage, world::Index, BitSet, DenseVecStorage, Entity, Join};
use std::{borrow::Cow, collections::HashMap};
#[derive(Default)]
pub struct CompositeTransformRes {
mask: BitSet,
inner: DenseVecStorage<Mat2d>,
inner_inverse: DenseVecStorage<Mat2d>,
}
impl CompositeTransformRes {
pub fn new() -> Self {
Self::default()
}
pub fn read(&self) -> CompositeTransformJoinable {
CompositeTransformJoinable {
mask: &self.mask,
storage: &self.inner,
}
}
pub fn write(&mut self) -> CompositeTransformJoinableMut {
CompositeTransformJoinableMut {
mask: &self.mask,
storage: &mut self.inner,
}
}
pub fn read_inverse(&self) -> CompositeTransformJoinable {
CompositeTransformJoinable {
mask: &self.mask,
storage: &self.inner_inverse,
}
}
pub fn write_inverse(&mut self) -> CompositeTransformJoinableMut {
CompositeTransformJoinableMut {
mask: &self.mask,
storage: &mut self.inner_inverse,
}
}
pub(crate) fn add(&mut self, entity: Entity, matrix: Mat2d) {
let id = entity.id();
if self.mask.contains(id) {
unsafe {
*self.inner.get_mut(id) = matrix;
*self.inner_inverse.get_mut(id) = (!matrix).unwrap_or_default();
}
} else {
unsafe {
self.inner.insert(id, matrix);
self.inner_inverse.insert(id, (!matrix).unwrap_or_default());
}
self.mask.add(entity.id());
}
}
pub(crate) fn clear(&mut self) {
for id in &self.mask {
unsafe {
self.inner.remove(id);
self.inner_inverse.remove(id);
}
}
self.mask.clear();
}
}
pub struct CompositeTransformJoinableMut<'a> {
mask: &'a BitSet,
storage: &'a mut DenseVecStorage<Mat2d>,
}
impl<'a> Join for CompositeTransformJoinableMut<'a> {
type Mask = &'a BitSet;
type Type = &'a mut Mat2d;
type Value = &'a mut DenseVecStorage<Mat2d>;
unsafe fn open(self) -> (Self::Mask, Self::Value) {
(self.mask, self.storage)
}
unsafe fn get(value: &mut Self::Value, id: Index) -> Self::Type {
let value: *mut Self::Value = value as *mut Self::Value;
(*value).get_mut(id)
}
}
pub struct CompositeTransformJoinable<'a> {
mask: &'a BitSet,
storage: &'a DenseVecStorage<Mat2d>,
}
impl<'a> Join for CompositeTransformJoinable<'a> {
type Mask = &'a BitSet;
type Type = &'a Mat2d;
type Value = &'a DenseVecStorage<Mat2d>;
unsafe fn open(self) -> (Self::Mask, Self::Value) {
(self.mask, self.storage)
}
unsafe fn get(value: &mut Self::Value, id: Index) -> Self::Type {
value.get(id)
}
}
#[derive(Default)]
pub struct CompositeCameraCache {
pub(crate) last_view_size: Vec2,
pub(crate) world_transforms: HashMap<Entity, Mat2d>,
pub(crate) world_inverse_transforms: HashMap<Entity, Mat2d>,
}
impl CompositeCameraCache {
pub fn last_view_size(&self) -> Vec2 {
self.last_view_size
}
pub fn screen_to_world_space(&self, entity: Entity, point: Vec2) -> Option<Vec2> {
self.world_inverse_transforms
.get(&entity)
.map(|m| *m * point)
}
pub fn world_to_screen_space(&self, entity: Entity, point: Vec2) -> Option<Vec2> {
self.world_transforms.get(&entity).map(|m| *m * point)
}
pub fn world_transform(&self, entity: Entity) -> Option<Mat2d> {
self.world_transforms.get(&entity).cloned()
}
pub fn world_inverse_transform(&self, entity: Entity) -> Option<Mat2d> {
self.world_inverse_transforms.get(&entity).cloned()
}
pub fn world_both_transforms(&self, entity: Entity) -> Option<(Mat2d, Mat2d)> {
if let Some(t) = self.world_transforms.get(&entity) {
if let Some(i) = self.world_inverse_transforms.get(&entity) {
return Some((*t, *i));
}
}
None
}
}
#[derive(Debug, Default)]
pub struct CompositeUiInteractibles {
pub(crate) bounding_boxes: HashMap<Cow<'static, str>, Rect>,
}
impl CompositeUiInteractibles {
pub fn find_rect(&self, name: &str) -> Option<Rect> {
self.bounding_boxes.get(name).copied()
}
pub fn does_rect_contains_point(&self, name: &str, point: Vec2) -> bool {
if let Some(rect) = self.bounding_boxes.get(name) {
rect.contains_point(point)
} else {
false
}
}
}