Skip to content

Commit 2a31c32

Browse files
author
Jay
committed
Add a .fit option for sprite size.
1 parent daccd14 commit 2a31c32

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/engine.zig

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ pub const LayoutSize = enum {
112112
pub const Fit = enum {
113113
stretch,
114114
fill,
115+
fit,
115116
};
116117

117118
/// An element is ignored when it is hidden. An element is drawn when it is
@@ -1596,6 +1597,25 @@ pub const Element = struct {
15961597
.h = image_height,
15971598
};
15981599
},
1600+
.fit => {
1601+
source = .{
1602+
.x = 0,
1603+
.y = 0,
1604+
.w = image_width,
1605+
.h = image_height,
1606+
};
1607+
// Don't fill the destination area. Slice off
1608+
// some of the destination area.
1609+
const dst_scale: f32 = element.rect.width / element.rect.height;
1610+
const src_scale: f32 = image_width / image_height;
1611+
if (src_scale >= dst_scale) {
1612+
// image too wide, hight will have blank space
1613+
dest.height = dest.width * src_scale;
1614+
} else {
1615+
// image too tall/high, width will have blank space
1616+
dest.width = dest.height * src_scale;
1617+
}
1618+
},
15991619
.fill => {
16001620
// We need a slice of the source image that fits the
16011621
// ratio of the destination area.

0 commit comments

Comments
 (0)