mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-09-19 19:08:05 +08:00
Implement event/response roundtrip (#49)
This commit is contained in:
@@ -11,25 +11,45 @@ pub enum Event {
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct Trace(Vec<MouseState>);
|
||||
#[derive(Debug, Clone)]
|
||||
struct MouseState {
|
||||
x: u32,
|
||||
y: u32,
|
||||
mod_keys: ModKeys,
|
||||
mouse_keys: MouseKeys,
|
||||
#[repr(C)]
|
||||
pub enum Response {
|
||||
UpdateCanvas,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
enum Key {
|
||||
pub struct Trace(Vec<MouseState>);
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct MouseState {
|
||||
x: u32,
|
||||
y: u32,
|
||||
mod_keys: ModKeysStorage,
|
||||
mouse_keys: MouseKeysStorage,
|
||||
}
|
||||
|
||||
impl MouseState {
|
||||
pub const fn new() -> MouseState {
|
||||
MouseState {
|
||||
x: 0,
|
||||
y: 0,
|
||||
mod_keys: 0,
|
||||
mouse_keys: 0,
|
||||
}
|
||||
}
|
||||
pub const fn from_pos(x: u32, y: u32) -> MouseState {
|
||||
MouseState { x, y, mod_keys: 0, mouse_keys: 0 }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Key {
|
||||
None,
|
||||
}
|
||||
|
||||
type ModKeysStorage = u8;
|
||||
type MouseKeysStorage = u8;
|
||||
pub type ModKeysStorage = u8;
|
||||
pub type MouseKeysStorage = u8;
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
#[repr(transparent)]
|
||||
struct ModKeys(ModKeysStorage);
|
||||
pub struct ModKeys(ModKeysStorage);
|
||||
|
||||
impl ModKeys {
|
||||
pub fn get_key(&self, key: ModKey) -> bool {
|
||||
@@ -55,7 +75,7 @@ impl MouseKeys {
|
||||
|
||||
#[repr(u8)]
|
||||
#[derive(Debug, Clone)]
|
||||
enum ModKey {
|
||||
pub enum ModKey {
|
||||
Control = 1,
|
||||
Shift = 2,
|
||||
Alt = 4,
|
||||
@@ -63,7 +83,7 @@ enum ModKey {
|
||||
|
||||
#[repr(u8)]
|
||||
#[derive(Debug, Clone)]
|
||||
enum MouseKey {
|
||||
pub enum MouseKey {
|
||||
LeftMouse = 1,
|
||||
RightMouse = 2,
|
||||
MiddleMouse = 4,
|
||||
24
core/editor/src/dispatcher/mod.rs
Normal file
24
core/editor/src/dispatcher/mod.rs
Normal file
@@ -0,0 +1,24 @@
|
||||
pub mod events;
|
||||
use crate::EditorError;
|
||||
use events::{Event, Response};
|
||||
|
||||
pub type Callback = Box<dyn Fn(Response)>;
|
||||
pub struct Dispatcher {
|
||||
callback: Callback,
|
||||
}
|
||||
|
||||
impl Dispatcher {
|
||||
pub fn handle_event(&self, event: Event) -> Result<(), EditorError> {
|
||||
match event {
|
||||
Event::Click(_) => Ok(self.emit_response(Response::UpdateCanvas)),
|
||||
_ => todo!(),
|
||||
}
|
||||
}
|
||||
pub fn emit_response(&self, response: Response) {
|
||||
let func = &self.callback;
|
||||
func(response)
|
||||
}
|
||||
pub fn new(callback: Callback) -> Dispatcher {
|
||||
Dispatcher { callback }
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
use crate::events::Event;
|
||||
use crate::Color;
|
||||
use std::error::Error;
|
||||
use std::fmt::{self, Display};
|
||||
@@ -6,6 +7,7 @@ use std::fmt::{self, Display};
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum EditorError {
|
||||
InvalidOperation(String),
|
||||
InvalidEvent(String),
|
||||
Misc(String),
|
||||
Color(String),
|
||||
}
|
||||
@@ -14,6 +16,7 @@ impl Display for EditorError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
EditorError::InvalidOperation(e) => write!(f, "Failed to execute operation: {}", e),
|
||||
EditorError::InvalidEvent(e) => write!(f, "Failed to dispatch event: {}", e),
|
||||
EditorError::Misc(e) => write!(f, "{}", e),
|
||||
EditorError::Color(c) => write!(f, "Tried to construct an invalid color {:?}", c),
|
||||
}
|
||||
@@ -35,3 +38,4 @@ macro_rules! derive_from {
|
||||
derive_from!(&str, Misc);
|
||||
derive_from!(String, Misc);
|
||||
derive_from!(Color, Color);
|
||||
derive_from!(Event, InvalidEvent);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
mod color;
|
||||
mod dispatcher;
|
||||
mod error;
|
||||
mod scheduler;
|
||||
pub mod tools;
|
||||
pub mod workspace;
|
||||
|
||||
@@ -10,6 +10,13 @@ pub use error::EditorError;
|
||||
#[doc(inline)]
|
||||
pub use color::Color;
|
||||
|
||||
#[doc(inline)]
|
||||
pub use dispatcher::events;
|
||||
|
||||
#[doc(inline)]
|
||||
pub use dispatcher::Callback;
|
||||
|
||||
use dispatcher::Dispatcher;
|
||||
use tools::ToolState;
|
||||
use workspace::Workspace;
|
||||
|
||||
@@ -17,13 +24,18 @@ use workspace::Workspace;
|
||||
pub struct Editor {
|
||||
pub tools: ToolState,
|
||||
workspace: Workspace,
|
||||
dispatcher: Dispatcher,
|
||||
}
|
||||
|
||||
impl Editor {
|
||||
pub fn new() -> Self {
|
||||
pub fn new(callback: Callback) -> Self {
|
||||
Self {
|
||||
tools: ToolState::new(),
|
||||
workspace: Workspace::new(),
|
||||
dispatcher: Dispatcher::new(callback),
|
||||
}
|
||||
}
|
||||
pub fn handle_event(&mut self, event: events::Event) -> Result<(), EditorError> {
|
||||
self.dispatcher.handle_event(event)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
pub mod events;
|
||||
Reference in New Issue
Block a user