Extras: the zoom key zooms only while playing and never takes keys
The zoom key went to the zoom on every screen, so with zoom on, Z could not be typed in screens with text fields that share the game's key handler. Now it zooms only while playing with no sign or book open, lets go when a screen opens, and always passes the key on, like a vanilla key mapping. A vanilla mapping bound to the same key still acts; the Extras page marks the zoom key "Also bound" and names it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -191,6 +191,16 @@ impl Zoom {
|
||||
pub fn release(&mut self) {
|
||||
self.held = false;
|
||||
}
|
||||
/// A press or release of the zoom key. Only a press while `playing` (no
|
||||
/// screen or text field open) zooms; anywhere else the key belongs to
|
||||
/// the screen, and the zoom lets go.
|
||||
pub fn key(&mut self, pressed: bool, repeat: bool, playing: bool, level: u8) {
|
||||
if !pressed || !playing {
|
||||
self.release();
|
||||
} else if !repeat {
|
||||
self.press(level);
|
||||
}
|
||||
}
|
||||
pub fn held(&self) -> bool {
|
||||
self.held
|
||||
}
|
||||
@@ -212,6 +222,19 @@ impl Zoom {
|
||||
}
|
||||
}
|
||||
}
|
||||
/// Vanilla key mappings bound to the zoom key. The key still reaches them
|
||||
/// (a key bound twice triggers both, as vanilla's do), so the Extras page
|
||||
/// says so.
|
||||
pub fn zoom_key_conflicts(e: &Extras, keys: &crate::keybinds::KeyBinds) -> Vec<&'static str> {
|
||||
if e.zoom_key == "key.keyboard.unknown" {
|
||||
return Vec::new();
|
||||
}
|
||||
crate::keybinds::MAPPINGS
|
||||
.iter()
|
||||
.map(|m| m.0)
|
||||
.filter(|name| keys.get(name) == e.zoom_key)
|
||||
.collect()
|
||||
}
|
||||
/// The field-of-view modifier target `Camera.tickFov` eases toward: vanilla's
|
||||
/// (`getFieldOfViewModifier`) divided by the zoom. A zoom of 1 leaves it as is.
|
||||
pub fn fov_target(vanilla: f32, zoom: f32) -> f32 {
|
||||
@@ -526,6 +549,48 @@ mod tests {
|
||||
assert!((floor - 7.0).abs() < 1e-3, "{floor}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_zoom_key_only_zooms_while_playing() {
|
||||
let mut z = Zoom::default();
|
||||
// In chat, a sign, a text field or any screen: no zoom.
|
||||
z.key(true, false, false, 4);
|
||||
assert!(!z.held());
|
||||
z.key(true, false, true, 4);
|
||||
assert!(z.held());
|
||||
// A screen opening while held lets go (its key presses are the screen's).
|
||||
z.key(true, true, false, 4);
|
||||
assert!(!z.held());
|
||||
// Key repeats do not restart a zoom the wheel changed.
|
||||
z.key(true, false, true, 4);
|
||||
z.scroll(1.0);
|
||||
z.key(true, true, true, 4);
|
||||
assert!((z.factor(false) - 5.0).abs() < 1e-5);
|
||||
z.key(false, false, true, 4);
|
||||
assert!(!z.held());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn zoom_key_conflicts_name_the_vanilla_mappings() {
|
||||
let mut e = Extras::default();
|
||||
let mut keys = crate::keybinds::KeyBinds::default();
|
||||
assert!(zoom_key_conflicts(&e, &keys).is_empty());
|
||||
e.zoom_key = "key.keyboard.c".into();
|
||||
assert_eq!(
|
||||
zoom_key_conflicts(&e, &keys),
|
||||
[
|
||||
"key.saveToolbarActivator",
|
||||
"key.debug.crash",
|
||||
"key.debug.copyLocation"
|
||||
]
|
||||
);
|
||||
keys.set("key.drop", "key.keyboard.z");
|
||||
e.zoom_key = "key.keyboard.z".into();
|
||||
assert_eq!(zoom_key_conflicts(&e, &keys), ["key.drop"]);
|
||||
e.zoom_key = "key.keyboard.unknown".into();
|
||||
keys.set("key.smoothCamera", "key.keyboard.unknown");
|
||||
assert!(zoom_key_conflicts(&e, &keys).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sensitivity_scales_with_zoom() {
|
||||
assert_eq!(turn_scale(1.0, true), 1.0);
|
||||
|
||||
@@ -8530,9 +8530,7 @@ impl ApplicationHandler for Game<'_> {
|
||||
let PhysicalKey::Code(key) = event.physical_key else {
|
||||
return Ok(());
|
||||
};
|
||||
if self.extras_key(key, event.state == ElementState::Pressed, event.repeat) {
|
||||
return Ok(());
|
||||
}
|
||||
self.extras_key(key, event.state == ElementState::Pressed, event.repeat);
|
||||
// key.smoothCamera toggles the cinematic camera.
|
||||
if self.screen == Screen::Playing
|
||||
&& event.state == ElementState::Pressed
|
||||
|
||||
@@ -163,15 +163,11 @@ impl Game<'_> {
|
||||
self.debug_key(key, KeyAction::Release);
|
||||
}
|
||||
}
|
||||
// The zoom extra's key, as the keyboard hook sees it.
|
||||
if self.screen == Screen::Playing {
|
||||
let action = arg(2)?;
|
||||
if self.extras_key(physical, action != "up", false) {
|
||||
if action == "tap" {
|
||||
self.extras_key(physical, false, false);
|
||||
}
|
||||
return ok();
|
||||
}
|
||||
// The zoom extra sees the key as the keyboard hook does.
|
||||
let action = arg(2)?;
|
||||
self.extras_key(physical, action != "up", false);
|
||||
if action == "tap" {
|
||||
self.extras_key(physical, false, false);
|
||||
}
|
||||
// Text screens consume raw presses; releases follow key binds.
|
||||
let typing = matches!(self.screen, Screen::Chat | Screen::Options);
|
||||
|
||||
@@ -28,17 +28,19 @@ impl Game<'_> {
|
||||
let e = &self.settings.extras;
|
||||
e.zoom.then(|| keybinds::key_code(&e.zoom_key)).flatten()
|
||||
}
|
||||
/// A key press or release while playing; true when the zoom key took it.
|
||||
pub(super) fn extras_key(&mut self, key: KeyCode, pressed: bool, repeat: bool) -> bool {
|
||||
/// A key press or release. The zoom key zooms only while playing with
|
||||
/// no text open; the key itself always goes on to the screen and the key
|
||||
/// binds, as a vanilla key mapping's would.
|
||||
pub(super) fn extras_key(&mut self, key: KeyCode, pressed: bool, repeat: bool) {
|
||||
if self.zoom_key() != Some(key) {
|
||||
return false;
|
||||
return;
|
||||
}
|
||||
if !pressed {
|
||||
self.extras.zoom.release();
|
||||
} else if !repeat && self.screen == Screen::Playing && self.title.is_none() {
|
||||
self.extras.zoom.press(self.settings.extras.zoom_level);
|
||||
}
|
||||
true
|
||||
let playing = self.screen == Screen::Playing
|
||||
&& self.title.is_none()
|
||||
&& self.sign_edit.is_none()
|
||||
&& self.book.is_none();
|
||||
let level = self.settings.extras.zoom_level;
|
||||
self.extras.zoom.key(pressed, repeat, playing, level);
|
||||
}
|
||||
/// Wheel steps while playing; true when zooming took them from the hotbar.
|
||||
pub(super) fn extras_scroll(&mut self, steps: f32) -> bool {
|
||||
|
||||
@@ -418,8 +418,18 @@ impl OptionsUi {
|
||||
HRow::Extra(item) => {
|
||||
let cy = y + ROW_H / 2.0;
|
||||
let id = ROW + i as u64;
|
||||
let cost = item.cost().map_or((0, ""), |c| (c as u8, c.label()));
|
||||
let (caption, text) = (item.caption(), item.description());
|
||||
let mut cost = item.cost().map_or((0, ""), |c| (c as u8, c.label()));
|
||||
let (caption, mut text) = (item.caption(), item.description().to_owned());
|
||||
// A vanilla mapping on the same key: both act on it, like
|
||||
// the binds list's duplicates; say which.
|
||||
let conflicts = extras::zoom_key_conflicts(&s.extras, &s.keys);
|
||||
if item == extras::Item::ZoomKey && !conflicts.is_empty() {
|
||||
let names: Vec<String> =
|
||||
conflicts.iter().map(|c| crate::lang::get(c)).collect();
|
||||
cost = (3, "Also bound");
|
||||
text = format!("{} uses this key too; both act on it.", names.join(", "));
|
||||
}
|
||||
let text = text.as_str();
|
||||
if let Some(on) = item.switch(&s.extras) {
|
||||
ui.described_row(x, y, caption, "", 0.0, cost, text, cw - 60.0);
|
||||
ui.switch(id, control_x, cy, on);
|
||||
|
||||
@@ -36,6 +36,11 @@ Horizon category in Controls, and adding one would put a non-vanilla mapping int
|
||||
### Zoom (`zoom`, default off)
|
||||
- Hold the zoom key (default **Z**, unused by every vanilla 26.3 key mapping; C
|
||||
is `key.saveToolbarActivator`) to zoom. The key does nothing while zoom is off.
|
||||
- It zooms only while playing: in chat, signs, books, text fields and every
|
||||
other screen the key is typed or handled as usual. The key is never taken
|
||||
from vanilla's mappings: one bound to the same key still acts (a key bound
|
||||
twice triggers both, as vanilla's do), and the Extras page marks the zoom key
|
||||
"Also bound" and names the mapping.
|
||||
- Starts at the default level (4x, settable 2x to 10x); the scroll wheel changes
|
||||
it while the key is held (and does not move the hotbar then).
|
||||
- **Smooth** (default on) goes through vanilla's own FOV easing: the zoom divides
|
||||
|
||||
Reference in New Issue
Block a user