summaryrefslogtreecommitdiff
path: root/src/sketch/root.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/sketch/root.zig')
-rw-r--r--src/sketch/root.zig102
1 files changed, 92 insertions, 10 deletions
diff --git a/src/sketch/root.zig b/src/sketch/root.zig
index 94ce617..33c9ba7 100644
--- a/src/sketch/root.zig
+++ b/src/sketch/root.zig
@@ -22,10 +22,23 @@
//! see `shape`.
const std = @import("std");
pub const abi = @import("abi");
-pub const shape = @import("shape.zig");
+/// Duck-typed points, rects and colors (named `shapes` inside this file,
+/// where `Frame.shape` is the method).
+pub const shape = shapes;
+const shapes = @import("shape.zig");
pub const Key = abi.Key;
pub const Camera = @import("camera.zig").Camera;
pub const tween = @import("tween.zig");
+/// Vectors (`@Vector(2, f32)`), affine transforms, Béziers and noise: the
+/// geometry module the `vector` library uses too.
+pub const math = @import("math");
+/// Circles, lines and polygons as `Command.shape`s: a `Tessellation` to
+/// build yourself (L1), or a `Pen` and `circle`/`line`/`polygon` (L2).
+pub const draw = @import("draw.zig");
+pub const Clock = @import("clock.zig").Clock;
+pub const List = @import("list.zig").List;
+/// Data computed in draw from a few State fields, kept in Session and rebuilt when they change (or after a reload).
+pub const Derived = @import("derived.zig").Derived;
pub const web = @import("web.zig");
const request = @import("request.zig");
pub const Request = request.Request;
@@ -36,12 +49,14 @@ pub const Color = abi.Color;
pub const Event = abi.Event;
pub const Input = abi.Input;
pub const Mode = abi.Mode;
+pub const MouseButton = abi.MouseButton;
pub const Host = abi.Host;
pub const Image = abi.Image;
pub const Audio = abi.Audio;
pub const Buffer = abi.Buffer;
pub const Cursor = abi.Cursor;
pub const Voice = abi.Voice;
+pub const BlendMode = abi.BlendMode;
/// What `draw` receives: input, host services, memory by lifetime and the
/// command list. Commands may be built from anything: the methods copy what
@@ -71,35 +86,91 @@ pub const Frame = struct {
return f.scratch.dupe(T, items) catch &.{};
}
+ /// Colour parameters take anything `shape.color` does (`0x202020`,
+ /// `sk.Color.gray(220)`, `.{ .r = 1, .g = 0, .b = 0 }`), and the
+ /// `Color` constants by name: `f.clear(.white)`.
pub fn clear(f: *Frame, color: anytype) void {
- f.emit(.{ .clear = shape.color(color) });
+ f.emit(.{ .clear = paint(color) });
}
pub fn fill(f: *Frame, r: anytype, color: anytype) void {
- f.emit(.{ .rect = .{ .dst = shape.rect(r), .color = shape.color(color) } });
+ f.emit(.{ .rect = .{ .dst = shapes.rect(r), .color = paint(color) } });
}
/// `options` may set `.src` (a rect within the image) and `.tint` (a color).
pub fn image(f: *Frame, img: Image, dst: anytype, options: anytype) void {
const O = @TypeOf(options);
f.emit(.{ .image = .{
.image = img,
- .dst = shape.rect(dst),
- .src = if (@hasField(O, "src")) shape.rect(options.src) else .{},
- .tint = if (@hasField(O, "tint")) shape.color(options.tint) else .white,
+ .dst = shapes.rect(dst),
+ .src = if (@hasField(O, "src")) shapes.rect(options.src) else .{},
+ .tint = if (@hasField(O, "tint")) paint(options.tint) else .white,
} });
}
+ /// Processing's `imageMode(CENTER)` under a transform: the image (or
+ /// `src` within it) centred on `xf`'s origin, along its axes, at
+ /// `size` (default: the image's, or `src`'s). `.place(at, radians)`
+ /// turns it, `.translate(x, y)` only moves it. Nothing is drawn while
+ /// the size is unknown (an image the web is still loading).
+ pub fn imageAt(f: *Frame, img: Image, xf: math.Affine, options: struct { src: Rect = .{}, size: ?[2]f32 = null, tint: Color = .white }) void {
+ const wh: [2]f32 = options.size orelse if (options.src.w > 0) .{ options.src.w, options.src.h } else blk: {
+ const px = f.host.imageSize(img);
+ break :blk .{ @floatFromInt(px[0]), @floatFromInt(px[1]) };
+ };
+ if (wh[0] == 0 or wh[1] == 0) return;
+ const hw = wh[0] / 2;
+ const hh = wh[1] / 2;
+ f.emit(.{ .image = .{
+ .image = img,
+ .dst = .{ .x = -hw, .y = -hh, .w = wh[0], .h = wh[1] },
+ .src = options.src,
+ .tint = options.tint,
+ .corners = .{
+ .top_left = xf.apply(.{ -hw, -hh }),
+ .top_right = xf.apply(.{ hw, -hh }),
+ .bottom_left = xf.apply(.{ -hw, hh }),
+ },
+ } });
+ }
+ /// Draws the 2D commands after it into `img` (made by
+ /// `host.newImage`), until `f.target(.none)` or the frame ends. The
+ /// image keeps what was drawn, across frames: a canvas that is never
+ /// cleared. It is a Session cache, so repaint it from State when
+ /// `f.in.jumped`.
+ pub fn target(f: *Frame, img: Image) void {
+ f.emit(.{ .target = img });
+ }
+ /// How the 2D commands after it blend, until the next `blend` or the
+ /// frame ends (which goes back to `.alpha`).
+ pub fn blend(f: *Frame, mode: BlendMode) void {
+ f.emit(.{ .blend = mode });
+ }
pub fn text(f: *Frame, x: f32, y: f32, scale: f32, bytes: []const u8, color: anytype) void {
- f.emit(.{ .text = .{ .x = x, .y = y, .scale = scale, .color = shape.color(color), .bytes = f.keep(u8, bytes) } });
+ f.emit(.{ .text = .{ .x = x, .y = y, .scale = scale, .color = paint(color), .bytes = f.keep(u8, bytes) } });
}
pub fn print(f: *Frame, x: f32, y: f32, scale: f32, color: anytype, comptime fmt: []const u8, args: anytype) void {
- f.emit(.{ .text = .{ .x = x, .y = y, .scale = scale, .color = shape.color(color), .bytes = f.format(fmt, args) } });
+ f.emit(.{ .text = .{ .x = x, .y = y, .scale = scale, .color = paint(color), .bytes = f.format(fmt, args) } });
}
/// Formats into scratch memory.
pub fn format(f: *Frame, comptime fmt: []const u8, args: anytype) []const u8 {
return f.scratch.print(fmt, args) catch "?";
}
+ /// Fills `t`'s region in `color` (see `draw`). Its slices are not
+ /// copied: `t` must live until the frame is drawn (scratch memory, or
+ /// a session value not cleared before then).
+ pub fn shape(f: *Frame, t: *const draw.Tessellation, color: Color) void {
+ if (t.indices.items.len == 0) return;
+ f.emit(.{ .shape = .{ .color = color, .points = t.points.items, .ends = t.ends.items, .indices = t.indices.items } });
+ }
+ /// `t` with a colour per subpath: `colors[i]` for subpath `i` (one per
+ /// `t.ends` item; a stroked circle has two, its outline and its hole,
+ /// which must share one). Many shapes of many colours in one command,
+ /// like fading particles. Neither slice is copied (see `shape`).
+ pub fn shapeColors(f: *Frame, t: *const draw.Tessellation, colors: []const Color) void {
+ if (t.indices.items.len == 0) return;
+ f.emit(.{ .shape = .{ .color = if (colors.len > 0) colors[colors.len - 1] else .white, .points = t.points.items, .ends = t.ends.items, .indices = t.indices.items, .colors = colors } });
+ }
/// A rect, or `null` to stop clipping.
pub fn clip(f: *Frame, r: anytype) void {
- f.emit(.{ .clip = if (@TypeOf(r) == @TypeOf(null)) null else shape.rect(r) });
+ f.emit(.{ .clip = if (@TypeOf(r) == @TypeOf(null)) null else shapes.rect(r) });
}
pub fn canvas(f: *Frame, w: u32, h: u32) void {
f.emit(.{ .canvas = .{ .w = w, .h = h } });
@@ -171,6 +242,13 @@ pub const Frame = struct {
}
};
+/// A colour argument: `shape.color`'s forms, or a `Color` constant by
+/// name (`.white`), which an `anytype` parameter cannot resolve itself.
+fn paint(value: anytype) Color {
+ if (@TypeOf(value) == @EnumLiteral()) return @field(Color, @tagName(value));
+ return shapes.color(value);
+}
+
/// What `command` receives: somewhere to answer, the current input, and the
/// allocators for Session and Store.
pub const Console = struct {
@@ -305,7 +383,11 @@ pub fn Glue(comptime App: type) type {
test {
_ = request;
_ = abi.layout;
- _ = shape;
+ _ = shapes;
_ = @import("camera.zig");
_ = tween;
+ _ = draw;
+ _ = @import("clock.zig");
+ _ = @import("list.zig");
+ _ = @import("derived.zig");
}