CCPUDynamicAttribute.h
9.69 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/****************************************************************************
Copyright (C) 2013 Henry van Merode. All rights reserved.
Copyright (c) 2015-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#ifndef __CC_PU_PARTICLE_3D_DYNAMIC_ATTRIBUTE_H__
#define __CC_PU_PARTICLE_3D_DYNAMIC_ATTRIBUTE_H__
#include "base/CCRef.h"
#include "math/CCMath.h"
#include "CCPUSimpleSpline.h"
#include <vector>
NS_CC_BEGIN
enum PUInterpolationType
{
IT_LINEAR,
IT_SPLINE
};
/** Comparer used for sorting vector in ascending order
*/
struct PUControlPointSorter
{
bool operator() (const Vec2& a, const Vec2& b)
{
return a.x < b.x;
}
};
/* The DynamicAttribute class or its child classes encapsulate an attribute with specific (dynamic) behaviour.
@remarks
This class provides a uniform interface for retrieving the value of an attribute, while the calculation of
this value may vary. Each subclass provides its own implementation of the getValue() function and the calling
application doesn't need to know about the underlying logic. A subclass could just return a value that has
previously been set, but it can also return a value that is randomly generated by a pre-defined min/max interval.
The DynamicAttribute class is used in situations where different behaviour of a certain attribute is needed,
but where implementation of this behaviour may not be scattered or duplicated within the application that needs
it.
*/
class CC_DLL PUDynamicAttribute : public Ref
{
public:
enum DynamicAttributeType
{
DAT_FIXED,
DAT_RANDOM,
DAT_CURVED,
DAT_OSCILLATE
};
/** Constructor
*/
PUDynamicAttribute ();
/** Destructor
*/
virtual ~PUDynamicAttribute ();
/** Virtual function that needs to be implemented by its childs.
*/
virtual float getValue (float x = 0) = 0;
/** Todo
*/
DynamicAttributeType getType () const;
/** Todo
*/
void setType (DynamicAttributeType type);
/** Returns true if one of the attributes was changed by an external source.
*/
bool isValueChangedExternally() const;
virtual void copyAttributesTo(PUDynamicAttribute* dynamicAttribute) = 0;
virtual PUDynamicAttribute* clone() = 0;
protected:
DynamicAttributeType _type;
bool _valueChangedExternally;
};
/* This class is an implementation of the DynamicAttribute class in its most simple form. It just returns a value
that has previously been set.
@remarks
Although use of a regular attribute within the class that needs it is preferred, its benefit is that it makes
use of the generic 'getValue' mechanism of a DynamicAttribute.
*/
class CC_DLL PUDynamicAttributeFixed : public PUDynamicAttribute
{
public:
/** Constructor
*/
PUDynamicAttributeFixed ();
/** Copy constructor
*/
PUDynamicAttributeFixed (const PUDynamicAttributeFixed& dynamicAttributeFixed);
/** Destructor
*/
~PUDynamicAttributeFixed ();
/** Todo
*/
virtual float getValue (float x = 0) override;
/** Todo
*/
virtual void setValue (float value);
virtual PUDynamicAttributeFixed* clone() override;
virtual void copyAttributesTo(PUDynamicAttribute* dynamicAttribute) override;
protected:
float _value;
};
/* This class generates random values within a given minimum and maximum interval.
*/
class CC_DLL PUDynamicAttributeRandom : public PUDynamicAttribute
{
public:
/** Constructor
*/
PUDynamicAttributeRandom ();
/** Copy constructor
*/
PUDynamicAttributeRandom (const PUDynamicAttributeRandom& dynamicAttributeRandom);
/** Destructor
*/
~PUDynamicAttributeRandom ();
/** Todo
*/
virtual float getValue (float x = 0) override;
/** Todo
*/
void setMin (float min);
float getMin () const;
void setMax (float max);
float getMax () const;
void setMinMax (float min, float max);
virtual PUDynamicAttributeRandom* clone() override;
virtual void copyAttributesTo(PUDynamicAttribute* dynamicAttribute) override;
protected:
float _min, _max;
};
/* This is a more complex usage of the DynamicAttribute principle. This class returns a value on an curve.
@remarks
After setting a number of control points, this class is able to interpolate a point on the curve that is based
on these control points. Interpolation is done in different flavours. linear?provides linear interpolation
of a value on the curve, while spline?generates a smooth curve and the returns a value that lies on that curve.
*/
class CC_DLL PUDynamicAttributeCurved : public PUDynamicAttribute
{
public:
typedef std::vector<Vec2> ControlPointList;
/** Constructor
*/
PUDynamicAttributeCurved ();
PUDynamicAttributeCurved (PUInterpolationType interpolationType);
/** Copy constructor
*/
PUDynamicAttributeCurved (const PUDynamicAttributeCurved& dynamicAttributeCurved);
/** Destructor
*/
~PUDynamicAttributeCurved ();
/** Get and set the curve type
*/
void setInterpolationType (PUInterpolationType interpolationType);
PUInterpolationType getInterpolationType () const;
/** Todo
*/
virtual float getValue (float x = 0) override;
/** Todo
*/
virtual void addControlPoint (float x, float y);
/** Todo
*/
const ControlPointList& getControlPoints () const;
/** Todo
*/
void processControlPoints ();
/** Todo
*/
size_t getNumControlPoints() const;
/** Todo
*/
void removeAllControlPoints();
virtual PUDynamicAttributeCurved* clone() override;
virtual void copyAttributesTo(PUDynamicAttribute* dynamicAttribute) override;
protected:
/** Todo
*/
float _range;
/** Todo
*/
PUSimpleSpline _spline;
/** Todo
*/
PUInterpolationType _interpolationType;
/** Todo
*/
ControlPointList _controlPoints;
/** Find an iterator that forms the low (left) value of the interval where x lies in.
*/
inline ControlPointList::iterator findNearestControlPointIterator(float x);
/** Helper functions
*/
inline ControlPointList::iterator getFirstValidIterator();
inline ControlPointList::iterator getLastValidIterator();
};
/* This class generates values based on an oscillating function (i.e. Sine).
*/
class CC_DLL PUDynamicAttributeOscillate : public PUDynamicAttribute
{
public:
enum OscillationType
{
OSCT_SINE,
OSCT_SQUARE
};
/** Constructor
*/
PUDynamicAttributeOscillate ();
/** Copy constructor
*/
PUDynamicAttributeOscillate (const PUDynamicAttributeOscillate& dynamicAttributeOscillate);
/** Destructor
*/
~PUDynamicAttributeOscillate ();
/** Todo
*/
virtual float getValue (float x = 0) override;
/** Get and set the OscillationType
*/
OscillationType getOscillationType () const;
void setOscillationType (OscillationType oscillationType);
/** Get and set the Frequency
*/
float getFrequency () const;
void setFrequency (float frequency);
/** Get and set the Phase
*/
float getPhase () const;
void setPhase (float phase);
/** Get and set the Base
*/
float getBase () const;
void setBase (float base);
/** Get and set the Amplitude
*/
float getAmplitude () const;
void setAmplitude (float amplitude);
virtual PUDynamicAttributeOscillate* clone() override;
virtual void copyAttributesTo(PUDynamicAttribute* dynamicAttribute) override;
protected:
OscillationType _oscillationType;
float _frequency;
float _phase;
float _base;
float _amplitude;
};
/* Helper class to do some generic calculation.
*/
class PUDynamicAttributeHelper
{
public:
/* Return the value of a DynamicAttribute, given te x value.
*/
float calculate(PUDynamicAttribute* dyn, float x, float defaultValue = 0.0f);
};
NS_CC_END
#endif