mirror of
https://github.com/allankoechke/slint-paint-app.git
synced 2026-11-30 10:58:20 +00:00
196 lines
6.9 KiB
Plaintext
196 lines
6.9 KiB
Plaintext
import { Button } from "std-widgets.slint";
|
|
|
|
// Represent an x,y point on canvas
|
|
export struct Point {
|
|
x: length,
|
|
y: length,
|
|
}
|
|
|
|
// Represent a shape type enumeration
|
|
export enum CanvasShapeType { Select, Dot, Line, Rectangle, Circle, Oval, Arc, Polygon }
|
|
|
|
// Represent a shape and its related data
|
|
export struct CanvasShape {
|
|
type: CanvasShapeType,
|
|
segments: [Point],
|
|
stroke_color: color,
|
|
fill_color: color,
|
|
stroke_width: length,
|
|
is_closed: bool,
|
|
}
|
|
|
|
component CanvasButton inherits Rectangle {
|
|
in property <string> id;
|
|
in property <string> label;
|
|
in-out property <bool> toggled: false;
|
|
in property <bool> enabled: true;
|
|
|
|
callback clicked <=> ta.clicked;
|
|
|
|
background: enabled ? (toggled ? #cccccc : #ffffff) : #f0f0f0;
|
|
border-radius: 5px;
|
|
|
|
ta := TouchArea {
|
|
clicked => { if enabled { toggled = !toggled; } }
|
|
}
|
|
|
|
Text {
|
|
text: label;
|
|
color: enabled ? toggled ? #000000 : #888888 : #cccccc;
|
|
horizontal-alignment: center;
|
|
vertical-alignment: center;
|
|
font-weight: enabled && toggled ? FontWeight.bold : FontWeight.normal;
|
|
}
|
|
}
|
|
|
|
export component MainWindow inherits Window {
|
|
width: 720px;
|
|
height: 480px;
|
|
|
|
// Canvas model
|
|
property <[CanvasShape]> shapes: [];
|
|
property <CanvasShapeType> current-shape-type: CanvasShapeType.Select;
|
|
property <bool> tools-enabled: true;
|
|
|
|
// Build the path commands for a given shape
|
|
pure callback build_commands(CanvasShape) -> string;
|
|
pure callback should-end-drawing(CanvasShapeType, int) -> bool;
|
|
|
|
// get bounding box min and max positions
|
|
pure callback bounding_box_min_pos(CanvasShape) -> Point;
|
|
pure callback bounding_box_max_pos(CanvasShape) -> Point;
|
|
|
|
VerticalLayout {
|
|
topbar := Rectangle {
|
|
background: #ffffff;
|
|
height: 50px;
|
|
border-width: 1px;
|
|
|
|
HorizontalLayout {
|
|
spacing: 10px;
|
|
padding: 5px;
|
|
|
|
CanvasButton {
|
|
label: "Select";
|
|
toggled: root.current-shape-type == CanvasShapeType.Select;
|
|
clicked => { root.current-shape-type = CanvasShapeType.Select; }
|
|
enabled: root.tools-enabled;
|
|
}
|
|
|
|
CanvasButton {
|
|
label: "Dot";
|
|
toggled: root.current-shape-type == CanvasShapeType.Dot;
|
|
clicked => { root.current-shape-type = CanvasShapeType.Dot; }
|
|
enabled: root.tools-enabled;
|
|
}
|
|
|
|
CanvasButton {
|
|
label: "Line";
|
|
toggled: root.current-shape-type == CanvasShapeType.Line;
|
|
clicked => { root.current-shape-type = CanvasShapeType.Line; }
|
|
enabled: root.tools-enabled;
|
|
}
|
|
|
|
CanvasButton {
|
|
label: "Rectangle";
|
|
toggled: root.current-shape-type == CanvasShapeType.Rectangle;
|
|
clicked => { root.current-shape-type = CanvasShapeType.Rectangle; }
|
|
enabled: root.tools-enabled;
|
|
}
|
|
}
|
|
}
|
|
|
|
canvas := Rectangle {
|
|
background: #ffffff;
|
|
|
|
property <[Point]> drawing-pts: [];
|
|
property <bool> is-drawing: false;
|
|
|
|
cta := TouchArea {
|
|
clicked => {
|
|
// Start drawing a new line or end current line
|
|
if (!is-drawing) {
|
|
drawing-pts = [];
|
|
|
|
if root.current-shape-type == CanvasShapeType.Select {
|
|
// Do not modify drawing state in selection mode
|
|
return;
|
|
}
|
|
is-drawing = true;
|
|
drawing-pts.push({ x: self.mouse-x, y: self.mouse-y });
|
|
if (should-end-drawing(root.current-shape-type, drawing-pts.length)) {
|
|
is-drawing = false;
|
|
root.shapes.push({
|
|
type: root.current-shape-type,
|
|
segments: drawing-pts,
|
|
stroke_color: #000000,
|
|
fill_color: #ffffff,
|
|
stroke_width: 2px,
|
|
is_closed: false
|
|
});
|
|
}
|
|
} else {
|
|
drawing-pts.push({ x: self.mouse-x, y: self.mouse-y });
|
|
if (should-end-drawing(root.current-shape-type, drawing-pts.length)) {
|
|
is-drawing = false;
|
|
root.shapes.push({
|
|
type: root.current-shape-type,
|
|
segments: drawing-pts,
|
|
stroke_color: #000000,
|
|
fill_color: #ffffff,
|
|
stroke_width: 2px,
|
|
is_closed: false
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
for shape in root.shapes: Rectangle {
|
|
// Bounding box for the shape
|
|
x: self.bounding-box-min.x;
|
|
y: self.bounding-box-min.y;
|
|
|
|
border-width: is-selected || is-drawing ? 1px : 0;
|
|
border-color: #ccc;
|
|
|
|
width: self.bounding-box-max.x - self.bounding-box-min.x;
|
|
height: self.bounding-box-max.y - self.bounding-box-min.y;
|
|
|
|
property <bool> is-selected: false;
|
|
property <Point> bounding-box-min: root.bounding_box_min_pos(shape);
|
|
property <Point> bounding-box-max: root.bounding_box_max_pos(shape);
|
|
|
|
p := Path {
|
|
property <string> path-commands: root.build_commands(shape);
|
|
|
|
// Calculate the bounding box for the shape to determine its position and size
|
|
x: 0px;
|
|
y: 0px;
|
|
|
|
width: 100%;
|
|
height: 100%;
|
|
stroke: shape.stroke_color;
|
|
stroke-width: shape.stroke_width;
|
|
commands: path-commands;
|
|
|
|
changed path-commands => {
|
|
// Log the built path commands for debugging
|
|
debug("Built path commands: " + root.build_commands(shape));
|
|
}
|
|
}
|
|
|
|
// Draw all points
|
|
for pt in shape.segments: Rectangle {
|
|
x: pt.x;
|
|
y: pt.y;
|
|
width: 6px; // shape.stroke-width;
|
|
height: self.width;
|
|
background: shape.stroke-color;
|
|
border-radius: 3px; //shape.stroke-width/2.0;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|