list bindings in settings menu, allow deleting bindings

This commit is contained in:
Crispy 2025-03-30 21:27:21 +02:00
parent be699aa0ec
commit 14edee5a53
5 changed files with 106 additions and 44 deletions

View file

@ -1,11 +1,15 @@
use std::{collections::BTreeMap, mem::transmute};
use raylib::{
color::Color,
drawing::{RaylibDraw, RaylibDrawHandle},
ffi::{KeyboardKey, MouseButton},
RaylibHandle,
};
use serde::{Deserialize, Serialize};
use crate::{ui::text_button, Globals};
#[derive(Debug, Clone, Copy, Deserialize, Serialize, PartialEq, Eq, PartialOrd, Ord)]
#[repr(u8)]
pub enum ActionId {
@ -33,7 +37,11 @@ impl Default for Input {
};
bind_key(ActionId::Undo, vec![KEY_LEFT_CONTROL], KEY_Z);
bind_key(ActionId::Redo, vec![KEY_LEFT_CONTROL], KEY_Y);
bind_key(ActionId::Redo, vec![KEY_LEFT_CONTROL, KEY_LEFT_SHIFT], KEY_Z);
bind_key(
ActionId::Redo,
vec![KEY_LEFT_CONTROL, KEY_LEFT_SHIFT],
KEY_Z,
);
bind_key(ActionId::Copy, vec![KEY_LEFT_CONTROL], KEY_C);
bind_key(ActionId::Paste, vec![KEY_LEFT_CONTROL], KEY_V);
bind_key(ActionId::ToggleMenu, vec![], KEY_ESCAPE);
@ -67,6 +75,30 @@ pub struct Input {
}
impl Input {
pub fn draw_edit(&mut self, d: &mut RaylibDrawHandle, globals: &Globals) {
let mut y = 96;
for action_index in 0..ActionId::SIZE {
let action = ActionId::from_usize(action_index).unwrap();
d.draw_text(&format!("{action:?}"), 16, y, 20, Color::ORANGE);
if self.bindings[action_index].is_empty() {
y += 32;
}
for (binding_index, binding) in self.bindings[action_index].iter().enumerate() {
if text_button(d, &globals.mouse, 160, y, 80, "remove") {
self.bindings[action_index].remove(binding_index);
return;
}
let trigger = format!("{:?}", binding.trigger);
d.draw_text(&trigger, 256, y, 20, Color::LIMEGREEN);
let x = 264 + d.measure_text(&trigger, 20);
let modifiers = format!("{:?}", binding.modifiers);
d.draw_text(&modifiers, x, y, 20, Color::LIGHTBLUE);
y += 32;
}
}
}
pub fn update(&mut self, rl: &RaylibHandle) {
for i in 0..ActionId::SIZE {
let bindings = &self.bindings[i];