-
Notifications
You must be signed in to change notification settings - Fork 5
/
CustomButton.cs
276 lines (234 loc) · 10.5 KB
/
CustomButton.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using AmongUs.GameOptions;
using UnityEngine;
namespace DillyzRoleApi_Rewritten
{
// https://stackoverflow.com/questions/7056041/can-you-assign-a-function-to-a-variable-in-c
public class CustomButton
{
public KillButtonCustomData GameInstance; // ADD A NULL CHECK IF YOU USE THIS OK?!
public string name = "OtherKill";
public string imageName = "DillyzRoleApi_Rewritten.Assets.kill.png";
private float _cooldown = 30;
public float cooldown {
get {
return _cooldown >= 0 ? _cooldown : GameOptionsManager.Instance.currentNormalGameOptions.KillCooldown * Math.Abs(_cooldown);
}
set {
_cooldown = value;
}
}
public bool targetButton = false;
public bool buttonTargetsGhosts = false;
public bool caresAboutMoving = true;
public List<string> allowedRoles;
public bool canTargetSelf = false;
public List<string> rolesCantTarget;
public string buttonText;
public Color32 textOutlineColor;
public byte globalId;
public byte topGlobalId = 0;
public Assembly epicAssemblyFail = Assembly.GetExecutingAssembly();
private Action<KillButtonCustomData, bool> _onClicked;
private Action _onUpdate;
private Func<bool, bool> _canUse; // the bool is the original return
public static Dictionary<string, CustomButton> buttonMap = new Dictionary<string, CustomButton>();
public static CustomButton getButtonByName(string name) => buttonMap.ContainsKey(name) ? buttonMap[name] : null;
public static CustomButton addButton(Assembly epicAssemblyFail, string name, string imageName, float cooldown, bool isTargetButton, string[] allowedRoles,
string[] rolesCantTarget, Action<KillButtonCustomData, bool> onClicked) =>
buttonMap[name] = new CustomButton(epicAssemblyFail, name, imageName, cooldown, isTargetButton, allowedRoles, rolesCantTarget, onClicked);
public static List<CustomButton> AllCustomButtons => buttonMap.Values.ToArray().ToList();
public static CustomButton getById(byte targetId) {
foreach (CustomButton button in AllCustomButtons)
if (button.globalId == targetId)
return button;
return null;
}
public CustomButton(Assembly epicAssemblyFail, string name, string imageName, float cooldown, bool isTargetButton, string[] allowedRoles,
string[] rolesCantTarget, Action<KillButtonCustomData, bool> onClicked)
{
this.globalId = topGlobalId++;
DillyzRoleApiMain.Instance.Log.LogInfo($"Button ID {this.globalId} exists under {name}.");
this.epicAssemblyFail = epicAssemblyFail;
this.buttonText = this.name = name;
this.imageName = imageName;
this._cooldown = cooldown;
this.targetButton = isTargetButton;
this.buttonTargetsGhosts = false;
this.caresAboutMoving = true;
if (allowedRoles != null)
this.allowedRoles = allowedRoles.ToList();
this.canTargetSelf = false;
if (rolesCantTarget != null)
this.rolesCantTarget = rolesCantTarget.ToList();
this.textOutlineColor = this.targetButton ? CustomPalette.KillButtonTextOutline : CustomPalette.PassiveButtonTextOutline;
this._onClicked = onClicked;
}
public bool RoleAllowed(string role) {
return allowedRoles == null || allowedRoles.Contains(role);
}
public void OnClicked(KillButtonCustomData button, bool success) {
if (this._onClicked != null)
this._onClicked(button, success);
}
public void SetCustomUpdate(Action onUpdate) {
this._onUpdate = onUpdate;
}
// the bool in the return is representing
public void SetCanUse(Func<bool, bool> canUse)
{
this._canUse = canUse;
}
public Action GetCustomUpdate()
{
return this._onUpdate;
}
public Func<bool, bool> GetCanUse()
{
return this._canUse;
}
// USE TIMER FUNCS AND VARS!!!!!
public Action<KillButtonCustomData, bool> useTimerCallback; // button - interupted
public float useTime = 0f;
public void SetUseTimeButton(float useTime, Action<KillButtonCustomData, bool> useTimerCallback) {
this.useTime = Math.Abs(useTime);
this.useTimerCallback = useTimerCallback;
}
// end user time stuff
public static void ResetAllButtons()
{
PlayerControl.LocalPlayer.SetKillTimer(GameOptionsManager.Instance.CurrentGameOptions.GetFloat(FloatOptionNames.KillCooldown));
if (HudManagerPatch.AllKillButtons != null)
{
foreach (KillButtonCustomData button in HudManagerPatch.AllKillButtons)
{
button.lastUse = DateTime.UtcNow;
if (button.useTimerMode && button.buttonData.RoleAllowed(DillyzUtil.getRoleName(PlayerControl.LocalPlayer)))
button.InterruptUseTimer(true);
}
}
}
}
// used to attach data to kill button clones
[Il2CppItem]
public class KillButtonCustomData : MonoBehaviour
{
public float maxCooldown;
public DateTime lastUse;
public CustomButton buttonData;
public KillButton killButton;
public SpriteRenderer blockSpr;
public bool blockingButton = false;
public bool showIconOnBlocked = false;
public bool isSetup = false;
public bool useTimerMode = false;
public void Setup(CustomButton buttonData, KillButton killButton)
{
this.buttonData = buttonData;
this.killButton = killButton;
this.maxCooldown = this.buttonData.cooldown;
this.lastUse = DateTime.UtcNow;
this.isSetup = true;
this.buttonData.GameInstance = this;
}
public void Update()
{
InternalUpdate();
buttonData.GetCustomUpdate()?.Invoke();
}
private void InternalUpdate()
{
if (!isSetup)
return;
this.killButton.buttonLabelText.text = this.buttonData.buttonText;
this.killButton.buttonLabelText.SetOutlineColor(this.buttonData.textOutlineColor);
if (useTimerMode) {
TimeSpan useTimeLeft = DateTime.UtcNow - lastUse;
float useTimeRemaining = this.buttonData.useTime - ((float)useTimeLeft.TotalMilliseconds / 1000f);
this.killButton.SetCoolDown(useTimeRemaining, this.buttonData.useTime);
this.killButton.cooldownTimerText.color = Palette.AcceptedGreen;
if (useTimeRemaining > 0)
{
if (this.buttonData.targetButton)
SetTarget(null);
this.killButton.SetDisabled();
return;
}
InterruptUseTimer();
}
float timeRemaining = 0;
if (this.buttonData.cooldown != 0f)
{
TimeSpan timeLeft = DateTime.UtcNow - lastUse;
timeRemaining = maxCooldown - ((float)timeLeft.TotalMilliseconds / 1000f);
this.killButton.SetCoolDown(timeRemaining < 0f ? 0f : timeRemaining, maxCooldown);
}
else
this.killButton.SetCoolDown(0f, 1f);
if (blockSpr != null)
{
blockSpr.enabled = blockingButton && showIconOnBlocked && !useTimerMode;
if (blockSpr.enabled)
killButton.cooldownTimerText.text = "";
}
if (blockingButton) {
if (this.buttonData.targetButton)
SetTarget(null);
this.killButton.SetDisabled();
return;
}
if (this.buttonData.targetButton)
{
SetTarget(DillyzUtil.getClosestPlayer(PlayerControl.LocalPlayer, this.buttonData.rolesCantTarget,
GameOptionsData.KillDistances[GameOptionsManager.Instance.currentNormalGameOptions.KillDistance], !this.buttonData.buttonTargetsGhosts,
this.buttonData.canTargetSelf));
return;
}
if (timeRemaining <= 0 && !PlayerControl.LocalPlayer.inVent)
this.killButton.SetEnabled();
else
this.killButton.SetDisabled();
}
public bool CanUse()
{
if (!isSetup)
return false;
bool ogreturn = (MeetingHud.Instance == null && (buttonData.RoleAllowed(DillyzUtil.getRoleName(PlayerControl.LocalPlayer))));
Func<bool, bool> customret = buttonData.GetCanUse();
return customret != null ? customret.Invoke(ogreturn) : ogreturn;
}
public void SetTarget(PlayerControl player)
{
if (!isSetup)
return;
if (this.killButton.currentTarget != null && this.killButton.currentTarget != player)
this.killButton.currentTarget.ToggleHighlight(false, PlayerControl.LocalPlayer.Data.Role.TeamType);
if (PlayerControl.LocalPlayer.inVent) {
this.killButton.currentTarget = null;
this.killButton.SetDisabled();
return;
}
this.killButton.currentTarget = player;
if (killButton.currentTarget != null)
{
this.killButton.currentTarget.ToggleHighlight(true, PlayerControl.LocalPlayer.Data.Role.TeamType);
this.killButton.SetEnabled();
return;
}
this.killButton.SetDisabled();
}
public void InterruptUseTimer() => InterruptUseTimer(false);
public void InterruptUseTimer(bool actaullyInterrupted) {
if (!useTimerMode)
return;
if (buttonData.useTimerCallback != null)
buttonData.useTimerCallback(this, actaullyInterrupted);
useTimerMode = false;
lastUse = DateTime.UtcNow;
killButton.cooldownTimerText.color = Palette.White;
}
}
}