Add shape side selection with JSON deserialization

This commit is contained in:
Henry Sloan
2021-07-20 18:18:56 -04:00
parent 8ea6fd8d6a
commit 2fe1d60086
6 changed files with 28 additions and 43 deletions
@@ -7,7 +7,7 @@
<h3>{{ option.title }}</h3> <h3>{{ option.title }}</h3>
<p>{{ option.placeholder_text }}</p> <p>{{ option.placeholder_text }}</p>
</PopoverButton> </PopoverButton>
<NumberInput v-if="option.kind === 'number'" :callback="option.callback" :initialValue="option.initial" :step="option.step" :updateOnCallback="false" /> <NumberInput v-if="option.kind === 'number'" :callback="option.callback" :initialValue="option.initial" :step="option.step" :updateOnCallback="true" />
</template> </template>
</div> </div>
</template> </template>
@@ -65,9 +65,12 @@ export default defineComponent({
}, },
computed: {}, computed: {},
methods: { methods: {
async setToolSettings() { async setToolSettings(new_value: number) {
const { set_tool_settings, ToolSettings } = await wasm; // TODO: Each value-input widget (i.e. not a button) should map to a field in a settings struct,
set_tool_settings(this.$props.activeTool || "Select", new ToolSettings()); // 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() { data() {
+5 -3
View File
@@ -1,9 +1,10 @@
use crate::shims::Error; 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 crate::EDITOR_STATE;
use editor_core::input::input_preprocessor::ModifierKeys; use editor_core::input::input_preprocessor::ModifierKeys;
use editor_core::input::mouse::ScrollDelta; use editor_core::input::mouse::ScrollDelta;
use editor_core::message_prelude::*; use editor_core::message_prelude::*;
use editor_core::tool::tool_settings::ToolSettings;
use editor_core::{ use editor_core::{
input::mouse::{MouseState, ViewportPosition}, input::mouse::{MouseState, ViewportPosition},
LayerId, LayerId,
@@ -25,9 +26,10 @@ pub fn select_tool(tool: String) -> Result<(), JsValue> {
/// Update the settings for a given tool /// Update the settings for a given tool
#[wasm_bindgen] #[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) { 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()), None => Err(Error::new(&format!("Couldn't select {} because it was not recognized as a valid tool", tool)).into()),
}) })
} }
-22
View File
@@ -1,7 +1,5 @@
use crate::shims::Error; use crate::shims::Error;
use editor_core::input::keyboard::Key; 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::tool::{SelectAppendMode, ToolType};
use editor_core::Color as InnerColor; use editor_core::Color as InnerColor;
use wasm_bindgen::prelude::*; 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<ToolSettings, JsValue> {
// 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 { macro_rules! match_string_to_enum {
(match ($e:expr) {$($var:ident),* $(,)?}) => { (match ($e:expr) {$($var:ident),* $(,)?}) => {
match $e { match $e {
+1 -1
View File
@@ -179,7 +179,7 @@ impl ToolType {
ToolType::Select => ToolSettings::Select { append_mode: SelectAppendMode::New }, ToolType::Select => ToolSettings::Select { append_mode: SelectAppendMode::New },
ToolType::Ellipse => ToolSettings::Ellipse, ToolType::Ellipse => ToolSettings::Ellipse,
ToolType::Shape => ToolSettings::Shape { ToolType::Shape => ToolSettings::Shape {
shape: Shape::Polygon { vertices: 3 }, shape_type: ShapeType::Polygon { vertices: 6 },
}, },
_ => todo!(), _ => todo!(),
} }
+7 -5
View File
@@ -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 { pub enum ToolSettings {
Select { append_mode: SelectAppendMode }, Select { append_mode: SelectAppendMode },
Ellipse, 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 { pub enum SelectAppendMode {
New, New,
Add, Add,
@@ -13,8 +15,8 @@ pub enum SelectAppendMode {
Intersect, Intersect,
} }
#[derive(Debug, Clone, Copy, Eq, PartialEq)] #[derive(Debug, Clone, Copy, Eq, PartialEq, Serialize, Deserialize)]
pub enum Shape { pub enum ShapeType {
Star { vertices: u32 }, Star { vertices: u32 },
Polygon { vertices: u32 }, Polygon { vertices: u32 },
} }
+8 -8
View File
@@ -1,5 +1,5 @@
use crate::input::{mouse::ViewportPosition, InputPreprocessor}; 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 crate::{document::Document, message_prelude::*};
use document_core::{layers::style, Operation}; use document_core::{layers::style, Operation};
use glam::{DAffine2, DVec2}; use glam::{DAffine2, DVec2};
@@ -70,13 +70,13 @@ impl Fsm for ShapeToolFsmState {
data.drag_start = input.mouse.position; data.drag_start = input.mouse.position;
data.drag_current = input.mouse.position; data.drag_current = input.mouse.position;
data.sides = 6; data.sides = if let Some(&ToolSettings::Shape {
if let Some(tool_settings) = tool_data.tool_settings.get(&crate::tool::ToolType::Shape) { shape_type: ShapeType::Polygon { vertices },
if let crate::tool::ToolSettings::Shape { shape } = tool_settings { }) = tool_data.tool_settings.get(&ToolType::Shape)
if let crate::tool::Shape::Polygon { vertices } = shape { {
data.sides = *vertices as u8; vertices as u8
} } else {
} 6
}; };
responses.push_back(Operation::MountWorkingFolder { path: vec![] }.into()); responses.push_back(Operation::MountWorkingFolder { path: vec![] }.into());