[AvatarScaleMod] i forgotr

This commit is contained in:
NotAKidoS 2023-10-03 20:26:30 -05:00
parent 2861957e3d
commit 92bbd72338
16 changed files with 544 additions and 305 deletions

View file

@ -1,71 +1,137 @@
using System.Runtime.CompilerServices;
using System.IO;
using System.Reflection;
using ABI_RC.Core.InteractionSystem;
using ABI_RC.Core.IO;
using BTKUILib;
using BTKUILib.UIObjects;
using BTKUILib.UIObjects.Components;
using MelonLoader;
using NAK.AvatarScaleMod.Integrations.BTKUI;
using NAK.AvatarScaleMod.AvatarScaling;
using UnityEngine;
namespace NAK.AvatarScaleMod.Integrations;
public static class BTKUIAddon
namespace NAK.AvatarScaleMod.Integrations
{
#region Initialization
[MethodImpl(MethodImplOptions.NoInlining)]
public static void Initialize()
public static class BtkUiAddon
{
Page as_RootPage = new(ModSettings.SettingsCategory, ModSettings.SettingsCategory, true, "")
{
MenuTitle = ModSettings.SettingsCategory,
MenuSubtitle = "Avatar Scale Mod Settings"
};
private static string _selectedPlayer;
QuickMenuAPI.OnMenuRegenerate += (_) =>
{
SchedulerSystem.AddJob((InjectMenu), 1f, 1f, 1);
};
}
#region Initialization
// private static void InjectMenu()
// {
// MelonLogger.Msg("Injecting into QM!");
// CVR_MenuManager.Instance.quickMenu.View.ExecuteScript(Scripts.GetEmbeddedScript("menu.js"));
// }
private static void InjectMenu()
{
MelonLogger.Msg("Injecting into QM!");
string menuJsPath = Path.Combine(Application.streamingAssetsPath, "Cohtml", "UIResources", "AvatarScaleMod", "menu.js");
string menuJsContent = ReadJSFile(menuJsPath);
CVR_MenuManager.Instance.quickMenu.View._view.ExecuteScript(menuJsContent);
}
private static string ReadJSFile(string path)
{
if(File.Exists(path))
public static void Initialize()
{
return File.ReadAllText(path);
PrepareIcons();
SetupRootPage();
SetupPlayerSelectPage();
RegisterEventHandlers();
}
MelonLogger.Warning($"File not found: {path}");
return string.Empty;
private static void PrepareIcons()
{
QuickMenuAPI.PrepareIcon(ModSettings.ModName, "ASM_Icon_AvatarHeightConfig", GetIconStream("ASM_Icon_AvatarHeightConfig.png"));
QuickMenuAPI.PrepareIcon(ModSettings.ModName, "ASM_Icon_AvatarHeightCopy", GetIconStream("ASM_Icon_AvatarHeightCopy.png"));
}
private static void SetupRootPage()
{
// we only need the page, as we inject our own elements into it aa
Page rootPage = new Page(ModSettings.ModName, ModSettings.SettingsCategory, true, "ASM_Icon_AvatarHeightConfig")
{
MenuTitle = ModSettings.SettingsCategory,
MenuSubtitle = "Universal Scaling Settings"
};
}
private static void SetupPlayerSelectPage()
{
// what other things would be worth adding here?
Category category = QuickMenuAPI.PlayerSelectPage.AddCategory(ModSettings.SettingsCategory, ModSettings.ModName);
Button button = category.AddButton("Copy Height", "ASM_Icon_AvatarHeightCopy", "Copy selected players Eye Height.");
button.OnPress += OnCopyPlayerHeight;
}
private static void RegisterEventHandlers()
{
QuickMenuAPI.OnPlayerSelected += (_, id) => _selectedPlayer = id;
QuickMenuAPI.OnMenuRegenerate += _ => ScheduleMenuInjection();
QuickMenuAPI.OnTabChange += OnTabChange;
}
private static void ScheduleMenuInjection()
{
CVR_MenuManager.Instance.quickMenu.View.BindCall("asm-AvatarHeightUpdated", new Action<float>(OnAvatarHeightUpdated));
SchedulerSystem.AddJob(InjectMenu, 1f, 1f, 1);
}
private static void InjectMenu()
{
AvatarScaleMod.Logger.Msg("Injecting into our BTKUI AvatarScaleMod page!");
string menuJsPath = Path.Combine(Application.streamingAssetsPath, "Cohtml", "UIResources", "AvatarScaleMod", "menu.js");
string menuJsContent = File.Exists(menuJsPath) ? File.ReadAllText(menuJsPath) : string.Empty;
if (string.IsNullOrEmpty(menuJsContent))
{
AvatarScaleMod.Logger.Msg("Injecting embedded menu.js included with mod!");
CVR_MenuManager.Instance.quickMenu.View._view.ExecuteScript(Scripts.GetEmbeddedScript("menu.js"));
}
else
{
AvatarScaleMod.Logger.Msg($"Injecting development menu.js found in: {menuJsPath}");
CVR_MenuManager.Instance.quickMenu.View._view.ExecuteScript(menuJsContent);
}
}
#endregion
private static void OnCopyPlayerHeight()
{
float networkHeight = AvatarScaleManager.Instance.GetNetworkHeight(_selectedPlayer);
if (networkHeight < 0) return;
AvatarScaleManager.Instance.SetHeight(networkHeight);
}
private static void OnAvatarHeightUpdated(float height)
{
AvatarScaleManager.Instance.SetHeight(height);
}
#region Private Methods
private static DateTime lastTime = DateTime.Now;
private static void OnTabChange(string newTab, string previousTab)
{
if (newTab == "btkUI-AvatarScaleMod-MainPage")
{
TimeSpan timeDifference = DateTime.Now - lastTime;
if (timeDifference.TotalSeconds <= 0.5)
{
AvatarScaleManager.Instance.ResetHeight();
return;
}
}
lastTime = DateTime.Now;
}
private static Stream GetIconStream(string iconName)
{
Assembly assembly = Assembly.GetExecutingAssembly();
string assemblyName = assembly.GetName().Name;
return assembly.GetManifestResourceStream($"{assemblyName}.resources.{iconName}");
}
#endregion
#region Melon Pref Helpers
internal static void AddMelonToggle(ref Category category, MelonPreferences_Entry<bool> entry)
{
category.AddToggle(entry.DisplayName, entry.Description, entry.Value).OnValueUpdated += b => entry.Value = b;
}
internal static void AddMelonSlider(ref Page page, MelonPreferences_Entry<float> entry, float min, float max, int decimalPlaces = 2)
{
page.AddSlider(entry.DisplayName, entry.Description, entry.Value, min, max, decimalPlaces).OnValueUpdated += f => entry.Value = f;
}
#endregion
}
#endregion
#region Melon Pref Helpers
internal static void AddMelonToggle(ref Category category, MelonPreferences_Entry<bool> entry)
{
category.AddToggle(entry.DisplayName, entry.Description, entry.Value).OnValueUpdated += b => entry.Value = b;
}
internal static void AddMelonSlider(ref Page page, MelonPreferences_Entry<float> entry, float min, float max, int decimalPlaces = 2)
{
page.AddSlider(entry.DisplayName, entry.Description, entry.Value, min, max, decimalPlaces).OnValueUpdated += f => entry.Value = f;
}
#endregion
}

View file

@ -1,87 +0,0 @@
using ABI_RC.Systems.Camera;
using BTKUILib;
using BTKUILib.UIObjects;
using MelonLoader;
using UnityEngine;
namespace NAK.AvatarScaleMod.Integrations.BTKUI;
public class PortableCameraCategory
{
private static string categoryName = "Portable Camera";
internal static void AddCategory(Page parent)
{
QuickMenuAPI.OnTabChange += OnTabChange;
// Create category and add elements to it
var category = parent.AddCategory(categoryName);
category.AddButton("Take Photo", "TakePhoto-Icon", "Quickly take a photo. This respects set timers & other related settings.").OnPress += TakePhoto;
category.AddButton("Cycle Delay", "CycleDelay-Icon", "Quickly cycle photo timers. Off, 3s, 5s, 10s.").OnPress += CycleCaptureDelay;
category.AddButton("Open Folder", "OpenFolder-Icon", "Quickly open the root of the ChilloutVR screenshots folder in Windows Explorer.").OnPress += OpenScreenshotsFolder;
// Clone of the default camera settings page
var settingsPage = category.AddPage("Settings", "Settings-Icon", "Sub page of settings to configure the portable camera.", parent.MenuTitle);
settingsPage.AddCategory("Main Settings");
settingsPage.AddSlider("Field of View", "Field of View of portable camera.", 40f, 10f, 120f);
settingsPage.AddSlider("Focal Length", "Focal Length of portable camera.", 50f, 24f, 200f);
settingsPage.AddSlider("Aperture", "Aperture of portable camera.", 1.8f, 1.2f, 8f);
}
private static bool PortableCameraReady()
{
bool active = (bool)(PortableCamera.Instance?.IsActive());
if (!active) CVRCamController.Instance?.Toggle();
return active;
}
private static void TakePhoto()
{
MelonLogger.Msg("Took photo!");
if (PortableCameraReady())
PortableCamera.Instance?.MakePhoto();
}
private static void CycleCaptureDelay()
{
if (PortableCameraReady())
{
PortableCamera.Instance?.ChangeCameraCaptureDelay();
QuickMenuAPI.ShowAlertToast("Delay set to " + PortableCamera.Instance.timerText.text, 1);
}
}
//this was mistake, but now feature cause fuck it
private static void PauseCamera()
{
MelonLogger.Msg("Paused camera!");
GameObject camera = PortableCamera.Instance.gameObject;
PortableCamera.Instance.gameObject.SetActive(!camera.activeSelf);
}
private static void OpenScreenshotsFolder()
{
MelonLogger.Msg("Opened screenshots folder!");
string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "ChilloutVR");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
Application.OpenURL("file:///" + path);
}
private static DateTime lastTime = DateTime.Now;
private static void OnTabChange(string newTab, string previousTab)
{
if (newTab == "btkUI-AvatarScaleMod-MainPage")
{
TimeSpan timeDifference = DateTime.Now - lastTime;
if (timeDifference.TotalSeconds <= 0.5)
{
// The new page and previous page are equal and were opened within 0.5 seconds of each other
CVRCamController.Instance?.Toggle();
}
}
lastTime = DateTime.Now;
}
}