颜色布景层类LayerColor
颜色布景层类LayerColor是Layer的子类,包含Layer类的特性,并且有两个拓展功能:为布景层增添颜色以及设置不透明度。
分析其源码:
class CC_DLL LayerColor : public Layer, public BlendProtocol
{
public:
/** Creates a fullscreen black layer.
*
* @return An autoreleased LayerColor object.
*/
static LayerColor* create();
/** Creates a Layer with color, width and height in Points.
*
* @param color The color of layer.
* @param width The width of layer.
* @param height The height of layer.
* @return An autoreleased LayerColor object.
*/
static LayerColor * create(const Color4B& color, GLfloat width, GLfloat height);
/** Creates a Layer with color. Width and height are the window size.
*
* @param color The color of layer.
* @return An autoreleased LayerColor object.
*/
static LayerColor * create(const Color4B& color);
/** Change width in Points.
*
* @param w The width of layer.
*/
void changeWidth(GLfloat w);
/** Change height in Points.
*
* @param h The height of layer.
*/
void changeHeight(GLfloat h);
/** Change width and height in Points.
*
* @param w The width of layer.
* @param h The Height of layer.
@since v0.8
*/
void changeWidthAndHeight(GLfloat w ,GLfloat h);
//
// Overrides
//
virtual void draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) override;
virtual void setContentSize(const Size & var) override;
/** BlendFunction. Conforms to BlendProtocol protocol */
/**
* @lua NA
*/
virtual const BlendFunc& getBlendFunc() const override;
/**
*@code
*When this function bound into js or lua,the parameter will be changed
*In js: var setBlendFunc(var src, var dst)
*In lua: local setBlendFunc(local src, local dst)
*@endcode
*/
virtual void setBlendFunc(const BlendFunc& blendFunc) override;
virtual std::string getDescription() const override;
CC_CONSTRUCTOR_ACCESS:
LayerColor();
virtual ~LayerColor();
bool init() override;
bool initWithColor(const Color4B& color, GLfloat width, GLfloat height);
bool initWithColor(const Color4B& color);
protected:
void onDraw(const Mat4& transform, uint32_t flags);
virtual void updateColor() override;
BlendFunc _blendFunc;
Vec2 _squareVertices[4];
Color4F _squareColors[4];
CustomCommand _customCommand;
Vec3 _noMVPVertices[4];
private:
CC_DISALLOW_COPY_AND_ASSIGN(LayerColor);
};
主要用法举例如下:
auto colorBack = LayerColor::create
( Color4B(170,170,170,255),
64*4+4*5, 64*4+4*5
);
colorBack->ignoreAnchorPointForPosition(false);
colorBack->setAnchorPoint(Point(0.5,0.5));
colorBack->setPosition(Point(visiblesize.width/2, visiblesize.height/2));
this->addChild(colorBack);
重点说明这句:colorBack->ignoreAnchorPointForPosition(false);
使用ignoreAnchorPointForPosition将是否忽略锚点置为false,由于是否忽略锚点的默认值是true,也就是忽略锚点,而以左下角为锚点。可以让布景层考虑锚点的影响
展示效果:
带有颜色渐变布景层类LayerGradient
class CC_DLL LayerGradient : public LayerColor
{
public:
/** Creates a fullscreen black layer.
*
* @return An autoreleased LayerGradient object.
*/
static LayerGradient* create();
/** Creates a full-screen Layer with a gradient between start and end.
*
* @param start The start color.
* @param end The end color.
* @return An autoreleased LayerGradient object.
*/
static LayerGradient* create(const Color4B& start, const Color4B& end);
/** Creates a full-screen Layer with a gradient between start and end in the direction of v.
*
* @param start The start color.
* @param end The end color.
* @param v The direction of gradient color.
* @return An autoreleased LayerGradient object.
*/
static LayerGradient* create(const Color4B& start, const Color4B& end, const Vec2& v);
Vec2(1,0)从左往右渐变
Vec2(-1,0)从右往左渐变
Vec2(0,1) 从下往上渐变
Vec2(0,-1)从上往下渐变
Vec2(1,1)从左下往右上渐变
Vec2(-1,1)从右下往左上渐变
Vec2(1,-1) 从左上往右下渐变
Vec2(-1,-1)从右上往左下渐变
/** Whether or not the interpolation will be compressed in order to display all the colors of the gradient both in canonical and non canonical vectors.
Default: true.
*
* @param compressedInterpolation The interpolation will be compressed if true.
*/
void setCompressedInterpolation(bool compressedInterpolation);
/** Get the compressedInterpolation
*
* @return The interpolation will be compressed if true.
*/
bool isCompressedInterpolation() const;
/** Sets the start color of the gradient.
*
* @param startColor The start color.
*/
void setStartColor( const Color3B& startColor );
/** Returns the start color of the gradient.
*
* @return The start color.
*/
const Color3B& getStartColor() const;
/** Sets the end color of the gradient.
*
* @param endColor The end color.
*/
void setEndColor( const Color3B& endColor );
/** Returns the end color of the gradient.
*
* @return The end color.
*/
const Color3B& getEndColor() const;
/** Returns the start opacity of the gradient.
*
* @param startOpacity The start opacity, from 0 to 255.
*/
void setStartOpacity( GLubyte startOpacity );
/** Returns the start opacity of the gradient.
*
* @return The start opacity.
*/
GLubyte getStartOpacity() const;
/** Returns the end opacity of the gradient.
*
* @param endOpacity The end opacity, from 0 to 255.
*/
void setEndOpacity( GLubyte endOpacity );
/** Returns the end opacity of the gradient.
*
* @return The end opacity.
*/
GLubyte getEndOpacity() const;
/** Sets the directional vector that will be used for the gradient.
The default value is vertical direction (0,-1).
*
* @param alongVector The direction of gradient.
*/
void setVector(const Vec2& alongVector);
/** Returns the directional vector used for the gradient.
*
* @return The direction of gradient.
*/
const Vec2& getVector() const;
virtual std::string getDescription() const override;
CC_CONSTRUCTOR_ACCESS:
LayerGradient();
virtual ~LayerGradient();
virtual bool init() override;
/** Initializes the Layer with a gradient between start and end.
* @js init
* @lua init
*/
bool initWithColor(const Color4B& start, const Color4B& end);
/** Initializes the Layer with a gradient between start and end in the direction of v.
* @js init
* @lua init
*/
bool initWithColor(const Color4B& start, const Color4B& end, const Vec2& v);
protected:
virtual void updateColor() override;
Color3B _startColor;
Color3B _endColor;
GLubyte _startOpacity;
GLubyte _endOpacity;
Vec2 _alongVector;
bool _compressedInterpolation;
};
具体实例:
LayerGradient* layergradient = LayerGradient::create
(Color4B(0,255,0,0),Color4B(0,0,255,255));
addChild(layergradient);