feat: 增加 ShapeStyle 配置

This commit is contained in:
2026-03-03 15:57:00 +08:00
parent 71c4de08a6
commit a4adcd42c8

View File

@@ -51,6 +51,35 @@ impl std::fmt::Display for BrushKind {
} }
} }
#[derive(Debug, Clone, Copy, PartialEq)]
enum ShapeStyle {
Stroke,
Fill,
FillAndStroke,
}
impl ShapeStyle {
const ALL: [ShapeStyle; 3] = [
ShapeStyle::Stroke,
ShapeStyle::Fill,
ShapeStyle::FillAndStroke,
];
}
impl std::fmt::Display for ShapeStyle {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
match self {
ShapeStyle::Stroke => "Stroke",
ShapeStyle::Fill => "Fill",
ShapeStyle::FillAndStroke => "FillAndStroke",
}
)
}
}
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
enum Message { enum Message {
MousePressed(Point), MousePressed(Point),
@@ -69,6 +98,7 @@ enum Message {
Decrement(ConfigOption), Decrement(ConfigOption),
BrushSelected(BrushKind), BrushSelected(BrushKind),
ShapeStyleSelected(ShapeStyle),
/// 全局鼠标释放 /// 全局鼠标释放
WindowMouseRelease, WindowMouseRelease,
@@ -194,6 +224,8 @@ struct Config {
airbrush_radius: i32, airbrush_radius: i32,
airbrush_density: i32, airbrush_density: i32,
rounded_radius: i32, rounded_radius: i32,
brush_kind: Option<BrushKind>,
shape_style: Option<ShapeStyle>,
} }
impl Default for Config { impl Default for Config {
@@ -209,7 +241,10 @@ impl Config {
airbrush_radius: 4, airbrush_radius: 4,
airbrush_density: 16, airbrush_density: 16,
rounded_radius: 3, rounded_radius: 3,
brush_kind: None,
shape_style: None,
}; };
fn incr(&mut self, option: ConfigOption, step: i32) { fn incr(&mut self, option: ConfigOption, step: i32) {
match option { match option {
ConfigOption::EraserWidth => { ConfigOption::EraserWidth => {
@@ -244,6 +279,14 @@ impl Config {
} }
} }
} }
fn set_brush_kind(&mut self, kind: BrushKind) {
self.brush_kind = Some(kind);
}
fn set_shape_style(&mut self, shape_style: ShapeStyle) {
self.shape_style = Some(shape_style);
}
} }
#[derive(Debug, Clone, Copy, PartialEq)] #[derive(Debug, Clone, Copy, PartialEq)]
@@ -281,8 +324,6 @@ struct PaintApp {
config: Config, config: Config,
brush_selected: Option<BrushKind>,
foreground_color: MSColor, foreground_color: MSColor,
background_color: MSColor, background_color: MSColor,
} }
@@ -307,7 +348,6 @@ impl PaintApp {
image_handle: image::Handle::from_rgba(width as u32, height as u32, pixels), image_handle: image::Handle::from_rgba(width as u32, height as u32, pixels),
dirty: false, dirty: false,
config: Config::default(), config: Config::default(),
brush_selected: None,
foreground_color: MSColor::BLACK, foreground_color: MSColor::BLACK,
background_color: MSColor::WHITE, background_color: MSColor::WHITE,
}; };
@@ -475,7 +515,7 @@ impl PaintApp {
.padding(padding::right(5)), .padding(padding::right(5)),
pick_list( pick_list(
&BrushKind::ALL[..], &BrushKind::ALL[..],
self.brush_selected, self.config.brush_kind,
Message::BrushSelected, Message::BrushSelected,
) )
.placeholder("Brush..."), .placeholder("Brush..."),
@@ -506,6 +546,12 @@ impl PaintApp {
], ],
] ]
.padding(padding::right(5)), .padding(padding::right(5)),
pick_list(
&ShapeStyle::ALL[..],
self.config.shape_style,
Message::ShapeStyleSelected,
)
.placeholder("Shape Style..."),
]; ];
debug_area = debug_area.padding(padding::top(10).left(5).bottom(10)); debug_area = debug_area.padding(padding::top(10).left(5).bottom(10));
@@ -592,7 +638,10 @@ impl PaintApp {
} }
} }
Message::BrushSelected(kind) => { Message::BrushSelected(kind) => {
self.brush_selected = Some(kind); self.config.set_brush_kind(kind);
}
Message::ShapeStyleSelected(kind) => {
self.config.set_shape_style(kind);
} }
Message::WindowMouseRelease => { Message::WindowMouseRelease => {
// 处理鼠标在 canvas_area 外面释放 // 处理鼠标在 canvas_area 外面释放
@@ -718,7 +767,7 @@ impl PaintApp {
pub fn update_with_brush(&mut self, message: Message) { pub fn update_with_brush(&mut self, message: Message) {
let mut brush_fn: fn(&mut MSCanvas, Point) = MSCanvas::brush_circle; let mut brush_fn: fn(&mut MSCanvas, Point) = MSCanvas::brush_circle;
if let Some(kind) = self.brush_selected { if let Some(kind) = self.config.brush_kind {
brush_fn = match kind { brush_fn = match kind {
BrushKind::Circle => MSCanvas::brush_circle, BrushKind::Circle => MSCanvas::brush_circle,
BrushKind::Square => MSCanvas::brush_square, BrushKind::Square => MSCanvas::brush_square,