forked from CJBok/SDV-Mods
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModEntry.cs
271 lines (229 loc) · 11.9 KB
/
ModEntry.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
using System;
using System.Collections.Generic;
using System.Linq;
using CJBShowItemSellPrice.Framework;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using StardewModdingAPI;
using StardewModdingAPI.Events;
using StardewValley;
using StardewValley.Menus;
using SObject = StardewValley.Object;
namespace CJBShowItemSellPrice
{
/// <summary>The mod entry point.</summary>
internal class ModEntry : Mod
{
/*********
** Fields
*********/
/// <summary>The spritesheet source rectangle for the coin icon.</summary>
private readonly Rectangle CoinSourceRect = new Rectangle(5, 69, 6, 6);
/// <summary>The spritesheet source rectangle for the tooltip box.</summary>
private readonly Rectangle TooltipSourceRect = new Rectangle(0, 256, 60, 60);
/// <summary>The pixel size of the tooltip box's border (i.e. the number of pixels to offset for text to appear inside the box).</summary>
private const int TooltipBorderSize = 12;
/// <summary>The padding between elements in the tooltip box.</summary>
private const int Padding = 5;
/// <summary>The pixel offset to apply to the tooltip box relative to the cursor position.</summary>
private readonly Vector2 TooltipOffset = new Vector2(Game1.tileSize / 2);
/// <summary>The label text for the single-item price.</summary>
private string SingleLabel;
/// <summary>The label text for the stack price.</summary>
private string StackLabel;
/// <summary>The cached toolbar instance.</summary>
private Toolbar Toolbar;
/// <summary>The cached toolbar slots.</summary>
private IList<ClickableComponent> ToolbarSlots;
/// <summary>Metadata that isn't available from the game data directly.</summary>
private DataModel Data;
/*********
** Public methods
*********/
/// <summary>The mod entry point, called after the mod is first loaded.</summary>
/// <param name="helper">Provides simplified APIs for writing mods.</param>
public override void Entry(IModHelper helper)
{
// init text
this.SingleLabel = this.Helper.Translation.Get("labels.single-price") + ":";
this.StackLabel = this.Helper.Translation.Get("labels.stack-price") + ":";
// load data
this.Data = helper.Data.ReadJsonFile<DataModel>("assets/data.json") ?? new DataModel();
this.Data.ForceSellable ??= new HashSet<int>();
// hook events
helper.Events.Display.RenderedActiveMenu += this.OnRenderedActiveMenu;
helper.Events.Display.RenderedHud += this.OnRenderedHud;
helper.Events.GameLoop.UpdateTicked += this.OnUpdateTicked;
}
/*********
** Private methods
*********/
/// <summary>Raised after the game state is updated (≈60 times per second).</summary>
/// <param name="sender">The event sender.</param>
/// <param name="e">The event arguments.</param>
private void OnUpdateTicked(object sender, UpdateTickedEventArgs e)
{
// cache the toolbar & slots
if (e.IsOneSecond)
{
if (Context.IsPlayerFree)
{
this.Toolbar = Game1.onScreenMenus.OfType<Toolbar>().FirstOrDefault();
this.ToolbarSlots = this.Toolbar != null
? this.Helper.Reflection.GetField<List<ClickableComponent>>(this.Toolbar, "buttons").GetValue()
: null;
}
else
{
this.Toolbar = null;
this.ToolbarSlots = null;
}
}
}
/// <summary>When a menu is open (<see cref="Game1.activeClickableMenu"/> isn't null), raised after that menu is drawn to the sprite batch but before it's rendered to the screen.</summary>
/// <param name="sender">The event sender.</param>
/// <param name="e">The event arguments.</param>
private void OnRenderedActiveMenu(object sender, RenderedActiveMenuEventArgs e)
{
// get item
Item item = this.GetItemFromMenu(Game1.activeClickableMenu);
if (item == null)
return;
// draw tooltip
this.DrawPriceTooltip(Game1.spriteBatch, Game1.smallFont, item);
}
/// <summary>Raised after drawing the HUD (item toolbar, clock, etc) to the sprite batch, but before it's rendered to the screen. The vanilla HUD may be hidden at this point (e.g. because a menu is open).</summary>
/// <param name="sender">The event sender.</param>
/// <param name="e">The event arguments.</param>
private void OnRenderedHud(object sender, EventArgs e)
{
if (!Context.IsPlayerFree)
return;
// get item
Item item = this.GetItemFromToolbar();
if (item == null)
return;
// draw tooltip
this.DrawPriceTooltip(Game1.spriteBatch, Game1.smallFont, item);
}
/// <summary>Get the hovered item from an arbitrary menu.</summary>
/// <param name="menu">The menu whose hovered item to find.</param>
private Item GetItemFromMenu(IClickableMenu menu)
{
// game menu
if (menu is GameMenu gameMenu)
{
IClickableMenu page = this.Helper.Reflection.GetField<List<IClickableMenu>>(gameMenu, "pages").GetValue()[gameMenu.currentTab];
if (page is InventoryPage)
return this.Helper.Reflection.GetField<Item>(page, "hoveredItem").GetValue();
else if (page is CraftingPage)
return this.Helper.Reflection.GetField<Item>(page, "hoverItem").GetValue();
}
// from inventory UI
else if (menu is MenuWithInventory inventoryMenu)
return inventoryMenu.hoveredItem;
// CJB mods
else if (menu.GetType().FullName == "CJBItemSpawner.Framework.ItemMenu")
return this.Helper.Reflection.GetField<Item>(menu, "HoveredItem").GetValue();
return null;
}
/// <summary>Get the hovered item from the on-screen toolbar.</summary>
private Item GetItemFromToolbar()
{
if (!Context.IsPlayerFree || this.Toolbar == null || this.ToolbarSlots == null)
return null;
// find hovered slot
int x = Game1.getMouseX();
int y = Game1.getMouseY();
ClickableComponent hoveredSlot = this.ToolbarSlots.FirstOrDefault(slot => slot.containsPoint(x, y));
if (hoveredSlot == null)
return null;
// get inventory index
int index = this.ToolbarSlots.IndexOf(hoveredSlot);
if (index < 0 || index > Game1.player.Items.Count - 1)
return null;
// get hovered item
return Game1.player.Items[index];
}
/// <summary>Draw a tooltip box which shows the unit and stack prices for an item.</summary>
/// <param name="spriteBatch">The sprite batch to update.</param>
/// <param name="font">The font with which to draw text.</param>
/// <param name="item">The item whose price to display.</param>
private void DrawPriceTooltip(SpriteBatch spriteBatch, SpriteFont font, Item item)
{
// get info
int stack = item.Stack;
int? price = this.GetSellPrice(item);
if (price == null)
return;
// basic measurements
const int borderSize = ModEntry.TooltipBorderSize;
const int padding = ModEntry.Padding;
int coinSize = this.CoinSourceRect.Width * Game1.pixelZoom;
int lineHeight = (int)font.MeasureString("X").Y;
Vector2 offsetFromCursor = this.TooltipOffset;
bool showStack = stack > 1;
// prepare text
string unitLabel = this.SingleLabel;
string unitPrice = price.ToString();
string stackLabel = this.StackLabel;
string stackPrice = (price * stack).ToString();
// get dimensions
Vector2 unitPriceSize = font.MeasureString(unitPrice);
Vector2 stackPriceSize = font.MeasureString(stackPrice);
Vector2 labelSize = font.MeasureString(unitLabel);
if (showStack)
labelSize = new Vector2(Math.Max(labelSize.X, font.MeasureString(stackLabel).X), labelSize.Y * 2);
Vector2 innerSize = new Vector2(labelSize.X + padding + Math.Max(unitPriceSize.X, showStack ? stackPriceSize.X : 0) + padding + coinSize, labelSize.Y);
Vector2 outerSize = innerSize + new Vector2((borderSize + padding) * 2);
// get position
float x = (Mouse.GetState().X / Game1.options.zoomLevel) - offsetFromCursor.X - outerSize.X;
float y = (Mouse.GetState().Y / Game1.options.zoomLevel) + offsetFromCursor.Y + borderSize;
// adjust position to fit on screen
Rectangle area = new Rectangle((int)x, (int)y, (int)outerSize.X, (int)outerSize.Y);
if (area.Right > Game1.viewport.Width)
x = Game1.viewport.Width - area.Width;
if (area.Bottom > Game1.viewport.Height)
y = Game1.viewport.Height - area.Height;
// draw tooltip box
IClickableMenu.drawTextureBox(spriteBatch, Game1.menuTexture, this.TooltipSourceRect, (int)x, (int)y, (int)outerSize.X, (int)outerSize.Y, Color.White);
// draw coins
spriteBatch.Draw(Game1.debrisSpriteSheet, new Vector2(x + outerSize.X - borderSize - padding - coinSize, y + borderSize + padding), this.CoinSourceRect, Color.White, 0.0f, Vector2.Zero, Game1.pixelZoom, SpriteEffects.None, 1f);
if (showStack)
spriteBatch.Draw(Game1.debrisSpriteSheet, new Vector2(x + outerSize.X - borderSize - padding - coinSize, y + borderSize + padding + lineHeight), this.CoinSourceRect, Color.White, 0.0f, Vector2.Zero, Game1.pixelZoom, SpriteEffects.None, 1f);
// draw text
Utility.drawTextWithShadow(spriteBatch, unitLabel, font, new Vector2(x + borderSize + padding, y + borderSize + padding), Game1.textColor);
Utility.drawTextWithShadow(spriteBatch, unitPrice, font, new Vector2(x + outerSize.X - borderSize - padding - coinSize - padding - unitPriceSize.X, y + borderSize + padding), Game1.textColor);
if (showStack)
{
Utility.drawTextWithShadow(spriteBatch, stackLabel, font, new Vector2(x + borderSize + padding, y + borderSize + padding + lineHeight), Game1.textColor);
Utility.drawTextWithShadow(spriteBatch, stackPrice, font, new Vector2(x + outerSize.X - borderSize - padding - coinSize - padding - stackPriceSize.X, y + borderSize + padding + lineHeight), Game1.textColor);
}
}
/// <summary>Get the sell price for an item.</summary>
/// <param name="item">The item to check.</param>
/// <returns>Returns the sell price, or <c>null</c> if it can't be sold.</returns>
private int? GetSellPrice(Item item)
{
// skip unsellable item
if (!this.CanBeSold(item))
return null;
// get price
int price = item is SObject obj
? obj.sellToStorePrice()
: item.salePrice() / 2;
return price >= 0
? price
: null as int?;
}
/// <summary>Get whether an item can be sold.</summary>
/// <param name="item">The item to check.</param>
private bool CanBeSold(Item item)
{
return
(item is SObject obj && obj.canBeShipped())
|| this.Data.ForceSellable.Contains(item.Category);
}
}
}