Compare commits
2 Commits
a9cf91ace7
...
02c18c7387
| Author | SHA1 | Date | |
|---|---|---|---|
| 02c18c7387 | |||
| a261d975c1 |
122
src/mscanvas.rs
122
src/mscanvas.rs
@@ -727,6 +727,94 @@ impl MSCanvas {
|
||||
self.fill_rect_color(x, y, width, height, self.background_color)
|
||||
}
|
||||
|
||||
/// 填充一个圆角矩形
|
||||
/// - x, y: 左上角坐标
|
||||
/// - width, height: 尺寸(必须 > 0)
|
||||
/// - radius: 圆角半径(会被 clamp 到合理范围)
|
||||
pub fn fill_round_rect(&mut self, x: f32, y: f32, width: f32, height: f32, radius: f32) {
|
||||
if (width as i32) <= 2 * self.line_width
|
||||
|| (height as i32) <= 2 * self.line_width
|
||||
|| width <= 2.0 * radius
|
||||
|| height <= 2.0 * radius
|
||||
{
|
||||
self.fill_rect(x as i32, y as i32, width as i32, height as i32);
|
||||
return;
|
||||
}
|
||||
|
||||
let min_x = x;
|
||||
let max_x = x + width - 1.0;
|
||||
let min_y = y;
|
||||
let max_y = y + height - 1.0;
|
||||
|
||||
// 预计算四个圆角的圆心
|
||||
let corners = [
|
||||
((x + radius) as i32, (y + radius) as i32), // 左上
|
||||
((x + width - 1.0 - radius) as i32, (y + radius) as i32), // 右上
|
||||
(
|
||||
(x + width - 1.0 - radius) as i32,
|
||||
(y + height - 1.0 - radius) as i32,
|
||||
), // 右下
|
||||
((x + radius) as i32, (y + height - 1.0 - radius) as i32), // 左下
|
||||
];
|
||||
|
||||
let min_x = min_x as i32;
|
||||
let max_x = max_x as i32;
|
||||
let min_y = min_y as i32;
|
||||
let max_y = max_y as i32;
|
||||
let radius_sq = (radius + 0.05) * (radius + 0.05);
|
||||
let radius_int = radius as i32;
|
||||
for py in min_y..=max_y {
|
||||
for px in min_x..=max_x {
|
||||
// 判断是否在“直边区域”(无需检查圆角)
|
||||
if px >= min_x + radius_int && px <= max_x - radius_int {
|
||||
// 在左右圆角之间的竖直带 → 一定在内部
|
||||
self.draw_pixel_at1(px, py);
|
||||
continue;
|
||||
}
|
||||
if py >= min_y + radius_int && py <= max_y - radius_int {
|
||||
// 在上下圆角之间的水平带 → 一定在内部
|
||||
self.draw_pixel_at1(px, py);
|
||||
continue;
|
||||
}
|
||||
|
||||
// 否则,点位于四个角落之一,需检查是否在四分之一圆内
|
||||
let in_corner =
|
||||
// 左上角区域
|
||||
(distance_sq1((px, py), corners[0]) <= radius_sq) ||
|
||||
// 右上角区域
|
||||
(distance_sq1((px, py), corners[1]) <= radius_sq) ||
|
||||
// 右下角区域
|
||||
(distance_sq1((px, py), corners[2]) <= radius_sq) ||
|
||||
// 左下角区域
|
||||
(distance_sq1((px, py), corners[3]) <= radius_sq);
|
||||
|
||||
if in_corner {
|
||||
self.draw_pixel_at1(px, py);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn fill_round_rect1(&mut self, p1: Point, p2: Point, radius: f32) {
|
||||
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_round_rect(x, y, width, height, radius);
|
||||
}
|
||||
|
||||
pub fn round_rect(&mut self, x: f32, y: f32, width: f32, height: f32, radius: f32) {
|
||||
if (width as i32) < 2 * self.line_width
|
||||
|| (height as i32) < 2 * self.line_width
|
||||
@@ -812,18 +900,18 @@ impl MSCanvas {
|
||||
self.round_rect(x, y, width, height, radius);
|
||||
}
|
||||
|
||||
pub fn stroke_rect(&mut self, x: i32, y: i32, width: i32, height: i32) {
|
||||
pub fn rect(&mut self, x: i32, y: i32, width: i32, height: i32) {
|
||||
if width < 2 * self.line_width || height < 2 * self.line_width {
|
||||
self.fill_rect(x, y, width, height);
|
||||
self.fill_rect_foreground_color(x, y, width, height);
|
||||
return;
|
||||
}
|
||||
self.fill_rect(x, y, width, self.line_width);
|
||||
self.fill_rect(x, y + height - self.line_width, width, self.line_width);
|
||||
self.fill_rect(x, y, self.line_width, height);
|
||||
self.fill_rect(x + width - self.line_width, y, self.line_width, height);
|
||||
self.fill_rect_foreground_color(x, y, width, self.line_width);
|
||||
self.fill_rect_foreground_color(x, y + height - self.line_width, width, self.line_width);
|
||||
self.fill_rect_foreground_color(x, y, self.line_width, height);
|
||||
self.fill_rect_foreground_color(x + width - self.line_width, y, self.line_width, height);
|
||||
}
|
||||
|
||||
pub fn stroke_rect1(&mut self, p1: Point, p2: Point) {
|
||||
pub fn rect1(&mut self, p1: Point, p2: Point) {
|
||||
let mut x = p1.x;
|
||||
let mut y = p1.y;
|
||||
let mut width = (p2.x - p1.x);
|
||||
@@ -840,7 +928,7 @@ impl MSCanvas {
|
||||
y += height;
|
||||
height = -height;
|
||||
}
|
||||
self.stroke_rect(x as i32, y as i32, width as i32, height as i32);
|
||||
self.rect(x as i32, y as i32, width as i32, height as i32);
|
||||
}
|
||||
|
||||
pub fn clear(&mut self) {
|
||||
@@ -905,7 +993,7 @@ impl MSCanvas {
|
||||
while t <= 1.0 {
|
||||
let point = self.recursive_bezier(&control_points, t);
|
||||
self.brush_circle(point);
|
||||
t += 0.001;
|
||||
t += 0.0005;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1108,3 +1196,19 @@ fn normalize_radian(radian: f32) -> f32 {
|
||||
|
||||
r
|
||||
}
|
||||
|
||||
#[inline]
|
||||
/// 辅助函数:计算两点间距离的平方(避免开方)
|
||||
fn distance_sq(a: Point, b: Point) -> f32 {
|
||||
let dx = a.x - b.x;
|
||||
let dy = a.y - b.y;
|
||||
dx * dx + dy * dy
|
||||
}
|
||||
|
||||
#[inline]
|
||||
/// 辅助函数:计算两点间距离的平方(避免开方)
|
||||
fn distance_sq1(a: (i32, i32), b: (i32, i32)) -> f32 {
|
||||
let dx = a.0 - b.0;
|
||||
let dy = a.1 - b.1;
|
||||
(dx * dx + dy * dy) as f32
|
||||
}
|
||||
|
||||
142
src/paint.rs
142
src/paint.rs
@@ -246,6 +246,14 @@ impl Config {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
enum ControlState {
|
||||
Zero,
|
||||
One,
|
||||
Two,
|
||||
Three,
|
||||
}
|
||||
|
||||
struct PaintApp {
|
||||
tool_states: [bool; Tool::Count as usize],
|
||||
tool_selected: Tool,
|
||||
@@ -261,11 +269,9 @@ struct PaintApp {
|
||||
end_point: Point,
|
||||
|
||||
/// 贝塞尔曲线控制
|
||||
is_controlling: bool,
|
||||
control_state: ControlState,
|
||||
control_points: Vec<Point>,
|
||||
|
||||
is_path_beginning: bool,
|
||||
|
||||
/// 用于显示的图像句柄缓存
|
||||
/// 每次像素变化后需要重新生成
|
||||
image_handle: image::Handle,
|
||||
@@ -290,7 +296,6 @@ impl PaintApp {
|
||||
let pixels = canvas.get_pixels();
|
||||
let config = Config::default();
|
||||
canvas.set_line_width(config.line_width);
|
||||
canvas.set_background_color(MSColor::GREEN);
|
||||
Self {
|
||||
tool_states: [false; Tool::Count as usize],
|
||||
tool_selected: Tool::Count,
|
||||
@@ -298,9 +303,8 @@ impl PaintApp {
|
||||
is_drawing: false,
|
||||
begin_point: Point::ORIGIN,
|
||||
end_point: Point::ORIGIN,
|
||||
is_controlling: false,
|
||||
control_state: ControlState::Zero,
|
||||
control_points: Vec::with_capacity(2),
|
||||
is_path_beginning: false,
|
||||
image_handle: image::Handle::from_rgba(width as u32, height as u32, pixels),
|
||||
dirty: false,
|
||||
config,
|
||||
@@ -797,46 +801,79 @@ impl PaintApp {
|
||||
|
||||
pub fn update_with_curve(&mut self, message: Message) {
|
||||
match message {
|
||||
Message::MousePressed(pos) => {
|
||||
if self.is_controlling {
|
||||
if self.control_points.len() == 0 {
|
||||
self.canvas.restore_pixels();
|
||||
self.canvas
|
||||
.quadratic_bezier(self.begin_point, self.end_point, pos);
|
||||
self.control_points.push(pos);
|
||||
} else {
|
||||
self.canvas.restore_pixels();
|
||||
self.canvas.cubic_bezier(
|
||||
self.begin_point,
|
||||
self.end_point,
|
||||
self.control_points[0],
|
||||
pos,
|
||||
);
|
||||
self.control_points.push(pos);
|
||||
}
|
||||
self.dirty = true;
|
||||
} else {
|
||||
Message::MousePressed(pos) => match self.control_state {
|
||||
ControlState::Zero => {
|
||||
self.begin_point = pos;
|
||||
self.is_drawing = true;
|
||||
self.canvas.save_pixels();
|
||||
self.begin_point = pos;
|
||||
self.control_state = ControlState::One;
|
||||
}
|
||||
}
|
||||
ControlState::One => {
|
||||
self.is_drawing = true;
|
||||
self.canvas.restore_pixels();
|
||||
self.canvas
|
||||
.quadratic_bezier(self.begin_point, self.end_point, pos);
|
||||
self.dirty = true;
|
||||
self.control_state = ControlState::Two;
|
||||
}
|
||||
ControlState::Two => {
|
||||
self.is_drawing = true;
|
||||
self.canvas.restore_pixels();
|
||||
self.canvas.cubic_bezier(
|
||||
self.begin_point,
|
||||
self.end_point,
|
||||
self.control_points[0],
|
||||
pos,
|
||||
);
|
||||
self.dirty = true;
|
||||
self.control_state = ControlState::Three;
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
Message::MouseReleased(pos) => {
|
||||
if self.control_points.len() == 0 {
|
||||
self.is_drawing = false;
|
||||
self.end_point = pos;
|
||||
self.is_controlling = true;
|
||||
} else if self.control_points.len() == 2 {
|
||||
self.control_points.clear();
|
||||
self.is_controlling = false;
|
||||
self.is_drawing = false;
|
||||
match self.control_state {
|
||||
ControlState::One => {
|
||||
self.end_point = pos;
|
||||
}
|
||||
ControlState::Two => {
|
||||
self.control_points.push(pos);
|
||||
}
|
||||
ControlState::Three => {
|
||||
self.control_state = ControlState::Zero;
|
||||
self.control_points.clear();
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
Message::MouseMoved(pos) => {
|
||||
if self.is_drawing {
|
||||
self.canvas.restore_pixels();
|
||||
self.canvas
|
||||
.draw_line_with_circle_brush(self.begin_point, pos);
|
||||
self.dirty = true;
|
||||
match self.control_state {
|
||||
ControlState::One => {
|
||||
self.canvas.restore_pixels();
|
||||
self.canvas
|
||||
.draw_line_with_circle_brush(self.begin_point, pos);
|
||||
self.dirty = true;
|
||||
}
|
||||
ControlState::Two => {
|
||||
self.canvas.restore_pixels();
|
||||
self.canvas
|
||||
.quadratic_bezier(self.begin_point, self.end_point, pos);
|
||||
self.dirty = true;
|
||||
}
|
||||
ControlState::Three => {
|
||||
self.canvas.restore_pixels();
|
||||
self.canvas.restore_pixels();
|
||||
self.canvas.cubic_bezier(
|
||||
self.begin_point,
|
||||
self.end_point,
|
||||
self.control_points[0],
|
||||
pos,
|
||||
);
|
||||
self.dirty = true;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
@@ -857,7 +894,7 @@ impl PaintApp {
|
||||
Message::MouseMoved(pos) => {
|
||||
if self.is_drawing {
|
||||
self.canvas.restore_pixels();
|
||||
self.canvas.stroke_rect1(self.begin_point, pos);
|
||||
self.canvas.rect1(self.begin_point, pos);
|
||||
self.dirty = true;
|
||||
}
|
||||
}
|
||||
@@ -867,24 +904,27 @@ impl PaintApp {
|
||||
|
||||
pub fn update_with_polygon(&mut self, message: Message) {
|
||||
match message {
|
||||
Message::MousePressed(pos) => {
|
||||
if self.is_path_beginning {
|
||||
Message::MousePressed(pos) => match self.control_state {
|
||||
ControlState::Zero => {
|
||||
self.is_drawing = true;
|
||||
self.begin_point = pos;
|
||||
self.canvas.save_pixels();
|
||||
self.canvas.begin_path();
|
||||
self.canvas.move_to(pos);
|
||||
self.control_state = ControlState::One;
|
||||
}
|
||||
ControlState::One => {
|
||||
self.is_drawing = true;
|
||||
self.canvas.restore_pixels();
|
||||
self.canvas.stroke();
|
||||
self.canvas
|
||||
.draw_line_with_circle_brush(self.begin_point, pos);
|
||||
self.dirty = true;
|
||||
} else {
|
||||
self.is_path_beginning = true;
|
||||
self.is_drawing = true;
|
||||
self.canvas.save_pixels();
|
||||
self.begin_point = pos;
|
||||
self.canvas.begin_path();
|
||||
self.canvas.move_to(pos);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
Message::MouseReleased(pos) => {
|
||||
self.is_drawing = false;
|
||||
self.canvas.line_to(pos);
|
||||
self.begin_point = pos;
|
||||
}
|
||||
@@ -901,7 +941,7 @@ impl PaintApp {
|
||||
self.canvas.close_path();
|
||||
self.canvas.stroke();
|
||||
self.is_drawing = false;
|
||||
self.is_path_beginning = false;
|
||||
self.control_state = ControlState::Zero;
|
||||
self.dirty = true;
|
||||
}
|
||||
_ => {}
|
||||
@@ -978,7 +1018,7 @@ impl PaintApp {
|
||||
self.tool_selected = idx.into();
|
||||
|
||||
self.is_drawing = false;
|
||||
self.is_path_beginning = false;
|
||||
self.control_state = ControlState::Zero;
|
||||
}
|
||||
|
||||
/// 将原始字节转换为 Iced 的图像句柄
|
||||
|
||||
Reference in New Issue
Block a user