From 41bf96a3f56e2bc478f9b67d1a21cb62947da10b Mon Sep 17 00:00:00 2001 From: codereader Date: Sun, 17 Apr 2022 06:23:48 +0200 Subject: [PATCH] #5940: Provide a virtual method for entity subclasses to be notified about colour changes. When a LightNode's colour key is changing, its renderables need to be refreshed. --- libs/render/RenderableGeometry.h | 6 +++--- radiantcore/entity/ColourKey.h | 23 ++++++++++++++++------- radiantcore/entity/EntityNode.cpp | 7 +++++++ radiantcore/entity/EntityNode.h | 7 ++++++- radiantcore/entity/light/LightNode.cpp | 6 ++++++ radiantcore/entity/light/LightNode.h | 2 ++ 6 files changed, 40 insertions(+), 11 deletions(-) diff --git a/libs/render/RenderableGeometry.h b/libs/render/RenderableGeometry.h index cbc7891cf7..01355b7b0c 100644 --- a/libs/render/RenderableGeometry.h +++ b/libs/render/RenderableGeometry.h @@ -129,10 +129,10 @@ class RenderableGeometry : if (_shader != shader) { clear(); - } - // Update our local shader reference - _shader = shader; + // Update our local shader reference + _shader = shader; + } if (_shader) { diff --git a/radiantcore/entity/ColourKey.h b/radiantcore/entity/ColourKey.h index 8222793f00..ece377555b 100644 --- a/radiantcore/entity/ColourKey.h +++ b/radiantcore/entity/ColourKey.h @@ -1,8 +1,8 @@ #pragma once +#include #include "ientity.h" #include "irender.h" -#include namespace entity { @@ -20,12 +20,16 @@ class ColourKey : RenderSystemWeakPtr _renderSystem; + std::function _onColourChanged; + public: - ColourKey() : - _colour(1,1,1) - { - captureShader(); - } + // The given callback will be invoked after the _color spawnarg has been changed + ColourKey(const std::function& onColourChanged) : + _colour(1,1,1), + _onColourChanged(onColourChanged) + { + captureShader(); + } const Vector3& getColour() const { @@ -33,7 +37,7 @@ class ColourKey : } // Called when "_color" keyvalue changes - void onKeyValueChanged(const std::string& value) + void onKeyValueChanged(const std::string& value) override { // Initialise the colour with white, in case the string parse fails _colour[0] = _colour[1] = _colour[2] = 1; @@ -47,6 +51,11 @@ class ColourKey : strm >> _colour.z(); captureShader(); + + if (_onColourChanged) + { + _onColourChanged(value); + } } void setRenderSystem(const RenderSystemPtr& renderSystem) diff --git a/radiantcore/entity/EntityNode.cpp b/radiantcore/entity/EntityNode.cpp index 5fc3905f63..28c036745f 100644 --- a/radiantcore/entity/EntityNode.cpp +++ b/radiantcore/entity/EntityNode.cpp @@ -22,6 +22,7 @@ EntityNode::EntityNode(const IEntityClassPtr& eclass) : _originKey(std::bind(&EntityNode::_originKeyChanged, this)), _nameKey(_spawnArgs), _renderableName(*this, _nameKey), + _colourKey(std::bind(&EntityNode::_colourKeyChanged, this, std::placeholders::_1)), _modelKey(*this), _keyObservers(_spawnArgs), _shaderParms(_keyObservers, _colourKey), @@ -45,6 +46,7 @@ EntityNode::EntityNode(const EntityNode& other) : _originKey(std::bind(&EntityNode::_originKeyChanged, this)), _nameKey(_spawnArgs), _renderableName(*this, _nameKey), + _colourKey(std::bind(&EntityNode::_colourKeyChanged, this, std::placeholders::_1)), _modelKey(*this), _keyObservers(_spawnArgs), _shaderParms(_keyObservers, _colourKey), @@ -536,6 +538,11 @@ void EntityNode::_originKeyChanged() // TODO: add virtual callout for subclasses } +void EntityNode::_colourKeyChanged(const std::string& value) +{ + onColourKeyChanged(value); +} + void EntityNode::_onNoShadowsSettingsChanged(const std::string& noShadowsValue) { _isShadowCasting = noShadowsValue != "1"; diff --git a/radiantcore/entity/EntityNode.h b/radiantcore/entity/EntityNode.h index 67729660dc..fc79528358 100644 --- a/radiantcore/entity/EntityNode.h +++ b/radiantcore/entity/EntityNode.h @@ -205,7 +205,11 @@ class EntityNode : virtual const Vector3& getWorldPosition() const = 0; protected: - virtual void onModelKeyChanged(const std::string& value); + virtual void onModelKeyChanged(const std::string& value); + + // Invoked when the colour key has changed its value + virtual void onColourKeyChanged(const std::string& value) + {} /** * greebo: construct() does the necessary setup, connects keyobservers, etc. @@ -231,6 +235,7 @@ class EntityNode : // Private function target - wraps to virtual protected signal void _modelKeyChanged(const std::string& value); void _originKeyChanged(); + void _colourKeyChanged(const std::string& value); void _onNoShadowsSettingsChanged(const std::string& value); void acquireShaders(); diff --git a/radiantcore/entity/light/LightNode.cpp b/radiantcore/entity/light/LightNode.cpp index 198b231392..2ab3ba4bd2 100644 --- a/radiantcore/entity/light/LightNode.cpp +++ b/radiantcore/entity/light/LightNode.cpp @@ -1236,4 +1236,10 @@ const IRenderEntity& LightNode::getLightEntity() const return *this; } +void LightNode::onColourKeyChanged(const std::string& value) +{ + _renderableOctagon.queueUpdate(); + _renderableLightVolume.queueUpdate(); +} + } // namespace entity diff --git a/radiantcore/entity/light/LightNode.h b/radiantcore/entity/light/LightNode.h index 282a56578f..7ef1ec6d52 100644 --- a/radiantcore/entity/light/LightNode.h +++ b/radiantcore/entity/light/LightNode.h @@ -205,6 +205,8 @@ class LightNode : void onVisibilityChanged(bool isVisibleNow) override; void onSelectionStatusChange(bool changeGroupStatus) override; + void onColourKeyChanged(const std::string& value) override; + private: void evaluateTransform();