Compare commits
3 Commits
362acfe3cf
...
c349a7ca82
| Author | SHA1 | Date | |
|---|---|---|---|
| c349a7ca82 | |||
| 058c3c4156 | |||
| 18fb90638f |
@@ -23,8 +23,9 @@ pub fn build(b: *std.Build) void {
|
||||
}),
|
||||
});
|
||||
|
||||
exe.linkLibrary(raylib_artifact);
|
||||
exe.root_module.addImport("shoots", shoots.module("shoots"));
|
||||
|
||||
exe.root_module.linkLibrary(raylib_artifact);
|
||||
exe.root_module.addImport("rl", raylib_dep.module("raylib"));
|
||||
|
||||
b.installArtifact(exe);
|
||||
|
||||
@@ -2,11 +2,11 @@
|
||||
.name = .examples,
|
||||
.version = "0.0.0",
|
||||
.fingerprint = 0x7bd0ad45c350042a,
|
||||
.minimum_zig_version = "0.15.1",
|
||||
.minimum_zig_version = "0.16.0",
|
||||
.dependencies = .{
|
||||
.raylib_zig = .{
|
||||
.url = "git+https://github.com/raylib-zig/raylib-zig?ref=devel#a4d18b2d1cf8fdddec68b5b084535fca0475f466",
|
||||
.hash = "raylib_zig-5.6.0-dev-KE8REL5MBQAf3p497t52Xw9P7ojndIkVOWPXnLiLLw2P",
|
||||
.url = "git+https://github.com/raylib-zig/raylib-zig?ref=devel#58f3c0fa328fc9ff48d17c2c3771ab9c5114aaa6",
|
||||
.hash = "raylib_zig-6.0.0-KE8REMNkBQCpxwqT9ubVNf5aEOcWRUVIaH2sgt_sDDoZ",
|
||||
},
|
||||
.shoots = .{
|
||||
.path = "../../",
|
||||
|
||||
@@ -1,26 +1,24 @@
|
||||
const std = @import("std");
|
||||
const rl = @import("rl");
|
||||
const shoots = @import("root.zig");
|
||||
const shoots = @import("shoots");
|
||||
|
||||
fn getTextWidth(string: []const u8, font: rl.Font) shoots.Real {
|
||||
var width: c_int = 0;
|
||||
for (string) |char| {
|
||||
width +=
|
||||
@as(c_int, @intFromFloat(font.recs[char - 32].width)) +
|
||||
font.glyphs[char - 32].offsetX;
|
||||
}
|
||||
fn getTextSize(string: []const u8, font: rl.Font, max_width: ?shoots.Real) shoots.Rect {
|
||||
_ = max_width;
|
||||
|
||||
const s = @as([:0]const u8, @ptrCast(string));
|
||||
return @as(shoots.Real, @floatFromInt(rl.measureText(s, font.baseSize)));
|
||||
const text_size = rl.measureTextEx(
|
||||
font,
|
||||
@as([:0]const u8, @ptrCast(string)),
|
||||
@floatFromInt(font.baseSize),
|
||||
@floatFromInt(1),
|
||||
);
|
||||
|
||||
return shoots.Rect{
|
||||
.h = @as(shoots.Real, text_size.y),
|
||||
.w = @as(shoots.Real, text_size.x),
|
||||
};
|
||||
}
|
||||
|
||||
fn getTextheight(string: []const u8, font: rl.Font) shoots.Real {
|
||||
_ = string;
|
||||
// std.debug.print("font.baseSize : {}\n", .{font.baseSize});
|
||||
return @as(shoots.Real, @floatFromInt(font.baseSize));
|
||||
}
|
||||
|
||||
const TextType = shoots.TextType(rl.Font, getTextWidth, getTextheight);
|
||||
const TextType = shoots.TextType(rl.Font, getTextSize);
|
||||
const TextureType = shoots.TextureType(rl.Texture2D);
|
||||
|
||||
const UI = shoots.Shoots(TextType, TextureType);
|
||||
@@ -99,12 +97,14 @@ inline fn button() UI.Node {
|
||||
);
|
||||
}
|
||||
|
||||
pub fn main() anyerror!void {
|
||||
pub fn main(init: std.process.Init) anyerror!void {
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
const screenWidth = 1200;
|
||||
const screenHeight = 700;
|
||||
var timer = try std.time.Timer.start();
|
||||
|
||||
const io = init.io;
|
||||
const start = std.Io.Clock.awake.now(io);
|
||||
|
||||
rl.initWindow(screenWidth, screenHeight, "raylib-zig [core] example - basic window");
|
||||
defer rl.closeWindow(); // Close window and OpenGL context
|
||||
@@ -115,7 +115,7 @@ pub fn main() anyerror!void {
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
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 : {}ms\n", .{start.untilNow(io, .awake).toMilliseconds()});
|
||||
|
||||
var scroll_offset: shoots.Pos = .{};
|
||||
|
||||
@@ -144,9 +144,12 @@ pub fn main() anyerror!void {
|
||||
),
|
||||
UI.Txt(
|
||||
UI.Text.init(
|
||||
"hello world again",
|
||||
"hello world again \n this is a new line",
|
||||
try rl.getFontDefault(),
|
||||
.{},
|
||||
.{
|
||||
.b = 255,
|
||||
.r = 255,
|
||||
},
|
||||
).setBackground(.{
|
||||
.a = 255,
|
||||
.g = 255,
|
||||
@@ -358,8 +361,6 @@ pub fn main() anyerror!void {
|
||||
}),
|
||||
};
|
||||
|
||||
// root.Shoots(root.TextType(raylib.Font,(function 'getTextWidth'),(function 'getTextheight')),root.TextureType(raylib.Texture)).NodeTypes
|
||||
// root.Shoots(root.TextType(raylib.Font,(function 'getTextWidth'),(function 'getTextheight')),root.TextureType(raylib.Texture)).Node
|
||||
const root = UI.Element(.{
|
||||
.name = "root",
|
||||
.pos = .{
|
||||
@@ -402,7 +403,8 @@ pub fn main() anyerror!void {
|
||||
rl.beginDrawing();
|
||||
defer rl.endDrawing();
|
||||
// std.debug.print("starting layout\n", .{});
|
||||
_ = timer.lap();
|
||||
var start_time = std.Io.Timestamp.now(init.io, std.Io.Clock.awake);
|
||||
|
||||
var sized = try UI.resolveSizing(al.allocator(), root);
|
||||
|
||||
const mouse_pos = rl.getMousePosition();
|
||||
@@ -423,9 +425,7 @@ pub fn main() anyerror!void {
|
||||
|
||||
const commands = try UI.getRenderCommands(sized, al.allocator());
|
||||
// std.debug.print("{f}\n", .{sized});
|
||||
std.debug.print("layout time : {}ms\n", .{
|
||||
@as(f32, @floatFromInt(timer.lap())) / @as(f32, @floatFromInt(std.time.ns_per_ms)),
|
||||
});
|
||||
std.debug.print("layout time : {}ns\n", .{start_time.untilNow(io, .awake).toNanoseconds()});
|
||||
|
||||
for (commands.items) |command| {
|
||||
// std.debug.print("command : {any}\n", .{command});
|
||||
@@ -469,7 +469,7 @@ pub fn main() anyerror!void {
|
||||
string,
|
||||
@as(i32, @intFromFloat(t.pos.x)),
|
||||
@as(i32, @intFromFloat(t.pos.y)),
|
||||
@as(i32, @intFromFloat(t.text.getTextHeight())),
|
||||
@as(i32, t.text.font.baseSize),
|
||||
rl.Color.init(
|
||||
t.text.colour.r,
|
||||
t.text.colour.g,
|
||||
@@ -504,7 +504,7 @@ pub fn main() anyerror!void {
|
||||
// clip.rect.h,
|
||||
// ), .blue);
|
||||
},
|
||||
.clip_end => |_| {
|
||||
.clip_end => {
|
||||
rl.endScissorMode();
|
||||
},
|
||||
}
|
||||
|
||||
26
src/root.zig
26
src/root.zig
@@ -22,18 +22,20 @@ const Spaceing = 4;
|
||||
|
||||
pub fn TextType(
|
||||
comptime FontType: type,
|
||||
comptime getTextWidthFunc: fn (string: []const u8, FontType) Real,
|
||||
comptime getTextHeightFunc: fn (string: []const u8, FontType) Real,
|
||||
comptime getTextSizeFunc: fn (string: []const u8, FontType, max_width: ?Real) Rect,
|
||||
) type {
|
||||
return struct {
|
||||
const Optional = struct {
|
||||
font: FontType,
|
||||
};
|
||||
// const Optional = struct {
|
||||
// font: FontType,
|
||||
// };
|
||||
const Self = @This();
|
||||
string: []const u8,
|
||||
colour: Colour,
|
||||
background: Colour,
|
||||
font: FontType,
|
||||
/// this lib just cares about rendering some text
|
||||
/// text wrapping is your job SDL3 ttf does it :D
|
||||
max_width: ?Real = null,
|
||||
|
||||
pub fn init(string: []const u8, font: FontType, colour: Colour) Self {
|
||||
return Self{
|
||||
@@ -55,12 +57,8 @@ pub fn TextType(
|
||||
return s;
|
||||
}
|
||||
|
||||
pub fn getTextWidth(self: Self) Real {
|
||||
return getTextWidthFunc(self.string, self.font);
|
||||
}
|
||||
|
||||
pub fn getTextHeight(self: Self) Real {
|
||||
return getTextHeightFunc(self.string, self.font);
|
||||
pub fn getTextSize(self: Self) Rect {
|
||||
return getTextSizeFunc(self.string, self.font, self.max_width);
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -419,9 +417,11 @@ pub fn Shoots(
|
||||
}
|
||||
},
|
||||
.text => {
|
||||
parent.element.rect.h += node.text.getTextHeight();
|
||||
const text_size: Rect = node.text.getTextSize();
|
||||
|
||||
parent.element.rect.h += text_size.h;
|
||||
// std.debug.print("parent.element.rect.h : {}\n", .{parent.element.rect.h});
|
||||
parent.element.rect.w += node.text.getTextWidth();
|
||||
parent.element.rect.w += text_size.w;
|
||||
// std.debug.print("parent.element.rect.w : {}\n", .{parent.element.rect.w});
|
||||
},
|
||||
.texture => {
|
||||
|
||||
Reference in New Issue
Block a user