const std = @import("std"); const basic_models = @import("basic_models.zig"); const Size_f = basic_models.Size_f; const Document = @This(); pub const Object = @import("Object.zig"); const shape = @import("shape/shape.zig"); size: Size_f, allocator: std.mem.Allocator, objects: std.ArrayList(Object), next_object_id: u64, pub fn init(allocator: std.mem.Allocator, size: Size_f) Document { return .{ .size = size, .allocator = allocator, .objects = std.ArrayList(Object).empty, .next_object_id = 1, }; } pub fn deinit(self: *Document) void { for (self.objects.items) |*obj| obj.deinit(self.allocator); self.objects.deinit(self.allocator); } pub fn addObject(self: *Document, template: Object) !void { const obj = try template.clone(self.allocator, &self.next_object_id); try self.objects.append(self.allocator, obj); } /// Добавляет объект в документ: как ребёнка родителя (если id найден), иначе в корень. pub fn addObjectUnderParentId(self: *Document, parent_id: ?u64, template: Object) !void { if (parent_id) |id| { if (self.findObjectById(id)) |parent| { try parent.addChild(self.allocator, template, &self.next_object_id); return; } } try self.addObject(template); } pub fn addShape(self: *Document, parent: ?*Object, shape_kind: Object.ShapeKind) !void { const obj = try shape.createObject(self.allocator, shape_kind); if (parent) |p| { try p.addChild(self.allocator, obj, &self.next_object_id); } else { try self.addObject(obj); } } /// Удаляет объект из документа (из корня или из детей родителя). Возвращает true, если объект был найден и удалён. pub fn removeObject(self: *Document, obj: *Object) bool { for (self.objects.items, 0..) |*item, i| { if (item == obj) { var removed = self.objects.orderedRemove(i); removed.deinit(self.allocator); return true; } if (removeFromChildren(self.allocator, &item.children, obj)) return true; } return false; } /// Удаляет объект по id. Возвращает true, если объект был найден и удалён. pub fn removeObjectById(self: *Document, obj_id: u64) bool { for (self.objects.items, 0..) |*item, i| { if (item.id == obj_id) { var removed = self.objects.orderedRemove(i); removed.deinit(self.allocator); return true; } if (removeFromChildrenById(self.allocator, &item.children, obj_id)) return true; } return false; } pub fn findObjectById(self: *Document, obj_id: u64) ?*Object { for (self.objects.items) |*item| { if (item.id == obj_id) return item; if (findInChildrenById(&item.children, obj_id)) |found| return found; } return null; } fn removeFromChildren(allocator: std.mem.Allocator, children: *std.ArrayList(Object), obj: *Object) bool { for (children.items, 0..) |*item, i| { if (item == obj) { var removed = children.orderedRemove(i); removed.deinit(allocator); return true; } if (removeFromChildren(allocator, &item.children, obj)) return true; } return false; } fn removeFromChildrenById(allocator: std.mem.Allocator, children: *std.ArrayList(Object), obj_id: u64) bool { for (children.items, 0..) |*item, i| { if (item.id == obj_id) { var removed = children.orderedRemove(i); removed.deinit(allocator); return true; } if (removeFromChildrenById(allocator, &item.children, obj_id)) return true; } return false; } fn findInChildrenById(children: *std.ArrayList(Object), obj_id: u64) ?*Object { for (children.items) |*item| { if (item.id == obj_id) return item; if (findInChildrenById(&item.children, obj_id)) |found| return found; } return null; }