Skip to content

Commit

Permalink
Refactor PanoramaUiEngine
Browse files Browse the repository at this point in the history
  • Loading branch information
danielkrupinski committed Sep 25, 2024
1 parent d15914c commit 9598a6e
Show file tree
Hide file tree
Showing 23 changed files with 111 additions and 115 deletions.
2 changes: 1 addition & 1 deletion Source/FeatureHelpers/HudInWorldPanelContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ struct InWorldPanelContainerUnloadHandler {

void handleUnload() const noexcept
{
hookContext.panels().deletePanelByHandle(hookContext.inWorldPanelContainerState().containerPanelHandle);
hookContext.template make<PanoramaUiEngine>().deletePanelByHandle(hookContext.inWorldPanelContainerState().containerPanelHandle);
}

private:
Expand Down
13 changes: 9 additions & 4 deletions Source/Features/Hud/BombTimer/BombTimerContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,21 @@ struct BombTimerContext {

[[nodiscard]] decltype(auto) bombTimerContainerPanel() const noexcept
{
if (auto&& bombTimerContainer = _context.panels().getPanelFromHandle(state().bombTimerContainerPanelHandle))
if (auto&& bombTimerContainer = uiEngine().getPanelFromHandle(state().bombTimerContainerPanelHandle))
return utils::lvalue<decltype(bombTimerContainer)>(bombTimerContainer);

updatePanelHandles();
return _context.panels().getPanelFromHandle(state().bombTimerContainerPanelHandle);
return uiEngine().getPanelFromHandle(state().bombTimerContainerPanelHandle);
}

[[nodiscard]] auto bombSiteIconPanel() const noexcept
{
return BombSiteIconPanel{_context.panels().getPanelFromHandle(state().bombSiteIconPanelHandle).clientPanel().template as<PanoramaImagePanel>()};
return BombSiteIconPanel{uiEngine().getPanelFromHandle(state().bombSiteIconPanelHandle).clientPanel().template as<PanoramaImagePanel>()};
}

[[nodiscard]] auto bombTimerTextPanel() const noexcept
{
return BombTimerTextPanel{_context.panels().getPanelFromHandle(state().bombTimerPanelHandle).clientPanel().template as<PanoramaLabel>()};
return BombTimerTextPanel{uiEngine().getPanelFromHandle(state().bombTimerPanelHandle).clientPanel().template as<PanoramaLabel>()};
}

void updatePanelHandles() const noexcept
Expand All @@ -85,5 +85,10 @@ struct BombTimerContext {
}

private:
[[nodiscard]] decltype(auto) uiEngine() const noexcept
{
return _context.template make<PanoramaUiEngine>();
}

HookContext& _context;
};
6 changes: 3 additions & 3 deletions Source/Features/Hud/BombTimer/BombTimerUnloadHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ struct BombTimerUnloadHandler {

void handleUnload() const noexcept
{
auto&& panels = hookContext.panels();
panels.deletePanelByHandle(state.bombTimerPanelHandle);
panels.deletePanelByHandle(state.bombTimerContainerPanelHandle);
auto&& uiEngine = hookContext.template make<PanoramaUiEngine>();
uiEngine.deletePanelByHandle(state.bombTimerPanelHandle);
uiEngine.deletePanelByHandle(state.bombTimerContainerPanelHandle);
}

HookContext& hookContext;
Expand Down
15 changes: 10 additions & 5 deletions Source/Features/Hud/DefusingAlert/DefusingAlertContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ class DefusingAlertContext {

[[nodiscard]] decltype(auto) defusingAlertContainerPanel() const noexcept
{
if (auto&& panel = context.panels().getPanelFromHandle(_state.defusingAlertContainerPanelHandle))
if (auto&& panel = uiEngine().getPanelFromHandle(_state.defusingAlertContainerPanelHandle))
return utils::lvalue<decltype(panel)>(panel);

updatePanelHandles();
return context.panels().getPanelFromHandle(_state.defusingAlertContainerPanelHandle);
return uiEngine().getPanelFromHandle(_state.defusingAlertContainerPanelHandle);
}

[[nodiscard]] auto defusingAlertCondition() const noexcept
Expand Down Expand Up @@ -53,20 +53,25 @@ class DefusingAlertContext {

[[nodiscard]] auto defusingCountdownTextPanel() const noexcept
{
return DefusingCountdownTextPanel{context.panels().getPanelFromHandle(_state.defusingTimerPanelHandle).clientPanel().template as<PanoramaLabel>()};
return DefusingCountdownTextPanel{uiEngine().getPanelFromHandle(_state.defusingTimerPanelHandle).clientPanel().template as<PanoramaLabel>()};
}

private:
[[nodiscard]] decltype(auto) uiEngine() const noexcept
{
return context.template make<PanoramaUiEngine>();
}

void updatePanelHandles() const noexcept
{
if (context.panels().getPanelFromHandle(_state.defusingTimerPanelHandle))
if (uiEngine().getPanelFromHandle(_state.defusingTimerPanelHandle))
return;

auto&& hudTeamCounter = context.hud().hudTeamCounter();
if (!hudTeamCounter)
return;

PanoramaUiEngine::runScript(hudTeamCounter,
uiEngine().runScript(hudTeamCounter,
R"(
(function() {
var defusingAlertContainer = $.CreatePanel('Panel', $.GetContextPanel().FindChildInLayoutFile('ScoreAndTimeAndBomb'), 'DefusingAlertContainer', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ struct DefusingAlertUnloadHandler {

void handleUnload() const noexcept
{
hookContext.panels().deletePanelByHandle(state.defusingAlertContainerPanelHandle);
hookContext.template make<PanoramaUiEngine>().deletePanelByHandle(state.defusingAlertContainerPanelHandle);
}

private:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ struct PostRoundTimerUnloadHandler {

void handleUnload() const noexcept
{
hookContext.panels().deletePanelByHandle(state().countdownContainerPanelHandle);
hookContext.template make<PanoramaUiEngine>().deletePanelByHandle(state().countdownContainerPanelHandle);
}

private:
Expand Down
4 changes: 2 additions & 2 deletions Source/Features/Sound/Details/SoundVisualizationFeature.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ class SoundVisualizationFeature {

const HudInWorldPanels panels{*containerPanelChildren};

if (state.containerPanelHandle != PanoramaUiEngine::getPanelHandle(containerPanel)) {
if (state.containerPanelHandle != hookContext.template make<PanoramaUiEngine>().getPanelHandle(containerPanel)) {
state.panelIndices.clear();
state.containerPanelHandle = PanoramaUiEngine::getPanelHandle(containerPanel);
state.containerPanelHandle = hookContext.template make<PanoramaUiEngine>().getPanelHandle(containerPanel);
}

std::size_t currentIndex = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,9 @@ class PlayerInformationThroughWalls {

const HudInWorldPanels panels{*containerPanelChildren};

if (state.containerPanelHandle != PanoramaUiEngine::getPanelHandle(containerPanel)) {
if (state.containerPanelHandle != hookContext.template make<PanoramaUiEngine>().getPanelHandle(containerPanel)) {
state.panelIndices.clear();
state.containerPanelHandle = PanoramaUiEngine::getPanelHandle(containerPanel);
state.containerPanelHandle = hookContext.template make<PanoramaUiEngine>().getPanelHandle(containerPanel);
}

const auto panel = getPanel(containerPanel, panels);
Expand Down
6 changes: 3 additions & 3 deletions Source/GameClasses/Hud/DeathNotice.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class DeathNotice {

[[nodiscard]] bool isLocalPlayerKiller() const noexcept
{
return context.panel().hasClass(PanoramaSymbols::instance().deathNoticeKillerSymbol);
return context.panel().hasClass(context.panoramaSymbols().deathNoticeKillerSymbol);
}

[[nodiscard]] auto wasSpawnedThisRound() const noexcept
Expand All @@ -31,7 +31,7 @@ class DeathNotice {
[[nodiscard]] float getSpawnTime() const noexcept
{
float spawnTime = 0.0f;
if (const auto spawnTimeString = context.panel().getAttributeString(PanoramaSymbols::instance().spawnTimeSymbol, ""))
if (const auto spawnTimeString = context.panel().getAttributeString(context.panoramaSymbols().spawnTimeSymbol, ""))
StringParser{spawnTimeString}.parseFloat(spawnTime);
return spawnTime;
}
Expand All @@ -44,7 +44,7 @@ class DeathNotice {

void setSpawnTime(float spawnTime) const noexcept
{
context.panel().setAttributeString(PanoramaSymbols::instance().spawnTimeSymbol,
context.panel().setAttributeString(context.panoramaSymbols().spawnTimeSymbol,
StringBuilderStorage<20>{}.builder().put(static_cast<std::uint64_t>(spawnTime), '.', '0').cstring());
}

Expand Down
6 changes: 6 additions & 0 deletions Source/GameClasses/Hud/DeathNoticeContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ struct DeathNoticeContext {
return _hookContext.template make<PanoramaUiPanel>(_panel);
}

[[nodiscard]] decltype(auto) panoramaSymbols() const noexcept
{
return _hookContext.panoramaSymbols();
}

[[nodiscard]] decltype(auto) gameRules() const noexcept
{
return _hookContext.gameRules();
Expand All @@ -26,6 +31,7 @@ struct DeathNoticeContext {
return _hookContext.globalVars();
}

private:
HookContext& _hookContext;
cs2::CUIPanel* _panel;
};
2 changes: 1 addition & 1 deletion Source/GameClasses/PanelHandle.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ struct PanelHandle {
template <typename F>
[[nodiscard]] decltype(auto) getOrInit(F&& f) noexcept
{
if (auto&& panel = hookContext.panels().getPanelFromHandle(std::as_const(handle)))
if (auto&& panel = hookContext.template make<PanoramaUiEngine>().getPanelFromHandle(std::as_const(handle)))
return utils::lvalue<decltype(panel)>(panel);

auto&& panel = std::forward<F>(f)();
Expand Down
33 changes: 0 additions & 33 deletions Source/GameClasses/Panels.h

This file was deleted.

58 changes: 38 additions & 20 deletions Source/GameClasses/PanoramaUiEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,67 @@
#include <Platform/Macros/IsPlatform.h>
#include <GameDependencies/PanoramaUiEngineDeps.h>

struct PanoramaUiEngine {
static void runScript(cs2::CUIPanel* contextPanel, const char* scriptSource, const char* originFile, std::uint64_t line) noexcept
template <typename Context>
struct PanoramaUiPanel;

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

void runScript(cs2::CUIPanel* contextPanel, const char* scriptSource, const char* originFile, std::uint64_t line) noexcept
{
if (impl().runScript && impl().thisptr)
impl().runScript(*impl().thisptr, contextPanel, scriptSource, originFile, line);
if (deps().runScript && deps().thisptr)
deps().runScript(*deps().thisptr, contextPanel, scriptSource, originFile, line);
}

static cs2::CUIPanel* getPanelPointer(cs2::PanelHandle handle) noexcept
[[nodiscard]] decltype(auto) getPanelFromHandle(cs2::PanelHandle handle) noexcept
{
if (impl().getPanelPointer && impl().thisptr)
return impl().getPanelPointer(*impl().thisptr, &handle);
return nullptr;
if (deps().getPanelPointer && deps().thisptr)
return hookContext.template make<PanoramaUiPanel>(deps().getPanelPointer(*deps().thisptr, &handle));
return hookContext.template make<PanoramaUiPanel>(nullptr);
}

static cs2::PanelHandle getPanelHandle(cs2::CUIPanel* panel) noexcept
[[nodiscard]] cs2::PanelHandle getPanelHandle(cs2::CUIPanel* panel) noexcept
{
cs2::PanelHandle handle{};
if (impl().getPanelHandle && impl().thisptr) {
if (deps().getPanelHandle && deps().thisptr) {
#if IS_WIN64()
impl().getPanelHandle(*impl().thisptr, &handle, panel);
deps().getPanelHandle(*deps().thisptr, &handle, panel);
#elif IS_LINUX()
handle = impl().getPanelHandle(*impl().thisptr, panel);
handle = deps().getPanelHandle(*deps().thisptr, panel);
#endif
}
return handle;
}

static void onDeletePanel(cs2::PanelHandle panelHandle) noexcept
void deletePanelByHandle(cs2::PanelHandle handle) noexcept
{
if (impl().onDeletePanel && impl().thisptr)
impl().onDeletePanel(*impl().thisptr, &panelHandle);
if (handle.isValid())
onDeletePanel(handle);
}

static cs2::CPanoramaSymbol makeSymbol(int type, const char* text) noexcept
[[nodiscard]] cs2::CPanoramaSymbol makeSymbol(int type, const char* text) noexcept
{
if (impl().makeSymbol && impl().thisptr)
return impl().makeSymbol(*impl().thisptr, type, text);
if (deps().makeSymbol && deps().thisptr)
return deps().makeSymbol(*deps().thisptr, type, text);
return -1;
}

private:
[[nodiscard]] static const PanoramaUiEngineDeps& impl() noexcept
void onDeletePanel(cs2::PanelHandle panelHandle) noexcept
{
return PanoramaUiEngineDeps::instance();
if (deps().onDeletePanel && deps().thisptr)
deps().onDeletePanel(*deps().thisptr, &panelHandle);
}

[[nodiscard]] const auto& deps() const noexcept
{
return hookContext.gameDependencies().uiEngineDeps;
}

HookContext& hookContext;
};
3 changes: 2 additions & 1 deletion Source/GameClasses/PanoramaUiPanelContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <GameDependencies/PanelStyleDeps.h>

#include "ClientPanel.h"
#include "PanoramaUiEngine.h"
#include "PanoramaUiPanelChildPanels.h"
#include "PanoramaUiPanelClasses.h"
#include "PanoramaUiPanelMethodInvoker.h"
Expand All @@ -29,7 +30,7 @@ struct PanoramaUiPanelContext {

[[nodiscard]] decltype(auto) getHandle() const noexcept
{
return hookContext.panels().getPanelHandle(panel);
return hookContext.template make<PanoramaUiEngine>().getPanelHandle(panel);
}

void setSimpleForegroundColor(cs2::Color color) const noexcept
Expand Down
13 changes: 0 additions & 13 deletions Source/GameDependencies/DepsInstances.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,6 @@

#include <GlobalContext/GlobalContext.h>

inline const PanoramaSymbols& PanoramaSymbols::instance() noexcept
{
auto& panoramaSymbols = GlobalContext::instance().fullContext().gameDependencies.panoramaSymbols;
if (!panoramaSymbols.has_value())
panoramaSymbols.emplace();
return *panoramaSymbols;
}

inline HudDeps& HudDeps::instance() noexcept
{
return GlobalContext::instance().fullContext().gameDependencies.hudDeps;
Expand Down Expand Up @@ -66,11 +58,6 @@ inline const PanoramaLabelDeps& PanoramaLabelDeps::instance() noexcept
return GlobalContext::instance().fullContext().gameDependencies.panoramaLabelDeps;
}

inline const PanoramaUiEngineDeps& PanoramaUiEngineDeps::instance() noexcept
{
return GlobalContext::instance().fullContext().gameDependencies.uiEngineDeps;
}

inline const PanoramaUiPanelDeps& PanoramaUiPanelDeps::instance() noexcept
{
return GlobalContext::instance().fullContext().gameDependencies.panoramaUiPanelDeps;
Expand Down
11 changes: 8 additions & 3 deletions Source/GameDependencies/PanoramaSymbols.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@
#include <GameClasses/PanoramaUiEngine.h>

struct PanoramaSymbols {
[[nodiscard]] static const PanoramaSymbols& instance() noexcept;
template <typename HookContext>
PanoramaSymbols(HookContext& hookContext) noexcept
: deathNoticeKillerSymbol{hookContext.template make<PanoramaUiEngine>().makeSymbol(0, "DeathNotice_Killer")}
, spawnTimeSymbol{hookContext.template make<PanoramaUiEngine>().makeSymbol(0, "SpawnTime")}
{
}

cs2::CPanoramaSymbol deathNoticeKillerSymbol{PanoramaUiEngine::makeSymbol(0, "DeathNotice_Killer")};
cs2::CPanoramaSymbol spawnTimeSymbol{PanoramaUiEngine::makeSymbol(0, "SpawnTime")};
cs2::CPanoramaSymbol deathNoticeKillerSymbol;
cs2::CPanoramaSymbol spawnTimeSymbol;
};
2 changes: 0 additions & 2 deletions Source/GameDependencies/PanoramaUiEngineDeps.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ struct PanoramaUiEngineDeps {
{
}

[[nodiscard]] static const PanoramaUiEngineDeps& instance() noexcept;

cs2::CUIEngine** thisptr;

cs2::CUIEngine::getPanelHandle getPanelHandle;
Expand Down
Loading

0 comments on commit 9598a6e

Please sign in to comment.