@@ -145,11 +145,22 @@ pub const LayoutAlign = enum {
145145/// top to bottom, or every item is drawn in the centre.
146146pub const LayoutDirection = enum {
147147 top_to_bottom ,
148+
149+ /// Place all items along the top, from left to right
148150 left_to_right ,
151+
152+ /// Place items along the top, but wrap if you reach
153+ /// the end of the line.
154+ left_to_right_wrap ,
155+
149156 //right_to_left,
150157 //bottom_to_top,
158+
159+ /// Place all items centred in the middle of a panel
151160 centre ,
152- left_to_right_wrap ,
161+
162+ /// Place all items in the top left
163+ top_left ,
153164};
154165
155166/// The `normal` scale is designed for a regular person with regular
@@ -986,15 +997,7 @@ pub const Element = struct {
986997
987998 /// `add` a child element to this panel and return the element. Only
988999 /// permitted for the `panel` element type.
989- pub inline fn add (self : * Element , allocator : Allocator , child : * Element ) error {OutOfMemory }! * Element {
990- std .debug .assert (self .type == .panel );
991- try self .type .panel .children .append (allocator , child );
992- return child ;
993- }
994-
995- /// `add_alloc` a child element to this panel and return the element. Only
996- /// permitted for the `panel` element type.
997- pub inline fn add_alloc (
1000+ pub inline fn add (
9981001 self : * Element ,
9991002 allocator : Allocator ,
10001003 display : * Display ,
@@ -3297,6 +3300,7 @@ pub const Display = struct {
32973300 .left_to_right_wrap = > place_children_left_to_right_wrap (self , parent ),
32983301 .top_to_bottom = > place_children_top_to_bottom (self , parent , expanders .slice (), expander_weights ),
32993302 .centre = > place_children_centred (self , parent ),
3303+ .top_left = > place_children_top_left (self , parent ),
33003304 }
33013305
33023306 // Descend into child elements to allow child panels to also resize.
@@ -3344,6 +3348,16 @@ pub const Display = struct {
33443348 //parent.type.panel.scrollable.size.height = @max(needed_height, parent.height);
33453349 }
33463350
3351+ inline fn place_children_top_left (_ : * Display , parent : * Element ) void {
3352+ for (parent .type .panel .children .items ) | child | {
3353+ if (child .layout .position == .float ) continue ;
3354+ if (child .visible == .hidden ) continue ;
3355+ if (child .type == .expander ) continue ;
3356+ child .rect .x = parent .rect .x + parent .pad .left ;
3357+ child .rect .y = parent .rect .y + parent .pad .top ;
3358+ }
3359+ }
3360+
33473361 inline fn place_children_top_to_bottom (
33483362 _ : * Display ,
33493363 parent : * Element ,
@@ -3764,7 +3778,7 @@ pub const Display = struct {
37643778 });
37653779 unreachable ; // add_panel only accepts panels
37663780 }
3767- return self .root .add_alloc (allocator , self , element );
3781+ return self .root .add (allocator , self , element );
37683782 }
37693783
37703784 /// Convert a text string into an image that is sent as a texture to
@@ -3805,6 +3819,10 @@ pub const Display = struct {
38053819 /// elements. This releases a texture, only when all references to
38063820 /// a texture no longer exist.
38073821 pub fn release_texture_resource (self : * Display , allocator : Allocator , ti : * TextureInfo ) void {
3822+ if (ti .references == 0 ) {
3823+ err ("Attempt to release resource with no references" , .{});
3824+ return ;
3825+ }
38083826 ti .references -= 1 ;
38093827 if (ti .references != 0 ) {
38103828 if (ti .references < 0 ) {
@@ -4891,7 +4909,7 @@ pub const Display = struct {
48914909 parent : * Element ,
48924910 close_fn : Callback ,
48934911 ) (Error || Allocator .Error || Resources .Error )! * Element {
4894- return try parent .add_alloc (
4912+ return try parent .add (
48954913 allocator ,
48964914 display ,
48974915 .{
@@ -4921,17 +4939,13 @@ pub const Display = struct {
49214939 parent : * Element ,
49224940 size : f32 ,
49234941 ) (Error || Allocator .Error || Resources .Error )! * Element {
4924- return try parent .add (allocator , try engine .create_panel (
4925- allocator ,
4926- display ,
4927- .{
4928- .name = "spacer" ,
4929- .rect = .{ .width = size , .height = size },
4930- .layout = .{ .x = .shrinks , .y = .shrinks },
4931- .minimum = .{ .width = size , .height = size },
4932- .type = .{ .panel = .{} },
4933- },
4934- ));
4942+ return try parent .add (allocator , display , .{
4943+ .name = "spacer" ,
4944+ .rect = .{ .width = size , .height = size },
4945+ .layout = .{ .x = .shrinks , .y = .shrinks },
4946+ .minimum = .{ .width = size , .height = size },
4947+ .type = .{ .panel = .{} },
4948+ });
49354949 }
49364950
49374951 /// Add a label with generic settings needed for a paragraph.
@@ -4943,7 +4957,7 @@ pub const Display = struct {
49434957 name : []const u8 ,
49444958 text : []const u8 ,
49454959 ) (Error || Allocator .Error || Resources .Error )! void {
4946- _ = try parent .add_alloc (allocator , display , .{
4960+ _ = try parent .add (allocator , display , .{
49474961 .name = name ,
49484962 .focus = .accessibility_focus ,
49494963 .rect = .{ .x = 250 , .y = 50 , .width = 500 , .height = 80 },
@@ -5028,7 +5042,7 @@ fn find_minimum_panel_height(parent: *const Element, display: *Display) f32 {
50285042 return result ;
50295043 },
50305044
5031- .centre , .left_to_right , .left_to_right_wrap = > {
5045+ .centre , .left_to_right , .left_to_right_wrap , .top_left = > {
50325046 // centred all together
50335047 // a, next to b, next c.
50345048 //
@@ -5129,7 +5143,7 @@ fn find_minimum_panel_width(parent: *const Element, display: *Display) f32 {
51295143 }
51305144 return @max (minimum_needed , parent .minimum .width );
51315145 },
5132- .centre , .top_to_bottom = > {
5146+ .centre , .top_to_bottom , .top_left = > {
51335147 // a, centred upon b, centred upon c
51345148 // a, then b underneath, thn c underneath...
51355149 //
@@ -5785,29 +5799,28 @@ test "button sizing" {
57855799 _ = try display .load_font (allocator , "Roboto-Light" );
57865800 try eq (1 , display .fonts .items .len );
57875801
5788- var panel = try create_panel (allocator , display , .{
5802+ const panel = try display . add_panel (allocator , .{
57895803 .minimum = .{ .width = 5 , .height = 8 },
57905804 .type = .{ .panel = .{ .spacing = 0 , .direction = .left_to_right } },
57915805 .layout = .{ .x = .shrinks , .y = .shrinks },
57925806 });
5793- try display .add_panel (allocator , panel );
57945807 try eq (5 , panel .shrink_width (display , 500 ));
57955808 try eq (8 , panel .shrink_height (display , 500 ));
57965809
5797- const button = try create_button (allocator , display , .{
5810+ var button = try panel . add (allocator , display , .{
57985811 .visible = .visible ,
57995812 .rect = .{ .width = 50 , .height = 50 },
58005813 .minimum = .{ .width = 42 , .height = 41 },
58015814 .maximum = .{ .width = 82 , .height = 81 },
58025815 .type = .{ .button = .{ .text = "" } },
58035816 });
5817+ display .relayout ();
58045818 try eq (50 , button .shrink_width (display , 500 ));
58055819 try eq (50 , button .shrink_height (display , 500 ));
58065820 button .layout .x = .shrinks ;
58075821 button .layout .y = .shrinks ;
58085822 try eq (42 , button .shrink_width (display , 500 ));
58095823 try eq (41 , button .shrink_height (display , 500 ));
5810- _ = try panel .add (allocator , button );
58115824 display .relayout ();
58125825 try eq (42 , panel .shrink_width (display , 500 ));
58135826 try eq (42 , button .rect .width );
@@ -5957,21 +5970,20 @@ test "text input sizing" {
59575970 label .layout .x = .grows ;
59585971 try eq (91 , @round (label .shrink_width (display , 500 )));
59595972
5960- var panel = try create_panel (allocator , display , .{
5973+ var panel = try display . add_panel (allocator , .{
59615974 .rect = .{ .width = 500 , .height = 200 },
59625975 .minimum = .{ .width = 5 , .height = 8 },
59635976 .type = .{ .panel = .{ .spacing = 0 , .direction = .top_to_bottom } },
59645977 .layout = .{ .x = .shrinks , .y = .shrinks },
59655978 });
5966- try display .add_panel (allocator , panel );
59675979 try eq (5 , panel .shrink_width (display , 500 ));
59685980 try eq (8 , panel .shrink_height (display , 500 ));
59695981
59705982 panel .layout .x = .shrinks ;
59715983 panel .layout .y = .shrinks ;
59725984 label .layout .x = .grows ;
59735985 label .layout .y = .shrinks ;
5974- _ = try panel .add (allocator , label );
5986+ _ = try panel .add (allocator , display , label );
59755987 label .pad .top = 0 ;
59765988 label .pad .bottom = 0 ;
59775989 display .relayout ();
@@ -5995,26 +6007,23 @@ test "test_init" {
59956007 const allocator = std .testing .allocator ;
59966008 var display = try Display .create (allocator , "test" , "test" , "test" , "./test/repo" , null , "test translation" , 0 );
59976009 defer display .destroy (allocator );
5998- var panel = try create_panel (allocator , display , .{
6010+ var panel = try display . add_panel (allocator , .{
59996011 .rect = .{ .width = 500 , .height = 200 },
60006012 .minimum = .{ .width = 5 , .height = 8 },
60016013 .type = .{ .panel = .{ .spacing = 0 , .direction = .top_to_bottom } },
60026014 .layout = .{ .x = .shrinks , .y = .shrinks },
60036015 });
6004- try display .add_panel (allocator , panel );
60056016
60066017 try eq (1 , display .root .type .panel .children .items .len );
6007- const element = try allocator .create (Element );
6008- element .* = .{
6018+ _ = try panel .add (allocator , .{
60096019 .name = "menu_bg" ,
60106020 .rect = .{ .x = 0 , .y = 0 , .width = 550 , .height = 100 },
60116021 .minimum = .{ .width = 300 , .height = 130 },
60126022 .layout = .{ .x = .fixed , .y = .fixed , .position = .float },
60136023 .background = .{ .colour = .{ .r = 99 , .g = 150 , .b = 50 , .a = 255 } },
60146024 .style = .background ,
60156025 .type = .{ .rectangle = .{} },
6016- };
6017- _ = try panel .add (allocator , element );
6026+ });
60186027 try eq (1 , display .root .type .panel .children .items [0 ].type .panel .children .items .len );
60196028}
60206029
0 commit comments