1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
const std = @import("std");
const builtin = @import("builtin");
const mem = std.mem;
const fmt = std.fmt;
const fs = std.fs;
const io = std.io;
const Allocator = mem.Allocator;
const Thread = std.Thread;
const input = @import("input.zig");
const gui = @import("ui.zig");
var allocator: Allocator = undefined;
pub const Pixel = packed struct {
r: u8 = 0,
g: u8 = 0,
b: u8 = 0,
_a: u8 = undefined,
};
var pixel_buffer: *const []Pixel = undefined;
pub const Action = enum {
primary,
text_cursor_left,
text_cursor_right,
};
// DEBUG
var img: gui.Image = undefined;
var font: gui.Font = undefined;
pub fn init() !void {
allocator = @import("root").allocator;
img = try .fromPath("assets/img/test_image.png", allocator);
//font = try .fromPath("assets/font/OpendyslexicRegular.otf", allocator, .{.font_height = 128});
font = try .fromPath("assets/font/OpenSans-VariableFont_wdth,wght.ttf", allocator, .{.font_height = 256});
try input.bindPointerButton(.primary, .left);
try input.bindKey(.text_cursor_left, .arrow_left);
try input.bindKey(.text_cursor_right, .arrow_right);
try gui.globalInit(allocator);
ui = try gui.Context.init(.{.allocator = allocator});
errdefer ui.deinit();
user_input = try ui.textBufferFromString("samplwwwawwwwwwwwwwwwwwwwwwwwwwwwwwwww");
}
pub fn deinit() void {
font.deinit();
img.deinit();
user_input.deinit();
ui.deinit();
gui.globalDeinit();
}
var window_size: @Vector(2, u32) = undefined;
var fwindow_size: @Vector(2, f32) = undefined;
pub fn setWindowSize(ws: @Vector(2, c_uint)) void {
window_size = @intCast(ws);
fwindow_size = @floatFromInt(window_size);
pixel_buffer = @ptrCast(@import("root").pixels);
}
var ui: gui.Context = undefined;
var user_input: gui.TextBuffer = undefined;
var dbg_poss = [3]gui.V2{.{0.1, 0.1}, .{0.2, 0.2}, .{0.3, 0.3}};
var dbg_colors = [3]gui.Color{.hex(0xFFFF0000), .hex(0xFF00FF00), .hex(0xFF0000FF)};
var time: f32 = 0;
pub fn loop(dt: f32) !bool {
// TODO styles or something. TBD once we have some real world tests
ui.frameStart();
//std.debug.print("{d}\n", .{gui.mouse_delta});
//if (@reduce(.And, gui.mouse_delta == @Vector(2, f32){0, 0})) return false;
time += dt;
try ui.textFmt("{d:.1} FPS", .{1/dt}, .{.pos = .{0, 0}, .size = .{1, 1}}, .{.font = &font, .scale = 0.1});
//std.debug.print("{d:.1}\n", .{1/dt});
//const pos = gui.V2{-1+time*0.1,-1+time*0.1};
//const color = gui.Color.float(1,1,0,0.5);
//try ui.rectangle(.{.pos = pos, .size = .{1,1}}, .{.color = color});
try ui.rectangle(.{.pos = .{0,0}, .size = .{1,1}}, .{.color = .float(1,1,1,1), .image = img});
//try ui.areaBegin(.{.rect = .{.pos = .{time * 0.0, 0.0}, .size = .{0.9-0.0*time, 0.9}}, .children_offset = .{time * 0.0, -time * 0.1}});
//for (0..1) |i| {
// try ui.textField(&user_input, @import("root").utf8_of_keys_pressed, .{.pos = .{0, 0.1 + 0.1 * @as(f32, @floatFromInt(i)) / 3}, .size = .{1, 1}}, .{
// .text_options = .{.font = &font, .scale = 0.05},
// .cursor_to_hovered_glpyh_if = input.ended(.primary),
// .cursor_move_left = input.ended(.text_cursor_left),
// .cursor_move_right = input.ended(.text_cursor_right),
// });
//}
//try ui.rectangle(.{.pos = .{time * 0.0, 0}, .size = .{1,1}}, .{
// .color = .hex(0xffff0000),
//});
//try ui.rectangle(ui._current_area.rect, .{.color = .hex(0xff00ff00), .apply_area_transformations = false, .clip_to_area = true});
//try ui.areaEnd();
//for (&dbg_poss, &dbg_colors) |*dbg_pos, dbg_color| {
// var rect_drag: ?gui.V2 = null;
// try ui.rectangle(.{.pos = dbg_pos.*, .size = .{1, 0.2}}, .{
// .color = dbg_color,
// .drag = &rect_drag,
// .begin_drag_if_hover_and = input.started(.primary),
// .end_drag_if = input.ended(.primary),
// });
// if (rect_drag) |drag| {
// dbg_pos.* += drag;
// dbg_pos[0] = @max(dbg_pos[0], 0);
// dbg_pos[1] = @max(dbg_pos[1], 0);
// }
//}
gui.clear(@ptrCast(pixel_buffer.*));
try ui.draw(.{
.pixels = @ptrCast(pixel_buffer.*),
.bytes_per_pixel = @sizeOf(Pixel),
.pixels_per_row = window_size[0],
});
return true;
}
|