points теперь слайс

This commit is contained in:
2026-03-03 20:38:57 +03:00
parent 9a795c22f1
commit 446cd80616
10 changed files with 86 additions and 67 deletions

View File

@@ -44,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

@@ -4,42 +4,35 @@ const Property = @import("../Property.zig").Property;
const PropertyData = @import("../Property.zig").Data; const PropertyData = @import("../Property.zig").Data;
const Point2_f = @import("../basic_models.zig").Point2_f; 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 common = @import("common.zig");
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 } },
.{ .data = .{ .closed = false } },
.{ .data = .{ .filled = true } },
.{ .data = .{ .fill_rgba = 0x000000FF } },
};
/// Теги обязательных свойств. /// Теги обязательных свойств = теги из default_shape_properties.
pub fn getRequiredTags() []const std.meta.Tag(PropertyData) { pub const required_tags = common.tagsFromProperties(&default_shape_properties);
return &[_]std.meta.Tag(PropertyData){
.points,
};
}
/// Добавляет к объекту свойства по умолчанию для ломаной.
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 по точкам. /// Локальные границы: AABB по точкам.
pub fn getLocalBounds(obj: *const Object) !Rect_f { pub fn getLocalBounds(obj: *const Object) !Rect_f {
try shape_mod.ensure(obj, .broken); try shape_mod.ensure(obj, .broken);
const p = obj.getProperty(.points).?; const p = obj.getProperty(.points).?;
if (p.points.items.len == 0) return error.EmptyPoints; if (p.points.len == 0) return error.EmptyPoints;
var min_x: f32 = p.points.items[0].x; var min_x: f32 = p.points[0].x;
var max_x: f32 = min_x; var max_x: f32 = min_x;
var min_y: f32 = p.points.items[0].y; var min_y: f32 = p.points[0].y;
var max_y: f32 = min_y; var max_y: f32 = min_y;
for (p.points.items[1..]) |pt| { for (p.points[1..]) |pt| {
min_x = @min(min_x, pt.x); min_x = @min(min_x, pt.x);
max_x = @max(max_x, pt.x); max_x = @max(max_x, pt.x);
min_y = @min(min_y, pt.y); min_y = @min(min_y, pt.y);

View File

@@ -0,0 +1,10 @@
const std = @import("std");
const Property = @import("../Property.zig").Property;
const PropertyData = @import("../Property.zig").Data;
/// Теги свойств — те же, что фигура добавляет к стандартным (из default_shape_properties).
pub fn tagsFromProperties(comptime props: []const Property) [props.len]std.meta.Tag(PropertyData) {
var result: [props.len]std.meta.Tag(PropertyData) = undefined;
for (props, &result) |p, *r| r.* = std.meta.activeTag(p.data);
return result;
}

View File

@@ -1,11 +1,11 @@
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 common = @import("common.zig");
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 } },
@@ -14,15 +14,8 @@ pub const default_shape_properties = [_]Property{
.{ .data = .{ .fill_rgba = 0x000000FF } }, .{ .data = .{ .fill_rgba = 0x000000FF } },
}; };
/// Теги обязательных свойств. /// Теги обязательных свойств = теги из default_shape_properties.
pub fn getRequiredTags() []const std.meta.Tag(PropertyData) { pub const required_tags = common.tagsFromProperties(&default_shape_properties);
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]. /// Локальные границы эллипса: [-radii.x, -radii.y] .. [radii.x, radii.y].
pub fn getLocalBounds(obj: *const Object) !Rect_f { pub fn getLocalBounds(obj: *const Object) !Rect_f {

View File

@@ -1,24 +1,17 @@
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 common = @import("common.zig");
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 } } },
}; };
/// Теги обязательных свойств. /// Теги обязательных свойств = теги из default_shape_properties.
pub fn getRequiredTags() []const std.meta.Tag(PropertyData) { pub const required_tags = common.tagsFromProperties(&default_shape_properties);
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. /// Локальные границы: от (0,0) до end_point.
pub fn getLocalBounds(obj: *const Object) !Rect_f { pub fn getLocalBounds(obj: *const Object) !Rect_f {

View File

@@ -4,19 +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");
pub const Rect = basic_models.Rectf; pub const Rect = basic_models.Rect_f;
/// Добавляет к объекту список свойств фигуры. Для .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 => {
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;
} }
@@ -42,11 +60,12 @@ pub fn ensure(obj: *const Object, expected_kind: Object.ShapeKind) !void {
} }
} }
/// Обязательные теги = те свойства, которые фигура добавляет к общим (из default_shape_properties).
fn requiredTagsFor(kind: Object.ShapeKind) []const std.meta.Tag(PropertyData) { fn requiredTagsFor(kind: Object.ShapeKind) []const std.meta.Tag(PropertyData) {
return switch (kind) { return switch (kind) {
.line => line.getRequiredTags(), .line => &line.required_tags,
.ellipse => ellipse.getRequiredTags(), .ellipse => &ellipse.required_tags,
.broken => broken.getRequiredTags(), .broken => &broken.required_tags,
}; };
} }
@@ -71,7 +90,7 @@ test "getLocalBounds" {
try std.testing.expect(line_bounds.x == 0); try std.testing.expect(line_bounds.x == 0);
try std.testing.expect(line_bounds.y == 0); try std.testing.expect(line_bounds.y == 0);
try std.testing.expect(line_bounds.w == 100); try std.testing.expect(line_bounds.w == 100);
try std.testing.expect(line_bounds.h == 0); try std.testing.expect(line_bounds.h == 200);
var ellipse_obj = try shape.createObject(allocator, .ellipse); var ellipse_obj = try shape.createObject(allocator, .ellipse);
defer ellipse_obj.deinit(allocator); defer ellipse_obj.deinit(allocator);

View File

@@ -74,16 +74,19 @@ 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 } });
}, },
} }
} }

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

@@ -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");
@@ -564,10 +565,12 @@ fn drawPropertyEditor(canvas: *Canvas, obj: *Document.Object, prop: *const Prope
} }
}, },
.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 +680,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| {