1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
#include "ShaderParms.h"
#include "ColourKey.h"
#include "KeyObserverMap.h"
#include "string/convert.h"
#include <functional>
#include <sigc++/bind.h>
namespace entity
{
ShaderParms::ShaderParms(KeyObserverMap& keyObserverMap, ColourKey& colourKey) :
_keyObserverMap(keyObserverMap),
_colourKey(colourKey),
_parmValues(MAX_ENTITY_SHADERPARMS, 0.0f)
{
_parmValues[3] = 1.0f; // parm3 = alpha, defaults to 1.0f
}
float ShaderParms::getParmValue(int parmNum) const
{
assert(parmNum >= 0 && parmNum < static_cast<int>(MAX_ENTITY_SHADERPARMS));
// For parm0..parm2, use the colour keyobserver to retrieve the value
return parmNum > 2 ? _parmValues[parmNum] : static_cast<float>(_colourKey.getColour()[parmNum]);
}
void ShaderParms::addKeyObservers()
{
for (std::size_t i = MIN_SHADERPARM_NUM_TO_OBSERVE; i < MAX_ENTITY_SHADERPARMS; ++i)
{
_keyObserverMap.observeKey(
"shaderParm" + string::to_string(i),
sigc::bind<0>(sigc::mem_fun(this, &ShaderParms::onShaderParmKeyValueChanged), i)
);
}
}
void ShaderParms::onShaderParmKeyValueChanged(std::size_t parm, const std::string& value)
{
// For empty spawnarg values, revert to our default values
if (value.empty())
{
// parm0 defaults to 3
_parmValues[parm] = parm == 3 ? 1.0f : 0.0f;
}
else
{
// Get the floating point value and cache it locally
_parmValues[parm] = string::convert<float>(value);
}
}
} // namespace
|