feat: Rank item editor prefixes by value and color them by rarity

This commit is contained in:
VAC committed 2026-10-16 22:15:54 -03:00
1 parent 249787187d
commit 23ac33cb93
1 file changed
+30 -5
+30 -5
View File
@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Terraria;
using Terraria.GameContent.UI;
using Terraria.ID;
#if TMODLOADER
using Terraria.ModLoader;
@@ -19,6 +20,8 @@ public sealed class ItemEditor() : Window("itemEditor", "Item Editor", new Vecto
readonly Scroll _scroll = new();
readonly Item _reference = new();
(int Type, int Prefix) _referenceKey = (-1, -1);
(int Prefix, int Value, int Rarity)[] _prefixOptions = [];
int _prefixType = -1;
int _x, _y, _width;
bool _firstGroup;
@@ -141,21 +144,43 @@ public sealed class ItemEditor() : Window("itemEditor", "Item Editor", new Vecto
return Lang.prefix[prefix].Value;
}
// Ranked by the value each prefix gives, like reforge tiers.
(int Prefix, int Value, int Rarity)[] PrefixOptions(Item item)
{
if (_prefixType == item.type)
return _prefixOptions;
var sample = new Item();
_prefixOptions = [.. Prefixes(item).Prepend(0).Select(prefix =>
{
sample.SetDefaults(item.type);
if (prefix > 0)
sample.Prefix(prefix);
return (prefix, sample.value, sample.rare);
}).OrderBy(option => option.prefix != 0).ThenByDescending(option => option.value).ThenBy(option => PrefixName(option.prefix), StringComparer.OrdinalIgnoreCase)];
_prefixType = item.type;
return _prefixOptions;
}
void DrawPrefixes(Item item)
{
int[] prefixes = [.. Prefixes(item).OrderBy(PrefixName, StringComparer.OrdinalIgnoreCase)];
if (prefixes.Length == 0)
(int Prefix, int Value, int Rarity)[] options = PrefixOptions(item);
if (options.Length <= 1)
return;
Group("Prefix");
const int Columns = 3, Gap = 6;
const float Scale = 0.76f;
int width = (_width - Gap * (Columns - 1)) / Columns;
int[] options = [0, .. prefixes];
for (int i = 0; i < options.Length; i++)
{
var bounds = new Rectangle(_x + i % Columns * (width + Gap), _y + i / Columns * 34, width, 30);
if (Widgets.Button(bounds, options[i] == 0 ? "None" : PrefixName(options[i]), item.prefix == options[i], 0.76f) && item.prefix != options[i])
ApplyPrefix(item, options[i]);
int prefix = options[i].Prefix;
if (Widgets.Button(bounds, "", item.prefix == prefix, Scale) && item.prefix != prefix)
ApplyPrefix(item, prefix);
string name = Render.Ellipsize(prefix == 0 ? "None" : PrefixName(prefix), Scale, bounds.Width - 12);
Render.TextCentered(Widgets.SpriteBatch, name, bounds, ItemRarity.GetColor(options[i].Rarity), Scale);
}
_y += (options.Length + Columns - 1) / Columns * 34 + 4;
}