Документ в канвасе
This commit is contained in:
@@ -4,28 +4,27 @@ const dvui = @import("dvui");
|
|||||||
const Document = @import("models/Document.zig");
|
const Document = @import("models/Document.zig");
|
||||||
const RenderEngine = @import("render/RenderEngine.zig").RenderEngine;
|
const RenderEngine = @import("render/RenderEngine.zig").RenderEngine;
|
||||||
const ImageRect = @import("models/basic_models.zig").ImageRect;
|
const ImageRect = @import("models/basic_models.zig").ImageRect;
|
||||||
const Size = dvui.Size;
|
|
||||||
const Color = dvui.Color;
|
const Color = dvui.Color;
|
||||||
|
|
||||||
const Canvas = @This();
|
const Canvas = @This();
|
||||||
|
|
||||||
allocator: std.mem.Allocator,
|
allocator: std.mem.Allocator,
|
||||||
|
document: *Document,
|
||||||
|
render_engine: RenderEngine,
|
||||||
texture: ?dvui.Texture = null,
|
texture: ?dvui.Texture = null,
|
||||||
size: Size = .{ .w = 800, .h = 600 },
|
pos: dvui.Point = dvui.Point{ .x = 400, .y = 400 },
|
||||||
pos: dvui.Point = dvui.Point{ .x = 0, .y = 0 },
|
|
||||||
scroll: dvui.ScrollInfo = .{
|
scroll: dvui.ScrollInfo = .{
|
||||||
.vertical = .auto,
|
.vertical = .auto,
|
||||||
.horizontal = .auto,
|
.horizontal = .auto,
|
||||||
},
|
},
|
||||||
native_scaling: bool = true,
|
native_scaling: bool = true,
|
||||||
document: ?*Document = null,
|
|
||||||
render_engine: RenderEngine,
|
|
||||||
_visible_rect: ?ImageRect = null,
|
_visible_rect: ?ImageRect = null,
|
||||||
_zoom: f32 = 1,
|
_zoom: f32 = 1,
|
||||||
|
|
||||||
pub fn init(allocator: std.mem.Allocator, engine: RenderEngine) Canvas {
|
pub fn init(allocator: std.mem.Allocator, document: *Document, engine: RenderEngine) Canvas {
|
||||||
return .{
|
return .{
|
||||||
.allocator = allocator,
|
.allocator = allocator,
|
||||||
|
.document = document,
|
||||||
.render_engine = engine,
|
.render_engine = engine,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -79,11 +78,12 @@ pub fn addZoom(self: *Canvas, value: f32) void {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn getScaledImageSize(self: Canvas) ImageRect {
|
pub fn getScaledImageSize(self: Canvas) ImageRect {
|
||||||
|
const doc = self.document;
|
||||||
return .{
|
return .{
|
||||||
.x = @intFromFloat(self.pos.x),
|
.x = @intFromFloat(self.pos.x),
|
||||||
.y = @intFromFloat(self.pos.y),
|
.y = @intFromFloat(self.pos.y),
|
||||||
.w = @intFromFloat(self.size.w * self._zoom),
|
.w = @intFromFloat(doc.size.width * self._zoom),
|
||||||
.h = @intFromFloat(self.size.h * self._zoom),
|
.h = @intFromFloat(doc.size.height * self._zoom),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ pub const OpenDocument = struct {
|
|||||||
const default_size = basic_models.Size{ .width = 800, .height = 600 };
|
const default_size = basic_models.Size{ .width = 800, .height = 600 };
|
||||||
self.document = Document.init(allocator, default_size);
|
self.document = Document.init(allocator, default_size);
|
||||||
self.cpu_render = CpuRenderEngine.init(allocator, .Squares);
|
self.cpu_render = CpuRenderEngine.init(allocator, .Squares);
|
||||||
self.canvas = Canvas.init(allocator, (&self.cpu_render).renderEngine());
|
self.canvas = Canvas.init(allocator, &self.document, (&self.cpu_render).renderEngine());
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(self: *OpenDocument) void {
|
pub fn deinit(self: *OpenDocument) void {
|
||||||
|
|||||||
@@ -112,12 +112,6 @@ fn gui_frame(ctx: *WindowContext) bool {
|
|||||||
dvui.label(@src(), "Tools", .{}, .{});
|
dvui.label(@src(), "Tools", .{}, .{});
|
||||||
if (active_doc) |doc| {
|
if (active_doc) |doc| {
|
||||||
const canvas = &doc.canvas;
|
const canvas = &doc.canvas;
|
||||||
if (dvui.button(@src(), "Fill Random Color", .{}, .{}) or ctx.frame_index == 0) {
|
|
||||||
canvas.exampleReset() catch |err| {
|
|
||||||
std.debug.print("Error reset example: {}\n", .{err});
|
|
||||||
};
|
|
||||||
canvas.pos = .{ .x = 400, .y = 400 };
|
|
||||||
}
|
|
||||||
if (dvui.checkbox(@src(), &canvas.native_scaling, "Scaling", .{})) {}
|
if (dvui.checkbox(@src(), &canvas.native_scaling, "Scaling", .{})) {}
|
||||||
if (dvui.button(@src(), if (doc.cpu_render.type == .Gradient) "Gradient" else "Squares", .{}, .{})) {
|
if (dvui.button(@src(), if (doc.cpu_render.type == .Gradient) "Gradient" else "Squares", .{}, .{})) {
|
||||||
if (doc.cpu_render.type == .Gradient) {
|
if (doc.cpu_render.type == .Gradient) {
|
||||||
|
|||||||
Reference in New Issue
Block a user