diff --git a/MapHelper.cs b/MapHelper.cs index 6b6fb9c..03ed387 100644 --- a/MapHelper.cs +++ b/MapHelper.cs @@ -1,8 +1,8 @@ -using Microsoft.Xna.Framework; -using System.Linq; +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,9 +19,21 @@ 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); + + // 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); - 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 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 } }