mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Draw Layer Edges inside each targeted layer
This commit is contained in:
1 parent
1553cd3c9e
commit
b95da03dbc
3 files changed
+166
-22
No files matched your search
@@ -11,7 +11,7 @@ use craft::ui::canvas::{
|
||||
Canvas, CanvasOutput, CanvasScrollbars, CanvasSpace, Extent, NavigationOptions, PageBorder, PageFrame, Pasteboard, PasteboardColor, PixelRect,
|
||||
RasterDisplay, RasterSource, RotateView, ToolHost, ToolHostResponse, ViewTarget, WheelScheme, canvas_space, easing_view, ruler_guide, tool_pass,
|
||||
};
|
||||
use craft::ui::canvas_controls::{GuidesOutput, SelectionOutline};
|
||||
use craft::ui::canvas_controls::{GuidesOutput, path_runs};
|
||||
use craft::ui::contract::{Bind, Props, Show, ShowContainer};
|
||||
use craft::ui::primitives::Checker;
|
||||
use craft::ui::runtime::env::Label;
|
||||
@@ -24,6 +24,10 @@ use super::{Document, PhotocraftApp, View, tools};
|
||||
use photocraft_doc::LayerId;
|
||||
use photocraft_geom::Rect as DRect;
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "layer_edges_drawing_tests.rs"]
|
||||
mod layer_edges_drawing_tests;
|
||||
|
||||
impl View {
|
||||
/// This view as the framework's canvas view of `rect` at `ppp` device pixels per point, mirrored by View › Flip
|
||||
/// Horizontal and stretched by View › Pixel Aspect Ratio Correction.
|
||||
@@ -454,6 +458,10 @@ pub(crate) fn show(app: &mut PhotocraftApp, ui: &mut egui::Ui, view: &mut View,
|
||||
// The moving pixels over the document, which hides the layer they come from, and under the controls.
|
||||
space.paint_picture(ctx, transform_picture);
|
||||
space.paint_picture(ctx, clipping);
|
||||
// Layer Edges is above the picture and below the guides and selection controls.
|
||||
if let Some(edges) = super::transform_controls::layer_edges(app, idx) {
|
||||
space.paint_picture(ctx, layer_edge_shapes(&space.view, ppp, &edges));
|
||||
}
|
||||
// The grid is drawn over the picture.
|
||||
if app.ui.view.shows(app.ui.view.show.pixel_grid) {
|
||||
let own = grid_prep.map(|prep| prep.source(app));
|
||||
@@ -503,7 +511,44 @@ fn pasteboards(app: &mut PhotocraftApp, ui: &mut egui::Ui, rect: Rect, idx: usiz
|
||||
Pasteboard::new(label, &pages).border(border).uncovered(&uncovered).show_at(ui, rect);
|
||||
}
|
||||
|
||||
/// The selection's marching ants, the Layer Edges outline and the pixel grid.
|
||||
/// Paint one device pixel inside each layer rectangle. A filled mesh keeps straight,
|
||||
/// pixel-aligned edges solid blue without stroking across the layer's boundary.
|
||||
fn layer_edge_shapes(view: &CanvasView, ppp: f32, edges: &craft::behavior::kurbo::BezPath) -> Vec<egui::Shape> {
|
||||
let pixel = ppp.recip();
|
||||
path_runs(view, edges)
|
||||
.into_iter()
|
||||
.filter_map(|(points, _)| {
|
||||
let mut outer: [Pos2; 4] = points.try_into().ok()?;
|
||||
if view.rotation == 0.0 {
|
||||
for corner in &mut outer {
|
||||
*corner = egui::pos2((corner.x * ppp).round() * pixel, (corner.y * ppp).round() * pixel);
|
||||
}
|
||||
}
|
||||
let [a, b, c, d] = outer;
|
||||
let x = b - a;
|
||||
let y = d - a;
|
||||
let (width, height) = (x.length(), y.length());
|
||||
if width == 0.0 || height == 0.0 {
|
||||
return None;
|
||||
}
|
||||
let x = x * (pixel / width).min(0.5);
|
||||
let y = y * (pixel / height).min(0.5);
|
||||
let inner = [a + x + y, b - x + y, c - x - y, d + x - y];
|
||||
let mut mesh = egui::Mesh::default();
|
||||
for point in outer.into_iter().chain(inner) {
|
||||
mesh.colored_vertex(point, Color32::from_rgb(0, 0, 255));
|
||||
}
|
||||
for i in 0..4 {
|
||||
let next = (i + 1) % 4;
|
||||
mesh.add_triangle(i, next, i + 4);
|
||||
mesh.add_triangle(next, next + 4, i + 4);
|
||||
}
|
||||
Some(egui::Shape::mesh(mesh))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// The selection's marching ants.
|
||||
fn selection_controls(app: &mut PhotocraftApp, ui: &mut egui::Ui, rect: Rect, idx: usize, label: &str) {
|
||||
let ants = ants_shown(app, idx);
|
||||
if let Some(state) = app.session.documents().get(idx) {
|
||||
@@ -518,9 +563,6 @@ fn selection_controls(app: &mut PhotocraftApp, ui: &mut egui::Ui, rect: Rect, id
|
||||
overlay.show_at(ui, rect);
|
||||
}
|
||||
}
|
||||
if let Some(edges) = super::transform_controls::layer_edges(app, idx) {
|
||||
SelectionOutline::solid(label.to_owned(), std::slice::from_ref(&edges)).show_at(ui, rect);
|
||||
}
|
||||
}
|
||||
|
||||
/// The controls the tools show over the document, in the order that decides who takes a press: a control shown later
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
use egui::epaint::{ClippedShape, Mesh};
|
||||
use serde_json::json;
|
||||
|
||||
use super::*;
|
||||
|
||||
const BLUE: Color32 = Color32::from_rgb(0, 0, 255);
|
||||
|
||||
fn paint_app() -> (Box<PhotocraftApp>, u64, u64, u64) {
|
||||
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
|
||||
app.run("file.new", json!({"width": 1200, "height": 800, "background": "white"})).unwrap();
|
||||
let background = app.session.active().unwrap().doc.background().unwrap().id.0;
|
||||
let mut paint = |name, x| {
|
||||
let result = app.run("layer.new.layer", json!({"name": name})).unwrap();
|
||||
app.run("select.rect", json!({"x": x, "y": 100, "width": 190, "height": 160})).unwrap();
|
||||
app.run("edit.fill", json!({"contents": "color", "color": "#ff0000"})).unwrap();
|
||||
app.run("select.deselect", json!({})).unwrap();
|
||||
result["layer"].as_u64().unwrap()
|
||||
};
|
||||
let a = paint("First", 101);
|
||||
let b = paint("Second", 410);
|
||||
app.sync_views();
|
||||
app.ui.extras.rulers = false;
|
||||
app.ui.view.extras = true;
|
||||
app.ui.view.show.layer_edges = true;
|
||||
(app, background, a, b)
|
||||
}
|
||||
|
||||
fn blue_meshes(shapes: &[ClippedShape]) -> Vec<&Mesh> {
|
||||
fn add<'a>(shape: &'a egui::Shape, found: &mut Vec<&'a Mesh>) {
|
||||
match shape {
|
||||
egui::Shape::Mesh(mesh) if mesh.vertices.len() == 8 && mesh.vertices.iter().all(|v| v.color == BLUE) => found.push(mesh),
|
||||
egui::Shape::Vec(shapes) => shapes.iter().for_each(|shape| add(shape, found)),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
let mut found = Vec::new();
|
||||
for shape in shapes {
|
||||
add(&shape.shape, &mut found);
|
||||
}
|
||||
found
|
||||
}
|
||||
|
||||
fn covers(mesh: &Mesh, sample: Pos2) -> bool {
|
||||
let side = |a: Pos2, b: Pos2| (b.x - a.x) * (sample.y - a.y) - (b.y - a.y) * (sample.x - a.x);
|
||||
mesh.indices.chunks_exact(3).any(|tri| {
|
||||
let [a, b, c] = [tri[0], tri[1], tri[2]].map(|i| mesh.vertices[i as usize].pos);
|
||||
let signs = [side(a, b), side(b, c), side(c, a)];
|
||||
signs.iter().all(|&s| s >= 0.0) || signs.iter().all(|&s| s <= 0.0)
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn canvas_draws_one_blue_device_pixel_inside_each_targeted_layer() {
|
||||
let (mut app, background, a, b) = paint_app();
|
||||
app.run("layer.select", json!({"layer": a})).unwrap();
|
||||
for id in [b, background] {
|
||||
app.run("layer.select", json!({"layer": id, "mode": "add"})).unwrap();
|
||||
}
|
||||
assert_eq!(app.session.active().unwrap().selected_layers().len(), 3);
|
||||
|
||||
for ppp in [1.0, 2.0] {
|
||||
let ctx = egui::Context::default();
|
||||
PhotocraftApp::setup_context(&ctx, crate::theme::AppTheme::pro());
|
||||
ctx.set_pixels_per_point(ppp);
|
||||
let rect = Rect::from_min_size(Pos2::ZERO, egui::vec2(650.0, 450.0));
|
||||
let view = View { zoom: 2.0 / 3.0, center: [600.0, 400.0], doc_size: [1200, 800], ..Default::default() };
|
||||
let mut draw = |extras, layer_edges| {
|
||||
app.ui.view.extras = extras;
|
||||
app.ui.view.show.layer_edges = layer_edges;
|
||||
ctx.run_ui(egui::RawInput { screen_rect: Some(rect), ..Default::default() }, |ui| {
|
||||
super::super::canvas_view(&mut app, ui, CanvasHost::main(0), rect, view.clone(), true);
|
||||
})
|
||||
};
|
||||
let frame = draw(true, true);
|
||||
assert_eq!(frame.pixels_per_point, ppp);
|
||||
let mut outlines = blue_meshes(&frame.shapes);
|
||||
outlines.sort_by(|a, b| a.vertices[0].pos.x.total_cmp(&b.vertices[0].pos.x));
|
||||
assert_eq!(outlines.len(), 2, "draw both paint layers and skip the Background");
|
||||
for mesh in outlines {
|
||||
let corner = mesh.vertices[0].pos;
|
||||
let inside = mesh.vertices[4].pos;
|
||||
assert_eq!((inside - corner) * ppp, egui::vec2(1.0, 1.0));
|
||||
for v in &mesh.vertices {
|
||||
assert_eq!((v.pos.x * ppp).fract(), 0.0);
|
||||
assert_eq!((v.pos.y * ppp).fract(), 0.0);
|
||||
}
|
||||
assert!(covers(mesh, corner + egui::Vec2::splat(0.5 / ppp)), "the outermost pixel is blue");
|
||||
assert!(!covers(mesh, corner + egui::Vec2::splat(1.5 / ppp)), "the next pixel stays untouched");
|
||||
assert!(!covers(mesh, corner - egui::Vec2::splat(0.5 / ppp)), "the line stays inside");
|
||||
}
|
||||
assert!(blue_meshes(&draw(false, true).shapes).is_empty(), "Extras hides Layer Edges");
|
||||
assert!(blue_meshes(&draw(true, false).shapes).is_empty(), "Layer Edges off draws nothing");
|
||||
}
|
||||
}
|
||||
@@ -12,29 +12,37 @@ use craft::ui::contract::{Bind, Props as _, Show as _};
|
||||
#[allow(unused_imports)]
|
||||
use super::{Color32, LayerContent, Pos2, Rect, Stroke, Vec2, vec2};
|
||||
|
||||
/// View › Show › Layer Edges: the active layer's content bounds (cached per tile) where the canvas
|
||||
/// shows them, as a closed path. During a Move drag that is at the offset the image is drawn
|
||||
/// with, so the outline moves in lockstep with the layer's pixels (#2413). `None` with Layer Edges off.
|
||||
/// View › Show › Layer Edges: a closed subpath for each targeted layer's content bounds.
|
||||
/// During a Move drag the outline follows the offset of the displayed pixels (#2413).
|
||||
pub(crate) fn layer_edges(app: &PhotocraftApp, idx: usize) -> Option<BezPath> {
|
||||
if !app.ui.view.shows(app.ui.view.show.layer_edges) {
|
||||
return None;
|
||||
}
|
||||
let st = app.session.documents().get(idx)?;
|
||||
let id = st.active_layer?;
|
||||
let b = st.doc.layer(id)?.surface().map(photocraft_compose::bounds::content_bounds)?;
|
||||
if b.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let (dx, dy) = crate::move_ui::shown_layer_offset(app, st.doc.id, st.revision, id).unwrap_or((0, 0));
|
||||
let b = b.translate(dx, dy);
|
||||
let (x0, y0, x1, y1) = (f64::from(b.x0), f64::from(b.y0), f64::from(b.x1), f64::from(b.y1));
|
||||
let mut path = BezPath::new();
|
||||
path.move_to((x0, y0));
|
||||
path.line_to((x1, y0));
|
||||
path.line_to((x1, y1));
|
||||
path.line_to((x0, y1));
|
||||
path.close_path();
|
||||
Some(path)
|
||||
for &id in st.selected_layers() {
|
||||
if st.doc.is_background(id) {
|
||||
continue;
|
||||
}
|
||||
let Some(layer) = st.doc.layer(id) else { continue };
|
||||
let bounds = match layer.surface() {
|
||||
Some(surface) => photocraft_compose::bounds::content_bounds(surface),
|
||||
None if layer.visible => photocraft_compose::layer_bounds(layer, st.doc.bounds()),
|
||||
None => continue,
|
||||
};
|
||||
if bounds.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let (dx, dy) = crate::move_ui::shown_layer_offset(app, st.doc.id, st.revision, id).unwrap_or((0, 0));
|
||||
let b = bounds.translate(dx, dy);
|
||||
let (x0, y0, x1, y1) = (f64::from(b.x0), f64::from(b.y0), f64::from(b.x1), f64::from(b.y1));
|
||||
path.move_to((x0, y0));
|
||||
path.line_to((x1, y0));
|
||||
path.line_to((x1, y1));
|
||||
path.line_to((x0, y1));
|
||||
path.close_path();
|
||||
}
|
||||
(!path.elements().is_empty()).then_some(path)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
Reference in new issue
Block a user