Compare commits

...

2 commits

Author SHA1 Message Date
11fd29c9c6 show input conflicts in binding menu 2025-04-03 14:25:00 +02:00
3fdcb50694 cleanup 2025-04-03 14:02:12 +02:00

View file

@ -103,7 +103,6 @@ type InputMap = BTreeMap<ActionId, Vec<Binding>>;
pub struct Input { pub struct Input {
bindings: [Vec<Binding>; ActionId::SIZE], bindings: [Vec<Binding>; ActionId::SIZE],
states: [BindingState; ActionId::SIZE], states: [BindingState; ActionId::SIZE],
#[serde(skip)]
editing_binding: Option<(ActionId, usize, BindingEdit)>, editing_binding: Option<(ActionId, usize, BindingEdit)>,
} }
@ -141,6 +140,17 @@ impl Input {
let x = binding_text_x + 10 + d.measure_text(&trigger, 20); let x = binding_text_x + 10 + d.measure_text(&trigger, 20);
let modifiers = format!("{:?}", binding.modifiers); let modifiers = format!("{:?}", binding.modifiers);
d.draw_text(&modifiers, x, y + 5, 20, Color::LIGHTBLUE); d.draw_text(&modifiers, x, y + 5, 20, Color::LIGHTBLUE);
let conflicts = conflicts(&self.bindings, binding, action);
if !conflicts.is_empty() {
let x = x + 10 + d.measure_text(&modifiers, 20);
d.draw_text(
&format!("also used by: {conflicts:?}"),
x,
y + 5,
20,
Color::ORANGERED,
);
}
y += 32; y += 32;
} }
if text_button(d, &globals.mouse, buttons_x, y, 130, "add binding") { if text_button(d, &globals.mouse, buttons_x, y, 130, "add binding") {
@ -152,15 +162,15 @@ impl Input {
if let Some((action, binding_index, edit_state)) = &mut self.editing_binding { if let Some((action, binding_index, edit_state)) = &mut self.editing_binding {
globals.mouse.update(d); globals.mouse.update(d);
let border = screen_centered_rect(d, 368, 128); let border = screen_centered_rect(d, 408, 128);
d.draw_rectangle_rec(border, BG_LIGHT); d.draw_rectangle_rec(border, BG_LIGHT);
let bounds = screen_centered_rect(d, 360, 120); let bounds = screen_centered_rect(d, 400, 120);
d.draw_rectangle_rec(bounds, BG_DARK); d.draw_rectangle_rec(bounds, BG_DARK);
let x = bounds.x as i32; let x = bounds.x as i32;
let y = bounds.y as i32; let y = bounds.y as i32;
d.draw_text( d.draw_text(
&format!("editing binding for {action:?}"), &format!("editing binding for {action:?}"),
x + 5, x + 10,
y + 5, y + 5,
20, 20,
Color::WHITE, Color::WHITE,
@ -218,6 +228,17 @@ impl Input {
}; };
let text = format!("{:?} + {:?}", b.modifiers, b.trigger); let text = format!("{:?} + {:?}", b.modifiers, b.trigger);
d.draw_text(&text, x + 5, y + 5, 20, colour); d.draw_text(&text, x + 5, y + 5, 20, colour);
let conflicts = conflicts(&self.bindings, b, *action);
if !conflicts.is_empty() {
d.draw_text(
&format!("conflicts: {conflicts:?}"),
x + 200,
y + 40,
20,
Color::ORANGERED,
);
}
} }
if text_button(d, &globals.mouse, ok_btn_x, ok_btn_y, ok_btn_width, "ok") { if text_button(d, &globals.mouse, ok_btn_x, ok_btn_y, ok_btn_width, "ok") {
if let BindingEdit::Releasing(binding) = edit_state { if let BindingEdit::Releasing(binding) = edit_state {
@ -274,6 +295,29 @@ impl Input {
} }
} }
fn conflicts(
bindings: &[Vec<Binding>; ActionId::SIZE],
search: &Binding,
skip: ActionId,
) -> Vec<ActionId> {
let mut matches = Vec::new();
for i in 0..ActionId::SIZE {
if skip as usize == i {
continue;
}
let bindings = &bindings[i];
for binding in bindings {
if binding == search {
matches.push(ActionId::from_usize(i).unwrap());
break;
}
}
}
matches
}
impl ActionId { impl ActionId {
pub const SIZE: usize = Self::_EnumSize as usize; pub const SIZE: usize = Self::_EnumSize as usize;
@ -286,7 +330,7 @@ impl ActionId {
} }
} }
#[derive(Clone, Debug, Serialize, Deserialize)] #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct Binding { pub struct Binding {
modifiers: Vec<Button>, modifiers: Vec<Button>,
trigger: Button, trigger: Button,
@ -295,8 +339,8 @@ pub struct Binding {
impl From<InputMap> for Input { impl From<InputMap> for Input {
fn from(value: InputMap) -> Self { fn from(value: InputMap) -> Self {
let mut new = Self::default(); let mut new = Self::default();
for (action, saved_bindings) in value { for (action, loaded_bindings) in value {
new.bindings[action as usize] = saved_bindings; new.bindings[action as usize] = loaded_bindings;
} }
new new
} }