-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbutton.zig
More file actions
97 lines (83 loc) · 3.21 KB
/
Copy pathbutton.zig
File metadata and controls
97 lines (83 loc) · 3.21 KB
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
const std = @import("std");
const fintui = @import("fintui");
fn drawOutlineBox(tui: *fintui.Tui, x: u16, y: u16, width: u16, height: u16, style: fintui.Screen.Cell.Style) !void {
for (0..height) |j| {
try tui.drawCell(x, @intCast(y + j), .{
.grapheme = if (j == 0) '╭' else if (j == height - 1) '╰' else '│',
.style = style,
});
if (j == 0 or j == height - 1) {
for (0..width - 2) |i| {
try tui.drawCell(@intCast(x + i + 1), @intCast(y + j), .{
.grapheme = '─',
.style = style,
});
}
}
try tui.drawCell(@intCast(x + width - 1), @intCast(y + j), .{
.grapheme = if (j == 0) '╮' else if (j == height - 1) '╯' else '│',
.style = style,
});
}
}
pub fn main(init: std.process.Init) !void {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
var stdout_buf: [1024]u8 = undefined;
var stdout = std.Io.File.stdout().writer(init.io, &stdout_buf);
const writer = &stdout.interface;
const stdin = std.Io.File.stdin();
var tui = try fintui.Tui.init(
init.gpa,
writer,
init.io,
);
defer tui.deinit() catch {};
const message = "the button";
const Rect = struct {
x: u16,
y: u16,
width: u16,
height: u16,
};
const button: Rect = .{
.x = @intCast((tui.screen.width - message.len - 8) / 2),
.y = @intCast((tui.screen.height - 5) / 2),
.width = message.len + 8,
.height = 5,
};
var button_down: u8 = 0;
while (true) {
defer _ = arena.reset(.free_all);
defer tui.render() catch {};
_ = try tui.frameDelta(init.io, .fromNanoseconds(std.time.ns_per_s / 60)) orelse continue;
if (try fintui.event.poll(stdin.handle)) |event| {
switch (event) {
.key => |key| {
if (@intFromEnum(key) == 'q') break;
},
.mouse => |mouse| if (mouse.state == .left_down) {
if (mouse.x >= button.x and mouse.x < button.x + button.width and mouse.y >= button.y and mouse.y < button.y + button.height) {
button_down = 20;
}
},
}
}
try tui.fill(.{});
if (button_down > 0) {
button_down -= 1;
try drawOutlineBox(&tui, button.x, button.y + 1, button.width, button.height, .{
.fg = .{ .index = .red },
});
try tui.drawString(@intCast((tui.screen.width - message.len) / 2), @intCast((tui.screen.height - 1) / 2 + 1), message, .{});
} else {
try drawOutlineBox(&tui, button.x, button.y, button.width, button.height, .{
.fg = .{ .index = .red },
});
try tui.drawString(@intCast((tui.screen.width - message.len) / 2), @intCast((tui.screen.height - 1) / 2), message, .{});
try tui.drawString(button.x, button.y + button.height, "╰" ++ ("─" ** (message.len + 6)) ++ "╯", .{
.fg = .{ .index = .black },
});
}
}
}