From 2fe1d6008603d3f0d4b58069ef764e04dd5c7bb2 Mon Sep 17 00:00:00 2001 From: Henry Sloan Date: Tue, 20 Jul 2021 18:18:56 -0400 Subject: [PATCH] Add shape side selection with JSON deserialization --- .../widgets/options/ToolOptions.vue | 11 ++++++---- client/web/wasm/src/document.rs | 8 ++++--- client/web/wasm/src/wrappers.rs | 22 ------------------- core/editor/src/tool/mod.rs | 2 +- core/editor/src/tool/tool_settings.rs | 12 +++++----- core/editor/src/tool/tools/shape.rs | 16 +++++++------- 6 files changed, 28 insertions(+), 43 deletions(-) diff --git a/client/web/src/components/widgets/options/ToolOptions.vue b/client/web/src/components/widgets/options/ToolOptions.vue index 0cc45b5988..624a55e70c 100644 --- a/client/web/src/components/widgets/options/ToolOptions.vue +++ b/client/web/src/components/widgets/options/ToolOptions.vue @@ -7,7 +7,7 @@

{{ option.title }}

{{ option.placeholder_text }}

- + @@ -65,9 +65,12 @@ export default defineComponent({ }, computed: {}, methods: { - async setToolSettings() { - const { set_tool_settings, ToolSettings } = await wasm; - set_tool_settings(this.$props.activeTool || "Select", new ToolSettings()); + async setToolSettings(new_value: number) { + // TODO: Each value-input widget (i.e. not a button) should map to a field in a settings struct, + // and updating a widget should send the whole updated struct to the backend. + // Later, it could send a single-field update to the backend. + const { set_tool_settings } = await wasm; + set_tool_settings(this.$props.activeTool || "Select", { Shape: { shape_type: { Polygon: { vertices: new_value } } } }); }, }, data() { diff --git a/client/web/wasm/src/document.rs b/client/web/wasm/src/document.rs index eaaf36158f..9114704496 100644 --- a/client/web/wasm/src/document.rs +++ b/client/web/wasm/src/document.rs @@ -1,9 +1,10 @@ use crate::shims::Error; -use crate::wrappers::{translate_key, translate_tool, Color, ToolSettings}; +use crate::wrappers::{translate_key, translate_tool, Color}; use crate::EDITOR_STATE; use editor_core::input::input_preprocessor::ModifierKeys; use editor_core::input::mouse::ScrollDelta; use editor_core::message_prelude::*; +use editor_core::tool::tool_settings::ToolSettings; use editor_core::{ input::mouse::{MouseState, ViewportPosition}, LayerId, @@ -25,9 +26,10 @@ pub fn select_tool(tool: String) -> Result<(), JsValue> { /// Update the settings for a given tool #[wasm_bindgen] -pub fn set_tool_settings(tool: String, settings: ToolSettings) -> Result<(), JsValue> { +pub fn set_tool_settings(tool: String, settings: &JsValue) -> Result<(), JsValue> { + let settings: ToolSettings = settings.into_serde().expect("Invalid JSON for ToolSettings"); EDITOR_STATE.with(|editor| match translate_tool(&tool) { - Some(tool) => editor.borrow_mut().handle_message(ToolMessage::SetToolSettings(tool, settings.inner())).map_err(convert_error), + Some(tool) => editor.borrow_mut().handle_message(ToolMessage::SetToolSettings(tool, settings)).map_err(convert_error), None => Err(Error::new(&format!("Couldn't select {} because it was not recognized as a valid tool", tool)).into()), }) } diff --git a/client/web/wasm/src/wrappers.rs b/client/web/wasm/src/wrappers.rs index 790912d9ee..a6564bec15 100644 --- a/client/web/wasm/src/wrappers.rs +++ b/client/web/wasm/src/wrappers.rs @@ -1,7 +1,5 @@ use crate::shims::Error; use editor_core::input::keyboard::Key; -use editor_core::tool::tool_settings::Shape; -use editor_core::tool::tool_settings::ToolSettings as InnerToolSettings; use editor_core::tool::{SelectAppendMode, ToolType}; use editor_core::Color as InnerColor; use wasm_bindgen::prelude::*; @@ -26,26 +24,6 @@ impl Color { } } -#[wasm_bindgen] -pub struct ToolSettings(InnerToolSettings); - -#[wasm_bindgen] -impl ToolSettings { - #[wasm_bindgen(constructor)] - pub fn new() -> Result { - // TODO - Ok(Self(InnerToolSettings::Shape { - shape: Shape::Polygon { vertices: 6 }, - })) - } -} - -impl ToolSettings { - pub fn inner(&self) -> InnerToolSettings { - self.0 - } -} - macro_rules! match_string_to_enum { (match ($e:expr) {$($var:ident),* $(,)?}) => { match $e { diff --git a/core/editor/src/tool/mod.rs b/core/editor/src/tool/mod.rs index 3d97a27a84..d477eeb4b3 100644 --- a/core/editor/src/tool/mod.rs +++ b/core/editor/src/tool/mod.rs @@ -179,7 +179,7 @@ impl ToolType { ToolType::Select => ToolSettings::Select { append_mode: SelectAppendMode::New }, ToolType::Ellipse => ToolSettings::Ellipse, ToolType::Shape => ToolSettings::Shape { - shape: Shape::Polygon { vertices: 3 }, + shape_type: ShapeType::Polygon { vertices: 6 }, }, _ => todo!(), } diff --git a/core/editor/src/tool/tool_settings.rs b/core/editor/src/tool/tool_settings.rs index 8233db3e37..a4c1ef9533 100644 --- a/core/editor/src/tool/tool_settings.rs +++ b/core/editor/src/tool/tool_settings.rs @@ -1,11 +1,13 @@ -#[derive(Debug, Clone, Copy, Eq, PartialEq)] +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Clone, Copy, Eq, PartialEq, Serialize, Deserialize)] pub enum ToolSettings { Select { append_mode: SelectAppendMode }, Ellipse, - Shape { shape: Shape }, + Shape { shape_type: ShapeType }, } -#[derive(Debug, Clone, Copy, Eq, PartialEq)] +#[derive(Debug, Clone, Copy, Eq, PartialEq, Serialize, Deserialize)] pub enum SelectAppendMode { New, Add, @@ -13,8 +15,8 @@ pub enum SelectAppendMode { Intersect, } -#[derive(Debug, Clone, Copy, Eq, PartialEq)] -pub enum Shape { +#[derive(Debug, Clone, Copy, Eq, PartialEq, Serialize, Deserialize)] +pub enum ShapeType { Star { vertices: u32 }, Polygon { vertices: u32 }, } diff --git a/core/editor/src/tool/tools/shape.rs b/core/editor/src/tool/tools/shape.rs index aba9d35a75..cb1522a2b6 100644 --- a/core/editor/src/tool/tools/shape.rs +++ b/core/editor/src/tool/tools/shape.rs @@ -1,5 +1,5 @@ use crate::input::{mouse::ViewportPosition, InputPreprocessor}; -use crate::tool::{DocumentToolData, Fsm, ToolActionHandlerData}; +use crate::tool::{DocumentToolData, Fsm, ShapeType, ToolActionHandlerData, ToolSettings, ToolType}; use crate::{document::Document, message_prelude::*}; use document_core::{layers::style, Operation}; use glam::{DAffine2, DVec2}; @@ -70,13 +70,13 @@ impl Fsm for ShapeToolFsmState { data.drag_start = input.mouse.position; data.drag_current = input.mouse.position; - data.sides = 6; - if let Some(tool_settings) = tool_data.tool_settings.get(&crate::tool::ToolType::Shape) { - if let crate::tool::ToolSettings::Shape { shape } = tool_settings { - if let crate::tool::Shape::Polygon { vertices } = shape { - data.sides = *vertices as u8; - } - } + data.sides = if let Some(&ToolSettings::Shape { + shape_type: ShapeType::Polygon { vertices }, + }) = tool_data.tool_settings.get(&ToolType::Shape) + { + vertices as u8 + } else { + 6 }; responses.push_back(Operation::MountWorkingFolder { path: vec![] }.into());