Compare commits

...

6 Commits

Author SHA1 Message Date
2e2c140d5b applyPropertyPatch 2026-03-03 20:46:23 +03:00
129206ce4f Очистка 2026-03-03 20:39:01 +03:00
446cd80616 points теперь слайс 2026-03-03 20:38:57 +03:00
9a795c22f1 Изменён лимит частоты перерисовки 2026-03-03 20:10:30 +03:00
84c9a55ee5 refactor: Удалена нереализованная фигура "Дуга"
Полностью удалены модель, инструменты, рендеринг и связанные UI-элементы для фигуры "Дуга", поскольку она не была реализована в системе.
Также обновлены иконки для инструментов "Линия" и "Ломаная линия".
2026-03-03 20:07:03 +03:00
4bb98f1f41 заливка круга и closed 2026-03-03 19:59:50 +03:00
18 changed files with 107 additions and 261 deletions

View File

@@ -116,6 +116,7 @@ fn redraw(self: *Canvas) !void {
} }
self._last_redraw_time_ms = std.time.milliTimestamp(); self._last_redraw_time_ms = std.time.milliTimestamp();
self.frame_index += 1; self.frame_index += 1;
self.redraw_throttle_ms = @max(1, @as(u32, @intCast(self.render_engine.getStats().render_time_ns / std.time.ns_per_ms / 3)));
} }
pub fn exampleReset(self: *Canvas) !void { pub fn exampleReset(self: *Canvas) !void {

View File

@@ -1,9 +1,8 @@
const dvui = @import("dvui"); const dvui = @import("dvui");
pub const line = dvui.entypo.line_graph; pub const line = dvui.entypo.flow_line;
pub const ellipse = dvui.entypo.circle; pub const ellipse = dvui.entypo.circle;
pub const arc = dvui.entypo.loop; pub const broken = dvui.entypo.line_graph;
pub const broken = dvui.entypo.flow_line;
pub const trash = dvui.entypo.trash; pub const trash = dvui.entypo.trash;
pub const cross = dvui.entypo.cross; pub const cross = dvui.entypo.cross;
pub const plus = dvui.entypo.plus; pub const plus = dvui.entypo.plus;

View File

@@ -6,7 +6,6 @@ const Object = @This();
pub const ShapeKind = enum { pub const ShapeKind = enum {
line, line,
ellipse, ellipse,
arc,
broken, broken,
}; };
@@ -45,7 +44,7 @@ pub fn getProperty(self: Object, tag: std.meta.Tag(PropertyData)) ?*const Proper
pub fn setProperty(self: *Object, allocator: std.mem.Allocator, prop: Property) !void { pub fn setProperty(self: *Object, allocator: std.mem.Allocator, prop: Property) !void {
for (self.properties.items, 0..) |*p, i| { for (self.properties.items, 0..) |*p, i| {
if (std.meta.activeTag(p.data) == std.meta.activeTag(prop.data)) { if (std.meta.activeTag(p.data) == std.meta.activeTag(prop.data)) {
if (p.data == .points) p.data.points.deinit(allocator); if (p.data == .points) allocator.free(p.data.points);
self.properties.items[i] = prop; self.properties.items[i] = prop;
return; return;
} }

View File

@@ -19,7 +19,8 @@ pub const Data = union(enum) {
arc_percent: f32, arc_percent: f32,
end_point: Point2_f, end_point: Point2_f,
points: std.ArrayList(Point2_f), /// Владеет памятью; при deinit/clone — free/duplicate.
points: []const Point2_f,
/// Замкнутый контур (для ломаной: отрезок последняя–первая точка + заливка). /// Замкнутый контур (для ломаной: отрезок последняя–первая точка + заливка).
closed: bool, closed: bool,
@@ -39,7 +40,7 @@ pub const Property = struct {
pub fn deinit(self: *Property, allocator: std.mem.Allocator) void { pub fn deinit(self: *Property, allocator: std.mem.Allocator) void {
switch (self.data) { switch (self.data) {
.points => |*list| list.deinit(allocator), .points => |slice| allocator.free(slice),
else => {}, else => {},
} }
self.* = undefined; self.* = undefined;
@@ -47,9 +48,13 @@ pub const Property = struct {
pub fn clone(self: Property, allocator: std.mem.Allocator) !Property { pub fn clone(self: Property, allocator: std.mem.Allocator) !Property {
return switch (self.data) { return switch (self.data) {
.points => |list| .{ .points => |slice| .{
.data = .{ .data = .{
.points = try list.clone(allocator), .points = blk: {
const copy = try allocator.alloc(Point2_f, slice.len);
@memcpy(copy, slice);
break :blk copy;
},
}, },
}, },
else => .{ .data = self.data }, else => .{ .data = self.data },

View File

@@ -1,23 +0,0 @@
const std = @import("std");
const Object = @import("../Object.zig");
const PropertyData = @import("../Property.zig").Data;
const Rect_f = @import("../basic_models.zig").Rect_f;
const shape_mod = @import("shape.zig");
/// Теги обязательных свойств (arc не реализован).
pub fn getRequiredTags() []const std.meta.Tag(PropertyData) {
return &[_]std.meta.Tag(PropertyData){};
}
/// Добавляет свойства по умолчанию для дуги.
pub fn appendDefaultShapeProperties(allocator: std.mem.Allocator, obj: *Object) !void {
_ = allocator;
_ = obj;
return error.ArcNotImplemented;
}
/// Локальные границы дуги (не реализовано).
pub fn getLocalBounds(obj: *const Object) !Rect_f {
try shape_mod.ensure(obj, .arc);
return error.ArcNotImplemented;
}

View File

@@ -6,49 +6,15 @@ const Point2_f = @import("../basic_models.zig").Point2_f;
const Rect_f = @import("../basic_models.zig").Rect_f; const Rect_f = @import("../basic_models.zig").Rect_f;
const shape_mod = @import("shape.zig"); const shape_mod = @import("shape.zig");
/// Точки ломаной по умолчанию. /// Свойства фигуры по умолчанию (добавляются к общим). points — слайс на статический массив.
pub const default_points = [_]Point2_f{ pub const default_shape_properties_points = [_]Point2_f{
.{ .x = 0, .y = 0 }, .{ .x = 0, .y = 0 },
.{ .x = 80, .y = 0 }, .{ .x = 80, .y = 0 },
.{ .x = 80, .y = 60 }, .{ .x = 80, .y = 60 },
}; };
pub const default_shape_properties = [_]Property{
/// Теги обязательных свойств. .{ .data = .{ .points = &default_shape_properties_points } },
pub fn getRequiredTags() []const std.meta.Tag(PropertyData) { .{ .data = .{ .closed = false } },
return &[_]std.meta.Tag(PropertyData){ .{ .data = .{ .filled = true } },
.points, .{ .data = .{ .fill_rgba = 0x000000FF } },
}; };
}
/// Добавляет к объекту свойства по умолчанию для ломаной.
pub fn appendDefaultShapeProperties(allocator: std.mem.Allocator, obj: *Object) !void {
var points = std.ArrayList(Point2_f).empty;
try points.appendSlice(allocator, &default_points);
try obj.properties.append(allocator, .{ .data = .{ .points = points } });
try obj.properties.append(allocator, .{ .data = .{ .closed = false } });
try obj.properties.append(allocator, .{ .data = .{ .filled = true } });
try obj.properties.append(allocator, .{ .data = .{ .fill_rgba = obj.getProperty(.stroke_rgba).?.stroke_rgba } });
}
/// Локальные границы: AABB по точкам.
pub fn getLocalBounds(obj: *const Object) !Rect_f {
try shape_mod.ensure(obj, .broken);
const p = obj.getProperty(.points).?;
if (p.points.items.len == 0) return error.EmptyPoints;
var min_x: f32 = p.points.items[0].x;
var max_x: f32 = min_x;
var min_y: f32 = p.points.items[0].y;
var max_y: f32 = min_y;
for (p.points.items[1..]) |pt| {
min_x = @min(min_x, pt.x);
max_x = @max(max_x, pt.x);
min_y = @min(min_y, pt.y);
max_y = @max(max_y, pt.y);
}
return .{
.x = min_x,
.y = min_y,
.w = max_x - min_x,
.h = max_y - min_y,
};
}

View File

@@ -1,34 +1,14 @@
const std = @import("std"); const std = @import("std");
const Object = @import("../Object.zig"); const Object = @import("../Object.zig");
const Property = @import("../Property.zig").Property; const Property = @import("../Property.zig").Property;
const PropertyData = @import("../Property.zig").Data;
const Rect_f = @import("../basic_models.zig").Rect_f; const Rect_f = @import("../basic_models.zig").Rect_f;
const shape_mod = @import("shape.zig"); const shape_mod = @import("shape.zig");
/// Свойства фигуры по умолчанию. /// Свойства фигуры по умолчанию (добавляются к общим).
pub const default_shape_properties = [_]Property{ pub const default_shape_properties = [_]Property{
.{ .data = .{ .radii = .{ .x = 50, .y = 50 } } }, .{ .data = .{ .radii = .{ .x = 50, .y = 50 } } },
.{ .data = .{ .arc_percent = 100.0 } }, .{ .data = .{ .arc_percent = 100.0 } },
.{ .data = .{ .closed = true } },
.{ .data = .{ .filled = false } },
.{ .data = .{ .fill_rgba = 0x000000FF } },
}; };
/// Теги обязательных свойств.
pub fn getRequiredTags() []const std.meta.Tag(PropertyData) {
return &([_]std.meta.Tag(PropertyData){std.meta.activeTag(default_shape_properties[0].data)});
}
/// Добавляет к объекту свойства по умолчанию для эллипса.
pub fn appendDefaultShapeProperties(allocator: std.mem.Allocator, obj: *Object) !void {
for (default_shape_properties) |prop| try obj.properties.append(allocator, prop);
}
/// Локальные границы эллипса: [-radii.x, -radii.y] .. [radii.x, radii.y].
pub fn getLocalBounds(obj: *const Object) !Rect_f {
try shape_mod.ensure(obj, .ellipse);
const r = obj.getProperty(.radii).?;
return .{
.x = -r.radii.x,
.y = -r.radii.y,
.w = 2 * r.radii.x,
.h = 2 * r.radii.y,
};
}

View File

@@ -1,37 +1,10 @@
const std = @import("std"); const std = @import("std");
const Object = @import("../Object.zig"); const Object = @import("../Object.zig");
const Property = @import("../Property.zig").Property; const Property = @import("../Property.zig").Property;
const PropertyData = @import("../Property.zig").Data;
const Rect_f = @import("../basic_models.zig").Rect_f; const Rect_f = @import("../basic_models.zig").Rect_f;
const shape_mod = @import("shape.zig"); const shape_mod = @import("shape.zig");
/// Свойства фигуры по умолчанию. /// Свойства фигуры по умолчанию (добавляются к общим).
pub const default_shape_properties = [_]Property{ pub const default_shape_properties = [_]Property{
.{ .data = .{ .end_point = .{ .x = 100, .y = 200 } } }, .{ .data = .{ .end_point = .{ .x = 100, .y = 200 } } },
}; };
/// Теги обязательных свойств.
pub fn getRequiredTags() []const std.meta.Tag(PropertyData) {
return &([_]std.meta.Tag(PropertyData){std.meta.activeTag(default_shape_properties[0].data)});
}
/// Добавляет к объекту свойства по умолчанию для линии.
pub fn appendDefaultShapeProperties(allocator: std.mem.Allocator, obj: *Object) !void {
for (default_shape_properties) |prop| try obj.properties.append(allocator, prop);
}
/// Локальные границы: от (0,0) до end_point.
pub fn getLocalBounds(obj: *const Object) !Rect_f {
try shape_mod.ensure(obj, .line);
const ep = obj.getProperty(.end_point).?;
const min_x = @min(0, ep.end_point.x);
const max_x = @max(0, ep.end_point.x);
const min_y = @min(0, ep.end_point.y);
const max_y = @max(0, ep.end_point.y);
return .{
.x = min_x,
.y = min_y,
.w = max_x - min_x,
.h = max_y - min_y,
};
}

View File

@@ -4,22 +4,37 @@ const Property = @import("../Property.zig").Property;
const PropertyData = @import("../Property.zig").Data; const PropertyData = @import("../Property.zig").Data;
const defaultCommonProperties = Object.defaultCommonProperties; const defaultCommonProperties = Object.defaultCommonProperties;
const basic_models = @import("../basic_models.zig"); const basic_models = @import("../basic_models.zig");
const Point2_f = basic_models.Point2_f;
const line = @import("line.zig"); const line = @import("line.zig");
const ellipse = @import("ellipse.zig"); const ellipse = @import("ellipse.zig");
const broken = @import("broken.zig"); const broken = @import("broken.zig");
const arc = @import("arc.zig"); pub const Rect = basic_models.Rect_f;
pub const Rect = basic_models.Rectf; /// Добавляет к объекту список свойств фигуры. Для .points дублирует слайс (объект владеет).
fn appendShapeProperties(allocator: std.mem.Allocator, obj: *Object, props: []const Property) !void {
for (props) |prop| {
if (prop.data == .points) {
const pts = prop.data.points;
const copy = try allocator.alloc(Point2_f, pts.len);
@memcpy(copy, pts);
try obj.properties.append(allocator, .{ .data = .{ .points = copy } });
} else {
try obj.properties.append(allocator, prop);
}
}
}
/// Создаёт объект с дефолтными общими и фигурными свойствами. /// Создаёт объект с дефолтными общими и фигурными свойствами.
pub fn createObject(allocator: std.mem.Allocator, shape_kind: Object.ShapeKind) !Object { pub fn createObject(allocator: std.mem.Allocator, shape_kind: Object.ShapeKind) !Object {
var obj = try createWithCommonProperties(allocator, shape_kind); var obj = try createWithCommonProperties(allocator, shape_kind);
errdefer obj.deinit(allocator); errdefer obj.deinit(allocator);
switch (shape_kind) { switch (shape_kind) {
.line => try line.appendDefaultShapeProperties(allocator, &obj), .line => try appendShapeProperties(allocator, &obj, &line.default_shape_properties),
.ellipse => try ellipse.appendDefaultShapeProperties(allocator, &obj), .ellipse => try appendShapeProperties(allocator, &obj, &ellipse.default_shape_properties),
.broken => try broken.appendDefaultShapeProperties(allocator, &obj), .broken => {
.arc => try arc.appendDefaultShapeProperties(allocator, &obj), try appendShapeProperties(allocator, &obj, &broken.default_shape_properties);
try obj.setProperty(allocator, .{ .data = .{ .fill_rgba = obj.getProperty(.stroke_rgba).?.stroke_rgba } });
},
} }
return obj; return obj;
} }
@@ -35,62 +50,3 @@ fn createWithCommonProperties(allocator: std.mem.Allocator, shape_kind: Object.S
.children = std.ArrayList(Object).empty, .children = std.ArrayList(Object).empty,
}; };
} }
/// Проверяет тип объекта и наличие обязательных свойств.
pub fn ensure(obj: *const Object, expected_kind: Object.ShapeKind) !void {
if (obj.shape != expected_kind) return error.WrongShapeKind;
const tags = requiredTagsFor(expected_kind);
for (tags) |tag| {
if (obj.getProperty(tag) == null) return error.MissingRequiredProperty;
}
}
fn requiredTagsFor(kind: Object.ShapeKind) []const std.meta.Tag(PropertyData) {
return switch (kind) {
.line => line.getRequiredTags(),
.ellipse => ellipse.getRequiredTags(),
.broken => broken.getRequiredTags(),
.arc => arc.getRequiredTags(),
};
}
/// Локальные границы (AABB).
pub fn getLocalBounds(obj: *const Object) !Rect {
return switch (obj.shape) {
.line => line.getLocalBounds(obj),
.ellipse => ellipse.getLocalBounds(obj),
.broken => broken.getLocalBounds(obj),
.arc => arc.getLocalBounds(obj),
};
}
test "getLocalBounds" {
const shape = @This();
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
var line_obj = try shape.createObject(allocator, .line);
defer line_obj.deinit(allocator);
const line_bounds = try getLocalBounds(&line_obj);
try std.testing.expect(line_bounds.x == 0);
try std.testing.expect(line_bounds.y == 0);
try std.testing.expect(line_bounds.w == 100);
try std.testing.expect(line_bounds.h == 0);
var ellipse_obj = try shape.createObject(allocator, .ellipse);
defer ellipse_obj.deinit(allocator);
const ellipse_bounds = try getLocalBounds(&ellipse_obj);
try std.testing.expect(ellipse_bounds.x == -50);
try std.testing.expect(ellipse_bounds.y == -50);
try std.testing.expect(ellipse_bounds.w == 100);
try std.testing.expect(ellipse_bounds.h == 100);
var broken_obj = try shape.createObject(allocator, .broken);
defer broken_obj.deinit(allocator);
const broken_bounds = try getLocalBounds(&broken_obj);
try std.testing.expect(broken_bounds.x == 0);
try std.testing.expect(broken_bounds.y == 0);
try std.testing.expect(broken_bounds.w == 80);
try std.testing.expect(broken_bounds.h == 60);
}

View File

@@ -74,18 +74,20 @@ fn randomizeObjectProperties(allocator: std.mem.Allocator, doc_size: *const Size
} }); } });
}, },
.broken => { .broken => {
var points = std.ArrayList(Point2_f).empty; var list = std.ArrayList(Point2_f).empty;
defer list.deinit(allocator);
const n = rng.intRangeLessThan(usize, 2, 9); const n = rng.intRangeLessThan(usize, 2, 9);
var x: f32 = 0; var x: f32 = 0;
var y: f32 = 0; var y: f32 = 0;
for (0..n) |_| { for (0..n) |_| {
try points.append(allocator, .{ .x = x, .y = y }); try list.append(allocator, .{ .x = x, .y = y });
x += randFloat(rng, -40, 80); x += randFloat(rng, -40, 80);
y += randFloat(rng, -30, 60); y += randFloat(rng, -30, 60);
} }
try obj.setProperty(allocator, .{ .data = .{ .points = points } }); const slice = try allocator.alloc(Point2_f, list.items.len);
@memcpy(slice, list.items);
try obj.setProperty(allocator, .{ .data = .{ .points = slice } });
}, },
.arc => {},
} }
} }

View File

@@ -1,8 +0,0 @@
const Document = @import("../../models/Document.zig");
const pipeline = @import("pipeline.zig");
const DrawContext = pipeline.DrawContext;
const Object = Document.Object;
/// Дуга (не реализовано).
pub fn draw(_: *DrawContext, _: *const Object) void {}

View File

@@ -17,7 +17,7 @@ pub fn draw(
allocator: std.mem.Allocator, allocator: std.mem.Allocator,
) !void { ) !void {
const p_prop = obj.getProperty(.points) orelse return; const p_prop = obj.getProperty(.points) orelse return;
const pts = p_prop.points.items; const pts = p_prop.points;
if (pts.len < 2) return; if (pts.len < 2) return;
const stroke = if (obj.getProperty(.stroke_rgba)) |s| pipeline.rgbaToPma(s.stroke_rgba) else default_stroke; const stroke = if (obj.getProperty(.stroke_rgba)) |s| pipeline.rgbaToPma(s.stroke_rgba) else default_stroke;
const thickness = if (obj.getProperty(.thickness)) |t| t.thickness else default_thickness; const thickness = if (obj.getProperty(.thickness)) |t| t.thickness else default_thickness;

View File

@@ -4,7 +4,6 @@ const pipeline = @import("pipeline.zig");
const line = @import("line.zig"); const line = @import("line.zig");
const ellipse = @import("ellipse.zig"); const ellipse = @import("ellipse.zig");
const broken = @import("broken.zig"); const broken = @import("broken.zig");
const arc = @import("arc.zig");
const basic_models = @import("../../models/basic_models.zig"); const basic_models = @import("../../models/basic_models.zig");
const Rect_i = basic_models.Rect_i; const Rect_i = basic_models.Rect_i;
const Size_i = basic_models.Size_i; const Size_i = basic_models.Size_i;
@@ -32,7 +31,6 @@ fn drawObject(
.line => line.draw(ctx, obj), .line => line.draw(ctx, obj),
.ellipse => try ellipse.draw(ctx, obj, allocator), .ellipse => try ellipse.draw(ctx, obj, allocator),
.broken => try broken.draw(ctx, obj, allocator), .broken => try broken.draw(ctx, obj, allocator),
.arc => arc.draw(ctx, obj),
} }
for (obj.children.items) |*child| { for (obj.children.items) |*child| {

View File

@@ -2,6 +2,7 @@ const std = @import("std");
const std_math = std.math; const std_math = std.math;
const Document = @import("../../models/Document.zig"); const Document = @import("../../models/Document.zig");
const pipeline = @import("pipeline.zig"); const pipeline = @import("pipeline.zig");
const line = @import("line.zig");
const DrawContext = pipeline.DrawContext; const DrawContext = pipeline.DrawContext;
const Color = @import("dvui").Color; const Color = @import("dvui").Color;
const basic_models = @import("../../models/basic_models.zig"); const basic_models = @import("../../models/basic_models.zig");
@@ -9,6 +10,7 @@ const Point2_f = basic_models.Point2_f;
const Object = Document.Object; const Object = Document.Object;
const default_stroke: Color.PMA = .{ .r = 0, .g = 0, .b = 0, .a = 255 }; const default_stroke: Color.PMA = .{ .r = 0, .g = 0, .b = 0, .a = 255 };
const default_fill: Color.PMA = .{ .r = 0, .g = 0, .b = 0, .a = 0 };
const default_thickness: f32 = 2.0; const default_thickness: f32 = 2.0;
/// Эллипс с центром (0,0) и полуосями radii. Обводка — полоса расстояния до контура (чёткая линия, не круги). /// Эллипс с центром (0,0) и полуосями radii. Обводка — полоса расстояния до контура (чёткая линия, не круги).
@@ -23,6 +25,10 @@ pub fn draw(ctx: *DrawContext, obj: *const Object, allocator: std.mem.Allocator)
const stroke = if (obj.getProperty(.stroke_rgba)) |s| pipeline.rgbaToPma(s.stroke_rgba) else default_stroke; const stroke = if (obj.getProperty(.stroke_rgba)) |s| pipeline.rgbaToPma(s.stroke_rgba) else default_stroke;
const thickness = if (obj.getProperty(.thickness)) |t| t.thickness else default_thickness; const thickness = if (obj.getProperty(.thickness)) |t| t.thickness else default_thickness;
const arc_percent = std_math.clamp(if (obj.getProperty(.arc_percent)) |p| p.arc_percent else 100.0, 0.0, 100.0); const arc_percent = std_math.clamp(if (obj.getProperty(.arc_percent)) |p| p.arc_percent else 100.0, 0.0, 100.0);
const closed = if (obj.getProperty(.closed)) |c| c.closed else true;
const filled = if (obj.getProperty(.filled)) |f| f.filled else false;
const fill_color = if (obj.getProperty(.fill_rgba)) |f| pipeline.rgbaToPma(f.fill_rgba) else default_fill;
const do_fill = filled and (closed or arc_percent >= 100.0);
const t = &ctx.transform; const t = &ctx.transform;
const min_r = @min(rx, ry); const min_r = @min(rx, ry);
@@ -66,6 +72,10 @@ pub fn draw(ctx: *DrawContext, obj: *const Object, allocator: std.mem.Allocator)
stroke_ctx.pixels = buffer; stroke_ctx.pixels = buffer;
stroke_ctx.replace_mode = true; stroke_ctx.replace_mode = true;
if (do_fill) {
stroke_ctx.startFill(allocator) catch return;
}
const inv_rx = 1.0 / rx; const inv_rx = 1.0 / rx;
const inv_ry = 1.0 / ry; const inv_ry = 1.0 / ry;
const arc_len = 2.0 * std_math.pi * arc_percent / 100.0; const arc_len = 2.0 * std_math.pi * arc_percent / 100.0;
@@ -91,5 +101,16 @@ pub fn draw(ctx: *DrawContext, obj: *const Object, allocator: std.mem.Allocator)
} }
} }
if (closed and arc_percent < 100.0) {
const end_x = rx * std_math.sin(arc_len);
const end_y = ry * std_math.cos(arc_len);
line.drawLine(&stroke_ctx, 0, 0, 0, ry, stroke, thickness, false);
line.drawLine(&stroke_ctx, 0, 0, end_x, end_y, stroke, thickness, false);
}
if (do_fill) {
stroke_ctx.stopFill(allocator, fill_color);
}
ctx.compositeDrawerContext(&stroke_ctx, t.opacity); ctx.compositeDrawerContext(&stroke_ctx, t.opacity);
} }

View File

@@ -1,7 +1,6 @@
const Toolbar = @import("Toolbar.zig"); const Toolbar = @import("Toolbar.zig");
const line = @import("tools/line.zig"); const line = @import("tools/line.zig");
const ellipse = @import("tools/ellipse.zig"); const ellipse = @import("tools/ellipse.zig");
const arc = @import("tools/arc.zig");
const broken = @import("tools/broken.zig"); const broken = @import("tools/broken.zig");
const icons = @import("../icons.zig"); const icons = @import("../icons.zig");
@@ -16,11 +15,6 @@ pub const default_tools = [_]Toolbar.ToolDescriptor{
.icon_tvg = icons.ellipse, .icon_tvg = icons.ellipse,
.implementation = &ellipse.tool, .implementation = &ellipse.tool,
}, },
.{
.name = "Arc",
.icon_tvg = icons.arc,
.implementation = &arc.tool,
},
.{ .{
.name = "Broken line", .name = "Broken line",
.icon_tvg = icons.broken, .icon_tvg = icons.broken,

View File

@@ -1,10 +0,0 @@
const Tool = @import("../Tool.zig");
const shape = @import("../../models/shape/shape.zig");
fn onCanvasClick(ctx: *const Tool.ToolContext) !void {
const canvas = ctx.canvas;
var obj = shape.createObject(canvas.allocator, .arc) catch return;
defer obj.deinit(canvas.allocator);
try ctx.addObject(obj);
}
pub const tool = Tool.Tool{ .onCanvasClick = onCanvasClick };

View File

@@ -6,6 +6,7 @@ const Document = @import("../models/Document.zig");
const Property = @import("../models/Property.zig").Property; const Property = @import("../models/Property.zig").Property;
const PropertyData = @import("../models/Property.zig").Data; const PropertyData = @import("../models/Property.zig").Data;
const Rect_i = @import("../models/basic_models.zig").Rect_i; const Rect_i = @import("../models/basic_models.zig").Rect_i;
const Point2_f = @import("../models/basic_models.zig").Point2_f;
const Tool = @import("../toolbar/Tool.zig"); const Tool = @import("../toolbar/Tool.zig");
const RenderStats = @import("../render/RenderStats.zig"); const RenderStats = @import("../render/RenderStats.zig");
const icons = @import("../icons.zig"); const icons = @import("../icons.zig");
@@ -379,6 +380,11 @@ fn drawStatsPanel(stats: RenderStats, frame_index: u64) void {
panel.deinit(); panel.deinit();
} }
fn applyPropertyPatch(canvas: *Canvas, obj: *Document.Object, patch: Property) void {
obj.setProperty(canvas.allocator, patch) catch {};
canvas.requestRedraw();
}
fn drawPropertyEditor(canvas: *Canvas, obj: *Document.Object, prop: *const Property, row_index: usize) void { fn drawPropertyEditor(canvas: *Canvas, obj: *Document.Object, prop: *const Property, row_index: usize) void {
const row_id: usize = row_index * 16; const row_id: usize = row_index * 16;
const is_even = row_index % 2 == 0; const is_even = row_index % 2 == 0;
@@ -420,8 +426,7 @@ fn drawPropertyEditor(canvas: *Canvas, obj: *Document.Object, prop: *const Prope
changed = res.changed or changed; changed = res.changed or changed;
} }
if (changed) { if (changed) {
obj.setProperty(canvas.allocator, .{ .data = .{ .position = next } }) catch {}; applyPropertyPatch(canvas, obj, .{ .data = .{ .position = next } });
canvas.requestRedraw();
} }
}, },
.angle => |angle| { .angle => |angle| {
@@ -434,8 +439,7 @@ fn drawPropertyEditor(canvas: *Canvas, obj: *Document.Object, prop: *const Prope
subrow.deinit(); subrow.deinit();
if (res.changed) { if (res.changed) {
next = degrees * std.math.pi / 180.0; next = degrees * std.math.pi / 180.0;
obj.setProperty(canvas.allocator, .{ .data = .{ .angle = next } }) catch {}; applyPropertyPatch(canvas, obj, .{ .data = .{ .angle = next } });
canvas.requestRedraw();
} }
} }
}, },
@@ -459,29 +463,25 @@ fn drawPropertyEditor(canvas: *Canvas, obj: *Document.Object, prop: *const Prope
changed = res.changed or changed; changed = res.changed or changed;
} }
if (changed) { if (changed) {
obj.setProperty(canvas.allocator, .{ .data = .{ .scale = next } }) catch {}; applyPropertyPatch(canvas, obj, .{ .data = .{ .scale = next } });
canvas.requestRedraw();
} }
}, },
.visible => |v| { .visible => |v| {
var next = v; var next = v;
if (dvui.checkbox(@src(), &next, "Visible", .{})) { if (dvui.checkbox(@src(), &next, "Visible", .{})) {
obj.setProperty(canvas.allocator, .{ .data = .{ .visible = next } }) catch {}; applyPropertyPatch(canvas, obj, .{ .data = .{ .visible = next } });
canvas.requestRedraw();
} }
}, },
.opacity => |opacity| { .opacity => |opacity| {
var next = opacity; var next = opacity;
if (dvui.sliderEntry(@src(), "{d:0.2}", .{ .value = &next, .min = 0.0, .max = 1.0, .interval = 0.01 }, .{ .expand = .horizontal })) { if (dvui.sliderEntry(@src(), "{d:0.2}", .{ .value = &next, .min = 0.0, .max = 1.0, .interval = 0.01 }, .{ .expand = .horizontal })) {
obj.setProperty(canvas.allocator, .{ .data = .{ .opacity = next } }) catch {}; applyPropertyPatch(canvas, obj, .{ .data = .{ .opacity = next } });
canvas.requestRedraw();
} }
}, },
.locked => |v| { .locked => |v| {
var next = v; var next = v;
if (dvui.checkbox(@src(), &next, "Locked", .{})) { if (dvui.checkbox(@src(), &next, "Locked", .{})) {
obj.setProperty(canvas.allocator, .{ .data = .{ .locked = next } }) catch {}; applyPropertyPatch(canvas, obj, .{ .data = .{ .locked = next } });
canvas.requestRedraw();
} }
}, },
.size => |size| { .size => |size| {
@@ -504,8 +504,7 @@ fn drawPropertyEditor(canvas: *Canvas, obj: *Document.Object, prop: *const Prope
changed = res.changed or changed; changed = res.changed or changed;
} }
if (changed) { if (changed) {
obj.setProperty(canvas.allocator, .{ .data = .{ .size = next } }) catch {}; applyPropertyPatch(canvas, obj, .{ .data = .{ .size = next } });
canvas.requestRedraw();
} }
}, },
.radii => |radii| { .radii => |radii| {
@@ -528,15 +527,13 @@ fn drawPropertyEditor(canvas: *Canvas, obj: *Document.Object, prop: *const Prope
changed = res.changed or changed; changed = res.changed or changed;
} }
if (changed) { if (changed) {
obj.setProperty(canvas.allocator, .{ .data = .{ .radii = next } }) catch {}; applyPropertyPatch(canvas, obj, .{ .data = .{ .radii = next } });
canvas.requestRedraw();
} }
}, },
.arc_percent => |pct| { .arc_percent => |pct| {
var next = pct; var next = pct;
if (dvui.sliderEntry(@src(), "{d:0.0}%", .{ .value = &next, .min = 0.0, .max = 100.0, .interval = 1.0 }, .{ .expand = .horizontal })) { if (dvui.sliderEntry(@src(), "{d:0.0}%", .{ .value = &next, .min = 0.0, .max = 100.0, .interval = 1.0 }, .{ .expand = .horizontal })) {
obj.setProperty(canvas.allocator, .{ .data = .{ .arc_percent = next } }) catch {}; applyPropertyPatch(canvas, obj, .{ .data = .{ .arc_percent = next } });
canvas.requestRedraw();
} }
}, },
.end_point => |pt| { .end_point => |pt| {
@@ -559,15 +556,16 @@ fn drawPropertyEditor(canvas: *Canvas, obj: *Document.Object, prop: *const Prope
changed = res.changed or changed; changed = res.changed or changed;
} }
if (changed) { if (changed) {
obj.setProperty(canvas.allocator, .{ .data = .{ .end_point = next } }) catch {}; applyPropertyPatch(canvas, obj, .{ .data = .{ .end_point = next } });
canvas.requestRedraw();
} }
}, },
.points => |points| { .points => |points| {
var list = points.clone(canvas.allocator) catch { var list = std.ArrayList(Point2_f).empty;
dvui.label(@src(), "Points: {d}", .{points.items.len}, .{}); list.appendSlice(canvas.allocator, points) catch {
dvui.label(@src(), "Points: {d}", .{points.len}, .{});
return; return;
}; };
defer list.deinit(canvas.allocator);
dvui.label(@src(), "Points: {d}", .{list.items.len}, .{}); dvui.label(@src(), "Points: {d}", .{list.items.len}, .{});
var changed = false; var changed = false;
@@ -677,13 +675,13 @@ fn drawPropertyEditor(canvas: *Canvas, obj: *Document.Object, prop: *const Prope
} }
if (changed) { if (changed) {
obj.setProperty(canvas.allocator, .{ .data = .{ .points = list } }) catch { const slice = canvas.allocator.alloc(Point2_f, list.items.len) catch return;
list.deinit(canvas.allocator); @memcpy(slice, list.items);
obj.setProperty(canvas.allocator, .{ .data = .{ .points = slice } }) catch {
canvas.allocator.free(slice);
return; return;
}; };
canvas.requestRedraw(); canvas.requestRedraw();
} else {
list.deinit(canvas.allocator);
} }
}, },
.fill_rgba => |rgba| { .fill_rgba => |rgba| {
@@ -701,23 +699,20 @@ fn drawPropertyEditor(canvas: *Canvas, obj: *Document.Object, prop: *const Prope
const res = dvui.textEntryNumber(@src(), T, .{ .value = &next, .min = @as(T, 0.0), .max = @as(T, 100.0) }, .{ .expand = .horizontal }); const res = dvui.textEntryNumber(@src(), T, .{ .value = &next, .min = @as(T, 0.0), .max = @as(T, 100.0) }, .{ .expand = .horizontal });
subrow.deinit(); subrow.deinit();
if (res.changed) { if (res.changed) {
obj.setProperty(canvas.allocator, .{ .data = .{ .thickness = next } }) catch {}; applyPropertyPatch(canvas, obj, .{ .data = .{ .thickness = next } });
canvas.requestRedraw();
} }
} }
}, },
.closed => |v| { .closed => |v| {
var next = v; var next = v;
if (dvui.checkbox(@src(), &next, "Closed", .{})) { if (dvui.checkbox(@src(), &next, "Closed", .{})) {
obj.setProperty(canvas.allocator, .{ .data = .{ .closed = next } }) catch {}; applyPropertyPatch(canvas, obj, .{ .data = .{ .closed = next } });
canvas.requestRedraw();
} }
}, },
.filled => |v| { .filled => |v| {
var next = v; var next = v;
if (dvui.checkbox(@src(), &next, "Filled", .{})) { if (dvui.checkbox(@src(), &next, "Filled", .{})) {
obj.setProperty(canvas.allocator, .{ .data = .{ .filled = next } }) catch {}; applyPropertyPatch(canvas, obj, .{ .data = .{ .filled = next } });
canvas.requestRedraw();
} }
}, },
} }
@@ -733,12 +728,11 @@ fn drawColorEditor(canvas: *Canvas, obj: *Document.Object, rgba: u32, is_fill: b
.{ .expand = .horizontal }, .{ .expand = .horizontal },
)) { )) {
const next = colorToRgba(hsv.toColor()); const next = colorToRgba(hsv.toColor());
if (is_fill) { const patch: Property = if (is_fill)
obj.setProperty(canvas.allocator, .{ .data = .{ .fill_rgba = next } }) catch {}; .{ .data = .{ .fill_rgba = next } }
} else { else
obj.setProperty(canvas.allocator, .{ .data = .{ .stroke_rgba = next } }) catch {}; .{ .data = .{ .stroke_rgba = next } };
} applyPropertyPatch(canvas, obj, patch);
canvas.requestRedraw();
} }
} }

View File

@@ -19,7 +19,6 @@ fn shapeLabel(shape: Object.ShapeKind) []const u8 {
return switch (shape) { return switch (shape) {
.line => "Line", .line => "Line",
.ellipse => "Ellipse", .ellipse => "Ellipse",
.arc => "Arc",
.broken => "Broken line", .broken => "Broken line",
}; };
} }