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
use super::App;
use crate::{
storage::asset::{
project::{GridHighlight, scene_instance_path::selected_scene_instance_index},
scene::grid::GridLocation,
},
ui::{
ContextMenuAction, ContextMenuBuilder,
action::UiAction,
scene_instance::{
dnd::{dnd_drag_source, dnd_drop_zone},
widget::{EmptyGridSpot, SceneInstanceWidget},
},
},
};
use egui::{
Color32, Frame, Id, KeyboardShortcut, Modifiers, Ui, UiBuilder, Vec2,
scroll_area::ScrollBarVisibility::AlwaysVisible,
};
use egui::{DragAndDrop, Label, LayerId, Order, Response, Sense, Widget};
use egui_phosphor_icons::icons;
use emath::{Rect, vec2};
use epaint::{Stroke, StrokeKind};
use std::sync::atomic::{AtomicBool, Ordering::Relaxed};
impl App {
pub fn effects_grid(&mut self, ui: &mut Ui) {
egui::ScrollArea::both()
.id_salt("grid scroll")
.auto_shrink([false, false])
.scroll_bar_visibility(AlwaysVisible)
.show(ui, |ui| {
let Some(project) = self.project.as_mut() else {
return;
};
let grid_width = project.grid_width();
let grid_height = project.grid_height();
let quick_row = project.quick_row_index();
let quick_col = grid_width.saturating_sub(1);
let grid_highlight = project.grid_highlight;
let effects_size = self.persistent_state.effects_size();
ui.set_width(grid_width as f32 * (effects_size + 20.) + 20.);
ui.set_height(grid_height as f32 * (effects_size + 20.) + 20.);
let to_global = ui
.ctx()
.layer_transform_to_global(ui.layer_id())
.unwrap_or_default();
let grid = &mut project.scenes_instances_grid;
let start_pos = ui.cursor().min;
let mut dropped = None;
let mut clicked_selection = None;
// Draw column highlight if configured (before rows)
if grid_highlight == GridHighlight::Column {
let quick_scene_rect = Rect::from_min_size(
start_pos + vec2(20. + quick_col as f32 * (effects_size + 20.), 20.),
vec2(
effects_size,
(effects_size + 20.) * grid_height as f32 - 20.,
),
)
.expand(10.);
ui.painter().rect_filled(
quick_scene_rect,
5.,
Color32::GOLD.blend(Color32::from_black_alpha(200)),
);
}
for row in 0..grid_height {
// Highlight last row if configured
if grid_highlight == GridHighlight::Row && row == quick_row {
let quick_scene_rect = Rect::from_min_size(
start_pos + vec2(20., 20. + row as f32 * (effects_size + 20.)),
vec2((effects_size + 20.) * grid_width as f32 - 20., effects_size),
)
.expand(10.);
ui.painter().rect_filled(
quick_scene_rect,
5.,
Color32::GOLD.blend(Color32::from_black_alpha(200)),
);
}
for col in 0..grid_width {
let location = GridLocation { col, row };
let rect = to_global.mul_rect(Rect::from_min_size(
start_pos
+ vec2(20., 20.)
+ vec2(
col as f32 * (effects_size + 20.),
row as f32 * (effects_size + 20.),
),
vec2(effects_size, effects_size),
));
let (_, dropped_payload) = ui
.scope_builder(UiBuilder::new().max_rect(rect), |ui| {
dnd_drop_zone::<GridLocation, ()>(
ui,
Frame::default().corner_radius(2.),
|ui| {
let scene = grid.get_mut(&location);
match scene {
Some(scene_instance) => {
let item_id = Id::new((
"Draggable Scene Widget",
scene_instance.id,
));
let mut widget_response =
dnd_drag_source(ui, item_id, location, |ui| {
ui.add(SceneInstanceWidget {
scene_instance,
timing: &self.timing,
collections: &self.collections,
effects_size,
sound_data: &self.sound_data,
})
});
// setup context actions
ContextMenuBuilder::default()
.add_action(ContextMenuAction {
description: "Duplicate Scene".to_string(),
keyboard_shortcut: KeyboardShortcut::new(
Modifiers::COMMAND,
egui::Key::D,
),
ctx_action: |location| {
UiAction::CloneSceneInstance(location)
.enqueue();
},
key_action: || {
UiAction::CloneSceneInstancePath(
selected_scene_instance_index(),
)
.enqueue();
},
})
.add_action(ContextMenuAction {
description: "Delete Scene".to_string(),
keyboard_shortcut: KeyboardShortcut::new(
Modifiers::default(),
egui::Key::Delete,
),
ctx_action: |location| {
UiAction::DeleteSceneInstance {
location,
}
.enqueue();
},
key_action: || {
UiAction::DeleteSceneInstancePath(
selected_scene_instance_index(),
)
.enqueue();
},
})
.show(&mut widget_response, location);
// also handle backspace as delete action for MacOS
if !ui.ctx().egui_wants_keyboard_input()
&& ui.ctx().input_mut(|i| {
i.consume_key(
Modifiers::default(),
egui::Key::Backspace,
)
})
{
UiAction::DeleteSceneInstancePath(
selected_scene_instance_index(),
)
.enqueue();
}
if ui.ctx().is_being_dragged(item_id) {
ui.painter().rect_filled(
widget_response.rect,
5.0,
Color32::from_gray(100),
);
} else if self.selected_scene_instance == location {
ui.painter().rect_stroke(
widget_response.rect,
5.0,
Stroke::new(2., Color32::from_gray(200)),
StrokeKind::Outside,
);
} else if widget_response.hovered() {
ui.painter().rect_stroke(
widget_response.rect,
5.0,
Stroke::new(1., Color32::from_gray(160)),
StrokeKind::Outside,
);
}
if widget_response.clicked() {
clicked_selection = Some(location);
}
}
None => {
ui.add(EmptyGridSpot {
location,
collections: &mut self.collections,
effects_size,
});
}
};
},
)
})
.inner; // dnd_drop_zone
if let Some(payload) = dropped_payload {
dropped = Some((payload, row, col));
}
}
}
if let Some((location, row, col)) = dropped {
// The user dropped onto this cell
let from = *location;
let to = GridLocation { row, col };
// Ownership of the scene instances must temporarily be taken to swap
let to_item = grid.remove(&to);
let from_item = { grid.remove(&from) };
if let Some(from_item) = from_item {
grid.insert(to, from_item);
}
// reinsert the to item to the from location
if let Some(to_item) = to_item {
grid.insert(from, to_item);
}
UiAction::SelectSceneByLocation(to).enqueue();
}
if let Some(location) = clicked_selection {
self.set_selected_scene_instance(location);
}
static SHOW_TRASH_ONE_MORE_FRAME: AtomicBool = AtomicBool::new(false);
let is_dragging = DragAndDrop::has_payload_of_type::<GridLocation>(ui.ctx());
if is_dragging || SHOW_TRASH_ONE_MORE_FRAME.load(Relaxed) {
SHOW_TRASH_ONE_MORE_FRAME.store(is_dragging, Relaxed);
let id = Id::new("Trash");
let layer_id = LayerId::new(Order::Foreground, id);
ui.scope_builder(UiBuilder::new().layer_id(layer_id), |ui| {
ui.set_clip_rect(ui.ctx().content_rect());
let rect = Rect::from_min_size(
ui.ctx().content_rect().left_bottom()
+ vec2(10.0, -(effects_size + 10.0)),
Vec2::splat(effects_size),
);
ui.place(rect, TrashWidget { effects_size });
});
}
});
}
}
pub struct TrashWidget {
effects_size: f32,
}
impl Widget for TrashWidget {
fn ui(self, ui: &mut Ui) -> Response {
let (response, dropped) =
dnd_drop_zone::<GridLocation, ()>(ui, Frame::default().corner_radius(2.), |ui| {
let rect = Rect::from_min_size(ui.cursor().min, Vec2::splat(self.effects_size));
ui.painter()
.rect_filled(rect, 5.0, Color32::from_rgb(150, 0, 0));
ui.place(
rect,
Label::new(icons::TRASH.regular().size(40.0).color(Color32::WHITE)),
);
ui.allocate_rect(rect, Sense::hover());
});
if let Some(dropped) = dropped {
UiAction::DeleteSceneInstance { location: *dropped }.enqueue();
}
response.response
}
}