engine/input: add mouse deltas

Movement is janky, though.
master
q3k 2021-04-04 17:48:13 +00:00
parent e0dc8444ca
commit 630073f916
3 changed files with 66 additions and 18 deletions

View File

@ -39,6 +39,10 @@ pub struct MouseCursor {
pub x: f32,
pub y: f32,
// delta x and y of mouse movement per frame.
pub dx: f32,
pub dy: f32,
pressed: BTreeSet<MouseButton>,
}
@ -46,6 +50,7 @@ impl MouseCursor {
pub fn new() -> Self {
Self {
x: 0., y: 0.,
dx: 0., dy: 0.,
pressed: BTreeSet::new(),
}
}

View File

@ -51,6 +51,9 @@ impl Time {
struct Main {
light1: ecs::EntityID,
light2: ecs::EntityID,
cx: f32,
cy: f32,
}
impl Main {
@ -150,7 +153,9 @@ impl Main {
.build();
Self {
light1, light2
light1, light2,
cx: 0.,
cy: 0.,
}
}
}
@ -166,15 +171,19 @@ struct MainData<'a> {
impl<'a> ecs::System <'a> for Main {
type SystemData = MainData<'a>;
fn run(&mut self, sd: Self::SystemData) {
let position: f32 = match sd.input.get().mouse_cursor() {
Some(cursor) => cursor.x * 3.14 * 2.0,
_ => (sd.time.get().instant() / 10.0) * 3.14 * 2.0,
let ts: f32 = (sd.time.get().instant() / 10.0) * 3.14 * 2.0;
let (dx, dy) = match sd.input.get().mouse_cursor() {
Some(cursor) => (cursor.dx, cursor.dy),
_ => (0.0, 0.0),
};
self.cx += (dx);
self.cy += (dy);
let camera = cgm::Point3::new(
7.0 + (position / 4.0).sin(),
12.0 + (position / 4.0).cos(),
3.0
self.cx.sin() * 10.0,
(self.cx.cos()*self.cy.cos()) * 10.0,
self.cy.sin() * 10.0,
);
let view = cgm::Matrix4::look_at(
@ -188,15 +197,15 @@ impl<'a> ecs::System <'a> for Main {
sd.scene_info.get().lock_cursor = true;
*sd.transforms.get_mut(self.light1).unwrap() = Transform::at(
-0.0 + (position*3.0).sin() * 4.0,
-0.0 + (position*4.0).cos() * 4.0,
-0.0 + (position*2.0).sin() * 3.0,
-0.0 + (ts*3.0).sin() * 4.0,
-0.0 + (ts*4.0).cos() * 4.0,
-0.0 + (ts*2.0).sin() * 3.0,
);
*sd.transforms.get_mut(self.light2).unwrap() = Transform::at(
-0.0 + (position*3.0).cos() * 4.0,
-0.0 + (position*4.0).sin() * 4.0,
-0.0 + (position*2.0).cos() * 3.0,
-0.0 + (ts*3.0).cos() * 4.0,
-0.0 + (ts*4.0).sin() * 4.0,
-0.0 + (ts*2.0).cos() * 3.0,
);
}

View File

@ -139,19 +139,41 @@ impl<'a> ecs::System<'a> for Renderer {
} else {
let mut device = input.devices.entry(status.input_device_id).or_insert(input::Device::MouseCursor(input::MouseCursor::new()));
if let &mut input::Device::MouseCursor(cursor) = &mut device {
let mut per_axis: BTreeMap<u32, Vec<f32>> = BTreeMap::new();
let (rx, ry) = (status.resolution[0], status.resolution[1]);
for event in events {
match event {
InternalEvent::MousePressed(button) => cursor.set_mouse_pressed(button),
InternalEvent::MouseReleased(button) => cursor.set_mouse_released(button),
InternalEvent::MouseMoved(x, y) => {
let (rx, ry) = (status.resolution[0], status.resolution[1]);
if rx != 0 && ry != 0 {
cursor.x = (x as f32) / (rx as f32);
cursor.y = (y as f32) / (ry as f32);
}
},
InternalEvent::AxisMotion(axis, delta) => {
per_axis.entry(axis).or_insert(vec![]).push(delta as f32);
},
}
}
// Has there been movement in any axis 0 (x) or 1 (y)? This happens if we receive
// multiple AxisMotion events for a given axis in a single frame.
let mut dx = 0f32;
let mut dy = 0f32;
if let Some(ldx) = per_axis.get(&0) {
dx = ldx.last().unwrap() - ldx.first().unwrap();
}
if let Some(ldy) = per_axis.get(&1) {
dy = ldy.last().unwrap() - ldy.first().unwrap();
}
if rx != 0 && ry != 0 {
cursor.dx = dx / (rx as f32);
cursor.dy = dy / (ry as f32);
}
}
}
@ -181,6 +203,7 @@ enum InternalEvent {
MousePressed(input::MouseButton),
MouseReleased(input::MouseButton),
MouseMoved(f64, f64),
AxisMotion(u32, f64),
}
impl Renderer {
@ -226,24 +249,29 @@ impl Renderer {
let mut events = vec![];
// TODO(q3k): migrate to EventLoop::run
self.events_loop.run_return(|ev, _, control_flow| {
*control_flow = winit::event_loop::ControlFlow::Poll;
match ev {
Event::WindowEvent {
event: WindowEvent::CloseRequested,
..
} => {
close = true;
*control_flow = winit::event_loop::ControlFlow::Exit;
},
Event::MainEventsCleared => {
*control_flow = winit::event_loop::ControlFlow::Exit;
},
Event::WindowEvent {
event: WindowEvent::CursorMoved { position, .. },
event: WindowEvent::CursorMoved { position, .. },
..
} => {
events.push(InternalEvent::MouseMoved(position.x, position.y));
},
Event::WindowEvent {
event: WindowEvent::MouseInput { state, button, .. },
event: WindowEvent::MouseInput { state, button, .. },
..
} => {
let button = match button {
@ -261,9 +289,15 @@ impl Renderer {
},
}
},
_ => {
*control_flow = winit::event_loop::ControlFlow::Poll;
Event::WindowEvent {
event: WindowEvent::AxisMotion { axis, value, .. },
..
} => {
events.push(InternalEvent::AxisMotion(axis, value));
},
_ => {},
}
});
return (close, events);