File: SkeletonState.h

package info (click to toggle)
mygui 3.4.3%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 38,792 kB
  • sloc: cpp: 133,849; ansic: 30,249; xml: 15,794; cs: 12,601; tcl: 776; python: 400; makefile: 35; sh: 4
file content (127 lines) | stat: -rw-r--r-- 2,429 bytes parent folder | download
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
121
122
123
124
125
126
127
/*!
	@file
	@author		Albert Semenov
	@date		08/2009
*/
#ifndef SKELETON_STATE_H_
#define SKELETON_STATE_H_

#include <Ogre.h>
#include "IAnimationNode.h"
#include "IAnimationGraph.h"

namespace animation
{

	class SkeletonState : public IAnimationNode
	{
	public:
		SkeletonState() :
			mState(nullptr)
		{
		}

		SkeletonState(std::string_view _name, IAnimationGraph* _graph) :
			IAnimationNode(_name, _graph),
			mState(nullptr)
		{
		}

		~SkeletonState() override
		{
			if (mState != nullptr)
				mState->setEnabled(false);
		}

		void setEvent(std::string_view _name, float _value = 0) override
		{
			if (!mState)
			{
				updateState();
				if (!mState)
					return;
			}

			if (_name == "Start")
				mState->setEnabled(true);
			else if (_name == "Stop")
				mState->setEnabled(false);
			else if (_name == "Position")
				mState->setTimePosition(_value);
			else if (_name == "Weight")
				mState->setWeight(_value);
		}

		void addConnection(std::string_view _eventout, IAnimationNode* _node, std::string_view _eventin) override
		{
			mConnections.emplace_back(_eventout, PairIn(_node, _eventin));
		}

		void setProperty(std::string_view _key, std::string_view _value) override
		{
			if (_key == "StateName")
			{
				if (mState != nullptr)
					mState->setEnabled(false);
				mState = nullptr;
				mStateName = _value;
			}
		}

		float getLength() override
		{
			if (!mState)
			{
				updateState();
				if (!mState)
					return 0;
			}
			return mState->getLength();
		}

		float getWeight()
		{
			if (mState == nullptr)
				return 0;
			return mState->getWeight();
		}

		float getPosition()
		{
			if (mState == nullptr)
				return 0;
			return mState->getTimePosition();
		}

		bool isEnabled()
		{
			if (mState == nullptr)
				return false;
			return mState->getEnabled();
		}

	private:
		void updateState()
		{
			Ogre::Any any = getGraph()->getData("OwnerEntity");
			if (any.has_value())
			{
				Ogre::Entity* entity = Ogre::any_cast<Ogre::Entity*>(any);
				entity->getSkeleton()->setBlendMode(Ogre::ANIMBLEND_CUMULATIVE);
				mState = entity->getAnimationState(mStateName);
			}
		}

	private:
		Ogre::AnimationState* mState;
		using PairIn = std::pair<IAnimationNode*, std::string>;
		using PairOut = std::pair<std::string, PairIn>;
		using VectorPairOut = std::vector<PairOut>;
		VectorPairOut mConnections;

		std::string mStateName;
	};

} // namespace animation

#endif // SKELETON_STATE_H_