Skip to content

Commit 78dd3c4

Browse files
committed
Fix open/save dialogs and texture loading
1 parent 1ad0a72 commit 78dd3c4

2 files changed

Lines changed: 104 additions & 55 deletions

File tree

crates/spaceman-dmm/src/dmi.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ impl IconCache {
8585
}
8686

8787
impl TextureCache {
88-
pub fn retrieve(&mut self, device: &Device, icons: &IconCache, id: usize) -> &Texture<'_> {
88+
pub fn retrieve(&mut self, device: &Device, icons: &IconCache, id: usize) -> &Texture<'static> {
8989
if id >= self.textures.len() {
9090
self.textures.resize_with(id + 1, Default::default);
9191
}
@@ -186,7 +186,7 @@ pub fn load_texture(device: &Device, bitmap: &lodepng::Bitmap<RGBA>) -> Texture<
186186
let mut mem = transfer_buffer.map::<u8>(device, true);
187187
let mut dest = mem.mem_mut();
188188
for pixel in &bitmap.buffer {
189-
dest.write_all(&[pixel.a, pixel.b, pixel.g, pixel.r])
189+
dest.write_all(&[pixel.r, pixel.g, pixel.b, pixel.a])
190190
.unwrap();
191191
}
192192
mem.unmap();

crates/spaceman-dmm/src/main.rs

Lines changed: 102 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use dmm_tools::dmm::Map;
1818
use dreammaker::objtree::{ObjectTree, TypeRef};
1919
use edit_prefab::EditPrefab;
2020
use imgui::*;
21-
use sdl3::dialog::DialogFileFilter;
21+
use sdl3::dialog::{DialogError, DialogFileFilter};
2222
use sdl3::gpu::{ColorTargetInfo, CommandBuffer, Device};
2323
use sdl3::keyboard::Scancode;
2424
use sdl3::pixels::Color;
@@ -81,6 +81,20 @@ pub struct EditorScene {
8181
stacked_inverted: bool,
8282

8383
mouse_drag_pos: Option<(i32, i32)>,
84+
85+
command_tx: mpsc::Sender<Command>,
86+
command_rx: mpsc::Receiver<Command>,
87+
}
88+
89+
enum Command {
90+
DialogError(DialogError),
91+
OpenEnvironment(PathBuf),
92+
OpenMap(Vec<PathBuf>),
93+
SaveMap {
94+
uid: usize,
95+
path: PathBuf,
96+
copy: bool,
97+
},
8498
}
8599

86100
pub struct Environment {
@@ -140,6 +154,7 @@ struct EditInstance {
140154

141155
impl EditorScene {
142156
fn new(device: &Device, logical_size: (u32, u32)) -> Self {
157+
let (command_tx, command_rx) = mpsc::channel();
143158
let mut ed = EditorScene {
144159
device: device.clone(),
145160
logical_size,
@@ -176,6 +191,9 @@ impl EditorScene {
176191
stacked_rendering: false,
177192
stacked_inverted: false,
178193
mouse_drag_pos: None,
194+
195+
command_tx,
196+
command_rx,
179197
};
180198
ed.finish_init();
181199
ed
@@ -264,6 +282,36 @@ impl EditorScene {
264282
}
265283

266284
fn run(&mut self) {
285+
while let Ok(command) = self.command_rx.try_recv() {
286+
match command {
287+
Command::DialogError(e) => self.handle_dialog_error(e),
288+
Command::OpenEnvironment(path) => self.load_environment(path),
289+
Command::OpenMap(paths) => {
290+
for path in paths {
291+
self.load_map(path);
292+
}
293+
},
294+
Command::SaveMap { uid, path, copy } => {
295+
if let Some(map) = self.maps.iter_mut().find(|m| m.uid == uid) {
296+
if let Some(hist) = map.state.hist() {
297+
if let Err(e) = hist.current().save(map.state.base_dmm()).to_file(&path)
298+
{
299+
self.errors.push(format!(
300+
"Error writing {}:\n{}",
301+
path.display(),
302+
e
303+
));
304+
}
305+
if !copy {
306+
map.path = Some(path);
307+
hist.mark_clean();
308+
}
309+
}
310+
}
311+
},
312+
}
313+
}
314+
267315
if let Some(loading) = self.loading_env.take() {
268316
match loading.rx.try_recv() {
269317
Ok(Ok(env)) => self.finish_loading_env(env),
@@ -1306,19 +1354,26 @@ impl EditorScene {
13061354
}
13071355
}
13081356

1357+
fn handle_dialog_error(&mut self, e: DialogError) {
1358+
match e {
1359+
DialogError::Canceled => {},
1360+
e => self.errors.push(e.to_string()),
1361+
}
1362+
}
1363+
13091364
fn open_environment(&mut self) {
1365+
let tx = self.command_tx.clone();
13101366
if let Err(e) = sdl3::dialog::show_open_file_dialog(
13111367
FILTERS_DME,
13121368
None::<&Path>,
13131369
false,
13141370
None,
1315-
Box::new(|result, _filter| {
1316-
// TODO
1317-
eprintln!("{:?}", result);
1318-
//self.load_environment(fname.into());
1371+
Box::new(move |result, _filter| match result {
1372+
Ok(mut fname) => tx.send(Command::OpenEnvironment(fname.remove(0))).unwrap(),
1373+
Err(e) => tx.send(Command::DialogError(e)).unwrap(),
13191374
}),
13201375
) {
1321-
self.errors.push(e.to_string());
1376+
self.handle_dialog_error(e);
13221377
}
13231378
}
13241379

@@ -1379,30 +1434,19 @@ impl EditorScene {
13791434
}
13801435

13811436
fn open_map(&mut self) {
1437+
let tx = self.command_tx.clone();
13821438
if let Err(e) = sdl3::dialog::show_open_file_dialog(
13831439
FILTERS_DMM,
13841440
None::<&Path>,
13851441
true,
13861442
None,
1387-
Box::new(|_paths, _| {
1388-
// TODO
1443+
Box::new(move |result, _| match result {
1444+
Ok(paths) => tx.send(Command::OpenMap(paths)).unwrap(),
1445+
Err(e) => tx.send(Command::DialogError(e)).unwrap(),
13891446
}),
13901447
) {
1391-
self.errors.push(e.to_string());
1448+
self.handle_dialog_error(e);
13921449
}
1393-
/*
1394-
match nfd::open_file_multiple_dialog(Some("dmm"), None) {
1395-
Ok(nfd::Response::Okay(fname)) => {
1396-
self.load_map(fname.into());
1397-
},
1398-
Ok(nfd::Response::OkayMultiple(fnames)) => {
1399-
for each in fnames {
1400-
self.load_map(each.into());
1401-
}
1402-
},
1403-
_ => {},
1404-
}
1405-
*/
14061450
}
14071451

14081452
fn load_map(&mut self, path: PathBuf) {
@@ -1447,17 +1491,24 @@ impl EditorScene {
14471491
if let Some(map) = self.maps.get_mut(self.map_current) {
14481492
if let Some(hist) = map.state.hist() {
14491493
if map.path.is_none() {
1494+
let uid = map.uid;
1495+
let tx = self.command_tx.clone();
14501496
if let Err(e) = sdl3::dialog::show_save_file_dialog(
14511497
FILTERS_DMM,
14521498
None::<&Path>,
14531499
None,
1454-
Box::new(|path, _| {
1455-
// TODO
1456-
eprintln!("save_map {:?}", path);
1457-
// map.path = Some(PathBuf::from(fname));
1500+
Box::new(move |result, _| match result {
1501+
Ok(mut paths) => tx
1502+
.send(Command::SaveMap {
1503+
uid,
1504+
path: paths.remove(0),
1505+
copy: false,
1506+
})
1507+
.unwrap(),
1508+
Err(e) => tx.send(Command::DialogError(e)).unwrap(),
14581509
}),
14591510
) {
1460-
self.errors.push(e.to_string());
1511+
self.handle_dialog_error(e);
14611512
}
14621513
return;
14631514
}
@@ -1471,30 +1522,29 @@ impl EditorScene {
14711522
}
14721523
}
14731524

1474-
fn save_map_as(&mut self, _copy: bool) {
1525+
fn save_map_as(&mut self, copy: bool) {
14751526
if let Some(map) = self.maps.get_mut(self.map_current) {
14761527
if let Some(_hist) = map.state.hist() {
1528+
let uid = map.uid;
1529+
let tx = self.command_tx.clone();
14771530
if let Err(e) = sdl3::dialog::show_save_file_dialog(
14781531
FILTERS_DMM,
14791532
None::<&Path>,
14801533
None,
1481-
Box::new(|path, _| {
1482-
// TODO
1483-
eprintln!("save_map_as {:?}", path);
1484-
/*
1485-
let path = PathBuf::from(fname);
1486-
if let Err(e) = hist.current().save(map.state.base_dmm()).to_file(&path) {
1487-
self.errors
1488-
.push(format!("Error writing {}:\n{}", path.display(), e));
1489-
}
1490-
if !copy {
1491-
map.path = Some(path);
1492-
hist.mark_clean();
1493-
}
1494-
*/
1495-
}),
1534+
Box::new(
1535+
move |result: Result<Vec<PathBuf>, DialogError>, _| match result {
1536+
Ok(mut paths) => tx
1537+
.send(Command::SaveMap {
1538+
uid,
1539+
path: paths.remove(0),
1540+
copy,
1541+
})
1542+
.unwrap(),
1543+
Err(e) => tx.send(Command::DialogError(e)).unwrap(),
1544+
},
1545+
),
14961546
) {
1497-
self.errors.push(e.to_string());
1547+
self.handle_dialog_error(e);
14981548
}
14991549
}
15001550
}
@@ -1545,11 +1595,11 @@ impl EditorScene {
15451595

15461596
const FILTERS_DME: &[DialogFileFilter] = &[DialogFileFilter {
15471597
name: "DreamMaker Environment",
1548-
pattern: "*.dme",
1598+
pattern: "dme",
15491599
}];
15501600
const FILTERS_DMM: &[DialogFileFilter] = &[DialogFileFilter {
15511601
name: "DreamMaker Map",
1552-
pattern: "*.dmm",
1602+
pattern: "dmm",
15531603
}];
15541604

15551605
// ---------------------------------------------------------------------------
@@ -1666,17 +1716,17 @@ fn prepare_tool_icon(
16661716
tint,
16671717
dir,
16681718
} => {
1669-
/*if let Some(env) = environment {
1719+
if let Some(env) = environment {
16701720
if let Some(id) = env.icons.get_index(icon.as_ref()) {
16711721
let icon = env.icons.get_icon(id);
16721722
if let Some([u1, v1, u2, v2]) = icon.uv_of(&icon_state, dir) {
1673-
let tex = map_renderer
1723+
let texture = map_renderer
16741724
.icon_textures
1675-
.retrieve(&mut map_renderer.factory, &env.icons, id)
1725+
.retrieve(&map_renderer.device, &env.icons, id)
16761726
.clone();
1677-
let samp = map_renderer.sampler.clone();
16781727
ToolIcon::Loaded {
1679-
tex: renderer.textures().insert((tex, samp)),
1728+
texture,
1729+
sampler: map_renderer.sampler.clone(),
16801730
uv0: [u1, v1],
16811731
uv1: [u2, v2],
16821732
tint: Some(tint),
@@ -1687,8 +1737,7 @@ fn prepare_tool_icon(
16871737
} else {
16881738
ToolIcon::None
16891739
}
1690-
} else*/
1691-
{
1740+
} else {
16921741
ToolIcon::Dmi {
16931742
icon,
16941743
icon_state,

0 commit comments

Comments
 (0)