Add open mod folder buttons
Expose the Horizon mods directory from both the Classic and Horizon mods screens, creating it if needed before opening it. Closes #780
This commit is contained in:
@@ -225,6 +225,11 @@ impl Game<'_> {
|
||||
let _ = std::fs::create_dir_all(&dir);
|
||||
super::packs::open_folder(&dir);
|
||||
}
|
||||
Event::OpenModFolder => {
|
||||
let dir = Self::options_path().with_file_name(horizon_client::mod_list::DIR);
|
||||
let _ = std::fs::create_dir_all(&dir);
|
||||
super::packs::open_folder(&dir);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
|
||||
@@ -36,6 +36,8 @@ pub enum HRow {
|
||||
Extra(extras::Item),
|
||||
/// Installed mod `i` (`settings.mods.installed`) and its switch.
|
||||
Mod(usize),
|
||||
/// Opens the directory where Horizon mods are installed.
|
||||
OpenModFolder,
|
||||
}
|
||||
/// A sidebar section: name, description, rows.
|
||||
pub struct Section {
|
||||
@@ -192,12 +194,15 @@ pub const SECTIONS: &[Section] = &[
|
||||
name: "Mods",
|
||||
blurb: "Mods made for Horizon, from the horizon-mods folder. Each runs in a sandbox and stays off until you turn it on; check what it may do first.",
|
||||
rows: |s| {
|
||||
let mut rows = vec![HRow::OpenModFolder];
|
||||
if s.mods.installed.is_empty() {
|
||||
return vec![HRow::Header(
|
||||
rows.push(HRow::Header(
|
||||
"No mods: put .hmod files in the horizon-mods folder",
|
||||
)];
|
||||
));
|
||||
} else {
|
||||
rows.extend((0..s.mods.installed.len()).map(HRow::Mod));
|
||||
}
|
||||
(0..s.mods.installed.len()).map(HRow::Mod).collect()
|
||||
rows
|
||||
},
|
||||
},
|
||||
];
|
||||
@@ -450,6 +455,18 @@ impl OptionsUi {
|
||||
ui.described_row(x, y, &installed.title, "", 0.0, cost, text, cw - 60.0);
|
||||
ui.switch(ROW + i as u64, control_x, y + ROW_H / 2.0, s.mods.is_on(m));
|
||||
}
|
||||
HRow::OpenModFolder => {
|
||||
ui.row(x, y, "Mod folder", "", 0.0);
|
||||
let label = "Open Folder";
|
||||
let bw = ui.button_width(label, false).max(96.0);
|
||||
ui.button(
|
||||
ROW + i as u64,
|
||||
[control_x - bw, y + 12.0, bw, CONTROL],
|
||||
label,
|
||||
Button::Secondary,
|
||||
true,
|
||||
);
|
||||
}
|
||||
HRow::Extra(item) => {
|
||||
let cy = y + ROW_H / 2.0;
|
||||
let id = ROW + i as u64;
|
||||
@@ -730,6 +747,10 @@ impl OptionsUi {
|
||||
s.mods.toggle(*m);
|
||||
events.push(Event::Changed);
|
||||
}
|
||||
Some(HRow::OpenModFolder) if !right => {
|
||||
events.push(Event::ButtonClick);
|
||||
events.push(Event::OpenModFolder);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
@@ -908,4 +929,53 @@ mod tests {
|
||||
ui.click_horizon(&mut settings, &atlas, x, drag_y, framebuffer, true, false);
|
||||
assert_eq!(ui.hscroll, 0.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mods_section_opens_folder_from_its_button() {
|
||||
let atlas = atlas();
|
||||
let framebuffer = [2560, 1440];
|
||||
let mut options = section_ui("Mods");
|
||||
let mut settings = Settings::default();
|
||||
let mut batch = horizon_batch(framebuffer);
|
||||
let mut ui = Ui::dry(&atlas, &mut batch, (0.0, 0.0));
|
||||
options.horizon_layout(&settings, &mut ui, 1.0);
|
||||
let rect = ui
|
||||
.hits
|
||||
.iter()
|
||||
.find(|hit| hit.id == ROW)
|
||||
.expect("Mods section has an Open Folder button")
|
||||
.rect;
|
||||
let scale = ui.density / ui.batch.scale;
|
||||
let cursor = (
|
||||
(rect[0] + rect[2]) * 0.5 * scale,
|
||||
(rect[1] + rect[3]) * 0.5 * scale,
|
||||
);
|
||||
drop(ui);
|
||||
|
||||
assert_eq!(
|
||||
options.click_horizon(
|
||||
&mut settings,
|
||||
&atlas,
|
||||
cursor.0,
|
||||
cursor.1,
|
||||
framebuffer,
|
||||
false,
|
||||
false
|
||||
),
|
||||
vec![Event::ButtonClick, Event::OpenModFolder]
|
||||
);
|
||||
assert!(
|
||||
options
|
||||
.click_horizon(
|
||||
&mut settings,
|
||||
&atlas,
|
||||
cursor.0,
|
||||
cursor.1,
|
||||
framebuffer,
|
||||
false,
|
||||
true
|
||||
)
|
||||
.is_empty()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -734,6 +734,7 @@ enum Act {
|
||||
/// Back to Horizon's own menus (Horizon only, see `list_rows`).
|
||||
HorizonMenus,
|
||||
ToggleMod(usize),
|
||||
OpenModFolder,
|
||||
}
|
||||
enum WKind {
|
||||
Button {
|
||||
@@ -780,6 +781,7 @@ pub enum Event {
|
||||
/// The pack selection changed (reload resources).
|
||||
Packs(Vec<String>),
|
||||
OpenPackFolder,
|
||||
OpenModFolder,
|
||||
}
|
||||
/// `SliderableValueSet.applyValueImmediately`: these apply 600 ms after
|
||||
/// the last change (`OptionInstanceSliderButton.delayedApplyAt`).
|
||||
@@ -1626,7 +1628,18 @@ impl OptionsUi {
|
||||
}
|
||||
content = y + self.scroll - top + 2.0;
|
||||
out[list_start..].iter_mut().for_each(|w| w.list = true);
|
||||
out.push(done(h - 27.0));
|
||||
if page == Page::Mods {
|
||||
out.push(button(
|
||||
cx - 154.0,
|
||||
h - 27.0,
|
||||
150.0,
|
||||
Text::literal("Open Mod Folder"),
|
||||
Act::OpenModFolder,
|
||||
));
|
||||
out.push(button(cx + 4.0, h - 27.0, 150.0, tr("gui.done"), Act::Done));
|
||||
} else {
|
||||
out.push(done(h - 27.0));
|
||||
}
|
||||
}
|
||||
}
|
||||
// Layout may change the offset (language centering), or the list may
|
||||
@@ -2078,6 +2091,7 @@ impl OptionsUi {
|
||||
s.mods.toggle(i);
|
||||
events.push(Event::Changed);
|
||||
}
|
||||
Act::OpenModFolder => events.push(Event::OpenModFolder),
|
||||
Act::None => {}
|
||||
}
|
||||
}
|
||||
@@ -2450,6 +2464,31 @@ mod tests {
|
||||
assert!(empty.iter().any(|w| {
|
||||
matches!(&w.kind, WKind::Text { text, .. } if text.plain().contains(".hmod"))
|
||||
}));
|
||||
let open_folder = empty
|
||||
.iter()
|
||||
.find(|w| {
|
||||
matches!(
|
||||
&w.kind,
|
||||
WKind::Button {
|
||||
act: Act::OpenModFolder,
|
||||
..
|
||||
}
|
||||
)
|
||||
})
|
||||
.expect("Mods page has an Open Mod Folder button");
|
||||
assert_eq!(
|
||||
ui.click(
|
||||
&mut s,
|
||||
&atlas,
|
||||
open_folder.x + 1.0,
|
||||
open_folder.y + 1.0,
|
||||
size,
|
||||
[480, 240],
|
||||
false,
|
||||
false,
|
||||
),
|
||||
vec![Event::ButtonClick, Event::OpenModFolder]
|
||||
);
|
||||
for (id, dev) in [("normal", false), ("dev", true), ("", false)] {
|
||||
s.mods.installed.push(crate::mod_list::Installed {
|
||||
id: id.into(),
|
||||
|
||||
Reference in New Issue
Block a user