From b4760350d65766f5857d57e75ab26d4942f0b989 Mon Sep 17 00:00:00 2001 From: Rhoenicx Date: Fri, 24 Jul 2026 21:27:21 +0200 Subject: [PATCH 1/2] Added support for animated items to the map --- MapHelper.cs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/MapHelper.cs b/MapHelper.cs index 6b6fb9c..3bbf262 100644 --- a/MapHelper.cs +++ b/MapHelper.cs @@ -1,5 +1,5 @@ using Microsoft.Xna.Framework; -using System.Linq; +using Microsoft.Xna.Framework.Graphics; using Terraria; using Terraria.DataStructures; using Terraria.GameContent; @@ -20,8 +20,25 @@ public override void Draw(ref MapOverlayDrawContext context, ref string text) { continue; // do not draw items that are inacive or not whitelisted Main.instance.LoadItem(item.type); // Items SHOULD already be loaded, but in case it isn't have a backup + + SpriteFrame spriteFrame = new SpriteFrame(1, 1, 0, 0); + Texture2D itemTexture = TextureAssets.Item[item.type].Value; + + // Support for item animations. + if (ItemID.Sets.AnimatesAsSoul[item.type]) { + Rectangle itemFrame = Main.itemAnimations[item.type].GetFrame(itemTexture); + + byte columns = (byte)(itemTexture.Width / itemFrame.Width); + byte rows = (byte)(itemTexture.Height / itemFrame.Height); - if (context.Draw(TextureAssets.Item[item.type].Value, item.VisualPosition / 16, Color.White, new SpriteFrame(1, 1, 0, 0), 1f, 1.2f, Alignment.Center).IsMouseOver) + spriteFrame = new SpriteFrame( + columns, + rows, + (byte)(itemFrame.X / (itemTexture.Width / columns)), + (byte)(itemFrame.Y / (itemTexture.Height / rows))); + } + + if (context.Draw(itemTexture, item.VisualPosition / 16, Color.White, spriteFrame, 1f, 1.2f, Alignment.Center).IsMouseOver) text = item.HoverName; // Display the item's hover name when hovering over the icon } } From 49db8536e874ea2c0253644769edf7a2ced79ea6 Mon Sep 17 00:00:00 2001 From: Rhoenicx Date: Sat, 25 Jul 2026 14:24:13 +0200 Subject: [PATCH 2/2] Updated MapHelper ItemAnimation draw code Changed the draw code based on the suggestion of JavidPack: replaced ItemID.Sets.AnimatesAsSoul with Main.GetItemDrawFrame. Also added safeguards against dividing by zero. --- MapHelper.cs | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/MapHelper.cs b/MapHelper.cs index 3bbf262..03ed387 100644 --- a/MapHelper.cs +++ b/MapHelper.cs @@ -1,8 +1,8 @@ -using Microsoft.Xna.Framework; +using System; +using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Terraria; using Terraria.DataStructures; -using Terraria.GameContent; using Terraria.ID; using Terraria.Map; using Terraria.ModLoader; @@ -19,24 +19,19 @@ public override void Draw(ref MapOverlayDrawContext context, ref string text) { if (!IsWhitelistedItem(item.type)) continue; // do not draw items that are inacive or not whitelisted - Main.instance.LoadItem(item.type); // Items SHOULD already be loaded, but in case it isn't have a backup + // Using vanilla function to get the item frame and texture, also takes care of loading the texture. + Main.GetItemDrawFrame(item.type, out Texture2D itemTexture, out Rectangle itemFrame); - SpriteFrame spriteFrame = new SpriteFrame(1, 1, 0, 0); - Texture2D itemTexture = TextureAssets.Item[item.type].Value; - - // Support for item animations. - if (ItemID.Sets.AnimatesAsSoul[item.type]) { - Rectangle itemFrame = Main.itemAnimations[item.type].GetFrame(itemTexture); - - byte columns = (byte)(itemTexture.Width / itemFrame.Width); - byte rows = (byte)(itemTexture.Height / itemFrame.Height); + // Assuming all frames have an equal width and height, calculate the + // amount of columns and rows the spritesheet is supposed to have. + int columns = itemTexture.Width / Math.Max(1, itemFrame.Width); + int rows = itemTexture.Height / Math.Max(1, itemFrame.Height); - spriteFrame = new SpriteFrame( - columns, - rows, - (byte)(itemFrame.X / (itemTexture.Width / columns)), - (byte)(itemFrame.Y / (itemTexture.Height / rows))); - } + SpriteFrame spriteFrame = new SpriteFrame( + (byte)columns, + (byte)rows, + (byte)(itemFrame.X / Math.Max(1, itemTexture.Width / Math.Max(1, columns))), + (byte)(itemFrame.Y / Math.Max(1, itemTexture.Height / Math.Max(1, rows)))); if (context.Draw(itemTexture, item.VisualPosition / 16, Color.White, spriteFrame, 1f, 1.2f, Alignment.Center).IsMouseOver) text = item.HoverName; // Display the item's hover name when hovering over the icon