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 Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using ReLogic.Content;
using System;
using System.Collections.Generic;
using System.Text;
using Terraria;
using Terraria.Audio;
using Terraria.GameContent;
using Terraria.GameContent.UI.Elements;
using Terraria.GameInput;
using Terraria.ID;
using Terraria.Localization;
using Terraria.ModLoader;
using Terraria.UI;

namespace SoulHarvest.UI;

internal sealed class ReaperTreeButton : UIElement
{
    private readonly Action action;
    private readonly Color normalColor;
    private readonly Color hoverColor;
    private readonly Color borderColor;
    private string text = string.Empty;
    private float layoutScale = 1f;
    private float baseTextScale = 0.65f;
    private bool enabled = true;

    public bool Enabled
    {
        get => enabled;
        set
        {
            enabled = value;
            IgnoresMouseInteraction = !value;
        }
    }

    public ReaperTreeButton(Action action, Color normalColor, Color hoverColor, Color borderColor)
    {
        this.action = action;
        this.normalColor = normalColor;
        this.hoverColor = hoverColor;
        this.borderColor = borderColor;
        SetPadding(0f);
    }

    public void ApplyLayoutScale(float scale, float textScale)
    {
        layoutScale = scale;
        baseTextScale = textScale;
    }
    public void SetText(string value) => text = value;

    protected override void DrawSelf(SpriteBatch spriteBatch)
    {
        Rectangle bounds = GetDimensions().ToRectangle();
        Texture2D pixel = TextureAssets.MagicPixel.Value;
        Color background = !enabled ? new Color(29, 27, 29) : IsMouseHovering ? hoverColor : normalColor;
        Color border = !enabled ? new Color(66, 60, 59) : IsMouseHovering ? borderColor : borderColor * 0.68f;
        spriteBatch.Draw(pixel, bounds, background * 0.98f);
        ReaperTreeDraw.DrawBorder(spriteBatch, pixel, bounds, border, Math.Max(1, (int)(2f * layoutScale)));
        ReaperTreeDraw.DrawDiamond(spriteBatch, pixel, new Vector2(bounds.X + 11f * layoutScale, bounds.Center.Y), 4f * layoutScale, border);
        ReaperTreeDraw.DrawDiamond(spriteBatch, pixel, new Vector2(bounds.Right - 11f * layoutScale, bounds.Center.Y), 4f * layoutScale, border);
        float textScale = baseTextScale * layoutScale;
        string fitted = AltarDrawHelpers.FitText(text, bounds.Width - 32f * layoutScale, textScale);
        Vector2 size = FontAssets.MouseText.Value.MeasureString(fitted) * textScale;
        Utils.DrawBorderString(spriteBatch, fitted, new Vector2(bounds.Center.X - size.X * 0.5f, bounds.Center.Y - size.Y * 0.5f), enabled ? Color.White : new Color(116, 108, 104), textScale);
    }

    public override void LeftClick(UIMouseEvent evt)
    {
        base.LeftClick(evt);
        if (enabled)
            action();
    }
}