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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
#ifndef OPENMW_COMPONENTS_SCENEUTIL_CONTROLLER_H
#define OPENMW_COMPONENTS_SCENEUTIL_CONTROLLER_H
#include <memory>
#include <osg/NodeVisitor>
namespace SceneUtil
{
class ControllerSource
{
public:
virtual ~ControllerSource() {}
virtual float getValue(osg::NodeVisitor* nv) = 0;
};
class FrameTimeSource : public ControllerSource
{
public:
FrameTimeSource();
float getValue(osg::NodeVisitor* nv) override;
};
/// @note ControllerFunctions may be shared - you should not hold any state in it. That is why all its methods are
/// declared const.
class ControllerFunction
{
public:
virtual ~ControllerFunction() = default;
virtual float calculate(float input) const = 0;
/// Get the "stop time" of the controller function, typically the maximum of the calculate() function.
/// May not be meaningful for all types of controller functions.
virtual float getMaximum() const = 0;
};
class Controller
{
public:
Controller();
virtual ~Controller() {}
bool hasInput() const;
float getInputValue(osg::NodeVisitor* nv);
void setSource(std::shared_ptr<ControllerSource> source);
void setFunction(std::shared_ptr<ControllerFunction> function);
std::shared_ptr<ControllerSource> getSource() const;
std::shared_ptr<ControllerFunction> getFunction() const;
private:
std::shared_ptr<ControllerSource> mSource;
// The source value gets passed through this function before it's passed on to the DestValue.
std::shared_ptr<ControllerFunction> mFunction;
};
/// Pure virtual base class - visit() all controllers that are attached as UpdateCallbacks in a scene graph.
class ControllerVisitor : public osg::NodeVisitor
{
public:
ControllerVisitor();
void apply(osg::Node& node) override;
// Technically not required as the default implementation would trickle down to apply(Node&) anyway,
// but we'll shortcut instead to avoid the chain of virtual function calls
void apply(osg::MatrixTransform& node) override;
void apply(osg::Geometry& node) override;
void applyNode(osg::Node& node);
virtual void visit(osg::Node& node, Controller& ctrl) = 0;
};
class AssignControllerSourcesVisitor : public ControllerVisitor
{
public:
AssignControllerSourcesVisitor();
AssignControllerSourcesVisitor(std::shared_ptr<ControllerSource> toAssign);
/// Assign the wanted ControllerSource. May be overridden in derived classes.
/// By default assigns the ControllerSource passed to the constructor of this class if no ControllerSource is
/// assigned to that controller yet.
void visit(osg::Node& node, Controller& ctrl) override;
protected:
std::shared_ptr<ControllerSource> mToAssign;
};
class ForceControllerSourcesVisitor : public AssignControllerSourcesVisitor
{
public:
ForceControllerSourcesVisitor();
ForceControllerSourcesVisitor(std::shared_ptr<ControllerSource> toAssign);
/// Assign the wanted ControllerSource even if one is already assigned to the controller.
void visit(osg::Node& node, Controller& ctrl) override;
};
/// Finds the maximum of all controller functions in the given scene graph
class FindMaxControllerLengthVisitor : public ControllerVisitor
{
public:
FindMaxControllerLengthVisitor();
void visit(osg::Node&, Controller& ctrl) override;
float getMaxLength() const;
private:
float mMaxLength;
};
}
#endif
|