Major overhaul of input and communication systems

* Add input manager

* WIP lifetime hell

* Hell yeah, dark lifetime magic

* Replace events with actions in tools

* Fix borrow in GlobalEventHandler

* Fix typo in response-handler

* Distribute dispatch structure

* Add translation from events to actions

* Port default key actions to input_mapper

* Split actions macro

* Add handling for Ambiguous Mouse events

* Fix warnings and clippy lints

* Add actions macro

* WIP rework

* Add AsMessage macro

* Add implementation for derived enums

* Add macro implementation for top level message

* Add #[child] attribute to indicate derivation

* Replace some mentions of Actions and Responses with Message

* It compiles !!!

* Add functionality to some message handlers

* Add document rendering

* ICE

* Rework the previous code while keeping basic functionality

* Reduce parent-top-level macro args to only two

* Add workaround for ICE

* Fix cyclic reference in document.rs

* Make derive_transitive_child a bit more powerful

This addresses the todo that was left,
enabling arbitrary expressions to be passed as the last
parameter of a #[parent] attribute

* Adapt frontend message format

* Make responses use VecDeque

Our responses are a queue so we should use a queue type for them

* Move traits to sensible location

* Are we rectangle yet?

* Simplify, improve & document `derive_discriminant`

* Change `child` to `sub_discriminant`

This only applies to `ToDiscriminant`.
Code using `#[impl_message]` continues to work.

* Add docs for `derive_transitive_child`

* Finish docs and improve macros

The improvements are that impl_message now uses trait
resolution to obtain the parent's discriminant
and that derive_as_message now allows for non-unit
variants (which we don't use but it's nice to have,
just in case)

* Remove logging call

* Move files around and cleanup structure

* Fix proc macro doc tests

* Improve actions_fn!() macro

* Add ellipse tool

* Pass populated actions list to the input mapper

* Add KeyState bitvector

* Merge mouse buttons into "keyboard"

* Add macro for initialization of key mapper table

* Add syntactic sugar for the macro

* Implement mapping function

* Translate the remaining tools

* Fix shape tool

* Add keybindings for line and pen tool

* Fix modifiers

* Cleanup

* Add doc comments for the actions macro

* Fix formatting

* Rename MouseMove to PointerMove

* Add keybinds for tools

* Apply review suggestions

* Rename KeyMappings -> KeyMappingEntries

* Apply review changes

Co-authored-by: T0mstone <realt0mstone@gmail.com>
Co-authored-by: Paul Kupper <kupper.pa@gmail.com>
This commit is contained in:
TrueDoctor
2021-05-23 01:26:24 +02:00
committed by Keavon Chambers
co-authored by T0mstone Paul Kupper
parent c08b2d4b31
commit 4b19a459b7
72 changed files with 3028 additions and 1856 deletions
+33 -63
View File
@@ -1,54 +1,22 @@
use crate::shims::Error;
use crate::wrappers::{translate_key, translate_tool, Color};
use crate::EDITOR_STATE;
use editor_core::{events, LayerId};
use editor_core::message_prelude::*;
use editor_core::{
input::mouse::{MouseState, ViewportPosition},
LayerId,
};
use wasm_bindgen::prelude::*;
fn convert_error(err: editor_core::EditorError) -> JsValue {
Error::new(&err.to_string()).into()
}
mod mouse_state {
pub(super) type MouseKeys = u8;
use editor_core::events::{self, Event, MouseState, ViewportPosition};
static mut MOUSE_STATE: MouseKeys = 0;
pub(super) fn translate_mouse_down(mod_keys: MouseKeys, position: ViewportPosition) -> Event {
translate_mouse_event(mod_keys, position, true)
}
pub(super) fn translate_mouse_up(mod_keys: MouseKeys, position: ViewportPosition) -> Event {
translate_mouse_event(mod_keys, position, false)
}
fn translate_mouse_event(mod_keys: MouseKeys, position: ViewportPosition, down: bool) -> Event {
let diff = unsafe { MOUSE_STATE } ^ mod_keys;
unsafe { MOUSE_STATE = mod_keys };
let mouse_keys = events::MouseKeys::from_bits(mod_keys).expect("invalid modifier keys");
let state = MouseState { position, mouse_keys };
match (down, diff) {
(true, 1) => Event::LmbDown(state),
(true, 2) => Event::RmbDown(state),
(true, 4) => Event::MmbDown(state),
(false, 1) => Event::LmbUp(state),
(false, 2) => Event::RmbUp(state),
(false, 4) => Event::MmbUp(state),
(down, _) => {
log::warn!("two buttons where modified at the same time. Modification: {:#010b}", diff);
if down {
Event::AmbiguousMouseDown(state)
} else {
Event::AmbiguousMouseUp(state)
}
}
}
}
}
/// Modify the currently selected tool in the document state store
#[wasm_bindgen]
pub fn select_tool(tool: String) -> Result<(), JsValue> {
EDITOR_STATE.with(|editor| match translate_tool(&tool) {
Some(tool) => editor.borrow_mut().handle_event(events::Event::SelectTool(tool)).map_err(convert_error),
Some(tool) => editor.borrow_mut().handle_message(ToolMessage::SelectTool(tool)).map_err(convert_error),
None => Err(Error::new(&format!("Couldn't select {} because it was not recognized as a valid tool", tool)).into()),
})
}
@@ -58,26 +26,24 @@ pub fn select_tool(tool: String) -> Result<(), JsValue> {
#[wasm_bindgen]
pub fn on_mouse_move(x: u32, y: u32) -> Result<(), JsValue> {
// TODO: Convert these screenspace viewport coordinates to canvas coordinates based on the current zoom and pan
let ev = events::Event::MouseMove(events::ViewportPosition { x, y });
EDITOR_STATE.with(|editor| editor.borrow_mut().handle_event(ev)).map_err(convert_error)
let ev = InputPreprocessorMessage::MouseMove(ViewportPosition { x, y });
EDITOR_STATE.with(|editor| editor.borrow_mut().handle_message(ev)).map_err(convert_error)
}
/// A mouse button depressed within screenspace the bounds of the viewport
#[wasm_bindgen]
pub fn on_mouse_down(x: u32, y: u32, mouse_keys: u8) -> Result<(), JsValue> {
// TODO: Convert these screenspace viewport coordinates to canvas coordinates based on the current zoom and pan
let pos = events::ViewportPosition { x, y };
let ev = mouse_state::translate_mouse_down(mouse_keys, pos);
EDITOR_STATE.with(|editor| editor.borrow_mut().handle_event(ev)).map_err(convert_error)
let pos = ViewportPosition { x, y };
let ev = InputPreprocessorMessage::MouseDown(MouseState::from_u8_pos(mouse_keys, pos));
EDITOR_STATE.with(|editor| editor.borrow_mut().handle_message(ev)).map_err(convert_error)
}
/// A mouse button released
#[wasm_bindgen]
pub fn on_mouse_up(x: u32, y: u32, mouse_keys: u8) -> Result<(), JsValue> {
// TODO: Convert these screenspace viewport coordinates to canvas coordinates based on the current zoom and pan
let pos = events::ViewportPosition { x, y };
let ev = mouse_state::translate_mouse_up(mouse_keys, pos);
EDITOR_STATE.with(|editor| editor.borrow_mut().handle_event(ev)).map_err(convert_error)
let pos = ViewportPosition { x, y };
let ev = InputPreprocessorMessage::MouseUp(MouseState::from_u8_pos(mouse_keys, pos));
EDITOR_STATE.with(|editor| editor.borrow_mut().handle_message(ev)).map_err(convert_error)
}
/// A keyboard button depressed within screenspace the bounds of the viewport
@@ -85,8 +51,8 @@ pub fn on_mouse_up(x: u32, y: u32, mouse_keys: u8) -> Result<(), JsValue> {
pub fn on_key_down(name: String) -> Result<(), JsValue> {
let key = translate_key(&name);
log::trace!("key down {:?}, name: {}", key, name);
let ev = events::Event::KeyDown(key);
EDITOR_STATE.with(|editor| editor.borrow_mut().handle_event(ev)).map_err(convert_error)
let ev = InputPreprocessorMessage::KeyDown(key);
EDITOR_STATE.with(|editor| editor.borrow_mut().handle_message(ev)).map_err(convert_error)
}
/// A keyboard button released
@@ -94,15 +60,15 @@ pub fn on_key_down(name: String) -> Result<(), JsValue> {
pub fn on_key_up(name: String) -> Result<(), JsValue> {
let key = translate_key(&name);
log::trace!("key up {:?}, name: {}", key, name);
let ev = events::Event::KeyUp(key);
EDITOR_STATE.with(|editor| editor.borrow_mut().handle_event(ev)).map_err(convert_error)
let ev = InputPreprocessorMessage::KeyUp(key);
EDITOR_STATE.with(|editor| editor.borrow_mut().handle_message(ev)).map_err(convert_error)
}
/// Update primary color
#[wasm_bindgen]
pub fn update_primary_color(primary_color: Color) -> Result<(), JsValue> {
EDITOR_STATE
.with(|editor| editor.borrow_mut().handle_event(events::Event::SelectPrimaryColor(primary_color.inner())))
.with(|editor| editor.borrow_mut().handle_message(ToolMessage::SelectPrimaryColor(primary_color.inner())))
.map_err(convert_error)
}
@@ -110,33 +76,35 @@ pub fn update_primary_color(primary_color: Color) -> Result<(), JsValue> {
#[wasm_bindgen]
pub fn update_secondary_color(secondary_color: Color) -> Result<(), JsValue> {
EDITOR_STATE
.with(|editor| editor.borrow_mut().handle_event(events::Event::SelectSecondaryColor(secondary_color.inner())))
.with(|editor| editor.borrow_mut().handle_message(ToolMessage::SelectSecondaryColor(secondary_color.inner())))
.map_err(convert_error)
}
/// Swap primary and secondary color
#[wasm_bindgen]
pub fn swap_colors() -> Result<(), JsValue> {
EDITOR_STATE.with(|editor| editor.borrow_mut().handle_event(events::Event::SwapColors)).map_err(convert_error)
EDITOR_STATE.with(|editor| editor.borrow_mut().handle_message(ToolMessage::SwapColors)).map_err(convert_error)
}
/// Reset primary and secondary colors to their defaults
#[wasm_bindgen]
pub fn reset_colors() -> Result<(), JsValue> {
EDITOR_STATE.with(|editor| editor.borrow_mut().handle_event(events::Event::ResetColors)).map_err(convert_error)
EDITOR_STATE.with(|editor| editor.borrow_mut().handle_message(ToolMessage::ResetColors)).map_err(convert_error)
}
/// Select a layer from the layer list
#[wasm_bindgen]
pub fn select_layer(path: Vec<LayerId>) -> Result<(), JsValue> {
EDITOR_STATE.with(|editor| editor.borrow_mut().handle_event(events::Event::SelectLayer(path))).map_err(convert_error)
EDITOR_STATE
.with(|editor| editor.borrow_mut().handle_message(DocumentMessage::SelectLayer(path)))
.map_err(convert_error)
}
/// Toggle visibility of a layer from the layer list
#[wasm_bindgen]
pub fn toggle_layer_visibility(path: Vec<LayerId>) -> Result<(), JsValue> {
EDITOR_STATE
.with(|editor| editor.borrow_mut().handle_event(events::Event::ToggleLayerVisibility(path)))
.with(|editor| editor.borrow_mut().handle_message(DocumentMessage::ToggleLayerVisibility(path)))
.map_err(convert_error)
}
@@ -144,7 +112,7 @@ pub fn toggle_layer_visibility(path: Vec<LayerId>) -> Result<(), JsValue> {
#[wasm_bindgen]
pub fn toggle_layer_expansion(path: Vec<LayerId>) -> Result<(), JsValue> {
EDITOR_STATE
.with(|editor| editor.borrow_mut().handle_event(events::Event::ToggleLayerExpansion(path)))
.with(|editor| editor.borrow_mut().handle_message(DocumentMessage::ToggleLayerExpansion(path)))
.map_err(convert_error)
}
@@ -152,18 +120,20 @@ pub fn toggle_layer_expansion(path: Vec<LayerId>) -> Result<(), JsValue> {
#[wasm_bindgen]
pub fn rename_layer(path: Vec<LayerId>, new_name: String) -> Result<(), JsValue> {
EDITOR_STATE
.with(|editor| editor.borrow_mut().handle_event(events::Event::RenameLayer(path, new_name)))
.with(|editor| editor.borrow_mut().handle_message(DocumentMessage::RenameLayer(path, new_name)))
.map_err(convert_error)
}
/// Deletes a layer from the layer list
#[wasm_bindgen]
pub fn delete_layer(path: Vec<LayerId>) -> Result<(), JsValue> {
EDITOR_STATE.with(|editor| editor.borrow_mut().handle_event(events::Event::DeleteLayer(path))).map_err(convert_error)
EDITOR_STATE
.with(|editor| editor.borrow_mut().handle_message(DocumentMessage::DeleteLayer(path)))
.map_err(convert_error)
}
/// Requests the backend to add a layer to the layer list
#[wasm_bindgen]
pub fn add_layer(path: Vec<LayerId>) -> Result<(), JsValue> {
EDITOR_STATE.with(|editor| editor.borrow_mut().handle_event(events::Event::AddLayer(path))).map_err(convert_error)
pub fn add_folder(path: Vec<LayerId>) -> Result<(), JsValue> {
EDITOR_STATE.with(|editor| editor.borrow_mut().handle_message(DocumentMessage::AddFolder(path))).map_err(convert_error)
}