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
|
// Local
|
||||||
use self::cell::Cell;
|
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.
|
/// A type representing a volume that may be part of an animated figure.
|
||||||
///
|
///
|
||||||
/// Figures are used to represent things like characters, NPCs, mobs, etc.
|
/// 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,
|
VolSize,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
pub enum ChunkErr {
|
pub enum ChunkErr {
|
||||||
OutOfBounds,
|
OutOfBounds,
|
||||||
}
|
}
|
||||||
|
@ -10,6 +10,7 @@ use crate::vol::{
|
|||||||
WriteVol,
|
WriteVol,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
pub enum DynaErr {
|
pub enum DynaErr {
|
||||||
OutOfBounds,
|
OutOfBounds,
|
||||||
}
|
}
|
||||||
|
@ -21,6 +21,7 @@ use crate::{
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
pub enum VolMapErr {
|
pub enum VolMapErr {
|
||||||
NoSuchChunk,
|
NoSuchChunk,
|
||||||
ChunkErr(ChunkErr),
|
ChunkErr(ChunkErr),
|
||||||
|
@ -34,7 +34,7 @@ impl GlobalState {
|
|||||||
/// Called after a change in play state has occured (usually used to reverse any temporary
|
/// Called after a change in play state has occured (usually used to reverse any temporary
|
||||||
/// effects a state may have made).
|
/// effects a state may have made).
|
||||||
pub fn on_play_state_changed(&mut self) {
|
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 segment;
|
||||||
pub mod terrain;
|
pub mod terrain;
|
||||||
|
|
||||||
// Library
|
|
||||||
use vek::*;
|
|
||||||
|
|
||||||
// Crate
|
// Crate
|
||||||
use crate::render::{
|
use crate::render::{
|
||||||
self,
|
self,
|
||||||
|
@ -9,7 +9,7 @@ use common::{
|
|||||||
ReadVol,
|
ReadVol,
|
||||||
},
|
},
|
||||||
volumes::dyna::Dyna,
|
volumes::dyna::Dyna,
|
||||||
terrain::{Block, TerrainChunk},
|
terrain::Block,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Crate
|
// Crate
|
||||||
|
@ -12,10 +12,7 @@ use vek::*;
|
|||||||
use client::Client;
|
use client::Client;
|
||||||
use common::{
|
use common::{
|
||||||
terrain::TerrainMap,
|
terrain::TerrainMap,
|
||||||
volumes::vol_map::{
|
volumes::vol_map::VolMapErr,
|
||||||
VolMap,
|
|
||||||
VolMapErr,
|
|
||||||
},
|
|
||||||
vol::SampleVol,
|
vol::SampleVol,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -179,7 +176,7 @@ impl Terrain {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn render(&self, renderer: &mut Renderer, globals: &Consts<Globals>) {
|
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(
|
renderer.render_terrain_chunk(
|
||||||
&chunk.model,
|
&chunk.model,
|
||||||
globals,
|
globals,
|
||||||
|
@ -17,7 +17,7 @@ use crate::{
|
|||||||
PlayState,
|
PlayState,
|
||||||
PlayStateResult,
|
PlayStateResult,
|
||||||
GlobalState,
|
GlobalState,
|
||||||
window::Event,
|
window::{Event, Key},
|
||||||
render::Renderer,
|
render::Renderer,
|
||||||
scene::Scene,
|
scene::Scene,
|
||||||
};
|
};
|
||||||
@ -75,14 +75,14 @@ impl SessionState {
|
|||||||
impl PlayState for SessionState {
|
impl PlayState for SessionState {
|
||||||
fn play(&mut self, global_state: &mut GlobalState) -> PlayStateResult {
|
fn play(&mut self, global_state: &mut GlobalState) -> PlayStateResult {
|
||||||
// Trap the cursor
|
// Trap the cursor
|
||||||
global_state.window.trap_cursor();
|
global_state.window.grab_cursor(true);
|
||||||
|
|
||||||
// Set up an fps clock
|
// Set up an fps clock
|
||||||
let mut clock = Clock::new();
|
let mut clock = Clock::new();
|
||||||
|
|
||||||
// Load a few chunks TODO: Remove this
|
// Load a few chunks TODO: Remove this
|
||||||
for x in -2..3 {
|
for x in -4..5 {
|
||||||
for y in -2..3 {
|
for y in -4..5 {
|
||||||
for z in -1..2 {
|
for z in -1..2 {
|
||||||
self.client.load_chunk(Vec3::new(x, y, z));
|
self.client.load_chunk(Vec3::new(x, y, z));
|
||||||
}
|
}
|
||||||
@ -97,8 +97,12 @@ impl PlayState for SessionState {
|
|||||||
Event::Close => return PlayStateResult::Shutdown,
|
Event::Close => return PlayStateResult::Shutdown,
|
||||||
// When 'q' is pressed, exit the session
|
// When 'q' is pressed, exit the session
|
||||||
Event::Char('q') => return PlayStateResult::Pop,
|
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
|
// 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?
|
// TODO: Do something if the event wasn't handled?
|
||||||
}
|
}
|
||||||
|
@ -15,8 +15,9 @@ use crate::{
|
|||||||
|
|
||||||
pub struct Window {
|
pub struct Window {
|
||||||
events_loop: glutin::EventsLoop,
|
events_loop: glutin::EventsLoop,
|
||||||
window: glutin::GlWindow,
|
|
||||||
renderer: Renderer,
|
renderer: Renderer,
|
||||||
|
window: glutin::GlWindow,
|
||||||
|
cursor_grabbed: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -47,13 +48,14 @@ impl Window {
|
|||||||
|
|
||||||
let tmp = Ok(Self {
|
let tmp = Ok(Self {
|
||||||
events_loop,
|
events_loop,
|
||||||
window,
|
|
||||||
renderer: Renderer::new(
|
renderer: Renderer::new(
|
||||||
device,
|
device,
|
||||||
factory,
|
factory,
|
||||||
tgt_color_view,
|
tgt_color_view,
|
||||||
tgt_depth_view,
|
tgt_depth_view,
|
||||||
)?,
|
)?,
|
||||||
|
window,
|
||||||
|
cursor_grabbed: false,
|
||||||
});
|
});
|
||||||
tmp
|
tmp
|
||||||
}
|
}
|
||||||
@ -62,15 +64,27 @@ impl Window {
|
|||||||
pub fn renderer_mut(&mut self) -> &mut Renderer { &mut self.renderer }
|
pub fn renderer_mut(&mut self) -> &mut Renderer { &mut self.renderer }
|
||||||
|
|
||||||
pub fn fetch_events(&mut self) -> Vec<Event> {
|
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![];
|
let mut events = vec![];
|
||||||
self.events_loop.poll_events(|event| match event {
|
self.events_loop.poll_events(|event| match event {
|
||||||
glutin::Event::WindowEvent { event, .. } => match event {
|
glutin::Event::WindowEvent { event, .. } => match event {
|
||||||
glutin::WindowEvent::CloseRequested => events.push(Event::Close),
|
glutin::WindowEvent::CloseRequested => events.push(Event::Close),
|
||||||
glutin::WindowEvent::ReceivedCharacter(c) => events.push(Event::Char(c)),
|
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::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))),
|
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)))
|
.map_err(|err| Error::BackendError(Box::new(err)))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn trap_cursor(&mut self) {
|
pub fn is_cursor_grabbed(&self) -> bool {
|
||||||
self.window.hide_cursor(true);
|
self.cursor_grabbed
|
||||||
self.window.grab_cursor(true)
|
|
||||||
.expect("Failed to grab cursor");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn untrap_cursor(&mut self) {
|
pub fn grab_cursor(&mut self, grab: bool) {
|
||||||
self.window.hide_cursor(false);
|
self.cursor_grabbed = grab;
|
||||||
self.window.grab_cursor(false)
|
self.window.hide_cursor(grab);
|
||||||
.expect("Failed to ungrab cursor");
|
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
|
/// Represents an incoming event from the window
|
||||||
pub enum Event {
|
pub enum Event {
|
||||||
/// The window has been requested to close.
|
/// The window has been requested to close.
|
||||||
Close,
|
Close,
|
||||||
/// A key has been typed that corresponds to a specific character.
|
/// A key has been typed that corresponds to a specific character.
|
||||||
Char(char),
|
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>),
|
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 {
|
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 mut chunk = TerrainChunk::filled(Block::empty(), TerrainChunkMeta::void());
|
||||||
|
|
||||||
let air = Block::empty();
|
let air = Block::empty();
|
||||||
@ -50,7 +52,7 @@ impl World {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
air
|
air
|
||||||
});
|
}).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
chunk
|
chunk
|
||||||
|
Loading…
Reference in New Issue
Block a user