Skip to content

Commit

Permalink
GRAPHICS: Add Renderable::fade()
Browse files Browse the repository at this point in the history
This lets a Renderable be fade in or out.
  • Loading branch information
DrMcCoy committed Jan 30, 2014
1 parent b8ccd2e commit 8c705b8
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
60 changes: 59 additions & 1 deletion src/graphics/renderable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,19 @@
#include <OgreSceneNode.h>
#include <OgreAnimation.h>
#include <OgreSceneManager.h>
#include <OgreControllerManager.h>

#include "common/threads.h"

#include "graphics/util.h"
#include "graphics/renderable.h"
#include "graphics/materialman.h"

#include "events/requests.h"

namespace Graphics {

Renderable::Renderable(const Common::UString &scene) : _scene(scene), _rootNode(0), _visible(false), _selectable(false) {
Renderable::Renderable(const Common::UString &scene) : _scene(scene), _rootNode(0), _visible(false), _selectable(false), _fader(0) {
_basePosition[0] = 0.0;
_basePosition[1] = 0.0;
_basePosition[2] = 0.0;
Expand All @@ -52,6 +58,58 @@ Renderable::Renderable(const Common::UString &scene) : _scene(scene), _rootNode(
}

Renderable::~Renderable() {
destroy();
}

void Renderable::destroy() {
if (!Common::isMainThread()) {
Events::MainThreadFunctor<void> functor(boost::bind(&Renderable::destroy, this));

return RequestMan.callInMainThread(functor);
}

if (_fader)
Ogre::ControllerManager::getSingleton().destroyController(_fader);
_fader = 0;
}

void Renderable::fade(FadeDirection direction, float length, bool loop) {
LOCK_FRAME();

Ogre::ControllerManager &controllerMan = Ogre::ControllerManager::getSingleton();

if (_fader)
controllerMan.destroyController(_fader);
_fader = 0;

std::list<Ogre::MaterialPtr> materials;
collectMaterials(materials, true, true);

for (std::list<Ogre::MaterialPtr>::iterator m = materials.begin(); m != materials.end(); ++m)
MaterialMan.setTransparent(*m, true);

Ogre::SharedPtr< Ogre::ControllerValue <Ogre::Real> > matVal(new MaterialAlphaControllerValue(materials));
Ogre::SharedPtr< Ogre::ControllerFunction<Ogre::Real> > animFunc(new AnimationControllerFunction(length, 0.0, (AnimationFunction) direction, loop));

_fader = controllerMan.createController(controllerMan.getFrameTimeSource(), matVal, animFunc);
}

void Renderable::stopFade() {
LOCK_FRAME();

if (_fader) {
Ogre::ControllerManager::getSingleton().destroyController(_fader);

std::list<Ogre::MaterialPtr> materials;
collectMaterials(materials);

for (std::list<Ogre::MaterialPtr>::iterator m = materials.begin(); m != materials.end(); ++m) {
MaterialMan.setAlphaModifier(*m, 1.0);
MaterialMan.resetTransparent(*m);
}
}

_fader = 0;
}

Ogre::SceneNode *Renderable::getRootNode() {
Expand Down
19 changes: 19 additions & 0 deletions src/graphics/renderable.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,22 @@

#include "common/ustring.h"

#include "graphics/controllers.h"

namespace Ogre {
class SceneNode;
class Animation;
}

namespace Graphics {

enum FadeDirection {
kFadeDirectionIn = kAnimationFunctionIncrease , ///< Fade in.
kFadeDirectionOut = kAnimationFunctionDecrease , ///< Fade out.
kFadeDirectionInOut = kAnimationFunctionIncreaseDecrease, ///< Fade in, then out again.
kFadeDirectionOutIn = kAnimationFunctionDecreaseIncrease ///< Fade out, then in again.
};

class Renderable {
public:
Renderable(const Common::UString &scene = "world");
Expand Down Expand Up @@ -90,6 +99,11 @@ class Renderable {
/** Change whether the renderable can be selected (picked) by the user. */
virtual void setSelectable(bool selectable);

/** Fade the renderable. */
void fade(FadeDirection direction, float length, bool loop);
/** Stop all fading. */
void stopFade();

protected:
Common::UString _scene;

Expand All @@ -102,12 +116,17 @@ class Renderable {
float _baseOrientation[4];
float _baseScale[3];

Ogre::Controller<Ogre::Real> *_fader;


void destroyAnimation(const Common::UString &name);
void destroyAnimation(Ogre::Animation *anim);

/** Collect all materials used in the renderable, optionally making them dynamic and/or transparent as well. */
virtual void collectMaterials(std::list<Ogre::MaterialPtr> &materials, bool makeDynamic = false, bool makeTransparent = false) = 0;

private:
void destroy();
};

} // End of namespace Graphics
Expand Down
2 changes: 2 additions & 0 deletions src/graphics/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ class OgreAnimator : public Ogre::FrameListener {
for (Ogre::AnimationStateIterator anims = getOgreSceneManager().getAnimationStateIterator(); anims.hasMoreElements(); anims.moveNext())
anims.current()->second->addTime(event.timeSinceLastFrame);

Ogre::ControllerManager::getSingleton().updateAllControllers();

return true;
}

Expand Down

0 comments on commit 8c705b8

Please sign in to comment.