Refactor: Перенёс логику создания фигур

Я перенёс логику создания объектов фигур из `Document.zig` и `Object.zig` в новый модуль `shape.zig`. Это упрощает добавление новых фигур и улучшает организацию кода.
This commit is contained in:
2026-02-24 19:59:57 +03:00
parent 1a94cc8bfd
commit f1a0e84272
9 changed files with 245 additions and 49 deletions

View File

@@ -1,10 +1,6 @@
const std = @import("std");
const basic_models = @import("basic_models.zig");
const Size = basic_models.Size;
const Point2 = basic_models.Point2;
const Property = @import("Property.zig").Property;
const PropertyData = @import("Property.zig").Data;
const defaultCommonProperties = @import("Property.zig").defaultCommonProperties;
const Object = @This();
pub const ShapeKind = enum {
@@ -68,41 +64,3 @@ pub fn deinit(self: *Object, allocator: std.mem.Allocator) void {
self.properties.deinit(allocator);
self.* = undefined;
}
fn createWithCommonProperties(allocator: std.mem.Allocator, shape: ShapeKind) !Object {
var properties_list = std.ArrayList(Property).empty;
errdefer properties_list.deinit(allocator);
for (defaultCommonProperties) |prop| try properties_list.append(allocator, prop);
return .{
.shape = shape,
.properties = properties_list,
.children = std.ArrayList(Object).empty,
};
}
pub fn createEllipse(allocator: std.mem.Allocator) !Object {
var obj = try createWithCommonProperties(allocator, .ellipse);
errdefer obj.deinit(allocator);
try obj.properties.append(allocator, .{ .data = .{ .radii = .{ .x = 50, .y = 50 } } });
return obj;
}
pub fn createLine(allocator: std.mem.Allocator) !Object {
var obj = try createWithCommonProperties(allocator, .line);
errdefer obj.deinit(allocator);
try obj.properties.append(allocator, .{ .data = .{ .end_point = .{ .x = 100, .y = 0 } } });
return obj;
}
pub fn createBrokenLine(allocator: std.mem.Allocator) !Object {
var obj = try createWithCommonProperties(allocator, .broken);
errdefer obj.deinit(allocator);
var points = std.ArrayList(Point2).empty;
try points.appendSlice(allocator, &.{
.{ .x = 0, .y = 0 },
.{ .x = 80, .y = 0 },
.{ .x = 80, .y = 60 },
});
try obj.properties.append(allocator, .{ .data = .{ .points = points } });
return obj;
}