Убраны комментарии лишние и улучшены модели
This commit is contained in:
@@ -2,12 +2,14 @@ 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();
|
||||
|
||||
size: Size,
|
||||
allocator: std.mem.Allocator,
|
||||
/// Корневые объекты документа (вложенность через Object.children).
|
||||
objects: std.ArrayList(Object),
|
||||
|
||||
pub fn init(allocator: std.mem.Allocator, size: Size) Document {
|
||||
@@ -23,112 +25,7 @@ pub fn deinit(self: *Document) void {
|
||||
self.objects.deinit(self.allocator);
|
||||
}
|
||||
|
||||
/// Добавить корневой объект в документ (клонирует в allocator документа).
|
||||
pub fn addObject(self: *Document, template: Object) !void {
|
||||
const obj = try template.clone(self.allocator);
|
||||
try self.objects.append(self.allocator, obj);
|
||||
}
|
||||
|
||||
/// Тип фигуры: определяет, как RenderEngine интерпретирует свойства и рисует объект.
|
||||
pub const ShapeKind = enum {
|
||||
rect,
|
||||
ellipse,
|
||||
line,
|
||||
path,
|
||||
};
|
||||
|
||||
/// Объект документа: тип фигуры, свойства и вложенные дочерние объекты.
|
||||
/// Типы объектов задаются конструкторами; UI и RenderEngine работают с union Property.
|
||||
pub const Object = struct {
|
||||
shape: ShapeKind,
|
||||
properties: std.ArrayList(Property),
|
||||
/// Вложенные объекты (дерево).
|
||||
children: std.ArrayList(Object),
|
||||
|
||||
/// Найти свойство по тегу и вернуть вариант union (caller делает switch).
|
||||
pub fn getProperty(self: Object, tag: std.meta.Tag(Property)) ?Property {
|
||||
for (self.properties.items) |prop| {
|
||||
if (std.meta.activeTag(prop) == tag) return prop;
|
||||
}
|
||||
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.*) == std.meta.activeTag(prop)) {
|
||||
self.properties.items[i] = prop;
|
||||
return;
|
||||
}
|
||||
}
|
||||
try self.properties.append(allocator, prop);
|
||||
}
|
||||
|
||||
/// Добавить дочерний объект (клонирует в переданный allocator).
|
||||
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);
|
||||
try properties_list.appendSlice(allocator, self.properties.items);
|
||||
|
||||
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);
|
||||
self.properties.deinit(allocator);
|
||||
self.* = undefined;
|
||||
}
|
||||
|
||||
/// Базовый объект с общим набором свойств (для внутреннего использования конструкторами).
|
||||
fn createWithCommon(allocator: std.mem.Allocator, shape: ShapeKind) !Object {
|
||||
const common = properties.defaultCommonProperties();
|
||||
var properties_list = std.ArrayList(Property).empty;
|
||||
errdefer properties_list.deinit(allocator);
|
||||
try properties_list.appendSlice(allocator, &common);
|
||||
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, .{ .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, .{ .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, .{ .end_point = .{ .x = 100, .y = 0 } });
|
||||
return obj;
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user