-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlugin.cs
212 lines (185 loc) · 9.16 KB
/
Plugin.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
using BepInEx;
using Dissonance;
using HarmonyLib;
using GameNetcodeStuff;
using BepInEx.Configuration;
using BepInEx.Logging;
using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
using System;
using System.Reflection;
namespace NameplateTweaks;
[BepInPlugin(modGUID, modName, modVersion)]
public class Plugin : BaseUnityPlugin
{
public const string modGUID = "taffyko.NameplateTweaks";
public const string modName = PluginInfo.PLUGIN_NAME;
public const string modVersion = PluginInfo.PLUGIN_VERSION;
public static ConfigEntry<bool> ConfigEnableSpeakingIndicator;
public static ConfigEntry<bool> ConfigVariableSpeakingIndicatorOpacity;
public static ConfigEntry<bool> ConfigSpeakingIndicatorAlwaysVisible;
public static ConfigEntry<float> ConfigNameplateScale;
public static ConfigEntry<float> ConfigNameplateVisibilityDistance;
public static ConfigEntry<bool> ConfigNameplateScaleWithDistance;
public static ManualLogSource log;
private readonly Harmony harmony = new Harmony(modGUID);
public static Vector3? OriginalNameplateScale = new Vector3(-0.0025f, 0.0025f, 0.0025f);
private void Awake()
{
log = BepInEx.Logging.Logger.CreateLogSource(modName);
log.LogInfo($"Loading {modGUID}");
ConfigEnableSpeakingIndicator = Config.Bind("Speaking Indicator", "EnableSpeakingIndicator", true, "Enable a voice-activity speaking indicator above player nameplates");
ConfigVariableSpeakingIndicatorOpacity = Config.Bind("Speaking Indicator", "VariableSpeakingIndicatorOpacity", true, "Speaking indicator opacity changes depending on volume");
ConfigSpeakingIndicatorAlwaysVisible = Config.Bind("Speaking Indicator", "SpeakingIndicatorAlwaysVisible", true, "Display speaking indicators even when the nameplate is hidden");
ConfigNameplateScale = Config.Bind("Nameplate", "NameplateScale", 1.5f, "Nameplate size multiplier (1.0 is the vanilla size)");
ConfigNameplateVisibilityDistance = Config.Bind("Nameplate", "NameplateVisibilityDistance", 20.0f, "Distance from the camera within which nameplates are visible (0.0 reverts to vanilla behavior). The length of the ship is ~20 units, for reference");
ConfigNameplateScaleWithDistance = Config.Bind("Nameplate", "NameplateScaleWithDistance", false, "Scale nameplates so that they retain their apparent size as they get further away");
// Plugin startup logic
harmony.PatchAll(Assembly.GetExecutingAssembly());
}
private void OnDestroy()
{
#if DEBUG
log.LogInfo($"Unloading {modGUID}");
harmony.UnpatchSelf();
foreach (var o in FindObjectsOfType<SpeakingIndicator>()) {
Destroy(o.gameObject);
}
foreach (var o in FindObjectsOfType<PlayerControllerB>()) {
o.usernameBillboard.localScale = OriginalNameplateScale.Value;
}
#endif
}
}
public class SpeakingIndicator : MonoBehaviour {
public PlayerControllerB player;
public Canvas canvas;
public GameObject canvasItem;
public CanvasGroup canvasItemAlpha;
public static Dictionary<PlayerControllerB, SpeakingIndicator> speakingIndicators = new Dictionary<PlayerControllerB, SpeakingIndicator>();
private static Texture2D speakingIconTexture = null;
public static Texture2D GetSpeakingIconTexture() {
if (speakingIconTexture != null) {
return speakingIconTexture;
}
var pttIcon = GameObject.Find("PTTIcon");
return pttIcon.GetComponent<Image>().sprite.texture;
}
// Creates a SpeakingIndicator object if it doesn't already exist for a given player
public static SpeakingIndicator GetSpeakingIndicator(PlayerControllerB player) {
speakingIndicators.TryGetValue(player, out var speakingIndicator);
if (speakingIndicator == null) {
var speakingIndicatorObject = new GameObject("SpeakingIndicator");
speakingIndicatorObject.SetActive(false);
speakingIndicator = speakingIndicatorObject.AddComponent<SpeakingIndicator>();
speakingIndicator.player = player;
speakingIndicator.transform.SetParent(player.transform, false);
speakingIndicators.Add(player, speakingIndicator);
speakingIndicatorObject.SetActive(true);
}
return speakingIndicator;
}
public void Awake() {
// Create canvas item for the speaking indicator sprite, parent it to the player nameplate
canvasItem = new GameObject("SpeakingIndicatorCanvasItem");
canvasItem.transform.SetParent(player.usernameBillboard, false);
canvasItem.AddComponent<CanvasRenderer>();
canvasItemAlpha = canvasItem.AddComponent<CanvasGroup>();
canvasItemAlpha.alpha = 0f;
canvasItem.transform.localPosition = new Vector3(0f, 60f, 0f);
var image = canvasItem.AddComponent<Image>();
image.sprite = Sprite.Create(GetSpeakingIconTexture(), new Rect(0f,0f,260f,280f), new Vector2(130f,140f), 100.0f);
}
private float lexp(float a, float b, float t) {
return Mathf.Lerp(a, b, Mathf.Exp(-t));
}
public void Update() {
VoicePlayerState playerVoice = player.voicePlayerState;
if (playerVoice != null) {
// Display speaking indicator based on voice amplitude
float detectedAmplitude = Mathf.Clamp(playerVoice.Amplitude * 35f, 0.0f, 1f);
if (Plugin.ConfigVariableSpeakingIndicatorOpacity.Value) {
// With variable opacity
if (detectedAmplitude > 0.01f) {
canvasItemAlpha.alpha = lexp(canvasItemAlpha.alpha, detectedAmplitude, Time.deltaTime*100f);
} else {
canvasItemAlpha.alpha = lexp(canvasItemAlpha.alpha, detectedAmplitude, Time.deltaTime*50f);
}
} else {
// Without variable opacity
if (detectedAmplitude > 0.05f) {
canvasItemAlpha.alpha = 1f;
} else if (detectedAmplitude < 0.01f) {
canvasItemAlpha.alpha = 0f;
}
}
}
if (!Plugin.ConfigSpeakingIndicatorAlwaysVisible.Value) {
canvasItemAlpha.alpha = Math.Min(canvasItemAlpha.alpha, player.usernameAlpha.alpha);
}
if (player.IsOwner) {
// Hide own speaking indicator
canvasItemAlpha.alpha = 0f;
}
if (!Plugin.ConfigEnableSpeakingIndicator.Value) {
Destroy(gameObject);
}
}
public void OnDestroy() {
speakingIndicators.Remove(player);
Destroy(canvasItem);
}
}
[HarmonyPatch]
public class Patches {
[HarmonyPatch(typeof(HUDManager), "UpdateSpectateBoxSpeakerIcons")]
[HarmonyPostfix]
public static void UpdateSpectateBoxSpeakerIcons(HUDManager __instance, ref Dictionary<Animator, PlayerControllerB> ___spectatingPlayerBoxes) {
// Only show spectator speaking icon when push-to-talk button is pressed
foreach (var (anim, player) in ___spectatingPlayerBoxes) {
if (player.IsOwner) {
if (IngamePlayerSettings.Instance?.settings?.pushToTalk ?? false) {
var pttPressed = IngamePlayerSettings.Instance.playerInput?.actions?.FindAction("VoiceButton", false)?.IsPressed() ?? false;
anim.SetBool("speaking", pttPressed);
}
break;
}
}
}
[HarmonyPatch(typeof(PlayerControllerB), "Update")]
[HarmonyPostfix]
public static void PlayerUpdate(PlayerControllerB __instance) {
__instance.usernameBillboardText.text = __instance.playerUsername;
// Create speaking indicators if they don't already exist
if (Plugin.ConfigEnableSpeakingIndicator.Value) {
SpeakingIndicator.GetSpeakingIndicator(__instance);
}
// Make billboard follow player's head
__instance.usernameBillboard.position = new Vector3(
__instance.playerGlobalHead.position.x,
__instance.playerGlobalHead.position.y + 0.55f,
__instance.playerGlobalHead.position.z
);
if (__instance == StartOfRound.Instance?.localPlayerController) {
// Hide own nameplate
__instance.usernameAlpha.alpha = 0f;
} else if (StartOfRound.Instance?.localPlayerController != null) {
float distance = Vector3.Distance(
StartOfRound.Instance.localPlayerController.gameplayCamera.transform.position,
__instance.usernameBillboard.position
);
if (distance < Plugin.ConfigNameplateVisibilityDistance.Value) {
__instance.usernameAlpha.alpha = 1f;
__instance.usernameBillboardText.enabled = true;
__instance.usernameCanvas.gameObject.SetActive(value: true);
}
// Nameplate scale
var distanceFactor = 1.0f;
if (Plugin.ConfigNameplateScaleWithDistance.Value) {
distanceFactor = 1.0f + Math.Max(0.0f, (distance - 4f) * 0.11f);
}
__instance.usernameBillboard.localScale = Plugin.OriginalNameplateScale.Value * Plugin.ConfigNameplateScale.Value * distanceFactor;
}
}
}