-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDabYeeter.cs
107 lines (88 loc) · 3.75 KB
/
DabYeeter.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
using HMUI;
using IPA.Utilities;
using System;
using System.Runtime.CompilerServices;
using UnityEngine;
using UnityEngine.UI;
using YeetDab.Utilities;
using Zenject;
namespace YeetDab
{
class DabYeeter : IInitializable, IDisposable
{
private static FieldAccessor<MainMenuViewController, Button>.Accessor SoloButton = FieldAccessor<MainMenuViewController, Button>.GetAccessor("_soloButton");
private static FieldAccessor<ButtonSpriteSwap, Sprite>.Accessor ActiveStateSprite = FieldAccessor<ButtonSpriteSwap, Sprite>.GetAccessor("_normalStateSprite");
private static FieldAccessor<ButtonSpriteSwap, Sprite>.Accessor HighlightStateSprite = FieldAccessor<ButtonSpriteSwap, Sprite>.GetAccessor("_highlightStateSprite");
private MainMenuViewController mainMenuViewController;
public DabYeeter(MainMenuViewController mainMenuView)
{
this.mainMenuViewController = mainMenuView;
}
public void Initialize()
{
if (mainMenuViewController != null)
mainMenuViewController.didActivateEvent += MainMenuView_didActivateEvent;
else
Plugin.Log.Error(GetLogString("DabYeeter failed to initialize. Reason: mainMenuView == null."));
}
public void Dispose()
{
if (mainMenuViewController != null)
mainMenuViewController.didActivateEvent -= MainMenuView_didActivateEvent;
}
private void MainMenuView_didActivateEvent(bool firstActivation, bool addedToHierarchy, bool screenSystemEnabling)
{
if (firstActivation)
YeetTheDab();
}
private void YeetTheDab()
{
Button soloButton = SoloButton(ref mainMenuViewController);
ButtonSpriteSwap soloSpriteSwap = soloButton?.GetComponentInChildren<ButtonSpriteSwap>() ?? null;
Image img = GetImageOverlayFromButton(soloButton);
if (soloSpriteSwap == null || img == null)
{
Plugin.Log.Error(GetLogString("Dab can't be yeeted! Reason: Failed to get all required components from the soloButton."));
return;
}
MenuButtonSprites soloButtonSprites = LoadButtonSprites("SoloOutlineOverlay.png", "SoloNormalState.png", "SoloHighlightState.png");
ReplaceMenuButtonSprites(ref img, ref soloSpriteSwap, soloButtonSprites);
Plugin.Log.Info("DabYeeter: Successfully yeeted dab");
}
private Image GetImageOverlayFromButton(Button button)
{
Transform imgTrans = button?.transform.Find("Image/ImageOverlay") ?? null;
Image img = imgTrans?.GetComponent<Image>() ?? null;
if (img == null)
Plugin.Log.Error(GetLogString("Failed to get Image from button."));
return img;
}
private void ReplaceMenuButtonSprites(ref Image img, ref ButtonSpriteSwap soloSpriteSwap, MenuButtonSprites menuButtonSprites)
{
img.sprite = menuButtonSprites.OverlaySprite;
ActiveStateSprite(ref soloSpriteSwap) = menuButtonSprites.NormalStateSprite;
HighlightStateSprite(ref soloSpriteSwap) = menuButtonSprites.HighlightStateSprite;
}
internal static MenuButtonSprites LoadButtonSprites(string outlineFileName, string defaultBGFileName, string highlightedBGFileName) =>
new MenuButtonSprites(SpriteUtils.LoadSpriteFromResources(outlineFileName),
SpriteUtils.LoadSpriteFromResources(defaultBGFileName),
SpriteUtils.LoadSpriteFromResources(highlightedBGFileName)
);
private string GetLogString(string logMessage, [CallerMemberName] string callerMethodName = "")
{
return $"Class: DabYeeter, Method: {callerMethodName}, Message: {logMessage}";
}
}
internal struct MenuButtonSprites
{
public readonly Sprite OverlaySprite;
public readonly Sprite NormalStateSprite;
public readonly Sprite HighlightStateSprite;
public MenuButtonSprites(Sprite overlaySprite, Sprite normalStateSprite, Sprite highlightStateSprite)
{
OverlaySprite = overlaySprite;
NormalStateSprite = normalStateSprite;
HighlightStateSprite = highlightStateSprite;
}
}
}