-
-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #803 from jeevcat/sam/viewer-interface
High-level fj-viewer interface
- Loading branch information
Showing
9 changed files
with
249 additions
and
413 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,23 @@ | ||
use crate::screen::Position; | ||
use crate::screen::NormalizedPosition; | ||
|
||
/// An input event | ||
pub enum Event { | ||
/// The cursor has moved to another position | ||
CursorMoved(Position), | ||
|
||
/// A key has been pressed or released | ||
Key(Key, KeyState), | ||
|
||
/// The user scrolled | ||
Scroll(MouseScrollDelta), | ||
} | ||
|
||
/// Describes a difference in the vertical mouse scroll wheel state. | ||
/// Positive values indicate movement forward (away from the user). | ||
pub enum MouseScrollDelta { | ||
/// Amount in lines to scroll. | ||
Line(f64), | ||
/// Amount in pixels to scroll. | ||
Pixel(f64), | ||
} | ||
|
||
/// A keyboard or mouse key | ||
pub enum Key { | ||
/// The escape key | ||
Escape, | ||
|
||
/// The numerical key `1` | ||
Key1, | ||
|
||
/// The numerical key `2` | ||
Key2, | ||
|
||
/// The numerical key `3` | ||
Key3, | ||
|
||
/// The left mouse key | ||
MouseLeft, | ||
|
||
/// The right mouse key | ||
MouseRight, | ||
} | ||
|
||
/// Defines the meaning of a key event | ||
pub enum KeyState { | ||
/// A key was pressed | ||
Pressed, | ||
|
||
/// A key was released | ||
Released, | ||
/// Move the model up, down, left or right | ||
Translate { | ||
/// The normalized position of the cursor before input | ||
previous: NormalizedPosition, | ||
/// The normalized position of the cursor after input | ||
current: NormalizedPosition, | ||
}, | ||
|
||
/// Rotate the model around the focus point | ||
Rotation { | ||
/// The angle around the screen x axis to rotate (in radians) | ||
angle_x: f64, | ||
/// The angle around the screen y axis to rotate (in radians) | ||
angle_y: f64, | ||
}, | ||
|
||
/// Move the view forwards and backwards | ||
Zoom(f64), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,136 +1,51 @@ | ||
use fj_interop::mesh::Mesh; | ||
use fj_math::Point; | ||
|
||
use super::{ | ||
event::KeyState, movement::Movement, rotation::Rotation, zoom::Zoom, Event, | ||
Key, | ||
}; | ||
use crate::{ | ||
camera::Camera, | ||
screen::{Position, Size}, | ||
}; | ||
use super::{movement::Movement, rotation::Rotation, zoom::Zoom, Event}; | ||
use crate::camera::{Camera, FocusPoint}; | ||
|
||
/// Input handling abstraction | ||
/// | ||
/// Takes user input and applies them to application state. | ||
pub struct Handler { | ||
cursor: Option<Position>, | ||
focus_point: FocusPoint, | ||
|
||
movement: Movement, | ||
rotation: Rotation, | ||
zoom: Zoom, | ||
} | ||
|
||
impl Handler { | ||
/// Returns the state of the cursor position. | ||
pub fn cursor(&self) -> Option<Position> { | ||
self.cursor | ||
} | ||
|
||
/// Handle an input event | ||
pub fn handle_event( | ||
&mut self, | ||
event: Event, | ||
screen_size: Size, | ||
mesh: &Mesh<Point<3>>, | ||
camera: &mut Camera, | ||
actions: &mut Actions, | ||
) { | ||
pub fn handle_event(&mut self, event: Event, camera: &mut Camera) { | ||
match event { | ||
Event::CursorMoved(position) => { | ||
if let Some(previous) = self.cursor { | ||
let diff_x = position.x - previous.x; | ||
let diff_y = position.y - previous.y; | ||
|
||
self.movement.apply(self.cursor, camera, screen_size); | ||
self.rotation.apply(diff_x, diff_y, camera); | ||
} | ||
|
||
self.cursor = Some(position); | ||
} | ||
Event::Key(Key::Escape, KeyState::Pressed) => actions.exit = true, | ||
|
||
Event::Key(Key::Key1, KeyState::Pressed) => { | ||
actions.toggle_model = true | ||
} | ||
Event::Key(Key::Key2, KeyState::Pressed) => { | ||
actions.toggle_mesh = true | ||
} | ||
Event::Key(Key::Key3, KeyState::Pressed) => { | ||
actions.toggle_debug = true | ||
} | ||
|
||
Event::Key(Key::MouseLeft, KeyState::Pressed) => { | ||
let focus_point = | ||
camera.focus_point(screen_size, self.cursor(), mesh); | ||
|
||
self.rotation.start(focus_point); | ||
} | ||
Event::Key(Key::MouseLeft, KeyState::Released) => { | ||
self.rotation.stop(); | ||
Event::Translate { previous, current } => self.movement.apply( | ||
previous, | ||
current, | ||
&self.focus_point, | ||
camera, | ||
), | ||
Event::Rotation { angle_x, angle_y } => { | ||
self.rotation | ||
.apply(angle_x, angle_y, &self.focus_point, camera) | ||
} | ||
Event::Zoom(zoom_delta) => { | ||
self.zoom.apply(zoom_delta, &self.focus_point, camera) | ||
} | ||
Event::Key(Key::MouseRight, KeyState::Pressed) => { | ||
let focus_point = | ||
camera.focus_point(screen_size, self.cursor(), mesh); | ||
|
||
self.movement.start(focus_point, self.cursor); | ||
} | ||
Event::Key(Key::MouseRight, KeyState::Released) => { | ||
self.movement.stop(); | ||
} | ||
|
||
Event::Scroll(delta) => { | ||
self.zoom.push(delta); | ||
} | ||
|
||
_ => {} | ||
} | ||
} | ||
|
||
/// Update application state from user input. | ||
pub fn update( | ||
&mut self, | ||
delta_t: f64, | ||
camera: &mut Camera, | ||
screen_size: Size, | ||
mesh: &Mesh<Point<3>>, | ||
) { | ||
let focus_point = camera.focus_point(screen_size, self.cursor(), mesh); | ||
self.zoom.apply_to_camera(delta_t, focus_point, camera); | ||
/// A new focus point was selected (or deselected) | ||
pub fn focus(&mut self, focus_point: FocusPoint) { | ||
self.focus_point = focus_point; | ||
} | ||
} | ||
|
||
impl Default for Handler { | ||
fn default() -> Self { | ||
Self { | ||
cursor: None, | ||
focus_point: FocusPoint::none(), | ||
|
||
movement: Movement::new(), | ||
rotation: Rotation::new(), | ||
zoom: Zoom::new(), | ||
movement: Movement, | ||
rotation: Rotation, | ||
zoom: Zoom, | ||
} | ||
} | ||
} | ||
|
||
/// Intermediate input state container | ||
/// | ||
/// Used as a per frame state container for sending application state to `winit`. | ||
#[derive(Default)] | ||
pub struct Actions { | ||
/// Application exit state. | ||
pub exit: bool, | ||
|
||
/// Toggle for the shaded display of the model. | ||
pub toggle_model: bool, | ||
/// Toggle for the model's wireframe. | ||
pub toggle_mesh: bool, | ||
/// Toggle for debug information. | ||
pub toggle_debug: bool, | ||
} | ||
|
||
impl Actions { | ||
/// Returns a new `Actions`. | ||
pub fn new() -> Self { | ||
Self::default() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.