Убраны комментарии лишние и улучшены модели
This commit is contained in:
117
src/models/Object.zig
Normal file
117
src/models/Object.zig
Normal file
@@ -0,0 +1,117 @@
|
||||
const std = @import("std");
|
||||
const basic_models = @import("basic_models.zig");
|
||||
const defaultCommonProperties = @import("Property.zig").defaultCommonProperties;
|
||||
const Property = @import("Property.zig").Property;
|
||||
const PropertyData = @import("Property.zig").Data;
|
||||
const Point2 = basic_models.Point2;
|
||||
const Size = basic_models.Size;
|
||||
const Object = @This();
|
||||
|
||||
pub const ShapeKind = enum {
|
||||
rect,
|
||||
line,
|
||||
ellipse,
|
||||
arc,
|
||||
broken,
|
||||
};
|
||||
|
||||
shape: ShapeKind,
|
||||
properties: std.ArrayList(Property),
|
||||
children: std.ArrayList(Object),
|
||||
|
||||
pub fn getProperty(self: Object, tag: std.meta.Tag(PropertyData)) ?*const PropertyData {
|
||||
for (self.properties.items) |*prop| {
|
||||
if (std.meta.activeTag(prop.data) == tag) return &prop.data;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
pub fn setProperty(self: *Object, allocator: std.mem.Allocator, prop: Property) !void {
|
||||
for (self.properties.items, 0..) |*p, i| {
|
||||
if (std.meta.activeTag(p.data) == std.meta.activeTag(prop.data)) {
|
||||
if (p.data == .points) p.data.points.deinit(allocator);
|
||||
self.properties.items[i] = prop;
|
||||
return;
|
||||
}
|
||||
}
|
||||
try self.properties.append(allocator, prop);
|
||||
}
|
||||
|
||||
pub fn addChild(self: *Object, allocator: std.mem.Allocator, template: Object) !void {
|
||||
const obj = try template.clone(allocator);
|
||||
try self.children.append(allocator, obj);
|
||||
}
|
||||
|
||||
pub fn clone(self: Object, allocator: std.mem.Allocator) !Object {
|
||||
var properties_list = std.ArrayList(Property).empty;
|
||||
errdefer properties_list.deinit(allocator);
|
||||
for (self.properties.items) |prop| {
|
||||
try properties_list.append(allocator, try prop.clone(allocator));
|
||||
}
|
||||
|
||||
var children_list = std.ArrayList(Object).empty;
|
||||
errdefer children_list.deinit(allocator);
|
||||
for (self.children.items) |child| {
|
||||
try children_list.append(allocator, try child.clone(allocator));
|
||||
}
|
||||
|
||||
return .{
|
||||
.shape = self.shape,
|
||||
.properties = properties_list,
|
||||
.children = children_list,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn deinit(self: *Object, allocator: std.mem.Allocator) void {
|
||||
for (self.children.items) |*child| child.deinit(allocator);
|
||||
self.children.deinit(allocator);
|
||||
for (self.properties.items) |*prop| prop.deinit(allocator);
|
||||
self.properties.deinit(allocator);
|
||||
self.* = undefined;
|
||||
}
|
||||
|
||||
fn createWithCommon(allocator: std.mem.Allocator, shape: ShapeKind) !Object {
|
||||
const common = defaultCommonProperties();
|
||||
var properties_list = std.ArrayList(Property).empty;
|
||||
errdefer properties_list.deinit(allocator);
|
||||
for (common) |d| try properties_list.append(allocator, .{ .data = d });
|
||||
return .{
|
||||
.shape = shape,
|
||||
.properties = properties_list,
|
||||
.children = std.ArrayList(Object).empty,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn createRect(allocator: std.mem.Allocator) !Object {
|
||||
var obj = try createWithCommon(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);
|
||||
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);
|
||||
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);
|
||||
errdefer obj.deinit(allocator);
|
||||
var points = std.ArrayList(Point2).init(allocator);
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user