Archive incari-client (client, 26.3, clean)
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
# Incari's Zoom (features/zoom) for the Rust build (ADR 0068, 0069). Hooks by name only
|
||||
# (native-hooks.json), so it builds on any Horizon with these hook points. Its settings are the
|
||||
# client's (rust/mods/incari): the module menu shows them.
|
||||
[mod]
|
||||
id = "incari_zoom"
|
||||
version = "0.1.0"
|
||||
priority = 100
|
||||
|
||||
[[module]]
|
||||
id = "runtime"
|
||||
file = "crates/horizon-client/src/game.rs"
|
||||
name = "incari_zoom"
|
||||
source = "runtime.rs"
|
||||
|
||||
[[injection]]
|
||||
id = "key"
|
||||
hook = "key"
|
||||
hook_version = 1
|
||||
kind = "HEAD"
|
||||
helper = "key.rs"
|
||||
bindings = [
|
||||
{ name = "game", ty = "&mut crate::game::Game<'_>", value = "&mut *self" },
|
||||
{ name = "key", ty = "&str", value = "key" },
|
||||
{ name = "pressed", ty = "bool", value = "pressed" },
|
||||
]
|
||||
|
||||
[[injection]]
|
||||
id = "zoom"
|
||||
hook = "zoom"
|
||||
hook_version = 1
|
||||
kind = "HEAD"
|
||||
helper = "zoom.rs"
|
||||
bindings = [
|
||||
{ name = "game", ty = "&crate::game::Game<'_>", value = "self" },
|
||||
{ name = "zoom", ty = "&mut Option<crate::game::native_hooks::NativeZoom>", value = "&mut *zoom" },
|
||||
]
|
||||
@@ -0,0 +1,10 @@
|
||||
// The Zoom key (the module's `zoom` setting), held or pressed.
|
||||
fn incari_zoom_key(game: &mut crate::game::Game<'_>, key: &str, pressed: bool) {
|
||||
use crate::game::{incari_client as c, incari_zoom as m};
|
||||
let bound = c::settings(m::ID, |s| s.key("zoom")).filter(|k| k.is_bound());
|
||||
if bound != Some(incari_core::input::Key::from_name(key)) {
|
||||
return;
|
||||
}
|
||||
let playing = game.native_playing();
|
||||
m::with(|k| k.key(pressed, playing));
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
// Incari's Zoom for the Rust build (ADR 0068, 0069), as features/zoom does it: hold (or toggle) the
|
||||
// key to zoom in, OptiFine's way. Horizon's zoom hook narrows the world's field of view and leaves
|
||||
// the hand and HUD alone, eases in and out through the field of view as the spyglass does, and
|
||||
// scales mouse turning. It has no cinematic camera for a mod to turn on, so Smooth turning turns
|
||||
// slower, as Slower does. Its settings are the module's in `incari_core::modules` (the Java ids).
|
||||
// No game types: rust/lib.rs tests it.
|
||||
|
||||
/// The module's id, as the Java client's profiles name it.
|
||||
pub const ID: &str = "zoom";
|
||||
|
||||
/// The key's state, as the Java module keeps it.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
pub struct Keys {
|
||||
/// Flipped by each press (the key's `onPress`).
|
||||
pub toggled: bool,
|
||||
/// The key is held (it went down with no screen open and hasn't come up).
|
||||
pub held: bool,
|
||||
}
|
||||
|
||||
impl Keys {
|
||||
/// The module's key went down (`pressed`) or up; `playing` with no screen open.
|
||||
pub fn key(&mut self, pressed: bool, playing: bool) {
|
||||
if !pressed {
|
||||
self.held = false;
|
||||
} else if playing {
|
||||
self.held = true;
|
||||
self.toggled = !self.toggled;
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether the view's zoomed, for the `mode` setting's key (None while the module is off).
|
||||
pub fn active(&mut self, mode: Option<&str>) -> bool {
|
||||
let active = match mode {
|
||||
None => false,
|
||||
Some("hold") => self.held,
|
||||
Some(_) => self.toggled,
|
||||
};
|
||||
if !active {
|
||||
self.toggled = false;
|
||||
}
|
||||
active
|
||||
}
|
||||
}
|
||||
|
||||
/// The zoom asked of Horizon for `amount` (the `amount` setting, times closer), `turning` (its
|
||||
/// key) and `ease`: the factor, whether it eases, and the scale of mouse turning.
|
||||
pub fn zoom(amount: f32, turning: &str, ease: bool) -> (f32, bool, f32) {
|
||||
let factor = amount.clamp(1.0, 10.0);
|
||||
let turn = if turning == "usual" {
|
||||
1.0
|
||||
} else {
|
||||
1.0 / factor
|
||||
};
|
||||
(factor, ease, turn)
|
||||
}
|
||||
|
||||
// The window's thread only: keys and the zoom hook both run there.
|
||||
thread_local! {
|
||||
static KEYS: std::cell::Cell<Keys> = const {
|
||||
std::cell::Cell::new(Keys { toggled: false, held: false })
|
||||
};
|
||||
}
|
||||
|
||||
/// Runs `f` on the key's state.
|
||||
pub fn with<R>(f: impl FnOnce(&mut Keys) -> R) -> R {
|
||||
KEYS.with(|cell| {
|
||||
let mut keys = cell.get();
|
||||
let r = f(&mut keys);
|
||||
cell.set(keys);
|
||||
r
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn holds_or_toggles() {
|
||||
let mut k = Keys::default();
|
||||
k.key(true, false);
|
||||
assert!(!k.active(Some("hold")), "a press on a screen doesn't zoom");
|
||||
k.key(true, true);
|
||||
assert!(k.active(Some("hold")));
|
||||
k.key(false, false);
|
||||
assert!(!k.active(Some("hold")), "letting go anywhere ends it");
|
||||
k.key(true, true);
|
||||
k.key(false, true);
|
||||
assert!(k.active(Some("toggle")));
|
||||
assert!(!k.active(None));
|
||||
assert!(
|
||||
!k.active(Some("toggle")),
|
||||
"turning it off forgets the toggle"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn zooms_four_times_and_turns_slower_by_default() {
|
||||
assert_eq!(zoom(4.0, "smooth", false), (4.0, false, 0.25));
|
||||
assert_eq!(zoom(10.0, "usual", true), (10.0, true, 1.0));
|
||||
assert_eq!(zoom(2.5, "slower", false), (2.5, false, 0.4));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn keeps_the_key() {
|
||||
with(|k| k.held = true);
|
||||
assert!(with(|k| k.held));
|
||||
with(|k| *k = Keys::default());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
// The zoom, while it's on: Horizon asks while playing, and the spyglass's own wins.
|
||||
fn incari_zoom_view(
|
||||
game: &crate::game::Game<'_>,
|
||||
zoom: &mut Option<crate::game::native_hooks::NativeZoom>,
|
||||
) {
|
||||
use crate::game::{incari_client as c, incari_zoom as m};
|
||||
let _ = game;
|
||||
let wanted = c::settings(m::ID, |s| {
|
||||
(
|
||||
s.choice("mode"),
|
||||
m::zoom(s.float("amount"), s.choice("turning"), s.bool("ease")),
|
||||
)
|
||||
});
|
||||
let active = m::with(|k| k.active(wanted.map(|w| w.0)));
|
||||
if zoom.is_some() || !active {
|
||||
return;
|
||||
}
|
||||
if let Some((_, (factor, smooth, turn))) = wanted {
|
||||
*zoom = Some(crate::game::native_hooks::NativeZoom {
|
||||
factor,
|
||||
smooth,
|
||||
turn,
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user