Add plumbing for event system (#52)

* Add plumbing for event system

* Apply review suggestions

* Add swap and reset color functions
This commit is contained in:
T0mstone
2021-03-28 23:39:33 +02:00
committed by GitHub
parent ea1f9f30f0
commit c437f1bd22
9 changed files with 352 additions and 210 deletions
+47 -30
View File
@@ -8,51 +8,68 @@ use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn select_tool(tool: String) -> Result<(), JsValue> {
EDITOR_STATE.with(|editor| match translate_tool(&tool) {
Some(tool) => {
editor.borrow_mut().tools.active_tool = tool;
Ok(())
}
Some(tool) => editor.borrow_mut().handle_event(events::Event::SelectTool(tool)).map_err(|err| Error::new(&err.to_string()).into()),
None => Err(Error::new(&format!("Couldn't select {} because it was not recognized as a valid tool", tool)).into()),
})
}
/// Mouse movement with the bounds of the canvas
#[wasm_bindgen]
pub fn on_mouse_move(x: u32, y: u32) {
EDITOR_STATE.with(|editor| {
let mut editor = editor.borrow_mut();
if editor.tools.mouse_is_clicked {
editor.tools.trace.append_point(x, y)
}
})
pub fn on_mouse_move(x: u32, y: u32) -> Result<(), JsValue> {
let ev = events::Event::MouseMovement(events::CanvasPosition { x, y });
EDITOR_STATE.with(|editor| editor.borrow_mut().handle_event(ev)).map_err(|err| Error::new(&err.to_string()).into())
}
/// Mouse click within the bounds of the canvas
#[wasm_bindgen]
pub fn on_mouse_click(x: u32, y: u32) -> Result<(), JsValue> {
let ev = events::Event::Click(events::MouseState::from_pos(x, y));
EDITOR_STATE
.with(|editor| {
let mut editor = editor.borrow_mut();
editor.tools.mouse_is_clicked = true;
editor.tools.trace.clear();
editor.handle_event(ev)
})
.map_err(|err| Error::new(&err.to_string()).into())
pub fn on_mouse_down(x: u32, y: u32, mouse_keys: u8) -> Result<(), JsValue> {
let mouse_keys = events::MouseKeys::from_bits(mouse_keys).expect("invalid modifier keys");
let ev = events::Event::MouseDown(events::MouseState {
position: events::CanvasPosition { x, y },
mouse_keys,
});
EDITOR_STATE.with(|editor| editor.borrow_mut().handle_event(ev)).map_err(|err| Error::new(&err.to_string()).into())
}
/// Mouse released
#[wasm_bindgen]
pub fn on_mouse_release() {
EDITOR_STATE.with(|editor| editor.borrow_mut().tools.mouse_is_clicked = false)
pub fn on_mouse_up(x: u32, y: u32, mouse_keys: u8) -> Result<(), JsValue> {
let mouse_keys = events::MouseKeys::from_bits(mouse_keys).expect("invalid modifier keys");
let ev = events::Event::MouseDown(events::MouseState {
position: events::CanvasPosition { x, y },
mouse_keys,
});
EDITOR_STATE.with(|editor| editor.borrow_mut().handle_event(ev)).map_err(|err| Error::new(&err.to_string()).into())
}
/// Update working colors
/// Update primary color
#[wasm_bindgen]
pub fn update_colors(primary_color: Color, secondary_color: Color) {
EDITOR_STATE.with(|editor| {
let mut editor = editor.borrow_mut();
editor.tools.primary_color = primary_color.inner();
editor.tools.secondary_color = secondary_color.inner();
})
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())))
.map_err(|err: editor_core::EditorError| Error::new(&err.to_string()).into())
}
/// Update secondary color
#[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())))
.map_err(|err: editor_core::EditorError| Error::new(&err.to_string()).into())
}
/// 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(|err: editor_core::EditorError| Error::new(&err.to_string()).into())
}
/// 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(|err: editor_core::EditorError| Error::new(&err.to_string()).into())
}