@@ -607,6 +607,10 @@ pub const Element = struct {
607607 tint_texture (texture , display .theme .failed_button_colour );
608608 return ;
609609 },
610+ .custom = > {
611+ tint_texture (texture , self .background .colour );
612+ return ;
613+ },
610614 else = > {
611615 // Otherwise apply toggle colurs if needed
612616 },
@@ -766,6 +770,52 @@ pub const Element = struct {
766770 }
767771 }
768772
773+ pub inline fn set_image (
774+ self : * Element ,
775+ gpa : Allocator ,
776+ display : * Display ,
777+ repository : * Resources ,
778+ name : []const u8 ,
779+ ) (Allocator .Error || Resources .Error || engine .Error )! ? * TextureInfo {
780+ const start = std .time .milliTimestamp ();
781+ const texture = try display .load_texture_resource_from_repo (gpa , repository , name );
782+ if (texture == null ) {
783+ info ("set_image failed to find image resource named \" {s}\" " , .{name });
784+ return null ;
785+ }
786+ const end = std .time .milliTimestamp ();
787+ debug ("set_image loaded image named \" {s}\" in {d}ms" , .{ name , end - start });
788+
789+ if (self .texture != null ) {
790+ display .release_texture_resource (gpa , self .texture .? );
791+ self .texture = null ;
792+ }
793+ self .texture = texture .? ;
794+ return texture ;
795+ }
796+
797+ pub inline fn set_background_image (
798+ self : * Element ,
799+ gpa : Allocator ,
800+ display : * Display ,
801+ repository : * Resources ,
802+ name : []const u8 ,
803+ ) (Allocator .Error || Resources .Error || engine .Error )! ? * TextureInfo {
804+ const texture = try display .load_texture_resource_from_repo (gpa , repository , name );
805+ if (texture == null ) {
806+ info ("set_image failed to find image resource named \" {s}\" " , .{name });
807+ return null ;
808+ }
809+ debug ("set_background_image loaded image named \" {s}\" " , .{name });
810+
811+ if (self .background .image != null ) {
812+ display .release_texture_resource (gpa , self .background .image .? );
813+ self .background .image = null ;
814+ }
815+ self .background .image = texture .? ;
816+ return texture ;
817+ }
818+
769819 /// set_text updates the `text` and `translation` fields of labels,
770820 /// checkboxes and buttons, and regenerates the grahpics/image
771821 /// textures for each word if the text was changed or `forced`
@@ -1484,6 +1534,7 @@ pub const Element = struct {
14841534 _ : ? Clip ,
14851535 scroll_offset : Vector ,
14861536 ) void {
1537+ //debug("ds {s} {d}x{d}", .{ element.name, element.rect.width, element.rect.height });
14871538 if (element .texture ) | texture | {
14881539 var dest : Rect = .{
14891540 .x = element .rect .x + element .pad .left ,
@@ -1622,12 +1673,12 @@ pub const Element = struct {
16221673 dest .y += dest .height ;
16231674 dest .height = 0 - dest .height ;
16241675 }
1676+ element .apply_background_tint (display , background_image );
16251677 if (element .background .image_corner_radius == 0 ) {
16261678 _ = sdl .SDL_RenderTexture (display .renderer , background_image , null , @ptrCast (& dest ));
16271679 } else {
16281680 var corner : f32 = element .background .corner_radius ;
16291681 if (corner * 2 > dest .height ) corner = dest .height / 2 ;
1630- element .apply_background_tint (display , background_image );
16311682 _ = sdl .SDL_RenderTexture9Grid (
16321683 display .renderer ,
16331684 background_image ,
@@ -1683,7 +1734,19 @@ pub const Element = struct {
16831734 dest .height = 0 - dest .height ;
16841735 }
16851736 if (element .type .button .style != .custom ) {
1686- _ = sdl .SDL_SetTextureColorMod (icon_image , tint .r , tint .g , tint .b );
1737+ _ = sdl .SDL_SetTextureColorMod (
1738+ icon_image ,
1739+ tint .r ,
1740+ tint .g ,
1741+ tint .b ,
1742+ );
1743+ } else {
1744+ _ = sdl .SDL_SetTextureColorMod (
1745+ icon_image ,
1746+ element .colour .r ,
1747+ element .colour .g ,
1748+ element .colour .b ,
1749+ );
16871750 }
16881751 _ = sdl .SDL_RenderTexture (display .renderer , icon_image , null , @ptrCast (& dest ));
16891752 }
@@ -2576,7 +2639,7 @@ pub const Display = struct {
25762639 debug ("Checking for desktop icon" , .{});
25772640 if (try display .resources .lookupOne ("desktop icon" , .image , gpa )) | resource | {
25782641 var surface : SurfaceInfo = undefined ;
2579- try display .make_surface_from_resource (resource , gpa , & surface );
2642+ try display .make_surface_from_resource (display . resources , resource , gpa , & surface );
25802643 defer surface .deinit (gpa );
25812644 if (! sdl .SDL_SetWindowIcon (window , surface .surface )) {
25822645 info ("Did not set desktop icon" , .{});
@@ -3498,10 +3561,56 @@ pub const Display = struct {
34983561 return null ;
34993562 }
35003563 var si : SurfaceInfo = undefined ;
3501- try self .make_surface_from_resource (resource .? , allocator , & si );
3564+ try self .make_surface_from_resource (self . resources , resource .? , allocator , & si );
35023565 defer si .deinit (allocator );
35033566
3567+ const start = std .time .milliTimestamp ();
35043568 const texture = sdl .SDL_CreateTextureFromSurface (self .renderer , si .surface );
3569+ const end = std .time .milliTimestamp ();
3570+ debug ("sdl create texture in {d}ms" , .{end - start });
3571+
3572+ const ti = try TextureInfo .create (allocator , name , texture );
3573+ ti .references += 1 ;
3574+ try self .textures .put (allocator , ti .name , ti );
3575+ return ti ;
3576+ }
3577+
3578+ /// Load an image from the resource bundle or resource directory.
3579+ pub fn load_texture_resource_from_repo (
3580+ self : * Display ,
3581+ allocator : Allocator ,
3582+ bucket : * Resources ,
3583+ name : []const u8 ,
3584+ ) (Error || Allocator .Error || Resources .Error )! ? * TextureInfo {
3585+ if (name .len == 0 ) {
3586+ return null ;
3587+ }
3588+
3589+ if (self .textures .get (name )) | texture | {
3590+ texture .references += 1 ;
3591+ return texture ;
3592+ }
3593+
3594+ var start = std .time .milliTimestamp ();
3595+ const resource = try bucket .lookupOne (name , .image , allocator );
3596+ if (resource == null ) {
3597+ return null ;
3598+ }
3599+ var end = std .time .milliTimestamp ();
3600+ debug ("search image named \" {s}\" in {d}ms" , .{ name , end - start });
3601+ start = end ;
3602+
3603+ var si : SurfaceInfo = undefined ;
3604+ try self .make_surface_from_resource (bucket , resource .? , allocator , & si );
3605+ defer si .deinit (allocator );
3606+ end = std .time .milliTimestamp ();
3607+ debug ("made surface named \" {s}\" in {d}ms" , .{ name , end - start });
3608+
3609+ start = std .time .milliTimestamp ();
3610+ const texture = sdl .SDL_CreateTextureFromSurface (self .renderer , si .surface );
3611+ end = std .time .milliTimestamp ();
3612+ debug ("sdl create texture in {d}ms" , .{end - start });
3613+
35053614 const ti = try TextureInfo .create (allocator , name , texture );
35063615 ti .references += 1 ;
35073616 try self .textures .put (allocator , ti .name , ti );
@@ -3520,13 +3629,14 @@ pub const Display = struct {
35203629 }
35213630 };
35223631
3523- pub fn make_surface_from_resource (
3524- self : * Display ,
3632+ fn make_surface_from_resource (
3633+ _ : * Display ,
3634+ bucket : * Resources ,
35253635 resource : * Resource ,
35263636 allocator : Allocator ,
35273637 si : * SurfaceInfo ,
35283638 ) (Error || Allocator .Error )! void {
3529- si .buffer = try sdl_load_resource (self . resources , resource , allocator );
3639+ si .buffer = try sdl_load_resource (bucket , resource , allocator );
35303640 errdefer allocator .free (si .buffer );
35313641 si .img = zigimg .Image .fromMemory (allocator , si .buffer [0.. ]) catch | e | {
35323642 if (e == error .OutOfMemory ) return error .OutOfMemory ;
0 commit comments