Skip to content

Commit

Permalink
Direct port of "Added support for Modded Builder Toggles", tModLoader…
Browse files Browse the repository at this point in the history
…#2511 (tModLoader#3731)

* Direct port of "Added support for Modded Builder Toggles", tModLoader#2511

Co-Authored-By: Kirtle <[email protected]>

* Convert to file-scoped namespaces

Co-Authored-By: Kirtle <[email protected]>

* Save and load modded builder toggles, doc updates

* Update ExampleBuilderToggle.cs

* Remove GlobalBuilderToggle, it's impossible to use correctly with multiple mods

* Localize, custom hover icon support

* Move a patch to .TML.cs

---------

Co-authored-by: Kirtle <[email protected]>
  • Loading branch information
JavidPack and xKirtle committed Aug 17, 2023
1 parent bdc36b6 commit 716495e
Show file tree
Hide file tree
Showing 16 changed files with 829 additions and 8 deletions.
53 changes: 53 additions & 0 deletions ExampleMod/Content/ExampleBuilderToggle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using Microsoft.Xna.Framework;
using Terraria;
using Terraria.Localization;
using Terraria.ModLoader;

namespace ExampleMod.Content
{
// The examples in this file don't actually affect anything, they just show typical approaches for the BuilderToggle half of the effect.
// A full example would have code doing something, such as drawing an overlay, after checking ModContent.GetInstance<YourBuilderToggle>().Active and ModContent.GetInstance<YourBuilderToggle>().CurrentState.
// That code is highly dependent on what you want to accomplish.

public class ExampleBuilderToggle : BuilderToggle
{
public override bool Active() => Main.LocalPlayer.HeldItem.IsAir;

public override int NumberOfStates => 4;
public override string DisplayValue() {
string text = "Color: ";
string[] textMessages = new[] { "Red", "Blue", "Green", "Yellow" };

return text + textMessages[CurrentState];
}

public override Color DisplayColorTexture() {
Color[] colors = new[] { Color.Red, Color.Blue, Color.Green, Color.Yellow };

return colors[CurrentState];
}
}

public class ExampleBuilderToggleDimmedLight : BuilderToggle
{
public static LocalizedText OnText { get; private set; }
public static LocalizedText OffText { get; private set; }

public override string Texture => "ExampleMod/Content/ExampleBuilderToggle";
public override bool Active() => true;
public override int NumberOfStates => 2;

public override void SetStaticDefaults() {
OnText = this.GetLocalization(nameof(OnText));
OffText = this.GetLocalization(nameof(OffText));
}

public override string DisplayValue() {
return CurrentState == 0 ? OnText.Value : OffText.Value;
}

public override Color DisplayColorTexture() {
return CurrentState == 0 ? Color.White : new Color(127, 127, 127);
}
}
}
Binary file added ExampleMod/Content/ExampleBuilderToggle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions ExampleMod/Localization/TranslationsNeeded.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
en-US, 504/504, 100%, missing 0
ru-RU, 6/504, 1%, missing 498
zh-Hans, 1/504, 0%, missing 503
en-US, 506/506, 100%, missing 0
ru-RU, 6/506, 1%, missing 500
zh-Hans, 1/506, 0%, missing 505
7 changes: 7 additions & 0 deletions ExampleMod/Localization/en-US.hjson
Original file line number Diff line number Diff line change
Expand Up @@ -1085,6 +1085,13 @@ Mods: {

UI.ExampleResource: Example Resource: {0} / {1}
WorldGen.ExampleOrePassMessage: Example Mod Ores

BuilderToggles: {
ExampleBuilderToggleDimmedLight: {
OnText: Example On
OffText: Example Off
}
}
}
}

Expand Down
7 changes: 7 additions & 0 deletions ExampleMod/Localization/ru-RU.hjson
Original file line number Diff line number Diff line change
Expand Up @@ -1085,6 +1085,13 @@ Mods: {

// UI.ExampleResource: Example Resource: {0} / {1}
// WorldGen.ExampleOrePassMessage: Example Mod Ores

BuilderToggles: {
ExampleBuilderToggleDimmedLight: {
// OnText: Example On
// OffText: Example Off
}
}
}
}

Expand Down
7 changes: 7 additions & 0 deletions ExampleMod/Localization/zh-Hans.hjson
Original file line number Diff line number Diff line change
Expand Up @@ -1085,6 +1085,13 @@ Mods: {

// UI.ExampleResource: Example Resource: {0} / {1}
// WorldGen.ExampleOrePassMessage: Example Mod Ores

BuilderToggles: {
ExampleBuilderToggleDimmedLight: {
// OnText: Example On
// OffText: Example Off
}
}
}
}

Expand Down
164 changes: 164 additions & 0 deletions patches/tModLoader/Terraria/Main.TML.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@
using Terraria.Audio;
using Terraria.DataStructures;
using Terraria.GameContent;
using Terraria.ID;
using Terraria.GameInput;
using Terraria.Localization;
using Terraria.ModLoader;
using Terraria.ModLoader.Engine;
using Terraria.ModLoader.UI;
using Terraria.Social;
using Terraria.UI.Chat;
using ReLogic.Content;
using Terraria.UI.Gamepad;

namespace Terraria;

Expand Down Expand Up @@ -165,6 +169,166 @@ public static void InfoDisplayPageHandler(int startX, ref string mouseText, out
spriteBatch.Draw(TextureAssets.InfoIcon[13].Value, buttonPosition - Vector2.One * 2f, null, OurFavoriteColor, 0f, default, 1f, SpriteEffects.None, 0f);
}
}

public static void BuilderTogglePageHandler(int startY, int activeToggles, out bool moveDownForButton, out int startIndex, out int endIndex) {
startIndex = 0;
endIndex = activeToggles;
moveDownForButton = false;
string text = "";

if (activeToggles > 12) {
startIndex = 12 * BuilderToggleLoader.BuilderTogglePage;

if (activeToggles - startIndex < 12)
endIndex = activeToggles;
else if (startIndex == 0)
endIndex = startIndex + 12;
else
endIndex = startIndex + 11;

Texture2D buttonTexture = UICommon.InfoDisplayPageArrowTexture.Value;
bool hover = false;
Vector2 buttonPosition = new Vector2(3, startY - 6f);

if (BuilderToggleLoader.BuilderTogglePage != 0) {
moveDownForButton = true;

spriteBatch.Draw(buttonTexture, buttonPosition + new Vector2(0, 13f), new Rectangle(0, 0, buttonTexture.Width, buttonTexture.Height), Color.White, -(float)Math.PI / 2, default, 1f, SpriteEffects.None, 0f);
if ((float)mouseX >= buttonPosition.X && (float)mouseY >= buttonPosition.Y && (float)mouseX <= buttonPosition.X + (float)buttonTexture.Width && (float)mouseY <= buttonPosition.Y + (float)buttonTexture.Height && !PlayerInput.IgnoreMouseInterface) {
hover = true;

player[myPlayer].mouseInterface = true;
text = "Previous Page";
mouseText = true;

if (mouseLeft && mouseLeftRelease) {
SoundEngine.PlaySound(SoundID.MenuTick);
mouseLeftRelease = false;

if (BuilderToggleLoader.BuilderTogglePage > 0)
BuilderToggleLoader.BuilderTogglePage--;
}

spriteBatch.Draw(TextureAssets.InfoIcon[13].Value, buttonPosition + new Vector2(0, 17) - Vector2.One * 2f, null, OurFavoriteColor, -(float)Math.PI / 2, default, 1f, SpriteEffects.None, 0f);
}
}

buttonPosition = new Vector2(3, startY + ((endIndex - startIndex) + (BuilderToggleLoader.BuilderTogglePage != 0).ToInt()) * 24f - 6f);

if (BuilderToggleLoader.BuilderTogglePage != activeToggles / 12) {
spriteBatch.Draw(buttonTexture, buttonPosition + new Vector2(0, 12f), new Rectangle(0, 0, buttonTexture.Width, buttonTexture.Height), Color.White, (float)Math.PI / 2, new Vector2(buttonTexture.Width, buttonTexture.Height), 1f, SpriteEffects.None, 0f);

if ((float)mouseX >= buttonPosition.X && (float)mouseY >= buttonPosition.Y && (float)mouseX <= buttonPosition.X + (float)buttonTexture.Width && (float)mouseY <= buttonPosition.Y + (float)buttonTexture.Height && !PlayerInput.IgnoreMouseInterface) {
hover = true;

player[myPlayer].mouseInterface = true;
text = "Next Page";
mouseText = true;

if (mouseLeft && mouseLeftRelease) {
SoundEngine.PlaySound(SoundID.MenuTick);
mouseLeftRelease = false;

if (BuilderToggleLoader.BuilderTogglePage < activeToggles / 12)
BuilderToggleLoader.BuilderTogglePage++;
}

spriteBatch.Draw(TextureAssets.InfoIcon[13].Value, buttonPosition + new Vector2(4, 12) - Vector2.One * 2f, null, OurFavoriteColor, (float)Math.PI / 2, new Vector2(buttonTexture.Width, buttonTexture.Height), 1f, SpriteEffects.None, 0f);
}
}

if (mouseText && hover) {
float colorByte = (float)mouseTextColor / 255f;
Color textColor = new Color(colorByte, colorByte, colorByte);

ChatManager.DrawColorCodedStringWithShadow(spriteBatch, FontAssets.MouseText.Value, text, new Vector2(mouseX + 14, mouseY + 14), textColor, 0f, Vector2.Zero, Vector2.One);
mouseText = false;
}
}
}

private void DrawBuilderAccToggles_Inner(Vector2 start)
{
Player player = Main.player[myPlayer];
int[] builderAccStatus = player.builderAccStatus;
List<BuilderToggle> activeToggles = BuilderToggleLoader.ActiveBuilderTogglesList();
bool shiftHotbarLock = activeToggles.Count / 12 != BuilderToggleLoader.BuilderTogglePage || activeToggles.Count % 12 >= 10;
Vector2 startPosition = start - new Vector2(0, shiftHotbarLock ? 42 : 21);
string text = "";

BuilderTogglePageHandler((int)startPosition.Y, activeToggles.Count, out bool moveDownForButton, out int startIndex, out int endIndex);
for (int i = startIndex; i < endIndex; i++) {
BuilderToggle builderToggle = activeToggles[i];

Texture2D texture = ModContent.Request<Texture2D>(builderToggle.Texture).Value;
Rectangle rectangle = new Rectangle(0, 0, texture.Width, texture.Height);
Color color = builderToggle.DisplayColorTexture();

Vector2 position = startPosition + new Vector2(0, moveDownForButton ? 24 : 0) + new Vector2(0, (i % 12) * 24);
text = builderToggle.DisplayValue();
int numberOfStates = builderToggle.NumberOfStates;
int toggleType = builderToggle.Type;

/*
BuilderToggleLoader.ModifyNumberOfStates(builderToggle, ref numberOfStates);
BuilderToggleLoader.ModifyDisplayValue(builderToggle, ref text);
*/

bool hover = Utils.CenteredRectangle(position, new Vector2(14f)).Contains(MouseScreen.ToPoint()) && !PlayerInput.IgnoreMouseInterface;
bool click = hover && mouseLeft && mouseLeftRelease;

if (toggleType == BuilderToggle.BlockSwap.Type || toggleType == BuilderToggle.TorchBiome.Type) {
if (toggleType == BuilderToggle.BlockSwap.Type)
rectangle = texture.Frame(3, 1, builderToggle.CurrentState != 0 ? 1 : 0);
else
rectangle = texture.Frame(4, 1, builderToggle.CurrentState == 0 ? 1 : 0);

position += new Vector2(1, 0);
}
else
rectangle = builderToggle.Type < 10 ? new Rectangle(builderToggle.Type * 16, 16, 14, 14) : rectangle;

/*
BuilderToggleLoader.ModifyDisplayColor(builderToggle, ref color);
BuilderToggleLoader.ModifyDisplayTexture(builderToggle, ref texture, ref rectangle);
*/

spriteBatch.Draw(texture, position, rectangle, color, 0f, rectangle.Size() / 2f, 1f, SpriteEffects.None, 0f);

if (hover) {
player.mouseInterface = true;
mouseText = true;

if (toggleType != BuilderToggle.BlockSwap.Type && toggleType != BuilderToggle.TorchBiome.Type) {
Asset<Texture2D> iconHover = ModContent.Request<Texture2D>(builderToggle.HoverTexture);
spriteBatch.Draw(iconHover.Value, position, null, OurFavoriteColor, 0f, iconHover.Value.Size() / 2f, 1f, SpriteEffects.None, 0f);
}
else if (toggleType == BuilderToggle.BlockSwap.Type)
spriteBatch.Draw(texture, position, texture.Frame(3, 1, 2), OurFavoriteColor, 0f, rectangle.Size() / 2f, 0.9f, SpriteEffects.None, 0f);
else if (toggleType == BuilderToggle.TorchBiome.Type)
spriteBatch.Draw(texture, position, texture.Frame(4, 1, builderToggle.CurrentState == 0 ? 3 : 2), OurFavoriteColor, 0f, rectangle.Size() / 2f, 0.9f, SpriteEffects.None, 0f);
}

if (click) {
builderAccStatus[toggleType] = (builderAccStatus[toggleType] + 1) % numberOfStates;
SoundEngine.PlaySound((toggleType == BuilderToggle.BlockSwap.Type || toggleType == BuilderToggle.TorchBiome.Type) ? SoundID.Unlock : SoundID.MenuTick);
mouseLeftRelease = false;
}


UILinkPointNavigator.SetPosition(6000 + i % 12, position + rectangle.Size() * 0.15f);

if (mouseText && hover && HoverItem.type <= 0) {
float colorByte = (float)mouseTextColor / 255f;
Color textColor = new Color(colorByte, colorByte, colorByte);

ChatManager.DrawColorCodedStringWithShadow(spriteBatch, FontAssets.MouseText.Value, text, new Vector2(mouseX + 14, mouseY + 14), textColor, 0f, Vector2.Zero, Vector2.One);
mouseText = false;
}
}

UILinkPointNavigator.Shortcuts.BUILDERACCCOUNT = (endIndex - startIndex);
}

//Mirrors code used in UpdateTime
/// <summary>
Expand Down
45 changes: 44 additions & 1 deletion patches/tModLoader/Terraria/Main.cs.patch
Original file line number Diff line number Diff line change
Expand Up @@ -4195,6 +4195,19 @@
UILinkPointNavigator.SetPosition(2503, vector2 + stringSize * 0.5f);
UILinkPointNavigator.Shortcuts.NPCCHAT_ButtonsRight2 = true;
}
@@ -32869,8 +_,12 @@
}
}

+ /*
GetBuilderAccsCountToShow(LocalPlayer, out var _, out var _, out var totalDrawnIcons);
bool pushSideToolsUp = totalDrawnIcons >= 10;
+ */
+ int activeToggles = BuilderToggleLoader.ActiveBuilderToggles();
+ bool pushSideToolsUp = activeToggles / 12 != BuilderToggleLoader.BuilderTogglePage || activeToggles % 12 >= 10;
if (!PlayerInput.UsingGamepad)
DrawHotbarLockIcon(num, num2, pushSideToolsUp);

@@ -33145,17 +_,25 @@
if (num34 == 147)
bannerMouseOver = true;
Expand Down Expand Up @@ -4457,7 +4470,20 @@

new Microsoft.Xna.Framework.Color(150, 150, 150, 150);
if (mouseX >= num && (float)mouseX <= (float)num + (float)TextureAssets.InventoryBack.Width() * inventoryScale && mouseY >= num2 && (float)mouseY <= (float)num2 + (float)TextureAssets.InventoryBack.Height() * inventoryScale && !PlayerInput.IgnoreMouseInterface) {
@@ -34533,17 +_,39 @@
@@ -34461,6 +_,8 @@
spriteBatch.Draw(value, new Vector2(pivotTopLeftX, num), value.Frame(2, 1, 1), OurFavoriteColor, 0f, default(Vector2), num2, SpriteEffects.None, 0f);
}

+ // These 2 methods are no longer necessary because they had hardcoded values and did nearly the same thing. Builder Toggles are now much more flexible and dynamic
+ /*
private void DrawBlockReplacementIcon(int pivotTopLeftX, int pivotTopLeftY, bool pushSideToolsUp, int gamepadPointOffset)
{
if (!playerInventory)
@@ -34530,20 +_,43 @@

UILinkPointNavigator.SetPosition(6000 + gamepadPointOffset, vector + rectangle.Size() * 0.65f);
}
+ */

public static void CraftItem(Recipe r)
{
Expand Down Expand Up @@ -5038,6 +5064,23 @@
if (mapStyle == 1 && mapEnabled)
Y += 261;
}
@@ -38513,6 +_,7 @@
if (false || Main.player[myPlayer].sign >= 0)
return;

+ /*
int num = 0;
Player player = Main.player[myPlayer];
int[] builderAccStatus = Main.player[myPlayer].builderAccStatus;
@@ -38700,6 +_,8 @@
}

UILinkPointNavigator.Shortcuts.BUILDERACCCOUNT = num;
+ */
+ DrawBuilderAccToggles_Inner(start);
}

private static void GetBuilderAccsCountToShow(Player plr, out int blockReplaceIcons, out int torchGodIcons, out int totalDrawnIcons)
@@ -39089,6 +_,11 @@
private static void TryDisposingEverything()
{
Expand Down
Loading

0 comments on commit 716495e

Please sign in to comment.