Move Controller base classes to SceneUtil, add visitor to assign ControllerSources
parent
de2c85e0f8
commit
c516e897ee
@ -0,0 +1,86 @@
|
||||
#include "controller.hpp"
|
||||
|
||||
#include "statesetupdater.hpp"
|
||||
|
||||
#include <osg/Drawable>
|
||||
#include <osg/Geode>
|
||||
|
||||
namespace SceneUtil
|
||||
{
|
||||
|
||||
|
||||
Controller::Controller()
|
||||
{
|
||||
}
|
||||
|
||||
bool Controller::hasInput() const
|
||||
{
|
||||
return mSource.get() != NULL;
|
||||
}
|
||||
|
||||
float Controller::getInputValue(osg::NodeVisitor* nv)
|
||||
{
|
||||
return mFunction->calculate(mSource->getValue(nv));
|
||||
}
|
||||
|
||||
FrameTimeSource::FrameTimeSource()
|
||||
{
|
||||
}
|
||||
|
||||
float FrameTimeSource::getValue(osg::NodeVisitor *nv)
|
||||
{
|
||||
return nv->getFrameStamp()->getSimulationTime();
|
||||
}
|
||||
|
||||
AssignControllerSourcesVisitor::AssignControllerSourcesVisitor()
|
||||
: osg::NodeVisitor(TRAVERSE_ALL_CHILDREN)
|
||||
{
|
||||
}
|
||||
|
||||
AssignControllerSourcesVisitor::AssignControllerSourcesVisitor(boost::shared_ptr<ControllerSource> toAssign)
|
||||
: mToAssign(toAssign)
|
||||
, osg::NodeVisitor(TRAVERSE_ALL_CHILDREN)
|
||||
{
|
||||
}
|
||||
|
||||
void AssignControllerSourcesVisitor::apply(osg::Node &node)
|
||||
{
|
||||
osg::NodeCallback* callback = node.getUpdateCallback();
|
||||
while (callback)
|
||||
{
|
||||
if (Controller* ctrl = dynamic_cast<Controller*>(callback))
|
||||
assign(node, *ctrl);
|
||||
if (CompositeStateSetUpdater* composite = dynamic_cast<CompositeStateSetUpdater*>(callback))
|
||||
{
|
||||
for (unsigned int i=0; i<composite->getNumControllers(); ++i)
|
||||
{
|
||||
StateSetUpdater* statesetcontroller = composite->getController(i);
|
||||
if (Controller* ctrl = dynamic_cast<Controller*>(statesetcontroller))
|
||||
assign(node, *ctrl);
|
||||
}
|
||||
}
|
||||
|
||||
callback = callback->getNestedCallback();
|
||||
}
|
||||
|
||||
traverse(node);
|
||||
}
|
||||
|
||||
void AssignControllerSourcesVisitor::apply(osg::Geode &geode)
|
||||
{
|
||||
for (unsigned int i=0; i<geode.getNumDrawables(); ++i)
|
||||
{
|
||||
osg::Drawable* drw = geode.getDrawable(i);
|
||||
osg::Drawable::UpdateCallback* callback = drw->getUpdateCallback();
|
||||
if (Controller* ctrl = dynamic_cast<Controller*>(callback))
|
||||
assign(geode, *ctrl);
|
||||
}
|
||||
}
|
||||
|
||||
void AssignControllerSourcesVisitor::assign(osg::Node&, Controller &ctrl)
|
||||
{
|
||||
if (!ctrl.mSource.get())
|
||||
ctrl.mSource = mToAssign;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,64 @@
|
||||
#ifndef OPENMW_COMPONENTS_SCENEUTIL_CONTROLLER_H
|
||||
#define OPENMW_COMPONENTS_SCENEUTIL_CONTROLLER_H
|
||||
|
||||
#include <boost/shared_ptr.hpp>
|
||||
|
||||
#include <osg/NodeVisitor>
|
||||
|
||||
namespace SceneUtil
|
||||
{
|
||||
|
||||
class ControllerSource
|
||||
{
|
||||
public:
|
||||
virtual float getValue(osg::NodeVisitor* nv) = 0;
|
||||
};
|
||||
|
||||
class FrameTimeSource : public ControllerSource
|
||||
{
|
||||
public:
|
||||
FrameTimeSource();
|
||||
virtual float getValue(osg::NodeVisitor* nv);
|
||||
};
|
||||
|
||||
class ControllerFunction
|
||||
{
|
||||
public:
|
||||
virtual float calculate(float input) = 0;
|
||||
};
|
||||
|
||||
class Controller
|
||||
{
|
||||
public:
|
||||
Controller();
|
||||
|
||||
bool hasInput() const;
|
||||
|
||||
float getInputValue(osg::NodeVisitor* nv);
|
||||
|
||||
boost::shared_ptr<ControllerSource> mSource;
|
||||
|
||||
// The source value gets passed through this function before it's passed on to the DestValue.
|
||||
boost::shared_ptr<ControllerFunction> mFunction;
|
||||
};
|
||||
|
||||
class AssignControllerSourcesVisitor : public osg::NodeVisitor
|
||||
{
|
||||
public:
|
||||
AssignControllerSourcesVisitor();
|
||||
AssignControllerSourcesVisitor(boost::shared_ptr<ControllerSource> toAssign);
|
||||
|
||||
virtual void apply(osg::Node& node);
|
||||
virtual void apply(osg::Geode& geode);
|
||||
|
||||
/// Assign the wanted ControllerSource. May be overriden in derived classes.
|
||||
/// By default assigns the ControllerSource passed to the constructor of this class if no ControllerSource is assigned to that controller yet.
|
||||
virtual void assign(osg::Node& node, Controller& ctrl);
|
||||
|
||||
private:
|
||||
boost::shared_ptr<ControllerSource> mToAssign;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue