-
Notifications
You must be signed in to change notification settings - Fork 961
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement bomb defuse sound visualization (#4191)
- Loading branch information
1 parent
fb4611c
commit cd648f4
Showing
13 changed files
with
199 additions
and
2 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 |
---|---|---|
|
@@ -3,5 +3,6 @@ | |
enum class HudInWorldPanelZOrder { | ||
Footstep, | ||
BombBeep, | ||
BombPlant | ||
BombPlant, | ||
BombDefuse | ||
}; |
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 BombDefuseSound { | ||
static constexpr auto kFadeAwayStart = 2.0f; | ||
static constexpr auto kFadeAwayDuration = 1.0f; | ||
|
||
[[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::kBombDefuseStartSoundName); | ||
} | ||
}; |
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 BombDefuseVisualizerHelpers { | ||
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,8 +1,9 @@ | ||
#pragma once | ||
|
||
#include "BombBeepSound.h" | ||
#include "BombDefuseSound.h" | ||
#include "BombPlantSound.h" | ||
#include "FootstepSound.h" | ||
#include "SoundWatcherImpl.h" | ||
|
||
using SoundWatcher = SoundWatcherImpl<FootstepSound, BombPlantSound, BombBeepSound>; | ||
using SoundWatcher = SoundWatcherImpl<FootstepSound, BombPlantSound, BombBeepSound, BombDefuseSound>; |
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,124 @@ | ||
#pragma once | ||
|
||
#include <CS2/Classes/Panorama.h> | ||
#include <FeatureHelpers/HudInWorldPanels.h> | ||
#include <FeatureHelpers/HudInWorldPanelFactory.h> | ||
#include <FeatureHelpers/Sound/BombDefuseSound.h> | ||
#include <FeatureHelpers/Sound/BombDefuseVisualizerHelpers.h> | ||
#include <FeatureHelpers/Sound/SoundWatcher.h> | ||
#include <FeatureHelpers/TogglableFeature.h> | ||
#include <GameClasses/PanoramaUiEngine.h> | ||
#include <Hooks/ViewRenderHook.h> | ||
|
||
struct BombDefusePanels { | ||
[[nodiscard]] static cs2::CPanel2D* createContainerPanel(const HudInWorldPanelFactory& inWorldFactory) noexcept | ||
{ | ||
return inWorldFactory.createPanel("BombDefuseContainer", HudInWorldPanelZOrder::BombDefuse); | ||
} | ||
|
||
static void createContentPanels(cs2::CUIPanel& containerPanel) noexcept | ||
{ | ||
for (std::size_t i = 0; i < kMaxNumberOfPanels; ++i) { | ||
PanoramaUiEngine::runScript(&containerPanel, | ||
R"( | ||
(function() { | ||
var bombDefusePanel = $.CreatePanel('Panel', $.GetContextPanel().FindChildInLayoutFile("BombDefuseContainer"), '', { | ||
style: 'width: 100px; height: 100px; x: -50px; y: -50px;' | ||
}); | ||
$.CreatePanel('Image', bombDefusePanel, '', { | ||
src: "s2r://panorama/images/icons/equipment/defuser.vsvg", | ||
style: "horizontal-align: center; vertical-align: center; img-shadow: 0px 0px 1px 3 #000000;", | ||
textureheight: "40" | ||
}); | ||
})();)", "", 0); | ||
} | ||
} | ||
|
||
static constexpr auto kMaxNumberOfPanels = 5; | ||
}; | ||
|
||
class BombDefuseVisualizer : public TogglableFeature<BombDefuseVisualizer> { | ||
public: | ||
explicit BombDefuseVisualizer(ViewRenderHook& viewRenderHook, SoundWatcher& soundWatcher) noexcept | ||
: viewRenderHook{ viewRenderHook } | ||
, soundWatcher{ soundWatcher } | ||
{ | ||
} | ||
|
||
void run(const BombDefuseVisualizerHelpers& 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<BombDefuseSound>().forEach([this, ¤tIndex, params](const PlayedSound& sound) { | ||
const auto soundInClipSpace = params.worldtoClipSpaceConverter.toClipSpace(sound.origin); | ||
if (!soundInClipSpace.onScreen()) | ||
return; | ||
|
||
const auto opacity = BombDefuseSound::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); | ||
style.setZIndex(-soundInClipSpace.z); | ||
|
||
const auto deviceCoordinates = soundInClipSpace.toNormalizedDeviceCoordinates(); | ||
cs2::CTransform3D* transformations[]{ params.transformFactory.create<cs2::CTransformScale3D>( | ||
BombDefuseSound::getScale(soundInClipSpace.z), BombDefuseSound::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<BombDefuseSound>(); | ||
} | ||
|
||
void onDisable() noexcept | ||
{ | ||
viewRenderHook.decrementReferenceCount(); | ||
soundWatcher.stopWatching<BombDefuseSound>(); | ||
panels.hidePanels(0); | ||
} | ||
|
||
HudInWorldPanels<BombDefusePanels> 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