initial commit

This commit is contained in:
2026-03-12 14:42:36 +01:00
commit 6188e14834
25 changed files with 2982 additions and 0 deletions

View File

@@ -0,0 +1,80 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<OutputPath>bin\$(Configuration)\Mods\mod</OutputPath>
<Nullable>enable</Nullable>
<Configurations>Debug;Release;Version22</Configurations>
<VintageStoryDataPathArgs></VintageStoryDataPathArgs>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)' == 'Version22'">
<TargetFramework>net10.0</TargetFramework>
<VINTAGE_STORY>$(VINTAGE_STORY_22PRE2)</VINTAGE_STORY>
<VintageStoryDataPathArgs> --dataPath "..\VintageStory22Data"</VintageStoryDataPathArgs>
</PropertyGroup>
<ItemGroup>
<Reference Include="VintagestoryAPI">
<HintPath>$(VINTAGE_STORY)/VintagestoryAPI.dll</HintPath>
<Private>false</Private>
</Reference>
<Reference Include="VSSurvivalMod">
<HintPath>$(VINTAGE_STORY)/Mods/VSSurvivalMod.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="VSEssentials">
<HintPath>$(VINTAGE_STORY)/Mods/VSEssentials.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="VSCreativeMod">
<HintPath>$(VINTAGE_STORY)/Mods/VSCreativeMod.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Newtonsoft.Json">
<HintPath>$(VINTAGE_STORY)/Lib/Newtonsoft.Json.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="0Harmony">
<HintPath>$(VINTAGE_STORY)/Lib/0Harmony.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="VintagestoryLib">
<HintPath>$(VINTAGE_STORY)/VintagestoryLib.dll</HintPath>
<Private>false</Private>
</Reference>
<Reference Include="protobuf-net">
<HintPath>$(VINTAGE_STORY)/Lib/protobuf-net.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="cairo-sharp">
<HintPath>$(VINTAGE_STORY)/Lib/cairo-sharp.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Microsoft.Data.Sqlite">
<HintPath>$(VINTAGE_STORY)/Lib/Microsoft.Data.Sqlite.dll</HintPath>
<Private>False</Private>
</Reference>
</ItemGroup>
<ItemGroup>
<Reference Include="configlib">
<HintPath>E:\Code\VintageStory\configlib_1.10.14\configlib.dll</HintPath>
<Private>False</Private>
</Reference>
</ItemGroup>
<ItemGroup>
<Content Include="modinfo.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<Content Include="modicon.png" Condition="Exists('modicon.png')">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project>

View File

@@ -0,0 +1,20 @@
namespace ChestPreview.Configs;
public sealed class Config
{
public string Mode { get; set; } = PreviewModes.UNDER_CURSOR;
public bool HoldKey { get; set; } = true;
public int BillboardColumnsPerBlock { get; set; } = 4;
public int ColumnsUnderCursor { get; set; } = 10;
public int PreviewNearbyRadius { get; set; } = 8;
public bool WhitelistedContainersOnly { get; set; } = true;
public string WhitelistedContainers { get; set; } = "barrel, crate, chest-*, trunk-*, storagevessel-*, stationarybasket-*, labeledchest-*";
public string BlacklistedContainers { get; set; } = "";
}

View File

@@ -0,0 +1,50 @@
using ChestPreview.Core;
using ConfigLib;
using System;
using Vintagestory.API.Client;
namespace ChestPreview.Configs;
internal class ConfigLibBridge(Config config) : IDisposable
{
public const string CONFIGLIB_MODID = "configlib";
private readonly Config config = config;
private ConfigLibModSystem? configLibSystem;
public static ConfigLibBridge? TryCreate(ICoreClientAPI api, Config config)
{
if (!api.ModLoader.IsModEnabled(CONFIGLIB_MODID))
return null;
ConfigLibModSystem? configLibSystem = api.ModLoader.GetModSystem<ConfigLibModSystem>();
if (configLibSystem == null)
return null;
var bridge = new ConfigLibBridge(config)
{
configLibSystem = configLibSystem
};
configLibSystem.SettingChanged += bridge.OnSettingChanged;
return bridge;
}
public void Dispose()
{
if (configLibSystem == null)
return;
configLibSystem.SettingChanged -= OnSettingChanged;
configLibSystem = null;
}
private void OnSettingChanged(string domain, IConfig configObject, ISetting settingObject)
{
if (domain != ChestPreviewModSystem.CONFIGLIB_DOMAIN || settingObject == null)
return;
settingObject.AssignSettingValue(config);
}
}

View File

@@ -0,0 +1,21 @@
namespace ChestPreview.Configs;
public static class PreviewModes
{
public const string NONE = "None";
public const string UNDER_CURSOR = "UnderCursor";
public const string ON_HOVERED_CONTAINER = "OnHoveredContainer";
public const string ON_NEARBY_CONTAINERS = "OnNearbyContainers";
public static string Normalize(string? mode)
{
return mode switch
{
NONE => NONE,
UNDER_CURSOR => UNDER_CURSOR,
ON_HOVERED_CONTAINER => ON_HOVERED_CONTAINER,
ON_NEARBY_CONTAINERS => ON_NEARBY_CONTAINERS,
_ => ON_HOVERED_CONTAINER
};
}
}

View File

@@ -0,0 +1,111 @@
using ChestPreview.Configs;
using ChestPreview.Rendering;
using System;
using Vintagestory.API.Client;
using Vintagestory.API.Common;
using Vintagestory.API.Config;
namespace ChestPreview.Core;
public class ChestPreviewModSystem : ModSystem
{
internal const string CONFIG_FILENAME = "chestpreview.json";
internal const string CONFIGLIB_DOMAIN = "chestpreview";
internal const string PREVIEW_CONTAINERS_HOTKEY_CODE = "chestpreview-preview-containers";
internal const string PREVIEW_CONTAINERS_NEARBY_HOTKEY_CODE = "chestpreview-preview-containers-nearby";
private Config config = new();
private CardRenderer? cardRenderer;
private ConfigLibBridge? configLibBridge;
private StorageHoverHudRenderer? storageHoverHudRenderer;
private WorldBillboardRenderer? worldBillboardRenderer;
public override bool ShouldLoad(EnumAppSide forSide) => forSide is EnumAppSide.Client;
public override void StartClientSide(ICoreClientAPI api)
{
config = LoadClientConfig(api);
RegisterHotkeys(api);
if (api.ModLoader.IsModEnabled(ConfigLibBridge.CONFIGLIB_MODID))
configLibBridge = ConfigLibBridge.TryCreate(api, config);
cardRenderer = new CardRenderer(api, config);
storageHoverHudRenderer = new StorageHoverHudRenderer(api, config, cardRenderer);
api.Event.RegisterRenderer(storageHoverHudRenderer, EnumRenderStage.Ortho, "chestpreview-storage-hover-hud");
worldBillboardRenderer = new WorldBillboardRenderer(api, config, cardRenderer);
api.Event.RegisterRenderer(worldBillboardRenderer, EnumRenderStage.AfterOIT, "chestpreview-world-billboard-preview");
}
public override void Dispose()
{
configLibBridge?.Dispose();
configLibBridge = null;
if (storageHoverHudRenderer is { } renderer)
{
renderer.Api.Event.UnregisterRenderer(renderer, EnumRenderStage.Ortho);
renderer.Dispose();
storageHoverHudRenderer = null;
}
if (worldBillboardRenderer is { } billboardRenderer)
{
billboardRenderer.Api.Event.UnregisterRenderer(billboardRenderer, EnumRenderStage.AfterOIT);
billboardRenderer.Dispose();
worldBillboardRenderer = null;
}
cardRenderer?.Dispose();
cardRenderer = null;
}
public Config LoadClientConfig(ICoreClientAPI api)
{
Config config;
try
{
config = api.LoadModConfig<Config>(CONFIG_FILENAME) ?? new Config();
}
catch (Exception ex)
{
Mod.Logger.Error("Error while loading config.");
Mod.Logger.Error(ex);
config = new Config();
}
config.Mode = PreviewModes.Normalize(config.Mode);
if (config.BillboardColumnsPerBlock <= 0)
config.BillboardColumnsPerBlock = 5;
if (config.ColumnsUnderCursor <= 0)
config.ColumnsUnderCursor = 10;
config.PreviewNearbyRadius = config.PreviewNearbyRadius;
api.StoreModConfig(config, CONFIG_FILENAME);
return config;
}
private static void RegisterHotkeys(ICoreClientAPI api)
{
api.Input.RegisterHotKey(
hotkeyCode: PREVIEW_CONTAINERS_HOTKEY_CODE,
name: Lang.Get("chestpreview:hotkey-preview-container"),
key: GlKeys.LShift,
type: HotkeyType.HelpAndOverlays,
altPressed: false,
ctrlPressed: false,
shiftPressed: false);
api.Input.RegisterHotKey(
hotkeyCode: PREVIEW_CONTAINERS_NEARBY_HOTKEY_CODE,
name: Lang.Get("chestpreview:hotkey-preview-containers-nearby"),
key: GlKeys.LShift,
type: HotkeyType.HelpAndOverlays,
altPressed: false,
ctrlPressed: true,
shiftPressed: false);
}
}

View File

@@ -0,0 +1,22 @@
using Vintagestory.API.MathTools;
namespace ChestPreview.Models;
internal readonly struct BillboardTarget(PreviewTarget previewTarget, Vec3d center, Vec3f right, Vec3f up, Vec3f forward, float width, float height, bool isFrontPlacement)
{
public PreviewTarget PreviewTarget { get; } = previewTarget;
public Vec3d Center { get; } = center;
public Vec3f Right { get; } = right;
public Vec3f Up { get; } = up;
public Vec3f Forward { get; } = forward;
public float Width { get; } = width;
public float Height { get; } = height;
public bool IsFrontPlacement { get; } = isFrontPlacement;
}

View File

@@ -0,0 +1,15 @@
using Vintagestory.API.Common;
using Vintagestory.API.MathTools;
namespace ChestPreview.Models;
internal readonly struct PreviewTarget(Block block, BlockEntity blockEntity, string mode, Vec3d anchor)
{
public Block Block { get; } = block;
public BlockEntity BlockEntity { get; } = blockEntity;
public string Mode { get; } = mode;
public Vec3d Anchor { get; } = anchor;
}

View File

@@ -0,0 +1,284 @@
using ChestPreview.Configs;
using ChestPreview.Core;
using ChestPreview.Models;
using ChestPreview.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text.RegularExpressions;
using Vintagestory.API.Client;
using Vintagestory.API.Common;
using Vintagestory.API.MathTools;
using Vintagestory.API.Util;
namespace ChestPreview;
internal partial class PreviewTargetProvider(ICoreClientAPI api, Config config) : IDisposable
{
private const float nearby_scan_interval_seconds = 0.1f;
private static readonly Regex list_separator_regex = ListSeparatorRegex();
private readonly ICoreClientAPI api = api;
private readonly Config config = config;
private readonly List<BlockEntity> nearbyContainerEntities = [];
private float nearbyScanAccumulator;
public void CollectTargets(float deltaTime, List<PreviewTarget> targets)
{
string mode = GetActiveMode();
if (mode == PreviewModes.NONE)
return;
if (mode == PreviewModes.ON_NEARBY_CONTAINERS)
{
CollectNearbyTargets(deltaTime, targets);
return;
}
if (!TryGetHoveredContainer(out Block hoveredBlock, out BlockEntity hoveredBlockEntity))
return;
if (!CanAccessContainer(hoveredBlockEntity))
return;
if (!IsContainerAllowed(hoveredBlock))
return;
targets.Add(new PreviewTarget(hoveredBlock, hoveredBlockEntity, mode, GetContainerAnchor(hoveredBlock, hoveredBlockEntity.Pos)));
}
public void Dispose()
{
nearbyContainerEntities.Clear();
}
private void CollectNearbyTargets(float deltaTime, List<PreviewTarget> targets)
{
nearbyScanAccumulator += deltaTime;
bool shouldRefresh = nearbyContainerEntities.Count == 0 || nearbyScanAccumulator >= nearby_scan_interval_seconds;
if (shouldRefresh)
{
RefreshNearbyContainers();
nearbyScanAccumulator = 0f;
}
foreach (BlockEntity blockEntity in nearbyContainerEntities)
{
Block block = api.World.BlockAccessor.GetBlock(blockEntity.Pos);
if (block.Id == 0)
continue;
if (!CanAccessContainer(blockEntity))
continue;
if (!IsContainerAllowed(block))
continue;
targets.Add(new PreviewTarget(block, blockEntity, PreviewModes.ON_NEARBY_CONTAINERS, GetContainerAnchor(block, blockEntity.Pos)));
}
}
private string GetActiveMode()
{
if (IsHotkeyHeld(ChestPreviewModSystem.PREVIEW_CONTAINERS_NEARBY_HOTKEY_CODE))
return PreviewModes.ON_NEARBY_CONTAINERS;
if (config.HoldKey && !IsHotkeyHeld(ChestPreviewModSystem.PREVIEW_CONTAINERS_HOTKEY_CODE))
return PreviewModes.NONE;
return PreviewModes.Normalize(config.Mode);
}
private bool IsHotkeyHeld(string hotkeyCode)
{
HotKey? hotKey = api.Input.GetHotKeyByCode(hotkeyCode);
if (hotKey?.CurrentMapping == null)
return false;
KeyCombination mapping = hotKey.CurrentMapping;
if (!IsKeyHeld(mapping.KeyCode))
return false;
if (mapping.SecondKeyCode.HasValue && !IsKeyHeld(mapping.SecondKeyCode.Value))
return false;
EntityControls? controls = api.World.Player?.Entity?.Controls;
if (mapping.Ctrl && controls?.CtrlKey != true)
return false;
if (mapping.Shift && controls?.ShiftKey != true)
return false;
return !mapping.Alt || IsAltHeld();
}
private bool IsKeyHeld(int keyCode)
{
bool[] keyStates = api.Input.KeyboardKeyStateRaw;
return keyCode >= 0 && keyCode < keyStates.Length && keyStates[keyCode];
}
private bool IsAltHeld()
{
return IsKeyHeld((int)GlKeys.AltLeft) || IsKeyHeld((int)GlKeys.AltRight);
}
private bool TryGetHoveredContainer(out Block block, out BlockEntity blockEntity)
{
block = null!;
blockEntity = null!;
BlockSelection? currentBlockSelection = api.World.Player?.CurrentBlockSelection;
if (currentBlockSelection == null)
return false;
BlockEntity? currentBlockEntity = api.World.BlockAccessor.GetBlockEntity(currentBlockSelection.Position);
if (currentBlockEntity is IBlockEntityContainer)
{
block = currentBlockSelection.Block;
blockEntity = currentBlockEntity;
return true;
}
if (!TryResolveMultiblockControllerContainer(currentBlockSelection, out Block controllerBlock, out BlockEntity controllerBlockEntity))
return false;
block = controllerBlock;
blockEntity = controllerBlockEntity;
return true;
}
private bool TryResolveMultiblockControllerContainer(BlockSelection blockSelection, out Block controllerBlock, out BlockEntity controllerBlockEntity)
{
controllerBlock = null!;
controllerBlockEntity = null!;
if (blockSelection.Block is not IMultiblockOffset multiblockOffset)
return false;
BlockPos controllerPos = multiblockOffset.GetControlBlockPos(blockSelection.Position);
BlockEntity? blockEntity = api.World.BlockAccessor.GetBlockEntity(controllerPos);
if (blockEntity is not IBlockEntityContainer)
return false;
controllerBlock = api.World.BlockAccessor.GetBlock(controllerPos);
if (controllerBlock.Id == 0)
return false;
controllerBlockEntity = blockEntity;
return true;
}
private void RefreshNearbyContainers()
{
nearbyContainerEntities.Clear();
EntityPlayer? playerEntity = api.World.Player?.Entity;
if (playerEntity?.CameraPos == null)
return;
float nearbyRadius = config.PreviewNearbyRadius;
float nearbyRadiusSquared = nearbyRadius * nearbyRadius;
Vec3d nearbyRadiusOffset = new(nearbyRadius, nearbyRadius, nearbyRadius);
BlockPos minPos = (playerEntity.CameraPos - nearbyRadiusOffset).AsBlockPos;
BlockPos maxPos = (playerEntity.CameraPos + nearbyRadiusOffset + new Vec3d(1d, 1d, 1d)).AsBlockPos;
api.World.BlockAccessor.WalkBlocks(minPos, maxPos, (block, x, y, z) =>
{
double horizontalDx = x + 0.5d - playerEntity.CameraPos.X;
double horizontalDz = z + 0.5d - playerEntity.CameraPos.Z;
if (horizontalDx * horizontalDx + horizontalDz * horizontalDz > nearbyRadiusSquared)
return;
if (Math.Abs(y + 0.5d - playerEntity.CameraPos.Y) > nearbyRadius)
return;
BlockEntity? blockEntity = api.World.BlockAccessor.GetBlockEntity(new BlockPos(x, y, z));
if (blockEntity is not IBlockEntityContainer)
return;
if (block.Id == 0)
return;
nearbyContainerEntities.Add(blockEntity);
});
}
private Vec3d GetContainerAnchor(Block block, BlockPos blockPos)
{
GetContainerBounds(block, blockPos, out Vector3 min, out Vector3 max);
Vector3 anchor = blockPos.ToVector3() + new Vector3((min.X + max.X) * 0.5f, max.Y + 0.1f, (min.Z + max.Z) * 0.5f);
return anchor.ToVec3d();
}
private void GetContainerBounds(Block block, BlockPos blockPos, out Vector3 min, out Vector3 max)
{
Cuboidf[]? selectionBoxes = block.GetSelectionBoxes(api.World.BlockAccessor, blockPos);
min = new Vector3(0f, 0f, 0f);
max = new Vector3(1f, 1f, 1f);
if (selectionBoxes is not { Length: > 0 })
return;
foreach (Cuboidf selectionBox in selectionBoxes)
{
min = Vector3.Min(min, selectionBox.Start.ToVector3());
max = Vector3.Max(max, selectionBox.End.ToVector3());
}
}
private bool CanAccessContainer(BlockEntity blockEntity)
{
if (blockEntity is not IBlockEntityContainer container || container.Inventory is not InventoryBase inventory)
return false;
IPlayer? player = api.World.Player;
EntityPlayer? playerEntity = player?.Entity;
if (player == null || playerEntity == null)
return false;
return inventory.CanPlayerAccess(player, playerEntity.Pos);
}
private bool IsContainerAllowed(Block block)
{
string? code = block.Code?.Path;
if (string.IsNullOrWhiteSpace(code))
return true;
HashSet<string> blacklist = ParseContainerCodes(config.BlacklistedContainers);
if (blacklist.Any(x => WildcardUtil.Match(x, code)))
return false;
if (!config.WhitelistedContainersOnly)
return true;
HashSet<string> whitelist = ParseContainerCodes(config.WhitelistedContainers);
return whitelist.Any(x => WildcardUtil.Match(x, code));
}
private static HashSet<string> ParseContainerCodes(string rawList)
{
if (string.IsNullOrWhiteSpace(rawList))
return [];
string[] entries = list_separator_regex.Split(rawList.Trim());
HashSet<string> result = new(StringComparer.OrdinalIgnoreCase);
foreach (string entry in entries)
{
if (!string.IsNullOrWhiteSpace(entry))
result.Add(entry);
}
return result;
}
[GeneratedRegex("[,;\\s]+", RegexOptions.Compiled)]
private static partial Regex ListSeparatorRegex();
}

View File

@@ -0,0 +1,10 @@
{
"profiles": {
"Client": {
"commandName": "Executable",
"executablePath": "dotnet",
"commandLineArgs": "\"$(VINTAGE_STORY)/Vintagestory.dll\" --tracelog --addModPath \"$(ProjectDir)/bin/$(Configuration)/Mods\" --addOrigin \"$(ProjectDir)/assets\"$(VintageStoryDataPathArgs)",
"workingDirectory": "$(VINTAGE_STORY)"
}
}
}

View File

@@ -0,0 +1,525 @@
using Cairo;
using ChestPreview.Configs;
using ChestPreview.Models;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using Vintagestory.API.Client;
using Vintagestory.API.Common;
using Vintagestory.API.Config;
using Vintagestory.API.MathTools;
namespace ChestPreview.Rendering;
internal class CardRenderer(ICoreClientAPI api, Config config) : IDisposable
{
private static readonly int rendered_icon_size = 64;
private static readonly float hud_z = 0f;
private static readonly double hud_outer_alpha = 0.36d;
private static readonly float cell_padding = 10f;
private static readonly float cell_gap = 6f;
private static readonly float cell_width = 72f;
private static readonly float cell_height = 86f;
private static readonly float icon_size = 64f;
private static readonly double count_font_size = 20d;
private static readonly float count_anchor_inset = 2f;
private readonly ICoreClientAPI api = api;
private readonly Config config = config;
private readonly Dictionary<string, CachedCardTexture> cardTextureByTarget = [];
private readonly Dictionary<int, LoadedTexture> atlasTextureById = [];
private readonly Dictionary<string, RenderedIconEntry> renderedIconByStack = [];
private int iconRenderVersion;
public static float Width => GetCardWidth(1);
public static float Height => GetCardHeight(1);
public bool TryGetOrCreateCardTexture(PreviewTarget target, out LoadedTexture texture)
{
return TryGetOrCreateCardTexture(target, GetDefaultMaxColumns(target), out texture);
}
public bool TryGetOrCreateCardTexture(PreviewTarget target, int maxColumns, out LoadedTexture texture)
{
texture = null!;
if (target.BlockEntity is not IBlockEntityContainer container || container.Inventory is not InventoryBase inventory)
return false;
IPlayer? player = api.World.Player;
EntityPlayer? playerEntity = player?.Entity;
if (player == null || playerEntity == null || !inventory.CanPlayerAccess(player, playerEntity.Pos))
return false;
string targetKey = CreateTargetKey(target.BlockEntity.Pos);
if (!cardTextureByTarget.TryGetValue(targetKey, out CachedCardTexture? cachedTexture))
{
cachedTexture = new CachedCardTexture(new LoadedTexture(api), string.Empty);
cardTextureByTarget[targetKey] = cachedTexture;
}
BuildLayout(inventory, maxColumns, out List<GroupedItemCell> cells, out int usedColumns, out int usedRows, out string fingerprint);
if (cachedTexture.Fingerprint != fingerprint)
{
ComposeTexture(cachedTexture.Texture, cells, usedColumns, usedRows);
cachedTexture.Fingerprint = fingerprint;
}
texture = cachedTexture.Texture;
return texture.TextureId != 0;
}
public void DrawAtScreen(float x, float y, LoadedTexture texture)
{
if (texture.TextureId == 0)
return;
api.Render.RenderTexture(texture.TextureId, x, y, texture.Width, texture.Height, hud_z, new Vec4f(1f, 1f, 1f, 1f));
}
public void ClampToScreen(ref float x, ref float y)
{
ClampToScreen(ref x, ref y, Width, Height);
}
public void ClampToScreen(ref float x, ref float y, float cardWidth, float cardHeight)
{
float max_x = api.Render.FrameWidth - cardWidth;
float max_y = api.Render.FrameHeight - cardHeight;
x = Math.Clamp(x, 0f, max_x);
y = Math.Clamp(y, 0f, max_y);
}
public void Dispose()
{
foreach (CachedCardTexture cardTexture in cardTextureByTarget.Values)
cardTexture.Texture.Dispose();
cardTextureByTarget.Clear();
atlasTextureById.Clear();
renderedIconByStack.Clear();
}
private void BuildLayout(IInventory inventory, int maxColumns, out List<GroupedItemCell> cells, out int usedColumns, out int usedRows, out string fingerprint)
{
List<GroupedItemCell> groupedItems = [];
int naturalOrder = 0;
foreach (ItemSlot? slot in inventory)
{
if (slot == null || slot.Empty || slot.Itemstack == null)
continue;
ItemStack stack = slot.Itemstack;
int existingIndex = FindMatchingItemIndex(groupedItems, stack);
if (existingIndex >= 0)
{
GroupedItemCell existing = groupedItems[existingIndex];
existing.Count += stack.StackSize;
continue;
}
ItemStack representativeStack = stack.Clone();
representativeStack.StackSize = 1;
groupedItems.Add(new GroupedItemCell(representativeStack, stack.StackSize, naturalOrder));
naturalOrder++;
}
groupedItems.Sort(static (left, right) =>
{
int countComparison = right.Count.CompareTo(left.Count);
return countComparison != 0 ? countComparison : left.NaturalOrder.CompareTo(right.NaturalOrder);
});
maxColumns = Math.Max(1, maxColumns);
usedColumns = GetUsedColumnCount(groupedItems.Count, maxColumns);
usedRows = GetRowCount(groupedItems.Count, maxColumns);
cells = groupedItems;
fingerprint = BuildFingerprint(cells, maxColumns, usedColumns, usedRows);
}
private int FindMatchingItemIndex(List<GroupedItemCell> groupedItems, ItemStack stack)
{
for (int i = 0; i < groupedItems.Count; i++)
{
if (AreStacksEquivalent(groupedItems[i].Stack, stack))
return i;
}
return -1;
}
private bool AreStacksEquivalent(ItemStack left, ItemStack right)
{
if (left.Class != right.Class || left.Id != right.Id)
return false;
ItemStack leftComparable = left.Clone();
ItemStack rightComparable = right.Clone();
leftComparable.StackSize = 1;
rightComparable.StackSize = 1;
return leftComparable.Equals(api.World, rightComparable, GlobalConstants.IgnoredStackAttributes);
}
private string BuildFingerprint(List<GroupedItemCell> cells, int maxColumns, int usedColumns, int usedRows)
{
StringBuilder builder = new();
builder.Append('m');
builder.Append(maxColumns);
builder.Append('|');
builder.Append('w');
builder.Append(GetCardWidth(usedColumns).ToString(CultureInfo.InvariantCulture));
builder.Append('h');
builder.Append(GetCardHeight(usedRows).ToString(CultureInfo.InvariantCulture));
builder.Append('|');
builder.Append("iv");
builder.Append(iconRenderVersion);
builder.Append('|');
foreach (GroupedItemCell cell in cells)
{
builder.Append((int)cell.Stack.Class);
builder.Append(':');
builder.Append(cell.Stack.Id);
builder.Append(':');
builder.Append(cell.Count);
builder.Append(':');
builder.Append(HashBytes(cell.Stack.ToBytes()));
builder.Append('|');
}
return builder.ToString();
}
private static uint HashBytes(byte[] bytes)
{
const uint offset_basis = 2166136261;
const uint prime = 16777619;
uint hash = offset_basis;
for (int i = 0; i < bytes.Length; i++)
hash = (hash ^ bytes[i]) * prime;
return hash;
}
private void ComposeTexture(LoadedTexture targetTexture, List<GroupedItemCell> cells, int usedColumns, int usedRows)
{
float hudWidth = GetCardWidth(usedColumns);
float hudHeight = GetCardHeight(usedRows);
using ImageSurface surface = new(Format.Argb32, (int)hudWidth, (int)hudHeight);
using Context context = new(surface);
context.Operator = Operator.Source;
context.SetSourceRGBA(0d, 0d, 0d, 0d);
context.Paint();
context.SetSourceRGBA(0.28d, 0.28d, 0.28d, hud_outer_alpha);
context.Rectangle(0d, 0d, hudWidth, hudHeight);
context.Fill();
context.SelectFontFace("sans", FontSlant.Normal, FontWeight.Bold);
context.SetFontSize(count_font_size);
api.Gui.LoadOrUpdateCairoTexture(surface, true, ref targetTexture);
for (int i = 0; i < cells.Count; i++)
{
GroupedItemCell cell = cells[i];
if (!TryGetCellBounds(i, usedColumns, hudWidth, hudHeight, out float cellX, out float cellY))
continue;
if (!TryGetIconTexture(cell.Stack, out LoadedTexture atlasTexture, out TextureAtlasPosition textureAtlasPosition))
continue;
float sourceX = textureAtlasPosition.x1 * atlasTexture.Width;
float sourceY = textureAtlasPosition.y1 * atlasTexture.Height;
float sourceWidth = (textureAtlasPosition.x2 - textureAtlasPosition.x1) * atlasTexture.Width;
float sourceHeight = (textureAtlasPosition.y2 - textureAtlasPosition.y1) * atlasTexture.Height;
float iconX = cellX + (cell_width - icon_size) / 2f;
float iconY = cellY + 1f;
api.Render.RenderTextureIntoTexture(atlasTexture, sourceX, sourceY, sourceWidth, sourceHeight, targetTexture, iconX, iconY, 0.01f);
}
using ImageSurface labelsSurface = new(Format.Argb32, (int)hudWidth, (int)hudHeight);
using Context labelsContext = new(labelsSurface);
LoadedTexture labelsTexture = new(api);
labelsContext.Operator = Operator.Source;
labelsContext.SetSourceRGBA(0d, 0d, 0d, 0d);
labelsContext.Paint();
labelsContext.SelectFontFace("sans", FontSlant.Normal, FontWeight.Bold);
labelsContext.SetFontSize(count_font_size);
for (int i = 0; i < cells.Count; i++)
{
GroupedItemCell cell = cells[i];
if (!TryGetCellBounds(i, usedColumns, hudWidth, hudHeight, out float cellX, out float cellY))
continue;
float iconX = cellX + (cell_width - icon_size) / 2f;
float iconY = cellY + 1f;
float iconRight = iconX + icon_size - count_anchor_inset;
float iconBottom = iconY + icon_size - count_anchor_inset;
string countText = cell.Count.ToString(CultureInfo.InvariantCulture);
TextExtents textExtents = labelsContext.TextExtents(countText);
double textX = iconRight - (textExtents.Width + textExtents.XBearing);
double textY = iconBottom - (textExtents.Height + textExtents.YBearing);
labelsContext.SetSourceRGBA(0d, 0d, 0d, 0.95d);
labelsContext.MoveTo(textX + 1d, textY + 1d);
labelsContext.ShowText(countText);
labelsContext.SetSourceRGBA(1d, 1d, 1d, 1d);
labelsContext.MoveTo(textX, textY);
labelsContext.ShowText(countText);
}
try
{
api.Gui.LoadOrUpdateCairoTexture(labelsSurface, true, ref labelsTexture);
api.Render.RenderTextureIntoTexture(labelsTexture, 0f, 0f, labelsTexture.Width, labelsTexture.Height, targetTexture, 0f, 0f, 0.01f);
}
finally
{
labelsTexture.Dispose();
}
api.Render.BindTexture2d(targetTexture.TextureId);
}
private bool TryGetIconTexture(ItemStack stack, out LoadedTexture atlasTexture, out TextureAtlasPosition textureAtlasPosition)
{
if (TryGetRenderedItemIcon(stack, out atlasTexture, out textureAtlasPosition))
return true;
TextureAtlasPosition fallbackTextureAtlasPosition = api.Render.GetTextureAtlasPosition(stack);
if (fallbackTextureAtlasPosition == null || !TryGetAtlasTexture(fallbackTextureAtlasPosition, out atlasTexture))
return false;
textureAtlasPosition = fallbackTextureAtlasPosition;
return true;
}
private bool TryGetRenderedItemIcon(ItemStack stack, out LoadedTexture atlasTexture, out TextureAtlasPosition textureAtlasPosition)
{
atlasTexture = null!;
textureAtlasPosition = null!;
string key = BuildStackKey(stack);
if (!renderedIconByStack.TryGetValue(key, out RenderedIconEntry? entry))
{
entry = new RenderedIconEntry(stack.Clone());
renderedIconByStack[key] = entry;
}
if (!entry.Requested)
{
entry.Requested = true;
api.Render.RenderItemStackToAtlas(
entry.Stack,
api.ItemTextureAtlas,
rendered_icon_size,
textureSubId =>
{
entry.TextureSubId = textureSubId;
entry.Ready = true;
iconRenderVersion++;
},
ColorUtil.WhiteArgb,
0f,
1f
);
}
if (!entry.Ready)
return false;
if (!TryGetAtlasPosition(api.ItemTextureAtlas, entry.TextureSubId, out TextureAtlasPosition texPos))
return false;
if (!TryGetAtlasTexture(api.ItemTextureAtlas, texPos.atlasNumber, out LoadedTexture tex))
return false;
atlasTexture = tex;
textureAtlasPosition = texPos;
return true;
}
private bool TryGetAtlasTexture(TextureAtlasPosition textureAtlasPosition, out LoadedTexture atlasTexture)
{
atlasTexture = null!;
int textureId = textureAtlasPosition.atlasTextureId;
if (textureId <= 0)
return false;
if (atlasTextureById.TryGetValue(textureId, out LoadedTexture? cachedAtlasTexture))
{
atlasTexture = cachedAtlasTexture;
return true;
}
int atlasSize = ResolveAtlasSize(textureId);
if (atlasSize <= 0)
return false;
atlasTexture = new LoadedTexture(api, textureId, atlasSize, atlasSize)
{
IgnoreUndisposed = true
};
atlasTextureById[textureId] = atlasTexture;
return true;
}
private int ResolveAtlasSize(int textureId)
{
if (TryGetAtlasSize(api.ItemTextureAtlas, textureId, out int itemAtlasSize))
return itemAtlasSize;
if (TryGetAtlasSize(api.BlockTextureAtlas, textureId, out int blockAtlasSize))
return blockAtlasSize;
if (TryGetAtlasSize(api.EntityTextureAtlas, textureId, out int entityAtlasSize))
return entityAtlasSize;
return 0;
}
private static bool TryGetAtlasPosition(ITextureAtlasAPI atlas, int textureSubId, out TextureAtlasPosition textureAtlasPosition)
{
textureAtlasPosition = null!;
if (atlas.Positions == null || textureSubId < 0 || textureSubId >= atlas.Positions.Length)
return false;
textureAtlasPosition = atlas.Positions[textureSubId];
return textureAtlasPosition != null;
}
private static bool TryGetAtlasTexture(ITextureAtlasAPI? atlas, int atlasNumber, out LoadedTexture texture)
{
texture = null!;
if (atlas == null || atlas.AtlasTextures == null || atlasNumber < 0 || atlasNumber >= atlas.AtlasTextures.Count)
return false;
LoadedTexture atlasTexture = atlas.AtlasTextures[atlasNumber];
if (atlasTexture.TextureId == 0)
return false;
texture = atlasTexture;
return true;
}
private static bool TryGetAtlasSize(ITextureAtlasAPI? atlas, int textureId, out int size)
{
size = 0;
if (atlas == null || atlas.AtlasTextures == null)
return false;
foreach (LoadedTexture atlasTexture in atlas.AtlasTextures)
{
if (atlasTexture.TextureId != textureId)
continue;
size = atlasTexture.Width;
if (size > 0)
return true;
}
return false;
}
private static string CreateTargetKey(BlockPos blockPos)
{
return $"{blockPos.X}:{blockPos.Y}:{blockPos.Z}";
}
private static string BuildStackKey(ItemStack stack)
{
return $"{(int)stack.Class}:{stack.Id}:{HashBytes(stack.ToBytes())}";
}
private static bool TryGetCellBounds(int index, int maxColumns, float hudWidth, float hudHeight, out float x, out float y)
{
int row = index / maxColumns;
int column = index % maxColumns;
x = cell_padding + column * (cell_width + cell_gap);
y = cell_padding + row * (cell_height + cell_gap);
if (x + cell_width > hudWidth - cell_padding || y + cell_height > hudHeight - cell_padding)
return false;
return true;
}
private static float GetCardWidth(int maxColumns)
{
return cell_padding * 2f + maxColumns * cell_width + (maxColumns - 1) * cell_gap;
}
private static float GetCardHeight(int rows)
{
return cell_padding * 2f + rows * cell_height + (rows - 1) * cell_gap;
}
private static int GetRowCount(int itemCount, int maxColumns)
{
int safeMaxColumns = Math.Max(1, maxColumns);
int safeItemCount = Math.Max(1, itemCount);
return (safeItemCount + safeMaxColumns - 1) / safeMaxColumns;
}
private static int GetUsedColumnCount(int itemCount, int maxColumns)
{
int safeMaxColumns = Math.Max(1, maxColumns);
if (itemCount <= 0)
return 1;
return Math.Min(itemCount, safeMaxColumns);
}
private int GetDefaultMaxColumns(PreviewTarget target)
{
if (target.Mode == PreviewModes.UNDER_CURSOR)
return Math.Max(1, config.ColumnsUnderCursor <= 0 ? 1 : config.ColumnsUnderCursor);
return Math.Max(1, config.BillboardColumnsPerBlock <= 0 ? 1 : config.BillboardColumnsPerBlock);
}
private class CachedCardTexture(LoadedTexture texture, string fingerprint)
{
public LoadedTexture Texture { get; } = texture;
public string Fingerprint { get; set; } = fingerprint;
}
private class GroupedItemCell(ItemStack stack, int count, int naturalOrder)
{
public ItemStack Stack { get; } = stack;
public int Count { get; set; } = count;
public int NaturalOrder { get; } = naturalOrder;
}
private class RenderedIconEntry(ItemStack stack)
{
public ItemStack Stack { get; } = stack;
public bool Requested { get; set; }
public bool Ready { get; set; }
public int TextureSubId { get; set; } = -1;
}
}

View File

@@ -0,0 +1,57 @@
using ChestPreview.Configs;
using ChestPreview.Models;
using System.Collections.Generic;
using Vintagestory.API.Client;
namespace ChestPreview.Rendering;
internal sealed class StorageHoverHudRenderer(ICoreClientAPI api, Config config, CardRenderer cardRenderer) : IRenderer
{
private const float hud_offset_y = 24f;
private readonly List<PreviewTarget> frameTargets = [];
private readonly CardRenderer cardRenderer = cardRenderer;
private readonly PreviewTargetProvider targetProvider = new(api, config);
public ICoreClientAPI Api { get; } = api;
public double RenderOrder => 5;
public int RenderRange => 0;
public void OnRenderFrame(float deltaTime, EnumRenderStage stage)
{
frameTargets.Clear();
targetProvider.CollectTargets(deltaTime, frameTargets);
foreach (PreviewTarget target in frameTargets)
DrawHudForTarget(target);
}
public void Dispose()
{
frameTargets.Clear();
targetProvider.Dispose();
}
private void DrawHudForTarget(PreviewTarget target)
{
if (target.Mode is not PreviewModes.UNDER_CURSOR)
return;
if (!cardRenderer.TryGetOrCreateCardTexture(target, out LoadedTexture texture))
return;
if (!TryGetHudPosition(texture.Width, texture.Height, out float x, out float y))
return;
cardRenderer.DrawAtScreen(x, y, texture);
}
private bool TryGetHudPosition(float cardWidth, float cardHeight, out float x, out float y)
{
x = Api.Input.MouseX - cardWidth / 2f;
y = Api.Input.MouseY + hud_offset_y;
cardRenderer.ClampToScreen(ref x, ref y, cardWidth, cardHeight);
return true;
}
}

View File

@@ -0,0 +1,329 @@
using ChestPreview.Configs;
using ChestPreview.Models;
using ChestPreview.Utils;
using System;
using System.Collections.Generic;
using System.Numerics;
using Vintagestory.API.Client;
using Vintagestory.API.Common;
using Vintagestory.API.MathTools;
using Vintagestory.GameContent;
namespace ChestPreview.Rendering;
internal class WorldBillboardPresenter(ICoreClientAPI api)
{
private const int multiblock_scan_radius = 2;
private static readonly float front_face_offset = 0.01f;
private readonly ICoreClientAPI api = api;
private readonly List<BillboardTarget> frameBillboards = [];
public IReadOnlyList<BillboardTarget> FrameBillboards => frameBillboards;
public void PrepareFrame(List<PreviewTarget> previewTargets)
{
frameBillboards.Clear();
foreach (PreviewTarget previewTarget in previewTargets)
{
if (!CanPresentAsBillboard(previewTarget))
continue;
if (!TryCreateBillboardTarget(previewTarget, out BillboardTarget billboardTarget))
continue;
frameBillboards.Add(billboardTarget);
}
}
public void Clear()
{
frameBillboards.Clear();
}
private static bool CanPresentAsBillboard(PreviewTarget previewTarget)
{
return previewTarget.Mode is PreviewModes.ON_HOVERED_CONTAINER or PreviewModes.ON_NEARBY_CONTAINERS;
}
private bool TryCreateBillboardTarget(PreviewTarget previewTarget, out BillboardTarget billboardTarget)
{
return TryCreateFrontBillboardTarget(previewTarget, out billboardTarget);
}
private bool TryCreateFrontBillboardTarget(PreviewTarget previewTarget, out BillboardTarget billboardTarget)
{
billboardTarget = default;
Block block = previewTarget.Block;
BlockPos blockPos = previewTarget.BlockEntity.Pos;
GetContainerBounds(block, blockPos, out Vector3 min, out Vector3 max);
if (!TryResolveFacingNormal(previewTarget.Block, previewTarget.BlockEntity, out Vec3f forward, out Vec3f right, out Vec3f up))
return false;
Vector3 centerLocal = (min + max) * 0.5f;
Vector3 halfExtents = (max - min) * 0.5f;
Vector3 rightVector = right.ToVector3();
Vector3 upVector = up.ToVector3();
Vector3 forwardVector = forward.ToVector3();
float halfWidth = Vector3.Dot(Vector3.Abs(rightVector), halfExtents);
float halfHeight = Vector3.Dot(Vector3.Abs(upVector), halfExtents);
float halfDepth = ResolveStableHalfDepth(forwardVector, halfExtents);
float width = halfWidth * 2f;
float height = halfHeight * 2f;
Vector3 center = blockPos.ToVector3() + centerLocal + forwardVector * (halfDepth + front_face_offset);
width = Math.Max(0.05f, width);
height = Math.Max(0.05f, height);
billboardTarget = new BillboardTarget(previewTarget, center.ToVec3d(), right, up, forward, width, height, true);
return true;
}
private static float ResolveStableHalfDepth(Vector3 forward, Vector3 halfExtents)
{
if (MathF.Abs(forward.Y) < 0.5f)
return MathF.Abs(forward.X) >= MathF.Abs(forward.Z) ? halfExtents.X : halfExtents.Z;
return MathF.Abs(forward.Y) >= MathF.Abs(forward.Z) ? halfExtents.Y : halfExtents.Z;
}
private void GetContainerBounds(Block block, BlockPos blockPos, out Vector3 min, out Vector3 max)
{
min = new Vector3(0f, 0f, 0f);
max = new Vector3(1f, 1f, 1f);
ExpandBoundsFromSelectionBoxes(block, blockPos, blockPos, ref min, ref max);
ExpandBoundsFromLinkedMultiblockParts(blockPos, ref min, ref max);
}
private void ExpandBoundsFromLinkedMultiblockParts(BlockPos controllerPos, ref Vector3 min, ref Vector3 max)
{
int minScanX = controllerPos.X - multiblock_scan_radius;
int maxScanX = controllerPos.X + multiblock_scan_radius;
int minScanY = controllerPos.Y - multiblock_scan_radius;
int maxScanY = controllerPos.Y + multiblock_scan_radius;
int minScanZ = controllerPos.Z - multiblock_scan_radius;
int maxScanZ = controllerPos.Z + multiblock_scan_radius;
Vector3 minLocal = min;
Vector3 maxLocal = max;
api.World.BlockAccessor.WalkBlocks(new BlockPos(minScanX, minScanY, minScanZ), new BlockPos(maxScanX, maxScanY, maxScanZ), (partBlock, x, y, z) =>
{
if (x == controllerPos.X && y == controllerPos.Y && z == controllerPos.Z)
return;
if (partBlock.Id == 0 || partBlock is not IMultiblockOffset multiblockOffset)
return;
BlockPos partPos = new(x, y, z);
BlockPos linkedControllerPos = multiblockOffset.GetControlBlockPos(partPos);
if (!IsSameBlockPos(linkedControllerPos, controllerPos))
return;
ExpandBoundsFromSelectionBoxes(partBlock, partPos, controllerPos, ref minLocal, ref maxLocal);
});
min = minLocal;
max = maxLocal;
}
private void ExpandBoundsFromSelectionBoxes(Block block, BlockPos sourcePos, BlockPos originPos, ref Vector3 min, ref Vector3 max)
{
Cuboidf[]? selectionBoxes = block.GetSelectionBoxes(api.World.BlockAccessor, sourcePos);
Vector3 sourceOffset = sourcePos.ToVector3() - originPos.ToVector3();
if (selectionBoxes is not { Length: > 0 })
{
min = Vector3.Min(min, sourceOffset);
max = Vector3.Max(max, sourceOffset + Vector3.One);
return;
}
foreach (Cuboidf selectionBox in selectionBoxes)
{
min = Vector3.Min(min, sourceOffset + selectionBox.Start.ToVector3());
max = Vector3.Max(max, sourceOffset + selectionBox.End.ToVector3());
}
}
private static bool IsSameBlockPos(BlockPos a, BlockPos b)
{
return a.X == b.X && a.Y == b.Y && a.Z == b.Z;
}
private bool TryResolveFacingNormal(Block block, BlockEntity blockEntity, out Vec3f forward, out Vec3f right, out Vec3f up)
{
forward = new Vec3f(0f, 0f, 1f);
right = new Vec3f(1f, 0f, 0f);
up = new Vec3f(0f, 1f, 0f);
if (TryGetMeshAngle(blockEntity, out float meshAngleRadians))
{
// MeshAngle is around Y axis in radians.
forward = new Vec3f(MathF.Sin(meshAngleRadians), 0f, MathF.Cos(meshAngleRadians));
NormalizeFacing(ref forward);
BuildBasisFromForward(forward, out right, out up);
return true;
}
if (TryGetBlockSideFacing(block, out forward))
{
BuildBasisFromForward(forward, out right, out up);
return true;
}
if (!TryGetPlayerFacing(blockEntity.Pos, out forward))
return false;
BuildBasisFromForward(forward, out right, out up);
return true;
}
private bool TryGetPlayerFacing(BlockPos blockPos, out Vec3f facing)
{
facing = new Vec3f(0f, 0f, 1f);
EntityPlayer? playerEntity = api.World.Player?.Entity;
if (playerEntity?.CameraPos == null)
return false;
Vec3d cameraPos = playerEntity.CameraPos + playerEntity.LocalEyePos;
double dx = cameraPos.X - (blockPos.X + 0.5d);
double dz = cameraPos.Z - (blockPos.Z + 0.5d);
if (Math.Abs(dx) <= 0.0001d && Math.Abs(dz) <= 0.0001d)
return false;
double absDx = Math.Abs(dx);
double absDz = Math.Abs(dz);
double diagonalTolerance = Math.Max(absDx, absDz) * 0.65d;
bool isDiagonalView = Math.Abs(absDx - absDz) <= diagonalTolerance;
if (isDiagonalView)
{
Vec3f facingX = dx >= 0d ? new Vec3f(1f, 0f, 0f) : new Vec3f(-1f, 0f, 0f);
Vec3f facingZ = dz >= 0d ? new Vec3f(0f, 0f, 1f) : new Vec3f(0f, 0f, -1f);
int obstructionX = GetFacingObstructionScore(blockPos, facingX);
int obstructionZ = GetFacingObstructionScore(blockPos, facingZ);
if (obstructionX != obstructionZ)
{
facing = obstructionX < obstructionZ ? facingX : facingZ;
return true;
}
}
facing = absDx >= absDz
? (dx >= 0d ? new Vec3f(1f, 0f, 0f) : new Vec3f(-1f, 0f, 0f))
: (dz >= 0d ? new Vec3f(0f, 0f, 1f) : new Vec3f(0f, 0f, -1f));
return true;
}
private int GetFacingObstructionScore(BlockPos blockPos, Vec3f facing)
{
int offsetX = Math.Sign(facing.X);
int offsetZ = Math.Sign(facing.Z);
int score = 0;
for (int yOffset = 0; yOffset <= 1; yOffset++)
{
BlockPos checkPos = new(blockPos.X + offsetX, blockPos.Y + yOffset, blockPos.Z + offsetZ);
if (IsSolidBlock(checkPos))
score++;
}
return score;
}
private bool IsSolidBlock(BlockPos blockPos)
{
Block block = api.World.BlockAccessor.GetBlock(blockPos);
if (block.Id == 0)
return false;
Cuboidf[]? collisionBoxes = block.GetCollisionBoxes(api.World.BlockAccessor, blockPos);
return collisionBoxes is { Length: > 0 };
}
private static bool TryGetBlockSideFacing(Block block, out Vec3f facing)
{
facing = new Vec3f(0f, 0f, 1f);
string? side = block.Variant?["side"];
if (string.IsNullOrWhiteSpace(side))
return false;
switch (side.ToLowerInvariant())
{
case "north":
facing = new Vec3f(0f, 0f, -1f);
return true;
case "south":
facing = new Vec3f(0f, 0f, 1f);
return true;
case "east":
facing = new Vec3f(1f, 0f, 0f);
return true;
case "west":
facing = new Vec3f(-1f, 0f, 0f);
return true;
case "up":
facing = new Vec3f(0f, 1f, 0f);
return true;
case "down":
facing = new Vec3f(0f, -1f, 0f);
return true;
default:
return false;
}
}
private static void BuildBasisFromForward(Vec3f forward, out Vec3f right, out Vec3f up)
{
up = new Vec3f(0f, 1f, 0f);
if (Math.Abs(forward.Y) > 0.5f)
{
up = forward.Y > 0f ? new Vec3f(0f, 0f, -1f) : new Vec3f(0f, 0f, 1f);
right = new Vec3f(1f, 0f, 0f);
return;
}
right = new Vec3f(-forward.Z, 0f, forward.X);
}
private static void NormalizeFacing(ref Vec3f facing)
{
float length = MathF.Sqrt(facing.X * facing.X + facing.Y * facing.Y + facing.Z * facing.Z);
if (length <= 0.0001f)
{
facing = new Vec3f(0f, 0f, 1f);
return;
}
float invLength = 1f / length;
facing = new Vec3f(facing.X * invLength, facing.Y * invLength, facing.Z * invLength);
}
private static bool TryGetMeshAngle(BlockEntity blockEntity, out float radians)
{
if (blockEntity is BlockEntityGenericTypedContainer genericContainer)
{
radians = genericContainer.MeshAngle;
return true;
}
if (blockEntity is BlockEntityCrate crate)
{
radians = crate.MeshAngle;
return true;
}
radians = 0f;
return false;
}
}

View File

@@ -0,0 +1,214 @@
using ChestPreview.Configs;
using ChestPreview.Models;
using System;
using System.Collections.Generic;
using Vintagestory.API.Client;
using Vintagestory.API.Common;
using Vintagestory.API.MathTools;
namespace ChestPreview.Rendering;
internal class WorldBillboardRenderer(ICoreClientAPI api, Config config, CardRenderer cardRenderer) : IRenderer
{
private static float card_cell_padding => 2f;
private static float card_cell_width => 72f;
private static float card_cell_gap => 4f;
private static float billboard_brightness => 1.18f;
private readonly ICoreClientAPI api = api;
private readonly List<PreviewTarget> frameTargets = [];
private readonly CardRenderer cardRenderer = cardRenderer;
private readonly PreviewTargetProvider targetProvider = new(api, config);
private readonly WorldBillboardPresenter worldBillboardPresenter = new(api);
private readonly MeshRef quadMeshRef = api.Render.UploadMesh(
QuadMeshUtil.GetCustomQuadModelData(
1f,
v: 1f,
0f,
v2: 0f,
dx: -1f,
dy: -1f,
dw: 2f,
dh: 2f,
r: byte.MaxValue,
g: byte.MaxValue,
b: byte.MaxValue,
a: byte.MaxValue
)
);
private bool disposed;
public ICoreClientAPI Api { get; } = api;
public Config Config { get; } = config;
public double RenderOrder => 0.61;
public int RenderRange => 24;
public void OnRenderFrame(float deltaTime, EnumRenderStage stage)
{
if (disposed || stage != EnumRenderStage.AfterOIT)
return;
frameTargets.Clear();
targetProvider.CollectTargets(deltaTime, frameTargets);
worldBillboardPresenter.PrepareFrame(frameTargets);
if (worldBillboardPresenter.FrameBillboards.Count == 0)
return;
api.Render.GlDisableCullFace();
api.Render.GLEnableDepthTest();
api.Render.GlToggleBlend(true, EnumBlendMode.PremultipliedAlpha);
try
{
foreach (BillboardTarget billboardTarget in worldBillboardPresenter.FrameBillboards)
{
int maxColumns = GetBillboardMaxColumns(billboardTarget);
if (!cardRenderer.TryGetOrCreateCardTexture(billboardTarget.PreviewTarget, maxColumns, out LoadedTexture texture))
continue;
RenderBillboard(billboardTarget, texture, maxColumns);
}
}
finally
{
StopShader(api.Render.CurrentActiveShader);
api.Render.GlToggleBlend(false, EnumBlendMode.PremultipliedAlpha);
api.Render.GLDisableDepthTest();
api.Render.GlEnableCullFace();
}
}
public void Dispose()
{
if (disposed)
return;
disposed = true;
frameTargets.Clear();
worldBillboardPresenter.Clear();
targetProvider.Dispose();
quadMeshRef.Dispose();
}
private void RenderBillboard(BillboardTarget billboardTarget, LoadedTexture texture, int maxColumns)
{
EntityPlayer? playerEntity = api.World.Player?.Entity;
if (playerEntity?.CameraPos == null)
return;
float referenceWidthPx = GetCardWidthPx(maxColumns);
float widthScale = texture.Width / Math.Max(1f, referenceWidthPx);
float textureAspect = texture.Height / (float)System.Math.Max(1, texture.Width);
float scaledWidth;
float textureHeight;
Vec3d center;
if (billboardTarget.IsFrontPlacement)
{
scaledWidth = billboardTarget.Width * widthScale;
textureHeight = scaledWidth * textureAspect;
if (scaledWidth > billboardTarget.Width)
{
scaledWidth = billboardTarget.Width;
textureHeight = scaledWidth * textureAspect;
}
if (textureHeight > billboardTarget.Height)
{
textureHeight = billboardTarget.Height;
scaledWidth = textureHeight / Math.Max(0.0001f, textureAspect);
}
center = billboardTarget.Center;
}
else
{
scaledWidth = billboardTarget.Width * widthScale;
textureHeight = scaledWidth * textureAspect;
center = new Vec3d(
billboardTarget.PreviewTarget.Anchor.X,
billboardTarget.PreviewTarget.Anchor.Y + textureHeight / 2f,
billboardTarget.PreviewTarget.Anchor.Z
);
}
float[] modelMatrix = CreateModelMatrix(billboardTarget, center, scaledWidth, textureHeight, playerEntity.CameraPos);
float brightness = billboard_brightness;
IStandardShaderProgram shader = api.Render.PreparedStandardShader(
billboardTarget.PreviewTarget.BlockEntity.Pos.X,
billboardTarget.PreviewTarget.BlockEntity.Pos.Y,
billboardTarget.PreviewTarget.BlockEntity.Pos.Z,
new Vec4f(brightness, brightness, brightness, 1f)
);
shader.Tex2D = texture.TextureId;
shader.AlphaTest = 0.01f;
shader.RgbaAmbientIn = new Vec3f(brightness, brightness, brightness);
shader.RgbaLightIn = new Vec4f(brightness, brightness, brightness, 1f);
shader.RgbaGlowIn = new Vec4f(0f, 0f, 0f, 0f);
shader.RgbaTint = new Vec4f(brightness, brightness, brightness, 1f);
shader.ModelMatrix = modelMatrix;
shader.ViewMatrix = api.Render.CameraMatrixOriginf;
shader.ProjectionMatrix = api.Render.CurrentProjectionMatrix;
api.Render.BindTexture2d(texture.TextureId);
api.Render.RenderMesh(quadMeshRef);
}
private static void StopShader(IShaderProgram? shader)
{
if (shader == null)
return;
try
{
shader.Stop();
}
catch
{
}
}
private static float GetCardWidthPx(int columns)
{
int safeColumns = Math.Max(1, columns);
return card_cell_padding * 2f + safeColumns * card_cell_width + (safeColumns - 1) * card_cell_gap;
}
private int GetBillboardMaxColumns(BillboardTarget billboardTarget)
{
int columnsPerBlock = Math.Max(1, Config.BillboardColumnsPerBlock <= 0 ? 1 : Config.BillboardColumnsPerBlock);
int blockWidth = Math.Max(1, (int)MathF.Round(billboardTarget.Width));
return columnsPerBlock * blockWidth;
}
private static float[] CreateModelMatrix(BillboardTarget billboardTarget, Vec3d center, float width, float height, Vec3d cameraPos)
{
float halfWidth = width / 2f;
float halfHeight = height / 2f;
return
[
billboardTarget.Right.X * halfWidth,
billboardTarget.Right.Y * halfWidth,
billboardTarget.Right.Z * halfWidth,
0f,
billboardTarget.Up.X * halfHeight,
billboardTarget.Up.Y * halfHeight,
billboardTarget.Up.Z * halfHeight,
0f,
billboardTarget.Forward.X,
billboardTarget.Forward.Y,
billboardTarget.Forward.Z,
0f,
(float)(center.X - cameraPos.X),
(float)(center.Y - cameraPos.Y),
(float)(center.Z - cameraPos.Z),
1f
];
}
}

View File

@@ -0,0 +1,22 @@
using System.Numerics;
using Vintagestory.API.MathTools;
namespace ChestPreview.Utils;
internal static class MathUtils
{
internal static Vector3 ToVector3(this BlockPos blockPos)
{
return new Vector3(blockPos.X, blockPos.Y, blockPos.Z);
}
internal static Vector3 ToVector3(this Vec3f vector)
{
return new Vector3(vector.X, vector.Y, vector.Z);
}
internal static Vec3d ToVec3d(this Vector3 vector)
{
return new Vec3d(vector.X, vector.Y, vector.Z);
}
}

View File

@@ -0,0 +1,86 @@
{
"version": 1,
"file": "chestpreview.json",
"patches": {},
"settings": [
{
"type": "separator",
"title": "Client-side"
},
{
"code": "Mode",
"comment": "config-desc-Mode",
"type": "string",
"default": "UnderCursor",
"values": [
"None",
"UnderCursor",
"OnHoveredContainer",
"OnNearbyContainers"
],
"clientSide": true
},
{
"code": "HoldKey",
"comment": "config-desc-HoldKey",
"type": "boolean",
"default": true,
"clientSide": true
},
{
"code": "BillboardColumnsPerBlock",
"comment": "config-desc-BillboardColumnsPerBlock",
"type": "int",
"default": 4,
"clientSide": true,
"range": {
"min": 2,
"max": 20
}
},
{
"code": "ColumnsUnderCursor",
"comment": "config-desc-ColumnsUnderCursor",
"type": "int",
"default": 10,
"clientSide": true,
"range": {
"min": 2,
"max": 20
}
},
{
"code": "PreviewNearbyRadius",
"comment": "config-desc-PreviewNearbyRadius",
"type": "int",
"default": 8,
"clientSide": true,
"logarithmic": true,
"range": {
"min": 3,
"max": 30
}
},
{
"code": "WhitelistedContainersOnly",
"comment": "config-desc-WhitelistedContainersOnly",
"type": "boolean",
"default": true,
"clientSide": true
},
{
"code": "WhitelistedContainers",
"comment": "config-desc-WhitelistedContainers",
"type": "string",
"default": "barrel, crate, chest-*, trunk-*, storagevessel-*, stationarybasket-*, labeledchest-*",
"clientSide": true
},
{
"code": "BlacklistedContainers",
"comment": "config-desc-BlacklistedContainers",
"type": "string",
"default": "",
"clientSide": true
}
]
}

View File

@@ -0,0 +1,12 @@
{
"hotkey-preview-container": "Preview container",
"hotkey-preview-containers-nearby": "Preview containers nearby",
"config-desc-Mode": "Preview mode. Valid values: None, UnderCursor, OnHoveredContainer, OnNearbyContainers.",
"config-desc-HoldKey": "Previews only show while the \"Preview containers\" key is held.",
"config-desc-BillboardColumnsPerBlock": "Columns per block width used for world billboards. For containers 2 blocks wide it's doubled.",
"config-desc-ColumnsUnderCursor": "Columns in the UnderCursor mode.",
"config-desc-PreviewNearbyRadius": "Radius for \"Preview containers nearby\".",
"config-desc-WhitelistedContainersOnly": "If true, only container codes in WhitelistedContainers are handled.",
"config-desc-WhitelistedContainers": "Allowed container codes, separated by comma, semicolon, or spaces. No effectif WhitelistedContainersOnly==false.",
"config-desc-BlacklistedContainers": "Blocked container codes, separated by comma, semicolon, or spaces."
}

View File

@@ -0,0 +1,15 @@
{
"$schema": "https://moddbcdn.vintagestory.at/schema/modinfo.latest.json",
"type": "Code",
"modid": "chestpreview",
"name": "Chest Preview",
"authors": [
"OrekiWoof"
],
"description": "see containers' contents without having to open them",
"version": "1.0.0-pre.1",
"dependencies": {
"game": "1.21.0"
},
"side": "Client"
}

View File

@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RunWorkingDirectory>$(MSBuildProjectDirectory)</RunWorkingDirectory>
<Configurations>Debug;Release;Version22</Configurations>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Cake.Frosting" Version="5.0.0" />
<PackageReference Include="Cake.Json" Version="7.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
<ItemGroup>
<Reference Include="VintagestoryAPI">
<HintPath>$(VINTAGE_STORY)/VintagestoryAPI.dll</HintPath>
</Reference>
</ItemGroup>
</Project>

View File

@@ -0,0 +1,122 @@
using Cake.Common;
using Cake.Common.IO;
using Cake.Common.Tools.DotNet;
using Cake.Common.Tools.DotNet.Clean;
using Cake.Common.Tools.DotNet.Publish;
using Cake.Core;
using Cake.Frosting;
using Cake.Json;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.IO;
using System.Linq;
using Vintagestory.API.Common;
namespace CakeBuild;
public static class Program
{
public static int Main(string[] args)
{
return new CakeHost()
.UseContext<BuildContext>()
.Run(args);
}
}
public class BuildContext : FrostingContext
{
public const string PROJECT_NAME = "ChestPreview";
public string BuildConfiguration { get; }
public string Version { get; }
public string Name { get; }
public bool SkipJsonValidation { get; }
public string GameVersion { get; }
public BuildContext(ICakeContext context)
: base(context)
{
BuildConfiguration = context.Argument("configuration", "Release");
SkipJsonValidation = context.Argument("skipJsonValidation", false);
var modInfo = context.DeserializeJsonFromFile<ModInfo>($"../{PROJECT_NAME}/modinfo.json");
Version = modInfo.Version;
Name = modInfo.ModID;
GameVersion = modInfo.Dependencies.First(x => x.ModID == "game").Version;
}
}
[TaskName("ValidateJson")]
public sealed class ValidateJsonTask : FrostingTask<BuildContext>
{
public override void Run(BuildContext context)
{
if (context.SkipJsonValidation)
{
return;
}
var jsonFiles = context.GetFiles($"../{BuildContext.PROJECT_NAME}/assets/**/*.json");
foreach (var file in jsonFiles)
{
try
{
var json = File.ReadAllText(file.FullPath);
JToken.Parse(json);
}
catch (JsonException ex)
{
throw new Exception($"Validation failed for JSON file: {file.FullPath}{Environment.NewLine}{ex.Message}", ex);
}
}
}
}
[TaskName("Build")]
[IsDependentOn(typeof(ValidateJsonTask))]
public sealed class BuildTask : FrostingTask<BuildContext>
{
public override void Run(BuildContext context)
{
context.DotNetClean($"../{BuildContext.PROJECT_NAME}/{BuildContext.PROJECT_NAME}.csproj",
new DotNetCleanSettings
{
Configuration = context.BuildConfiguration
});
context.DotNetPublish($"../{BuildContext.PROJECT_NAME}/{BuildContext.PROJECT_NAME}.csproj",
new DotNetPublishSettings
{
Configuration = context.BuildConfiguration
});
}
}
[TaskName("Package")]
[IsDependentOn(typeof(BuildTask))]
public sealed class PackageTask : FrostingTask<BuildContext>
{
public override void Run(BuildContext context)
{
context.EnsureDirectoryExists("../Releases");
context.CleanDirectory("../Releases");
context.EnsureDirectoryExists($"../Releases/{context.Name}");
context.CopyFiles($"../{BuildContext.PROJECT_NAME}/bin/{context.BuildConfiguration}/Mods/mod/publish/*", $"../Releases/{context.Name}");
if (context.DirectoryExists($"../{BuildContext.PROJECT_NAME}/assets"))
{
context.CopyDirectory($"../{BuildContext.PROJECT_NAME}/assets", $"../Releases/{context.Name}/assets");
}
context.CopyFile($"../{BuildContext.PROJECT_NAME}/modinfo.json", $"../Releases/{context.Name}/modinfo.json");
if (context.FileExists($"../{BuildContext.PROJECT_NAME}/modicon.png"))
{
context.CopyFile($"../{BuildContext.PROJECT_NAME}/modicon.png", $"../Releases/{context.Name}/modicon.png");
}
context.Zip($"../Releases/{context.Name}", $"../Releases/{context.Name}_v{context.Version}-v{context.GameVersion}.zip");
}
}
[TaskName("Default")]
[IsDependentOn(typeof(PackageTask))]
public class DefaultTask : FrostingTask
{
}

2
ChestPreview/build.ps1 Normal file
View File

@@ -0,0 +1,2 @@
dotnet run --project ZZCakeBuild/CakeBuild.csproj -- $args
exit $LASTEXITCODE;

1
ChestPreview/build.sh Normal file
View File

@@ -0,0 +1 @@
dotnet run --project ./ZZCakeBuild/CakeBuild.csproj -- "$@"