Skip to content

Commit

Permalink
v1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
NotLe0n committed Apr 17, 2021
1 parent 88aa86d commit bb26880
Show file tree
Hide file tree
Showing 11 changed files with 444 additions and 136 deletions.
51 changes: 0 additions & 51 deletions BetterChests.cs

This file was deleted.

12 changes: 6 additions & 6 deletions BetterChests.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31112.23
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BetterChests", "BetterChests.csproj", "{4AE73323-57DD-4F6E-BA82-91458D43C821}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BetterChests", "BetterChests.csproj", "{F911D326-3D9A-4EDC-84EF-AA1A5D88709A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{4AE73323-57DD-4F6E-BA82-91458D43C821}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4AE73323-57DD-4F6E-BA82-91458D43C821}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4AE73323-57DD-4F6E-BA82-91458D43C821}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4AE73323-57DD-4F6E-BA82-91458D43C821}.Release|Any CPU.Build.0 = Release|Any CPU
{F911D326-3D9A-4EDC-84EF-AA1A5D88709A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F911D326-3D9A-4EDC-84EF-AA1A5D88709A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F911D326-3D9A-4EDC-84EF-AA1A5D88709A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F911D326-3D9A-4EDC-84EF-AA1A5D88709A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {4EC07AD4-6ABE-46BC-927C-8BA28E80A370}
SolutionGuid = {53288DF6-7277-4755-B5CE-763F332F828F}
EndGlobalSection
EndGlobal
40 changes: 0 additions & 40 deletions ILEdits.cs

This file was deleted.

2 changes: 1 addition & 1 deletion build.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
displayName = Better Chests
author = NotLe0n
version = 0.4
version = 1.0
hideCode = false
hideResources = false
includeSource = false
Expand Down
18 changes: 16 additions & 2 deletions description.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
[c/FFE900:This mod adds more options to sort your chests]
[c/FFE900:This mod improves some aspects of vanilla chests]

For a Changlog and more information visit my homepage!

Contact [c/66EEFF:NotLe0n#7696] on discord if you have any problems.

Special Thanks to:
[c/66EEFF:direwolf420, darthmorf and jopojelly for a UIElement]

_________________________

[c/FFE900: Changes]
_________________________

- "Deposit All" and "Loot all" have a confirmation button now.
- Clicking on "Sort Items" will open a menu with more sort options to the right and a search bar below the chest UI.

_________________________

[c/FFE900: How to use]
_________________________

[c/C4C4C4: Simply open a chest and click on "Sort Items".]
[c/C4C4C4: This will open another menu to the right.]
[c/C4C4C4: This will open another menu to the right.]

[c/C4C4C4: Use the search bar to search for items]
88 changes: 88 additions & 0 deletions src/BetterChests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using Microsoft.Xna.Framework;
using System.Collections.Generic;
using Terraria;
using Terraria.ModLoader;
using Terraria.UI;

namespace BetterChests.src
{
public class BetterChests : Mod
{
internal UserInterface SortUserInterface;
internal UserInterface ConfirmationUserInterface;
public static BetterChests instance;

public override void Load()
{
instance = this;

if (!Main.dedServ)
{
SortUserInterface = new UserInterface();
SortUserInterface.SetState(new SortOptionsUI());

ConfirmationUserInterface = new UserInterface();
ConfirmationUserInterface.SetState(new ConfirmationUI());
}

ILEdits.Load();
}

public override void Unload()
{
instance = null;

SortUserInterface = null;
ConfirmationUserInterface = null;

base.Unload();
}

private GameTime _lastUpdateUiGameTime;
public override void UpdateUI(GameTime gameTime)
{
_lastUpdateUiGameTime = gameTime;
if (Main.LocalPlayer.chest != -1)
{
if (SortOptionsUI.visible)
{
SortUserInterface.Update(gameTime);
}

if (ConfirmationUI.visible)
{
ConfirmationUserInterface.Update(gameTime);
}
}
else
{
ConfirmationUI.visible = false;
}
}

public override void ModifyInterfaceLayers(List<GameInterfaceLayer> layers)
{
int mouseTextIndex = layers.FindIndex(layer => layer.Name.Equals("Vanilla: Mouse Text"));
if (mouseTextIndex != -1)
{
layers.Insert(mouseTextIndex, new LegacyGameInterfaceLayer(
"BetterChests: UI",
delegate
{
if (Main.LocalPlayer.chest != -1)
{
if (SortOptionsUI.visible)
{
SortUserInterface.Draw(Main.spriteBatch, _lastUpdateUiGameTime);
}
if (ConfirmationUI.visible)
{
ConfirmationUserInterface.Draw(Main.spriteBatch, _lastUpdateUiGameTime);
}
}
return true;
}, InterfaceScaleType.UI));
}
}
}
}
37 changes: 37 additions & 0 deletions src/ConfirmationUI.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Terraria;
using Terraria.GameContent.UI.Elements;
using Terraria.UI;

namespace BetterChests.src
{
internal class ConfirmationUI : UIState
{
public static bool visible = false;
public int buttonID;
public float topOffset;
public MouseEvent onclick;

public override void OnInitialize()
{
var confirmation = new UITextOption("Are you sure?");
confirmation.TextColor = Color.Red;
confirmation.Top.Set(Main.instance.invBottom + topOffset, 0);
confirmation.Left.Set(506, 0); // magic number because vanilla does it the same way lmao
confirmation.OnMouseDown += onclick;
Append(confirmation);
}

public override void Update(GameTime gameTime)
{
base.Update(gameTime);

ChestUI.ButtonScale[buttonID] = 0f;
}
}
}
82 changes: 82 additions & 0 deletions src/ILEdits.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using MonoMod.Cil;
using System.Reflection;
using Terraria.UI;

namespace BetterChests.src
{
public class ILEdits
{
public static void Load()
{
IL.Terraria.UI.ChestUI.DrawButton += EditButton;
}

private static void EditButton(ILContext il)
{
var c = new ILCursor(il);

if (!c.TryGotoNext(MoveType.After, i => i.MatchCall<ChestUI>("LootAll")))
return;

c.Prev.Operand = typeof(ILEdits).GetMethod("OpenLootConfirmation", BindingFlags.NonPublic | BindingFlags.Static);

// IL_0362: br.s IL_038C
// IL_0364: call void Terraria.UI.ChestUI::DepositAll()
// <=== here

if (!c.TryGotoNext(MoveType.After, i => i.MatchCall<ChestUI>("DepositAll")))
return;

c.Prev.Operand = typeof(ILEdits).GetMethod("OpenDepositConfirmation", BindingFlags.NonPublic | BindingFlags.Static);

// The goal of this IL edit is to replace SortChest() with code to open my UI
// IL_0385: br.s IL_038C
// IL_0387: call void Terraria.UI.ItemSorting::SortChest()
// <=== here

if (!c.TryGotoNext(MoveType.After, i => i.MatchCall<ItemSorting>("SortChest")))
return;

c.Prev.Operand = typeof(ILEdits).GetMethod("ToggleSortUI", BindingFlags.NonPublic | BindingFlags.Static);
}

private static void ToggleSortUI()
{
SortOptionsUI.visible = !SortOptionsUI.visible;
}

private static void OpenDepositConfirmation()
{
var ui = new ConfirmationUI
{
buttonID = ChestUI.ButtonID.DepositAll,
topOffset = 55,
onclick = (evt, elm) =>
{
ChestUI.DepositAll();
ConfirmationUI.visible = false;
}
};

BetterChests.instance.ConfirmationUserInterface.SetState(ui);
ConfirmationUI.visible = true;
}

private static void OpenLootConfirmation()
{
var ui = new ConfirmationUI
{
buttonID = ChestUI.ButtonID.LootAll,
topOffset = 30,
onclick = (evt, elm) =>
{
ChestUI.LootAll();
ConfirmationUI.visible = false;
}
};

BetterChests.instance.ConfirmationUserInterface.SetState(ui);
ConfirmationUI.visible = true;
}
}
}
Loading

0 comments on commit bb26880

Please sign in to comment.