diff --git a/Cargo.lock b/Cargo.lock index 00da30cf1..1a2aad855 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5252,6 +5252,13 @@ version = "1.0.23" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "29666d0abbfad1e3dc4dcf6144730dd3a3ab225bbbdac83319345b1b44ccfc1b" +[[package]] +name = "zoom" +version = "1.0.0" +dependencies = [ + "horizon-mod-sdk", +] + [[package]] name = "zopfli" version = "0.8.4" diff --git a/Cargo.toml b/Cargo.toml index cc88441a4..259734b98 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [workspace] exclude = ["launcher", "crates/horizon-telemetry-server", "crates/horizon-mod-registry", "third_party/wgpu-hal"] -members = ["crates/horizon-core", "crates/horizon-dualsense", "crates/horizon-threads", "crates/horizon-world", "crates/horizon-client", "crates/horizon-server-files", "crates/horizon-server", "crates/horizon-bots", "crates/horizon-assets", "crates/horizon-protocol", "crates/horizon-theme", "crates/horizon-prepare", "crates/horizon-telemetry", "crates/horizon-bench", "crates/horizon-friends", "crates/horizon-relay", "crates/horizon-mod-sdk", "crates/horizon-mods", "crates/horizon-mods/tests/guest", "crates/horizon-mods/tests/ui_guest", "crates/cargo-horizon-mod", "crates/horizon-port-pack", "crates/horizon-shaders", "crates/horizon-shader-api", "crates/horizon-release-sign", "crates/horizon-mod-sdk/examples/coords-hud", "crates/horizon-mod-sdk/examples/predator-missile", "mods/official/fps-overlay", "mods/official/keystrokes", "xtask"] +members = ["crates/horizon-core", "crates/horizon-dualsense", "crates/horizon-threads", "crates/horizon-world", "crates/horizon-client", "crates/horizon-server-files", "crates/horizon-server", "crates/horizon-bots", "crates/horizon-assets", "crates/horizon-protocol", "crates/horizon-theme", "crates/horizon-prepare", "crates/horizon-telemetry", "crates/horizon-bench", "crates/horizon-friends", "crates/horizon-relay", "crates/horizon-mod-sdk", "crates/horizon-mods", "crates/horizon-mods/tests/guest", "crates/horizon-mods/tests/ui_guest", "crates/cargo-horizon-mod", "crates/horizon-port-pack", "crates/horizon-shaders", "crates/horizon-shader-api", "crates/horizon-release-sign", "crates/horizon-mod-sdk/examples/coords-hud", "crates/horizon-mod-sdk/examples/predator-missile", "mods/official/fps-overlay", "mods/official/keystrokes", "mods/official/zoom", "xtask"] resolver = "2" [workspace.package] diff --git a/docs/mods.md b/docs/mods.md index 2895f897e..9fad50f57 100644 --- a/docs/mods.md +++ b/docs/mods.md @@ -15,7 +15,7 @@ folder, loads no runtime and plays exactly like vanilla 26.3. | The contract (WIT) | `crates/horizon-mod-sdk/wit/horizon.wit` | 0.1: log, game-read, chat, hud, storage, command, ui; tick, frame, chat and command hooks | | SDK for mod authors | `crates/horizon-mod-sdk` | 0.1 | | Example mods | `crates/horizon-mod-sdk/examples/coords-hud`, `examples/predator-missile` | coordinates, facing and time HUD, `/where`, `/coords`; `/missile`, a guided missile from vanilla commands | -| Horizon's official mods | `mods/official/` | the extras moving to the registry: FPS Counter (`fps-overlay`), Keystrokes | +| Horizon's official mods | `mods/official/` | the extras moving to the registry: FPS Counter (`fps-overlay`), Keystrokes, Zoom | | Runtime | `crates/horizon-mods` | wasmtime 48 LTS, component model | | Authoring tool | `crates/cargo-horizon-mod` | `cargo horizon-mod new [--template code\|content] / build / run / check`, and the registry's CI (`registry build / review / list`) | | In the game | `crates/horizon-client/src/game/mods.rs`, Options > Mods | client mods | @@ -428,6 +428,7 @@ so the same rustc and `Cargo.lock` give the same file. |---|---|---|---| | FPS Counter | `fps_overlay` | Extras > FPS counter | `corner`: `top_left` (default), `top_right`, `bottom_left`, `bottom_right`; `lows`: `false` (default), `true` | | Keystrokes | `keystrokes` | Extras > Keystrokes | `corner`: as above, default `top_left`; `cps`: `true` (default), `false` | +| Zoom | `zoom` | Extras > Zoom | `level`: `2`, `3`, `4` (default), `6`, `8`, `10`; `smooth`: `true` (default), `false`; `sensitivity` (slower turning): `true` (default), `false`. The key is the game's: `key.zoom.zoom` in `horizon-mods.txt`, default `key.keyboard.z` | ## Versions and compatibility diff --git a/mods/official/zoom/Cargo.toml b/mods/official/zoom/Cargo.toml new file mode 100644 index 000000000..2225b00e8 --- /dev/null +++ b/mods/official/zoom/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "zoom" +version = "1.0.0" +edition.workspace = true +license.workspace = true +publish = false +description = "Horizon's official zoom: hold a key to zoom, the wheel changes it while held." + +[lib] +crate-type = ["cdylib"] + +[dependencies] +horizon-mod-sdk = { path = "../../../crates/horizon-mod-sdk" } + +[lints] +workspace = true diff --git a/mods/official/zoom/horizon-mod.toml b/mods/official/zoom/horizon-mod.toml new file mode 100644 index 000000000..bd699d0b5 --- /dev/null +++ b/mods/official/zoom/horizon-mod.toml @@ -0,0 +1,9 @@ +id = "zoom" +name = "Zoom" +version = "1.0.0" +description = "Hold a key to zoom in; the mouse wheel changes the zoom while you hold it. Horizon's own, formerly an extra." +authors = ["Horizon"] +api = "0.1" +side = "client" +capabilities = ["render", "input", "ui", "storage"] +experimental = ["render", "input", "ui"] diff --git a/mods/official/zoom/src/lib.rs b/mods/official/zoom/src/lib.rs new file mode 100644 index 000000000..072a40eb0 --- /dev/null +++ b/mods/official/zoom/src/lib.rs @@ -0,0 +1,141 @@ +//! Horizon's official zoom, formerly the built-in extra: hold the key (Z +//! by default) to zoom in, and turn the mouse wheel while holding it to zoom +//! further in or out. Its settings page has the extra's four options: the +//! game lists the key there first and keeps the player's choice +//! (`horizon-mods.txt`); the rest is kept in the mod's storage (`level`, +//! `smooth`, `sensitivity`), +//! where the game writes a player's old extra settings when it installs the +//! mod for them. +use horizon_mod_sdk::{ + Mod, Registration, + input::{self, Event}, + render::{self, Zoom as ZoomView}, + storage, + ui::{self, ButtonWidget, Widget}, +}; + +/// The key's name for `register-key`. +const KEY: &str = "zoom"; +/// The zooms the starting zoom cycles through, and the wheel's limits. +const LEVELS: [u8; 6] = [2, 3, 4, 6, 8, 10]; +const MIN: f32 = 1.25; +const MAX: f32 = 10.0; +/// Each wheel step multiplies the zoom by this. +const STEP: f32 = 1.25; +/// Settings page buttons, under the key's row. +const LEVEL_BUTTON: u32 = 2; +const SMOOTH_BUTTON: u32 = 3; +const SENSITIVITY_BUTTON: u32 = 4; + +struct Zoom { + /// The zoom the key starts at, one of `LEVELS`. + level: u8, + /// Ease in and out like the spyglass. + smooth: bool, + /// Turn slower the more you zoom in. + sensitivity: bool, + /// The zoom while the key is held; None when it is up. + held: Option, +} + +impl Zoom { + /// Tells the game the zoom in effect. + fn show(&self) { + let factor = self.held.unwrap_or(1.0); + render::set_zoom(ZoomView { + factor, + smooth: self.smooth, + turn: if self.sensitivity { 1.0 / factor } else { 1.0 }, + }); + } + fn page(&self) { + let on = |b: bool| if b { "ON" } else { "OFF" }; + ui::set_settings(&[ + Widget::Button(ButtonWidget { + id: LEVEL_BUTTON, + label: format!("Starting zoom: {}x", self.level), + }), + Widget::Button(ButtonWidget { + id: SMOOTH_BUTTON, + label: format!("Smooth zoom: {}", on(self.smooth)), + }), + Widget::Button(ButtonWidget { + id: SENSITIVITY_BUTTON, + label: format!("Slower turning: {}", on(self.sensitivity)), + }), + ]); + } + fn save(&self) { + let flag = |b: bool| if b { &b"true"[..] } else { &b"false"[..] }; + let _ = storage::set("level", self.level.to_string().as_bytes()); + let _ = storage::set("smooth", flag(self.smooth)); + let _ = storage::set("sensitivity", flag(self.sensitivity)); + } +} + +fn stored(key: &str) -> Option { + storage::get(key).and_then(|v| String::from_utf8(v).ok()) +} + +impl Mod for Zoom { + fn init() -> Result<(Self, Registration), String> { + input::register_key(KEY, "Zoom key", "key.keyboard.z")?; + // The extra's defaults: 4x, smooth, slower turning. + let level = stored("level") + .and_then(|v| v.trim().parse::().ok()) + .map_or(4, |v| v.clamp(2, MAX as u8)); + let flag = |key: &str| stored(key).is_none_or(|v| v.trim() != "false"); + let zoom = Zoom { + level, + smooth: flag("smooth"), + sensitivity: flag("sensitivity"), + held: None, + }; + zoom.page(); + Ok((zoom, Registration::new().on_frame())) + } + fn frame(&mut self, _seconds: f32) { + let mut changed = false; + for event in input::poll() { + match event { + Event::KeyDown(k) if k == KEY => { + self.held = Some(f32::from(self.level).clamp(MIN, MAX)); + // While held, the wheel zooms instead of moving the hotbar. + input::claim_wheel(true); + } + Event::KeyUp(k) if k == KEY => { + self.held = None; + input::claim_wheel(false); + } + Event::Wheel(steps) => { + let Some(z) = &mut self.held else { continue }; + *z = (*z * STEP.powf(steps)).clamp(MIN, MAX); + } + _ => continue, + } + changed = true; + } + if changed { + self.show(); + } + } + fn ui_select(&mut self, id: u32, _row: u32) { + match id { + LEVEL_BUTTON => { + // As the extra cycled: an unknown level counts as the first. + let i = LEVELS.iter().position(|&l| l == self.level).unwrap_or(0); + self.level = LEVELS[(i + 1) % LEVELS.len()]; + } + SMOOTH_BUTTON => self.smooth = !self.smooth, + SENSITIVITY_BUTTON => self.sensitivity = !self.sensitivity, + _ => return, + } + self.save(); + self.page(); + } + fn ui_settings_opened(&mut self) { + self.page(); + } +} + +horizon_mod_sdk::export_mod!(Zoom);