panels
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
#import "modules/std.sx";
|
||||
#import "modules/allocators.sx";
|
||||
#import "modules/opengl.sx";
|
||||
#import "ui/types.sx";
|
||||
#import "ui/render.sx";
|
||||
@@ -15,12 +16,22 @@ UIPipeline :: struct {
|
||||
root: ViewChild;
|
||||
has_root: bool;
|
||||
|
||||
// Frame arena infrastructure
|
||||
arena_a: Arena;
|
||||
arena_b: Arena;
|
||||
frame_index: s64;
|
||||
body: Closure() -> View;
|
||||
has_body: bool;
|
||||
parent_allocator: Allocator;
|
||||
|
||||
init :: (self: *UIPipeline, width: f32, height: f32) {
|
||||
self.render_tree = RenderTree.init();
|
||||
self.renderer.init();
|
||||
self.screen_width = width;
|
||||
self.screen_height = height;
|
||||
self.has_root = false;
|
||||
self.has_body = false;
|
||||
self.frame_index = 0;
|
||||
}
|
||||
|
||||
init_font :: (self: *UIPipeline, path: [:0]u8, size: f32, dpi_scale: f32) {
|
||||
@@ -35,6 +46,16 @@ UIPipeline :: struct {
|
||||
self.has_root = true;
|
||||
}
|
||||
|
||||
set_body :: (self: *UIPipeline, body_fn: Closure() -> View) {
|
||||
self.body = body_fn;
|
||||
self.has_body = true;
|
||||
self.parent_allocator = context.allocator;
|
||||
// Initialize both arenas (256KB initial, grows automatically)
|
||||
self.arena_a.create(self.parent_allocator, 262144);
|
||||
self.arena_b.create(self.parent_allocator, 262144);
|
||||
self.frame_index = 0;
|
||||
}
|
||||
|
||||
resize :: (self: *UIPipeline, width: f32, height: f32) {
|
||||
self.screen_width = width;
|
||||
self.screen_height = height;
|
||||
@@ -48,9 +69,12 @@ UIPipeline :: struct {
|
||||
|
||||
// Run one frame: layout → render → commit
|
||||
tick :: (self: *UIPipeline) {
|
||||
if self.has_body {
|
||||
self.tick_with_body();
|
||||
return;
|
||||
}
|
||||
if self.has_root == false { return; }
|
||||
|
||||
screen := Frame.make(0.0, 0.0, self.screen_width, self.screen_height);
|
||||
proposal := ProposedSize.fixed(self.screen_width, self.screen_height);
|
||||
|
||||
// Layout
|
||||
@@ -67,11 +91,49 @@ UIPipeline :: struct {
|
||||
self.root.view.render(@ctx, self.root.computed_frame);
|
||||
|
||||
// Commit to GPU
|
||||
self.commit_gpu();
|
||||
}
|
||||
|
||||
tick_with_body :: (self: *UIPipeline) {
|
||||
build_arena : *Arena = if self.frame_index & 1 == 0 then @self.arena_a else @self.arena_b;
|
||||
build_arena.reset();
|
||||
|
||||
// Reset render_tree nodes (backing is stale after arena reset)
|
||||
self.render_tree.nodes.items = xx 0;
|
||||
self.render_tree.nodes.len = 0;
|
||||
self.render_tree.nodes.cap = 0;
|
||||
|
||||
push Context.{ allocator = xx build_arena, data = context.data } {
|
||||
// Workaround: self.body() crashes through struct field (issue-0010)
|
||||
body_fn := self.body;
|
||||
root_view := body_fn();
|
||||
self.root = ViewChild.{ view = root_view };
|
||||
self.has_root = true;
|
||||
|
||||
proposal := ProposedSize.fixed(self.screen_width, self.screen_height);
|
||||
root_size := self.root.view.size_that_fits(proposal);
|
||||
self.root.computed_frame = Frame.{
|
||||
origin = Point.zero(),
|
||||
size = root_size
|
||||
};
|
||||
self.root.view.layout(self.root.computed_frame);
|
||||
|
||||
self.render_tree.clear();
|
||||
ctx := RenderContext.init(@self.render_tree);
|
||||
self.root.view.render(@ctx, self.root.computed_frame);
|
||||
|
||||
self.commit_gpu();
|
||||
}
|
||||
|
||||
self.frame_index += 1;
|
||||
}
|
||||
|
||||
commit_gpu :: (self: *UIPipeline) {
|
||||
glEnable(GL_BLEND);
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
|
||||
self.renderer.begin(self.screen_width, self.screen_height);
|
||||
self.renderer.begin(self.screen_width, self.screen_height, self.font.texture_id);
|
||||
self.renderer.process(@self.render_tree);
|
||||
self.renderer.flush();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user