From 543fc4fec12746aed72b4dbbbbab13647812e084 Mon Sep 17 00:00:00 2001 From: TrueDoctor Date: Thu, 25 Mar 2021 18:58:21 +0100 Subject: [PATCH] Add tool_state functionality to wasm-wrapper (#42) --- client/web/wasm/src/viewport.rs | 14 +++++++++++--- client/web/wasm/src/wrappers.rs | 27 +++++++++++++++++++++++++++ core/editor/src/color.rs | 10 ++++++++++ core/editor/src/tools/mod.rs | 12 ++++++++++++ 4 files changed, 60 insertions(+), 3 deletions(-) diff --git a/client/web/wasm/src/viewport.rs b/client/web/wasm/src/viewport.rs index ebfa59f8..6deaa215 100644 --- a/client/web/wasm/src/viewport.rs +++ b/client/web/wasm/src/viewport.rs @@ -1,10 +1,18 @@ -use crate::wrappers::Color; +use crate::wrappers::{translate_tool, Color}; +use graphite_editor_core::tools::ToolState; 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) { - todo!() +pub fn select_tool(tool: String) -> Result<(), JsValue> { + let tool_state = unsafe { &mut TOOL_STATE }; + if let Some(tool) = translate_tool(tool.as_str()) { + Ok(tool_state.select_tool(tool)) + } else { + Err(JsValue::from(format!("Couldn't select {} because it was not recognized as a valid tool", tool))) + } } /// Mouse movement with the bounds of the canvas diff --git a/client/web/wasm/src/wrappers.rs b/client/web/wasm/src/wrappers.rs index 15483d2e..9515e5de 100644 --- a/client/web/wasm/src/wrappers.rs +++ b/client/web/wasm/src/wrappers.rs @@ -1,3 +1,4 @@ +use graphite_editor_core::tools::{SelectAppendMode, ToolType}; use graphite_editor_core::Color as InnerColor; use wasm_bindgen::prelude::*; @@ -11,3 +12,29 @@ impl Color { Self(InnerColor::from_rgbaf32(red, green, blue, alpha).unwrap_throw()) } } + +pub fn translate_tool(name: &str) -> Option { + match name { + "Select" => Some(ToolType::Select), + "Crop" => Some(ToolType::Crop), + "Navigate" => Some(ToolType::Navigate), + "Sample" => Some(ToolType::Sample), + "Path" => Some(ToolType::Path), + "Pen" => Some(ToolType::Pen), + "Line" => Some(ToolType::Line), + "Rectangle" => Some(ToolType::Rectangle), + "Ellipse" => Some(ToolType::Ellipse), + "Shape" => Some(ToolType::Shape), + _ => None, + } +} + +pub fn translate_append_mode(name: &str) -> Option { + match name { + "New" => Some(SelectAppendMode::New), + "Add" => Some(SelectAppendMode::Add), + "Subtract" => Some(SelectAppendMode::Subtract), + "Intersect" => Some(SelectAppendMode::Intersect), + _ => None, + } +} diff --git a/core/editor/src/color.rs b/core/editor/src/color.rs index 70af190d..70d35158 100644 --- a/core/editor/src/color.rs +++ b/core/editor/src/color.rs @@ -10,6 +10,12 @@ pub struct Color { } impl Color { + pub const BLACK: Color = Color::from_unsafe(0., 0., 0.); + pub const WHITE: Color = Color::from_unsafe(1., 1., 1.); + pub const RED: Color = Color::from_unsafe(1., 0., 0.); + pub const GREEN: Color = Color::from_unsafe(0., 1., 0.); + pub const BLUE: Color = Color::from_unsafe(0., 0., 1.); + pub fn from_rgbaf32(red: f32, green: f32, blue: f32, alpha: f32) -> Result { let color = Color { red, green, blue, alpha }; if [red, green, blue, alpha].iter().any(|c| c.is_sign_negative() || !c.is_finite()) { @@ -17,6 +23,10 @@ impl Color { } Ok(color) } + const fn from_unsafe(red: f32, green: f32, blue: f32) -> Color { + Color { red, green, blue, alpha: 1. } + } + pub fn from_rgb8(red: u8, green: u8, blue: u8) -> Color { Color::from_rgba8(red, green, blue, 255) } diff --git a/core/editor/src/tools/mod.rs b/core/editor/src/tools/mod.rs index 69747544..06b747c4 100644 --- a/core/editor/src/tools/mod.rs +++ b/core/editor/src/tools/mod.rs @@ -10,12 +10,22 @@ pub struct ToolState { } impl ToolState { + pub const fn default() -> ToolState { + ToolState { + primary_color: Color::BLACK, + secondary_color: Color::WHITE, + active_tool: ToolType::Select, + tool_settings: [ToolSettings::Select { append_mode: SelectAppendMode::New }; TOOL_COUNT], + // TODO: Initialize to sensible values + } + } pub fn select_tool(&mut self, tool: ToolType) { self.active_tool = tool } } #[repr(usize)] +#[derive(Debug, Clone)] pub enum ToolType { Select = 0, Crop = 1, @@ -30,10 +40,12 @@ pub enum ToolType { // all discriminats must be strictly smaller than TOOL_COUNT! } +#[derive(Debug, Clone, Copy)] pub enum ToolSettings { Select { append_mode: SelectAppendMode }, } +#[derive(Debug, Clone, Copy)] pub enum SelectAppendMode { New, Add,