add brush library
This commit is contained in:
@@ -766,6 +766,8 @@ struct EditorApp {
|
||||
brush_hardness: f32, // brush hardness 0.0–1.0
|
||||
brush_spacing: f32, // dabs_per_radius (fraction of radius per dab)
|
||||
brush_use_fg: bool, // true = paint with FG (stroke) color, false = BG (fill) color
|
||||
/// Full brush settings for the currently active preset (carries elliptical, jitter, etc.)
|
||||
active_brush_settings: lightningbeam_core::brush_settings::BrushSettings,
|
||||
// Audio engine integration
|
||||
#[allow(dead_code)] // Must be kept alive to maintain audio output
|
||||
audio_stream: Option<cpal::Stream>,
|
||||
@@ -1047,6 +1049,7 @@ impl EditorApp {
|
||||
brush_hardness: 0.5,
|
||||
brush_spacing: 0.1,
|
||||
brush_use_fg: true,
|
||||
active_brush_settings: lightningbeam_core::brush_settings::BrushSettings::default(),
|
||||
audio_stream,
|
||||
audio_controller,
|
||||
audio_event_rx,
|
||||
@@ -5503,6 +5506,7 @@ impl eframe::App for EditorApp {
|
||||
brush_hardness: &mut self.brush_hardness,
|
||||
brush_spacing: &mut self.brush_spacing,
|
||||
brush_use_fg: &mut self.brush_use_fg,
|
||||
active_brush_settings: &mut self.active_brush_settings,
|
||||
audio_controller: self.audio_controller.as_ref(),
|
||||
video_manager: &self.video_manager,
|
||||
playback_time: &mut self.playback_time,
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
/// - Document settings (when nothing is focused)
|
||||
|
||||
use eframe::egui::{self, DragValue, Ui};
|
||||
use lightningbeam_core::brush_settings::{bundled_brushes, BrushSettings};
|
||||
use lightningbeam_core::actions::{SetDocumentPropertiesAction, SetShapePropertiesAction};
|
||||
use lightningbeam_core::layer::{AnyLayer, LayerTrait};
|
||||
use lightningbeam_core::selection::FocusSelection;
|
||||
@@ -25,6 +26,8 @@ pub struct InfopanelPane {
|
||||
tool_section_open: bool,
|
||||
/// Whether the shape properties section is expanded
|
||||
shape_section_open: bool,
|
||||
/// Index of the selected brush preset (None = custom / unset)
|
||||
selected_brush_preset: Option<usize>,
|
||||
}
|
||||
|
||||
impl InfopanelPane {
|
||||
@@ -32,6 +35,7 @@ impl InfopanelPane {
|
||||
Self {
|
||||
tool_section_open: true,
|
||||
shape_section_open: true,
|
||||
selected_brush_preset: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -302,6 +306,12 @@ impl InfopanelPane {
|
||||
|
||||
// Raster paint tools
|
||||
Tool::Draw | Tool::Erase | Tool::Smudge if is_raster_paint_tool => {
|
||||
// Brush preset picker (Draw tool only)
|
||||
if matches!(tool, Tool::Draw) {
|
||||
self.render_brush_preset_grid(ui, shared);
|
||||
ui.add_space(2.0);
|
||||
}
|
||||
|
||||
// Color source toggle (Draw tool only)
|
||||
if matches!(tool, Tool::Draw) {
|
||||
ui.horizontal(|ui| {
|
||||
@@ -351,6 +361,81 @@ impl InfopanelPane {
|
||||
});
|
||||
}
|
||||
|
||||
/// Render the brush preset thumbnail grid for the Draw raster tool.
|
||||
fn render_brush_preset_grid(&mut self, ui: &mut Ui, shared: &mut SharedPaneState) {
|
||||
let presets = bundled_brushes();
|
||||
if presets.is_empty() { return; }
|
||||
|
||||
let gap = 3.0;
|
||||
let cols = 2usize;
|
||||
let cell_w = ((ui.available_width() - gap * (cols as f32 - 1.0)) / cols as f32).max(50.0);
|
||||
let cell_h = 80.0;
|
||||
|
||||
for (row_idx, chunk) in presets.chunks(cols).enumerate() {
|
||||
ui.horizontal(|ui| {
|
||||
ui.spacing_mut().item_spacing.x = gap;
|
||||
for (col_idx, preset) in chunk.iter().enumerate() {
|
||||
let idx = row_idx * cols + col_idx;
|
||||
let is_selected = self.selected_brush_preset == Some(idx);
|
||||
|
||||
let (rect, resp) = ui.allocate_exact_size(
|
||||
egui::vec2(cell_w, cell_h),
|
||||
egui::Sense::click(),
|
||||
);
|
||||
|
||||
let painter = ui.painter();
|
||||
|
||||
let bg = if is_selected {
|
||||
egui::Color32::from_rgb(45, 65, 95)
|
||||
} else if resp.hovered() {
|
||||
egui::Color32::from_rgb(45, 50, 62)
|
||||
} else {
|
||||
egui::Color32::from_rgb(32, 36, 44)
|
||||
};
|
||||
painter.rect_filled(rect, 4.0, bg);
|
||||
if is_selected {
|
||||
painter.rect_stroke(
|
||||
rect, 4.0,
|
||||
egui::Stroke::new(1.5, egui::Color32::from_rgb(80, 140, 220)),
|
||||
egui::StrokeKind::Middle,
|
||||
);
|
||||
}
|
||||
|
||||
// Dab preview (upper portion, leaving 18 px for the name)
|
||||
let preview_rect = egui::Rect::from_min_size(
|
||||
rect.min + egui::vec2(4.0, 4.0),
|
||||
egui::vec2(cell_w - 8.0, cell_h - 22.0),
|
||||
);
|
||||
paint_brush_dab(painter, preview_rect, &preset.settings);
|
||||
|
||||
// Name
|
||||
painter.text(
|
||||
egui::pos2(rect.center().x, rect.max.y - 9.0),
|
||||
egui::Align2::CENTER_CENTER,
|
||||
preset.name,
|
||||
egui::FontId::proportional(9.5),
|
||||
if is_selected {
|
||||
egui::Color32::from_rgb(140, 190, 255)
|
||||
} else {
|
||||
egui::Color32::from_gray(160)
|
||||
},
|
||||
);
|
||||
|
||||
if resp.clicked() {
|
||||
self.selected_brush_preset = Some(idx);
|
||||
let s = &preset.settings;
|
||||
*shared.brush_radius = s.radius_at_pressure(0.5).clamp(1.0, 200.0);
|
||||
*shared.brush_opacity = s.opaque.clamp(0.0, 1.0);
|
||||
*shared.brush_hardness = s.hardness.clamp(0.0, 1.0);
|
||||
*shared.brush_spacing = s.dabs_per_radius;
|
||||
*shared.active_brush_settings = s.clone();
|
||||
}
|
||||
}
|
||||
});
|
||||
ui.add_space(gap);
|
||||
}
|
||||
}
|
||||
|
||||
// Transform section: deferred to Phase 2 (DCEL elements don't have instance transforms)
|
||||
|
||||
/// Render shape properties section (fill/stroke)
|
||||
@@ -864,6 +949,40 @@ impl InfopanelPane {
|
||||
}
|
||||
}
|
||||
|
||||
/// Draw a brush dab preview into `rect` approximating the brush falloff shape.
|
||||
///
|
||||
/// Renders N concentric filled circles from outermost to innermost. Because each
|
||||
/// inner circle overwrites the pixels of all outer circles beneath it, the visible
|
||||
/// alpha at distance `d` from the centre equals the alpha of the innermost circle
|
||||
/// whose radius ≥ `d`. This step-approximates the actual brush falloff formula:
|
||||
/// `opa = ((1 − r) / (1 − hardness))²` for `r > hardness`, 1 inside the hard core.
|
||||
fn paint_brush_dab(painter: &egui::Painter, rect: egui::Rect, s: &BrushSettings) {
|
||||
let center = rect.center();
|
||||
let max_r = (rect.width().min(rect.height()) / 2.0 - 2.0).max(1.0);
|
||||
let h = s.hardness;
|
||||
let a = s.opaque;
|
||||
|
||||
const N: usize = 12;
|
||||
for i in 0..N {
|
||||
// t: normalized radial position of this ring, 1.0 = outermost edge
|
||||
let t = 1.0 - i as f32 / N as f32;
|
||||
let r = max_r * t;
|
||||
|
||||
let opa_weight = if h >= 1.0 || t <= h {
|
||||
1.0f32
|
||||
} else {
|
||||
let x = (1.0 - t) / (1.0 - h).max(1e-4);
|
||||
(x * x).min(1.0)
|
||||
};
|
||||
|
||||
let alpha = (opa_weight * a * 220.0).min(220.0) as u8;
|
||||
painter.circle_filled(
|
||||
center, r,
|
||||
egui::Color32::from_rgba_unmultiplied(200, 200, 220, alpha),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert MIDI note number to note name (e.g. 60 -> "C4")
|
||||
fn midi_note_name(note: u8) -> String {
|
||||
const NAMES: [&str; 12] = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"];
|
||||
|
||||
@@ -194,6 +194,8 @@ pub struct SharedPaneState<'a> {
|
||||
pub brush_spacing: &'a mut f32,
|
||||
/// Whether the brush paints with the foreground (fill) color (true) or background (stroke) color (false)
|
||||
pub brush_use_fg: &'a mut bool,
|
||||
/// Full brush settings for the active preset (carries elliptical, jitter, slow_tracking, etc.)
|
||||
pub active_brush_settings: &'a mut lightningbeam_core::brush_settings::BrushSettings,
|
||||
/// Audio engine controller for playback control (wrapped in Arc<Mutex<>> for thread safety)
|
||||
pub audio_controller: Option<&'a std::sync::Arc<std::sync::Mutex<daw_backend::EngineController>>>,
|
||||
/// Video manager for video decoding and frame caching
|
||||
|
||||
@@ -20,7 +20,7 @@ struct GpuDab {
|
||||
x: f32, y: f32, radius: f32, hardness: f32, // bytes 0–15
|
||||
opacity: f32, color_r: f32, color_g: f32, color_b: f32, // bytes 16–31
|
||||
color_a: f32, ndx: f32, ndy: f32, smudge_dist: f32, // bytes 32–47
|
||||
blend_mode: u32, _pad0: u32, _pad1: u32, _pad2: u32, // bytes 48–63
|
||||
blend_mode: u32, elliptical_dab_ratio: f32, elliptical_dab_angle: f32, lock_alpha: f32, // bytes 48–63
|
||||
}
|
||||
|
||||
struct Params {
|
||||
@@ -76,7 +76,20 @@ fn bilinear_sample(px: f32, py: f32) -> vec4<f32> {
|
||||
fn apply_dab(current: vec4<f32>, dab: GpuDab, px: i32, py: i32) -> vec4<f32> {
|
||||
let dx = f32(px) + 0.5 - dab.x;
|
||||
let dy = f32(py) + 0.5 - dab.y;
|
||||
let rr = (dx * dx + dy * dy) / (dab.radius * dab.radius);
|
||||
|
||||
// Normalised squared distance — supports circular and elliptical dabs.
|
||||
var rr: f32;
|
||||
if dab.elliptical_dab_ratio > 1.001 {
|
||||
// Rotate into the dab's local frame.
|
||||
// Major axis is along dab.elliptical_dab_angle; minor axis is compressed by ratio.
|
||||
let c = cos(dab.elliptical_dab_angle);
|
||||
let s = sin(dab.elliptical_dab_angle);
|
||||
let dx_r = dx * c + dy * s; // along major axis
|
||||
let dy_r = (-dx * s + dy * c) * dab.elliptical_dab_ratio; // minor axis compressed
|
||||
rr = (dx_r * dx_r + dy_r * dy_r) / (dab.radius * dab.radius);
|
||||
} else {
|
||||
rr = (dx * dx + dy * dy) / (dab.radius * dab.radius);
|
||||
}
|
||||
if rr > 1.0 { return current; }
|
||||
|
||||
// Quadratic falloff: flat inner core, smooth quadratic outer zone.
|
||||
@@ -94,15 +107,17 @@ fn apply_dab(current: vec4<f32>, dab: GpuDab, px: i32, py: i32) -> vec4<f32> {
|
||||
}
|
||||
|
||||
if dab.blend_mode == 0u {
|
||||
// Normal: "over" operator
|
||||
// Normal: "over" operator on premultiplied RGBA.
|
||||
// If lock_alpha > 0.5, preserve the destination alpha unchanged.
|
||||
let dab_a = opa_weight * dab.opacity * dab.color_a;
|
||||
if dab_a <= 0.0 { return current; }
|
||||
let ba = 1.0 - dab_a;
|
||||
let out_a = select(dab_a + ba * current.a, current.a, dab.lock_alpha > 0.5);
|
||||
return vec4<f32>(
|
||||
dab_a * dab.color_r + ba * current.r,
|
||||
dab_a * dab.color_g + ba * current.g,
|
||||
dab_a * dab.color_b + ba * current.b,
|
||||
dab_a + ba * current.a,
|
||||
out_a,
|
||||
);
|
||||
} else if dab.blend_mode == 1u {
|
||||
// Erase: multiplicative alpha reduction
|
||||
|
||||
@@ -4720,18 +4720,14 @@ impl StagePane {
|
||||
if !is_raster { return; }
|
||||
|
||||
let brush = {
|
||||
use lightningbeam_core::brush_settings::BrushSettings;
|
||||
BrushSettings {
|
||||
radius_log: shared.brush_radius.ln(),
|
||||
hardness: *shared.brush_hardness,
|
||||
opaque: *shared.brush_opacity,
|
||||
dabs_per_radius: *shared.brush_spacing,
|
||||
color_h: 0.0,
|
||||
color_s: 0.0,
|
||||
color_v: 0.0,
|
||||
pressure_radius_gain: 0.3,
|
||||
pressure_opacity_gain: 0.8,
|
||||
}
|
||||
// Start from the active preset (carries elliptical ratio/angle, jitter, etc.)
|
||||
// then override the four parameters the user controls via UI sliders.
|
||||
let mut b = shared.active_brush_settings.clone();
|
||||
b.radius_log = shared.brush_radius.ln();
|
||||
b.hardness = *shared.brush_hardness;
|
||||
b.opaque = *shared.brush_opacity;
|
||||
b.dabs_per_radius = *shared.brush_spacing;
|
||||
b
|
||||
};
|
||||
|
||||
let color = if matches!(blend_mode, lightningbeam_core::raster_layer::RasterBlendMode::Erase) {
|
||||
|
||||
Reference in New Issue
Block a user