2
0
mirror of https://github.com/AxioDL/metaforce.git synced 2025-06-06 22:33:28 +00:00

Start implementing input

This commit is contained in:
Phillip Stephens 2022-02-07 02:45:56 -08:00
parent 710b9ae6b3
commit 72502ec9a2
Signed by: Antidote
GPG Key ID: F8BEE4C83DACA60D
5 changed files with 196 additions and 66 deletions

View File

@ -6,6 +6,8 @@
#include <cinttypes> #include <cinttypes>
namespace aurora { namespace aurora {
enum class SpecialKey : std::uint8_t;
struct WindowSize; struct WindowSize;
struct App; struct App;
@ -25,6 +27,12 @@ struct AppDelegate {
virtual void onAppWindowMoved(std::int32_t x, std::int32_t y) noexcept = 0; virtual void onAppWindowMoved(std::int32_t x, std::int32_t y) noexcept = 0;
virtual void onAppExiting() noexcept = 0; virtual void onAppExiting() noexcept = 0;
// Input
virtual void onCharKeyDown(std::uint8_t charCode, bool is_repeat) noexcept = 0;
virtual void onCharKeyUp(std::uint8_t charCode) noexcept = 0;
virtual void onSpecialKeyDown(const SpecialKey& key, bool is_repeat) noexcept = 0;
virtual void onSpecialKeyUp(const SpecialKey& key) noexcept = 0;
// virtual void resized([[maybe_unused]] const WindowSize& rect, [[maybe_unused]] bool sync) noexcept {} // virtual void resized([[maybe_unused]] const WindowSize& rect, [[maybe_unused]] bool sync) noexcept {}
// virtual void mouseDown([[maybe_unused]] const SWindowCoord& coord, [[maybe_unused]] EMouseButton button, // virtual void mouseDown([[maybe_unused]] const SWindowCoord& coord, [[maybe_unused]] EMouseButton button,
// [[maybe_unused]] EModifierKey mods) noexcept {} // [[maybe_unused]] EModifierKey mods) noexcept {}

View File

@ -9,4 +9,9 @@ void App_onAppPostDraw(AppDelegate& cb) noexcept;
void App_onAppWindowResized(AppDelegate& cb, const WindowSize& size) noexcept; void App_onAppWindowResized(AppDelegate& cb, const WindowSize& size) noexcept;
void App_onAppWindowMoved(AppDelegate& cb, std::int32_t x, std::int32_t y) noexcept; void App_onAppWindowMoved(AppDelegate& cb, std::int32_t x, std::int32_t y) noexcept;
void App_onAppExiting(AppDelegate& cb) noexcept; void App_onAppExiting(AppDelegate& cb) noexcept;
// Input
void App_onCharKeyDown(AppDelegate& cb, std::uint8_t code, bool is_repeat) noexcept;
void App_onCharKeyUp(AppDelegate& cb, std::uint8_t code) noexcept;
void App_onSpecialKeyDown(AppDelegate& cb, const SpecialKey& key, bool is_repeat);
void App_onSpecialKeyUp(AppDelegate& cb, const SpecialKey& key);
} // namespace aurora } // namespace aurora

View File

@ -12,4 +12,18 @@ void App_onAppWindowMoved(AppDelegate& cb, std::int32_t x, std::int32_t y) noexc
cb.onAppWindowMoved(x, y); cb.onAppWindowMoved(x, y);
} }
void App_onAppExiting(AppDelegate& cb) noexcept { cb.onAppExiting(); } void App_onAppExiting(AppDelegate& cb) noexcept { cb.onAppExiting(); }
// Input
void App_onCharKeyDown(AppDelegate& cb, std::uint8_t code, bool is_repeat) noexcept {
cb.onCharKeyDown(code, is_repeat);
}
void App_onCharKeyUp(AppDelegate& cb, std::uint8_t code) noexcept {
cb.onCharKeyUp(code);
}
void App_onSpecialKeyDown(AppDelegate& cb, const SpecialKey& key, bool is_repeat) {
cb.onSpecialKeyDown(key, is_repeat);
}
void App_onSpecialKeyUp(AppDelegate& cb, const SpecialKey& key) {
cb.onSpecialKeyUp(key);
}
} // namespace aurora } // namespace aurora

View File

@ -3,21 +3,22 @@
#![allow(unused_variables)] #![allow(unused_variables)]
#![allow(unused_unsafe)] #![allow(unused_unsafe)]
use std::{num::NonZeroU8, time::Instant};
use wgpu::Backend; use wgpu::Backend;
use winit::{ use winit::{
event::{Event, WindowEvent, KeyboardInput}, event::{ElementState, Event, KeyboardInput, WindowEvent},
event_loop::ControlFlow, event_loop::ControlFlow,
}; };
use winit::event::VirtualKeyCode;
use std::{num::NonZeroU8, time::Instant, collections::HashMap};
use crate::{ use crate::{
gpu::{create_depth_texture, create_render_texture, initialize_gpu, DeviceHolder}, ffi::WindowSize,
imgui::{initialize_imgui, ImGuiState}, gpu::{create_depth_texture, create_render_texture, DeviceHolder, initialize_gpu},
imgui::{ImGuiState, initialize_imgui},
shaders::render_into_pass, shaders::render_into_pass,
ffi::{WindowSize},
}; };
use crate::ffi::SpecialKey;
mod gpu; mod gpu;
mod imgui; mod imgui;
@ -42,6 +43,11 @@ mod ffi {
pub(crate) fn App_onAppWindowResized(cb: Pin<&mut AppDelegate>, size: &WindowSize); pub(crate) fn App_onAppWindowResized(cb: Pin<&mut AppDelegate>, size: &WindowSize);
pub(crate) fn App_onAppWindowMoved(cb: Pin<&mut AppDelegate>, x: i32, y: i32); pub(crate) fn App_onAppWindowMoved(cb: Pin<&mut AppDelegate>, x: i32, y: i32);
pub(crate) fn App_onAppExiting(cb: Pin<&mut AppDelegate>); pub(crate) fn App_onAppExiting(cb: Pin<&mut AppDelegate>);
// Input
pub(crate) fn App_onCharKeyDown(cb: Pin<&mut AppDelegate>, code: u8, is_repeat: bool);
pub(crate) fn App_onCharKeyUp(cb: Pin<&mut AppDelegate>, code: u8);
pub(crate) fn App_onSpecialKeyDown(cb: Pin<&mut AppDelegate>, key: &SpecialKey, is_repeat: bool);
pub(crate) fn App_onSpecialKeyUp(cb: Pin<&mut AppDelegate>, key: &SpecialKey);
} }
pub struct Window { pub struct Window {
@ -148,13 +154,13 @@ fn app_run(mut delegate: cxx::UniquePtr<ffi::AppDelegate>) {
let window = winit::window::WindowBuilder::new().build(&event_loop).unwrap(); let window = winit::window::WindowBuilder::new().build(&event_loop).unwrap();
let gpu = initialize_gpu(&window); let gpu = initialize_gpu(&window);
let imgui = initialize_imgui(&window, &gpu); let imgui = initialize_imgui(&window, &gpu);
let mut special_keys_pressed : [bool; 27] = [false; 27];
shaders::construct_state(gpu.device.clone(), gpu.queue.clone(), &gpu.config); shaders::construct_state(gpu.device.clone(), gpu.queue.clone(), &gpu.config);
let app = App { window: ffi::Window { inner: Box::new(WindowContext { window }) }, gpu, imgui}; let app = App { window: ffi::Window { inner: Box::new(WindowContext { window }) }, gpu, imgui};
unsafe { unsafe {
APP.replace(app); APP.replace(app);
ffi::App_onAppLaunched(delegate.as_mut().unwrap()); ffi::App_onAppLaunched(delegate.as_mut().unwrap());
}; };
let mut last_frame: Option<Instant> = None; let mut last_frame: Option<Instant> = None;
event_loop.run(move |event, _, control_flow| { event_loop.run(move |event, _, control_flow| {
// Have the closure take ownership of the resources. // Have the closure take ownership of the resources.
@ -197,9 +203,62 @@ fn app_run(mut delegate: cxx::UniquePtr<ffi::AppDelegate>) {
ffi::App_onAppWindowMoved(delegate.as_mut().unwrap(), loc.x, loc.y); ffi::App_onAppWindowMoved(delegate.as_mut().unwrap(), loc.x, loc.y);
} }
} }
Event::WindowEvent { event: WindowEvent::KeyboardInput { Event::WindowEvent {
input: KeyboardInput, .. }, .. } => { event: WindowEvent::KeyboardInput {
input:
KeyboardInput {
scancode,
virtual_keycode: Some(key),
state,
..
},
..
},
..
} => {
// TODO: Handle normal keys, this will require a refactor in game runtime code
let special_key = match key {
VirtualKeyCode::F1 => SpecialKey::F1,
VirtualKeyCode::F2 => SpecialKey::F2,
VirtualKeyCode::F3 => SpecialKey::F3,
VirtualKeyCode::F4 => SpecialKey::F4,
VirtualKeyCode::F5 => SpecialKey::F5,
VirtualKeyCode::F6 => SpecialKey::F6,
VirtualKeyCode::F7 => SpecialKey::F7,
VirtualKeyCode::F8 => SpecialKey::F8,
VirtualKeyCode::F9 => SpecialKey::F9,
VirtualKeyCode::F10 => SpecialKey::F10,
VirtualKeyCode::F11 => SpecialKey::F11,
VirtualKeyCode::F12 => SpecialKey::F12,
VirtualKeyCode::Escape => SpecialKey::Esc,
VirtualKeyCode::Return => SpecialKey::Enter,
VirtualKeyCode::Back => SpecialKey::Backspace,
VirtualKeyCode::Insert => SpecialKey::Insert,
VirtualKeyCode::Delete => SpecialKey::Delete,
VirtualKeyCode::Home => SpecialKey::Home,
VirtualKeyCode::PageUp => SpecialKey::PgUp,
VirtualKeyCode::PageDown => SpecialKey::PgDown,
VirtualKeyCode::Left => SpecialKey::Left,
VirtualKeyCode::Right => SpecialKey::Right,
VirtualKeyCode::Up => SpecialKey::Up,
VirtualKeyCode::Down => SpecialKey::Down,
VirtualKeyCode::Tab => SpecialKey::Tab,
_ => SpecialKey::None,
};
if special_key != SpecialKey::None {
let pressed = state == ElementState::Pressed;
let repeat = special_keys_pressed[key as usize] == pressed;
special_keys_pressed[key as usize] = pressed;
unsafe {
if pressed {
ffi::App_onSpecialKeyDown(delegate.as_mut().unwrap(), &special_key, repeat);
} else {
ffi::App_onSpecialKeyUp(delegate.as_mut().unwrap(), &special_key);
}
}
}
} }
Event::WindowEvent { .. } => {} Event::WindowEvent { .. } => {}
Event::DeviceEvent { .. } => {} Event::DeviceEvent { .. } => {}

View File

@ -102,9 +102,7 @@ private:
} while (count.QuadPart < end); } while (count.QuadPart < end);
} }
#else #else
void NanoSleep(const duration_t duration) { void NanoSleep(const duration_t duration) { std::this_thread::sleep_for(duration); }
std::this_thread::sleep_for(duration);
}
#endif #endif
}; };
@ -453,6 +451,52 @@ public:
CDvdFile::Shutdown(); CDvdFile::Shutdown();
} }
void onCharKeyDown(uint8_t code, bool is_repeat) noexcept override {
Log.report(logvisor::Info, FMT_STRING("DEBUG CHAR KEYS: '{}', is_repeat {}"), static_cast<char>(code), is_repeat);
// if (!ImGuiWindowCallback::m_keyboardCaptured && g_mainMP1) {
if (g_mainMP1) {
if (MP1::CGameArchitectureSupport* as = g_mainMP1->GetArchSupport()) {
as->charKeyDown(code, boo::EModifierKey::None, is_repeat);
}
}
// }
}
void onCharKeyUp(uint8_t code) noexcept override {
Log.report(logvisor::Info, FMT_STRING("DEBUG CHAR KEYS: '{}'"), static_cast<char>(code));
if (g_mainMP1) {
if (MP1::CGameArchitectureSupport* as = g_mainMP1->GetArchSupport()) {
as->charKeyUp(code, boo::EModifierKey::None);
}
}
}
void onSpecialKeyDown(const aurora::SpecialKey& key, bool is_repeat) noexcept override {
Log.report(logvisor::Info, FMT_STRING("DEBUG KEYS: SpecialKey {}, is_repeat {}"), key, is_repeat);
/* TODO: Temporarily convert the aurora enum to boo's until we refactor everything */
if (g_mainMP1) {
if (MP1::CGameArchitectureSupport* as = g_mainMP1->GetArchSupport()) {
as->specialKeyDown(boo::ESpecialKey(key), boo::EModifierKey::None, is_repeat);
}
}
// if (True(mods & boo::EModifierKey::Alt)) {
// if (key == boo::ESpecialKey::Enter) {
// m_fullscreenToggleRequested = true;
// } else if (key == boo::ESpecialKey::F4) {
// m_windowInvalid = true;
// }
// }
}
void onSpecialKeyUp(const aurora::SpecialKey& key) noexcept override {
/* TODO: Temporarily convert the aurora enum to boo's until we refactor everything */
if (g_mainMP1) {
if (MP1::CGameArchitectureSupport* as = g_mainMP1->GetArchSupport()) {
as->specialKeyUp(boo::ESpecialKey(key), boo::EModifierKey::None);
}
}
}
[[nodiscard]] std::string getGraphicsApi() const { return m_cvarCommons.getGraphicsApi(); } [[nodiscard]] std::string getGraphicsApi() const { return m_cvarCommons.getGraphicsApi(); }
[[nodiscard]] uint32_t getSamples() const { return m_cvarCommons.getSamples(); } [[nodiscard]] uint32_t getSamples() const { return m_cvarCommons.getSamples(); }