Skip to content

Commit

Permalink
Merge pull request #489 from drewhoener/master
Browse files Browse the repository at this point in the history
1.6.4 Patch PR
  • Loading branch information
drewhoener authored Apr 20, 2024
2 parents 3adad50 + 2bff42b commit 761826b
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 59 deletions.
76 changes: 76 additions & 0 deletions UIInfoSuite2/Infrastructure/SoundHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using System;
using System.IO;
using Microsoft.Xna.Framework.Audio;
using StardewModdingAPI;
using StardewValley;

namespace UIInfoSuite2.Infrastructure;

public enum Sounds
{
LevelUp
}

public class SoundHelper
{
private static readonly Lazy<SoundHelper> _instance = new(() => new SoundHelper());

private bool _initialized;

protected SoundHelper() { }

public static SoundHelper Instance => _instance.Value;

public void Initialize(IModHelper helper)
{
if (_initialized)
{
throw new InvalidOperationException("Cannot re-initialize sound helper");
}

RegisterSound(helper, Sounds.LevelUp, "LevelUp.wav");

_initialized = true;
}

private static string GetQualifiedSoundName(Sounds sound)
{
return $"UIInfoSuite.sounds.{sound.ToString()}";
}

private static void RegisterSound(
IModHelper helper,
Sounds sound,
string fileName,
int instanceLimit = -1,
CueDefinition.LimitBehavior? limitBehavior = null
)
{
CueDefinition newCueDefinition = new() { name = GetQualifiedSoundName(sound) };

if (instanceLimit > 0)
{
newCueDefinition.instanceLimit = instanceLimit;
newCueDefinition.limitBehavior = limitBehavior ?? CueDefinition.LimitBehavior.ReplaceOldest;
} else if (limitBehavior.HasValue)
{
newCueDefinition.limitBehavior = limitBehavior.Value;
}

SoundEffect audio;
string filePath = Path.Combine(helper.DirectoryPath, "assets", fileName);
using (var stream = new FileStream(filePath, FileMode.Open))
{
audio = SoundEffect.FromStream(stream);
}

newCueDefinition.SetSound(audio, Game1.audioEngine.GetCategoryIndex("Sound"));
Game1.soundBank.AddCue(newCueDefinition);
ModEntry.MonitorObject.Log($"Registered Sound: {newCueDefinition.name}");
}

public static void Play(Sounds sound)
{
Game1.playSound(GetQualifiedSoundName(sound));
}
}
2 changes: 2 additions & 0 deletions UIInfoSuite2/ModEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public override void Entry(IModHelper helper)
#region Generic mod config menu
private void OnGameLaunched(object sender, GameLaunchedEventArgs e)
{
SoundHelper.Instance.Initialize(Helper);

// get Generic Mod Config Menu's API (if it's installed)
ISemanticVersion? modVersion = Helper.ModRegistry.Get("spacechase0.GenericModConfigMenu")?.Manifest?.Version;
var minModVersion = "1.6.0";
Expand Down
1 change: 0 additions & 1 deletion UIInfoSuite2/Options/ModOptionsPageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ public ModOptionsPageHandler(IModHelper helper, ModOptions options, bool showPer
showWhenAnimalNeedsPet,
showCalendarAndBillboardOnGameMenuButton,
showCropAndBarrelTime,
experienceBar,
showItemHoverInformation,
showTravelingMerchant,
showRainyDayIcon,
Expand Down
57 changes: 6 additions & 51 deletions UIInfoSuite2/UIElements/ExperienceBar.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using StardewModdingAPI;
using StardewModdingAPI.Enums;
using StardewModdingAPI.Events;
Expand All @@ -17,7 +13,7 @@

namespace UIInfoSuite2.UIElements;

public partial class ExperienceBar : IDisposable
public partial class ExperienceBar
{
#region Properties
private readonly PerScreen<Item> _previousItem = new();
Expand Down Expand Up @@ -47,7 +43,8 @@ public partial class ExperienceBar : IDisposable
{ SkillType.Fishing, new Rectangle(20, 428, 10, 10) },
{ SkillType.Foraging, new Rectangle(60, 428, 10, 10) },
{ SkillType.Mining, new Rectangle(30, 428, 10, 10) },
{ SkillType.Combat, new Rectangle(120, 428, 10, 10) }
{ SkillType.Combat, new Rectangle(120, 428, 10, 10) },
{ SkillType.Luck, new Rectangle(50, 428, 10, 10) }
};

private static readonly Dictionary<SkillType, Color> ExperienceFillColor = new()
Expand All @@ -56,16 +53,15 @@ public partial class ExperienceBar : IDisposable
{ SkillType.Fishing, new Color(17, 84, 252, 0.63f) },
{ SkillType.Foraging, new Color(0, 234, 0, 0.63f) },
{ SkillType.Mining, new Color(145, 104, 63, 0.63f) },
{ SkillType.Combat, new Color(204, 0, 3, 0.63f) }
{ SkillType.Combat, new Color(204, 0, 3, 0.63f) },
{ SkillType.Luck, new Color(232, 223, 42, 0.63f) }
};

private readonly PerScreen<Rectangle> _experienceIconRectangle = new(() => SkillIconRectangles[SkillType.Farming]);

private readonly PerScreen<Rectangle> _levelUpIconRectangle = new(() => SkillIconRectangles[SkillType.Farming]);
private readonly PerScreen<Color> _experienceFillColor = new(() => ExperienceFillColor[SkillType.Farming]);

private SoundEffectInstance _soundEffect;

private bool ExperienceBarFadeoutEnabled { get; set; } = true;
private bool ExperienceGainTextEnabled { get; set; } = true;
private bool LevelUpAnimationEnabled { get; set; } = true;
Expand All @@ -80,36 +76,12 @@ public ExperienceBar(IModHelper helper)
{
_helper = helper;

InitializeSound();

if (_helper.ModRegistry.IsLoaded("DevinLematty.LevelExtender"))
{
_levelExtenderApi = _helper.ModRegistry.GetApi<ILevelExtender>("DevinLematty.LevelExtender");
}
}

private void InitializeSound()
{
var path = string.Empty;
try
{
path = Path.Combine(_helper.DirectoryPath, "assets", "LevelUp.wav");
_soundEffect = SoundEffect.FromStream(new FileStream(path, FileMode.Open)).CreateInstance();
}
catch (Exception ex)
{
ModEntry.MonitorObject.Log(
"Error loading sound file from " + path + ": " + ex.Message + Environment.NewLine + ex.StackTrace,
LogLevel.Error
);
}
}

public void Dispose()
{
_soundEffect.Dispose();
}

public void ToggleOption(
bool experienceBarEnabled,
bool experienceBarFadeoutEnabled,
Expand Down Expand Up @@ -189,7 +161,7 @@ private void OnLevelChanged(object sender, LevelChangedEventArgs e)

_experienceBarVisibleTimer.Value = ExperienceBarVisibleTicks;

PlayLevelUpSoundEffect();
SoundHelper.Play(Sounds.LevelUp);
}
}

Expand Down Expand Up @@ -298,23 +270,6 @@ private void InitializeExperiencePoints()
}
}

private void PlayLevelUpSoundEffect()
{
if (_soundEffect == null)
{
return;
}

_soundEffect.Volume = Game1.options.soundVolumeLevel;
Task.Factory.StartNew(
async () =>
{
await Task.Delay(200);
_soundEffect?.Play();
}
);
}

private bool TryGetCurrentLevelIndexFromSkillChange(out int currentLevelIndex)
{
currentLevelIndex = -1;
Expand Down
13 changes: 11 additions & 2 deletions UIInfoSuite2/UIElements/ShowItemEffectRanges.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
Expand All @@ -16,7 +17,7 @@ namespace UIInfoSuite2.UIElements;
internal class ShowItemEffectRanges : IDisposable
{
#region Properties
private readonly PerScreen<List<Point>> _effectiveArea = new(() => new List<Point>());
private readonly PerScreen<HashSet<Point>> _effectiveArea = new(() => new HashSet<Point>());

private readonly Mutex _mutex = new();

Expand Down Expand Up @@ -185,7 +186,15 @@ private void UpdateEffectiveArea()
* and now it's the tile that's being hovered over.
* That new behavior might not be intended and might get rolled back.
*/
AddTilesToHighlightedArea(currentItem.GetSprinklerTiles());

// Move tiles to 0, 0 and then offset by the correct tile.
IEnumerable<Vector2> unplacedSprinklerTiles = currentItem.GetSprinklerTiles();
if (currentItem.TileLocation != validTile)
{
unplacedSprinklerTiles = unplacedSprinklerTiles.Select(tile => tile - currentItem.TileLocation + validTile);
}

AddTilesToHighlightedArea(unplacedSprinklerTiles);

similarObjects = GetSimilarObjectsInLocation("sprinkler");
foreach (Object next in similarObjects)
Expand Down
3 changes: 2 additions & 1 deletion UIInfoSuite2/UIElements/ShowItemHoverInformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,8 @@ private void DrawAdvancedTooltip(SpriteBatch spriteBatch)
bool notShippedYet = hoveredObject != null &&
hoveredObject.countsForShippedCollection() &&
!Game1.player.basicShipped.ContainsKey(hoveredObject.ItemId) &&
hoveredObject.Type != "Fish";
hoveredObject.Type != "Fish" &&
hoveredObject.Category != Object.skillBooksCategory;
if (notShippedYet &&
hoveredObject != null &&
ModEntry.DGA.IsCustomObject(hoveredObject, out DynamicGameAssetsHelper? dgaHelper))
Expand Down
6 changes: 5 additions & 1 deletion UIInfoSuite2/UIElements/ShowRainyDayIcon.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using StardewModdingAPI;
Expand Down Expand Up @@ -148,17 +149,20 @@ private void CreateTileSheet()
// 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];
Texture2D weatherBorderTexture = Texture2D.FromFile(Game1.graphics.GraphicsDevice, Path.Combine(_helper.DirectoryPath, "assets", "weatherbox.png"));
var weatherBorderColors = new Color[15 * 15];
var cursorColors = new Color[Game1.mouseCursors.Width * Game1.mouseCursors.Height];
var cursorColors_1_6 = new Color[Game1.mouseCursors_1_6.Width * Game1.mouseCursors_1_6.Height];
var bounds = new Rectangle(0, 0, Game1.mouseCursors.Width, Game1.mouseCursors.Height);
var bounds_1_6 = new Rectangle(0, 0, Game1.mouseCursors_1_6.Width, Game1.mouseCursors_1_6.Height);
weatherBorderTexture.GetData(weatherBorderColors);
Game1.mouseCursors.GetData(cursorColors);
Game1.mouseCursors_1_6.GetData(cursorColors_1_6);
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));
Tools.GetSubTexture(subTextureColors, weatherBorderColors, new Rectangle(0, 0, 15, 15), new Rectangle(0, 0, 15, 15));
// Copy to each destination
for (var i = 0; i < 4; i++)
{
Expand Down
2 changes: 1 addition & 1 deletion UIInfoSuite2/UIInfoSuite2.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Version>2.3.1</Version>
<Version>2.3.2</Version>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ModZipPath>$(SolutionDir)\Releases</ModZipPath>
Expand Down
Binary file added UIInfoSuite2/assets/weatherbox.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions UIInfoSuite2/manifest.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"Name": "UI Info Suite 2",
"Author": "Annosz",
"Version": "2.3.1",
"Version": "2.3.2",
"Description": "Adds a useful information to the user interface. Based on Cdaragorn's excellent UI Info Suite.",
"UniqueID": "Annosz.UiInfoSuite2",
"EntryDll": "UIInfoSuite2.dll",
"MinimumApiVersion": "4.0.0",
"MinimumApiVersion": "4.0.5",
"UpdateKeys": [
"GitHub:Annosz/UIInfoSuite2"
]
Expand Down

0 comments on commit 761826b

Please sign in to comment.