XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

DeathMod

【Terraria】DeathMod (now aka Soul Harvest)

公开
关注 0 Fork 0 Star 0
UTF-8
using SoulHarvest.Common;
using SoulHarvest.Items;
using SoulHarvest.Tiles;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
using Terraria;
using Terraria.Audio;
using Terraria.GameContent;
using Terraria.GameContent.UI.Elements;
using Terraria.ID;
using Terraria.Localization;
using Terraria.ModLoader;
using Terraria.UI;

namespace SoulHarvest.UI;

internal static class AltarDrawHelpers
{
    public static void DrawItemIcon(SpriteBatch spriteBatch, Item item, Vector2 center, float maximumSize)
    {
        Main.instance.LoadItem(item.type);
        Texture2D texture = TextureAssets.Item[item.type].Value;
        Rectangle frame = Main.itemAnimations[item.type]?.GetFrame(texture) ?? texture.Frame();
        float scale = Math.Min(1f, maximumSize / Math.Max(frame.Width, frame.Height));
        spriteBatch.Draw(texture, center, frame, Color.White, 0f, frame.Size() * 0.5f, scale, SpriteEffects.None, 0f);
    }

    public static void DrawItemSlot(SpriteBatch spriteBatch, Texture2D pixel, Vector2 center, float size, Color background, Color border)
    {
        int slotSize = Math.Max(1, (int)MathF.Round(size));
        Rectangle bounds = new(
            (int)MathF.Round(center.X - slotSize * 0.5f),
            (int)MathF.Round(center.Y - slotSize * 0.5f),
            slotSize,
            slotSize);
        spriteBatch.Draw(pixel, bounds, background * 0.94f);
        DrawBorder(spriteBatch, pixel, bounds, border, Math.Max(1, (int)MathF.Round(size / 30f)));
    }

    public static string FitText(string text, float maximumWidth, float scale)
    {
        if (maximumWidth <= 0f || scale <= 0f)
            return string.Empty;
        if (FontAssets.MouseText.Value.MeasureString(text).X * scale <= maximumWidth)
            return text;

        const string ellipsis = "…";
        while (text.Length > 1
            && FontAssets.MouseText.Value.MeasureString(text + ellipsis).X * scale > maximumWidth)
        {
            text = text[..^1];
        }
        return text + ellipsis;
    }

    public static void DrawBorder(SpriteBatch batch, Texture2D pixel, Rectangle bounds, Color color, int thickness)
    {
        thickness = Math.Clamp(thickness, 1, Math.Max(1, Math.Min(bounds.Width, bounds.Height) / 2));
        batch.Draw(pixel, new Rectangle(bounds.X, bounds.Y, bounds.Width, thickness), color);
        batch.Draw(pixel, new Rectangle(bounds.X, bounds.Bottom - thickness, bounds.Width, thickness), color);
        batch.Draw(pixel, new Rectangle(bounds.X, bounds.Y, thickness, bounds.Height), color);
        batch.Draw(pixel, new Rectangle(bounds.Right - thickness, bounds.Y, thickness, bounds.Height), color);
    }
}