Skip to content

Commit 81adeac

Browse files
committed
Move Vec<BrushStroke> to Table<BrushStroke>
1 parent 4dc9e7f commit 81adeac

8 files changed

Lines changed: 41 additions & 52 deletions

File tree

editor/src/messages/portfolio/document/data_panel/data_panel_message_handler.rs

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -217,46 +217,6 @@ trait TableRowLayout {
217217
}
218218
}
219219

220-
impl<T: TableRowLayout> TableRowLayout for Vec<T> {
221-
fn type_name() -> &'static str {
222-
"Vec"
223-
}
224-
fn identifier(&self) -> String {
225-
format!("Vec<{}> ({} element{})", T::type_name(), self.len(), if self.len() == 1 { "" } else { "s" })
226-
}
227-
fn element_page(&self, data: &mut LayoutData) -> Vec<LayoutGroup> {
228-
if let Some(step) = data.desired_path.get(data.current_depth).cloned() {
229-
match step {
230-
PathStep::Element(index) => {
231-
if let Some(row) = self.get(index) {
232-
data.current_depth += 1;
233-
let result = row.layout_with_breadcrumb(data);
234-
data.current_depth -= 1;
235-
return result;
236-
} else {
237-
warn!("Desired path truncated");
238-
data.desired_path.truncate(data.current_depth);
239-
}
240-
}
241-
PathStep::Attribute { .. } => {
242-
warn!("Attribute path step inside a Vec is unsupported");
243-
data.desired_path.truncate(data.current_depth);
244-
}
245-
}
246-
}
247-
248-
let mut rows = self
249-
.iter()
250-
.enumerate()
251-
.map(|(index, row)| vec![TextLabel::new(format!("{index}")).narrow(true).widget_instance(), row.cell_widget(PathStep::Element(index))])
252-
.collect::<Vec<_>>();
253-
254-
rows.insert(0, column_headings(&["", "element"]));
255-
256-
vec![LayoutGroup::table(rows, false)]
257-
}
258-
}
259-
260220
impl<T: TableRowLayout> TableRowLayout for Table<T> {
261221
fn type_name() -> &'static str {
262222
"Table"

editor/src/messages/portfolio/document/graph_operation/utility_types.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,8 @@ impl<'a> ModifyInputsContext<'a> {
580580
let Some(brush_node_id) = self.existing_network_node_id("Brush", true) else {
581581
return;
582582
};
583-
self.set_input_with_refresh(InputConnector::node(brush_node_id, 1), NodeInput::value(TaggedValue::BrushStrokes(strokes), false), false);
583+
let strokes_table = strokes.into_iter().map(graphene_std::table::TableRow::new_from_element).collect();
584+
self.set_input_with_refresh(InputConnector::node(brush_node_id, 1), NodeInput::value(TaggedValue::BrushStrokeTable(strokes_table), false), false);
584585
}
585586

586587
pub fn resize_artboard(&mut self, location: IVec2, dimensions: IVec2) {

editor/src/messages/portfolio/document/node_graph/document_node_definitions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1391,7 +1391,7 @@ fn document_node_definitions() -> HashMap<DefinitionIdentifier, DocumentNodeDefi
13911391
}),
13921392
inputs: vec![
13931393
NodeInput::value(TaggedValue::Raster(Default::default()), true),
1394-
NodeInput::value(TaggedValue::BrushStrokes(Vec::new()), false),
1394+
NodeInput::value(TaggedValue::BrushStrokeTable(Default::default()), false),
13951395
NodeInput::value(TaggedValue::BrushCache(BrushCache::default()), false),
13961396
],
13971397
..Default::default()

editor/src/messages/tool/tool_messages/brush_tool.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,8 @@ impl BrushToolData {
321321

322322
if reference == DefinitionIdentifier::Network("Brush".into()) && node_id != layer.to_node() {
323323
let points_input = node.inputs.get(1)?;
324-
let Some(TaggedValue::BrushStrokes(strokes)) = points_input.as_value() else { continue };
325-
self.strokes.clone_from(strokes);
324+
let Some(TaggedValue::BrushStrokeTable(strokes)) = points_input.as_value() else { continue };
325+
self.strokes = strokes.iter_element_values().cloned().collect();
326326

327327
return Some(layer);
328328
}

node-graph/graph-craft/src/document/value.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,9 @@ tagged_value! {
191191
#[serde(deserialize_with = "graphic_types::vector_types::gradient::migrate_gradient_stops")] // TODO: Eventually remove this migration document upgrade code
192192
#[serde(alias = "GradientPositions", alias = "GradientStops")]
193193
GradientTable(Table<GradientStops>),
194+
#[serde(deserialize_with = "brush_nodes::migrations::migrate_brush_strokes_to_table")] // TODO: Eventually remove this migration document upgrade code
195+
#[serde(alias = "BrushStrokes")]
196+
BrushStrokeTable(Table<BrushStroke>),
194197
// ============
195198
// SCALAR TYPES
196199
// ============
@@ -208,7 +211,6 @@ tagged_value! {
208211
Stroke(Stroke),
209212
Gradient(Gradient),
210213
Font(Font),
211-
BrushStrokes(Vec<BrushStroke>),
212214
BrushCache(BrushCache),
213215
DocumentNode(DocumentNode),
214216
ContextFeatures(ContextFeatures),

node-graph/interpreted-executor/src/node_registry.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ fn node_registry() -> HashMap<ProtoNodeIdentifier, HashMap<NodeIOTypes, NodeCons
102102
async_node!(graphene_core::memo::MonitorNode<_, _, _>, input: Context, fn_params: [Context => Table<u8>]),
103103
async_node!(graphene_core::memo::MonitorNode<_, _, _>, input: Context, fn_params: [Context => Graphic]),
104104
async_node!(graphene_core::memo::MonitorNode<_, _, _>, input: Context, fn_params: [Context => graphene_std::text::Font]),
105-
async_node!(graphene_core::memo::MonitorNode<_, _, _>, input: Context, fn_params: [Context => Vec<BrushStroke>]),
105+
async_node!(graphene_core::memo::MonitorNode<_, _, _>, input: Context, fn_params: [Context => Table<BrushStroke>]),
106106
async_node!(graphene_core::memo::MonitorNode<_, _, _>, input: Context, fn_params: [Context => BrushCache]),
107107
async_node!(graphene_core::memo::MonitorNode<_, _, _>, input: Context, fn_params: [Context => DocumentNode]),
108108
async_node!(graphene_core::memo::MonitorNode<_, _, _>, input: Context, fn_params: [Context => graphene_std::raster::curve::Curve]),
@@ -180,7 +180,7 @@ fn node_registry() -> HashMap<ProtoNodeIdentifier, HashMap<NodeIOTypes, NodeCons
180180
async_node!(graphene_core::memo::MemoNode<_, _>, input: Context, fn_params: [Context => graphene_std::vector::style::Stroke]),
181181
async_node!(graphene_core::memo::MemoNode<_, _>, input: Context, fn_params: [Context => graphene_std::vector::style::Gradient]),
182182
async_node!(graphene_core::memo::MemoNode<_, _>, input: Context, fn_params: [Context => graphene_std::text::Font]),
183-
async_node!(graphene_core::memo::MemoNode<_, _>, input: Context, fn_params: [Context => Vec<BrushStroke>]),
183+
async_node!(graphene_core::memo::MemoNode<_, _>, input: Context, fn_params: [Context => Table<BrushStroke>]),
184184
async_node!(graphene_core::memo::MemoNode<_, _>, input: Context, fn_params: [Context => BrushCache]),
185185
async_node!(graphene_core::memo::MemoNode<_, _>, input: Context, fn_params: [Context => DocumentNode]),
186186
async_node!(graphene_core::memo::MemoNode<_, _>, input: Context, fn_params: [Context => graphene_std::ContextFeatures]),

node-graph/nodes/brush/src/brush.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ async fn brush(
192192
/// Optional raster content that may be drawn onto.
193193
mut image: Table<Raster<CPU>>,
194194
/// The list of brush stroke paths drawn by the Brush tool, with each including both its coordinates and styles.
195-
strokes: Vec<BrushStroke>,
195+
strokes: Table<BrushStroke>,
196196
/// Internal cache data used to accelerate rendering of the brush content.
197197
cache: BrushCache,
198198
) -> Table<Raster<CPU>> {
@@ -205,11 +205,15 @@ async fn brush(
205205
let bounds = Table::new_from_row(table_row.clone()).bounding_box(DAffine2::IDENTITY, false);
206206
let [start, end] = if let RenderBoundingBox::Rectangle(rect) = bounds { rect } else { [DVec2::ZERO, DVec2::ZERO] };
207207
let image_bbox = AxisAlignedBbox { start, end };
208-
let stroke_bbox = strokes.iter().map(|s| s.bounding_box()).reduce(|a, b| a.union(&b)).unwrap_or(AxisAlignedBbox::ZERO);
208+
let stroke_bbox = strokes.iter_element_values().map(|s| s.bounding_box()).reduce(|a, b| a.union(&b)).unwrap_or(AxisAlignedBbox::ZERO);
209209
let bbox = if image_bbox.size().length() < 0.1 { stroke_bbox } else { stroke_bbox.union(&image_bbox) };
210210
let background_bounds = bbox.to_transform();
211211

212-
let mut draw_strokes: Vec<_> = strokes.iter().filter(|&s| !matches!(s.style.blend_mode, BlendMode::Erase | BlendMode::Restore)).cloned().collect();
212+
let mut draw_strokes: Vec<_> = strokes
213+
.iter_element_values()
214+
.filter(|&s| !matches!(s.style.blend_mode, BlendMode::Erase | BlendMode::Restore))
215+
.cloned()
216+
.collect();
213217

214218
let mut brush_plan = cache.compute_brush_plan(table_row, &draw_strokes);
215219

@@ -273,12 +277,12 @@ async fn brush(
273277
actual_image = blend_with_mode(actual_image, stroke_texture, stroke.style.blend_mode, (stroke.style.color.a() * 100.) as f64);
274278
}
275279

276-
let has_erase_or_restore_strokes = strokes.iter().any(|s| matches!(s.style.blend_mode, BlendMode::Erase | BlendMode::Restore));
280+
let has_erase_or_restore_strokes = strokes.iter_element_values().any(|s| matches!(s.style.blend_mode, BlendMode::Erase | BlendMode::Restore));
277281
if has_erase_or_restore_strokes {
278282
let opaque_image = Image::new(bbox.size().x as u32, bbox.size().y as u32, Color::WHITE);
279283
let mut erase_restore_mask = TableRow::new_from_element(Raster::new_cpu(opaque_image)).with_attribute("transform", background_bounds);
280284

281-
for stroke in strokes {
285+
for stroke in strokes.into_iter().map(|row| row.into_element()) {
282286
let mut brush_texture = cache.get_cached_brush(&stroke.style);
283287
if brush_texture.is_none() {
284288
let tex = create_brush_texture(&stroke.style).await;

node-graph/nodes/brush/src/lib.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
11
pub mod brush;
22
pub mod brush_cache;
33
pub mod brush_stroke;
4+
5+
pub mod migrations {
6+
use crate::brush_stroke::BrushStroke;
7+
use core_types::table::{Table, TableRow};
8+
9+
// TODO: Eventually remove this migration document upgrade code
10+
pub fn migrate_brush_strokes_to_table<'de, D: serde::Deserializer<'de>>(deserializer: D) -> Result<Table<BrushStroke>, D::Error> {
11+
use serde::Deserialize;
12+
13+
#[derive(serde::Serialize, serde::Deserialize)]
14+
#[serde(untagged)]
15+
enum BrushStrokeTableFormat {
16+
BrushStrokes(Vec<BrushStroke>),
17+
BrushStrokeTable(Table<BrushStroke>),
18+
}
19+
20+
Ok(match BrushStrokeTableFormat::deserialize(deserializer)? {
21+
BrushStrokeTableFormat::BrushStrokes(strokes) => strokes.into_iter().map(TableRow::new_from_element).collect(),
22+
BrushStrokeTableFormat::BrushStrokeTable(table) => table,
23+
})
24+
}
25+
}

0 commit comments

Comments
 (0)