diff --git a/src/mscanvas.rs b/src/mscanvas.rs index cc02d91..d6a5732 100644 --- a/src/mscanvas.rs +++ b/src/mscanvas.rs @@ -651,6 +651,26 @@ impl MSCanvas { } } + pub fn fill_rect1(&mut self, p1: Point, p2: Point) { + let mut x = p1.x; + let mut y = p1.y; + let mut width = (p2.x - p1.x); + let mut height = (p2.y - p1.y); + if width < 0.0 && height < 0.0 { + x = p2.x; + y = p2.y; + width = (p1.x - p2.x); + height = (p1.y - p2.y); + } else if width < 0.0 { + x += width; + width = -width; + } else if height < 0.0 { + y += height; + height = -height; + } + self.fill_rect(x as i32, y as i32, width as i32, height as i32); + } + /// 填充一个圆角矩形 /// - x, y: 左上角坐标 /// - width, height: 尺寸(必须 > 0) diff --git a/src/paint.rs b/src/paint.rs index fbe4631..157ec97 100644 --- a/src/paint.rs +++ b/src/paint.rs @@ -948,7 +948,20 @@ impl PaintApp { Message::MouseMoved(pos) => { if self.is_drawing { self.canvas.restore_pixels(); - self.canvas.stroke_rect1(self.begin_point, pos); + match self.get_shape_style() { + ShapeStyle::Stroke => { + self.canvas.stroke_rect1(self.begin_point, pos); + } + ShapeStyle::Fill => { + self.canvas.fill_rect1(self.begin_point, pos); + } + ShapeStyle::FillAndStroke => { + self.canvas.fill_color(self.background_color); + self.canvas.fill_rect1(self.begin_point, pos); + self.canvas.fill_color(self.foreground_color); + self.canvas.stroke_rect1(self.begin_point, pos); + } + } self.dirty = true; } } @@ -993,7 +1006,20 @@ impl PaintApp { } Message::MouseDoubleClick(_pos) => { self.canvas.close_path(); - self.canvas.stroke(); + match self.get_shape_style() { + ShapeStyle::Stroke => { + self.canvas.stroke(); + } + ShapeStyle::Fill => { + self.canvas.fill(); + } + ShapeStyle::FillAndStroke => { + self.canvas.fill_color(self.background_color); + self.canvas.fill(); + self.canvas.fill_color(self.foreground_color); + self.canvas.stroke(); + } + } self.is_drawing = false; self.control_state = ControlState::Zero; self.dirty = true; @@ -1016,7 +1042,20 @@ impl PaintApp { Message::MouseMoved(pos) => { if self.is_drawing { self.canvas.restore_pixels(); - self.canvas.stroke_ellipse1(self.begin_point, pos); + match self.get_shape_style() { + ShapeStyle::Stroke => { + self.canvas.stroke_ellipse1(self.begin_point, pos); + } + ShapeStyle::Fill => { + self.canvas.fill_ellipse1(self.begin_point, pos); + } + ShapeStyle::FillAndStroke => { + self.canvas.fill_color(self.background_color); + self.canvas.fill_ellipse1(self.begin_point, pos); + self.canvas.fill_color(self.foreground_color); + self.canvas.stroke_ellipse1(self.begin_point, pos); + } + } self.dirty = true; } } @@ -1038,11 +1077,36 @@ impl PaintApp { Message::MouseMoved(pos) => { if self.is_drawing { self.canvas.restore_pixels(); - self.canvas.stroke_round_rect1( - self.begin_point, - pos, - self.config.rounded_radius as f32, - ); + match self.get_shape_style() { + ShapeStyle::Stroke => { + self.canvas.stroke_round_rect1( + self.begin_point, + pos, + self.config.rounded_radius as f32, + ); + } + ShapeStyle::Fill => { + self.canvas.fill_round_rect1( + self.begin_point, + pos, + self.config.rounded_radius as f32, + ); + } + ShapeStyle::FillAndStroke => { + self.canvas.fill_color(self.background_color); + self.canvas.fill_round_rect1( + self.begin_point, + pos, + self.config.rounded_radius as f32, + ); + self.canvas.fill_color(self.foreground_color); + self.canvas.stroke_round_rect1( + self.begin_point, + pos, + self.config.rounded_radius as f32, + ); + } + } self.dirty = true; } } @@ -1052,6 +1116,14 @@ impl PaintApp { // endregion + fn get_shape_style(&self) -> ShapeStyle { + self.config.shape_style.unwrap_or(ShapeStyle::Stroke) + } + + fn get_brush_kind(&self) -> BrushKind { + self.config.brush_kind.unwrap_or(BrushKind::Circle) + } + pub fn update_tool_states(&mut self, tool: Tool) { let idx = tool as usize; if idx >= self.tool_states.len() { @@ -1060,7 +1132,20 @@ impl PaintApp { if self.tool_selected == Tool::Polygon && self.control_state != ControlState::Zero { // 切换到其他工具,闭合路径 self.canvas.close_path(); - self.canvas.stroke(); + match self.get_shape_style() { + ShapeStyle::Stroke => { + self.canvas.stroke(); + } + ShapeStyle::Fill => { + self.canvas.fill(); + } + ShapeStyle::FillAndStroke => { + self.canvas.fill_color(self.background_color); + self.canvas.fill(); + self.canvas.fill_color(self.foreground_color); + self.canvas.stroke(); + } + } self.dirty = true; }