forked from LeFauxMatt/StardewMods
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TooManyAnimals.cs
284 lines (245 loc) · 9.67 KB
/
TooManyAnimals.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
namespace StardewMods.TooManyAnimals;
using System;
using System.Collections.Generic;
using System.Linq;
using HarmonyLib;
using StardewModdingAPI;
using StardewModdingAPI.Events;
using StardewModdingAPI.Utilities;
using StardewMods.Common.Helpers;
using StardewMods.Common.Integrations.GenericModConfigMenu;
using StardewMods.CommonHarmony.Helpers;
using StardewValley;
using StardewValley.Menus;
using SObject = StardewValley.Object;
/// <inheritdoc />
public class TooManyAnimals : Mod
{
private readonly PerScreen<int> _currentPage = new();
private readonly PerScreen<ClickableTextureComponent?> _nextPage = new();
private readonly PerScreen<ClickableTextureComponent?> _previousPage = new();
private ModConfig? _config;
private static TooManyAnimals? Instance { get; set; }
private ModConfig Config
{
get
{
if (this._config is not null)
{
return this._config;
}
ModConfig? config = null;
try
{
config = this.Helper.ReadConfig<ModConfig>();
}
catch (Exception)
{
// ignored
}
this._config = config ?? new ModConfig();
Log.Trace(this._config.ToString());
return this._config;
}
}
private int CurrentPage
{
get => this._currentPage.Value;
set
{
if (this._currentPage.Value == value)
{
return;
}
this._currentPage.Value = value;
Game1.activeClickableMenu = new PurchaseAnimalsMenu(this.Stock);
}
}
private ClickableTextureComponent NextPage
{
get => this._nextPage.Value ??= new(
new(0, 0, 12 * Game1.pixelZoom, 11 * Game1.pixelZoom),
Game1.mouseCursors,
new(365, 495, 12, 11),
Game1.pixelZoom)
{
myID = 69420,
};
}
private ClickableTextureComponent PreviousPage
{
get => this._previousPage.Value ??= new(
new(0, 0, 12 * Game1.pixelZoom, 11 * Game1.pixelZoom),
Game1.mouseCursors,
new(352, 495, 12, 11),
Game1.pixelZoom)
{
myID = 69421,
};
}
private List<SObject>? Stock { get; set; }
/// <inheritdoc />
public override void Entry(IModHelper helper)
{
TooManyAnimals.Instance = this;
Log.Monitor = this.Monitor;
I18n.Init(this.Helper.Translation);
if (this.Helper.ModRegistry.IsLoaded("furyx639.FuryCore"))
{
Log.Alert("Remove FuryCore, it is no longer needed by this mod!");
}
// Patches
HarmonyHelper.AddPatch(
this.ModManifest.UniqueID,
AccessTools.Constructor(typeof(PurchaseAnimalsMenu), new[] { typeof(List<SObject>) }),
typeof(TooManyAnimals),
nameof(TooManyAnimals.PurchaseAnimalsMenu_constructor_prefix));
HarmonyHelper.ApplyPatches(this.ModManifest.UniqueID);
// Events
this.Helper.Events.Display.MenuChanged += this.OnMenuChanged;
this.Helper.Events.Display.RenderedActiveMenu += this.OnRenderedActiveMenu;
this.Helper.Events.GameLoop.GameLaunched += this.OnGameLaunched;
this.Helper.Events.Input.ButtonsChanged += this.OnButtonsChanged;
this.Helper.Events.Input.ButtonPressed += this.OnButtonPressed;
}
private static void PurchaseAnimalsMenu_constructor_prefix(ref List<SObject> stock)
{
// Get actual stock
TooManyAnimals.Instance!.Stock ??= stock;
// Limit stock
stock = TooManyAnimals.Instance.Stock
.Skip(TooManyAnimals.Instance.CurrentPage * TooManyAnimals.Instance.Config.AnimalShopLimit)
.Take(TooManyAnimals.Instance.Config.AnimalShopLimit)
.ToList();
}
private void OnButtonPressed(object? sender, ButtonPressedEventArgs e)
{
if (Game1.activeClickableMenu is not PurchaseAnimalsMenu || this.Stock is null || this.Stock.Count <= this.Config.AnimalShopLimit)
{
return;
}
if (e.Button is not SButton.MouseLeft or SButton.MouseRight && !(e.Button.IsActionButton() || e.Button.IsUseToolButton()))
{
return;
}
var (x, y) = Game1.getMousePosition(true);
if (this.NextPage.containsPoint(x, y) && (this.CurrentPage + 1) * this.Config.AnimalShopLimit < this.Stock.Count)
{
this.CurrentPage++;
}
if (this.PreviousPage.containsPoint(x, y) && this.CurrentPage > 0)
{
this.CurrentPage--;
}
}
private void OnButtonsChanged(object? sender, ButtonsChangedEventArgs e)
{
if (Game1.activeClickableMenu is not PurchaseAnimalsMenu || this.Stock is null || this.Stock.Count <= this.Config.AnimalShopLimit)
{
return;
}
if (this.Config.ControlScheme.NextPage.JustPressed() && (this.CurrentPage + 1) * this.Config.AnimalShopLimit < this.Stock.Count)
{
this.CurrentPage++;
return;
}
if (this.Config.ControlScheme.PreviousPage.JustPressed() && this.CurrentPage > 0)
{
this.CurrentPage--;
}
}
private void OnGameLaunched(object? sender, GameLaunchedEventArgs e)
{
var gmcm = new GenericModConfigMenuIntegration(this.Helper.ModRegistry);
if (!gmcm.IsLoaded)
{
return;
}
// Register mod configuration
gmcm.Register(
this.ModManifest,
() => this._config = new(),
() => this.Helper.WriteConfig(this.Config));
gmcm.API!.AddSectionTitle(this.ModManifest, I18n.Section_General_Name, I18n.Section_General_Description);
// Animal Shop Limit
gmcm.API.AddNumberOption(
this.ModManifest,
() => this.Config.AnimalShopLimit,
value => this.Config.AnimalShopLimit = value,
I18n.Config_AnimalShopLimit_Name,
I18n.Config_AnimalShopLimit_Tooltip,
fieldId: nameof(ModConfig.AnimalShopLimit));
gmcm.API.AddSectionTitle(this.ModManifest, I18n.Section_Controls_Name, I18n.Section_Controls_Description);
// Next Page
gmcm.API.AddKeybindList(
this.ModManifest,
() => this.Config.ControlScheme.NextPage,
value => this.Config.ControlScheme.NextPage = value,
I18n.Config_NextPage_Name,
I18n.Config_NextPage_Tooltip,
nameof(Controls.NextPage));
// Previous Page
gmcm.API.AddKeybindList(
this.ModManifest,
() => this.Config.ControlScheme.PreviousPage,
value => this.Config.ControlScheme.PreviousPage = value,
I18n.Config_PreviousPage_Name,
I18n.Config_PreviousPage_Tooltip,
nameof(Controls.PreviousPage));
}
private void OnMenuChanged(object? sender, MenuChangedEventArgs e)
{
// Reset Stock/CurrentPage
if (e.NewMenu is not PurchaseAnimalsMenu menu)
{
this.Stock = null;
this._currentPage.Value = 0;
return;
}
// Reposition Next/Previous Page Buttons
this.NextPage.bounds.X = menu.xPositionOnScreen + menu.width - this.NextPage.bounds.Width;
this.NextPage.bounds.Y = menu.yPositionOnScreen + menu.height;
this.NextPage.leftNeighborID = this.PreviousPage.myID;
this.PreviousPage.bounds.X = menu.xPositionOnScreen;
this.PreviousPage.bounds.Y = menu.yPositionOnScreen + menu.height;
this.PreviousPage.rightNeighborID = this.NextPage.myID;
for (var index = 0; index < menu.animalsToPurchase.Count; index++)
{
var i = index + this.CurrentPage * this.Config.AnimalShopLimit;
if (ReferenceEquals(menu.animalsToPurchase[index].texture, Game1.mouseCursors))
{
menu.animalsToPurchase[index].sourceRect.X = i % 3 * 16 * 2;
menu.animalsToPurchase[index].sourceRect.Y = 448 + i / 3 * 16;
}
if (ReferenceEquals(menu.animalsToPurchase[index].texture, Game1.mouseCursors2))
{
menu.animalsToPurchase[index].sourceRect.X = 128 + i % 3 * 16 * 2;
menu.animalsToPurchase[index].sourceRect.Y = i / 3 * 16;
}
}
// Assign neighborId for controller
var maxY = menu.animalsToPurchase.Max(component => component.bounds.Y);
var bottomComponents = menu.animalsToPurchase.Where(component => component.bounds.Y == maxY).ToList();
this.PreviousPage.upNeighborID = bottomComponents.OrderBy(component => Math.Abs(component.bounds.Center.X - this.PreviousPage.bounds.X)).First().myID;
this.NextPage.upNeighborID = bottomComponents.OrderBy(component => Math.Abs(component.bounds.Center.X - this.NextPage.bounds.X)).First().myID;
foreach (var component in bottomComponents)
{
component.downNeighborID = component.bounds.Center.X <= menu.xPositionOnScreen + menu.width / 2 ? this.PreviousPage.myID : this.NextPage.myID;
}
}
private void OnRenderedActiveMenu(object? sender, RenderedActiveMenuEventArgs e)
{
if (Game1.activeClickableMenu is not PurchaseAnimalsMenu || this.Stock is null || this.Stock.Count <= this.Config.AnimalShopLimit)
{
return;
}
if ((this.CurrentPage + 1) * this.Config.AnimalShopLimit < this.Stock.Count)
{
this.NextPage.draw(e.SpriteBatch);
}
if (this.CurrentPage > 0)
{
this.PreviousPage.draw(e.SpriteBatch);
}
}
}