Skip to content

Commit

Permalink
Add laser pointer colours and stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
kodenamekrak committed Jan 7, 2023
1 parent 03f35ad commit 29d3c7b
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 31 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# QonsistentSaberColors

description
Menu saber colors reflect in game saber colors.

## Credits

Expand Down
10 changes: 6 additions & 4 deletions include/ColorManager.hpp
Original file line number Diff line number Diff line change
@@ -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);
}
1 change: 1 addition & 0 deletions include/ModConfig.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@

DECLARE_CONFIG(ModConfig,
CONFIG_VALUE(Enabled, bool, "Enabled", true);
CONFIG_VALUE(ColoredLasers, bool, "Colored lasers", true);
)
93 changes: 77 additions & 16 deletions src/ColorManager.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "main.hpp"
#include "ModConfig.hpp"
#include "ColorManager.hpp"

#include "GlobalNamespace/VRController.hpp"
Expand All @@ -13,24 +14,34 @@
#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;

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<GlobalNamespace::PlayerDataModel*>().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;
}
Expand All @@ -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<GlobalNamespace::VRController*>();
leftController = controllers[1];
rightController = controllers[0];
}

void SetColors()
{
GetColorScheme();
SetColors(leftSaberColor, rightSaberColor);
colorSchemesSettings = UnityEngine::Resources::FindObjectsOfTypeAll<GlobalNamespace::PlayerDataModel*>()[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<GlobalNamespace::SetSaberGlowColor*>();
for(auto leftGlow : leftGlowColors)
Expand Down Expand Up @@ -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<UnityEngine::MeshRenderer*>();
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);
}
}
26 changes: 25 additions & 1 deletion src/SettingsViewController.cpp
Original file line number Diff line number Diff line change
@@ -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)
{}
{
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();
});
}
70 changes: 61 additions & 9 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<UnityEngine::MeshRenderer*>();
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;
}
Expand Down Expand Up @@ -81,9 +132,10 @@ extern "C" void load() {
getModConfig().Init(modInfo);

QuestUI::Init();
QuestUI::Register::RegisterModSettingsViewController<QonsistentSaberColors::SettingsViewController*>(modInfo, "QonsistentSaberColors");
QuestUI::Register::RegisterAllModSettingsViewController<QonsistentSaberColors::SettingsViewController*>(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);
Expand Down

0 comments on commit 29d3c7b

Please sign in to comment.