axmol/extensions/Particle3D/PU/CCPUDynamicAttribute.h

333 lines
9.0 KiB
C
Raw Normal View History

2015-02-11 18:14:22 +08:00
/****************************************************************************
2015-03-02 16:05:26 +08:00
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.
2021-12-25 10:04:45 +08:00
2022-10-01 16:24:52 +08:00
https://axmolengine.github.io/
2021-12-25 10:04:45 +08:00
2015-02-11 18:14:22 +08:00
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:
2021-12-25 10:04:45 +08:00
2015-02-11 18:14:22 +08:00
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
2021-12-25 10:04:45 +08:00
2015-02-11 18:14:22 +08:00
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.
****************************************************************************/
2022-07-15 19:17:01 +08:00
#ifndef __AX_PU_PARTICLE_3D_DYNAMIC_ATTRIBUTE_H__
#define __AX_PU_PARTICLE_3D_DYNAMIC_ATTRIBUTE_H__
2015-02-11 18:14:22 +08:00
#include "base/CCRef.h"
#include "math/CCMath.h"
2015-03-05 13:24:06 +08:00
#include "CCPUSimpleSpline.h"
2015-02-11 18:14:22 +08:00
#include <vector>
NS_AX_BEGIN
2015-02-11 18:14:22 +08:00
enum PUInterpolationType
{
IT_LINEAR,
IT_SPLINE
};
/** Comparer used for sorting vector in ascending order
2021-12-25 10:04:45 +08:00
*/
2015-02-11 18:14:22 +08:00
struct PUControlPointSorter
{
2021-12-25 10:04:45 +08:00
bool operator()(const Vec2& a, const Vec2& b) { return a.x < b.x; }
2015-02-11 18:14:22 +08:00
};
/* 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
2021-12-25 10:04:45 +08:00
application doesn't need to know about the underlying logic. A subclass could just return a value that has
2015-02-11 18:14:22 +08:00
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,
2021-12-25 10:04:45 +08:00
but where implementation of this behaviour may not be scattered or duplicated within the application that needs
2015-02-11 18:14:22 +08:00
it.
*/
2022-07-15 19:17:01 +08:00
class AX_EX_DLL PUDynamicAttribute : public Ref
2015-02-11 18:14:22 +08:00
{
2021-12-25 10:04:45 +08:00
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;
2015-02-11 18:14:22 +08:00
};
/* 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.
*/
2022-07-15 19:17:01 +08:00
class AX_EX_DLL PUDynamicAttributeFixed : public PUDynamicAttribute
2015-02-11 18:14:22 +08:00
{
2021-12-25 10:04:45 +08:00
public:
/** Constructor
*/
PUDynamicAttributeFixed();
2015-02-11 18:14:22 +08:00
2021-12-25 10:04:45 +08:00
/** Copy constructor
*/
PUDynamicAttributeFixed(const PUDynamicAttributeFixed& dynamicAttributeFixed);
2015-02-11 18:14:22 +08:00
2021-12-25 10:04:45 +08:00
/** Destructor
*/
~PUDynamicAttributeFixed();
2015-02-11 18:14:22 +08:00
2021-12-25 10:04:45 +08:00
/** Todo
*/
virtual float getValue(float x = 0) override;
2015-02-11 18:14:22 +08:00
2021-12-25 10:04:45 +08:00
/** Todo
*/
virtual void setValue(float value);
2015-02-11 18:14:22 +08:00
2021-12-25 10:04:45 +08:00
virtual PUDynamicAttributeFixed* clone() override;
virtual void copyAttributesTo(PUDynamicAttribute* dynamicAttribute) override;
2015-03-02 13:07:32 +08:00
2021-12-25 10:04:45 +08:00
protected:
float _value;
2015-02-11 18:14:22 +08:00
};
/* This class generates random values within a given minimum and maximum interval.
2021-12-25 10:04:45 +08:00
*/
2022-07-15 19:17:01 +08:00
class AX_EX_DLL PUDynamicAttributeRandom : public PUDynamicAttribute
2015-02-11 18:14:22 +08:00
{
2021-12-25 10:04:45 +08:00
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;
2015-03-02 13:07:32 +08:00
2015-02-11 18:14:22 +08:00
protected:
2021-12-25 10:04:45 +08:00
float _min, _max;
2015-02-11 18:14:22 +08:00
};
/* 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.
*/
2022-07-15 19:17:01 +08:00
class AX_EX_DLL PUDynamicAttributeCurved : public PUDynamicAttribute
2015-02-11 18:14:22 +08:00
{
2021-12-25 10:04:45 +08:00
public:
typedef std::vector<Vec2> ControlPointList;
2015-02-11 18:14:22 +08:00
2021-12-25 10:04:45 +08:00
/** Constructor
*/
PUDynamicAttributeCurved();
PUDynamicAttributeCurved(PUInterpolationType interpolationType);
2015-02-11 18:14:22 +08:00
2021-12-25 10:04:45 +08:00
/** Copy constructor
*/
PUDynamicAttributeCurved(const PUDynamicAttributeCurved& dynamicAttributeCurved);
2015-02-11 18:14:22 +08:00
2021-12-25 10:04:45 +08:00
/** Destructor
*/
~PUDynamicAttributeCurved();
2015-02-11 18:14:22 +08:00
2021-12-25 10:04:45 +08:00
/** Get and set the curve type
*/
void setInterpolationType(PUInterpolationType interpolationType);
PUInterpolationType getInterpolationType() const;
2015-02-11 18:14:22 +08:00
2021-12-25 10:04:45 +08:00
/** Todo
*/
virtual float getValue(float x = 0) override;
2015-02-11 18:14:22 +08:00
2021-12-25 10:04:45 +08:00
/** Todo
*/
virtual void addControlPoint(float x, float y);
2015-03-02 13:07:32 +08:00
2021-12-25 10:04:45 +08:00
/** Todo
*/
const ControlPointList& getControlPoints() const;
2015-02-11 18:14:22 +08:00
2021-12-25 10:04:45 +08:00
/** Todo
*/
void processControlPoints();
2015-02-11 18:14:22 +08:00
2021-12-25 10:04:45 +08:00
/** Todo
*/
size_t getNumControlPoints() const;
2015-02-11 18:14:22 +08:00
2021-12-25 10:04:45 +08:00
/** Todo
*/
void removeAllControlPoints();
2015-02-11 18:14:22 +08:00
2021-12-25 10:04:45 +08:00
virtual PUDynamicAttributeCurved* clone() override;
virtual void copyAttributesTo(PUDynamicAttribute* dynamicAttribute) override;
2015-02-11 18:14:22 +08:00
2021-12-25 10:04:45 +08:00
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();
2015-02-11 18:14:22 +08:00
};
/* This class generates values based on an oscillating function (i.e. Sine).
2021-12-25 10:04:45 +08:00
*/
2022-07-15 19:17:01 +08:00
class AX_EX_DLL PUDynamicAttributeOscillate : public PUDynamicAttribute
2015-02-11 18:14:22 +08:00
{
2021-12-25 10:04:45 +08:00
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;
2015-02-11 18:14:22 +08:00
};
/* Helper class to do some generic calculation.
2021-12-25 10:04:45 +08:00
*/
2015-02-11 18:14:22 +08:00
class PUDynamicAttributeHelper
{
2021-12-25 10:04:45 +08:00
public:
/* Return the value of a DynamicAttribute, given te x value.
*/
float calculate(PUDynamicAttribute* dyn, float x, float defaultValue = 0.0f);
2015-02-11 18:14:22 +08:00
};
NS_AX_END
2015-02-11 18:14:22 +08:00
#endif