forked from LeFauxMatt/StardewMods
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ToolbarIcons.cs
475 lines (423 loc) · 15.5 KB
/
ToolbarIcons.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
namespace StardewMods.ToolbarIcons;
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using StardewModdingAPI;
using StardewModdingAPI.Events;
using StardewModdingAPI.Utilities;
using StardewMods.Common.Enums;
using StardewMods.Common.Helpers;
using StardewMods.Common.Integrations.GenericModConfigMenu;
using StardewMods.ToolbarIcons.ModIntegrations;
using StardewMods.ToolbarIcons.UI;
using StardewValley;
using StardewValley.Menus;
/// <inheritdoc />
public class ToolbarIcons : Mod
{
private const string AlwaysScrollMapId = "bcmpinc.AlwaysScrollMap";
private const string CJBCheatsMenuId = "CJBok.CheatsMenu";
private const string CJBItemSpawnerId = "CJBok.ItemSpawner";
private const string DynamicGameAssetsId = "spacechase0.DynamicGameAssets";
private const string GenericModConfigMenuId = "spacechase0.GenericModConfigMenu";
private const string StardewAquariumId = "Cherry.StardewAquarium";
private readonly PerScreen<ToolbarIconsApi?> _api = new();
private readonly PerScreen<ComponentArea> _area = new(() => ComponentArea.Custom);
private readonly PerScreen<ClickableComponent?> _button = new();
private readonly PerScreen<string> _hoverText = new();
private readonly PerScreen<Toolbar?> _toolbar = new();
private ModConfig? _config;
private ToolbarIconsApi Api
{
get => this._api.Value ??= new(this.Helper, this.Config.Icons, this.Components);
}
private ComponentArea Area
{
get => this._area.Value;
set => this._area.Value = value;
}
private ClickableComponent? Button
{
get
{
var toolbar = Game1.onScreenMenus.OfType<Toolbar>().FirstOrDefault();
if (this.Toolbar is not null && ReferenceEquals(toolbar, this.Toolbar))
{
return this._button.Value;
}
if (toolbar is not null)
{
this.Toolbar = toolbar;
var buttons = this.Helper.Reflection.GetField<List<ClickableComponent>>(toolbar, "buttons").GetValue();
this._button.Value = buttons.First();
return this._button.Value;
}
return null;
}
}
private ComplexIntegration? ComplexIntegration { get; set; }
private Dictionary<string, ClickableTextureComponent> Components { get; } = new();
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 string HoverText
{
get => this._hoverText.Value;
set => this._hoverText.Value = value;
}
private bool Loaded { get; set; }
private SimpleIntegration? SimpleIntegration { get; set; }
private Toolbar? Toolbar
{
get => this._toolbar.Value;
set => this._toolbar.Value = value;
}
/// <inheritdoc />
public override void Entry(IModHelper helper)
{
Log.Monitor = this.Monitor;
I18n.Init(helper.Translation);
ThemeHelper.Init(this.Helper, "furyx639.ToolbarIcons/Icons", "furyx639.ToolbarIcons/Arrows");
if (this.Helper.ModRegistry.IsLoaded("furyx639.FuryCore"))
{
Log.Alert("Remove FuryCore, it is no longer needed by this mod!");
}
// Events
this.Helper.Events.Content.AssetRequested += ToolbarIcons.OnAssetRequested;
this.Helper.Events.GameLoop.GameLaunched += this.OnGameLaunched;
this.Helper.Events.Input.ButtonPressed += this.OnButtonPressed;
this.Helper.Events.Input.CursorMoved += this.OnCursorMoved;
this.Helper.Events.Display.RenderedHud += this.OnRenderedHud;
this.Helper.Events.Display.RenderingHud += this.OnRenderingHud;
this.Helper.Events.GameLoop.SaveLoaded += this.OnSaveLoaded;
}
/// <inheritdoc />
public override object GetApi()
{
return this.Api;
}
private static void OnAssetRequested(object? sender, AssetRequestedEventArgs e)
{
if (e.Name.IsEquivalentTo("furyx639.ToolbarIcons/Icons"))
{
e.LoadFromModFile<Texture2D>("assets/icons.png", AssetLoadPriority.Exclusive);
return;
}
if (e.Name.IsEquivalentTo("furyx639.ToolbarIcons/Arrows"))
{
e.LoadFromModFile<Texture2D>("assets/arrows.png", AssetLoadPriority.Exclusive);
return;
}
if (e.Name.IsEquivalentTo("furyx639.ToolbarIcons/Toolbar"))
{
e.LoadFrom(() => new Dictionary<string, string>(), AssetLoadPriority.Exclusive);
}
}
private void DrawButton(SpriteBatch b, Vector2 pos)
{
var label = I18n.Config_OpenMenu_Name();
var dims = Game1.dialogueFont.MeasureString(I18n.Config_OpenMenu_Name());
var bounds = new Rectangle((int)pos.X, (int)pos.Y, (int)dims.X + Game1.tileSize, Game1.tileSize);
if (Game1.activeClickableMenu.GetChildMenu() is null)
{
var point = Game1.getMousePosition();
if (Game1.oldMouseState.LeftButton == ButtonState.Released && Mouse.GetState().LeftButton == ButtonState.Pressed && bounds.Contains(point))
{
Game1.activeClickableMenu.SetChildMenu(new ToolbarIconsMenu(this.Config.Icons, this.Components));
return;
}
}
IClickableMenu.drawTextureBox(
b,
Game1.mouseCursors,
new(432, 439, 9, 9),
bounds.X,
bounds.Y,
bounds.Width,
bounds.Height,
Color.White,
Game1.pixelZoom,
false,
1f);
Utility.drawTextWithShadow(
b,
label,
Game1.dialogueFont,
new Vector2(bounds.Left + bounds.Right - dims.X, bounds.Top + bounds.Bottom - dims.Y) / 2f,
Game1.textColor,
1f,
1f,
-1,
-1,
0f);
}
private void OnButtonPressed(object? sender, ButtonPressedEventArgs e)
{
if (!Game1.displayHUD || Game1.activeClickableMenu is not null || !Game1.onScreenMenus.OfType<Toolbar>().Any())
{
return;
}
if (e.Button is not SButton.MouseLeft or SButton.MouseRight)
{
return;
}
var (x, y) = Game1.getMousePosition(true);
var component = this.Components.Values.FirstOrDefault(component => component.visible && component.containsPoint(x, y));
if (component is not null)
{
Game1.playSound("drumkit6");
this.Api.Invoke(component.name);
this.Helper.Input.Suppress(e.Button);
}
}
private void OnCursorMoved(object? sender, CursorMovedEventArgs e)
{
if (!Game1.displayHUD || Game1.activeClickableMenu is not null || !Game1.onScreenMenus.OfType<Toolbar>().Any())
{
return;
}
var (x, y) = Game1.getMousePosition(true);
this.HoverText = string.Empty;
foreach (var component in this.Components.Values.Where(component => component.visible))
{
component.tryHover(x, y);
if (component.bounds.Contains(x, y))
{
this.HoverText = component.hoverText;
}
}
}
private void OnGameLaunched(object? sender, GameLaunchedEventArgs e)
{
this.SimpleIntegration = SimpleIntegration.Init(this.Helper, this.Api);
this.ComplexIntegration = ComplexIntegration.Init(this.Helper, this.Api);
// Integrations
this.ComplexIntegration.AddMethodWithParams(ToolbarIcons.StardewAquariumId, 1, I18n.Button_StardewAquarium(), "OpenAquariumCollectionMenu", "aquariumprogress", Array.Empty<string>());
this.ComplexIntegration.AddMethodWithParams(ToolbarIcons.CJBCheatsMenuId, 4, I18n.Button_CheatsMenu(), "OpenCheatsMenu", 0, true);
this.ComplexIntegration.AddMethodWithParams(ToolbarIcons.DynamicGameAssetsId, 6, I18n.Button_DynamicGameAssets(), "OnStoreCommand", "dga_store", Array.Empty<string>());
this.ComplexIntegration.AddMethodWithParams(ToolbarIcons.GenericModConfigMenuId, 13, I18n.Button_GenericModConfigMenu(), "OpenListMenu", 0);
this.ComplexIntegration.AddCustomAction(
ToolbarIcons.CJBItemSpawnerId,
5,
I18n.Button_ItemSpawner(),
mod =>
{
var buildMenu = this.Helper.Reflection.GetMethod(mod, "BuildMenu", false);
return buildMenu is not null
? () => { Game1.activeClickableMenu = buildMenu.Invoke<ItemGrabMenu>(); }
: null;
});
this.ComplexIntegration.AddCustomAction(
ToolbarIcons.AlwaysScrollMapId,
2,
I18n.Button_AlwaysScrollMap(),
mod =>
{
var config = mod.GetType().GetField("config")?.GetValue(mod);
if (config is null)
{
return null;
}
var enabledIndoors = this.Helper.Reflection.GetField<bool>(config, "EnabledIndoors", false);
var enabledOutdoors = this.Helper.Reflection.GetField<bool>(config, "EnabledOutdoors", false);
if (enabledIndoors is null || enabledOutdoors is null)
{
return null;
}
return () =>
{
if (Game1.currentLocation.IsOutdoors)
{
enabledOutdoors.SetValue(!enabledOutdoors.GetValue());
}
else
{
enabledIndoors.SetValue(!enabledIndoors.GetValue());
}
};
});
}
private void OnRenderedHud(object? sender, RenderedHudEventArgs e)
{
if (!this.Loaded || !Game1.displayHUD || Game1.activeClickableMenu is not null || !Game1.onScreenMenus.OfType<Toolbar>().Any())
{
return;
}
if (!string.IsNullOrWhiteSpace(this.HoverText))
{
IClickableMenu.drawHoverText(e.SpriteBatch, this.HoverText, Game1.smallFont);
}
}
private void OnRenderingHud(object? sender, RenderingHudEventArgs e)
{
if (!Game1.displayHUD || Game1.activeClickableMenu is not null)
{
return;
}
this.ReorientComponents();
foreach (var component in this.Components.Values.Where(component => component.visible))
{
var icons = this.Helper.GameContent.Load<Texture2D>("furyx639.ToolbarIcons/Icons");
e.SpriteBatch.Draw(
icons,
new(component.bounds.X, component.bounds.Y),
new(0, 0, 16, 16),
Color.White,
0f,
Vector2.Zero,
2f,
SpriteEffects.None,
1f);
component.draw(e.SpriteBatch);
}
}
private void OnSaveLoaded(object? sender, SaveLoadedEventArgs e)
{
if (!this.Loaded)
{
this.Loaded = true;
foreach (var (key, data) in this.Helper.GameContent.Load<IDictionary<string, string>>("furyx639.ToolbarIcons/Toolbar"))
{
var info = data.Split('/');
var modId = key.Split('/')[0];
var index = int.Parse(info[2]);
switch (info[3])
{
case "method":
this.SimpleIntegration?.AddMethod(modId, index, info[0], info[4], info[1]);
break;
case "keybind":
this.SimpleIntegration?.AddKeybind(modId, index, info[0], info[4], info[1]);
break;
}
}
}
this.ReorientComponents();
var gmcm = new GenericModConfigMenuIntegration(this.Helper.ModRegistry);
if (gmcm.IsLoaded)
{
// Register mod configuration
gmcm.Register(
this.ModManifest,
() => this._config = new(),
this.SaveConfig);
gmcm.API.AddComplexOption(
this.ModManifest,
I18n.Config_CustomizeToolbar_Name,
this.DrawButton,
I18n.Config_CustomizeToolbar_Tooltip,
height: () => Game1.tileSize);
}
}
private void ReorientComponents()
{
if (this.Button is null || this.Toolbar is null || !this.Components.Values.Any(component => component.visible))
{
return;
}
var xAlign = this.Button.bounds.X < Game1.viewport.Width / 2;
var yAlign = this.Button.bounds.Y < Game1.viewport.Height / 2;
ComponentArea area;
int x;
int y;
if (this.Toolbar.width > this.Toolbar.height)
{
x = this.Button.bounds.Left;
if (yAlign)
{
area = ComponentArea.Top;
y = this.Button.bounds.Bottom + 20;
}
else
{
area = ComponentArea.Bottom;
y = this.Button.bounds.Top - 52;
}
}
else
{
y = this.Button.bounds.Top;
if (xAlign)
{
area = ComponentArea.Left;
x = this.Button.bounds.Right + 20;
}
else
{
area = ComponentArea.Right;
x = this.Button.bounds.Left - 52;
}
}
var firstComponent = this.Components.Values.First(component => component.visible);
if (area != this.Area || firstComponent.bounds.X != x || firstComponent.bounds.Y != y)
{
this.ReorientComponents(area, x, y);
}
}
private void ReorientComponents(ComponentArea area, int x, int y)
{
this.Area = area;
foreach (var icon in this.Config.Icons)
{
if (this.Components.TryGetValue(icon.Id, out var component))
{
if (!icon.Enabled)
{
component.visible = false;
continue;
}
component.visible = true;
component.bounds.X = x;
component.bounds.Y = y;
switch (area)
{
case ComponentArea.Top:
case ComponentArea.Bottom:
x += component.bounds.Width + 4;
break;
case ComponentArea.Right:
case ComponentArea.Left:
y += component.bounds.Height + 4;
break;
case ComponentArea.Custom:
default:
break;
}
}
}
}
private void SaveConfig()
{
foreach (var icon in this.Config.Icons)
{
if (this.Components.TryGetValue(icon.Id, out var component))
{
component.visible = icon.Enabled;
}
}
this.Helper.WriteConfig(this.Config);
this.ReorientComponents();
}
}