draw item

This commit is contained in:
Jeeves 2025-05-28 08:44:41 -06:00
parent 34f6d4d742
commit 793c4d7211

View file

@ -36,7 +36,7 @@ pub fn main() !void {
screen_height = @floatFromInt(raylib.GetScreenHeight());
scales.recalculate();
}
if (raylib.IsKeyPressed('Z')) item.setBig(!item.big);
if (raylib.IsKeyPressed('Z')) item.setBig(!item.large);
if (raylib.IsKeyPressed('S')) raylib.TakeScreenshot("screenshot.png");
raylib.BeginDrawing();
@ -62,7 +62,8 @@ var scales: Scales = undefined;
/// Cached scaling and positioning values for dynamic window resizing.
pub const Scales = struct {
item_icon_scale: f32,
item_icon_small_scale: f32,
item_icon_large_scale: f32,
item_title_font_size: f32,
item_subtitle_font_size: f32,
@ -73,10 +74,12 @@ pub const Scales = struct {
/// Recalculate scales after screen resize.
pub fn recalculate(self: *Scales) void {
self.item_icon_scale = screen_height * 0.72 / screen_height;
self.item_icon_small_scale = 0.272727;
self.item_icon_large_scale = 0.272727;
self.item_title_font_size = 16;
self.column_icon_scale = screen_height * 0.75 / screen_height;
self.column_title_font_size = screen_height * 13 / screen_height;
self.column_icon_scale = 0.75;
self.column_title_font_size = 13;
self.column_position_center = .{
.x = std.math.lerp(0.0, screen_width, 0.18),
.y = std.math.lerp(0.0, screen_height, 0.15),
@ -118,10 +121,10 @@ pub const Column = struct {
.y = icon_position.y + icon_height + 6,
};
var y: f32 = scales.column_position_center.y + icon_height + title_size.y + 32;
var y: f32 = scales.column_position_center.y + icon_height + title_size.y + 24;
for (self.items.items) |item| {
item.position = .{ .x = scales.column_position_center.x, .y = y };
// item.draw();
item.draw();
y += 64;
}
@ -143,15 +146,15 @@ pub const Column = struct {
};
pub const Item = struct {
time: f32 = 0.0,
start_scale: f32,
position: raylib.Vector2 = .{ .x = 0, .y = 0 },
scale: f32,
icon_scale: f32,
start_scale: f32,
time: f32 = 0.0,
icon: raylib.Texture2D,
title: []const u8,
subtitle: []const u8,
big: bool = true,
large: bool = true,
pub fn init(texture: raylib.Texture2D, title: []const u8, subtitle: []const u8) Item {
raylib.SetTextureFilter(texture, raylib.TEXTURE_FILTER_BILINEAR);
@ -159,32 +162,37 @@ pub const Item = struct {
.icon = texture,
.title = title,
.subtitle = subtitle,
.scale = scales.item_icon_scale,
.start_scale = scales.item_icon_scale,
.icon_scale = scales.item_icon_small_scale,
.start_scale = scales.item_icon_small_scale,
};
}
pub fn draw(self: *Item) void {
self.time += raylib.GetFrameTime();
self.scale = std.math.lerp(
self.icon_scale = std.math.lerp(
self.start_scale,
if (self.big) scales.item_icon_scale else scales.item_icon_scale * 0.5,
if (self.large) scales.item_icon_large_scale else scales.item_icon_small_scale,
easeOutExpo(self.time / 0.333),
);
const title_pos = raylib.Vector2{
.x = self.position.x + 16 + @as(f32, @floatFromInt(self.icon.width)) * self.scale,
.y = (self.position.y + @as(f32, @floatFromInt(self.icon.height)) * self.scale) / 2.0,
const icon_position = raylib.Vector2{
.x = self.position.x - (@as(f32, @floatFromInt(self.icon.width)) * self.icon_scale - 64),
.y = self.position.y,
};
const title_size = raylib.MeasureTextEx(global_font, @ptrCast(self.title), scales.item_title_font_size, 1);
const title_position = raylib.Vector2{
.x = icon_position.x + 8 + @as(f32, @floatFromInt(self.icon.width)) * self.icon_scale,
.y = icon_position.y + (@as(f32, @floatFromInt(self.icon.height)) * self.icon_scale) / 2.0 - title_size.y / 2.0,
};
raylib.DrawTextureEx(self.icon, self.position, 0, self.scale, raylib.WHITE);
raylib.DrawTextEx(global_font, @ptrCast(self.title), title_pos, 18.0, 1, raylib.WHITE);
raylib.DrawTextureEx(self.icon, icon_position, 0, self.icon_scale, raylib.WHITE);
raylib.DrawTextEx(global_font, @ptrCast(self.title), title_position, scales.item_title_font_size, 1, raylib.WHITE);
}
pub fn setBig(self: *Item, big: bool) void {
self.big = big;
self.large = big;
self.time = 0;
self.start_scale = self.scale;
self.start_scale = self.icon_scale;
}
fn easeOutExpo(x: f32) f32 {