Add tool_state functionality to wasm-wrapper (#42)

This commit is contained in:
TrueDoctor 2021-03-25 18:58:21 +01:00 committed by Keavon Chambers
parent d6e02c6832
commit 543fc4fec1
4 changed files with 60 additions and 3 deletions

View File

@ -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

View File

@ -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<ToolType> {
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<SelectAppendMode> {
match name {
"New" => Some(SelectAppendMode::New),
"Add" => Some(SelectAppendMode::Add),
"Subtract" => Some(SelectAppendMode::Subtract),
"Intersect" => Some(SelectAppendMode::Intersect),
_ => None,
}
}

View File

@ -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<Color, EditorError> {
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)
}

View File

@ -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,