-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMethods.cs
320 lines (295 loc) · 13.9 KB
/
Methods.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
using HarmonyLib;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using StardewValley;
using StardewValley.Menus;
using System;
using System.Collections.Generic;
namespace CustomBackpack
{
public partial class ModEntry
{
private static bool IsWithinBounds(InventoryMenu instance, int x, int y)
{
int first = (instance.actualInventory.Count <= instance.capacity ? 0 : scrolled.Value * instance.capacity / instance.rows);
Rectangle rect = new Rectangle(instance.inventory[first].bounds.X, instance.inventory[first].bounds.Y, instance.inventory[first + instance.capacity - 1].bounds.X + instance.inventory[first + instance.capacity - 1].bounds.Width - instance.inventory[first].bounds.X, instance.inventory[first+instance.capacity - 1].bounds.Y + instance.inventory[first+instance.capacity - 1].bounds.Height - instance.inventory[first].bounds.Y);
return rect.Contains(x, y);
}
public static Rectangle GetBounds(InventoryMenu __instance, int i)
{
int offset = __instance.capacity >= __instance.actualInventory.Count ? 0 : scrolled.Value;
int width = __instance.capacity / __instance.rows;
if (i < offset * width || i >= offset * width + __instance.capacity)
{
return new Rectangle();
}
else
{
return new Rectangle(__instance.xPositionOnScreen + i % width * 64 + __instance.horizontalGap * (i % width), __instance.yPositionOnScreen + (i / width - offset) * (64 + __instance.verticalGap) + (i / width - offset - 1) * 4 - ((((i - offset * width) >= width || !__instance.playerInventory || __instance.verticalGap != 0)) ? 0 : 12), 64, 64);
}
}
private static int GetDownNeighbor(InventoryMenu __instance, int i)
{
int width = __instance.capacity / __instance.rows;
int offset = (__instance.capacity >= __instance.actualInventory.Count ? 0 : scrolled.Value) * width;
if (i < offset || i >= offset + __instance.capacity)
{
return -99999;
}
else
{
return (i >= offset + width * (__instance.rows - 1)) ? 102 : IDOffset + i + width - offset;
}
}
private static int GetUpNeighbor(InventoryMenu __instance, int i)
{
int width = __instance.capacity / __instance.rows;
int offset = (__instance.capacity >= __instance.actualInventory.Count ? 0 : scrolled.Value) * width;
if (i < offset || i >= offset + __instance.capacity)
{
return -99999;
}
else
{
return (i < offset + width) ? 12340 + i % width : IDOffset + i - width - offset;
}
}
private static int GetLeftNeighbor(InventoryMenu __instance, int i)
{
int width = __instance.capacity / __instance.rows;
int offset = (__instance.capacity >= __instance.actualInventory.Count ? 0 : scrolled.Value) * width;
if (i < offset || i >= offset + __instance.capacity)
{
return -99999;
}
else if(i % width == 0)
{
return 107;
}
else
{
return IDOffset + i - 1 - offset;
}
}
private static int GetRightNeighbor(InventoryMenu __instance, int i)
{
int width = __instance.capacity / __instance.rows;
int offset = (__instance.capacity >= __instance.actualInventory.Count ? 0 : scrolled.Value) * width;
if (i < offset || i >= offset + __instance.capacity)
{
return -99999;
}
else if (i % width == width - 1)
{
return 106;
}
else
{
return IDOffset + i + 1 - offset;
}
}
public static bool SetPlayerSlots(int slots, bool force = false)
{
if (!Config.ModEnabled || Game1.player is null || slots < 1)
return false;
Game1.activeClickableMenu = null;
if(slots == Game1.player.MaxItems)
{
SMonitor.Log($"Player backpack slots already at {slots}");
return false;
}
SMonitor.Log($"Changing player backpack slots from {Game1.player.MaxItems} to {slots}");
if(slots < Game1.player.MaxItems)
{
if (!force)
{
for (int i = Game1.player.Items.Count - 1; i >= slots; i--)
{
if (Game1.player.Items.Count <= i)
break;
if (Game1.player.Items[i] is not null)
{
SMonitor.Log($"Slot {i} isn't empty, aborting");
return false;
}
}
}
while (Game1.player.Items.Count > slots)
{
Game1.player.Items.RemoveAt(Game1.player.Items.Count - 1);
}
}
else
{
while (Game1.player.Items.Count < slots)
{
Game1.player.Items.Add(null);
}
}
Game1.player.maxItems.Value = slots;
return true;
}
private static void OnHover(ref InventoryMenu __instance, int x, int y)
{
if (__instance.capacity >= __instance.actualInventory.Count)
return;
if (Game1.input.GetMouseState().ScrollWheelValue != Game1.oldMouseState.ScrollWheelValue)
{
if (Game1.oldMouseState.ScrollWheelValue - Game1.input.GetMouseState().ScrollWheelValue > 0)
{
ChangeScroll(__instance, 1);
}
else
{
ChangeScroll(__instance, -1);
}
}
}
public static bool ChangeScroll(InventoryMenu __instance, int change)
{
if (change == 0)
return false;
if (scrolled.Value + change >= 0 && __instance.actualInventory.Count >= __instance.capacity / __instance.rows * (scrolled.Value + change) + __instance.capacity)
{
scrolled.Value += change;
Game1.playSound("shiny4");
var offset = __instance.GetOffset();
for (int i = 0; i < __instance.inventory.Count; i++)
{
__instance.inventory[i].myID = offset > i || offset + __instance.capacity <= i ? -99999 : IDOffset + i - offset;
__instance.inventory[i].bounds = GetBounds(__instance, i);
__instance.inventory[i].leftNeighborID = GetLeftNeighbor(__instance, i);
__instance.inventory[i].rightNeighborID = GetRightNeighbor(__instance, i);
__instance.inventory[i].downNeighborID = GetDownNeighbor(__instance, i);
__instance.inventory[i].upNeighborID = GetUpNeighbor(__instance, i);
}
return true;
}
return false;
}
public static void DrawUIElements(SpriteBatch b, InventoryMenu __instance)
{
int mouseX = Game1.getMouseX();
int mouseY = Game1.getMouseY();
int gridWidth = __instance.capacity / __instance.rows;
int totalRows = __instance.actualInventory.Count / gridWidth;
if (totalRows <= __instance.rows)
return;
int minScrollWidth = 4;
int maxScrollWidth = 16;
var cc1 = __instance.inventory[(scrolled.Value + 1) * gridWidth - 1];
Point corner1 = cc1.bounds.Location + new Point(cc1.bounds.Width, 0);
var cc2 = __instance.inventory[(scrolled.Value + __instance.rows) * gridWidth - 1];
Point corner2 = cc2.bounds.Location + new Point(cc2.bounds.Width, cc2.bounds.Height);
Point middle = corner1 + new Point(0, (corner2.Y - corner1.Y) / 2);
scrollArea.Value = new Rectangle(corner1, new Point(24, corner2.Y - corner1.Y));
if(scrollWidth.Value > 226)
IClickableMenu.drawTextureBox(b, Game1.mouseCursors, new Rectangle(403, 383, 6, 6), scrollArea.Value.X + maxScrollWidth - scrollWidth.Value, scrollArea.Value.Y, scrollWidth.Value, scrollArea.Value.Height, Color.White, 4f, false, -1f);
else
b.Draw(scrollTexture, new Rectangle(scrollArea.Value.X + maxScrollWidth - scrollWidth.Value, scrollArea.Value.Y, scrollWidth.Value, scrollArea.Value.Height), Color.White);
int scrollIntervals = totalRows - __instance.rows + 1;
int handleHeight = Math.Max(Config.MinHandleHeight, scrollArea.Value.Height / scrollIntervals);
int handleOffset = (handleHeight - scrollArea.Value.Height / scrollIntervals) / 2;
int scrollableHeight = (scrollArea.Value.Height - handleOffset * 2);
float scrollInterval = scrollableHeight / (float)scrollIntervals;
int handleY = scrollArea.Value.Y + (int)Math.Round(scrollInterval * scrolled.Value);
if(scrolled.Value == totalRows - __instance.rows)
{
handleY = scrollArea.Value.Y + scrollArea.Value.Height - handleHeight;
}
bool inScrollArea = scrollArea.Value.Contains(mouseX, mouseY);
if (inScrollArea)
{
ChangeScroll(__instance, scrollChange.Value);
}
if (inScrollArea || scrolling.Value || (Config.ShowArrows && (upArrow.bounds.Contains(mouseX, mouseY) || downArrow.bounds.Contains(mouseX, mouseY))))
{
scrollWidth.Value = Math.Min(maxScrollWidth - 4, scrollWidth.Value + 1);
if (scrolling.Value)
{
int yOffset = Math.Max(Math.Min(mouseY, scrollArea.Value.Y + scrollArea.Value.Height - 1), scrollArea.Value.Y) - scrollArea.Value.Y - handleOffset;
ChangeScroll(__instance, (int)(yOffset / scrollInterval) - scrolled.Value);
}
}
else if(scrollWidth.Value > 4)
{
scrollWidth.Value = Math.Max(minScrollWidth, scrollWidth.Value - 1);
}
if (scrollWidth.Value > 226)
b.Draw(Game1.mouseCursors, new Rectangle(scrollArea.Value.X + maxScrollWidth - scrollWidth.Value, handleY, scrollWidth.Value, handleHeight), new Rectangle(435, 463, 6, 10), Color.White);
else
b.Draw(handleTexture, new Rectangle(scrollArea.Value.X + maxScrollWidth - scrollWidth.Value, handleY, scrollWidth.Value, handleHeight), Color.White);
if (scrollWidth.Value > maxScrollWidth / 2 && Config.ShowArrows)
{
if (scrolled.Value > 0)
{
upArrow.setPosition(corner1.X - 3, corner1.Y - 23);
upArrow.draw(b);
}
if (scrolled.Value * __instance.capacity / __instance.rows + __instance.capacity < __instance.actualInventory.Count)
{
downArrow.setPosition(corner2.X - 3, corner2.Y - 3);
downArrow.draw(b);
}
}
if(__instance is not FullInventoryMenu)
{
if (SHelper.Input.IsDown(Config.ShowExpandedButton))
{
OpenFullInventory(__instance);
return;
}
expandButton.setPosition(corner2.X + 36, corner2.Y - 11);
expandButton.draw(b);
}
if (Game1.oldMouseState.LeftButton == ButtonState.Pressed)
{
if (__instance is not FullInventoryMenu && expandButton.containsPoint(mouseX, mouseY))
{
OpenFullInventory(__instance);
return;
}
else if (Config.ShowArrows && !inScrollArea)
{
if (pressTime.Value == 0 || (pressTime.Value >= 20 && pressTime.Value % 4 == 0))
{
if (upArrow.containsPoint(mouseX, mouseY))
{
ChangeScroll(__instance, -1);
}
else if (downArrow.containsPoint(mouseX, mouseY))
{
ChangeScroll(__instance, 1);
}
}
if (pressTime.Value < 20)
pressTime.Value++;
}
}
else
{
pressTime.Value = 0;
}
if (Config.ShowRowNumbers)
{
int width = __instance.capacity / __instance.rows;
for (int i = 0; i < __instance.rows; i++)
{
var cc = __instance.inventory[(scrolled.Value + i) * width];
Vector2 toDraw = new Vector2(cc.bounds.X - 8, cc.bounds.Y + 16);
var strToDraw = (scrolled.Value + 1 + i) + "";
Vector2 strSize = Game1.tinyFont.MeasureString(strToDraw);
b.DrawString(Game1.tinyFont, strToDraw, toDraw + new Vector2(-strSize.X / 2f, -strSize.Y), Color.DimGray);
}
}
}
private static void OpenFullInventory(InventoryMenu __instance)
{
Game1.playSound("shwip");
oldScrolled.Value = scrolled.Value;
lastMenu.Value = Game1.activeClickableMenu;
Game1.activeClickableMenu = new FullInventoryPage(__instance, __instance.xPositionOnScreen, __instance.yPositionOnScreen, __instance.width, __instance.height);
}
}
}