diff --git a/crates/imaging/src/ui/curve/graph.rs b/crates/imaging/src/ui/curve/graph.rs index a74b2f377..20fdfce06 100644 --- a/crates/imaging/src/ui/curve/graph.rs +++ b/crates/imaging/src/ui/curve/graph.rs @@ -10,11 +10,11 @@ use craft_ui::contract::{ Access, Bind, BindMut, Common, Component, Cx, Edit as _, EditResponse, Focus as _, MeasureCx, Outcome, PaintUi, PartsCx, Pointer as _, Props as _, Remember as _, Role, WordSetCx as _, }; -use craft_ui::primitives::{Draw, PaintShape, StrokeWidth}; +use craft_ui::primitives::{ContentPainterCx, Draw, PaintShape, StrokeWidth}; use craft_ui::runtime::access::Name; use craft_ui::runtime::env::Label; use craft_ui::runtime::focus::{ActivateKeys, KeyUse}; -use craft_ui::style::{ComponentStyle, DataColor, FocusLook, Mix as _, StyleColor, StyleCx}; +use craft_ui::style::{ComponentStyle, DataColor, FocusLook, Mix as _, RuleKind, StyleColor, StyleCx}; use egui::{Color32, CornerRadius, Pos2, Rect, Response, Vec2, pos2, vec2}; use std::ops::Deref; use std::rc::Rc; @@ -124,13 +124,12 @@ impl Map { } } -/// The look of a plot: the frame, the histogram inks, the grid and the curves per channel. The graph and the preview -/// have one. +/// The look of a plot: the frame, the histogram inks, the diagonal and the curves per channel; the quarter grid is +/// divider rules. The graph, the preview and the contour thumbnails have one. #[derive(Clone, Debug, PartialEq)] pub struct PlotLook { frame: PlotFrame, inks: HistogramInks, - grid: StyleColor, baseline: StyleColor, /// The curve in each channel's colour, in `ChannelColor::ALL` order. curves: [StyleColor; 7], @@ -149,14 +148,13 @@ fn slot(color: ChannelColor) -> usize { const OTHER_FADE: f32 = 0.2; impl PlotLook { - pub(super) fn new(cx: &StyleCx<'_>) -> Self { + pub(crate) fn new(cx: &StyleCx<'_>) -> Self { let colors = &cx.tokens.color; let plot = cx.color(colors.surface.sunken); let curves = ChannelColor::ALL.map(|c| c.curve(cx)); PlotLook { frame: PlotFrame::new(cx), inks: HistogramInks::new(cx), - grid: cx.color(colors.line.divider), baseline: cx.color(colors.line.control_edge), curves, others: curves.map(|c| c.mix(plot, OTHER_FADE)), @@ -173,27 +171,22 @@ impl PlotLook { } /// The frame, the backdrop, the quarter grid and the diagonal of the plot `plot` (the value area is inset by the pad). - pub(super) fn backdrop( - &self, - ui: &PaintUi<'_>, - painter: &craft_ui::primitives::ContentPainterCx<'_>, - plot: Rect, - histogram: Option<&HistogramData>, - ) { + pub(super) fn backdrop(&self, painter: &ContentPainterCx<'_>, plot: Rect, histogram: Option<&HistogramData>) { paint_frame(painter, plot, &self.frame, true); let area = plot.shrink(self.pad); if let Some(data) = histogram { paint_bins(painter, area, data, HistogramLook::Backdrop, &self.inks); } - let draw = ui.draw(); - for i in 1..4 { - let f = i as f32 / 4.0; - let x = area.left() + f * area.width(); - let y = area.top() + f * area.height(); - line(&draw, [pos2(x, area.top()), pos2(x, area.bottom())], self.grid); - line(&draw, [pos2(area.left(), y), pos2(area.right(), y)], self.grid); - } - line(&draw, [area.left_bottom(), area.right_top()], self.baseline); + painter.with_draw(|draw| { + for i in 1..4 { + let f = i as f32 / 4.0; + let x = area.left() + f * area.width(); + let y = area.top() + f * area.height(); + draw.rule(RuleKind::Divider, pos2(x, area.top()), pos2(x, area.bottom())); + draw.rule(RuleKind::Divider, pos2(area.left(), y), pos2(area.right(), y)); + } + line(draw, [area.left_bottom(), area.right_top()], self.baseline); + }); } } @@ -692,7 +685,7 @@ impl Component for CurveGraph<'_> { let color = self.color(); { let painter = ui.content_painter(cx); - style.look.backdrop(ui, &painter, frame.plot, self.histogram); + style.look.backdrop(&painter, frame.plot, self.histogram); let draw = ui.draw(); for i in self.other_channels() { paint_line( diff --git a/crates/imaging/src/ui/curve/mod.rs b/crates/imaging/src/ui/curve/mod.rs index 8941eec18..8ecb410ac 100644 --- a/crates/imaging/src/ui/curve/mod.rs +++ b/crates/imaging/src/ui/curve/mod.rs @@ -10,8 +10,10 @@ pub(crate) mod stories; mod words; pub use editor::{CurveEditor, CurveEditorOutput, CurveRows}; +pub(crate) use graph::PlotLook; pub use graph::{CurveChannel, CurveSampler}; pub use model::{Curve, CurveRules}; pub use preview::CurvePreview; +pub(crate) use preview::paint_preview; pub use ramp::ToneRamp; pub use words::CurveWords; diff --git a/crates/imaging/src/ui/curve/preview.rs b/crates/imaging/src/ui/curve/preview.rs index e01a6bd95..75c802737 100644 --- a/crates/imaging/src/ui/curve/preview.rs +++ b/crates/imaging/src/ui/curve/preview.rs @@ -5,9 +5,10 @@ use super::model::Curve; use crate::ui::histogram::ChannelColor; use craft_behavior::{Input, Metrics, Sense}; use craft_ui::contract::{Common, Component, Cx, MeasureCx, PaintUi, Props as _, Role}; +use craft_ui::primitives::ContentPainterCx; use craft_ui::runtime::env::Label; use craft_ui::style::{ComponentStyle, StyleCx}; -use egui::{Response, Vec2}; +use egui::{Rect, Response, Vec2}; /// The style of a curve preview. #[derive(Clone, Debug, PartialEq, ComponentStyle)] @@ -82,14 +83,20 @@ impl Component for CurvePreview<'_> { } fn paint(&mut self, ui: &mut PaintUi<'_>, cx: &Cx<'_, Self>) { - let look = &cx.style.look; - let painter = ui.content_painter(cx); - look.backdrop(ui, &painter, cx.rect, None); - let map = Map { area: cx.rect.shrink(look.pad()), levels: self.levels }; - paint_line(&ui.draw(), &outputs(self.curve, self.sampler.as_ref(), self.levels), map, look.curve(self.color)); + let outputs = outputs(self.curve, self.sampler.as_ref(), self.levels); + paint_preview(&ui.content_painter(cx), &cx.style.look, cx.rect, &outputs, self.levels, self.color); } fn finish(self, cx: Cx<'_, Self>) -> Response { cx.response.clone() } } + +/// A curve drawn read-only over the plot `rect` in `look`: the frame, the quarter grid, the diagonal and the curve +/// through `outputs` (the output at each input level up to `levels`) in `color`. The preview and the contour +/// thumbnails paint with it. +pub(crate) fn paint_preview(painter: &ContentPainterCx<'_>, look: &PlotLook, rect: Rect, outputs: &[f32], levels: f32, color: ChannelColor) { + look.backdrop(painter, rect, None); + let map = Map { area: rect.shrink(look.pad()), levels }; + painter.with_draw(|draw| paint_line(draw, outputs, map, look.curve(color))); +} diff --git a/crates/imaging/src/ui/layer_style/contour/mod.rs b/crates/imaging/src/ui/layer_style/contour/mod.rs index bef94bf4f..0cb140ab4 100644 --- a/crates/imaging/src/ui/layer_style/contour/mod.rs +++ b/crates/imaging/src/ui/layer_style/contour/mod.rs @@ -15,13 +15,13 @@ use craft_ui::list::ThumbAspect; use craft_ui::patterns::presets::{PickerField, PresetEvent, PresetFooterEvent, PresetPicker, PresetRow, PresetState}; use craft_ui::primitives::ContentPainterCx; use craft_ui::runtime::env::{Env, Label}; -use craft_ui::style::{Corners, SurfaceKind}; -use egui::{Rect, Response, Shape, Stroke, Ui, pos2}; +use egui::{Rect, Response, Ui}; pub use editor::ContourEditorDialog; use crate::layer_style::{Contour, ContourPoint}; -use crate::ui::curve::Curve; +use crate::ui::curve::{Curve, PlotLook, paint_preview}; +use crate::ui::histogram::ChannelColor; craft_ui::words! { /// The words of the contour field and the Contour Editor, and the names of the built-in contours. @@ -83,10 +83,6 @@ pub(crate) type SharedSampler = Arc Vec + Send + Syn /// The levels of a contour as a curve: input and output run `0..=255`. const LEVELS: f32 = 255.0; -/// The line of a thumbnail's curve, as a share of its side. -const LINE_SHARE: f32 = 0.05; -/// The room around a thumbnail's curve, as a share of its side. -const INSET_SHARE: f32 = 0.08; /// A contour a picker lists: one of the built-in contours or one of the user's. #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] @@ -173,22 +169,15 @@ pub fn contour_rows(presets: &[Contour], user: &[Contour], words: &ContourWords, type SamplerFn = dyn Fn(&[[f32; 2]]) -> Vec + Send + Sync; -/// A thumbnail of a contour: its curve over a well. +/// A thumbnail of a contour: its curve in the curve preview's look (plot, quarter grid, diagonal). fn paint_thumbnail(painter: &ContentPainterCx<'_>, rect: Rect, curve: &Curve, sampler: Option<&SamplerFn>) { - painter.surface(rect, SurfaceKind::Sunken, Corners::None); let Curve::Points { points, corners } = curve else { return }; let outputs = Curve::through(points, corners, |points| match sampler { Some(sampler) => sampler(points), None => Curve::monotone(points, LEVELS), }); - let side = rect.width().min(rect.height()); - let area = rect.shrink(side * INSET_SHARE); - let line: Vec<_> = outputs - .iter() - .enumerate() - .map(|(input, output)| pos2(area.left() + input as f32 / LEVELS * area.width(), area.bottom() - output / LEVELS * area.height())) - .collect(); - painter.painter.add(Shape::line(line, Stroke::new(side * LINE_SHARE, painter.ink_on(SurfaceKind::Sunken)))); + let look = PlotLook::new(&painter.style_cx()); + paint_preview(painter, &look, rect, &outputs, LEVELS, ChannelColor::Neutral); } /// The contour as a curve in levels.