@@ -109,6 +109,11 @@ pub const LayoutSize = enum {
109109 shrinks ,
110110};
111111
112+ pub const Fit = enum {
113+ stretch ,
114+ fill ,
115+ };
116+
112117/// An element is ignored when it is hidden. An element is drawn when it is
113118/// visible. An element is culled when it is visible, but not currently inside
114119/// thd drawable area on the screen.
@@ -364,6 +369,7 @@ pub const Element = struct {
364369 sprite : struct {
365370 on_click : Callback = .{ .func = null },
366371 update : UpdateCallback = .{ .func = null },
372+ scale : Fit = .stretch ,
367373 },
368374 label : struct {
369375 font : * FontInfo = undefined ,
@@ -1566,12 +1572,54 @@ pub const Element = struct {
15661572 }
15671573
15681574 // TODO: Sprites might have frames
1569- const source : sdl.SDL_FRect = .{
1570- .x = 0 ,
1571- .y = 0 ,
1572- .w = @as (f32 , @floatFromInt (texture .texture .w )),
1573- .h = @as (f32 , @floatFromInt (texture .texture .h )),
1574- };
1575+
1576+ // Stretch the full image onto the drawing area
1577+ const image_width = @as (f32 , @floatFromInt (texture .texture .w ));
1578+ const image_height = @as (f32 , @floatFromInt (texture .texture .h ));
1579+ var source : sdl.SDL_FRect = undefined ;
1580+ switch (element .type .sprite .scale ) {
1581+ .stretch = > {
1582+ source = .{
1583+ .x = 0 ,
1584+ .y = 0 ,
1585+ .w = image_width ,
1586+ .h = image_height ,
1587+ };
1588+ },
1589+ .fill = > {
1590+ // We need a slice of the source image that fits the
1591+ // ratio of the destination area.
1592+ const dst_scale : f32 = element .rect .width / element .rect .height ;
1593+ const src_scale : f32 = image_width / image_height ;
1594+ if (src_scale >= dst_scale ) {
1595+ // Slice off some width
1596+ source = .{
1597+ .x = 0 ,
1598+ .y = 0 ,
1599+ .h = image_height ,
1600+ .w = image_height * dst_scale ,
1601+ };
1602+ source .x = (image_width - source .w ) / 2 ;
1603+ } else {
1604+ // Slice off some height
1605+ source = .{
1606+ .x = 0 ,
1607+ .y = 0 ,
1608+ .w = image_width ,
1609+ .h = image_width / dst_scale ,
1610+ };
1611+ source .y = (image_height - source .h ) / 2 ;
1612+ }
1613+ err ("img={d}x{d} scale={d} src={d}x{d}" , .{
1614+ image_width ,
1615+ image_height ,
1616+ dst_scale ,
1617+ source .w ,
1618+ source .h ,
1619+ });
1620+ },
1621+ }
1622+
15751623 //const source: Rect = .{
15761624 // .x = 0,
15771625 // .y = 0,
@@ -2612,7 +2660,7 @@ pub const Display = struct {
26122660 app_version : [:0 ]const u8 ,
26132661 app_id : [:0 ]const u8 ,
26142662 dev_resource_folder : []const u8 ,
2615- dev_resource_filter : ? fn (name : []const u8 , extension : Resource.Type ) bool ,
2663+ dev_resource_filter : ? fn (name : []const u8 , extension : FileType ) bool ,
26162664 translation_filename : []const u8 ,
26172665 gui_flags : usize ,
26182666 ) (Error || Allocator .Error || Resources .Error || engine .Error || error { Utf8ExpectedContinuation , Utf8OverlongEncoding , Utf8EncodesSurrogateHalf , Utf8CodepointTooLarge , Utf8InvalidStartByte } || std .fs .Dir .StatError || std .fs .File .StatError || std .fs .File .OpenError )! * Display {
@@ -5885,6 +5933,7 @@ pub const Translation = @import("translation.zig").Translation;
58855933
58865934const Resources = @import ("resources" ).Resources ;
58875935const Resource = @import ("resources" ).Resource ;
5936+ const FileType = @import ("resources" ).FileType ;
58885937
58895938const default_themes = @import ("theme.zig" ).default_themes ;
58905939const Theme = @import ("theme.zig" ).Theme ;
0 commit comments