can now scroll

This commit is contained in:
2026-01-22 13:46:30 +13:00
parent 61b278ac8f
commit 38e37dddc4

View File

@@ -1,6 +1,6 @@
const std = @import("std"); const std = @import("std");
const rl = @import("rl"); const rl = @import("rl");
const shoots = @import("shoots"); const shoots = @import("root.zig");
fn getTextWidth(string: []const u8, font: rl.Font) shoots.Real { fn getTextWidth(string: []const u8, font: rl.Font) shoots.Real {
var width: c_int = 0; var width: c_int = 0;
@@ -117,6 +117,8 @@ pub fn main() anyerror!void {
std.debug.print("shoots version :{s}\n", .{shoots.VERSION}); std.debug.print("shoots version :{s}\n", .{shoots.VERSION});
std.debug.print("raylib time : {}\n", .{@divTrunc(timer.lap(), std.time.ns_per_ms)}); std.debug.print("raylib time : {}\n", .{@divTrunc(timer.lap(), std.time.ns_per_ms)});
var scroll_offset: shoots.Pos = .{};
const child = &[_]UI.Node{ const child = &[_]UI.Node{
sideBar(), sideBar(),
button(), button(),
@@ -153,183 +155,205 @@ pub fn main() anyerror!void {
}, },
}), }),
// UI.Element(.{ UI.Element(.{
// .name = "child", .name = "child",
// .style = .{ .background_colour = .{ .style = .{ .background_colour = .{
// .b = 255, .b = 255,
// }, .padding = .{ .bottom = 5 } }, }, .padding = .{ .bottom = 5 } },
// .children = &[_]UI.Node{ .children = &[_]UI.Node{
// UI.Element(.{ UI.Element(.{
// .name = "branch1", .name = "branch1",
// // .rect = .{ .w = 200 }, // .rect = .{ .w = 200 },
// .style = .{ .style = .{
// .size_x = .grow, .size_x = .grow,
// .padding = .{ .padding = .{
// .bottom = 5, .bottom = 5,
// .left = 5, .left = 5,
// .top = 5, .top = 5,
// .right = 5, .right = 5,
// }, },
// // .layout = .right_to_left, // .layout = .right_to_left,
// .child_gap = 5, .child_gap = 5,
// }, },
// .children = &[_]UI.Node{ .children = &[_]UI.Node{
// UI.Element(.{ UI.Element(.{
// .name = "branch11", .name = "branch11",
// .style = .{ .style = .{
// .background_colour = .{ .background_colour = .{
// .g = 128, .g = 128,
// .b = 128, .b = 128,
// .r = 128, .r = 128,
// }, },
// .size_x = .grow, .size_x = .grow,
// }, },
// .rect = .{ .rect = .{
// .h = 40, .h = 40,
// .w = 40, .w = 40,
// }, },
// }), }),
// UI.Element(.{ UI.Element(.{
// .name = "branch12", .name = "branch12",
// .style = .{ .style = .{
// .background_colour = .{ .background_colour = .{
// .g = 128, .g = 128,
// .r = 128, .r = 128,
// }, },
// .size_x = .grow, .size_x = .grow,
// }, },
// .rect = .{ .rect = .{
// .h = 40, .h = 40,
// .w = 40, .w = 40,
// }, },
// }), }),
// UI.Element(.{ UI.Element(.{
// .name = "branch13", .name = "branch13",
// .rect = .{ .rect = .{
// .h = 40, .h = 40,
// .w = 40, .w = 40,
// }, },
// .style = .{ .style = .{
// .background_colour = .{ .background_colour = .{
// .g = 90, .g = 90,
// .b = 150, .b = 150,
// }, },
// }, },
// }), }),
// }, },
// }), }),
// UI.Element(.{ UI.Element(.{
// .name = "branch2", .name = "branch2",
// .style = .{ .style = .{
// .background_colour = .{ .background_colour = .{
// .r = 255, .r = 255,
// .g = 128, .g = 128,
// }, },
// .layout = .right_to_left, .layout = .right_to_left,
// .child_gap = 4, .child_gap = 4,
// .padding = .{ .padding = .{
// .left = 10, .left = 10,
// .bottom = 10, .bottom = 10,
// .right = 10, .right = 10,
// .top = 10, .top = 10,
// }, },
// }, },
// .children = &[_]UI.Node{ .children = &[_]UI.Node{
// UI.Element(.{ UI.Element(.{
// .name = "branch21", .name = "branch21",
// .rect = .{ .rect = .{
// .h = 30, .h = 30,
// .w = 30, .w = 30,
// }, },
// .style = .{ .style = .{
// .rounded = 40, .rounded = 40,
// .background_colour = .{ .background_colour = .{
// .r = 255, .r = 255,
// .b = 150, .b = 150,
// }, },
// }, },
// }), }),
// UI.Element(.{ UI.Element(.{
// .name = "branch22", .name = "branch22",
// .rect = .{ .rect = .{
// .h = 30, .h = 30,
// .w = 30, .w = 30,
// }, },
// .style = .{ .style = .{
// .rounded = 40, .rounded = 40,
// .background_colour = .{ .background_colour = .{
// .r = 90, .r = 90,
// .b = 150, .b = 150,
// }, },
// }, },
// }), }),
// UI.Element(.{ UI.Element(.{
// .name = "branch21", .name = "branch21",
// .rect = .{ .rect = .{
// .h = 30, .h = 30,
// .w = 30, .w = 30,
// }, },
// .style = .{ .style = .{
// .rounded = 40, .rounded = 40,
// .background_colour = .{ .background_colour = .{
// .r = 255, .r = 255,
// .b = 150, .b = 150,
// }, },
// }, },
// }), }),
// UI.Element(.{ UI.Element(.{
// .name = "branch22", .name = "branch22",
// .rect = .{ .rect = .{
// .h = 30, .h = 30,
// .w = 30, .w = 30,
// }, },
// .style = .{ .style = .{
// .rounded = 40, .rounded = 40,
// .background_colour = .{ .background_colour = .{
// .r = 90, .r = 90,
// .b = 150, .b = 150,
// }, },
// }, },
// }), }),
// UI.Element(.{ UI.Element(.{
// .name = "branch21", .name = "branch21",
// .rect = .{ .rect = .{
// .h = 30, .h = 30,
// .w = 30, .w = 30,
// }, },
// .style = .{ .style = .{
// .rounded = 40, .rounded = 40,
// .background_colour = .{ .background_colour = .{
// .r = 255, .r = 255,
// .b = 150, .b = 150,
// }, },
// }, },
// }), }),
// UI.Element(.{ UI.Element(.{
// .name = "branch22", .name = "branch22",
// .rect = .{ .rect = .{
// .h = 30, .h = 30,
// .w = 30, .w = 30,
// }, },
// .style = .{ .style = .{
// .rounded = 40, .rounded = 40,
// .background_colour = .{ .background_colour = .{
// .r = 90, .r = 90,
// .b = 150, .b = 150,
// }, },
// }, },
// }), }),
// }, },
// }), }),
// }, },
// }), }),
button(),
button(), UI.Element(.{
UI.Image(.{ .name = "scrollable",
.texture = image_texture, .scrollable = .{
.rect = .{ .dir = .y,
.h = @floatFromInt(image_texture.height), .offset = &scroll_offset,
.w = @floatFromInt(image_texture.width), },
.rect = .{ .h = 250 },
.children = &[_]UI.Node{
UI.Element(
.{
.style = .{
.background_colour = .{
.r = 255,
},
},
.children = &[_]UI.Node{
UI.Image(.{
.texture = image_texture,
.rect = .{
.h = @floatFromInt(image_texture.height),
.w = @floatFromInt(image_texture.width),
},
}),
button(),
button(),
},
},
),
}, },
}), }),
}; };
@@ -391,16 +415,21 @@ pub fn main() anyerror!void {
.left = rl.isMouseButtonDown(.left), .left = rl.isMouseButtonDown(.left),
.middle = rl.isMouseButtonDown(.middle), .middle = rl.isMouseButtonDown(.middle),
.right = rl.isMouseButtonDown(.right), .right = rl.isMouseButtonDown(.right),
.scroll_delta = .{
.y = rl.getMouseWheelMoveV().y,
.x = rl.getMouseWheelMoveV().x,
},
}); });
const commands = try UI.getRenderCommands(sized, al.allocator()); const commands = try UI.getRenderCommands(sized, al.allocator());
std.debug.print("{f}\n", .{sized}); // std.debug.print("{f}\n", .{sized});
// std.debug.print("layout time : {}ms\n", .{ std.debug.print("layout time : {}ms\n", .{
// @as(f32, @floatFromInt(timer.lap())) / @as(f32, @floatFromInt(std.time.ns_per_ms)), @as(f32, @floatFromInt(timer.lap())) / @as(f32, @floatFromInt(std.time.ns_per_ms)),
// }); });
for (commands.items) |command| { for (commands.items) |command| {
// std.debug.print("command : {any}\n", .{command}); // std.debug.print("command : {any}\n", .{command});
rl.clearBackground(.white);
switch (command) { switch (command) {
.rect => |r| { .rect => |r| {
if (r.rounding) |rounding| { if (r.rounding) |rounding| {
@@ -436,15 +465,22 @@ pub fn main() anyerror!void {
}, },
.text => |t| { .text => |t| {
const string = @as([:0]const u8, @ptrCast(t.text.string)); const string = @as([:0]const u8, @ptrCast(t.text.string));
rl.drawText(string, @as(i32, @intFromFloat(t.pos.x)), @as(i32, @intFromFloat(t.pos.y)), @as(i32, @intFromFloat(t.text.getTextHeight())), rl.Color.init( rl.drawText(
t.text.colour.r, string,
t.text.colour.g, @as(i32, @intFromFloat(t.pos.x)),
t.text.colour.b, @as(i32, @intFromFloat(t.pos.y)),
t.text.colour.a, @as(i32, @intFromFloat(t.text.getTextHeight())),
)); rl.Color.init(
t.text.colour.r,
t.text.colour.g,
t.text.colour.b,
t.text.colour.a,
),
);
}, },
.texture => |tex| { .texture => |tex| {
// std.debug.print("{}\n", .{tex.texture.texture}); // std.debug.print("{}\n", .{tex.texture.texture});
// std.debug.print("texture {}\n", .{tex});
rl.drawTexture( rl.drawTexture(
tex.texture.texture, tex.texture.texture,
@as(i32, @intFromFloat(tex.pos.x)), @as(i32, @intFromFloat(tex.pos.x)),
@@ -452,10 +488,28 @@ pub fn main() anyerror!void {
.white, .white,
); );
}, },
.clip_start => |clip| {
// std.debug.print("clip {}\n", .{clip});
rl.beginScissorMode(
@as(i32, @intFromFloat(clip.pos.x)),
@as(i32, @intFromFloat(clip.pos.y)),
@as(i32, @intFromFloat(clip.rect.w)),
@as(i32, @intFromFloat(clip.rect.h)),
);
// rl.drawRectangleRec(rl.Rectangle.init(
// clip.pos.x,
// clip.pos.y,
// clip.rect.w,
// clip.rect.h,
// ), .blue);
},
.clip_end => |_| {
rl.endScissorMode();
},
} }
} }
rl.clearBackground(.white);
_ = al.reset(.retain_capacity); _ = al.reset(.retain_capacity);
// rl.drawRectangle(0, 0, 200, 100, .blue); // rl.drawRectangle(0, 0, 200, 100, .blue);