Tooltips.

This commit is contained in:
IntegratedQuantum 2023-03-29 11:41:13 +02:00
parent 2a332e9c04
commit 58d1428392
2 changed files with 70 additions and 3 deletions

View File

@ -565,5 +565,33 @@ const inventory = struct {
fn render(mousePos: Vec2f) !void {
carriedItemSlot.pos = mousePos;
try carriedItemSlot.render(.{0, 0});
// Draw tooltip:
if(carriedItemStack.amount == 0) if(hoveredItemSlot) |hovered| {
if(hovered.itemStack.item) |item| {
const tooltip = try item.getTooltip();
var textBuffer: graphics.TextBuffer = try graphics.TextBuffer.init(main.threadAllocator, tooltip, .{}, false, .left);
defer textBuffer.deinit();
var size = try textBuffer.calculateLineBreaks(16, 256);
size[0] = 0;
for(textBuffer.lineBreaks.items) |lineBreak| {
size[0] = @max(size[0], lineBreak.width);
}
var pos = mousePos;
if(pos[0] + size[0] >= main.Window.getWindowSize()[0]/scale) {
pos[0] -= size[0];
}
if(pos[1] + size[1] >= main.Window.getWindowSize()[1]/scale) {
pos[1] -= size[1];
}
pos = @max(pos, Vec2f{0, 0});
const border1: f32 = 2;
const border2: f32 = 1;
draw.setColor(0xffffff00);
draw.rect(pos - @splat(2, border1), size + @splat(2, 2*border1));
draw.setColor(0xff000000);
draw.rect(pos - @splat(2, border2), size + @splat(2, 2*border2));
try textBuffer.render(pos[0], pos[1], 16);
}
};
}
};

View File

@ -167,6 +167,10 @@ pub const BaseItem = struct {
}
return self.texture.?;
}
fn getTooltip(self: BaseItem) []const u8 {
return self.name;
}
// TODO: Check if/how this is needed:
// protected Item(int stackSize) {
// id = Resource.EMPTY;
@ -568,12 +572,12 @@ const TextureGenerator = struct {
const lightTR = heightMap[x + 1][y] - heightMap[x][y + 1];
var light = 2 - @floatToInt(i32, @round((lightTL * 2 + lightTR) / 6));
light = @max(@min(light, 4), 0);
img.setRGB(x, y, material.colorPalette[@intCast(usize, light)]);
img.setRGB(x, 15 - y, material.colorPalette[@intCast(usize, light)]);
} else {
img.setRGB(x, y, if((x ^ y) & 1 == 0) Color{.r=255, .g=0, .b=255, .a=255} else Color{.r=0, .g=0, .b=0, .a=255});
img.setRGB(x, 15 - y, if((x ^ y) & 1 == 0) Color{.r=255, .g=0, .b=255, .a=255} else Color{.r=0, .g=0, .b=0, .a=255});
}
} else {
img.setRGB(x, y, Color{.r = 0, .g = 0, .b = 0, .a = 0});
img.setRGB(x, 15 - y, Color{.r = 0, .g = 0, .b = 0, .a = 0});
}
}
}
@ -932,6 +936,7 @@ const ToolPhysics = struct {
const Tool = struct {
craftingGrid: [25]?*const BaseItem,
materialGrid: [16][16]?*const BaseItem,
tooltip: ?[]const u8,
image: graphics.Image,
texture: ?graphics.Texture,
seed: u32,
@ -967,6 +972,7 @@ const Tool = struct {
var self = try main.globalAllocator.create(Tool);
self.image = try graphics.Image.init(main.globalAllocator, 16, 16);
self.texture = null;
self.tooltip = null;
return self;
}
@ -975,6 +981,7 @@ const Tool = struct {
texture.deinit();
}
self.image.deinit(main.globalAllocator);
if(self.tooltip) |tooltip| main.globalAllocator.free(tooltip);
main.globalAllocator.destroy(self);
}
@ -1036,6 +1043,27 @@ const Tool = struct {
}
return self.texture.?;
}
fn getTooltip(self: *Tool) ![]const u8 {
if(self.tooltip) |tooltip| return tooltip;
self.tooltip = try std.fmt.allocPrint(
main.globalAllocator,
\\Time to swing: {} s
\\Pickaxe power: {} %
\\Axe power: {} %
\\Shover power: {} %
\\Durability: {}/{}
,
.{
self.swingTime,
@floatToInt(i32, 100*self.pickaxePower),
@floatToInt(i32, 100*self.axePower),
@floatToInt(i32, 100*self.shovelPower),
self.durability, self.maxDurability,
}
);
return self.tooltip.?;
}
pub fn getPowerByBlockClass(self: *Tool, blockClass: blocks.BlockClass) f32 {
return switch(blockClass) {
@ -1112,6 +1140,17 @@ pub const Item = union(enum) {
}
}
pub fn getTooltip(self: Item) ![]const u8 {
switch(self) {
.baseItem => |_baseItem| {
return _baseItem.getTooltip();
},
.tool => |_tool| {
return try _tool.getTooltip();
},
}
}
pub fn getImage(self: Item) graphics.Image {
switch(self) {
.baseItem => |_baseItem| {