Plumb layer panel (#107)

* WIP ExpandFolder handling

* Implement response parsing in typescript

* Update layer panel with list sent by wasm

* Add events for layer interaction

* Add proper default naming

* Fix displaying of the eye icon

* Attach path to LayerPanelEntry

* Fix lint issues

Co-authored-by: Keavon Chambers <keavon@keavon.com>
This commit is contained in:
TrueDoctor
2021-05-07 10:17:46 +02:00
committed by Keavon Chambers
parent 76d3e8cde4
commit 6adb984f2d
14 changed files with 276 additions and 56 deletions

View File

@@ -1,6 +1,6 @@
use crate::{
layers::{self, Folder, Layer, LayerData, LayerDataTypes, Line, PolyLine, Rect, Shape},
response::{LayerPanelEntry, LayerType},
response::LayerPanelEntry,
DocumentError, DocumentResponse, LayerId, Operation,
};
@@ -172,16 +172,12 @@ impl Document {
/// any actual data, but rather metadata such as visibility and names of the layers.
pub fn layer_panel(&self, path: &[LayerId]) -> Result<Vec<LayerPanelEntry>, DocumentError> {
let folder = self.document_folder(path)?;
let l_type = |layer: &LayerDataTypes| match layer {
LayerDataTypes::Folder(_) => LayerType::Folder,
_ => LayerType::Shape,
};
let translate = |layer: &Layer| LayerPanelEntry {
name: layer.name.clone().unwrap_or_else(|| String::from("UnnamedFolder")),
visible: layer.visible,
layer_type: l_type(&layer.data),
};
let entries = folder.layers().iter().map(|layer| translate(layer)).collect();
let entries = folder
.layers()
.iter()
.zip(folder.layer_ids.iter())
.map(|(layer, id)| LayerPanelEntry::from_layer(layer, [path, &[*id]].concat()))
.collect();
Ok(entries)
}

View File

@@ -9,6 +9,7 @@ pub struct Folder {
next_assignment_id: LayerId,
pub layer_ids: Vec<LayerId>,
layers: Vec<Layer>,
pub collapsed: bool,
}
impl LayerData for Folder {
@@ -87,6 +88,7 @@ impl Default for Folder {
layer_ids: vec![],
layers: vec![],
next_assignment_id: 0,
collapsed: false,
}
}
}

View File

@@ -1,4 +1,7 @@
use crate::LayerId;
use crate::{
layers::{Layer, LayerDataTypes},
LayerId,
};
use serde::{Deserialize, Serialize};
use std::fmt;
@@ -7,12 +10,19 @@ pub struct LayerPanelEntry {
pub name: String,
pub visible: bool,
pub layer_type: LayerType,
pub collapsed: bool,
pub path: Vec<LayerId>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum LayerType {
Folder,
Shape,
Circle,
Rect,
Line,
PolyLine,
Ellipse,
}
impl fmt::Display for LayerType {
@@ -20,12 +30,47 @@ impl fmt::Display for LayerType {
let name = match self {
LayerType::Folder => "folder",
LayerType::Shape => "shape",
LayerType::Rect => "rect",
LayerType::Line => "line",
LayerType::Circle => "circle",
LayerType::PolyLine => "poly line",
LayerType::Ellipse => "ellipse",
};
formatter.write_str(name)
}
}
impl From<&LayerDataTypes> for LayerType {
fn from(data: &LayerDataTypes) -> Self {
use LayerDataTypes::*;
match data {
Folder(_) => LayerType::Folder,
Shape(_) => LayerType::Shape,
Circle(_) => LayerType::Circle,
Rect(_) => LayerType::Rect,
Line(_) => LayerType::Line,
PolyLine(_) => LayerType::PolyLine,
Ellipse(_) => LayerType::Ellipse,
}
}
}
impl LayerPanelEntry {
pub fn from_layer(layer: &Layer, path: Vec<LayerId>) -> Self {
let layer_type: LayerType = (&layer.data).into();
let name = layer.name.clone().unwrap_or_else(|| format!("Unnamed {}", layer_type));
let collapsed = if let LayerDataTypes::Folder(f) = &layer.data { f.collapsed } else { true };
Self {
name,
visible: layer.visible,
layer_type,
collapsed,
path,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[repr(C)]
// TODO - Make Copy when possible

View File

@@ -1,7 +1,6 @@
use std::{fmt, ops::Add};
use kurbo::{PathEl, Point, Vec2};
use log::info;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ShapePoints {
@@ -47,7 +46,6 @@ impl std::fmt::Display for ShapePoints {
let sine = theta.sin();
Vec2::new(v.x * cosine - v.y * sine, v.x * sine + v.y * cosine)
}
info!("sides{}", self.sides);
for i in 0..self.sides {
let radians = self.apothem_offset_angle() * ((i * 2 + (self.sides % 2)) as f64);
let offset = rotate(&self.extent, radians);

View File

@@ -0,0 +1,9 @@
use super::{Event, EventHandler, Operation, Response};
use crate::tools::{DocumentToolData, ToolData};
use crate::Document;
pub struct DocumentEventHandler {}
impl DocumentEventHandler {
fn pre_process_event(&mut self, editor_state: &Document, tool_data: &mut DocumentToolData, events: &mut Vec<Event>, responses: &mut Vec<Response>, operations: &mut Vec<Operation>) {}
}

View File

@@ -2,6 +2,7 @@ use crate::tools::ToolType;
use crate::Color;
use bitflags::bitflags;
use document_core::LayerId;
use serde::{Deserialize, Serialize};
#[doc(inline)]
@@ -18,8 +19,16 @@ pub enum Event {
SelectTool(ToolType),
SelectPrimaryColor(Color),
SelectSecondaryColor(Color),
SelectLayer(Vec<LayerId>),
ToggleLayerVisibility(Vec<LayerId>),
ToggleLayerExpansion(Vec<LayerId>),
DeleteLayer(Vec<LayerId>),
AddLayer(Vec<LayerId>),
RenameLayer(Vec<LayerId>, String),
SwapColors,
ResetColors,
AmbiguousMouseDown(MouseState),
AmbiguousMouseUp(MouseState),
LmbDown(MouseState),
RmbDown(MouseState),
MmbDown(MouseState),
@@ -34,6 +43,7 @@ pub enum Event {
#[derive(Debug, Clone, Serialize, Deserialize)]
#[repr(C)]
pub enum ToolResponse {
// These may not have the same names as any of the DocumentResponses
SetActiveTool { tool_name: String },
UpdateCanvas { document: String },
}

View File

@@ -0,0 +1,15 @@
use super::{input_manager::InputManager, Event, EventHandler, Operation, Response};
use crate::tools::{DocumentToolData, ToolData, ToolSettings};
use document_core::document::Document;
pub struct GlobalEventHandler {}
impl GlobalEventHandler {
fn new(tool_data: ToolData) -> Self {
Self {}
}
fn pre_process_event(&mut self, input: &InputManager, events: &mut Vec<Event>, responses: &mut Vec<Response>, operations: &mut Vec<Operation>) -> bool {
false
}
}

View File

@@ -90,6 +90,7 @@ impl Dispatcher {
_ => (),
}
}
_ => todo!("Implement layer handling"),
}
let (mut tool_responses, operations) = editor_state

View File

@@ -1,5 +1,8 @@
pub type PanelId = usize;
use serde::{Deserialize, Serialize};
pub type PanelId = u32;
#[derive(Debug, Default, Serialize, Deserialize)]
pub struct Workspace {
pub hovered_panel: PanelId,
pub root: PanelGroup,
@@ -15,14 +18,20 @@ impl Workspace {
// add panel / panel group
// delete panel / panel group
// move panel / panel group
// get_serialized_layout()
}
#[derive(Debug, Serialize, Deserialize)]
pub struct PanelGroup {
pub contents: Vec<Contents>,
pub layout_direction: LayoutDirection,
}
impl Default for PanelGroup {
fn default() -> Self {
Self::new()
}
}
impl PanelGroup {
fn new() -> PanelGroup {
PanelGroup {
@@ -32,16 +41,19 @@ impl PanelGroup {
}
}
#[derive(Debug, Serialize, Deserialize)]
pub enum Contents {
PanelArea(PanelArea),
Group(PanelGroup),
}
#[derive(Debug, Serialize, Deserialize)]
pub struct PanelArea {
pub panels: Vec<PanelId>,
pub active: PanelId,
}
#[derive(Debug, Serialize, Deserialize)]
pub enum LayoutDirection {
Horizontal,
Vertical,