Skip to content

Commit

Permalink
Refactor InWorldPanelContainer
Browse files Browse the repository at this point in the history
  • Loading branch information
danielkrupinski committed Aug 23, 2024
1 parent 9541915 commit 19d4a4d
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 47 deletions.
1 change: 0 additions & 1 deletion Source/FeatureHelpers/FeatureHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@
#include "WorldToClipSpaceConverter.h"

struct FeatureHelpers {
HudInWorldPanelContainer hudInWorldPanelContainer;
SoundWatcherState soundWatcherState;
};
60 changes: 37 additions & 23 deletions Source/FeatureHelpers/HudInWorldPanelContainer.h
Original file line number Diff line number Diff line change
@@ -1,43 +1,57 @@
#pragma once

#include <GameClasses/Hud/Hud.h>
#include <GameClasses/PanelHandle.h>
#include <GameClasses/PanoramaUiPanel.h>
#include <Helpers/PanoramaPanelPointer.h>

class HudInWorldPanelContainer {
public:
HudInWorldPanelContainer() = default;
HudInWorldPanelContainer(const HudInWorldPanelContainer&) = delete;
HudInWorldPanelContainer(HudInWorldPanelContainer&&) = delete;
HudInWorldPanelContainer& operator=(const HudInWorldPanelContainer&) = delete;
HudInWorldPanelContainer& operator=(HudInWorldPanelContainer&&) = delete;
struct InWorldPanelContainerState {
cs2::PanelHandle containerPanelHandle;
};

~HudInWorldPanelContainer() noexcept
template <typename HookContext>
struct InWorldPanelContainerUnloadHandler {
explicit InWorldPanelContainerUnloadHandler(HookContext& hookContext) noexcept
: hookContext{hookContext}
{
if (containerPanel.getHandle().isValid())
PanoramaUiEngine::onDeletePanel(containerPanel.getHandle());
}

template <typename Dependencies>
[[nodiscard]] auto get(Hud<Dependencies> hud, auto& hookContext) noexcept
void handleUnload() const noexcept
{
if (const auto container = containerPanel.get())
return PanoramaUiPanel{PanoramaUiPanelContext{hookContext, container}};
return createPanel(hud, hookContext);
hookContext.panels().deletePanelByHandle(hookContext.inWorldPanelContainerState().containerPanelHandle);
}

private:
template <typename Dependencies>
[[nodiscard]] auto createPanel(Hud<Dependencies> hud, auto& hookContext) noexcept
HookContext& hookContext;
};

template <typename HookContext>
class InWorldPanelContainer {
public:
explicit InWorldPanelContainer(HookContext& hookContext) noexcept
: hookContext{hookContext}
{
}

[[nodiscard]] auto get() noexcept
{
return handle().getOrInit(createContainerPanel());
}

private:
[[nodiscard]] decltype(auto) handle() const noexcept
{
return hookContext.template make<PanelHandle>(hookContext.inWorldPanelContainerState().containerPanelHandle);
}

[[nodiscard]] auto createContainerPanel() const noexcept
{
if (const auto hudReticle = hud.getHudReticle()) {
auto&& panel = hookContext.panelFactory().createPanel(hudReticle).uiPanel();
return [this] {
auto&& panel = hookContext.panelFactory().createPanel(hookContext.hud().getHudReticle()).uiPanel();
panel.fitParent();
containerPanel.handle = panel.getHandle();
return panel;
}
return PanoramaUiPanel{PanoramaUiPanelContext{hookContext, nullptr}};
};
}

PanoramaPanelPointer containerPanel;
HookContext& hookContext;
};
14 changes: 4 additions & 10 deletions Source/Features/Sound/Details/SoundVisualizationFeature.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,11 @@ struct SoundVisualizationFeatureToggle : FeatureToggleOnOff<SoundVisualizationFe
SoundVisualizationFeatureToggle(SoundVisualizationFeatureState& state,
HookDependencies& hookDependencies,
SoundWatcher soundWatcher,
ViewRenderHook& viewRenderHook,
HudInWorldPanelContainer& hudInWorldPanelContainer)
ViewRenderHook& viewRenderHook)
: state{state}
, hookDependencies{hookDependencies}
, soundWatcher{soundWatcher}
, viewRenderHook{viewRenderHook}
, hudInWorldPanelContainer{hudInWorldPanelContainer}
{
}

Expand All @@ -55,7 +53,7 @@ struct SoundVisualizationFeatureToggle : FeatureToggleOnOff<SoundVisualizationFe
{
viewRenderHook.decrementReferenceCount();
soundWatcher.stopWatching<SoundType>();
if (const auto containerPanel{hudInWorldPanelContainer.get(hookDependencies.hud(), hookDependencies)}) {
if (const auto containerPanel{hookDependencies.make<InWorldPanelContainer>().get()}) {
if (const auto containerPanelChildren{containerPanel.children().vector})
state.hideRemainingPanels(hookDependencies, HudInWorldPanels{*containerPanelChildren}, 0);
}
Expand All @@ -66,7 +64,6 @@ struct SoundVisualizationFeatureToggle : FeatureToggleOnOff<SoundVisualizationFe
HookDependencies& hookDependencies;
SoundWatcher soundWatcher;
ViewRenderHook& viewRenderHook;
HudInWorldPanelContainer& hudInWorldPanelContainer;
};

template <typename PanelsType, typename SoundType>
Expand All @@ -76,13 +73,11 @@ class SoundVisualizationFeature {
SoundVisualizationFeatureState& state,
HookDependencies& hookDependencies,
ViewRenderHook& viewRenderHook,
SoundWatcher soundWatcher,
HudInWorldPanelContainer& hudInWorldPanelContainer) noexcept
SoundWatcher soundWatcher) noexcept
: state{state}
, hookDependencies{hookDependencies}
, viewRenderHook{viewRenderHook}
, soundWatcher{soundWatcher}
, hudInWorldPanelContainer{hudInWorldPanelContainer}
{
}

Expand All @@ -99,7 +94,7 @@ class SoundVisualizationFeature {
if (!curtime)
return;

const auto containerPanel{hudInWorldPanelContainer.get(hookDependencies.hud(), hookDependencies)};
const auto containerPanel{hookDependencies.make<InWorldPanelContainer>().get()};
if (!containerPanel)
return;

Expand Down Expand Up @@ -164,5 +159,4 @@ class SoundVisualizationFeature {
HookDependencies& hookDependencies;
ViewRenderHook& viewRenderHook;
SoundWatcher soundWatcher;
HudInWorldPanelContainer& hudInWorldPanelContainer;
};
2 changes: 0 additions & 2 deletions Source/Features/Sound/SoundFeatures.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ struct SoundFeatures {
hookDependencies,
viewRenderHook,
SoundWatcher{helpers.soundWatcherState, hookDependencies},
helpers.hudInWorldPanelContainer
};
}

Expand All @@ -106,7 +105,6 @@ struct SoundFeatures {
hookDependencies,
SoundWatcher{helpers.soundWatcherState, hookDependencies},
viewRenderHook,
helpers.hudInWorldPanelContainer
};
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,9 @@ using HostageRescueIconToggle = PlayerStateIconToggle<HostageRescuePanel>;
using BlindedIconToggle = PlayerStateIconToggle<BlindedIconPanel>;

struct PlayerInformationThroughWallsToggle : FeatureToggleOnOff<PlayerInformationThroughWallsToggle> {
PlayerInformationThroughWallsToggle(PlayerInformationThroughWallsState& state, HookDependencies& dependencies, HudInWorldPanelContainer& hudInWorldPanelContainer, ViewRenderHook& viewRenderHook) noexcept
PlayerInformationThroughWallsToggle(PlayerInformationThroughWallsState& state, HookDependencies& dependencies, ViewRenderHook& viewRenderHook) noexcept
: state{state}
, dependencies{dependencies}
, hudInWorldPanelContainer{hudInWorldPanelContainer}
, viewRenderHook{viewRenderHook}
{
}
Expand Down Expand Up @@ -174,7 +173,7 @@ struct PlayerInformationThroughWallsToggle : FeatureToggleOnOff<PlayerInformatio
{
viewRenderHook.decrementReferenceCount();

if (const auto containerPanel{hudInWorldPanelContainer.get(dependencies.hud(), dependencies)}) {
if (const auto containerPanel{dependencies.make<InWorldPanelContainer>().get()}) {
if (const auto containerPanelChildren{containerPanel.children().vector})
hideRemainingPanels(HudInWorldPanels{*containerPanelChildren}, 0);
}
Expand All @@ -192,7 +191,6 @@ struct PlayerInformationThroughWallsToggle : FeatureToggleOnOff<PlayerInformatio

PlayerInformationThroughWallsState& state;
HookDependencies& dependencies;
HudInWorldPanelContainer& hudInWorldPanelContainer;
ViewRenderHook& viewRenderHook;
};

Expand Down Expand Up @@ -240,7 +238,7 @@ class PlayerInformationThroughWalls {
if (!positionInClipSpace.onScreen())
return;

const auto containerPanel{dependencies.getFeatureHelpers().hudInWorldPanelContainer.get(dependencies.hud(), dependencies)};
const auto containerPanel{dependencies.make<InWorldPanelContainer>().get()};
if (!containerPanel)
return;

Expand Down Expand Up @@ -290,7 +288,7 @@ class PlayerInformationThroughWalls {
if (!requestCrucialDependencies())
return;

const auto containerPanel{dependencies.getFeatureHelpers().hudInWorldPanelContainer.get(dependencies.hud(), dependencies)};
const auto containerPanel{dependencies.make<InWorldPanelContainer>().get()};
if (!containerPanel)
return;

Expand Down
2 changes: 1 addition & 1 deletion Source/Features/Visuals/VisualFeatures.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class LoopModeGameHook;
struct VisualFeatures {
[[nodiscard]] PlayerInformationThroughWallsToggle playerInformationThroughWalls() const noexcept
{
return PlayerInformationThroughWallsToggle{states.playerInformationThroughWallsState, hookDependencies, helpers.hudInWorldPanelContainer, viewRenderHook};
return PlayerInformationThroughWallsToggle{states.playerInformationThroughWallsState, hookDependencies, viewRenderHook};
}

[[nodiscard]] PlayerPositionToggle playerPositionToggle() const noexcept
Expand Down
6 changes: 4 additions & 2 deletions Source/GlobalContext/FullGlobalContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ struct FullGlobalContext {
{
hooks.viewRenderHook.getOriginalOnRenderStart()(thisptr);

HookDependencies dependencies{_gameDependencies, featureHelpers, bombStatusPanelState, featuresStates};
HookDependencies dependencies{_gameDependencies, featureHelpers, bombStatusPanelState, inWorldPanelContainerState, featuresStates};
SoundWatcher soundWatcher{featureHelpers.soundWatcherState, dependencies};
soundWatcher.update();
features(dependencies).soundFeatures().runOnViewMatrixUpdate();
Expand All @@ -68,7 +68,7 @@ struct FullGlobalContext {

[[nodiscard]] PeepEventsHookResult onPeepEventsHook() noexcept
{
HookDependencies dependencies{_gameDependencies, featureHelpers, bombStatusPanelState, featuresStates};
HookDependencies dependencies{_gameDependencies, featureHelpers, bombStatusPanelState, inWorldPanelContainerState, featuresStates};

features(dependencies).hudFeatures().defusingAlert().run();
features(dependencies).hudFeatures().killfeedPreserver().run();
Expand All @@ -81,6 +81,7 @@ struct FullGlobalContext {
if (unloadFlag) {
FeaturesUnloadHandler{dependencies, featuresStates}.handleUnload();
BombStatusPanelUnloadHandler{dependencies}.handleUnload();
InWorldPanelContainerUnloadHandler{dependencies}.handleUnload();
hooks.forceUninstall();
}

Expand All @@ -105,4 +106,5 @@ struct FullGlobalContext {
FeaturesStates featuresStates;
PanoramaGUI panoramaGUI;
BombStatusPanelState bombStatusPanelState;
InWorldPanelContainerState inWorldPanelContainerState;
};
2 changes: 1 addition & 1 deletion Source/GlobalContext/GlobalContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class GlobalContext {
MemoryPatterns{partialContext.patternFinders}
);

HookDependencies hookDependencies{fullContext().gameDependencies(), fullContext().getFeatureHelpers(), fullContext().bombStatusPanelState, fullContext().featuresStates};
HookDependencies hookDependencies{fullContext().gameDependencies(), fullContext().getFeatureHelpers(), fullContext().bombStatusPanelState, fullContext().inWorldPanelContainerState, fullContext().featuresStates};
if (const auto mainMenu{fullContext().gameDependencies().mainMenu}; mainMenu && *mainMenu)
fullContext().panoramaGUI.init(PanoramaUiPanel{PanoramaUiPanelContext{hookDependencies, (*mainMenu)->uiPanel}});
}
Expand Down
9 changes: 8 additions & 1 deletion Source/HookDependencies/HookDependencies.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ struct BombStatusPanelState;
struct FeaturesStates;

struct HookDependencies {
HookDependencies(GameDependencies& gameDependencies, FeatureHelpers& featureHelpers, BombStatusPanelState& bombStatusPanelState, FeaturesStates& featuresStates) noexcept
HookDependencies(GameDependencies& gameDependencies, FeatureHelpers& featureHelpers, BombStatusPanelState& bombStatusPanelState, InWorldPanelContainerState& inWorldPanelContainerState, FeaturesStates& featuresStates) noexcept
: _gameDependencies{gameDependencies}
, featureHelpers{featureHelpers}
, _bombStatusPanelState{bombStatusPanelState}
, _inWorldPanelContainerState{inWorldPanelContainerState}
, _featuresStates{featuresStates}
{
if (gameDependencies.worldToProjectionMatrix)
Expand Down Expand Up @@ -77,6 +78,11 @@ struct HookDependencies {
return _bombStatusPanelState;
}

[[nodiscard]] InWorldPanelContainerState& inWorldPanelContainerState() const noexcept
{
return _inWorldPanelContainerState;
}

[[nodiscard]] FeaturesStates& featuresStates() const noexcept
{
return _featuresStates;
Expand Down Expand Up @@ -188,6 +194,7 @@ struct HookDependencies {
GameDependencies& _gameDependencies;
FeatureHelpers& featureHelpers;
BombStatusPanelState& _bombStatusPanelState;
InWorldPanelContainerState& _inWorldPanelContainerState;
FeaturesStates& _featuresStates;

const cs2::CConcreteEntityList* entityList;
Expand Down

0 comments on commit 19d4a4d

Please sign in to comment.