Merge pull request #2176 from kacpherek/fix/2161-shared-key-bindings
🤖 Agent: Gate check by Ludovic's AI agent (Claude). Checked against 26.3: `KeyMapping.click` and `set` act on every mapping bound to the key, as here, and your second commit stops a key from replaying a press when two mappings share it by default. #2207 fixes the same issue; I kept this one because it came first and changes less. Gate: this wave's 55 PRs merged on main in one tree: clippy `-D warnings` clean, 1186 horizon-client and 163 horizon-world tests pass, and of the 101 harnesses `parity.py --changed-files` picked, 100 pass (`block_removal` failed on one spawner case from #2199's orb merging, fixed there). `lan_e2e.py` passes 24 checks with the host on its server thread and in-frame (chest, xp, pvp, doors, villagers, beds, frames, projectiles, arrows, fishing, damage, mobs, blast, drops, elytra, breath, predict, cracks, barter, kills, hurt, contacts, underwater, plates), plus forced-levels on the server thread. Thanks @kacpherek!
This commit is contained in:
@@ -11626,16 +11626,17 @@ impl ApplicationHandler for Game<'_> {
|
|||||||
}
|
}
|
||||||
self.extras_mouse(button, pressed);
|
self.extras_mouse(button, pressed);
|
||||||
self.mod_mouse_key(button, pressed);
|
self.mod_mouse_key(button, pressed);
|
||||||
match self.settings.keys.translate_mouse(button) {
|
for mapped in self.settings.keys.translate_mouse_all(button) {
|
||||||
Some(Err(b)) => self.play_mouse(b, pressed)?,
|
match mapped {
|
||||||
Some(Ok(k)) if pressed => {
|
Err(b) => self.play_mouse(b, pressed)?,
|
||||||
self.keys.insert(k);
|
Ok(k) if pressed => {
|
||||||
self.key_pressed(k)?;
|
self.keys.insert(k);
|
||||||
|
self.key_pressed(k)?;
|
||||||
|
}
|
||||||
|
Ok(k) => {
|
||||||
|
self.keys.remove(&k);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Some(Ok(k)) => {
|
|
||||||
self.keys.remove(&k);
|
|
||||||
}
|
|
||||||
None => {}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Screen::Inventory | Screen::Creative
|
Screen::Inventory | Screen::Creative
|
||||||
@@ -11883,18 +11884,24 @@ impl ApplicationHandler for Game<'_> {
|
|||||||
{
|
{
|
||||||
self.smooth_camera = !self.smooth_camera;
|
self.smooth_camera = !self.smooth_camera;
|
||||||
}
|
}
|
||||||
// Key binds: act as the default key of the bound mapping.
|
// Key binds: act as the default key of every mapping bound
|
||||||
let key = match self.settings.keys.translate(key) {
|
// to it (KeyMapping.set drives them all).
|
||||||
Some(Ok(k)) => k,
|
for mapped in self.settings.keys.translate_all(key) {
|
||||||
Some(Err(button)) => {
|
match mapped {
|
||||||
if self.screen == Screen::Playing && !event.repeat {
|
Ok(k) => {
|
||||||
self.play_mouse(button, event.state == ElementState::Pressed)?;
|
self.mapped_key(
|
||||||
|
k,
|
||||||
|
event.state == ElementState::Pressed,
|
||||||
|
event.repeat,
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
Err(button) => {
|
||||||
|
if self.screen == Screen::Playing && !event.repeat {
|
||||||
|
self.play_mouse(button, event.state == ElementState::Pressed)?;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return Ok(());
|
|
||||||
}
|
}
|
||||||
None => return Ok(()),
|
}
|
||||||
};
|
|
||||||
self.mapped_key(key, event.state == ElementState::Pressed, event.repeat)?;
|
|
||||||
}
|
}
|
||||||
WindowEvent::RedrawRequested => {
|
WindowEvent::RedrawRequested => {
|
||||||
self.observe_fullscreen();
|
self.observe_fullscreen();
|
||||||
|
|||||||
@@ -275,6 +275,20 @@ impl Default for KeyBinds {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/// Each default key once, in order: two mappings that share a default key
|
||||||
|
/// (Advancements and the debug profiler both on L) are one press of it, not
|
||||||
|
/// two (the second would close the screen the first opened). Vanilla clicks
|
||||||
|
/// both mappings, but the profiler one only acts with F3 held.
|
||||||
|
fn distinct<T: PartialEq>(keys: Vec<T>) -> Vec<T> {
|
||||||
|
let mut out = Vec::with_capacity(keys.len());
|
||||||
|
for k in keys {
|
||||||
|
if !out.contains(&k) {
|
||||||
|
out.push(k);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
out
|
||||||
|
}
|
||||||
|
|
||||||
impl KeyBinds {
|
impl KeyBinds {
|
||||||
pub fn get(&self, name: &str) -> &str {
|
pub fn get(&self, name: &str) -> &str {
|
||||||
self.keys
|
self.keys
|
||||||
@@ -320,49 +334,80 @@ impl KeyBinds {
|
|||||||
}
|
}
|
||||||
/// The mapping a physical input triggers (first in list order).
|
/// The mapping a physical input triggers (first in list order).
|
||||||
pub fn mapping_for(&self, key: &str) -> Option<&'static str> {
|
pub fn mapping_for(&self, key: &str) -> Option<&'static str> {
|
||||||
MAPPINGS.iter().map(|m| m.0).find(|n| self.get(n) == key)
|
self.mappings_for(key).next()
|
||||||
|
}
|
||||||
|
/// Every mapping a physical input triggers, in list order: like
|
||||||
|
/// `KeyMapping.set`/`click`, one key drives all the mappings bound to it.
|
||||||
|
pub fn mappings_for<'a>(&'a self, key: &'a str) -> impl Iterator<Item = &'static str> + 'a {
|
||||||
|
MAPPINGS
|
||||||
|
.iter()
|
||||||
|
.map(|m| m.0)
|
||||||
|
.filter(move |n| self.get(n) == key)
|
||||||
}
|
}
|
||||||
/// Translate a physical key into the default key of the mapping it is
|
/// Translate a physical key into the default key of the mapping it is
|
||||||
/// bound to, so game code written against vanilla defaults follows the
|
/// bound to, so game code written against vanilla defaults follows the
|
||||||
/// player's bindings. `None` means the key now does nothing; mouse
|
/// player's bindings. `None` means the key now does nothing; mouse
|
||||||
/// mappings bound to keys come back as `Err(button)`.
|
/// mappings bound to keys come back as `Err(button)`. The first of
|
||||||
|
/// [`KeyBinds::translate_all`].
|
||||||
pub fn translate(&self, key: KeyCode) -> Option<Result<KeyCode, MouseButton>> {
|
pub fn translate(&self, key: KeyCode) -> Option<Result<KeyCode, MouseButton>> {
|
||||||
|
self.translate_all(key).into_iter().next()
|
||||||
|
}
|
||||||
|
/// The default keys of every mapping `key` is bound to (empty when it
|
||||||
|
/// now does nothing), so a key bound to two mappings (forward and
|
||||||
|
/// sprint) drives both, as `KeyMapping.set` does.
|
||||||
|
pub fn translate_all(&self, key: KeyCode) -> Vec<Result<KeyCode, MouseButton>> {
|
||||||
let Some(name) = key_name(key) else {
|
let Some(name) = key_name(key) else {
|
||||||
return Some(Ok(key));
|
return vec![Ok(key)];
|
||||||
};
|
};
|
||||||
if let Some(mapping) = self.mapping_for(name) {
|
let bound: Vec<_> = self
|
||||||
let default = Self::default_of(mapping);
|
.mappings_for(name)
|
||||||
if let Some(code) = key_code(default) {
|
.filter_map(|mapping| {
|
||||||
return Some(Ok(code));
|
let default = Self::default_of(mapping);
|
||||||
}
|
match key_code(default) {
|
||||||
return mouse_button(default).map(Err);
|
Some(code) => Some(Ok(code)),
|
||||||
|
None => mouse_button(default).map(Err),
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
if !bound.is_empty() {
|
||||||
|
return distinct(bound);
|
||||||
}
|
}
|
||||||
// An unbound key whose default meaning was moved elsewhere is inert.
|
// An unbound key whose default meaning was moved elsewhere is inert.
|
||||||
if MAPPINGS
|
if MAPPINGS
|
||||||
.iter()
|
.iter()
|
||||||
.any(|(n, d, _)| *d == name && !self.is_default(n))
|
.any(|(n, d, _)| *d == name && !self.is_default(n))
|
||||||
{
|
{
|
||||||
return None;
|
return Vec::new();
|
||||||
}
|
}
|
||||||
Some(Ok(key))
|
vec![Ok(key)]
|
||||||
}
|
}
|
||||||
/// Translate a mouse button the same way (keys bound to mouse buttons).
|
/// Translate a mouse button the same way (keys bound to mouse buttons).
|
||||||
pub fn translate_mouse(&self, button: MouseButton) -> Option<Result<KeyCode, MouseButton>> {
|
pub fn translate_mouse(&self, button: MouseButton) -> Option<Result<KeyCode, MouseButton>> {
|
||||||
|
self.translate_mouse_all(button).into_iter().next()
|
||||||
|
}
|
||||||
|
/// [`KeyBinds::translate_all`] for a mouse button.
|
||||||
|
pub fn translate_mouse_all(&self, button: MouseButton) -> Vec<Result<KeyCode, MouseButton>> {
|
||||||
let name = mouse_name(button);
|
let name = mouse_name(button);
|
||||||
if let Some(mapping) = self.mapping_for(&name) {
|
let bound: Vec<_> = self
|
||||||
let default = Self::default_of(mapping);
|
.mappings_for(&name)
|
||||||
if let Some(b) = mouse_button(default) {
|
.filter_map(|mapping| {
|
||||||
return Some(Err(b));
|
let default = Self::default_of(mapping);
|
||||||
}
|
match mouse_button(default) {
|
||||||
return key_code(default).map(Ok);
|
Some(b) => Some(Err(b)),
|
||||||
|
None => key_code(default).map(Ok),
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
if !bound.is_empty() {
|
||||||
|
return distinct(bound);
|
||||||
}
|
}
|
||||||
if MAPPINGS
|
if MAPPINGS
|
||||||
.iter()
|
.iter()
|
||||||
.any(|(n, d, _)| *d == name && !self.is_default(n))
|
.any(|(n, d, _)| *d == name && !self.is_default(n))
|
||||||
{
|
{
|
||||||
return None;
|
return Vec::new();
|
||||||
}
|
}
|
||||||
Some(Err(button))
|
vec![Err(button)]
|
||||||
}
|
}
|
||||||
/// `AbstractContainerScreen.checkHotbarMouseClicked`: off hand, then hotbar.
|
/// `AbstractContainerScreen.checkHotbarMouseClicked`: off hand, then hotbar.
|
||||||
pub fn container_mouse_key(&self, button: MouseButton, creative: bool) -> Option<KeyCode> {
|
pub fn container_mouse_key(&self, button: MouseButton, creative: bool) -> Option<KeyCode> {
|
||||||
|
|||||||
Reference in New Issue
Block a user