|
| 1 | +using UnityEngine; |
| 2 | +using Il2CppSystem.Collections.Generic; |
| 3 | + |
| 4 | +namespace MalumMenu; |
| 5 | + |
| 6 | +public class ProtectUI : MonoBehaviour |
| 7 | +{ |
| 8 | + private Vector2 _scrollPosition = Vector2.zero; |
| 9 | + private Rect _windowRect = new(320, 10, 500, 300); |
| 10 | + public static List<PlayerControl> playersToProtect = new(); |
| 11 | + private bool _keepEveryoneProtected; |
| 12 | + |
| 13 | + private void OnGUI() |
| 14 | + { |
| 15 | + if (!CheatToggles.showProtectMenu) return; |
| 16 | + |
| 17 | + if(ColorUtility.TryParseHtmlString(MalumMenu.menuHtmlColor.Value, out var configUIColor)) |
| 18 | + { |
| 19 | + GUI.backgroundColor = configUIColor; |
| 20 | + } |
| 21 | + |
| 22 | + _windowRect = GUI.Window(5, _windowRect, (GUI.WindowFunction)ProtectWindow, "Protect Players"); |
| 23 | + } |
| 24 | + |
| 25 | + private void ProtectWindow(int windowID) |
| 26 | + { |
| 27 | + GUILayout.BeginVertical(); |
| 28 | + _scrollPosition = GUILayout.BeginScrollView(_scrollPosition, false, true); |
| 29 | + |
| 30 | + foreach (var pc in playersToProtect) |
| 31 | + { |
| 32 | + if (!pc.Data || !pc.Data.Role || string.IsNullOrEmpty(pc.Data.PlayerName)) |
| 33 | + { |
| 34 | + playersToProtect.Remove(pc); // Ensure to remove invalid players from the list |
| 35 | + break; // Exit the loop to avoid modifying the collection during iteration |
| 36 | + } |
| 37 | + } |
| 38 | + foreach (var pc in PlayerControl.AllPlayerControls) |
| 39 | + { |
| 40 | + if (!pc.Data || !pc.Data.Role || string.IsNullOrEmpty(pc.Data.PlayerName)) |
| 41 | + { |
| 42 | + if (playersToProtect.Contains(pc)) // Ensure to remove invalid players from the list |
| 43 | + { |
| 44 | + playersToProtect.Remove(pc); |
| 45 | + } |
| 46 | + continue; |
| 47 | + } |
| 48 | + |
| 49 | + GUILayout.BeginHorizontal(); |
| 50 | + GUILayout.Label($"<color=#{ColorUtility.ToHtmlStringRGB(pc.Data.Color)}>{pc.name}</color>", GUILayout.Width(140f)); |
| 51 | + GUILayout.Label($"{(pc.protectedByGuardianId != -1 ? $"<color=#00FF00>Protected</color> by <color=#{ColorUtility.ToHtmlStringRGB(GameData.Instance.GetPlayerById((byte)pc.protectedByGuardianId).Color)}>{GameData.Instance.GetPlayerById((byte)pc.protectedByGuardianId)._object.name}</color>" : "<color=#FF0000>Unprotected</color>")}", GUILayout.Width(135)); |
| 52 | + if (GUILayout.Button("Protect", GUIStyles.NormalButtonStyle) && Utils.isHost && !Utils.isLobby) |
| 53 | + { |
| 54 | + PlayerControl.LocalPlayer.RpcProtectPlayer(pc, pc.cosmetics.ColorId); |
| 55 | + } |
| 56 | + |
| 57 | + var keepProtected = playersToProtect.Contains(pc); |
| 58 | + keepProtected = GUILayout.Toggle(keepProtected, "Keep protected", GUIStyles.NormalToggleStyle); |
| 59 | + if (keepProtected && !playersToProtect.Contains(pc)) |
| 60 | + playersToProtect.Add(pc); |
| 61 | + else if (!keepProtected && playersToProtect.Contains(pc)) |
| 62 | + playersToProtect.Remove(pc); |
| 63 | + |
| 64 | + GUILayout.EndHorizontal(); |
| 65 | + } |
| 66 | + |
| 67 | + GUILayout.EndScrollView(); |
| 68 | + |
| 69 | + GUILayout.BeginHorizontal(); |
| 70 | + if (GUILayout.Button("Protect Everyone") && Utils.isHost && !Utils.isLobby) |
| 71 | + { |
| 72 | + foreach (var pc in PlayerControl.AllPlayerControls) |
| 73 | + { |
| 74 | + PlayerControl.LocalPlayer.RpcProtectPlayer(pc, pc.cosmetics.ColorId); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + GUILayout.FlexibleSpace(); |
| 79 | + |
| 80 | + _keepEveryoneProtected = GUILayout.Toggle(_keepEveryoneProtected, "Keep Everyone Protected"); |
| 81 | + if (_keepEveryoneProtected) |
| 82 | + { |
| 83 | + foreach (var pc in PlayerControl.AllPlayerControls) |
| 84 | + { |
| 85 | + if (!playersToProtect.Contains(pc)) |
| 86 | + { |
| 87 | + playersToProtect.Add(pc); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + else |
| 92 | + { |
| 93 | + if (PlayerControl.AllPlayerControls.Count == playersToProtect.Count) // Only clear the list if all players were being kept protected |
| 94 | + { |
| 95 | + playersToProtect.Clear(); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + GUILayout.EndHorizontal(); |
| 100 | + GUILayout.EndVertical(); |
| 101 | + GUI.DragWindow(); |
| 102 | + } |
| 103 | +} |
0 commit comments