Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions ItemCatalogueUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Terraria.ID;
using Terraria.ModLoader;
using Terraria.UI;
using System.Threading.Tasks;

namespace RecipeBrowser
{
Expand Down Expand Up @@ -243,7 +244,7 @@ private void ValidateItemDescription()
updateNeeded = true;
}

internal void Update()
internal async void Update()
{
// TODO: investigate why this Update is slower than RecipeCatalogueUI

Expand Down Expand Up @@ -296,10 +297,18 @@ internal void Update()
List<UIItemCatalogueItemSlot> slotsToUse = itemSlots;

if (SharedUI.instance.SelectedCategory.name == ArmorSetFeatureHelper.ArmorSetsHoverTest) {
if (ArmorSetFeatureHelper.armorSetSlots == null)
ArmorSetFeatureHelper.CalculateArmorSets();
slotsToUse = ArmorSetFeatureHelper.armorSetSlots.Cast<UIItemCatalogueItemSlot>().ToList();
ArmorSetFeatureHelper.AppendSpecialUI(itemGrid);
if (!(ArmorSetFeatureHelper.hasCalculated) && !(ArmorSetFeatureHelper.hasStarted))
await Task.Run(() => ArmorSetFeatureHelper.CalculateArmorSets());
if (ArmorSetFeatureHelper.hasCalculated && ArmorSetFeatureHelper.armorSetSlotsMutex.WaitOne(10))
{
slotsToUse = ArmorSetFeatureHelper.armorSetSlots.Cast<UIItemCatalogueItemSlot>().ToList();
ArmorSetFeatureHelper.armorSetSlotsMutex.ReleaseMutex();

ArmorSetFeatureHelper.AppendSpecialUI(itemGrid);
}
else
slotsToUse = new List<UIItemCatalogueItemSlot>(); //empty

}

foreach (var slot in slotsToUse)
Expand Down
55 changes: 43 additions & 12 deletions UIElements/UIArmorSetCatalogueItemSlot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using Terraria.ModLoader;
using Terraria.ModLoader.UI;
using Terraria.UI;
using Terraria.ID;
using System.Threading;

namespace RecipeBrowser.UIElements
{
Expand Down Expand Up @@ -76,7 +78,7 @@ public override void Update(GameTime gameTime) {
this.Height.Set(defaultBackgroundTexture.Height() * 4.6f * scale, 0f); // 50 heigh
else
this.Height.Set(defaultBackgroundTexture.Height() * 1.6f * scale, 0f);

needsUpdate = false;
}

Expand Down Expand Up @@ -197,11 +199,20 @@ internal static class ArmorSetFeatureHelper
{
internal static List<Tuple<Item, Item, Item, string, int>> sets;
internal static List<UIArmorSetCatalogueItemSlot> armorSetSlots;
internal static Mutex armorSetSlotsMutex = new Mutex();
internal static bool hasCalculated = false;
internal static bool hasStarted = false;
internal const string ArmorSetsHoverTest = "Armor Sets\n(Warning: May take many seconds to calculate)";

internal static void Unload() {
sets = null;

while (!armorSetSlotsMutex.WaitOne(100)) { }
armorSetSlots = null;
armorSetSlotsMutex.ReleaseMutex();

hasCalculated = false;
hasStarted = false;
UIArmorSetCatalogueItemSlot.drawPlayer = null;
}

Expand All @@ -214,10 +225,16 @@ internal static void AppendSpecialUI(UIGrid itemGrid) {

var showItemsCheckbox = new UICheckbox("Show Items", "Display the items that make up the set");
showItemsCheckbox.Selected = UIArmorSetCatalogueItemSlot.showItems;
showItemsCheckbox.OnSelectedChanged += (s, e) => {
showItemsCheckbox.OnSelectedChanged += (s, e) =>
{
UIArmorSetCatalogueItemSlot.showItems = showItemsCheckbox.Selected;
foreach (var item in armorSetSlots) {
item.needsUpdate = true;
if (armorSetSlots != null && armorSetSlotsMutex.WaitOne(10)) //we can't be clogging up main thread waiting for this
{
foreach (var item in armorSetSlots)
{
item.needsUpdate = true;
}
armorSetSlotsMutex.ReleaseMutex();
}
};
showItemsCheckbox.Left.Set(0, 0);
Expand Down Expand Up @@ -255,6 +272,7 @@ internal static void AppendSpecialUI(UIGrid itemGrid) {
}

internal static void CalculateArmorSets() {
hasStarted = true;
//new Category("Head", x => x.headSlot != -1, smallHead),
//new Category("Body", x => x.bodySlot != -1, smallBody),
//new Category("Legs", x => x.legSlot != -1, smallLegs),
Expand All @@ -263,23 +281,29 @@ internal static void CalculateArmorSets() {
List<Item> Heads = new List<Item>();
List<Item> Bodys = new List<Item>();
List<Item> Legs = new List<Item>();

// go thru all items and get all the armor pieces into their respective arrays
for (int type = 1; type < ItemLoader.ItemCount; type++) {
Item item = new Item();
item.SetDefaults(type, false);
if (item.type == 0)
continue;

if (item.headSlot != -1)

if (item.type == ItemID.None)
continue;

if (item.headSlot != -1)
Heads.Add(item);
if (item.bodySlot != -1)
if (item.bodySlot != -1)
Bodys.Add(item);
if (item.legSlot != -1)
if (item.legSlot != -1)
Legs.Add(item);
}
sets = new List<Tuple<Item, Item, Item, string, int>>();

sets = new List<Tuple<Item, Item, Item, string, int>>();
foreach (var head in Heads) {
foreach (var body in Bodys) {
foreach (var leg in Legs) {

testPlayer.statDefense = Player.DefenseStat.Default;
testPlayer.head = head.headSlot;
testPlayer.body = body.bodySlot;
Expand Down Expand Up @@ -348,12 +372,19 @@ internal static void CalculateArmorSets() {
// Check Head/Body, Head/Legs, etc?

armorSetSlots = new List<UIArmorSetCatalogueItemSlot>();
if (armorSetSlots.Count == 0) {
foreach (var set in sets) {
while (!armorSetSlotsMutex.WaitOne(1000)) { } //acquire mutex

if (armorSetSlots.Count == 0)
{
foreach (var set in sets)
{
var slot = new UIArmorSetCatalogueItemSlot(set);
armorSetSlots.Add(slot);
}
}
armorSetSlotsMutex.ReleaseMutex(); //release mutex
hasCalculated = true;

}
}
}