Something went wrong. Try again.
OpenMW is an open implementation of the Morrowind engine
Something went wrong. Try again.
3.8 kB · 150 lines
C++
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151#include "controller.hpp"
#include <algorithm>
#include "statesetupdater.hpp"
#include <osg/Drawable>#include <osg/Geometry>#include <osg/MatrixTransform>#include <osg/NodeCallback>
namespace SceneUtil{
Controller::Controller() {}
bool Controller::hasInput() const { return mSource.get() != nullptr; }
float Controller::getInputValue(osg::NodeVisitor* nv) { if (mFunction) return mFunction->calculate(mSource->getValue(nv)); else return mSource->getValue(nv); }
void Controller::setSource(std::shared_ptr<ControllerSource> source) { mSource = std::move(source); }
void Controller::setFunction(std::shared_ptr<ControllerFunction> function) { mFunction = std::move(function); }
std::shared_ptr<ControllerSource> Controller::getSource() const { return mSource; }
std::shared_ptr<ControllerFunction> Controller::getFunction() const { return mFunction; }
FrameTimeSource::FrameTimeSource() {}
float FrameTimeSource::getValue(osg::NodeVisitor* nv) { return static_cast<float>(nv->getFrameStamp()->getSimulationTime()); }
ControllerVisitor::ControllerVisitor() : osg::NodeVisitor(TRAVERSE_ALL_CHILDREN) { }
void ControllerVisitor::apply(osg::Node& node) { applyNode(node); }
void ControllerVisitor::apply(osg::MatrixTransform& node) { applyNode(node); }
void ControllerVisitor::apply(osg::Geometry& node) { applyNode(node); }
void ControllerVisitor::applyNode(osg::Node& node) { osg::Callback* callback = node.getUpdateCallback(); while (callback) { if (Controller* ctrl = dynamic_cast<Controller*>(callback)) visit(node, *ctrl); if (CompositeStateSetUpdater* composite = dynamic_cast<CompositeStateSetUpdater*>(callback)) { for (size_t i = 0; i < composite->getNumControllers(); ++i) { StateSetUpdater* statesetcontroller = composite->getController(i); if (Controller* ctrl = dynamic_cast<Controller*>(statesetcontroller)) visit(node, *ctrl); } }
callback = callback->getNestedCallback(); }
if (node.getNumChildrenRequiringUpdateTraversal() > 0) traverse(node); }
AssignControllerSourcesVisitor::AssignControllerSourcesVisitor() : ControllerVisitor() { }
AssignControllerSourcesVisitor::AssignControllerSourcesVisitor(std::shared_ptr<ControllerSource> toAssign) : ControllerVisitor() , mToAssign(std::move(toAssign)) { }
void AssignControllerSourcesVisitor::visit(osg::Node&, Controller& ctrl) { if (!ctrl.getSource()) ctrl.setSource(mToAssign); }
ForceControllerSourcesVisitor::ForceControllerSourcesVisitor() : AssignControllerSourcesVisitor() { }
ForceControllerSourcesVisitor::ForceControllerSourcesVisitor(std::shared_ptr<ControllerSource> toAssign) : AssignControllerSourcesVisitor(std::move(toAssign)) { }
void ForceControllerSourcesVisitor::visit(osg::Node&, Controller& ctrl) { ctrl.setSource(mToAssign); }
FindMaxControllerLengthVisitor::FindMaxControllerLengthVisitor() : SceneUtil::ControllerVisitor() , mMaxLength(0) { }
void FindMaxControllerLengthVisitor::visit(osg::Node&, Controller& ctrl) { if (ctrl.getFunction()) mMaxLength = std::max(mMaxLength, ctrl.getFunction()->getMaximum()); }
float FindMaxControllerLengthVisitor::getMaxLength() const { return mMaxLength; }
}