Skip to content

Commit

Permalink
Added the basics of mouse movement and some more key, mouse doesn't work
Browse files Browse the repository at this point in the history
currently because the DeviceEvent::Moved returns a AxisId which is not
"matchable" will wait for winit rust-windowing/winit#211
to happen
  • Loading branch information
Siebencorgie committed Aug 14, 2017
1 parent d04ba25 commit a054b03
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.1.0"
authors = ["Siebencorgie <siebencorgie@googlemail.com>"]

[dependencies]
winit = "0.7.1"
winit = "0.7.5"
vulkano = { path = "lib/vulkano/vulkano" }
vulkano-shader-derive = { path = "lib/vulkano/vulkano-shader-derive" }
vulkano-win = { path = "lib/vulkano/vulkano-win" }
Expand Down
2 changes: 2 additions & 0 deletions examples/simple_scene/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ ori-engine = { path = "../../" }
vulkano = "0.5.6"
vulkano-shader-derive = "0.5.6"
vulkano-win = "0.5.6"

winit = "0.7.5"
10 changes: 7 additions & 3 deletions examples/simple_scene/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@ use std::sync::{Arc, Mutex};
use std::thread;
use std::time::{Instant, Duration};

extern crate winit;

fn main() {

//Start

//Settings
let settings = Arc::new(Mutex::new(core::engine_settings::EngineSettings::new()
.with_dimensions(800, 600)
.with_dimensions(1200, 720)
.with_name("Teddy the bear")
.set_vulkan_silent()
.with_fullscreen_mode(true)
.with_fullscreen_mode(false)
.with_cursor_state(winit::CursorState::Grab)
.with_cursor_visibility(winit::MouseCursor::Arrow)
));

//Input
Expand All @@ -35,7 +39,7 @@ fn main() {
input_handler.key_map.clone()
);

///Start the input thread
//Start the input thread
input_handler.start();

//Import the ape
Expand Down
6 changes: 3 additions & 3 deletions src/core/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,9 @@ impl Camera for DefaultCamera{

//Fixed camera gittering by slowing down so one integer delta = movement of
// delta * sensitvity * time_delta * slowdown (virtual speed up)
let virtual_speedup = 0.25;
let x_offset: f32 = 0.0; //input_handler.keys.Delta_x as f32 * sensitivity * delta_time * virtual_speedup;
let y_offset: f32 = 0.0; //input_handler.keys.Delta_y as f32 * sensitivity * delta_time * virtual_speedup;
let virtual_speedup = 1.0;
let x_offset: f32 = key_map_inst.mouse_delta_x as f32 * sensitivity * delta_time * virtual_speedup;
let y_offset: f32 = key_map_inst.mouse_delta_y as f32 * sensitivity * delta_time * virtual_speedup;

self.yaw += x_offset;
self.pitch += y_offset;
Expand Down
58 changes: 56 additions & 2 deletions src/input/input_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ impl InputHandler{
//println!("Resized to {} / {}", width, height );
},
Moved(width, height) =>{
//println!("STATUS: INPUT HANDLER: moved: {} / {}", width, height );
println!("STATUS: INPUT HANDLER: moved window: {} / {}", width, height );

},
Closed => {
Expand Down Expand Up @@ -207,6 +207,11 @@ impl InputHandler{
Some(VirtualKeyCode::Return) => current_keys.enter = true,
Some(VirtualKeyCode::NumpadEnter) => current_keys.nume_enter = true,
Some(VirtualKeyCode::Escape) => current_keys.escape = true,
//arrows
Some(VirtualKeyCode::Up) => current_keys.up = true,
Some(VirtualKeyCode::Down) => current_keys.down = true,
Some(VirtualKeyCode::Left) => current_keys.left = true,
Some(VirtualKeyCode::Right) => current_keys.right = true,
_ => {},
}
},
Expand Down Expand Up @@ -276,6 +281,11 @@ impl InputHandler{
Some(VirtualKeyCode::Return) => current_keys.enter = false,
Some(VirtualKeyCode::NumpadEnter) => current_keys.nume_enter = false,
Some(VirtualKeyCode::Escape) => current_keys.escape = false,
//arrows
Some(VirtualKeyCode::Up) => current_keys.up = false,
Some(VirtualKeyCode::Down) => current_keys.down = false,
Some(VirtualKeyCode::Left) => current_keys.left = false,
Some(VirtualKeyCode::Right) => current_keys.right = false,



Expand All @@ -287,6 +297,7 @@ impl InputHandler{

},
MouseMoved {device_id, position} =>{
println!("STATUS: MOUSE {:?} moved to: {} / {}", device_id, position.0, position.1);

},
MouseEntered{device_id} =>{
Expand Down Expand Up @@ -320,18 +331,61 @@ impl InputHandler{
},
//Device
winit::Event::DeviceEvent{device_id, event} => {
//Using raw events for the mouse movement
//One could, potentually use a 3rd axis for a 3d controller movement
//I think
match event{
winit::DeviceEvent::Motion{axis, value} => {

/* waiting for winit 0.7.6 to land
match axis {
0 => current_keys.mouse_delta_x = value,
1 => current_keys.mouse_delta_y = value,
}
*/

println!("Mouse Motion: {:?} of value {}", axis, value);
}
//This could register raw device events, however, not used atm
_ => {},
}

},
//Awake (not implemented)
winit::Event::Awakened => {},

}
});

//Have to fake mouse via errors till a fix lands in winit > 0.7.5
if current_keys.down{
current_keys.mouse_delta_y = -1.0;
println!("Down", );
}else{
current_keys.mouse_delta_y = 0.0;
}
if current_keys.up{
current_keys.mouse_delta_y = 1.0;
println!("Up", );

}else{
current_keys.mouse_delta_y = 0.0;
}
if current_keys.left{
current_keys.mouse_delta_x = -1.0;
println!("left", );

}else{
current_keys.mouse_delta_x = 0.0;
}
if current_keys.right{
current_keys.mouse_delta_x = 1.0;
println!("right", );

}else{
current_keys.mouse_delta_x = 0.0;
}
//FAKE END

//Overwrite the Arc<Mutex<KeyMap>> with the new capture
{
let mut key_map_unlck = key_map_inst
Expand Down
33 changes: 28 additions & 5 deletions src/input/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,26 @@ pub mod input_handler;
#[derive(Debug, Copy, Clone)]
pub struct KeyMap {

//WINDOW
///Window info (usually not needed recreation is handled by renderer)
pub window_dimensions: [u32; 2],

//GLOBAL
///Global States
pub closed: bool,

//MOUSE
//moving
///Represents the current location of the mouse
pub mouse_location: [i32; 2],
///represents the current active delta of mouse mouement, this can be used to implement mouse
///speed dependent movement like camera-rotation
pub mouse_delta_x: f64,
//same as `mouse_delta_x` for axis-y
pub mouse_delta_y: f64,


//KEYBOARD
//normal keys
pub a: bool,
pub b: bool,
Expand Down Expand Up @@ -88,6 +102,11 @@ pub struct KeyMap {
pub enter: bool,
pub nume_enter: bool,
pub escape: bool,
pub up: bool,
pub down: bool,
pub left: bool,
pub right: bool,

//todo addrest
/*
F1,
Expand All @@ -114,11 +133,6 @@ Delete,
End,
PageDown,
PageUp,
Left,
Up,
Right,
Down,
Back,
Compose,
AbntC1,
AbntC2,
Expand Down Expand Up @@ -193,6 +207,10 @@ impl KeyMap{
//state
closed: false,

mouse_location: [0; 2],
mouse_delta_x: 0.0,
mouse_delta_y: 0.0,

//normal keys
a: false,
b: false,
Expand Down Expand Up @@ -257,6 +275,11 @@ impl KeyMap{
enter: false,
nume_enter: false,
escape: false,
//arrows
up: false,
down: false,
left: false,
right: false,
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/render/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl Window{

//Set the cursor state (can only be done on a already created window)
window.window().set_cursor(engine_settings_lck.cursor_visible_state);
window.window().set_cursor_state(engine_settings_lck.cursor_state);
window.window().set_cursor_state(engine_settings_lck.cursor_state).ok().expect("could not set cursor");

Window{
window: window,
Expand Down

0 comments on commit a054b03

Please sign in to comment.