improve inputs
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use pixels::{Pixels, SurfaceTexture};
|
||||
use prometeu_core::peripherals::InputSignals;
|
||||
use prometeu_core::Machine;
|
||||
use winit::{
|
||||
application::ApplicationHandler,
|
||||
@@ -23,15 +24,13 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
struct PrometeuApp {
|
||||
window: Option<&'static Window>,
|
||||
pixels: Option<Pixels<'static>>,
|
||||
|
||||
machine: Machine,
|
||||
|
||||
input_signals: InputSignals,
|
||||
|
||||
frame_dt: Duration,
|
||||
next_frame: Instant,
|
||||
|
||||
mouse_down: bool,
|
||||
mouse_just_pressed: bool,
|
||||
mouse_just_released: bool,
|
||||
mouse_pos_fb: (i32, i32),
|
||||
}
|
||||
|
||||
impl PrometeuApp {
|
||||
@@ -39,15 +38,11 @@ impl PrometeuApp {
|
||||
Self {
|
||||
window: None,
|
||||
pixels: None,
|
||||
input_signals: InputSignals::default(),
|
||||
machine: Machine::new(),
|
||||
|
||||
frame_dt: Duration::from_nanos(1_000_000_000 / 60),
|
||||
next_frame: Instant::now(),
|
||||
|
||||
mouse_down: false,
|
||||
mouse_just_pressed: false,
|
||||
mouse_just_released: false,
|
||||
mouse_pos_fb: (0, 0),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,39 +65,6 @@ impl PrometeuApp {
|
||||
w.request_redraw();
|
||||
}
|
||||
}
|
||||
|
||||
fn map_key(&mut self, code: KeyCode, is_down: bool) {
|
||||
match code {
|
||||
KeyCode::ArrowUp => self.machine.input.up = is_down,
|
||||
KeyCode::ArrowDown => self.machine.input.down = is_down,
|
||||
KeyCode::ArrowLeft => self.machine.input.left = is_down,
|
||||
KeyCode::ArrowRight => self.machine.input.right = is_down,
|
||||
|
||||
// A/B (troque depois como quiser)
|
||||
KeyCode::KeyZ => self.machine.input.a_down = is_down,
|
||||
KeyCode::KeyX => self.machine.input.b_down = is_down,
|
||||
|
||||
KeyCode::Enter => self.machine.input.start = is_down,
|
||||
KeyCode::ShiftLeft | KeyCode::ShiftRight => self.machine.input.select = is_down,
|
||||
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
fn update_touch_in_core(&mut self) {
|
||||
// no desktop, mouse emula touch
|
||||
self.machine.touch.present = true;
|
||||
self.machine.touch.x = self.mouse_pos_fb.0;
|
||||
self.machine.touch.y = self.mouse_pos_fb.1;
|
||||
|
||||
// pressed/released devem durar 1 frame (o core limpa em begin_frame)
|
||||
self.machine.touch.pressed = self.mouse_just_pressed;
|
||||
self.machine.touch.released = self.mouse_just_released;
|
||||
self.machine.touch.down = self.mouse_down;
|
||||
|
||||
self.mouse_just_pressed = false;
|
||||
self.mouse_just_released = false;
|
||||
}
|
||||
}
|
||||
|
||||
impl ApplicationHandler for PrometeuApp {
|
||||
@@ -169,28 +131,42 @@ impl ApplicationHandler for PrometeuApp {
|
||||
WindowEvent::KeyboardInput { event, .. } => {
|
||||
if let PhysicalKey::Code(code) = event.physical_key {
|
||||
let is_down = event.state == ElementState::Pressed;
|
||||
self.map_key(code, is_down);
|
||||
match code {
|
||||
KeyCode::ArrowUp => self.input_signals.up_signal = is_down,
|
||||
KeyCode::ArrowDown => self.input_signals.down_signal = is_down,
|
||||
KeyCode::ArrowLeft => self.input_signals.left_signal = is_down,
|
||||
KeyCode::ArrowRight => self.input_signals.right_signal = is_down,
|
||||
|
||||
// A/B (troque depois como quiser)
|
||||
KeyCode::KeyA => self.input_signals.a_signal = is_down,
|
||||
KeyCode::KeyD => self.input_signals.d_signal = is_down,
|
||||
KeyCode::KeyW => self.input_signals.w_signal = is_down,
|
||||
KeyCode::KeyS => self.input_signals.s_signal = is_down,
|
||||
KeyCode::KeyQ => self.input_signals.q_signal = is_down,
|
||||
KeyCode::KeyE => self.input_signals.e_signal = is_down,
|
||||
|
||||
KeyCode::KeyZ => self.input_signals.start_signal = is_down,
|
||||
KeyCode::ShiftLeft | KeyCode::ShiftRight => self.input_signals.select_signal = is_down,
|
||||
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
WindowEvent::CursorMoved { position, .. } => {
|
||||
self.mouse_pos_fb = window_to_fb(position.x as f32, position.y as f32, self.window());
|
||||
let v = window_to_fb(position.x as f32, position.y as f32, self.window());
|
||||
self.input_signals.x_pos = v.0;
|
||||
self.input_signals.y_pos = v.1;
|
||||
}
|
||||
|
||||
WindowEvent::MouseInput { state, button, .. } => {
|
||||
if button == MouseButton::Left {
|
||||
match state {
|
||||
ElementState::Pressed => {
|
||||
if !self.mouse_down {
|
||||
self.mouse_down = true;
|
||||
self.mouse_just_pressed = true;
|
||||
}
|
||||
self.input_signals.t_signal = true;
|
||||
}
|
||||
ElementState::Released => {
|
||||
if self.mouse_down {
|
||||
self.mouse_down = false;
|
||||
self.mouse_just_released = true;
|
||||
}
|
||||
self.input_signals.t_signal = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -207,11 +183,8 @@ impl ApplicationHandler for PrometeuApp {
|
||||
if now >= self.next_frame {
|
||||
self.next_frame += self.frame_dt;
|
||||
|
||||
// atualiza touch/input no core
|
||||
self.update_touch_in_core();
|
||||
|
||||
// executa 1 frame do PROMETEU
|
||||
self.machine.step_frame();
|
||||
self.machine.step_frame(&self.input_signals);
|
||||
|
||||
// pede redraw
|
||||
self.request_redraw();
|
||||
|
||||
Reference in New Issue
Block a user