mirror of
https://github.com/NotAKidoS/NAK_CVR_Mods.git
synced 2025-09-02 14:29:25 +00:00
[AvatarScaleMod] i forgotr
This commit is contained in:
parent
2861957e3d
commit
92bbd72338
16 changed files with 544 additions and 305 deletions
|
@ -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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue