diff --git a/src/paint.rs b/src/paint.rs index 7ec08cc..fbe4631 100644 --- a/src/paint.rs +++ b/src/paint.rs @@ -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)] enum Message { MousePressed(Point), @@ -69,6 +98,7 @@ enum Message { Decrement(ConfigOption), BrushSelected(BrushKind), + ShapeStyleSelected(ShapeStyle), /// 全局鼠标释放 WindowMouseRelease, @@ -194,6 +224,8 @@ struct Config { airbrush_radius: i32, airbrush_density: i32, rounded_radius: i32, + brush_kind: Option, + shape_style: Option, } impl Default for Config { @@ -209,7 +241,10 @@ impl Config { airbrush_radius: 4, airbrush_density: 16, rounded_radius: 3, + brush_kind: None, + shape_style: None, }; + fn incr(&mut self, option: ConfigOption, step: i32) { match option { 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)] @@ -281,8 +324,6 @@ struct PaintApp { config: Config, - brush_selected: Option, - foreground_color: MSColor, background_color: MSColor, } @@ -307,7 +348,6 @@ impl PaintApp { image_handle: image::Handle::from_rgba(width as u32, height as u32, pixels), dirty: false, config: Config::default(), - brush_selected: None, foreground_color: MSColor::BLACK, background_color: MSColor::WHITE, }; @@ -475,7 +515,7 @@ impl PaintApp { .padding(padding::right(5)), pick_list( &BrushKind::ALL[..], - self.brush_selected, + self.config.brush_kind, Message::BrushSelected, ) .placeholder("Brush..."), @@ -506,6 +546,12 @@ impl PaintApp { ], ] .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)); @@ -592,7 +638,10 @@ impl PaintApp { } } 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 => { // 处理鼠标在 canvas_area 外面释放 @@ -718,7 +767,7 @@ impl PaintApp { pub fn update_with_brush(&mut self, message: Message) { 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 { BrushKind::Circle => MSCanvas::brush_circle, BrushKind::Square => MSCanvas::brush_square,