add edit mode popup for keybindings

This commit is contained in:
Crispy 2025-04-02 22:48:08 +02:00
parent 14edee5a53
commit 1bb29b5f75
3 changed files with 39 additions and 6 deletions

View file

@ -17,7 +17,7 @@ pub enum MenuReturn {
impl Config {
#[must_use]
pub fn draw_edit(&mut self, d: &mut RaylibDrawHandle, globals: &Globals) -> MenuReturn {
pub fn draw_edit(&mut self, d: &mut RaylibDrawHandle, globals: &mut Globals) -> MenuReturn {
d.draw_text("Settings", 16, 16, 30, FG_CHAPTER_TITLE);
if text_button(d, &globals.mouse, 10, 60, 80, "apply") {

View file

@ -8,7 +8,12 @@ use raylib::{
};
use serde::{Deserialize, Serialize};
use crate::{ui::text_button, Globals};
use crate::{
theme::{BG_DARK, BG_LIGHT},
ui::text_button,
util::screen_centered_rect,
Globals,
};
#[derive(Debug, Clone, Copy, Deserialize, Serialize, PartialEq, Eq, PartialOrd, Ord)]
#[repr(u8)]
@ -52,6 +57,7 @@ impl Default for Input {
Self {
bindings,
states: Default::default(),
editing_input: None,
}
}
}
@ -72,11 +78,17 @@ type InputMap = BTreeMap<ActionId, Vec<Binding>>;
pub struct Input {
bindings: [Vec<Binding>; ActionId::SIZE],
states: [BindingState; ActionId::SIZE],
#[serde(skip)]
editing_input: Option<(ActionId, usize)>,
}
impl Input {
pub fn draw_edit(&mut self, d: &mut RaylibDrawHandle, globals: &Globals) {
pub fn draw_edit(&mut self, d: &mut RaylibDrawHandle, globals: &mut Globals) {
let mut y = 96;
if self.editing_input.is_some() {
globals.mouse.clear();
}
for action_index in 0..ActionId::SIZE {
let action = ActionId::from_usize(action_index).unwrap();
@ -89,13 +101,34 @@ impl Input {
self.bindings[action_index].remove(binding_index);
return;
}
if text_button(d, &globals.mouse, 245, y, 45, "edit") {
self.editing_input = Some((action, binding_index));
}
let trigger = format!("{:?}", binding.trigger);
d.draw_text(&trigger, 256, y, 20, Color::LIMEGREEN);
let x = 264 + d.measure_text(&trigger, 20);
d.draw_text(&trigger, 300, y, 20, Color::LIMEGREEN);
let x = 310 + d.measure_text(&trigger, 20);
let modifiers = format!("{:?}", binding.modifiers);
d.draw_text(&modifiers, x, y, 20, Color::LIGHTBLUE);
y += 32;
}
y += 8;
}
if let Some((action, binding_index)) = &self.editing_input {
globals.mouse.update(d); // todo less scuffed
let border = screen_centered_rect(d, 208, 88);
d.draw_rectangle_rec(border, BG_LIGHT);
let bounds = screen_centered_rect(d, 200, 80);
d.draw_rectangle_rec(bounds, BG_DARK);
// TODO capture and display current input
let x = bounds.x as i32;
let y = bounds.y as i32;
if text_button(d, &globals.mouse, x + 10, y + 40, 80, "ok") {}
if text_button(d, &globals.mouse, x + 100, y + 40, 80, "cancel") {
self.editing_input = None;
}
}
}

View file

@ -97,7 +97,7 @@ impl Game {
}
} else if let Some(config) = &mut self.edit_settings {
d.clear_background(BG_DARK);
match config.draw_edit(&mut d, &self.globals) {
match config.draw_edit(&mut d, &mut self.globals) {
config::MenuReturn::Stay => (),
config::MenuReturn::StaySave => {
self.globals.config = config.clone();