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

View file

@ -7,7 +7,7 @@
<h3>{{ option.title }}</h3>
<p>{{ option.placeholder_text }}</p>
</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>
</div>
</template>
@ -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() {

View file

@ -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()),
})
}

View file

@ -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<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 {
(match ($e:expr) {$($var:ident),* $(,)?}) => {
match $e {

View file

@ -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!(),
}

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 {
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 },
}

View file

@ -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());