Rework the ToolState and integrate it into the Editor struct (#48)

* Rework the ToolState and integrate it into the Editor struct

* Add asset manager sources to docs outline
This commit is contained in:
TrueDoctor
2021-03-27 22:02:01 +01:00
committed by GitHub
parent 4035138044
commit 04dc05e575
5 changed files with 91 additions and 42 deletions

View File

@@ -4,8 +4,13 @@ pub mod viewport;
pub mod window;
pub mod wrappers;
use graphite_editor_core::Editor;
use std::cell::RefCell;
use wasm_bindgen::prelude::*;
// the thread_local macro provides a way to initialize static variables with non-constant functions
thread_local! {pub static EDITOR_STATE: RefCell<Editor> = RefCell::new(Editor::new())}
#[wasm_bindgen(start)]
pub fn init() {
utils::set_panic_hook();

View File

@@ -1,19 +1,18 @@
use crate::shims::Error;
use crate::wrappers::{translate_tool, Color};
use graphite_editor_core::tools::ToolState;
use crate::EDITOR_STATE;
use wasm_bindgen::prelude::*;
pub static mut TOOL_STATE: ToolState = ToolState::default();
/// Modify the currently selected tool in the document state store
#[wasm_bindgen]
pub fn select_tool(tool: String) -> Result<(), JsValue> {
let tool_state = unsafe { &mut TOOL_STATE };
if let Some(tool) = translate_tool(&tool) {
Ok(tool_state.select_tool(tool))
} else {
Err(Error::new(&format!("Couldn't select {} because it was not recognized as a valid tool", tool)).into())
}
EDITOR_STATE.with(|editor| match translate_tool(&tool) {
Some(tool) => {
editor.borrow_mut().tools.active_tool = tool;
Ok(())
}
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
@@ -25,7 +24,9 @@ pub fn on_mouse_move(x: u32, y: u32) {
/// Update working colors
#[wasm_bindgen]
pub fn update_colors(primary_color: Color, secondary_color: Color) {
let tool_state = unsafe { &mut TOOL_STATE };
tool_state.set_primary_color(primary_color.inner());
tool_state.set_secondary_color(secondary_color.inner());
EDITOR_STATE.with(|editor| {
let mut editor = editor.borrow_mut();
editor.tools.primary_color = primary_color.inner();
editor.tools.secondary_color = secondary_color.inner();
})
}