diff --git a/README.md b/README.md index 70571e1..335c534 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # QonsistentSaberColors -description +Menu saber colors reflect in game saber colors. ## Credits diff --git a/include/ColorManager.hpp b/include/ColorManager.hpp index ed64b5c..be60dc4 100644 --- a/include/ColorManager.hpp +++ b/include/ColorManager.hpp @@ -1,11 +1,13 @@ #include "UnityEngine/Color.hpp" +#include "VRUIControls/VRLaserPointer.hpp" namespace QonsistentSaberColors { - void GetControllers(); + void UpdatePointers(); - void SetColors(); - void SetColors(UnityEngine::Color leftColor, UnityEngine::Color rightColor); + void UpdateControllerColors(); - void ResetColors(); + void UpdateLaserColor(); + + void SetLaser(VRUIControls::VRLaserPointer* laser); } \ No newline at end of file diff --git a/include/ModConfig.hpp b/include/ModConfig.hpp index a0f3da4..b950a0b 100644 --- a/include/ModConfig.hpp +++ b/include/ModConfig.hpp @@ -5,4 +5,5 @@ DECLARE_CONFIG(ModConfig, CONFIG_VALUE(Enabled, bool, "Enabled", true); + CONFIG_VALUE(ColoredLasers, bool, "Colored lasers", true); ) \ No newline at end of file diff --git a/src/ColorManager.cpp b/src/ColorManager.cpp index d1cd017..c41ec15 100644 --- a/src/ColorManager.cpp +++ b/src/ColorManager.cpp @@ -1,4 +1,5 @@ #include "main.hpp" +#include "ModConfig.hpp" #include "ColorManager.hpp" #include "GlobalNamespace/VRController.hpp" @@ -13,6 +14,11 @@ #include "UnityEngine/Resources.hpp" #include "UnityEngine/GameObject.hpp" #include "UnityEngine/Transform.hpp" +#include "UnityEngine/Material.hpp" +#include "UnityEngine/MeshRenderer.hpp" + +#include "VRUIControls/VRLaserPointer.hpp" +#include "VRUIControls/VRPointer.hpp" GlobalNamespace::VRController* leftController; GlobalNamespace::VRController* rightController; @@ -20,17 +26,22 @@ GlobalNamespace::VRController* rightController; UnityEngine::Color leftSaberColor; UnityEngine::Color rightSaberColor; +VRUIControls::VRLaserPointer* laserPointer; + +GlobalNamespace::ColorSchemesSettings* colorSchemesSettings; + const UnityEngine::Color defaultLeftColor {0.784314, 0.078431, 0.078431, 1.000000}; const UnityEngine::Color defaultRightColor{0.156863, 0.556863, 0.823529, 1.000000}; +const UnityEngine::Color defaultLaserColor{0.125490f, 0.752941f, 1.000000f, 0.501961f}; namespace QonsistentSaberColors { - void GetColorScheme() + void UpdateColors() { - auto colorSchemeSettings = UnityEngine::Resources::FindObjectsOfTypeAll().First()->playerData->colorSchemesSettings; - if(colorSchemeSettings->overrideDefaultColors) + getLogger().info("Updating colors"); + if(colorSchemesSettings->overrideDefaultColors && getModConfig().Enabled.GetValue()) { - auto scheme = colorSchemeSettings->GetSelectedColorScheme(); + auto scheme = colorSchemesSettings->GetSelectedColorScheme(); leftSaberColor = scheme->saberAColor; rightSaberColor = scheme->saberBColor; } @@ -41,23 +52,25 @@ namespace QonsistentSaberColors } } - void GetControllers() + void UpdatePointers() { - getLogger().info("Finding VRControllers via resources"); + getLogger().info("Updating neccessary pointers"); auto controllers = UnityEngine::Resources::FindObjectsOfTypeAll(); leftController = controllers[1]; rightController = controllers[0]; - } - void SetColors() - { - GetColorScheme(); - SetColors(leftSaberColor, rightSaberColor); + colorSchemesSettings = UnityEngine::Resources::FindObjectsOfTypeAll()[0]->playerData->colorSchemesSettings; } - void SetColors(UnityEngine::Color leftColor, UnityEngine::Color rightColor) + + void SetControllerColors(UnityEngine::Color leftColor, UnityEngine::Color rightColor) { - getLogger().info("Setting controller colors"); + getLogger().info("SetControllerColors()"); + if(!leftController->m_CachedPtr.m_value || !leftController || !rightController->m_CachedPtr.m_value || !rightController) + { + getLogger().info("One of the controllers was null, updating pointers"); + UpdatePointers(); + } auto leftGlowColors = leftController->GetComponentsInChildren(); for(auto leftGlow : leftGlowColors) @@ -95,11 +108,59 @@ namespace QonsistentSaberColors glowColor->colorManager->colorScheme->saberBColor = rightColor; glowColor->SetColors(); } - getLogger().info("Done setting colors"); } - void ResetColors() + void SetLaserColor(VRUIControls::VRLaserPointer* pointer) + { + getLogger().info("SetLaserColor()"); + if(!laserPointer || !laserPointer->m_CachedPtr.m_value) + { + getLogger().info("laserPointer was null, not setting color"); + return; + } + UnityEngine::Color color; + if(getModConfig().ColoredLasers.GetValue() && getModConfig().Enabled.GetValue()) + { + auto parent = pointer->get_transform()->get_parent()->get_name(); + parent == "ControllerLeft" ? color = leftSaberColor : color = rightSaberColor; + } + else + color = defaultLaserColor; + color.a = 0; + + auto renderer = laserPointer->GetComponentInChildren(); + auto matArray = renderer->GetMaterialArray(); + for(auto mat: matArray) + { + mat->set_color(color); + } + } + + void SetLaser(VRUIControls::VRLaserPointer* laser) + { + getLogger().info("SetLaser()"); + laserPointer = laser; + } + + void UpdateControllerColors() { - SetColors(defaultLeftColor, defaultRightColor); + getLogger().info("UpdateControllerColors()"); + UpdateColors(); + SetControllerColors(leftSaberColor, rightSaberColor); + } + + void UpdateLaserColor() + { + getLogger().info("UpdateLaserColor"); + if(laserPointer) + getLogger().info("laserPointer is valid"); + else + { + getLogger().info("laserPointer is null"); + return; + } + + UpdateColors(); + SetLaserColor(laserPointer); } } \ No newline at end of file diff --git a/src/SettingsViewController.cpp b/src/SettingsViewController.cpp index d1660c6..28334a3 100644 --- a/src/SettingsViewController.cpp +++ b/src/SettingsViewController.cpp @@ -1,7 +1,31 @@ #include "main.hpp" #include "SettingsViewController.hpp" +#include "ColorManager.hpp" +#include "ModConfig.hpp" + +#include "questui/shared/BeatSaberUI.hpp" DEFINE_TYPE(QonsistentSaberColors, SettingsViewController) void QonsistentSaberColors::SettingsViewController::DidActivate(bool firstActivation, bool addedToHierarchy, bool screenSystemEnabling) -{} \ No newline at end of file +{ + if(!firstActivation) + return; + + auto transform = QuestUI::BeatSaberUI::CreateScrollableSettingsContainer(get_transform())->get_transform(); + + QuestUI::BeatSaberUI::CreateToggle(transform, "Enabled", getModConfig().Enabled.GetValue(), [](bool value) + { + getLogger().info("Enabled toggle"); + getModConfig().Enabled.SetValue(value); + QonsistentSaberColors::UpdateControllerColors(); + QonsistentSaberColors::UpdateLaserColor(); + }); + + QuestUI::BeatSaberUI::CreateToggle(transform, "Colored lasers", getModConfig().ColoredLasers.GetValue(), [](bool value) + { + getLogger().info("Lasers toggle"); + getModConfig().ColoredLasers.SetValue(value); + QonsistentSaberColors::UpdateLaserColor(); + }); +} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 5bbcd9d..c497733 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,46 +10,97 @@ #include "UnityEngine/SceneManagement/Scene.hpp" #include "UnityEngine/SceneManagement/SceneManager.hpp" +#include "VRUIControls/VRPointer.hpp" +#include "VRUIControls/VRLaserPointer.hpp" + + +#include "UnityEngine/Resources.hpp" +#include "UnityEngine/GameObject.hpp" +#include "UnityEngine/Transform.hpp" +#include "UnityEngine/Material.hpp" +#include "UnityEngine/MeshRenderer.hpp" + static ModInfo modInfo; using namespace QonsistentSaberColors; +MAKE_HOOK_MATCH(VRPointer_CreateLaserPointerAndLaserHit, &VRUIControls::VRPointer::CreateLaserPointerAndLaserHit, void, VRUIControls::VRPointer* self) +{ + getLogger().info("VRPointer_CreateLaserPointerAndLaserHit"); + VRPointer_CreateLaserPointerAndLaserHit(self); + + auto mr = self->laserPointer->GetComponentInChildren(); + auto arr = mr->GetMaterialArray(); + for(int i = 0; i < arr.Length(); i++) + { + auto col = arr[i]->get_color(); + getLogger().info("Color for index %i is R:%f, G:%f, B:%f, A:%f", i, col.r, col.g, col.b, col.a); + } + + SetLaser(self->laserPointer); + if(getModConfig().ColoredLasers.GetValue() && getModConfig().Enabled.GetValue()) + UpdateLaserColor(); +} + MAKE_HOOK_MATCH(MainMenuViewController_DidActivate, &GlobalNamespace::MainMenuViewController::DidActivate, void, GlobalNamespace::MainMenuViewController* self, bool a, bool b, bool c) { + getLogger().info("MainMenuViewController_DidActivate"); MainMenuViewController_DidActivate(self, a, b, c); if(getModConfig().Enabled.GetValue()) - SetColors(); - else - ResetColors(); + { + UpdateControllerColors(); + if(getModConfig().ColoredLasers.GetValue()) + UpdateLaserColor(); + } } MAKE_HOOK_MATCH(ColorsOverrideSettingsPanelController_HandleOverrideColorsToggleValueChanged, &GlobalNamespace::ColorsOverrideSettingsPanelController::HandleOverrideColorsToggleValueChanged, void, GlobalNamespace::ColorsOverrideSettingsPanelController* self, bool isOn) { + getLogger().info("ColorsOverrideSettingsPanelController_HandleOverrideColorsToggleValueChanged"); ColorsOverrideSettingsPanelController_HandleOverrideColorsToggleValueChanged(self, isOn); if(getModConfig().Enabled.GetValue()) - SetColors(); + { + UpdateControllerColors(); + if(getModConfig().ColoredLasers.GetValue()) + UpdateLaserColor(); + } } MAKE_HOOK_MATCH(ColorsOverrideSettingsPanelController_HandleEditColorSchemeControllerDidChangeColorScheme, &GlobalNamespace::ColorsOverrideSettingsPanelController::HandleEditColorSchemeControllerDidChangeColorScheme, void, GlobalNamespace::ColorsOverrideSettingsPanelController* self, GlobalNamespace::ColorScheme* colorScheme) { + getLogger().info("ColorsOverrideSettingsPanelController_HandleEditColorSchemeControllerDidChangeColorScheme"); ColorsOverrideSettingsPanelController_HandleEditColorSchemeControllerDidChangeColorScheme(self, colorScheme); if(getModConfig().Enabled.GetValue()) - SetColors(); + { + UpdateControllerColors(); + if(getModConfig().ColoredLasers.GetValue()) + UpdateLaserColor(); + } } MAKE_HOOK_MATCH(ColorsOverrideSettingsPanelController_HandleDropDownDidSelectCellWithIdx, &GlobalNamespace::ColorsOverrideSettingsPanelController::HandleDropDownDidSelectCellWithIdx, void, GlobalNamespace::ColorsOverrideSettingsPanelController* self, HMUI::DropdownWithTableView* dropDownWithTableView, int idx) { + getLogger().info("ColorsOverrideSettingsPanelController_HandleDropDownDidSelectCellWithIdx"); ColorsOverrideSettingsPanelController_HandleDropDownDidSelectCellWithIdx(self, dropDownWithTableView, idx); if(getModConfig().Enabled.GetValue()) - SetColors(); + { + UpdateControllerColors(); + if(getModConfig().ColoredLasers.GetValue()) + UpdateLaserColor(); + } } MAKE_HOOK_MATCH(SceneManager_SetActiveScene, &UnityEngine::SceneManagement::SceneManager::SetActiveScene, bool, UnityEngine::SceneManagement::Scene scene) { + getLogger().info("SceneManager_SetActiveScene"); bool val = SceneManager_SetActiveScene(scene); - GetControllers(); + UpdatePointers(); if(getModConfig().Enabled.GetValue() && scene.get_name() == "HealthWarning") - SetColors(); + { + UpdateControllerColors(); + if(getModConfig().ColoredLasers.GetValue()) + UpdateLaserColor(); + } return val; } @@ -81,9 +132,10 @@ extern "C" void load() { getModConfig().Init(modInfo); QuestUI::Init(); - QuestUI::Register::RegisterModSettingsViewController(modInfo, "QonsistentSaberColors"); + QuestUI::Register::RegisterAllModSettingsViewController(modInfo, "QonsistentSaberColors"); getLogger().info("Installing hooks..."); + INSTALL_HOOK(getLogger(), VRPointer_CreateLaserPointerAndLaserHit); INSTALL_HOOK(getLogger(), MainMenuViewController_DidActivate); INSTALL_HOOK(getLogger(), ColorsOverrideSettingsPanelController_HandleOverrideColorsToggleValueChanged); INSTALL_HOOK(getLogger(), ColorsOverrideSettingsPanelController_HandleEditColorSchemeControllerDidChangeColorScheme);