You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
openmw-tes3mp/components/sceneutil/controller.hpp

109 lines
3.2 KiB
C++

#ifndef OPENMW_COMPONENTS_SCENEUTIL_CONTROLLER_H
#define OPENMW_COMPONENTS_SCENEUTIL_CONTROLLER_H
#include <memory>
#include <osg/NodeVisitor>
namespace SceneUtil
{
class ControllerSource
{
public:
10 years ago
virtual ~ControllerSource() { }
virtual float getValue(osg::NodeVisitor* nv) = 0;
};
class FrameTimeSource : public ControllerSource
{
public:
FrameTimeSource();
virtual float getValue(osg::NodeVisitor* nv);
};
/// @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:
Add virtual destructors To fix warnings: /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/7.3.1/../../../../include/c++/7.3.1/bits/shared_ptr_base.h:588:8: warning: delete called on non-final 'NifOsg::ControllerFunction' that has virtual functions but non-virtual destructor [-Wdelete-non-virtual-dtor] delete __p; ^ /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/7.3.1/../../../../include/c++/7.3.1/bits/shared_ptr_base.h:595:4: note: in instantiation of function template specialization 'std::__shared_count<__gnu_cxx::_S_atomic>::__shared_count<NifOsg::ControllerFunction *>' requested here : __shared_count(__p) ^ /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/7.3.1/../../../../include/c++/7.3.1/bits/shared_ptr_base.h:1079:17: note: in instantiation of function template specialization 'std::__shared_count<__gnu_cxx::_S_atomic>::__shared_count<NifOsg::ControllerFunction *>' requested here : _M_ptr(__p), _M_refcount(__p, typename is_array<_Tp>::type()) ^ /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/7.3.1/../../../../include/c++/7.3.1/bits/shared_ptr.h:129:25: note: in instantiation of function template specialization 'std::__shared_ptr<NifOsg::ControllerFunction, __gnu_cxx::_S_atomic>::__shared_ptr<NifOsg::ControllerFunction, void>' requested here shared_ptr(_Yp* __p) : __shared_ptr<_Tp>(__p) { } ^ /home/elsid/dev/openmw/components/nifosg/nifloader.cpp:242:39: note: in instantiation of function template specialization 'std::shared_ptr<NifOsg::ControllerFunction>::shared_ptr<NifOsg::ControllerFunction, void>' requested here callback->setFunction(std::shared_ptr<NifOsg::ControllerFunction>(new NifOsg::ControllerFunction(key))); ^ /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/7.3.1/../../../../include/c++/7.3.1/bits/unique_ptr.h:78:2: warning: delete called on non-final 'MWGui::ResponseCallback' that has virtual functions but non-virtual destructor [-Wdelete-non-virtual-dtor] delete __ptr; ^ /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/7.3.1/../../../../include/c++/7.3.1/bits/unique_ptr.h:268:4: note: in instantiation of member function 'std::default_delete<MWGui::ResponseCallback>::operator()' requested here get_deleter()(__ptr); ^ /home/elsid/dev/openmw/apps/openmw/mwgui/dialogue.cpp:58:23: note: in instantiation of member function 'std::unique_ptr<MWGui::ResponseCallback, std::default_delete<MWGui::ResponseCallback> >::~unique_ptr' requested here PersuasionDialog::PersuasionDialog(ResponseCallback* callback) ^
7 years ago
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();
virtual void apply(osg::Node& node);
// 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
virtual void apply(osg::MatrixTransform& node);
virtual void apply(osg::Geometry& node);
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.
virtual void visit(osg::Node& node, Controller& ctrl);
private:
std::shared_ptr<ControllerSource> mToAssign;
};
/// Finds the maximum of all controller functions in the given scene graph
class FindMaxControllerLengthVisitor : public ControllerVisitor
{
public:
FindMaxControllerLengthVisitor();
virtual void visit(osg::Node& , Controller& ctrl);
float getMaxLength() const;
private:
float mMaxLength;
};
}
#endif