feat: 实现 ShapeStyle 配置

This commit is contained in:
2026-03-03 20:55:02 +08:00
parent a4adcd42c8
commit 849cfb247d
2 changed files with 114 additions and 9 deletions

View File

@@ -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: 左上角坐标 /// - x, y: 左上角坐标
/// - width, height: 尺寸(必须 > 0 /// - width, height: 尺寸(必须 > 0

View File

@@ -948,7 +948,20 @@ impl PaintApp {
Message::MouseMoved(pos) => { Message::MouseMoved(pos) => {
if self.is_drawing { if self.is_drawing {
self.canvas.restore_pixels(); 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; self.dirty = true;
} }
} }
@@ -993,7 +1006,20 @@ impl PaintApp {
} }
Message::MouseDoubleClick(_pos) => { Message::MouseDoubleClick(_pos) => {
self.canvas.close_path(); 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.is_drawing = false;
self.control_state = ControlState::Zero; self.control_state = ControlState::Zero;
self.dirty = true; self.dirty = true;
@@ -1016,7 +1042,20 @@ impl PaintApp {
Message::MouseMoved(pos) => { Message::MouseMoved(pos) => {
if self.is_drawing { if self.is_drawing {
self.canvas.restore_pixels(); 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; self.dirty = true;
} }
} }
@@ -1038,11 +1077,36 @@ impl PaintApp {
Message::MouseMoved(pos) => { Message::MouseMoved(pos) => {
if self.is_drawing { if self.is_drawing {
self.canvas.restore_pixels(); self.canvas.restore_pixels();
self.canvas.stroke_round_rect1( match self.get_shape_style() {
self.begin_point, ShapeStyle::Stroke => {
pos, self.canvas.stroke_round_rect1(
self.config.rounded_radius as f32, 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; self.dirty = true;
} }
} }
@@ -1052,6 +1116,14 @@ impl PaintApp {
// endregion // 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) { pub fn update_tool_states(&mut self, tool: Tool) {
let idx = tool as usize; let idx = tool as usize;
if idx >= self.tool_states.len() { if idx >= self.tool_states.len() {
@@ -1060,7 +1132,20 @@ impl PaintApp {
if self.tool_selected == Tool::Polygon && self.control_state != ControlState::Zero { if self.tool_selected == Tool::Polygon && self.control_state != ControlState::Zero {
// 切换到其他工具,闭合路径 // 切换到其他工具,闭合路径
self.canvas.close_path(); 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; self.dirty = true;
} }