forked from spacechase0/GenericModConfigMenu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMod.cs
152 lines (133 loc) · 7.07 KB
/
Mod.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GenericModConfigMenu.UI;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using SpaceShared;
using StardewModdingAPI;
using StardewModdingAPI.Events;
using StardewValley;
using StardewValley.Menus;
namespace GenericModConfigMenu
{
public class Mod : StardewModdingAPI.Mod
{
public static Mod instance;
private Button configButton;
internal Dictionary<IManifest, ModConfig> configs = new Dictionary<IManifest, ModConfig>();
public override void Entry(IModHelper helper)
{
instance = this;
Log.Monitor = Monitor;
Texture2D tex = Helper.Content.Load<Texture2D>("assets/config-button.png");
configButton = new Button(tex);
configButton.LocalPosition = new Vector2(36, Game1.viewport.Height - 100);
configButton.Callback = (Element e) => TitleMenu.subMenu = new ModConfigMenu();
helper.Events.GameLoop.GameLaunched += onLaunched;
helper.Events.GameLoop.UpdateTicking += onUpdate;
helper.Events.Display.WindowResized += onWindowResized;
helper.Events.Display.Rendered += onRendered;
helper.Events.Input.MouseWheelScrolled += onMouseWheelScrolled;
}
public override object GetApi()
{
return new Api();
}
public class DummyConfig
{
public bool dummyBool = true;
public int dummyInt1 = 50;
public int dummyInt2 = 50;
public float dummyFloat1 = 0.5f;
public float dummyFloat2 = 0.5f;
public string dummyString1 = "Kirby";
public string dummyString2 = "Default";
internal static string[] dummyString2Choices = new string[] { "Default", "Kitties", "Cats", "Meow" };
public SButton dummyKeybinding = SButton.K;
public Color dummyColor = Color.White;
}
public DummyConfig config;
public class RandomColorWidgetState
{
public Color color;
}
private void onLaunched(object sender, GameLaunchedEventArgs e)
{
config = Helper.ReadConfig<DummyConfig>();
var api = Helper.ModRegistry.GetApi<IApi>(ModManifest.UniqueID);
api.RegisterModConfig(ModManifest, () => config = new DummyConfig(), () => Helper.WriteConfig(config));
api.RegisterLabel(ModManifest, "Dummy Label", "Testing labels");
api.RegisterSimpleOption(ModManifest, "Dummy Bool", "Testing a checkbox", () => config.dummyBool, (bool val) => config.dummyBool = val);
api.RegisterSimpleOption(ModManifest, "Dummy Int (1)", "Testing an int (simple)", () => config.dummyInt1, (int val) => config.dummyInt1 = val);
api.RegisterClampedOption(ModManifest, "Dummy Int (2)", "Testing an int (range)", () => config.dummyInt2, (int val) => config.dummyInt2 = val, 0, 100);
api.RegisterSimpleOption(ModManifest, "Dummy Float (1)", "Testing a float (simple)", () => config.dummyFloat1, (float val) => config.dummyFloat1 = val);
api.RegisterClampedOption(ModManifest, "Dummy Float (2)", "Testing a float (range)", () => config.dummyFloat2, (float val) => config.dummyFloat2 = val, 0, 1);
api.RegisterSimpleOption(ModManifest, "Dummy String (1)", "Testing a string", () => config.dummyString1, (string val) => config.dummyString1 = val);
api.RegisterChoiceOption(ModManifest, "Dummy String (2)", "Testing a dropdown box", () => config.dummyString2, (string val) => config.dummyString2 = val, DummyConfig.dummyString2Choices);
api.RegisterSimpleOption(ModManifest, "Dummy Keybinding", "Testing a keybinding", () => config.dummyKeybinding, (SButton val) => config.dummyKeybinding = val);
api.RegisterLabel(ModManifest, "", "");
// Complex widget - this just generates a random color on click.
Func<Vector2, object, object> randomColorUpdate =
(Vector2 pos, object state_) =>
{
var state = state_ as RandomColorWidgetState;
if (state == null)
state = new RandomColorWidgetState() { color = config.dummyColor };
var bounds = new Rectangle((int)pos.X + 12, (int)pos.Y + 12, 50 - 12 * 2, 50 - 12 * 2);
bool hover = bounds.Contains(Game1.getOldMouseX(), Game1.getOldMouseY());
if ( hover && Game1.oldMouseState.LeftButton == ButtonState.Released && Mouse.GetState().LeftButton == ButtonState.Pressed)
{
Random r = new Random();
state.color.R = (byte) r.Next(256);
state.color.G = (byte) r.Next(256);
state.color.B = (byte) r.Next(256);
}
return state;
};
Func<SpriteBatch, Vector2, object, object> randomColorDraw =
(SpriteBatch b, Vector2 pos, object state_) =>
{
var state = state_ as RandomColorWidgetState;
IClickableMenu.drawTextureBox(b, (int)pos.X, (int)pos.Y, 50, 50, Color.White);
var colorBox = new Rectangle((int)pos.X + 12, (int)pos.Y + 12, 50 - 12 * 2, 50 - 12 * 2);
b.Draw(Game1.staminaRect, colorBox, state.color);
return state;
};
Action<object> randomColorSave =
(object state) => config.dummyColor = (state as RandomColorWidgetState).color;
api.RegisterComplexOption(ModManifest, "Dummy Color", "Testing a complex widget (random color on click)", randomColorUpdate, randomColorDraw, randomColorSave);
}
private void onUpdate(object sender, UpdateTickingEventArgs e)
{
if (Game1.activeClickableMenu is TitleMenu tm)
{
if (TitleMenu.subMenu == null && Helper.Reflection.GetField<bool>(tm, "titleInPosition").GetValue())
configButton.Update();
}
}
private void onWindowResized(object sender, WindowResizedEventArgs e)
{
configButton.LocalPosition = new Vector2(configButton.Position.X, Game1.viewport.Height - 100);
}
private void onRendered(object sender, RenderedEventArgs e)
{
if ( Game1.activeClickableMenu is TitleMenu tm )
{
if (TitleMenu.subMenu == null && Helper.Reflection.GetField<bool>(tm, "titleInPosition").GetValue())
configButton.Draw(e.SpriteBatch);
}
}
private void onMouseWheelScrolled(object sender, MouseWheelScrolledEventArgs e)
{
Dropdown.ActiveDropdown?.receiveScrollWheelAction(e.Delta);
if (ModConfigMenu.ActiveConfigMenu is ModConfigMenu mcm)
mcm.receiveScrollWheelActionSmapi(e.Delta);
if (SpecificModConfigMenu.ActiveConfigMenu is SpecificModConfigMenu smcm)
smcm.receiveScrollWheelActionSmapi(e.Delta);
}
}
}