Skip to content

Commit

Permalink
Reset in-world panels state and PlayerInfoPanelCache when HUD has bee…
Browse files Browse the repository at this point in the history
…n recreated by the game
  • Loading branch information
danielkrupinski committed Jan 27, 2025
1 parent 72d18f3 commit 1bd2723
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 10 deletions.
33 changes: 25 additions & 8 deletions Source/Features/Common/InWorldPanels.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,19 @@ class InWorldPanels {
{
}

void updateState() const noexcept
{
if (!containerPanelExists()) {
hookContext.template make<PlayerInfoPanelCache>().clear();
state().reset();
}
}

[[nodiscard]] decltype(auto) getNextPlayerInfoPanel() const noexcept
{
if (auto&& existingPanel = getNextExistingPanel(state().playerInfoPanelListHead, perHookState().lastUsedPlayerInfoPanelIndex))
return existingPanel.template as<PlayerInfoPanel>(hookContext.template make<PlayerInfoPanelCache>().nextEntry());
auto&& newPanel = createPlayerInfoPanel(containerPanel());
auto&& newPanel = createPlayerInfoPanel(getOrCreateContainerPanel());
registerNewPanel(state().playerInfoPanelListHead, perHookState().lastUsedPlayerInfoPanelIndex);
hookContext.template make<PlayerInfoPanelCache>().allocateNewEntry();
return newPanel.template as<PlayerInfoPanel>(hookContext.template make<PlayerInfoPanelCache>().nextEntry());
Expand All @@ -35,7 +43,7 @@ class InWorldPanels {
{
if (auto&& existingPanel = getNextExistingPanel(state().soundVisualizationPanelListHeads[SoundVisualizationPanelTypes::indexOf<Type>()], perHookState().lastUsedSoundVisualizationPanelIndexes[SoundVisualizationPanelTypes::indexOf<Type>()]))
return utils::lvalue<decltype(existingPanel)>(existingPanel);
auto&& newPanel = createSoundVisualizationPanel<Type>(containerPanel());
auto&& newPanel = createSoundVisualizationPanel<Type>(getOrCreateContainerPanel());
registerNewPanel(state().soundVisualizationPanelListHeads[SoundVisualizationPanelTypes::indexOf<Type>()], perHookState().lastUsedSoundVisualizationPanelIndexes[SoundVisualizationPanelTypes::indexOf<Type>()]);
return utils::lvalue<decltype(newPanel)>(newPanel);
}
Expand Down Expand Up @@ -68,7 +76,7 @@ class InWorldPanels {
for (auto index = firstIndex; index.isValid(); index = state().panelList[index.value].indexOfNextPanelOfSameType()) {
if (state().panelList[index.value].isPanelActive()) {
state().panelList[index.value].markPanelInactive();
deactivatePanel(containerPanel().children()[index.value]);
deactivatePanel(getContainerPanel().children()[index.value]);
}
}
}
Expand Down Expand Up @@ -107,7 +115,7 @@ class InWorldPanels {
if (nextIndex.isValid()) {
lastUsedPanelIndex = nextIndex;
auto& nextPanelEntry = state().panelList[nextIndex.value];
auto&& panel = containerPanel().children()[nextIndex.value];
auto&& panel = getContainerPanel().children()[nextIndex.value];
if (!nextPanelEntry.isPanelActive()) {
activatePanel(panel);
nextPanelEntry.markPanelActive();
Expand Down Expand Up @@ -137,20 +145,29 @@ class InWorldPanels {
return hookContext.inWorldPanelsState();
}

[[nodiscard]] auto containerPanel() const noexcept
[[nodiscard]] decltype(auto) getOrCreateContainerPanel() const noexcept
{
return containerPanelHandle().getOrInit(createContainerPanel());
}

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

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

[[nodiscard]] bool containerPanelExists() const noexcept
{
return containerPanelHandle().panelExists();
}

[[nodiscard]] auto createContainerPanel() const noexcept
{
return [this] {
state().onContainerPanelCreation();
auto&& panel = hookContext.panelFactory().createPanel(hookContext.hud().getHudReticle()).uiPanel();
panel.fitParent();
return panel;
Expand Down
3 changes: 2 additions & 1 deletion Source/Features/Common/InWorldPanelsState.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ struct InWorldPanelsState {
InWorldPanelIndex playerInfoPanelListHead{};
std::array<InWorldPanelIndex, SoundVisualizationPanelTypes::size()> soundVisualizationPanelListHeads{};

void onContainerPanelCreation() noexcept
void reset() noexcept
{
containerPanelHandle = {};
panelList.clear();
playerInfoPanelListHead = {};
soundVisualizationPanelListHeads = {};
Expand Down
12 changes: 11 additions & 1 deletion Source/GameClasses/PanelHandle.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,27 @@ struct PanelHandle {
{
}

[[nodiscard]] decltype(auto) get() noexcept
{
return hookContext.template make<PanoramaUiEngine>().getPanelFromHandle(std::as_const(handle));
}

template <typename F>
[[nodiscard]] decltype(auto) getOrInit(F&& f) noexcept
{
if (auto&& panel = hookContext.template make<PanoramaUiEngine>().getPanelFromHandle(std::as_const(handle)))
if (auto&& panel = get())
return utils::lvalue<decltype(panel)>(panel);

auto&& panel = std::forward<F>(f)();
handle = panel.getHandle();
return utils::lvalue<decltype(panel)>(panel);
}

[[nodiscard]] bool panelExists() noexcept
{
return handle.isValid() && static_cast<bool>(get());
}

private:
HookContext& hookContext;
cs2::PanelHandle& handle;
Expand Down
1 change: 1 addition & 0 deletions Source/GlobalContext/GlobalContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ class GlobalContext {
{
HookDependencies dependencies{fullContext()};
fullContext().hooks.viewRenderHook.getOriginalOnRenderStart()(viewRender);
dependencies.make<InWorldPanels>().updateState();
SoundWatcher<decltype(dependencies)> soundWatcher{fullContext().soundWatcherState, dependencies};
soundWatcher.update();
fullContext().features(dependencies).soundFeatures().runOnViewMatrixUpdate();
Expand Down

0 comments on commit 1bd2723

Please sign in to comment.