clickable boxes

master
Patrick Cleavelin 2023-06-21 22:02:13 -05:00
parent 8ddd30ee88
commit d0880ab419
1 changed files with 50 additions and 41 deletions

View File

@ -41,6 +41,9 @@ var root_box: ?*UI_Box = null;
var current_box: ?*UI_Box = null; var current_box: ?*UI_Box = null;
var pushing_box: bool = false; var pushing_box: bool = false;
var popping_box: bool = false; var popping_box: bool = false;
var mouse_x: i32 = 0;
var mouse_y: i32 = 0;
var mouse_released: bool = false;
fn DeleteBoxChildren(box: *UI_Box, should_destroy: bool) void { fn DeleteBoxChildren(box: *UI_Box, should_destroy: bool) void {
if (box.first) |child| { if (box.first) |child| {
@ -50,8 +53,8 @@ fn DeleteBoxChildren(box: *UI_Box, should_destroy: bool) void {
} }
} }
fn MakeBox(label: [:0]const u8, flags: UI_Flags) anyerror!*UI_Box { fn MakeBox(label: [:0]const u8, flags: UI_Flags) anyerror!bool {
std.debug.print("making box '{s}'...", .{label}); //std.debug.print("making box '{s}'...", .{label});
popping_box = false; popping_box = false;
if (pushing_box) { if (pushing_box) {
@ -65,16 +68,15 @@ fn MakeBox(label: [:0]const u8, flags: UI_Flags) anyerror!*UI_Box {
if (box.next) |next| { if (box.next) |next| {
// Attempt to re-use cache // Attempt to re-use cache
if (std.mem.eql(u8, next.label, label)) { if (std.mem.eql(u8, next.label, label)) {
std.debug.print("using cache for '{s}'\n", .{next.label}); //std.debug.print("using cache for '{s}'\n", .{next.label});
next.flags = @enumToInt(flags); next.flags = @enumToInt(flags);
if (next.parent) |parent| { if (next.parent) |parent| {
parent.last = next; parent.last = next;
} }
current_box = next; current_box = next;
return next;
} else { } else {
// Invalid cache, delete next sibling while retaining the following one // Invalid cache, delete next sibling while retaining the following one
std.debug.print("make_box: invalidating cache for '{s}' when making new box '{s}'\n", .{ next.label, label }); //std.debug.print("make_box: invalidating cache for '{s}' when making new box '{s}'\n", .{ next.label, label });
const following_sibling = next.next; const following_sibling = next.next;
DeleteBoxChildren(next, false); DeleteBoxChildren(next, false);
@ -95,12 +97,10 @@ fn MakeBox(label: [:0]const u8, flags: UI_Flags) anyerror!*UI_Box {
if (next.parent) |parent| { if (next.parent) |parent| {
parent.last = next; parent.last = next;
} }
return next;
} }
} else { } else {
// No existing cache, create new box // No existing cache, create new box
std.debug.print("make_box: allocating new box: {s}\n", .{label}); //std.debug.print("make_box: allocating new box: {s}\n", .{label});
var new_box = try box_allocator.create(UI_Box); var new_box = try box_allocator.create(UI_Box);
new_box.* = UI_Box{ new_box.* = UI_Box{
.label = label, .label = label,
@ -120,11 +120,9 @@ fn MakeBox(label: [:0]const u8, flags: UI_Flags) anyerror!*UI_Box {
if (new_box.parent) |parent| { if (new_box.parent) |parent| {
parent.last = new_box; parent.last = new_box;
} }
return new_box;
} }
} else { } else {
std.debug.print("make_box: allocating new box: {s}\n", .{label}); //std.debug.print("make_box: allocating new box: {s}\n", .{label});
var new_box = try box_allocator.create(UI_Box); var new_box = try box_allocator.create(UI_Box);
new_box.* = UI_Box{ new_box.* = UI_Box{
.label = label, .label = label,
@ -140,12 +138,19 @@ fn MakeBox(label: [:0]const u8, flags: UI_Flags) anyerror!*UI_Box {
}; };
current_box = new_box; current_box = new_box;
return new_box;
} }
if (current_box) |box| {
if (box.flags & @enumToInt(UI_Flags.clickable) > 0) {
return TestBoxClick(box);
}
}
return false;
} }
fn PushBox(label: [:0]const u8, flags: UI_Flags) anyerror!*UI_Box { fn PushBox(label: [:0]const u8, flags: UI_Flags) anyerror!bool {
std.debug.print("pushing box '{s}'...", .{label}); //std.debug.print("pushing box '{s}'...", .{label});
if (popping_box) { if (popping_box) {
const box = try MakeBox(label, flags); const box = try MakeBox(label, flags);
@ -162,17 +167,16 @@ fn PushBox(label: [:0]const u8, flags: UI_Flags) anyerror!*UI_Box {
if (box.first) |first| { if (box.first) |first| {
// check if the same // check if the same
if (std.mem.eql(u8, first.label, label)) { if (std.mem.eql(u8, first.label, label)) {
std.debug.print("using cache for '{s}'\n", .{first.label}); //std.debug.print("using cache for '{s}'\n", .{first.label});
first.flags = @enumToInt(flags); first.flags = @enumToInt(flags);
current_box = first; current_box = first;
if (first.parent) |parent| { if (first.parent) |parent| {
parent.last = first; parent.last = first;
} }
return first;
} else { } else {
// Invalid cache // Invalid cache
std.debug.print("push_box: invalidating cache for '{s}' when making new box '{s}'\n", .{ first.label, label }); //std.debug.print("push_box: invalidating cache for '{s}' when making new box '{s}'\n", .{ first.label, label });
const following_sibling = first.next; const following_sibling = first.next;
DeleteBoxChildren(first, false); DeleteBoxChildren(first, false);
@ -193,10 +197,9 @@ fn PushBox(label: [:0]const u8, flags: UI_Flags) anyerror!*UI_Box {
if (first.parent) |parent| { if (first.parent) |parent| {
parent.last = first; parent.last = first;
} }
return first;
} }
} else { } else {
std.debug.print("push_box: allocating new box: {s}\n", .{label}); //std.debug.print("push_box: allocating new box: {s}\n", .{label});
var new_box = try box_allocator.create(UI_Box); var new_box = try box_allocator.create(UI_Box);
new_box.* = UI_Box{ new_box.* = UI_Box{
.label = label, .label = label,
@ -216,16 +219,23 @@ fn PushBox(label: [:0]const u8, flags: UI_Flags) anyerror!*UI_Box {
if (new_box.parent) |parent| { if (new_box.parent) |parent| {
parent.last = new_box; parent.last = new_box;
} }
return new_box;
} }
} else { } else {
pushing_box = false; pushing_box = false;
return try MakeBox(label, flags); return try MakeBox(label, flags);
} }
if (current_box) |box| {
if (box.flags & @enumToInt(UI_Flags.clickable) > 0) {
return TestBoxClick(box);
}
}
return false;
} }
fn PopBox() void { fn PopBox() void {
std.debug.print("popping box...", .{}); //std.debug.print("popping box...", .{});
if (current_box) |box| { if (current_box) |box| {
//if (box.parent) |parent| { //if (box.parent) |parent| {
//current_box = parent.last; //current_box = parent.last;
@ -236,21 +246,15 @@ fn PopBox() void {
return; return;
} }
std.debug.print("couldn't pop box\n", .{}); //std.debug.print("couldn't pop box\n", .{});
} }
fn TestBoxClick(box: *UI_Box, mouse_x: f32, mouse_y: f32, mouse_clicked: bool) bool { fn TestBoxClick(box: *UI_Box) bool {
return mouse_clicked and mouse_x >= box.computed_pos.x and mouse_x <= box.computed_pos.x + box.computed_size.x and mouse_y >= box.computed_pos.y and mouse_y <= box.computed_pos.y + box.computed_size.y; return mouse_released and @intToFloat(f32, mouse_x) >= box.computed_pos.x and @intToFloat(f32, mouse_x) <= box.computed_pos.x + box.computed_size.x and @intToFloat(f32, mouse_y) >= box.computed_pos.y and @intToFloat(f32, mouse_y) <= box.computed_pos.y + box.computed_size.y;
} }
fn MakeButton(label: [:0]const u8) !bool { fn MakeButton(label: [:0]const u8) !bool {
var box = try MakeBox(label, UI_Flags.clickable); return try MakeBox(label, UI_Flags.clickable);
const mouse_x = 0;
const mouse_y = 0;
const mouse_clicked = false;
return TestBoxClick(box, mouse_x, mouse_y, mouse_clicked);
} }
fn CountChildren(box: *UI_Box) u32 { fn CountChildren(box: *UI_Box) u32 {
@ -282,19 +286,19 @@ fn CountSiblings(box: *UI_Box) u32 {
fn DrawUI(box: *UI_Box, parent: ?*UI_Box, parent_pos: Vec2, parent_size: Vec2) void { fn DrawUI(box: *UI_Box, parent: ?*UI_Box, parent_pos: Vec2, parent_size: Vec2) void {
//DrawRectangle(int posX, int posY, int width, int height, Color color //DrawRectangle(int posX, int posY, int width, int height, Color color
std.debug.print("\n\ndrawing {s}\n", .{box.label}); //std.debug.print("\n\ndrawing {s}\n", .{box.label});
const num_siblings = if (parent) |p| (CountChildren(p) - 1) else 0; const num_siblings = if (parent) |p| (CountChildren(p) - 1) else 0;
std.debug.print("num_siblings {d}\n", .{num_siblings}); //std.debug.print("num_siblings {d}\n", .{num_siblings});
const num_children = CountChildren(box); //const num_children = CountChildren(box);
std.debug.print("num_children {d}\n", .{num_children}); //std.debug.print("num_children {d}\n", .{num_children});
const num_siblings_after_me = CountSiblings(box); const num_siblings_after_me = CountSiblings(box);
std.debug.print("num_siblings_after_me {d}\n", .{num_siblings_after_me}); //std.debug.print("num_siblings_after_me {d}\n", .{num_siblings_after_me});
const my_index = num_siblings - num_siblings_after_me; const my_index = num_siblings - num_siblings_after_me;
std.debug.print("num_index {d}\n", .{my_index}); //std.debug.print("num_index {d}\n", .{my_index});
box.computed_size = Vec2{ box.computed_size = Vec2{
.x = parent_size.x / (@intToFloat(f32, num_siblings) + 1), .x = parent_size.x / (@intToFloat(f32, num_siblings) + 1),
@ -329,15 +333,20 @@ pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; var gpa = std.heap.GeneralPurposeAllocator(.{}){};
box_allocator = gpa.allocator(); box_allocator = gpa.allocator();
root_box = try PushBox("RootContainer", UI_Flags.nothing); _ = try PushBox("RootContainer", UI_Flags.nothing);
root_box = current_box;
std.debug.print("Starting main loop\n", .{}); //std.debug.print("Starting main loop\n", .{});
var ran = false; var ran = false;
while (!raylib.WindowShouldClose()) { while (!raylib.WindowShouldClose()) {
current_box = root_box; current_box = root_box;
pushing_box = false; pushing_box = false;
popping_box = false; popping_box = false;
mouse_x = raylib.GetMouseX();
mouse_y = raylib.GetMouseY();
mouse_released = raylib.IsMouseButtonReleased(raylib.MouseButton.MOUSE_BUTTON_LEFT);
raylib.BeginDrawing(); raylib.BeginDrawing();
defer raylib.EndDrawing(); defer raylib.EndDrawing();
@ -345,10 +354,10 @@ pub fn main() !void {
_ = try PushBox("ButtonArray", UI_Flags.nothing); _ = try PushBox("ButtonArray", UI_Flags.nothing);
if (try MakeButton("click me")) { if (try MakeButton("click me")) {
std.debug.print("button clicked", .{}); std.debug.print("button clicked\n", .{});
} }
if (try MakeButton("click me 2")) { if (try MakeButton("click me 2")) {
std.debug.print("button 2 clicked", .{}); std.debug.print("button 2 clicked\n", .{});
} }
PopBox(); PopBox();