Blend modes (#252)

* Add backend for selecting layer blend mode

* Change dropdown input to support callback on change

* Add debug messages

* Fix canvas update for blend-modes

* Finish up and polish blend modes implementations

* Add changes from code review

Co-authored-by: Keavon Chambers <keavon@keavon.com>
Co-authored-by: Dennis Kobert <dennis@kobert.dev>
This commit is contained in:
George Atkinson
2021-07-23 10:21:07 -07:00
committed by GitHub
co-authored by Keavon Chambers Dennis Kobert
parent 63e3b5c604
commit 8f9168bfe5
18 changed files with 348 additions and 69 deletions
+17 -8
View File
@@ -38,11 +38,17 @@ fn layer_data<'a>(layer_data: &'a mut HashMap<Vec<LayerId>, LayerData>, path: &[
layer_data.get_mut(path).unwrap()
}
pub fn layer_panel_entry(layer_data: &mut LayerData, layer: &Layer, path: Vec<LayerId>) -> LayerPanelEntry {
pub fn layer_panel_entry(layer_data: &mut LayerData, layer: &mut Layer, path: Vec<LayerId>) -> LayerPanelEntry {
let blend_mode = layer.blend_mode.clone();
let layer_type: LayerType = (&layer.data).into();
let name = layer.name.clone().unwrap_or_else(|| format!("Unnamed {}", layer_type));
let arr = layer.current_bounding_box().unwrap_or([DVec2::ZERO, DVec2::ZERO]);
let arr = arr.iter().map(|x| (*x).into()).collect::<Vec<(f64, f64)>>();
if layer.cache_dirty {
layer.render();
}
let thumbnail = if let [(x_min, y_min), (x_max, y_max)] = arr.as_slice() {
format!(
r#"<svg xmlns="http://www.w3.org/2000/svg" viewBox="{} {} {} {}">{}</svg>"#,
@@ -50,14 +56,16 @@ pub fn layer_panel_entry(layer_data: &mut LayerData, layer: &Layer, path: Vec<La
y_min,
x_max - x_min,
y_max - y_min,
layer.cache.clone()
layer.thumbnail_cache.clone()
)
} else {
String::new()
};
LayerPanelEntry {
name,
visible: layer.visible,
blend_mode,
layer_type,
layer_data: *layer_data,
path,
@@ -73,16 +81,17 @@ impl Document {
/// Returns a list of `LayerPanelEntry`s intended for display purposes. These don't contain
/// any actual data, but rather metadata such as visibility and names of the layers.
pub fn layer_panel(&mut self, path: &[LayerId]) -> Result<Vec<LayerPanelEntry>, EditorError> {
let folder = self.document.document_folder(path)?;
let folder = self.document.document_folder_mut(path)?;
let ids = folder.as_folder()?.layer_ids.clone();
let self_layer_data = &mut self.layer_data;
let entries = folder
.as_folder()?
.layers()
.iter()
.zip(folder.as_folder()?.layer_ids.iter())
.as_folder_mut()?
.layers_mut()
.iter_mut()
.zip(ids)
.rev()
.map(|(layer, id)| {
let path = [path, &[*id]].concat();
let path = [path, &[id]].concat();
layer_panel_entry(layer_data(self_layer_data, &path), layer, path)
})
.collect();
@@ -3,6 +3,7 @@ use crate::{
consts::{MOUSE_ZOOM_RATE, VIEWPORT_SCROLL_RATE, VIEWPORT_ZOOM_SCALE_MAX, VIEWPORT_ZOOM_SCALE_MIN, WHEEL_ZOOM_RATE},
input::{mouse::ViewportPosition, InputPreprocessor},
};
use document_core::layers::BlendMode;
use document_core::layers::Layer;
use document_core::{DocumentResponse, LayerId, Operation as DocumentOperation};
use glam::{DAffine2, DVec2};
@@ -24,6 +25,7 @@ pub enum DocumentMessage {
DeleteSelectedLayers,
DuplicateSelectedLayers,
CopySelectedLayers,
SetBlendModeForSelectedLayers(BlendMode),
PasteLayers,
AddFolder(Vec<LayerId>),
RenameLayer(Vec<LayerId>, String),
@@ -342,6 +344,13 @@ impl MessageHandler<DocumentMessage, &InputPreprocessor> for DocumentMessageHand
}
.into(),
),
SetBlendModeForSelectedLayers(blend_mode) => {
let active_document = self.active_document();
for path in active_document.layer_data.iter().filter_map(|(path, data)| data.selected.then(|| path)) {
responses.push_back(DocumentOperation::SetLayerBlendMode { path: path.clone(), blend_mode }.into());
}
}
ToggleLayerVisibility(path) => {
responses.push_back(DocumentOperation::ToggleVisibility { path }.into());
}
+5 -1
View File
@@ -1,5 +1,8 @@
use crate::document::LayerData;
use document_core::{layers::LayerDataTypes, LayerId};
use document_core::{
layers::{BlendMode, LayerDataTypes},
LayerId,
};
use serde::{Deserialize, Serialize};
use std::fmt;
@@ -7,6 +10,7 @@ use std::fmt;
pub struct LayerPanelEntry {
pub name: String,
pub visible: bool,
pub blend_mode: BlendMode,
pub layer_type: LayerType,
pub layer_data: LayerData,
pub path: Vec<LayerId>,
+1
View File
@@ -1,5 +1,6 @@
use serde::{Deserialize, Serialize};
// TODO: Rename this `ToolOption` to not be plural in a separate commit (together with `enum LayerDataTypes`)
#[derive(Debug, Clone, Copy, Eq, PartialEq, Serialize, Deserialize)]
pub enum ToolOptions {
Select { append_mode: SelectAppendMode },