Rework the ToolState and integrate it into the Editor struct (#48)

* Rework the ToolState and integrate it into the Editor struct

* Add asset manager sources to docs outline
This commit is contained in:
TrueDoctor
2021-03-27 22:02:01 +01:00
committed by GitHub
parent 4035138044
commit 04dc05e575
5 changed files with 91 additions and 42 deletions
+5
View File
@@ -4,8 +4,13 @@ pub mod viewport;
pub mod window; pub mod window;
pub mod wrappers; pub mod wrappers;
use graphite_editor_core::Editor;
use std::cell::RefCell;
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;
// the thread_local macro provides a way to initialize static variables with non-constant functions
thread_local! {pub static EDITOR_STATE: RefCell<Editor> = RefCell::new(Editor::new())}
#[wasm_bindgen(start)] #[wasm_bindgen(start)]
pub fn init() { pub fn init() {
utils::set_panic_hook(); utils::set_panic_hook();
+13 -12
View File
@@ -1,19 +1,18 @@
use crate::shims::Error; use crate::shims::Error;
use crate::wrappers::{translate_tool, Color}; use crate::wrappers::{translate_tool, Color};
use graphite_editor_core::tools::ToolState; use crate::EDITOR_STATE;
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;
pub static mut TOOL_STATE: ToolState = ToolState::default();
/// Modify the currently selected tool in the document state store /// Modify the currently selected tool in the document state store
#[wasm_bindgen] #[wasm_bindgen]
pub fn select_tool(tool: String) -> Result<(), JsValue> { pub fn select_tool(tool: String) -> Result<(), JsValue> {
let tool_state = unsafe { &mut TOOL_STATE }; EDITOR_STATE.with(|editor| match translate_tool(&tool) {
if let Some(tool) = translate_tool(&tool) { Some(tool) => {
Ok(tool_state.select_tool(tool)) editor.borrow_mut().tools.active_tool = tool;
} else { Ok(())
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()),
})
} }
/// Mouse movement with the bounds of the canvas /// Mouse movement with the bounds of the canvas
@@ -25,7 +24,9 @@ pub fn on_mouse_move(x: u32, y: u32) {
/// Update working colors /// Update working colors
#[wasm_bindgen] #[wasm_bindgen]
pub fn update_colors(primary_color: Color, secondary_color: Color) { pub fn update_colors(primary_color: Color, secondary_color: Color) {
let tool_state = unsafe { &mut TOOL_STATE }; EDITOR_STATE.with(|editor| {
tool_state.set_primary_color(primary_color.inner()); let mut editor = editor.borrow_mut();
tool_state.set_secondary_color(secondary_color.inner()); editor.tools.primary_color = primary_color.inner();
editor.tools.secondary_color = secondary_color.inner();
})
} }
+10 -1
View File
@@ -15,6 +15,15 @@ use workspace::Workspace;
// TODO: serialize with serde to save the current editor state // TODO: serialize with serde to save the current editor state
pub struct Editor { pub struct Editor {
tools: ToolState, pub tools: ToolState,
workspace: Workspace, workspace: Workspace,
} }
impl Editor {
pub fn new() -> Self {
Self {
tools: ToolState::new(),
workspace: Workspace::new(),
}
}
}
+48 -29
View File
@@ -1,54 +1,67 @@
use crate::Color; use crate::Color;
use std::collections::HashMap;
const TOOL_COUNT: usize = 10;
pub struct ToolState { pub struct ToolState {
primary_color: Color, pub primary_color: Color,
secondary_color: Color, pub secondary_color: Color,
active_tool: ToolType, pub active_tool: ToolType,
tool_settings: [ToolSettings; TOOL_COUNT], tool_settings: HashMap<ToolType, ToolSettings>,
} }
impl ToolState { impl ToolState {
pub const fn default() -> ToolState { pub fn new() -> Self {
ToolState { ToolState {
primary_color: Color::BLACK, primary_color: Color::BLACK,
secondary_color: Color::WHITE, secondary_color: Color::WHITE,
active_tool: ToolType::Select, active_tool: ToolType::Select,
tool_settings: [ToolSettings::Select { append_mode: SelectAppendMode::New }; TOOL_COUNT], tool_settings: default_tool_settings(),
// TODO: Initialize to sensible values
} }
} }
pub fn select_tool(&mut self, tool: ToolType) { }
self.active_tool = tool
} fn default_tool_settings() -> HashMap<ToolType, ToolSettings> {
pub fn set_primary_color<T: Into<Color>>(&mut self, primary_color: T) { let tool_init = |tool: &ToolType| (*tool, tool.default_settings());
self.primary_color = primary_color.into(); [
} tool_init(&ToolType::Select),
pub fn set_secondary_color<T: Into<Color>>(&mut self, secondary_color: T) { tool_init(&ToolType::Shape), // TODO: Add more tool defaults
self.secondary_color = secondary_color.into(); ]
} .iter()
.cloned()
.collect()
} }
#[repr(usize)] #[repr(usize)]
#[derive(Debug, Clone)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ToolType { pub enum ToolType {
Select = 0, Select,
Crop = 1, Crop,
Navigate = 2, Navigate,
Sample = 3, Sample,
Path = 4, Path,
Pen = 5, Pen,
Line = 6, Line,
Rectangle = 7, Rectangle,
Ellipse = 8, Ellipse,
Shape = 9, Shape,
// all discriminats must be strictly smaller than TOOL_COUNT! // all discriminats must be strictly smaller than TOOL_COUNT!
} }
impl ToolType {
fn default_settings(&self) -> ToolSettings {
match self {
ToolType::Select => ToolSettings::Select { append_mode: SelectAppendMode::New },
ToolType::Shape => ToolSettings::Shape {
shape: Shape::Polygon { vertices: 3 },
},
_ => todo!(),
}
}
}
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub enum ToolSettings { pub enum ToolSettings {
Select { append_mode: SelectAppendMode }, Select { append_mode: SelectAppendMode },
Shape { shape: Shape },
} }
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
@@ -58,3 +71,9 @@ pub enum SelectAppendMode {
Subtract, Subtract,
Intersect, Intersect,
} }
#[derive(Debug, Clone, Copy)]
pub enum Shape {
Star { vertices: u32 },
Polygon { vertices: u32 },
}
+15
View File
@@ -7,6 +7,12 @@ pub struct Workspace {
} }
impl Workspace { impl Workspace {
pub fn new() -> Workspace {
Workspace {
hovered_panel: 0,
root: PanelGroup::new(),
}
}
// add panel / panel group // add panel / panel group
// delete panel / panel group // delete panel / panel group
// move panel / panel group // move panel / panel group
@@ -18,6 +24,15 @@ struct PanelGroup {
layout_direction: LayoutDirection, layout_direction: LayoutDirection,
} }
impl PanelGroup {
fn new() -> PanelGroup {
PanelGroup {
contents: vec![],
layout_direction: LayoutDirection::Horizontal,
}
}
}
enum Contents { enum Contents {
PanelArea(PanelArea), PanelArea(PanelArea),
Group(PanelGroup), Group(PanelGroup),