Базовая модель готова

This commit is contained in:
2026-02-23 23:37:02 +03:00
parent bd58286c98
commit dd9d5deb92
4 changed files with 35 additions and 28 deletions

View File

@@ -1,13 +1,10 @@
const std = @import("std");
const basic_models = @import("basic_models.zig");
const properties = @import("Property.zig");
const Property = properties.Property;
const PropertyData = properties.Data;
const Point2 = basic_models.Point2;
const Size = basic_models.Size;
const Object = @import("Object.zig");
const Document = @This();
pub const Object = @import("Object.zig");
size: Size,
allocator: std.mem.Allocator,
objects: std.ArrayList(Object),

View File

@@ -1,10 +1,10 @@
const std = @import("std");
const basic_models = @import("basic_models.zig");
const defaultCommonProperties = @import("Property.zig").defaultCommonProperties;
const Size = basic_models.Size;
const Point2 = basic_models.Point2;
const Property = @import("Property.zig").Property;
const PropertyData = @import("Property.zig").Data;
const Point2 = basic_models.Point2;
const Size = basic_models.Size;
const defaultCommonProperties = @import("Property.zig").defaultCommonProperties;
const Object = @This();
pub const ShapeKind = enum {
@@ -70,11 +70,10 @@ pub fn deinit(self: *Object, allocator: std.mem.Allocator) void {
self.* = undefined;
}
fn createWithCommon(allocator: std.mem.Allocator, shape: ShapeKind) !Object {
const common = defaultCommonProperties();
fn createWithCommonProperties(allocator: std.mem.Allocator, shape: ShapeKind) !Object {
var properties_list = std.ArrayList(Property).empty;
errdefer properties_list.deinit(allocator);
for (common) |d| try properties_list.append(allocator, .{ .data = d });
for (defaultCommonProperties) |prop| try properties_list.append(allocator, prop);
return .{
.shape = shape,
.properties = properties_list,
@@ -83,28 +82,28 @@ fn createWithCommon(allocator: std.mem.Allocator, shape: ShapeKind) !Object {
}
pub fn createRect(allocator: std.mem.Allocator) !Object {
var obj = try createWithCommon(allocator, .rect);
var obj = try createWithCommonProperties(allocator, .rect);
errdefer obj.deinit(allocator);
try obj.properties.append(allocator, .{ .data = .{ .size = .{ .width = 100, .height = 100 } } });
return obj;
}
pub fn createEllipse(allocator: std.mem.Allocator) !Object {
var obj = try createWithCommon(allocator, .ellipse);
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 createWithCommon(allocator, .line);
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 createWithCommon(allocator, .broken);
var obj = try createWithCommonProperties(allocator, .broken);
errdefer obj.deinit(allocator);
var points = std.ArrayList(Point2).init(allocator);
try points.appendSlice(allocator, &.{

View File

@@ -46,7 +46,7 @@ pub const Property = struct {
}
};
const default_common_data: []const Data = .{
const default_common_data = [_]Data{
.{ .position = .{ .x = 0, .y = 0 } },
.{ .angle = 0 },
.{ .scale = .{ .scale_x = 1, .scale_y = 1 } },
@@ -55,9 +55,13 @@ const default_common_data: []const Data = .{
.{ .locked = false },
};
pub fn defaultCommonProperties() []const Data {
return &default_common_data;
}
pub const defaultCommonProperties: [default_common_data.len]Property = blk: {
var result: [default_common_data.len]Property = undefined;
for (default_common_data, &result) |d, *p| {
p.* = .{ .data = d };
}
break :blk result;
};
test "Property wrapper and Data" {
const p = Property{ .data = .{ .opacity = 0.5 } };
@@ -66,8 +70,8 @@ test "Property wrapper and Data" {
}
test "common properties" {
const common = defaultCommonProperties();
try std.testing.expect(common[0] == .position);
try std.testing.expect(common[0].position.x == 0);
try std.testing.expect(common[2].visible == true);
try std.testing.expect(defaultCommonProperties[0].data == .position);
try std.testing.expect(defaultCommonProperties[0].data.position.x == 0);
try std.testing.expect(defaultCommonProperties[3].data == .visible);
try std.testing.expect(defaultCommonProperties[3].data.visible == true);
}