-
Notifications
You must be signed in to change notification settings - Fork 970
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement bomb beep sound visualization
- Loading branch information
1 parent
ff84d56
commit ee1473b
Showing
12 changed files
with
249 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#pragma once | ||
|
||
#include <algorithm> | ||
#include <cassert> | ||
#include <string_view> | ||
|
||
#include <CS2/Constants/SoundNames.h> | ||
|
||
struct BombBeepSound { | ||
static constexpr auto kFadeAwayStart = 0.0f; | ||
static constexpr auto kFadeAwayDuration = 0.3f; | ||
|
||
[[nodiscard]] static constexpr float getScale(float clipSpaceZ) noexcept | ||
{ | ||
return (std::max)(1.0f - clipSpaceZ / 1000.0f, 0.4f); | ||
} | ||
|
||
[[nodiscard]] static constexpr float getOpacity(float timeAlive) noexcept | ||
{ | ||
if (timeAlive >= kFadeAwayStart) { | ||
return 1.0f - (std::min)((timeAlive - kFadeAwayStart) / kFadeAwayDuration, 1.0f); | ||
} else { | ||
return 1.0f; | ||
} | ||
} | ||
|
||
[[nodiscard]] static constexpr bool isSound(std::string_view soundName) noexcept | ||
{ | ||
return soundName.starts_with(cs2::kBombSoundsPath) && std::string_view{ soundName.data() + cs2::kBombSoundsPath.length(), soundName.length() - cs2::kBombSoundsPath.length() }.starts_with(cs2::kBombBeepSoundsPrefix); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#pragma once | ||
|
||
#include <FeatureHelpers/GlobalVarsProvider.h> | ||
#include <FeatureHelpers/HudInWorldPanelFactory.h> | ||
#include <FeatureHelpers/WorldToClipSpaceConverter.h> | ||
#include <Helpers/PanoramaTransformFactory.h> | ||
|
||
struct BombBeepVisualizerHelpers { | ||
HudInWorldPanelFactory hudInWorldPanelFactory; | ||
GlobalVarsProvider globalVarsProvider; | ||
PanoramaTransformFactory transformFactory; | ||
WorldToClipSpaceConverter worldtoClipSpaceConverter; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
#pragma once | ||
|
||
#include "BombBeepSound.h" | ||
#include "BombPlantSound.h" | ||
#include "FootstepSound.h" | ||
#include "SoundWatcherImpl.h" | ||
|
||
using SoundWatcher = SoundWatcherImpl<FootstepSound, BombPlantSound>; | ||
using SoundWatcher = SoundWatcherImpl<FootstepSound, BombPlantSound, BombBeepSound>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
#pragma once | ||
|
||
#include <algorithm> | ||
|
||
#include <CS2/Classes/Panorama.h> | ||
#include <CS2/Constants/SoundNames.h> | ||
#include <FeatureHelpers/HudInWorldPanelFactory.h> | ||
#include <FeatureHelpers/Sound/BombBeepVisualizerHelpers.h> | ||
#include <FeatureHelpers/Sound/SoundWatcher.h> | ||
#include <FeatureHelpers/TogglableFeature.h> | ||
#include <FeatureHelpers/WorldToClipSpaceConverter.h> | ||
#include <GameClasses/Panel.h> | ||
#include <GameClasses/PanoramaUiEngine.h> | ||
#include <Helpers/HudProvider.h> | ||
#include <Helpers/PanoramaPanelPointer.h> | ||
#include <Helpers/PanoramaTransformFactory.h> | ||
#include <Hooks/ViewRenderHook.h> | ||
|
||
struct BombBeepPanels { | ||
void createPanels(const HudInWorldPanelFactory& inWorldFactory) noexcept | ||
{ | ||
if (bombBeepContainerPanelPointer.get()) | ||
return; | ||
|
||
const auto bombBeepContainer = inWorldFactory.createPanel("BombBeepContainer"); | ||
if (!bombBeepContainer) | ||
return; | ||
|
||
bombBeepContainerPanelPointer = bombBeepContainer->uiPanel; | ||
|
||
for (std::size_t i = 0; i < kMaxNumberOfBombBeepsToDraw; ++i) { | ||
PanoramaUiEngine::runScript(bombBeepContainer->uiPanel, | ||
R"( | ||
(function() { | ||
var bombBeepPanel = $.CreatePanel('Panel', $.GetContextPanel().FindChildInLayoutFile("BombBeepContainer"), '', { | ||
style: 'width: 100px; height: 100px; x: -50px; y: -50px;' | ||
}); | ||
$.CreatePanel('Image', bombBeepPanel, '', { | ||
src: "s2r://panorama/images/icons/ui/bomb_c4.svg", | ||
style: "horizontal-align: center; vertical-align: center; img-shadow: 0px 0px 1px 3 #000000;", | ||
textureheight: "40" | ||
}); | ||
})();)", "", 0); | ||
} | ||
} | ||
|
||
[[nodiscard]] PanoramaUiPanel getPanel(std::size_t index) const noexcept | ||
{ | ||
const auto containerPanel = bombBeepContainerPanelPointer.get(); | ||
if (!containerPanel) | ||
return PanoramaUiPanel{ nullptr }; | ||
|
||
if (const auto children = containerPanel.children()) { | ||
if (children->size > 0 && static_cast<std::size_t>(children->size) > index) | ||
return PanoramaUiPanel{ children->memory[index] }; | ||
} | ||
return PanoramaUiPanel{ nullptr }; | ||
} | ||
|
||
void hidePanels(std::size_t fromPanelIndex) const noexcept | ||
{ | ||
for (std::size_t i = fromPanelIndex; i < kMaxNumberOfBombBeepsToDraw; ++i) { | ||
const auto panel = getPanel(i); | ||
if (!panel) | ||
break; | ||
|
||
if (const auto style = panel.getStyle()) | ||
style.setOpacity(0.0f); | ||
} | ||
} | ||
|
||
PanoramaPanelPointer bombBeepContainerPanelPointer; | ||
|
||
private: | ||
static constexpr auto kMaxNumberOfBombBeepsToDraw = 5; | ||
}; | ||
|
||
class BombBeepVisualizer : public TogglableFeature<BombBeepVisualizer> { | ||
public: | ||
explicit BombBeepVisualizer(ViewRenderHook& viewRenderHook, SoundWatcher& soundWatcher) noexcept | ||
: viewRenderHook{ viewRenderHook } | ||
, soundWatcher{ soundWatcher } | ||
{ | ||
enable(); | ||
} | ||
|
||
~BombBeepVisualizer() noexcept | ||
{ | ||
if (panels.bombBeepContainerPanelPointer.getHandle().isValid()) | ||
PanoramaUiEngine::onDeletePanel(panels.bombBeepContainerPanelPointer.getHandle()); | ||
} | ||
|
||
void run(const BombBeepVisualizerHelpers& params) noexcept | ||
{ | ||
if (!isEnabled()) | ||
return; | ||
|
||
if (!params.globalVarsProvider || !params.globalVarsProvider.getGlobalVars()) | ||
return; | ||
|
||
if (!params.worldtoClipSpaceConverter) | ||
return; | ||
|
||
panels.createPanels(params.hudInWorldPanelFactory); | ||
|
||
std::size_t currentIndex = 0; | ||
std::as_const(soundWatcher).getSoundsOfType<BombBeepSound>().forEach([this, ¤tIndex, params](const PlayedSound& sound) { | ||
const auto soundInClipSpace = params.worldtoClipSpaceConverter.toClipSpace(sound.origin); | ||
if (!soundInClipSpace.onScreen()) | ||
return; | ||
|
||
const auto opacity = BombBeepSound::getOpacity(sound.getTimeAlive(params.globalVarsProvider.getGlobalVars()->curtime)); | ||
if (opacity <= 0.0f) | ||
return; | ||
|
||
const auto panel = panels.getPanel(currentIndex); | ||
if (!panel) | ||
return; | ||
|
||
const auto style = panel.getStyle(); | ||
if (!style) | ||
return; | ||
|
||
style.setOpacity(opacity); | ||
|
||
const auto deviceCoordinates = soundInClipSpace.toNormalizedDeviceCoordinates(); | ||
cs2::CTransform3D* transformations[]{ params.transformFactory.create<cs2::CTransformScale3D>( | ||
BombBeepSound::getScale(soundInClipSpace.z), BombBeepSound::getScale(soundInClipSpace.z), 1.0f | ||
), params.transformFactory.create<cs2::CTransformTranslate3D>( | ||
deviceCoordinates.getX(), | ||
deviceCoordinates.getY(), | ||
cs2::CUILength{ 0.0f, cs2::CUILength::k_EUILengthLength } | ||
) }; | ||
|
||
cs2::CUtlVector<cs2::CTransform3D*> dummyVector; | ||
dummyVector.allocationCount = 2; | ||
dummyVector.memory = transformations; | ||
dummyVector.growSize = 0; | ||
dummyVector.size = 2; | ||
|
||
style.setTransform3D(dummyVector); | ||
++currentIndex; | ||
}); | ||
|
||
panels.hidePanels(currentIndex); | ||
} | ||
|
||
private: | ||
friend TogglableFeature; | ||
|
||
void onEnable() noexcept | ||
{ | ||
viewRenderHook.incrementReferenceCount(); | ||
soundWatcher.startWatching<BombBeepSound>(); | ||
} | ||
|
||
void onDisable() noexcept | ||
{ | ||
viewRenderHook.decrementReferenceCount(); | ||
soundWatcher.stopWatching<BombBeepSound>(); | ||
panels.hidePanels(0); | ||
} | ||
|
||
BombBeepPanels panels; | ||
ViewRenderHook& viewRenderHook; | ||
SoundWatcher& soundWatcher; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters