mirror of
https://gitlab.com/veloren/veloren.git
synced 2024-08-30 18:12:32 +00:00
Cleaned up codebase, added cursor grab toggle
This commit is contained in:
parent
248577bdef
commit
8a37662cf0
@ -13,14 +13,6 @@ use crate::{
|
||||
// Local
|
||||
use self::cell::Cell;
|
||||
|
||||
/// A type representing a single figure bone (e.g: the limb of a character).
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct Bone {
|
||||
origin: Vec3<f32>,
|
||||
offset: Vec3<f32>,
|
||||
ori: Vec3<f32>,
|
||||
}
|
||||
|
||||
/// A type representing a volume that may be part of an animated figure.
|
||||
///
|
||||
/// Figures are used to represent things like characters, NPCs, mobs, etc.
|
||||
|
@ -1 +1 @@
|
||||
pub mod worker_pool;
|
||||
|
||||
|
@ -1,10 +0,0 @@
|
||||
// Library
|
||||
use threadpool::ThreadPool;
|
||||
|
||||
pub struct WorkerPool {
|
||||
thread_pool: ThreadPool,
|
||||
}
|
||||
|
||||
impl WorkerPool {
|
||||
|
||||
}
|
@ -14,6 +14,7 @@ use crate::vol::{
|
||||
VolSize,
|
||||
};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum ChunkErr {
|
||||
OutOfBounds,
|
||||
}
|
||||
|
@ -10,6 +10,7 @@ use crate::vol::{
|
||||
WriteVol,
|
||||
};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum DynaErr {
|
||||
OutOfBounds,
|
||||
}
|
||||
|
@ -21,6 +21,7 @@ use crate::{
|
||||
},
|
||||
};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum VolMapErr {
|
||||
NoSuchChunk,
|
||||
ChunkErr(ChunkErr),
|
||||
|
@ -34,7 +34,7 @@ impl GlobalState {
|
||||
/// Called after a change in play state has occured (usually used to reverse any temporary
|
||||
/// effects a state may have made).
|
||||
pub fn on_play_state_changed(&mut self) {
|
||||
self.window.untrap_cursor();
|
||||
self.window.grab_cursor(false);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,9 +1,6 @@
|
||||
pub mod segment;
|
||||
pub mod terrain;
|
||||
|
||||
// Library
|
||||
use vek::*;
|
||||
|
||||
// Crate
|
||||
use crate::render::{
|
||||
self,
|
||||
|
@ -9,7 +9,7 @@ use common::{
|
||||
ReadVol,
|
||||
},
|
||||
volumes::dyna::Dyna,
|
||||
terrain::{Block, TerrainChunk},
|
||||
terrain::Block,
|
||||
};
|
||||
|
||||
// Crate
|
||||
|
@ -12,10 +12,7 @@ use vek::*;
|
||||
use client::Client;
|
||||
use common::{
|
||||
terrain::TerrainMap,
|
||||
volumes::vol_map::{
|
||||
VolMap,
|
||||
VolMapErr,
|
||||
},
|
||||
volumes::vol_map::VolMapErr,
|
||||
vol::SampleVol,
|
||||
};
|
||||
|
||||
@ -179,7 +176,7 @@ impl Terrain {
|
||||
}
|
||||
|
||||
pub fn render(&self, renderer: &mut Renderer, globals: &Consts<Globals>) {
|
||||
for (pos, chunk) in &self.chunks {
|
||||
for (_, chunk) in &self.chunks {
|
||||
renderer.render_terrain_chunk(
|
||||
&chunk.model,
|
||||
globals,
|
||||
|
@ -17,7 +17,7 @@ use crate::{
|
||||
PlayState,
|
||||
PlayStateResult,
|
||||
GlobalState,
|
||||
window::Event,
|
||||
window::{Event, Key},
|
||||
render::Renderer,
|
||||
scene::Scene,
|
||||
};
|
||||
@ -75,14 +75,14 @@ impl SessionState {
|
||||
impl PlayState for SessionState {
|
||||
fn play(&mut self, global_state: &mut GlobalState) -> PlayStateResult {
|
||||
// Trap the cursor
|
||||
global_state.window.trap_cursor();
|
||||
global_state.window.grab_cursor(true);
|
||||
|
||||
// Set up an fps clock
|
||||
let mut clock = Clock::new();
|
||||
|
||||
// Load a few chunks TODO: Remove this
|
||||
for x in -2..3 {
|
||||
for y in -2..3 {
|
||||
for x in -4..5 {
|
||||
for y in -4..5 {
|
||||
for z in -1..2 {
|
||||
self.client.load_chunk(Vec3::new(x, y, z));
|
||||
}
|
||||
@ -97,8 +97,12 @@ impl PlayState for SessionState {
|
||||
Event::Close => return PlayStateResult::Shutdown,
|
||||
// When 'q' is pressed, exit the session
|
||||
Event::Char('q') => return PlayStateResult::Pop,
|
||||
// Toggle cursor grabbing
|
||||
Event::KeyDown(Key::ToggleCursor) => {
|
||||
global_state.window.grab_cursor(!global_state.window.is_cursor_grabbed());
|
||||
},
|
||||
// Pass all other events to the scene
|
||||
event => self.scene.handle_input_event(event),
|
||||
event => { self.scene.handle_input_event(event); },
|
||||
};
|
||||
// TODO: Do something if the event wasn't handled?
|
||||
}
|
||||
|
@ -15,8 +15,9 @@ use crate::{
|
||||
|
||||
pub struct Window {
|
||||
events_loop: glutin::EventsLoop,
|
||||
window: glutin::GlWindow,
|
||||
renderer: Renderer,
|
||||
window: glutin::GlWindow,
|
||||
cursor_grabbed: bool,
|
||||
}
|
||||
|
||||
|
||||
@ -47,13 +48,14 @@ impl Window {
|
||||
|
||||
let tmp = Ok(Self {
|
||||
events_loop,
|
||||
window,
|
||||
renderer: Renderer::new(
|
||||
device,
|
||||
factory,
|
||||
tgt_color_view,
|
||||
tgt_depth_view,
|
||||
)?,
|
||||
window,
|
||||
cursor_grabbed: false,
|
||||
});
|
||||
tmp
|
||||
}
|
||||
@ -62,15 +64,27 @@ impl Window {
|
||||
pub fn renderer_mut(&mut self) -> &mut Renderer { &mut self.renderer }
|
||||
|
||||
pub fn fetch_events(&mut self) -> Vec<Event> {
|
||||
// Copy data that is needed by the events closure to avoid lifetime errors
|
||||
// TODO: Remove this if/when the compiler permits it
|
||||
let cursor_grabbed = self.cursor_grabbed;
|
||||
|
||||
let mut events = vec![];
|
||||
self.events_loop.poll_events(|event| match event {
|
||||
glutin::Event::WindowEvent { event, .. } => match event {
|
||||
glutin::WindowEvent::CloseRequested => events.push(Event::Close),
|
||||
glutin::WindowEvent::ReceivedCharacter(c) => events.push(Event::Char(c)),
|
||||
glutin::WindowEvent::KeyboardInput { input, .. } => match input.virtual_keycode {
|
||||
Some(glutin::VirtualKeyCode::Escape) => events.push(if input.state == glutin::ElementState::Pressed {
|
||||
Event::KeyDown(Key::ToggleCursor)
|
||||
} else {
|
||||
Event::KeyUp(Key::ToggleCursor)
|
||||
}),
|
||||
_ => {},
|
||||
},
|
||||
_ => {},
|
||||
},
|
||||
glutin::Event::DeviceEvent { event, .. } => match event {
|
||||
glutin::DeviceEvent::MouseMotion { delta: (dx, dy), .. } =>
|
||||
glutin::DeviceEvent::MouseMotion { delta: (dx, dy), .. } if cursor_grabbed =>
|
||||
events.push(Event::CursorPan(Vec2::new(dx as f32, dy as f32))),
|
||||
_ => {},
|
||||
},
|
||||
@ -84,25 +98,33 @@ impl Window {
|
||||
.map_err(|err| Error::BackendError(Box::new(err)))
|
||||
}
|
||||
|
||||
pub fn trap_cursor(&mut self) {
|
||||
self.window.hide_cursor(true);
|
||||
self.window.grab_cursor(true)
|
||||
.expect("Failed to grab cursor");
|
||||
pub fn is_cursor_grabbed(&self) -> bool {
|
||||
self.cursor_grabbed
|
||||
}
|
||||
|
||||
pub fn untrap_cursor(&mut self) {
|
||||
self.window.hide_cursor(false);
|
||||
self.window.grab_cursor(false)
|
||||
.expect("Failed to ungrab cursor");
|
||||
pub fn grab_cursor(&mut self, grab: bool) {
|
||||
self.cursor_grabbed = grab;
|
||||
self.window.hide_cursor(grab);
|
||||
self.window.grab_cursor(grab)
|
||||
.expect("Failed to grab/ungrab cursor");
|
||||
}
|
||||
}
|
||||
|
||||
/// Represents a key that the game recognises after keyboard mapping
|
||||
pub enum Key {
|
||||
ToggleCursor,
|
||||
}
|
||||
|
||||
/// Represents an incoming event from the window
|
||||
pub enum Event {
|
||||
/// The window has been requested to close.
|
||||
Close,
|
||||
/// A key has been typed that corresponds to a specific character.
|
||||
Char(char),
|
||||
/// The cursor has been panned across the screen while trapped.
|
||||
/// The cursor has been panned across the screen while grabbed.
|
||||
CursorPan(Vec2<f32>),
|
||||
/// A key that the game recognises has been pressed down
|
||||
KeyDown(Key),
|
||||
/// A key that the game recognises has been released down
|
||||
KeyUp(Key),
|
||||
}
|
||||
|
@ -25,6 +25,8 @@ impl World {
|
||||
}
|
||||
|
||||
pub fn generate_chunk(&self, chunk_pos: Vec3<i32>) -> TerrainChunk {
|
||||
// TODO: This is all test code, remove/improve this later
|
||||
|
||||
let mut chunk = TerrainChunk::filled(Block::empty(), TerrainChunkMeta::void());
|
||||
|
||||
let air = Block::empty();
|
||||
@ -50,7 +52,7 @@ impl World {
|
||||
}
|
||||
} else {
|
||||
air
|
||||
});
|
||||
}).unwrap();
|
||||
}
|
||||
|
||||
chunk
|
||||
|
Loading…
Reference in New Issue
Block a user