Skip to content

Commit

Permalink
Fix icons rendering over menu and refactor structure
Browse files Browse the repository at this point in the history
  • Loading branch information
JLannoo committed Apr 24, 2024
1 parent d76a692 commit 00349dd
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 36 deletions.
2 changes: 1 addition & 1 deletion HaveIDonated.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.9.34723.18
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HaveIDonated", "HaveIDonated\HaveIDonated.csproj", "{A436E70A-0F73-4731-A300-851751930A8C}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HaveIDonated", "HaveIDonated\HaveIDonated.csproj", "{A436E70A-0F73-4731-A300-851751930A8C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
5 changes: 5 additions & 0 deletions HaveIDonated/HaveIDonated.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<EnableHarmony>true</EnableHarmony>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Pathoschild.Stardew.ModBuildConfig" Version="4.1.1" />
</ItemGroup>

<ItemGroup>
<Folder Include="Components\" />
</ItemGroup>

</Project>
17 changes: 14 additions & 3 deletions HaveIDonated/ModEntry.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
using StardewModdingAPI;
using HarmonyLib;
using HaveIDonated.Models;
using HaveIDonated.Patches;
using StardewModdingAPI;
using StardewModdingAPI.Events;

namespace HaveIDonated;

public class ModEntry: Mod {
private IModHelper _helper;

private Hover hover;
private InventoryIcons inventoryIcons;
public static Hover hover;
public static InventoryIcons inventoryIcons;

public static IMonitor MonitorObject;
public static Harmony HarmonyObject;

/// <summary>
/// Executed when mod is first loaded
Expand All @@ -18,6 +22,8 @@ public override void Entry(IModHelper helper) {
MonitorObject = Monitor;
_helper = helper;

InitializeHarmony();

helper.Events.GameLoop.DayStarted += OnDayStarted;
helper.Events.Player.InventoryChanged += OnInventoryChanged;
}
Expand All @@ -43,5 +49,10 @@ private void RestartModFunctions() {
hover = new Hover(_helper, bundleData);

}

private void InitializeHarmony() {
HarmonyObject = new Harmony(ModManifest.UniqueID);
InventoryPatches.Initialize(Monitor, HarmonyObject);
}
#endregion
}
33 changes: 22 additions & 11 deletions HaveIDonated/BundleData.cs → HaveIDonated/Models/BundleData.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using StardewValley.Locations;
using StardewValley;

namespace HaveIDonated;
namespace HaveIDonated.Models;

public class BundleData {
public class BundleData
{
public string roomName;
public string name;
public int bundleId;
Expand All @@ -17,7 +18,8 @@ public class BundleData {
public bool completed = false;
public string displayName;

public BundleData(string roomName, string name, int bundleId, BundleReward? reward, string? translatedName, List<Item> requiredItems, int requiredQuantity, int bundleColor) {
public BundleData(string roomName, string name, int bundleId, BundleReward? reward, string? translatedName, List<Item> requiredItems, int requiredQuantity, int bundleColor)
{
this.roomName = roomName;
this.name = name;
this.bundleId = bundleId;
Expand All @@ -27,35 +29,44 @@ public BundleData(string roomName, string name, int bundleId, BundleReward? rewa
this.requiredQuantity = requiredQuantity;
this.bundleColor = bundleColor;

if (Game1.getLocationFromName("CommunityCenter") is CommunityCenter cCenter) {
for (int i = 0; i < requiredItems.Count; i++) {
if (!cCenter.bundles[bundleId][i]) {
if (Game1.getLocationFromName("CommunityCenter") is CommunityCenter cCenter)
{
for (int i = 0; i < requiredItems.Count; i++)
{
if (!cCenter.bundles[bundleId][i])
{
missingItems.Add(requiredItems[i]);
}
}
}

if (requiredItems.Count - missingItems.Count > requiredQuantity) {
if (requiredItems.Count - missingItems.Count >= requiredQuantity)
{
completed = true;
}

displayName = translatedName ?? name;
}
}

public class BundleReward {
public class BundleReward
{
public int quantity;
public Item item;

public BundleReward(string data) {
public BundleReward(string data)
{
string[] arr = data.Split(' ');

string type = arr[0];
string id = arr[1];
bool parsed = int.TryParse(arr[2], out quantity);
if (parsed) {
if (parsed)
{
item = ItemRegistry.Create(id, quantity);
} else {
}
else
{
throw new Exception("Could not parse Bundle Reward Quantity");
}
}
Expand Down
1 change: 1 addition & 0 deletions HaveIDonated/Hover.cs → HaveIDonated/Models/Hover.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using HaveIDonated.Models;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using Force.DeepCloner;
using HaveIDonated.Models;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using StardewModdingAPI;
using StardewModdingAPI.Utilities;
using StardewValley;
using StardewValley.ItemTypeDefinitions;
using StardewValley.Menus;
Expand All @@ -12,30 +14,39 @@ public class InventoryIcons : IDisposable {
private readonly IModHelper _helper;
private readonly List<BundleData> _bundles;

private readonly PerScreen<List<(Item, ClickableComponent)>> itemsDrawn = new();
private readonly PerScreen<List<Bundle>> bundlesDrawn = new();

public InventoryIcons(IModHelper helper, List<BundleData> bundleData) {
_helper = helper;
_bundles = bundleData;

_helper.Events.Display.RenderedActiveMenu += onRenderedMenu;
GetBeingDrawn();

_helper.Events.Display.RenderedActiveMenu += OnRenderedActiveMenu;
}

#region Events
private void onRenderedMenu(object? sender, StardewModdingAPI.Events.RenderedActiveMenuEventArgs e) {
Draw(Game1.spriteBatch);
private void OnRenderedActiveMenu(object? sender, StardewModdingAPI.Events.RenderedActiveMenuEventArgs e) {
GetBeingDrawn();
}

public void Dispose() {
_helper.Events.Display.RenderedActiveMenu -= onRenderedMenu;
_helper.Events.Display.RenderedActiveMenu -= OnRenderedActiveMenu;
GC.SuppressFinalize(this);
}
#endregion

#region Methods
private void Draw(SpriteBatch spriteBatch) {
var items = GetItemsBeingDrawn();
var noteBundles = GetBundlesBeingDraw();
public void GetBeingDrawn() {
itemsDrawn.Value = GetItemsBeingDrawn();
bundlesDrawn.Value = GetBundlesBeingDrawn();
}

public void Draw(SpriteBatch spriteBatch) {
if (itemsDrawn.Value == null || bundlesDrawn.Value == null) return;

foreach(var item in items) {
foreach(var item in itemsDrawn.Value) {
var (bundlesDonatable, donatableToMuseum) = Utils.IsItemDonatable(item.Item1, _bundles);

if(bundlesDonatable.Count > 0) {
Expand All @@ -53,7 +64,7 @@ private void Draw(SpriteBatch spriteBatch) {
Vector2.Zero,
scale,
SpriteEffects.None,
0
1
);
}
}
Expand All @@ -74,13 +85,13 @@ private void Draw(SpriteBatch spriteBatch) {
Vector2.Zero,
scale,
SpriteEffects.None,
0
1
);
}
}
}

foreach(var noteBundle in noteBundles) {
foreach(var noteBundle in bundlesDrawn.Value) {
string[] bundleItemIDs = noteBundle.ingredients.Select(item => item.id).ToArray();
ParsedItemData[] itemsInBundle = bundleItemIDs.Select(id => ItemRegistry.GetData(id)).ToArray();

Expand All @@ -107,11 +118,6 @@ private void Draw(SpriteBatch spriteBatch) {
}
}
}

if(Game1.activeClickableMenu is IClickableMenu menu) {
menu.drawMouse(spriteBatch);

}
}

private List<(Item, ClickableComponent)> GetItemsBeingDrawn() {
Expand Down Expand Up @@ -207,7 +213,7 @@ private void Draw(SpriteBatch spriteBatch) {
return items;
}

private static List<Bundle> GetBundlesBeingDraw() {
private static List<Bundle> GetBundlesBeingDrawn() {
List<Bundle> items = new();

if(Game1.activeClickableMenu is JunimoNoteMenu menu && !menu.specificBundlePage) {
Expand Down
40 changes: 40 additions & 0 deletions HaveIDonated/Patches/InventoryPatches.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using HarmonyLib;
using Microsoft.Xna.Framework.Graphics;
using StardewModdingAPI;
using StardewValley.Menus;

namespace HaveIDonated.Patches;

internal class InventoryPatches {
private static IMonitor monitor;

internal static void Initialize(IMonitor Monitor, Harmony harmony) {
monitor = Monitor;

harmony.Patch(
original: AccessTools.Method(
typeof(InventoryMenu),
nameof(InventoryMenu.draw),
new Type[] { typeof(SpriteBatch) }
),
postfix: new HarmonyMethod(typeof(InventoryPatches), nameof(InventoryDraw_postfix))
);

harmony.Patch(
original: AccessTools.Method(
typeof(Bundle),
nameof(Bundle.draw),
new Type[] { typeof(SpriteBatch) }
),
postfix: new HarmonyMethod(typeof(InventoryPatches), nameof(InventoryDraw_postfix))
);
}

internal static void InventoryDraw_postfix(SpriteBatch b) {
try {
ModEntry.inventoryIcons?.Draw(b);
} catch (Exception ex) {
monitor.Log($"Failed drawing HaveIDonated.InventoryIcons {nameof(InventoryDraw_postfix)}\n{ex}", LogLevel.Error);
}
}
}
6 changes: 2 additions & 4 deletions HaveIDonated/Utils.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.Xna.Framework;
using HaveIDonated.Models;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using StardewValley;
using StardewValley.Locations;
Expand Down Expand Up @@ -248,9 +249,6 @@ public static (List<BundleData>, bool) IsItemDonatable(Item item, List<BundleDat
/// </summary>
public static float Oscillate(float periodInMS, float amplitude) {
float result = (float)Math.Sin((2*Math.PI / (periodInMS/1000)) * (Game1.ticks / 60f)) * amplitude;
if (result == amplitude) {
ModEntry.MonitorObject.Log("Tick");
}
return result;
}
}
8 changes: 8 additions & 0 deletions MenuPatches.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System;

public class Class1
{
public Class1()
{
}
}

1 comment on commit 00349dd

@JLannoo
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixes issue #1

Please sign in to comment.