Skip to content

Commit

Permalink
fix error showing lookup with broken textures
Browse files Browse the repository at this point in the history
  • Loading branch information
Pathoschild committed Apr 4, 2024
1 parent 34038e0 commit fd7f6bc
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 9 deletions.
37 changes: 33 additions & 4 deletions Common/DrawHelper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using StardewValley;

namespace Pathoschild.Stardew.Common
{
Expand Down Expand Up @@ -29,11 +30,32 @@ public static float GetSpaceWidth(SpriteFont font)
/// <param name="sprite">The sprite coordinates and dimensions in the sprite sheet.</param>
/// <param name="x">The X-position at which to draw the sprite.</param>
/// <param name="y">The X-position at which to draw the sprite.</param>
/// <param name="errorSize">The size of the error icon to draw if the sprite is invalid.</param>
/// <param name="color">The color to tint the sprite.</param>
/// <param name="scale">The scale to draw.</param>
public static void DrawSprite(this SpriteBatch spriteBatch, Texture2D sheet, Rectangle sprite, float x, float y, Color? color = null, float scale = 1)
public static void DrawSprite(this SpriteBatch spriteBatch, Texture2D sheet, Rectangle sprite, float x, float y, Vector2 errorSize, Color? color = null, float scale = 1)
{
spriteBatch.Draw(sheet, new Vector2(x, y), sprite, color ?? Color.White, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
try
{
spriteBatch.Draw(sheet, new Vector2(x, y), sprite, color ?? Color.White, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
}
catch
{
Utility.DrawErrorTexture(spriteBatch, new Rectangle((int)x, (int)y, (int)errorSize.X, (int)errorSize.Y), 0);
}
}

/// <inheritdoc cref="DrawSprite(SpriteBatch, Texture2D, Rectangle, float, float, Vector2, Color?, float)"/>
public static void DrawSprite(this SpriteBatch spriteBatch, Texture2D sheet, Rectangle sprite, float x, float y, Point errorSize, Color? color = null, float scale = 1)
{
try
{
spriteBatch.Draw(sheet, new Vector2(x, y), sprite, color ?? Color.White, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
}
catch
{
Utility.DrawErrorTexture(spriteBatch, new Rectangle((int)x, (int)y, errorSize.X, errorSize.Y), 0);
}
}

/// <summary>Draw a sprite to the screen scaled and centered to fit the given dimensions.</summary>
Expand All @@ -45,7 +67,14 @@ public static void DrawSprite(this SpriteBatch spriteBatch, Texture2D sheet, Rec
/// <param name="color">The color to tint the sprite.</param>
public static void DrawSpriteWithin(this SpriteBatch spriteBatch, SpriteInfo? sprite, float x, float y, Vector2 size, Color? color = null)
{
sprite?.Draw(spriteBatch, (int)x, (int)y, size, color);
try
{
sprite?.Draw(spriteBatch, (int)x, (int)y, size, color);
}
catch
{
Utility.DrawErrorTexture(spriteBatch, new Rectangle((int)x, (int)y, (int)size.X, (int)size.Y), 0);
}
}

/// <summary>Draw a sprite to the screen scaled and centered to fit the given dimensions.</summary>
Expand All @@ -65,7 +94,7 @@ public static void DrawSpriteWithin(this SpriteBatch spriteBatch, Texture2D shee
float topOffset = Math.Max((size.Y - (sprite.Height * scale)) / 2, 0);

// draw
spriteBatch.DrawSprite(sheet, sprite, x + leftOffset, y + topOffset, color ?? Color.White, scale);
spriteBatch.DrawSprite(sheet, sprite, x + leftOffset, y + topOffset, size, color ?? Color.White, scale);
}

/// <summary>Draw a sprite to the screen.</summary>
Expand Down
2 changes: 1 addition & 1 deletion LookupAnything/Components/LookupMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ public override void draw(SpriteBatch spriteBatch)
: this.height / (float)Sprites.Letter.Sprite.Height;

backgroundBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied, SamplerState.PointClamp);
backgroundBatch.DrawSprite(Sprites.Letter.Sheet, Sprites.Letter.Sprite, x, y, scale: scale);
backgroundBatch.DrawSprite(Sprites.Letter.Sheet, Sprites.Letter.Sprite, x, y, Sprites.Letter.Sprite.Size, scale: scale);
backgroundBatch.End();
}

Expand Down
2 changes: 1 addition & 1 deletion LookupAnything/Components/SearchMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public override void draw(SpriteBatch spriteBatch)
using (SpriteBatch backgroundBatch = new SpriteBatch(Game1.graphics.GraphicsDevice))
{
backgroundBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied, SamplerState.PointClamp);
backgroundBatch.DrawSprite(Sprites.Letter.Sheet, Sprites.Letter.Sprite, x, y, scale: this.width / (float)Sprites.Letter.Sprite.Width);
backgroundBatch.DrawSprite(Sprites.Letter.Sheet, Sprites.Letter.Sprite, x, y, Sprites.Letter.Sprite.Size, scale: this.width / (float)Sprites.Letter.Sprite.Width);
backgroundBatch.End();
}

Expand Down
4 changes: 2 additions & 2 deletions LookupAnything/Framework/Fields/CharacterFriendshipField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public CharacterFriendshipField(string label, FriendshipModel friendship)
}

// draw
spriteBatch.DrawSprite(CommonSprites.Icons.Sheet, icon, position.X + leftOffset, position.Y, color, Game1.pixelZoom);
spriteBatch.DrawSprite(CommonSprites.Icons.Sheet, icon, position.X + leftOffset, position.Y, CommonSprites.Icons.FilledHeart.Size, color, Game1.pixelZoom);
leftOffset += CommonSprites.Icons.FilledHeart.Width * Game1.pixelZoom;
}

Expand All @@ -80,7 +80,7 @@ public CharacterFriendshipField(string label, FriendshipModel friendship)
{
leftOffset += 1;
float zoom = (CommonSprites.Icons.EmptyHeart.Height / (CommonSprites.Icons.Stardrop.Height * 1f)) * Game1.pixelZoom;
spriteBatch.DrawSprite(CommonSprites.Icons.Sheet, CommonSprites.Icons.Stardrop, position.X + leftOffset, position.Y, Color.White * 0.25f, zoom);
spriteBatch.DrawSprite(CommonSprites.Icons.Sheet, CommonSprites.Icons.Stardrop, position.X + leftOffset, position.Y, CommonSprites.Icons.Stardrop.Size, Color.White * 0.25f, zoom);
leftOffset += CommonSprites.Icons.Stardrop.Width * zoom;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public override bool DrawPortrait(SpriteBatch spriteBatch, Vector2 position, Vec
// use character portrait (most villager NPCs)
if (npc.IsVillager && npc.Portrait != null && !this.IsGourmand) // Gourmand uses Professor Snail's portraits
{
spriteBatch.DrawSprite(npc.Portrait, new Rectangle(0, 0, NPC.portrait_width, NPC.portrait_height), position.X, position.Y, Color.White, size.X / NPC.portrait_width);
spriteBatch.DrawSprite(npc.Portrait, new Rectangle(0, 0, NPC.portrait_width, NPC.portrait_height), position.X, position.Y, new Point(NPC.portrait_width, NPC.portrait_height), Color.White, size.X / NPC.portrait_width);
return true;
}

Expand Down
1 change: 1 addition & 0 deletions LookupAnything/docs/release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
## Upcoming release
* Fixed upgrade level check for barns and coops.
* Fixed error if a target has an invalid texture.
* Fixed error showing lookup when a mod adds broken textures.

## 1.41.3
Released 28 March 2024 for SMAPI 4.0.0 or later.
Expand Down

0 comments on commit fd7f6bc

Please sign in to comment.