mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-30 10:15:00 +01:00
104 lines
3.9 KiB
Rust
104 lines
3.9 KiB
Rust
//! One pattern fill editor ([`PatternFillEditor`]) for Photoshop-style bitmap patterns (picker, scale, angle, Link
|
||
//! with Layer, Snap to Origin) and PowerPoint-style two-colour presets (swatch grid, foreground, background), in
|
||
//! Photoshop's Pattern Fill dialog ([`PatternFillDialog`]) or inline (a paint chip's Pattern editor, a Format pane).
|
||
|
||
use craft_theme::Srgba;
|
||
|
||
mod dialog;
|
||
pub(crate) mod editor;
|
||
#[cfg(feature = "suite")]
|
||
pub(crate) mod stories;
|
||
mod words;
|
||
|
||
pub use dialog::PatternFillDialog;
|
||
pub use editor::{PatternFillEditor, PatternFillOutput};
|
||
pub use words::PatternWords;
|
||
|
||
/// A pattern the editor offers: the app's pattern library (PhotoCraft's Patterns, a bitmap each)
|
||
/// or its preset patterns (PowerPoint's "pct25", "dkHorz", …), with the app's picture of its tile
|
||
/// ([`picture`](Self::picture)); a swatch without one shows its name.
|
||
#[derive(Clone, Debug, PartialEq, Eq, Hash, craft_ui::contract::MemoProps)]
|
||
#[non_exhaustive]
|
||
pub struct PatternSwatch {
|
||
/// The app's id, which [`PatternFillOptions::pattern`] holds.
|
||
pub id: String,
|
||
/// The name in the tooltip and for a screen reader ("Bubbles", "25 %").
|
||
pub name: String,
|
||
/// The tile's picture, a texture the app made: the picker's field and cells and the preset grid show it.
|
||
#[memo_props(hash = "a texture handle: the texture store keeps one picture under it")]
|
||
pub picture: Option<egui::TextureId>,
|
||
}
|
||
|
||
impl PatternSwatch {
|
||
/// A swatch of the app's pattern `id`, named `name`.
|
||
pub fn new(id: impl Into<String>, name: impl Into<String>) -> Self {
|
||
PatternSwatch { id: id.into(), name: name.into(), picture: None }
|
||
}
|
||
|
||
/// This swatch with its tile's `picture`.
|
||
#[must_use]
|
||
pub fn with_picture(mut self, picture: egui::TextureId) -> Self {
|
||
self.picture = Some(picture);
|
||
self
|
||
}
|
||
}
|
||
|
||
/// A preset pattern's two colours (PowerPoint's Foreground and Background): present means the
|
||
/// editor is in preset mode.
|
||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||
#[non_exhaustive]
|
||
pub struct PatternColors {
|
||
/// The pattern's foreground colour.
|
||
pub foreground: Srgba,
|
||
/// The pattern's background colour.
|
||
pub background: Srgba,
|
||
}
|
||
|
||
/// A pattern fill's values.
|
||
#[derive(Clone, Debug, PartialEq)]
|
||
#[non_exhaustive]
|
||
pub struct PatternFillOptions {
|
||
/// The pattern's id ([`PatternSwatch::id`]).
|
||
pub pattern: String,
|
||
/// Bitmap mode: the scale in percent, [`SCALE`](Self::SCALE).
|
||
pub scale: f64,
|
||
/// Bitmap mode: the angle in degrees, −180 to 180.
|
||
pub angle: f64,
|
||
/// Bitmap mode: Link with Layer (the pattern moves with the layer).
|
||
pub link: bool,
|
||
/// Bitmap mode: the pattern's origin offset in pixels; Snap to Origin sets (0, 0).
|
||
pub phase: [f64; 2],
|
||
/// Preset mode: the two colours. `None` is bitmap mode.
|
||
pub colors: Option<PatternColors>,
|
||
}
|
||
|
||
impl PatternColors {
|
||
/// The two colours of a preset pattern.
|
||
pub fn new(foreground: Srgba, background: Srgba) -> Self {
|
||
PatternColors { foreground, background }
|
||
}
|
||
}
|
||
|
||
impl PatternFillOptions {
|
||
/// The scale range in percent (PhotoCraft's `pattern_fill_cmds::SCALE_RANGE`).
|
||
pub const SCALE: std::ops::RangeInclusive<f64> = 1.0..=1000.0;
|
||
|
||
/// A bitmap pattern at 100 %, 0°, linked, at the origin (Photoshop's New Fill Layer ›
|
||
/// Pattern…).
|
||
pub fn bitmap(pattern: impl Into<String>) -> Self {
|
||
PatternFillOptions { pattern: pattern.into(), scale: 100.0, angle: 0.0, link: true, phase: [0.0, 0.0], colors: None }
|
||
}
|
||
|
||
/// A preset pattern with its two colours.
|
||
pub fn preset(pattern: impl Into<String>, colors: PatternColors) -> Self {
|
||
PatternFillOptions { colors: Some(colors), ..PatternFillOptions::bitmap(pattern) }
|
||
}
|
||
}
|
||
|
||
impl Default for PatternFillOptions {
|
||
/// A bitmap pattern with no pattern picked, for a form that starts from nothing.
|
||
fn default() -> Self {
|
||
PatternFillOptions::bitmap("")
|
||
}
|
||
}
|