File: FadeNodeAnimator.cpp

package info (click to toggle)
mygui 3.2.2%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 36,224 kB
  • sloc: cpp: 118,031; ansic: 30,202; xml: 15,544; cs: 12,602; tcl: 776; python: 417; makefile: 34
file content (161 lines) | stat: -rw-r--r-- 3,858 bytes parent folder | download | duplicates (4)
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*!
	@file
	@author     Albert Semenov
	@date       08/2008
*/
#include "FadeNodeAnimator.h"

namespace demo
{

	FadeNodeAnimator::FadeNodeAnimator() :
		mFadeDuration(0),
		mFadeType(0),
		mDestroy(true),
		mAlpha(0)
	{
		mSpeed.resize(getCount());
	}

	void FadeNodeAnimator::deserialization(MyGUI::xml::ElementPtr _node, MyGUI::Version _version)
	{
		MyGUI::xml::ElementEnumerator node = _node->getElementEnumerator();
		while (node.next("Property"))
		{
			const std::string& key = node->findAttribute("key");
			const std::string& value = node->findAttribute("value");

			if (key == "FadeDuration") mFadeDuration = MyGUI::utility::parseFloat(value);
			else if (key == "FadeType") mFadeType = MyGUI::utility::parseInt(value);//FIXME
		}
	}

	size_t FadeNodeAnimator::animate(
		bool _update,
		size_t _quad_count,
		MyGUI::VectorQuadData& _data,
		float _time,
		MyGUI::IVertexBuffer* _buffer,
		MyGUI::ITexture* _texture,
		const MyGUI::RenderTargetInfo& _info,
		const MyGUI::IntCoord& _coord,
		bool& _isAnimate
	)
	{

		addTime(_time);

		if (mDestroy && mAlpha == 0)
		{
			return _quad_count;
		}

		_isAnimate = true;

		_quad_count = tesselation(
			_quad_count,
			_data,
			_texture,
			_info,
			_coord);

		size_t index = 0;
		switch (mFadeType)
		{
		case 0: // random squares
			for (; index < _quad_count; ++index)
			{
				float alpha = pow(mAlpha, mSpeed[index]);

				unsigned int colour = 0xFFFFFF | ((unsigned int)(alpha * 255.0f) << 24);

				_data[index].vertex[MyGUI::QuadData::CornerLT].colour = colour;
				_data[index].vertex[MyGUI::QuadData::CornerRT].colour = colour;
				_data[index].vertex[MyGUI::QuadData::CornerLB].colour = colour;
				_data[index].vertex[MyGUI::QuadData::CornerRB].colour = colour;
			}
			break;
		case 1: // random not-squares
		case 2: // from center
		case 3: // TV
			for (int y = 0; y < getCountY() + 1; ++y)
			{
				for (int x = 0; x < getCountX() + 1; ++x)
				{
					index = x + y * getCountX();
					float speed = 1.0f;
					if (mFadeType == 1) // random not-squares
					{
						speed = (float)mSpeed[index % (getCountX() * getCountY())];
					}
					if (mFadeType == 2) // from center
					{
						float x1 = float(x - getCountX() / 2) / getCountX() * 2;
						float y1 = float(y - getCountY() / 2) / getCountY() * 2;
						speed = sqrt(x1 * x1 + y1 * y1);
					}
					if (mFadeType == 3) // TV
					{
						float x1 = float(x - getCountX() / 2) / getCountX() * 2;
						float y1 = float(y - getCountY() / 2) / getCountY() * 2;
						speed = sqrt(x1 * x1 * x1 * x1 + y1 * y1);
					}
					float alpha = pow(mAlpha, speed);

					unsigned int colour = 0xFFFFFF | ((unsigned int)(alpha * 255.0f) << 24);

					if ( x < getCountX() && y < getCountY() ) _data[index].vertex[MyGUI::QuadData::CornerLT].colour = colour;
					if ( x > 0       && y < getCountY() ) _data[index - 1].vertex[MyGUI::QuadData::CornerRT].colour = colour;
					if ( x < getCountX() && y > 0       ) _data[index - getCountX()].vertex[MyGUI::QuadData::CornerLB].colour = colour;
					if ( x > 0       && y > 0       ) _data[index - getCountX() - 1].vertex[MyGUI::QuadData::CornerRB].colour = colour;
				}
			}
			break;
		}

		return _quad_count;
	}

	void FadeNodeAnimator::addTime(float _time)
	{
		if (mDestroy)
		{
			if (mAlpha > 0)
			{
				mAlpha -= _time / mFadeDuration;
				if (mAlpha < 0) mAlpha = 0;
			}
		}
		else
		{
			if (mAlpha < 1)
			{
				mAlpha += _time / mFadeDuration;
				if (mAlpha > 1) mAlpha = 1;
			}
		}

	}

	void FadeNodeAnimator::create()
	{
		mDestroy = false;
		mAlpha = 0;

		int count = getCount();

		for (int index = 0; index < count; ++index)
			mSpeed[index] = ::rand() % 4 + 1;
	}

	void FadeNodeAnimator::destroy()
	{
		mDestroy = true;
	}

	void FadeNodeAnimator::attach(MyGUI::ILayerNode* _node)
	{
		_node->castType<MyGUI::RTTLayerNode>()->addLayerNodeAnimation(this);
	}

}