Functional input binding
This commit is contained in:
+10
-1
@@ -235,14 +235,23 @@ impl<'a> Drop for ContextLock<'a> {
|
||||
|
||||
pub trait UiExt {
|
||||
fn fullscreen_window(&self) -> Option<WindowToken<'_>>;
|
||||
fn right_align_text<T: AsRef<str>>(&self, text: T, space: f32);
|
||||
}
|
||||
|
||||
impl UiExt for imgui::Ui {
|
||||
fn fullscreen_window(&self) -> Option<WindowToken<'_>> {
|
||||
self.window("fullscreen")
|
||||
.position([0.0, 0.0], imgui::Condition::Always)
|
||||
.size(self.window_size(), imgui::Condition::Always)
|
||||
.size(self.io().display_size, imgui::Condition::Always)
|
||||
.flags(imgui::WindowFlags::NO_DECORATION)
|
||||
.begin()
|
||||
}
|
||||
|
||||
fn right_align_text<T: AsRef<str>>(&self, text: T, space: f32) {
|
||||
let width = self.calc_text_size(text.as_ref())[0];
|
||||
let [left, y] = self.cursor_pos();
|
||||
let right = left + space;
|
||||
self.set_cursor_pos([right - width, y]);
|
||||
self.text(text);
|
||||
}
|
||||
}
|
||||
|
||||
+29
-17
@@ -1,15 +1,18 @@
|
||||
use std::{sync::Arc, time::Instant};
|
||||
use std::{
|
||||
sync::{Arc, RwLock},
|
||||
time::Instant,
|
||||
};
|
||||
use wgpu::util::DeviceExt as _;
|
||||
use winit::{
|
||||
dpi::LogicalSize,
|
||||
event::{Event, KeyEvent, WindowEvent},
|
||||
event::{Event, WindowEvent},
|
||||
event_loop::{ActiveEventLoop, EventLoopProxy},
|
||||
window::WindowId,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
controller::ControllerState,
|
||||
emulator::{EmulatorClient, EmulatorCommand},
|
||||
input::InputMapper,
|
||||
renderer::GameRenderer,
|
||||
};
|
||||
|
||||
@@ -25,7 +28,7 @@ pub struct GameWindow {
|
||||
pipeline: wgpu::RenderPipeline,
|
||||
bind_group: wgpu::BindGroup,
|
||||
client: EmulatorClient,
|
||||
controller: ControllerState,
|
||||
input_mapper: Arc<RwLock<InputMapper>>,
|
||||
proxy: EventLoopProxy<UserEvent>,
|
||||
}
|
||||
|
||||
@@ -33,6 +36,7 @@ impl GameWindow {
|
||||
pub fn new(
|
||||
event_loop: &ActiveEventLoop,
|
||||
client: EmulatorClient,
|
||||
input_mapper: Arc<RwLock<InputMapper>>,
|
||||
proxy: EventLoopProxy<UserEvent>,
|
||||
) -> Self {
|
||||
let window = WindowStateBuilder::new(event_loop)
|
||||
@@ -153,15 +157,13 @@ impl GameWindow {
|
||||
cache: None,
|
||||
});
|
||||
|
||||
let controller = ControllerState::new();
|
||||
client.send_command(EmulatorCommand::SetKeys(controller.pressed()));
|
||||
Self {
|
||||
window,
|
||||
imgui: None,
|
||||
pipeline: render_pipeline,
|
||||
bind_group,
|
||||
client,
|
||||
controller,
|
||||
input_mapper,
|
||||
proxy,
|
||||
}
|
||||
}
|
||||
@@ -170,6 +172,7 @@ impl GameWindow {
|
||||
let window = &mut self.window;
|
||||
let imgui = self.imgui.as_mut().unwrap();
|
||||
let mut context = imgui.context.lock().unwrap();
|
||||
let mut new_size = None;
|
||||
|
||||
let now = Instant::now();
|
||||
context.io_mut().update_delta_time(now - imgui.last_frame);
|
||||
@@ -226,13 +229,18 @@ impl GameWindow {
|
||||
if ui.menu_item_config(label).selected(selected).build() {
|
||||
if let Some(size) = window.window.request_inner_size(dims) {
|
||||
window.handle_resize(&size);
|
||||
new_size = Some(size);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
ui.menu("Input", || {
|
||||
if ui.menu_item("Map Input") {
|
||||
let input_window = Box::new(InputWindow::new(event_loop, self.proxy.clone()));
|
||||
if ui.menu_item("Bind Inputs") {
|
||||
let input_window = Box::new(InputWindow::new(
|
||||
event_loop,
|
||||
self.input_mapper.clone(),
|
||||
self.proxy.clone(),
|
||||
));
|
||||
self.proxy
|
||||
.send_event(UserEvent::OpenWindow(input_window))
|
||||
.unwrap();
|
||||
@@ -287,17 +295,21 @@ impl GameWindow {
|
||||
|
||||
drop(rpass);
|
||||
|
||||
if let Some(size) = new_size {
|
||||
imgui.platform.handle_event::<UserEvent>(
|
||||
context.io_mut(),
|
||||
&window.window,
|
||||
&Event::WindowEvent {
|
||||
window_id: window.window.id(),
|
||||
event: WindowEvent::Resized(size),
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
window.queue.submit(Some(encoder.finish()));
|
||||
|
||||
frame.present();
|
||||
}
|
||||
|
||||
fn handle_key_event(&mut self, event: &KeyEvent) {
|
||||
if self.controller.key_event(event) {
|
||||
self.client
|
||||
.send_command(EmulatorCommand::SetKeys(self.controller.pressed()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl AppWindow for GameWindow {
|
||||
@@ -307,6 +319,7 @@ impl AppWindow for GameWindow {
|
||||
|
||||
fn init(&mut self) {
|
||||
self.imgui = Some(ImguiState::new(&self.window));
|
||||
self.window.window.request_redraw();
|
||||
}
|
||||
|
||||
fn handle_event(&mut self, event_loop: &ActiveEventLoop, event: &Event<UserEvent>) {
|
||||
@@ -314,7 +327,6 @@ impl AppWindow for GameWindow {
|
||||
Event::WindowEvent { event, .. } => match event {
|
||||
WindowEvent::Resized(size) => self.window.handle_resize(size),
|
||||
WindowEvent::CloseRequested => event_loop.exit(),
|
||||
WindowEvent::KeyboardInput { event, .. } => self.handle_key_event(event),
|
||||
WindowEvent::RedrawRequested => self.draw(event_loop),
|
||||
_ => (),
|
||||
},
|
||||
|
||||
+92
-13
@@ -1,29 +1,62 @@
|
||||
use std::time::Instant;
|
||||
|
||||
use winit::{
|
||||
event::{Event, WindowEvent},
|
||||
event_loop::{ActiveEventLoop, EventLoopProxy},
|
||||
use std::{
|
||||
sync::{Arc, RwLock},
|
||||
time::Instant,
|
||||
};
|
||||
|
||||
use winit::{
|
||||
dpi::LogicalSize,
|
||||
event::{Event, KeyEvent, WindowEvent},
|
||||
event_loop::{ActiveEventLoop, EventLoopProxy},
|
||||
platform::modifier_supplement::KeyEventExtModifierSupplement,
|
||||
};
|
||||
|
||||
use crate::{input::InputMapper, shrooms_vb_core::VBKey};
|
||||
|
||||
use super::{
|
||||
common::{ImguiState, UiExt as _, WindowState, WindowStateBuilder},
|
||||
common::{ImguiState, UiExt, WindowState, WindowStateBuilder},
|
||||
AppWindow, UserEvent,
|
||||
};
|
||||
|
||||
pub struct InputWindow {
|
||||
window: WindowState,
|
||||
imgui: Option<ImguiState>,
|
||||
input_mapper: Arc<RwLock<InputMapper>>,
|
||||
proxy: EventLoopProxy<UserEvent>,
|
||||
now_binding: Option<VBKey>,
|
||||
}
|
||||
|
||||
const KEY_NAMES: [(VBKey, &str); 14] = [
|
||||
(VBKey::LU, "Up"),
|
||||
(VBKey::LD, "Down"),
|
||||
(VBKey::LL, "Left"),
|
||||
(VBKey::LR, "Right"),
|
||||
(VBKey::SEL, "Select"),
|
||||
(VBKey::STA, "Start"),
|
||||
(VBKey::B, "B"),
|
||||
(VBKey::A, "A"),
|
||||
(VBKey::LT, "L-Trigger"),
|
||||
(VBKey::RT, "R-Trigger"),
|
||||
(VBKey::RU, "R-Up"),
|
||||
(VBKey::RD, "R-Down"),
|
||||
(VBKey::RL, "R-Left"),
|
||||
(VBKey::RR, "R-Right"),
|
||||
];
|
||||
|
||||
impl InputWindow {
|
||||
pub fn new(event_loop: &ActiveEventLoop, proxy: EventLoopProxy<UserEvent>) -> Self {
|
||||
pub fn new(
|
||||
event_loop: &ActiveEventLoop,
|
||||
input_mapper: Arc<RwLock<InputMapper>>,
|
||||
proxy: EventLoopProxy<UserEvent>,
|
||||
) -> Self {
|
||||
let window = WindowStateBuilder::new(event_loop)
|
||||
.with_title("Map Inputs")
|
||||
.with_title("Bind Inputs")
|
||||
.with_inner_size(LogicalSize::new(600, 400))
|
||||
.build();
|
||||
Self {
|
||||
window,
|
||||
imgui: None,
|
||||
input_mapper,
|
||||
now_binding: None,
|
||||
proxy,
|
||||
}
|
||||
}
|
||||
@@ -50,17 +83,50 @@ impl InputWindow {
|
||||
.expect("Failed to prepare frame");
|
||||
let ui = context.new_frame();
|
||||
|
||||
if let Some(window) = ui.fullscreen_window() {
|
||||
let mut render_key_bindings = || {
|
||||
if let Some(table) = ui.begin_table("controls", 2) {
|
||||
let binding_names = {
|
||||
let mapper = self.input_mapper.read().unwrap();
|
||||
mapper.binding_names()
|
||||
};
|
||||
ui.table_next_row();
|
||||
|
||||
ui.table_next_column();
|
||||
ui.text("Key");
|
||||
for (key, name) in KEY_NAMES {
|
||||
let binding = binding_names.get(&key).map(|s| s.as_str());
|
||||
ui.table_next_column();
|
||||
let [space, _] = ui.content_region_avail();
|
||||
ui.group(|| {
|
||||
ui.right_align_text(name, space * 0.20);
|
||||
ui.same_line();
|
||||
let label_text = if self.now_binding == Some(key) {
|
||||
"Press any input"
|
||||
} else {
|
||||
binding.unwrap_or("")
|
||||
};
|
||||
let label = format!("{}##{}", label_text, name);
|
||||
if ui.button_with_size(label, [space * 0.60, 0.0]) {
|
||||
self.now_binding = Some(key);
|
||||
}
|
||||
});
|
||||
ui.same_line();
|
||||
if ui.button(format!("Clear##{name}")) {
|
||||
let mut mapper = self.input_mapper.write().unwrap();
|
||||
mapper.clear_binding(key);
|
||||
}
|
||||
}
|
||||
|
||||
ui.table_next_column();
|
||||
ui.text("Value");
|
||||
table.end();
|
||||
}
|
||||
};
|
||||
|
||||
if let Some(window) = ui.fullscreen_window() {
|
||||
if let Some(tabs) = ui.tab_bar("tabs") {
|
||||
if let Some(tab) = ui.tab_item("Player 1") {
|
||||
render_key_bindings();
|
||||
tab.end();
|
||||
}
|
||||
tabs.end();
|
||||
}
|
||||
window.end();
|
||||
}
|
||||
let mut encoder: wgpu::CommandEncoder = window
|
||||
@@ -102,6 +168,17 @@ impl InputWindow {
|
||||
|
||||
frame.present();
|
||||
}
|
||||
|
||||
fn try_bind_key(&mut self, event: &KeyEvent) {
|
||||
if !event.state.is_pressed() {
|
||||
return;
|
||||
}
|
||||
let Some(vb) = self.now_binding.take() else {
|
||||
return;
|
||||
};
|
||||
let mut mapper = self.input_mapper.write().unwrap();
|
||||
mapper.bind_key(vb, event.key_without_modifiers());
|
||||
}
|
||||
}
|
||||
|
||||
impl AppWindow for InputWindow {
|
||||
@@ -111,6 +188,7 @@ impl AppWindow for InputWindow {
|
||||
|
||||
fn init(&mut self) {
|
||||
self.imgui = Some(ImguiState::new(&self.window));
|
||||
self.window.window.request_redraw();
|
||||
}
|
||||
|
||||
fn handle_event(&mut self, _: &ActiveEventLoop, event: &Event<UserEvent>) {
|
||||
@@ -121,6 +199,7 @@ impl AppWindow for InputWindow {
|
||||
.proxy
|
||||
.send_event(UserEvent::CloseWindow(self.id()))
|
||||
.unwrap(),
|
||||
WindowEvent::KeyboardInput { event, .. } => self.try_bind_key(event),
|
||||
WindowEvent::RedrawRequested => self.draw(),
|
||||
_ => (),
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user