Merge pull request #145 from ludovic111/feat/controllers-options

Controllers: Controller options, button binds and glyphs
This commit is contained in:
Ludovic Marie
2026-10-14 19:36:29 +02:00
committed by GitHub
8 changed files with 367 additions and 4 deletions
@@ -394,6 +394,8 @@ impl Game<'_> {
"sound" => Page::Sound,
"controls" => Page::Controls,
"keybinds" => Page::KeyBinds,
"controller" => Page::Controller,
"controller-binds" => Page::PadBinds,
"mouse" => Page::Mouse,
"chat" => Page::Chat,
"accessibility" => Page::Accessibility,
+1 -1
View File
@@ -112,7 +112,7 @@ impl Game<'_> {
}
Ok(())
}
fn handle_option_events(&mut self, events: Vec<Event>) -> Result<()> {
pub(super) fn handle_option_events(&mut self, events: Vec<Event>) -> Result<()> {
for e in events {
match e {
Event::Changed => {
+1
View File
@@ -168,6 +168,7 @@ impl Game<'_> {
}
self.pad.start();
let hardware = self.pad.read();
self.ui.options.pad_style = self.settings.controller.glyphs.resolve(self.pad.style);
let state = hardware.merge(&self.pad.injected);
self.pad.virtual_input = self.pad.injected != PadState::default();
for button in std::mem::take(&mut self.pad.taps) {
@@ -10,6 +10,7 @@ use horizon_client::{
creative::Creative,
gamepad::{self, Button},
hud::{HudBatch, rgba},
options_ui::{Event, Page},
widgets,
};
use std::time::Instant;
@@ -221,6 +222,16 @@ impl Game<'_> {
}
return Ok(());
}
// Controller Binds waiting for a button: this one (Start cancels).
if self.screen == Screen::Options
&& self.ui.options.page() == Page::PadBinds
&& let Some(action) = self.ui.options.selected_pad_action.take()
{
if button != Button::Start {
self.settings.controller.bind(action, Some(button));
}
return self.handle_option_events(vec![Event::Changed]);
}
match button {
Button::East | Button::Start => return self.pad_back(),
Button::South | Button::West => {
+102
View File
@@ -76,6 +76,49 @@ impl Button {
fn bit(self) -> u32 {
1 << self as u32
}
/// What is printed on the button for a pad family (`Auto` reads as Xbox).
pub fn label(self, style: GlyphStyle) -> &'static str {
let i = self as usize;
match style {
GlyphStyle::PlayStation => [
"×", "○", "□", "△", "L1", "R1", "L2", "R2", "Create", "Options", "L3", "R3", "↑",
"↓", "←", "→",
][i],
GlyphStyle::Nintendo => [
"B", "A", "Y", "X", "L", "R", "ZL", "ZR", "−", "+", "LS", "RS", "↑", "↓", "←", "→",
][i],
GlyphStyle::Xbox | GlyphStyle::Auto => [
"A", "B", "X", "Y", "LB", "RB", "LT", "RT", "View", "Menu", "LS", "RS", "↑", "↓",
"←", "→",
][i],
}
}
/// The glyph's colour (RGB): face buttons in their family's colours,
/// the rest white.
pub fn color(self, style: GlyphStyle) -> [u8; 3] {
let face = match self {
Button::South => 0,
Button::East => 1,
Button::West => 2,
Button::North => 3,
_ => return [255, 255, 255],
};
match style {
GlyphStyle::PlayStation => [
[124, 178, 232],
[235, 110, 110],
[222, 145, 205],
[92, 205, 170],
][face],
GlyphStyle::Nintendo => [255, 255, 255],
GlyphStyle::Xbox | GlyphStyle::Auto => [
[110, 200, 90],
[235, 90, 80],
[80, 150, 240],
[240, 200, 60],
][face],
}
}
}
/// A pad's inputs at one moment. Sticks are `[x, y]` with right and up
@@ -205,6 +248,16 @@ pub const ACTIONS: &[(&str, Option<Button>)] = &[
("key.smoothCamera", None),
];
/// An action's name in the binds list: vanilla's translation key, or
/// Horizon's own English for the pad-only actions.
pub fn action_caption(action: &str) -> &str {
match action {
HOTBAR_PREVIOUS => "Hotbar: Previous Slot",
HOTBAR_NEXT => "Hotbar: Next Slot",
other => other,
}
}
/// Which button pictures prompts use.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum GlyphStyle {
@@ -232,6 +285,22 @@ impl GlyphStyle {
_ => GlyphStyle::Auto,
}
}
/// The style the player chose, else the connected pad's.
pub fn resolve(self, detected: GlyphStyle) -> GlyphStyle {
match self {
GlyphStyle::Auto => match detected {
GlyphStyle::Auto => GlyphStyle::Xbox,
d => d,
},
chosen => chosen,
}
}
pub const ALL: [GlyphStyle; 4] = [
GlyphStyle::Auto,
GlyphStyle::Xbox,
GlyphStyle::PlayStation,
GlyphStyle::Nintendo,
];
/// The style for a pad by its USB vendor (Sony, Nintendo; Microsoft,
/// Valve's Steam Deck and Steam Input's virtual pad, and everything else
/// look like Xbox).
@@ -391,6 +460,14 @@ impl PadSettings {
pub fn reset_bindings(&mut self) {
self.bindings.clear();
}
/// Other actions on the same button (marked in the binds list).
pub fn conflicts(&self, action: &str) -> bool {
self.binding(action)
.is_some_and(|b| self.actions_of(b).iter().any(|a| *a != action))
}
pub fn is_default(&self, action: &str) -> bool {
!self.bindings.contains_key(action)
}
/// The actions a button triggers, in `ACTIONS` order.
pub fn actions_of(&self, button: Button) -> Vec<&'static str> {
ACTIONS
@@ -656,6 +733,31 @@ mod tests {
assert_eq!(cursor_delta([0.1, 0.0], 0.15, 0.1), (0.0, 0.0));
}
#[test]
fn labels_and_conflicts() {
assert_eq!(Button::South.label(GlyphStyle::Xbox), "A");
assert_eq!(Button::South.label(GlyphStyle::PlayStation), "×");
assert_eq!(Button::South.label(GlyphStyle::Nintendo), "B");
assert_eq!(Button::RightTrigger.label(GlyphStyle::PlayStation), "R2");
assert_eq!(Button::DPadLeft.label(GlyphStyle::Xbox), "←");
assert_eq!(
GlyphStyle::Auto.resolve(GlyphStyle::PlayStation),
GlyphStyle::PlayStation
);
assert_eq!(
GlyphStyle::Xbox.resolve(GlyphStyle::PlayStation),
GlyphStyle::Xbox
);
assert_eq!(GlyphStyle::Auto.resolve(GlyphStyle::Auto), GlyphStyle::Xbox);
let mut s = PadSettings::default();
assert!(!s.conflicts("key.jump"));
s.bind("key.drop", Some(Button::South));
assert!(s.conflicts("key.jump") && s.conflicts("key.drop"));
assert!(!s.is_default("key.drop") && s.is_default("key.jump"));
assert_eq!(action_caption(HOTBAR_NEXT), "Hotbar: Next Slot");
assert_eq!(action_caption("key.jump"), "key.jump");
}
#[test]
fn hotbar_wraps_and_glyphs_follow_the_vendor() {
assert_eq!(hotbar_step(8, true), 0);
+25 -1
View File
@@ -72,7 +72,7 @@ pub const SECTIONS: &[Section] = &[
},
Section {
name: "Controls",
blurb: "Keys, the mouse and how movement keys behave.",
blurb: "Keys, the mouse, controllers and how movement keys behave.",
rows: |_| {
let mut r = vec![HRow::Page(
"Key binds",
@@ -91,6 +91,26 @@ pub const SECTIONS: &[Section] = &[
);
r.push(HRow::Header("Movement"));
r.extend([Opt::ToggleCrouch, Opt::ToggleSprint, Opt::AutoJump].map(HRow::Opt));
r.push(HRow::Header("Controller"));
r.push(HRow::Page(
"Controller binds",
"Every action and its button",
Page::PadBinds,
));
r.extend(
[
Opt::PadEnabled,
Opt::PadVibration,
Opt::PadLookSensitivity,
Opt::PadToggleSneak,
Opt::PadInvertX,
Opt::PadInvertY,
Opt::PadMoveDeadzone,
Opt::PadLookDeadzone,
Opt::PadGlyphs,
]
.map(HRow::Opt),
);
r
},
},
@@ -185,6 +205,10 @@ fn switch_value(opt: Opt, s: &Settings) -> Option<bool> {
Opt::ShowSubtitles => s.show_subtitles,
Opt::ForceUnicode => s.force_unicode,
Opt::JapaneseGlyphs => s.japanese_glyphs,
Opt::PadEnabled => s.controller.enabled,
Opt::PadVibration => s.controller.vibration,
Opt::PadInvertX => s.controller.invert_x,
Opt::PadInvertY => s.controller.invert_y,
_ => return None,
})
}
+224 -1
View File
@@ -3,6 +3,7 @@
//! Language and Resource Packs, laid out like vanilla's HeaderAndFooter /
//! OptionsList screens and drawn with the shared widgets.
use crate::{
gamepad::{self, GlyphStyle},
hud::{HudAtlas, HudBatch, rgba},
keybinds::{self, CATEGORIES, KeyBinds, MAPPINGS},
lang::{self, LanguageInfo},
@@ -67,6 +68,16 @@ pub enum Opt {
NotificationDisplayTime,
ForceUnicode,
JapaneseGlyphs,
/// Horizon's controller settings (`horizon-controller.txt`).
PadEnabled,
PadVibration,
PadLookSensitivity,
PadInvertX,
PadInvertY,
PadToggleSneak,
PadMoveDeadzone,
PadLookDeadzone,
PadGlyphs,
}
fn tr(key: &str) -> Text {
Text::translate(key, &[])
@@ -153,6 +164,16 @@ impl Opt {
Opt::NotificationDisplayTime => "options.notifications.display_time",
Opt::ForceUnicode => "options.forceUnicodeFont",
Opt::JapaneseGlyphs => "options.japaneseGlyphVariants",
// Horizon's own options: English captions (no vanilla keys).
Opt::PadEnabled => "Controller",
Opt::PadVibration => "Vibration",
Opt::PadLookSensitivity => "Look Sensitivity",
Opt::PadInvertX => "Invert Look X",
Opt::PadInvertY => "Invert Look Y",
Opt::PadToggleSneak => "Sneak Button",
Opt::PadMoveDeadzone => "Move Deadzone",
Opt::PadLookDeadzone => "Look Deadzone",
Opt::PadGlyphs => "Button Glyphs",
}
}
pub(crate) fn kind(self, s: &Settings) -> Kind {
@@ -187,6 +208,9 @@ impl Opt {
Opt::ChatHeightFocused => Kind::Slider(s.chat_height_focused),
Opt::ChatHeightUnfocused => Kind::Slider(s.chat_height_unfocused),
Opt::ChatLineSpacing => Kind::Slider(s.chat_line_spacing),
Opt::PadLookSensitivity => Kind::Slider(s.controller.look_sensitivity),
Opt::PadMoveDeadzone => unit(s.controller.move_deadzone, 0.0, 0.5),
Opt::PadLookDeadzone => unit(s.controller.look_deadzone, 0.0, 0.5),
_ => Kind::Cycle,
}
}
@@ -313,6 +337,21 @@ impl Opt {
&[&format!("{:.1}", s.notification_display_time)],
),
),
Opt::PadLookSensitivity => percent(c, 2.0 * s.controller.look_sensitivity),
Opt::PadMoveDeadzone | Opt::PadLookDeadzone => {
let v = if self == Opt::PadMoveDeadzone {
s.controller.move_deadzone
} else {
s.controller.look_deadzone
};
Text::translate(
"options.percent_value",
&[
tr(c),
Text::literal(((v * 100.0).round() as i32).to_string()),
],
)
}
_ => generic(c, self.value_text(s)),
}
}
@@ -378,6 +417,21 @@ impl Opt {
Opt::ShowSubtitles => on_off(s.show_subtitles),
Opt::ForceUnicode => on_off(s.force_unicode),
Opt::JapaneseGlyphs => on_off(s.japanese_glyphs),
Opt::PadEnabled => on_off(s.controller.enabled),
Opt::PadVibration => on_off(s.controller.vibration),
Opt::PadInvertX => on_off(s.controller.invert_x),
Opt::PadInvertY => on_off(s.controller.invert_y),
Opt::PadToggleSneak => tr(if s.controller.toggle_sneak {
"options.key.toggle"
} else {
"options.key.hold"
}),
Opt::PadGlyphs => Text::literal(match s.controller.glyphs {
GlyphStyle::Auto => "Auto",
GlyphStyle::Xbox => "Xbox",
GlyphStyle::PlayStation => "PlayStation",
GlyphStyle::Nintendo => "Nintendo",
}),
_ => Text::empty(),
}
}
@@ -411,6 +465,9 @@ impl Opt {
Opt::ChatHeightFocused => s.chat_height_focused = v,
Opt::ChatHeightUnfocused => s.chat_height_unfocused = v,
Opt::ChatLineSpacing => s.chat_line_spacing = v,
Opt::PadLookSensitivity => s.controller.look_sensitivity = v,
Opt::PadMoveDeadzone => s.controller.move_deadzone = step(0.0, 50.0) / 100.0,
Opt::PadLookDeadzone => s.controller.look_deadzone = step(0.0, 50.0) / 100.0,
_ => {}
}
if self.affects_preset() {
@@ -484,6 +541,18 @@ impl Opt {
Opt::ShowSubtitles => s.show_subtitles = !s.show_subtitles,
Opt::ForceUnicode => s.force_unicode = !s.force_unicode,
Opt::JapaneseGlyphs => s.japanese_glyphs = !s.japanese_glyphs,
Opt::PadEnabled => s.controller.enabled = !s.controller.enabled,
Opt::PadVibration => s.controller.vibration = !s.controller.vibration,
Opt::PadInvertX => s.controller.invert_x = !s.controller.invert_x,
Opt::PadInvertY => s.controller.invert_y = !s.controller.invert_y,
Opt::PadToggleSneak => s.controller.toggle_sneak = !s.controller.toggle_sneak,
Opt::PadGlyphs => {
let i = GlyphStyle::ALL
.iter()
.position(|g| *g == s.controller.glyphs)
.unwrap_or(0) as u8;
s.controller.glyphs = GlyphStyle::ALL[usize::from(next(i, 4))];
}
_ => {}
}
if self.affects_preset() {
@@ -542,6 +611,9 @@ pub enum Page {
/// Horizon's graphics settings (`graphics_ui`).
Graphics,
GraphicsAdvanced,
/// Horizon's controller settings and button binds.
Controller,
PadBinds,
}
#[derive(Clone, Debug, PartialEq)]
enum Act {
@@ -552,6 +624,10 @@ enum Act {
Key(&'static str),
ResetKey(&'static str),
ResetKeys,
/// Controller binds: wait for a button, reset one, reset all.
PadKey(&'static str),
ResetPadKey(&'static str),
ResetPadKeys,
/// Back to Horizon's own menus (Horizon only, see `list_rows`).
HorizonMenus,
}
@@ -612,6 +688,10 @@ pub struct OptionsUi {
pub(crate) hdragging: Option<crate::graphics_ui::Item>,
/// Key Binds: the mapping waiting for a key.
pub selected_key: Option<&'static str>,
/// Controller Binds: the action waiting for a pad button.
pub selected_pad_action: Option<&'static str>,
/// The glyphs the binds list shows (the setting, else the pad's family).
pub pad_style: GlyphStyle,
pub languages: Vec<LanguageInfo>,
language_choice: Option<String>,
language_search: ui::EditBox,
@@ -640,6 +720,8 @@ impl Default for OptionsUi {
hscroll: 0.0,
hdragging: None,
selected_key: None,
selected_pad_action: None,
pad_style: GlyphStyle::Xbox,
languages: Vec::new(),
language_choice: None,
language_search: search,
@@ -759,6 +841,8 @@ impl OptionsUi {
Page::Font => "options.font.title",
Page::Packs => "resourcePack.title",
Page::Graphics | Page::GraphicsAdvanced => "options.videoTitle",
Page::Controller => "Controller Settings",
Page::PadBinds => "Controller Binds",
}
}
fn list_rows(&self, s: &Settings) -> Vec<Row> {
@@ -863,6 +947,41 @@ impl OptionsUi {
Opt::ToggleSprint,
Opt::AutoJump,
]));
// Horizon's controller page, after vanilla's rows.
rows.push(Row::Buttons(
button(
0.0,
0.0,
310.0,
Text::literal("Controller Settings..."),
Act::Push(Page::Controller),
),
None,
));
rows
}
Page::Controller => {
let mut rows = vec![Row::Buttons(
button(
0.0,
0.0,
310.0,
Text::literal("Controller Binds..."),
Act::Push(Page::PadBinds),
),
None,
)];
rows.extend(pairs(&[
Opt::PadEnabled,
Opt::PadVibration,
Opt::PadLookSensitivity,
Opt::PadToggleSneak,
Opt::PadInvertX,
Opt::PadInvertY,
Opt::PadMoveDeadzone,
Opt::PadLookDeadzone,
Opt::PadGlyphs,
]));
rows
}
Page::Mouse => pairs(&[
@@ -1082,6 +1201,88 @@ impl OptionsUi {
));
out.push(button(cx + 4.0, h - 27.0, 150.0, tr("gui.done"), Act::Done));
}
Page::PadBinds => {
out.push(Widget {
x: 0.0,
y: 12.0,
w,
h: 9.0,
kind: WKind::Text {
text: tr(Self::title(page)),
color: u32::MAX,
centered: true,
},
list: false,
});
let row_left = cx - 170.0;
let scrollbar_x = row_left + 340.0 + 6.0 + 2.0;
let list_start = out.len();
let mut y = top + 2.0 - self.scroll;
let pad = &s.controller;
for (action, _) in gamepad::ACTIONS {
out.push(Widget {
x: row_left + 2.0,
y: y + 6.0,
w: 200.0,
h: 9.0,
kind: WKind::Text {
text: tr(gamepad::action_caption(action)),
color: u32::MAX,
centered: false,
},
list: false,
});
let mut message = match pad.binding(action) {
Some(b) => {
let [r, g, bl] = b.color(self.pad_style);
Text::literal(b.label(self.pad_style))
.with_color(u32::from(r) << 16 | u32::from(g) << 8 | u32::from(bl))
}
None => tr("key.keyboard.unknown"),
};
if pad.conflicts(action) {
message = Text::literal("[ ")
.append(message)
.append(" ]")
.with_color(text::YELLOW);
}
if self.selected_pad_action == Some(*action) {
message = Text::literal("> ")
.append(message.with_style(&Style {
underlined: Some(true),
..Style::default()
}))
.append(" <")
.with_color(text::YELLOW);
}
let reset_x = scrollbar_x - 50.0 - 10.0;
let change_x = reset_x - 5.0 - 75.0;
out.push(button(change_x, y, 75.0, message, Act::PadKey(action)));
let reset = button(
reset_x,
y,
50.0,
tr("controls.reset"),
Act::ResetPadKey(action),
);
out.push(if pad.is_default(action) {
inactive(reset)
} else {
reset
});
y += 20.0;
}
content = y + self.scroll - top + 2.0;
out[list_start..].iter_mut().for_each(|w| w.list = true);
out.push(button(
cx - 154.0,
h - 27.0,
150.0,
tr("controls.resetAll"),
Act::ResetPadKeys,
));
out.push(button(cx + 4.0, h - 27.0, 150.0, tr("gui.done"), Act::Done));
}
Page::Language => {
top = 36.0;
bottom = h - 53.0;
@@ -1373,7 +1574,7 @@ impl OptionsUi {
if list {
ui::separators(batch, atlas, top, bottom, in_world);
let sx = match self.page() {
Page::KeyBinds => (w / 2.0).floor() + 170.0 + 8.0,
Page::KeyBinds | Page::PadBinds => (w / 2.0).floor() + 170.0 + 8.0,
Page::Language => (w / 2.0).floor() + 110.0 + 8.0,
_ => (w / 2.0).floor() + 155.0 + 8.0,
};
@@ -1466,6 +1667,10 @@ impl OptionsUi {
}
return events;
}
// Controller Binds: a click cancels the wait.
if self.selected_pad_action.take().is_some() {
return events;
}
// Key Binds: a mouse button pressed while waiting binds it.
if let Some(name) = self.selected_key.take() {
let button = if right {
@@ -1506,6 +1711,16 @@ impl OptionsUi {
s.keys.reset_all();
events.push(Event::Changed);
}
Act::PadKey(action) => self.selected_pad_action = Some(action),
Act::ResetPadKey(action) => {
s.controller
.bind(action, gamepad::PadSettings::default_of(action));
events.push(Event::Changed);
}
Act::ResetPadKeys => {
s.controller.reset_bindings();
events.push(Event::Changed);
}
Act::HorizonMenus => {
self.horizon_menus();
if let Ok(dir) = std::env::current_dir() {
@@ -1606,6 +1821,7 @@ impl OptionsUi {
self.scroll = 0.0;
self.dragging = None;
self.selected_key = None;
self.selected_pad_action = None;
match page {
Page::Language => {
if let Some(code) = self.language_choice.take()
@@ -1698,6 +1914,13 @@ impl OptionsUi {
_ => {}
}
}
if let Some(action) = self.selected_pad_action.take() {
// As on Key Binds, Escape unbinds; other keys cancel.
if key == K::Escape {
s.controller.bind(action, None);
}
return vec![Event::Changed];
}
if let Some(name) = self.selected_key.take() {
// KeyBindsScreen.keyPressed: Escape unbinds.
let value = if key == K::Escape {
+1 -1
View File
@@ -98,7 +98,7 @@ the label in the game font, sized to the GUI scale, with two styles:
- **Xbox / Steam Deck / generic**: A B X Y in their usual colours, LB RB LT
RT, View, Menu, stick icons.
- **PlayStation**: ✕ ○ □ △ drawn as shapes in their colours, L1 R1 L2 R2,
- **PlayStation**: × ○ □ △ in their colours (× rather than ✕, which the game fonts lack), L1 R1 L2 R2,
Create/Share, Options.
- **Nintendo**: B A Y X by position.