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
extern crate sdl2;
use sdl2::pixels::Color;
use sdl2::rect::Point;
use sdl2::render::Canvas;
use sdl2::video::Window;
use super::Orientation;
use drawable::{DrawSettings, Drawable, KnownSize, Position, State};
#[allow(missing_docs)]
pub enum ElementPositioning {
TopLeftCornered,
Centered,
}
#[allow(missing_docs)]
pub trait Stackable: Drawable + KnownSize {
fn as_drawable(&self) -> &dyn Drawable;
fn as_drawable_mut(&mut self) -> &mut dyn Drawable;
}
impl<T: Drawable + KnownSize> Stackable for T {
fn as_drawable(&self) -> &dyn Drawable {
self
}
fn as_drawable_mut(&mut self) -> &mut dyn Drawable {
self
}
}
pub struct Stack {
pub margin: u32,
pub orientation: Orientation,
pub positioning: ElementPositioning,
pub update_seq: bool,
pub content: Vec<Box<dyn Stackable>>,
}
impl Stack {
pub fn new(
margin: u32,
orientation: Orientation,
positioning: ElementPositioning,
update_seq: bool,
content: Vec<Box<dyn Stackable>>,
) -> Stack {
Stack {
margin,
orientation,
positioning,
update_seq,
content,
}
}
}
impl<'a> Drawable for Stack {
fn content(&self) -> Vec<&dyn Drawable> {
self.content.iter().map(|x| x.as_drawable()).collect()
}
fn content_mut(&mut self) -> Vec<&mut dyn Drawable> {
self.content
.iter_mut()
.map(|x| x.as_drawable_mut())
.collect()
}
fn draw(&self, canvas: &mut Canvas<Window>, pos: &Position, settings: DrawSettings) {
let rect = pos.into_rect_with_size(self.width() as u32, self.height() as u32);
let corner = rect.top_left();
if settings.notes_view {
canvas.set_draw_color(Color::RGB(0, 255, 0));
canvas.draw_rect(rect).expect("Can't draw");
}
let (width, height) = (self.width(), self.height());
match self.orientation {
Orientation::Vertical => {
let mut y = corner.y;
for obj in &self.content {
let corner = match self.positioning {
ElementPositioning::TopLeftCornered => Point::new(corner.x, y),
ElementPositioning::Centered => {
let px = corner.x + width as i32 / 2 - obj.width() as i32 / 2;
Point::new(px, y)
}
};
let pos = Position::TopLeftCorner(corner);
if settings.notes_view {
canvas.set_draw_color(Color::RGB(255, 0, 0));
canvas
.draw_rect(
pos.into_rect_with_size(obj.width() as u32, obj.height() as u32),
).expect("Can't draw");
}
obj.draw(canvas, &pos, settings);
y += obj.height() as i32 + self.margin as i32;
}
}
Orientation::Horizontal => {
let mut x = corner.x;
for obj in &self.content {
let corner = match self.positioning {
ElementPositioning::TopLeftCornered => Point::new(x, corner.y),
ElementPositioning::Centered => {
let py = corner.y + height as i32 / 2 - obj.height() as i32 / 2;
Point::new(x, py)
}
};
let pos = Position::TopLeftCorner(corner);
if settings.notes_view {
canvas.set_draw_color(Color::RGB(255, 0, 0));
canvas
.draw_rect(
pos.into_rect_with_size(obj.width() as u32, obj.height() as u32),
).expect("Can't draw");
}
obj.draw(canvas, &pos, settings);
x += obj.width() as i32 + self.margin as i32;
}
}
}
}
fn step(&mut self) {
let mut any_stepped = false;
for item in &mut self.content {
if item.state() == State::Working {
item.step();
any_stepped = true;
if self.update_seq {
return;
}
}
}
if !any_stepped {
for item in &mut self.content {
if item.state() == State::Final {
item.step();
}
}
}
}
fn state(&self) -> State {
self.content
.iter()
.map(|x| x.state())
.min()
.unwrap_or(State::Hidden)
}
}
impl KnownSize for Stack {
fn width(&self) -> usize {
match self.orientation {
Orientation::Horizontal => {
let content_size = self.content.iter().map(|x| x.width()).sum::<usize>();
let margins = self.margin as usize * (self.content.len() - 1);
content_size + margins
}
Orientation::Vertical => self.content.iter().map(|x| x.width()).max().unwrap_or(0),
}
}
fn height(&self) -> usize {
match self.orientation {
Orientation::Vertical => {
let content_size = self.content.iter().map(|x| x.height()).sum::<usize>();
let margins = self.margin as usize * (self.content.len() - 1);
content_size + margins
}
Orientation::Horizontal => self.content.iter().map(|x| x.height()).max().unwrap_or(0),
}
}
}