Skip to content

Commit

Permalink
Merge pull request #32 from drewhoener/bugfix-cloud-sprites
Browse files Browse the repository at this point in the history
Better weather icons
  • Loading branch information
Annosz authored Jan 20, 2021
2 parents 5a8d00a + 8c21512 commit 2289b54
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 9 deletions.
35 changes: 35 additions & 0 deletions UIInfoSuite2/Infrastucture/Tools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,40 @@ public static Item GetHoveredItem()

return hoverItem;
}

public static void GetSubTexture(Color[] output, Color[] originalColors, Rectangle sourceBounds, Rectangle clipArea)
{
if (output.Length < clipArea.Width * clipArea.Height)
{
return;
}

var dest = 0;
for (var yOffset = 0; yOffset < clipArea.Height; yOffset++)
{
for (var xOffset = 0; xOffset < clipArea.Width; xOffset++)
{
var idx = (clipArea.X + xOffset) + (sourceBounds.Width * (yOffset + clipArea.Y));
output[dest++] = originalColors[idx];
}
}

}

public static void SetSubTexture(Color[] sourceColors, Color[] destColors, int destWidth, Rectangle destBounds)
{
if(sourceColors.Length > destColors.Length || (destBounds.Width * destBounds.Height) > destColors.Length) {
return;
}
var srcIdx = 0;
for (var yOffset = 0; yOffset < destBounds.Height; yOffset++)
{
for (var xOffset = 0; xOffset < destBounds.Width; xOffset++)
{
var idx = (destBounds.X + xOffset) + (destWidth * (yOffset + destBounds.Y));
destColors[idx] = sourceColors[srcIdx++];
}
}
}
}
}
66 changes: 57 additions & 9 deletions UIInfoSuite2/UIElements/ShowRainyDayIcon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
using StardewValley;
using StardewValley.Menus;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework.Graphics;
using UIInfoSuite.Infrastructure;
using UIInfoSuite.Infrastructure.Extensions;

Expand All @@ -16,23 +13,31 @@ namespace UIInfoSuite.UIElements
class ShowRainyDayIcon : IDisposable
{
#region Properties

private bool _IsNextDayRainy;
Rectangle? _weatherIconSpriteLocation;
private string _hoverText;
private ClickableTextureComponent _rainyDayIcon;
private Texture2D _iconSheet;

private Color[] _weatherIconColors = null;
private const int WeatherSheetWidth = 45;
private const int WeatherSheetHeight = 15;

private readonly IModHelper _helper;
#endregion

#region Lifecycle
public ShowRainyDayIcon(IModHelper helper)
{
_helper = helper;
CreateTileSheet();
}

public void Dispose()
{
ToggleOption(false);
_iconSheet.Dispose();
}

public void ToggleOption(bool showTravelingMerchant)
Expand Down Expand Up @@ -60,9 +65,9 @@ private void OnRenderingHud(object sender, RenderingHudEventArgs e)
_rainyDayIcon =
new ClickableTextureComponent(
new Rectangle(iconPosition.X, iconPosition.Y, 40, 40),
Game1.animations,
_iconSheet,
_weatherIconSpriteLocation.Value,
2f);
8 / 3f);
_rainyDayIcon.draw(Game1.spriteBatch);
}
}
Expand All @@ -82,8 +87,51 @@ private void OnRenderedHud(object sender, RenderedHudEventArgs e)
#endregion

#region Logic

/// <summary>
/// Creates a custom tilesheet for weather icons.
/// Meant to mimic the TV screen, which has a border around it, while the individual icons in the Cursors tilesheet don't have a border
/// Extracts the border, and each individual weather icon and stitches them together into one separate sheet
///</summary>
private void CreateTileSheet()
{

ModEntry.MonitorObject.Log("Setting up icon sheet", LogLevel.Info);
// Setup Texture sheet as a copy, so as not to disturb existing sprites
_iconSheet = new Texture2D(Game1.graphics.GraphicsDevice , WeatherSheetWidth, WeatherSheetHeight);
_weatherIconColors = new Color[WeatherSheetWidth * WeatherSheetHeight];
var cursorColors = new Color[Game1.mouseCursors.Width * Game1.mouseCursors.Height];
var bounds = new Rectangle(0, 0, Game1.mouseCursors.Width, Game1.mouseCursors.Height);
Game1.mouseCursors.GetData(cursorColors);
var subTextureColors = new Color[15 * 15];

// Copy over the bits we want
// Border from TV screen
Tools.GetSubTexture(subTextureColors, cursorColors, bounds, new Rectangle(499, 307, 15, 15));
// Copy to each destination
Tools.SetSubTexture(subTextureColors, _weatherIconColors, WeatherSheetWidth, new Rectangle(0, 0, 15, 15));
Tools.SetSubTexture(subTextureColors, _weatherIconColors, WeatherSheetWidth, new Rectangle(15, 0, 15, 15));
Tools.SetSubTexture(subTextureColors, _weatherIconColors, WeatherSheetWidth, new Rectangle(30, 0, 15, 15));

subTextureColors = new Color[13 * 13];
// Rainy Weather
Tools.GetSubTexture(subTextureColors, cursorColors, bounds, new Rectangle(504, 333, 13, 13));
Tools.SetSubTexture(subTextureColors, _weatherIconColors, WeatherSheetWidth, new Rectangle(1, 1, 13, 13));

// Stormy Weather
Tools.GetSubTexture(subTextureColors, cursorColors, bounds, new Rectangle(426, 346, 13, 13));
Tools.SetSubTexture(subTextureColors, _weatherIconColors, WeatherSheetWidth, new Rectangle(16, 1, 13, 13));

// Snowy Weather
Tools.GetSubTexture(subTextureColors, cursorColors, bounds, new Rectangle(465, 346, 13, 13));
Tools.SetSubTexture(subTextureColors, _weatherIconColors, WeatherSheetWidth, new Rectangle(31, 1, 13, 13));

_iconSheet.SetData(_weatherIconColors);
}

private void GetWeatherIconSpriteLocation()
{

switch (Game1.weatherForTomorrow)
{
case Game1.weather_sunny:
Expand All @@ -95,19 +143,19 @@ private void GetWeatherIconSpriteLocation()

case Game1.weather_rain:
_IsNextDayRainy = true;
_weatherIconSpriteLocation = new Rectangle(268, 1750, 20, 20);
_weatherIconSpriteLocation = new Rectangle(0, 0, 15, 15);
_hoverText = _helper.SafeGetString(LanguageKeys.RainNextDay);
break;

case Game1.weather_lightning:
_IsNextDayRainy = true;
_weatherIconSpriteLocation = new Rectangle(272, 1641, 20, 20);
_weatherIconSpriteLocation = new Rectangle(15, 0, 15, 15);
_hoverText = _helper.SafeGetString(LanguageKeys.ThunderstormNextDay);
break;

case Game1.weather_snow:
_IsNextDayRainy = true;
_weatherIconSpriteLocation = new Rectangle(260, 680, 20, 20);
_weatherIconSpriteLocation = new Rectangle(30, 0, 15, 15);
_hoverText = _helper.SafeGetString(LanguageKeys.SnowNextDay);
break;

Expand Down

0 comments on commit 2289b54

Please sign in to comment.