CCLayer(集中了触摸键盘重力消息的委托处理 子类:CCLayerRGBA、Color、Gradient-渐变颜色层 CCLayerMultiplex-内部有一个层数组可以方便的切换显示层)

本文深入探讨了Cocos2d-x框架中的CCLayer类,包括其基本功能、触摸事件处理、重力消息接收、键盘事件支持以及颜色属性等高级特性。详细解释了如何使用CCLayer类来实现复杂的游戏逻辑和用户交互。



#ifndef __CCLAYER_H__

#define __CCLAYER_H__


#include "base_nodes/CCNode.h"

#include "CCProtocols.h"

#include "touch_dispatcher/CCTouchDelegateProtocol.h"

#include "platform/CCAccelerometerDelegate.h"

#include "keypad_dispatcher/CCKeypadDelegate.h"

#include "cocoa/CCArray.h"

#ifdef EMSCRIPTEN

#include "base_nodes/CCGLBufferedNode.h"

#endif // EMSCRIPTEN


NS_CC_BEGIN


typedef enum {   //触摸处理枚举 1、一次处理所有触摸 2、一个一个处理

kCCTouchesAllAtOnce,

kCCTouchesOneByOne,

} ccTouchesMode;


/**

 * @addtogroup layer

 * @{

 */


class CCTouchScriptHandlerEntry;


//

// CCLayer

//

/** @brief CCLayer is a subclass of CCNode that implements the TouchEventsDelegate protocol.


All features from CCNode are valid, plus the following new features:

- It can receive iPhone Touches   //触摸消息

- It can receive Accelerometer input //重力消息

*/

class CC_DLL CCLayer : public CCNode, public CCTouchDelegate, public CCAccelerometerDelegate, public CCKeypadDelegate

{

public:

    CCLayer();

    virtual ~CCLayer();

    virtual bool init();

    

    /** create one layer */

    static CCLayer *create(void);


    virtual void onEnter();

    virtual void onExit();

    virtual void onEnterTransitionDidFinish();

    

    // default implements are used to call script callback if exist

    virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);

    virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);

    virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);

    virtual void ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent);


    // default implements are used to call script callback if exist

    virtual void ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent);

    virtual void ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent);

    virtual void ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent);

    virtual void ccTouchesCancelled(CCSet *pTouches, CCEvent *pEvent);

    

    virtual void didAccelerate(CCAcceleration* pAccelerationValue);

    void registerScriptAccelerateHandler(int nHandler);

    void unregisterScriptAccelerateHandler(void);


    /** If isTouchEnabled, this method is called onEnter. Override it to change the

    way CCLayer receives touch events.

    ( Default: CCTouchDispatcher::sharedDispatcher()->addStandardDelegate(this,0); )

    Example:

    void CCLayer::registerWithTouchDispatcher()

    {

    CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this,INT_MIN+1,true);

    }

    @since v0.8.0

    */

    virtual void registerWithTouchDispatcher(void);

    

    /** Register script touch events handler */

    virtual void registerScriptTouchHandler(int nHandler, bool bIsMultiTouches = false, int nPriority = INT_MIN, bool bSwallowsTouches = false);

    /** Unregister script touch events handler */

    virtual void unregisterScriptTouchHandler(void);


    /** whether or not it will receive Touch events.

    You can enable / disable touch events with this property.

    Only the touches of this node will be affected. This "method" is not propagated to it's children.

    @since v0.8.1

    */

    virtual bool isTouchEnabled();

    virtual void setTouchEnabled(bool value); //设置层是否接受触摸

    

    virtual void setTouchMode(ccTouchesMode mode); //设置触摸模式

    virtual int getTouchMode();

    

    /** priority of the touch events. Default is 0 */

    virtual void setTouchPriority(int priority); //设置触摸优先级

    virtual int getTouchPriority();


    /** whether or not it will receive Accelerometer events

    You can enable / disable accelerometer events with this property.

    @since v0.8.1

    */

    virtual bool isAccelerometerEnabled(); //重力相关

    virtual void setAccelerometerEnabled(bool value);

    virtual void setAccelerometerInterval(double interval);


    /** whether or not it will receive keypad events

    You can enable / disable accelerometer events with this property.

    it's new in cocos2d-x

    */

    virtual bool isKeypadEnabled();  //键盘相关

    virtual void setKeypadEnabled(bool value);


    /** Register keypad events handler */

    void registerScriptKeypadHandler(int nHandler);

    /** Unregister keypad events handler */

    void unregisterScriptKeypadHandler(void);


    virtual void keyBackClicked(void);//键盘相关

    virtual void keyMenuClicked(void);

    

    inline CCTouchScriptHandlerEntry* getScriptTouchHandlerEntry() { return m_pScriptTouchHandlerEntry; };

    inline CCScriptHandlerEntry* getScriptKeypadHandlerEntry() { return m_pScriptKeypadHandlerEntry; };

    inline CCScriptHandlerEntry* getScriptAccelerateHandlerEntry() { return m_pScriptAccelerateHandlerEntry; };

protected:   

    bool m_bTouchEnabled;

    bool m_bAccelerometerEnabled;

    bool m_bKeypadEnabled;

    

private:

    // Script touch events handler(这个东西 有时间看看。。。。。。。。。)

    CCTouchScriptHandlerEntry* m_pScriptTouchHandlerEntry;

    CCScriptHandlerEntry* m_pScriptKeypadHandlerEntry;

    CCScriptHandlerEntry* m_pScriptAccelerateHandlerEntry;

    

    int m_nTouchPriority;

    ccTouchesMode m_eTouchMode;

    

    int  excuteScriptTouchHandler(int nEventType, CCTouch *pTouch);

    int  excuteScriptTouchHandler(int nEventType, CCSet *pTouches);

};


#ifdef __apple__

#pragma mark -

#pragma mark CCLayerRGBA

#endif


/** CCLayerRGBA is a subclass of CCLayer that implements the CCRGBAProtocol protocol using a solid color as the background.

 

 All features from CCLayer are valid, plus the following new features that propagate into children that conform to the CCRGBAProtocol:

 - opacity

 - RGB colors

 @since 2.1

 */

class CC_DLL CCLayerRGBA : public CCLayer, public CCRGBAProtocol  //有颜色属性的 cclayer

{

public:

    CREATE_FUNC(CCLayerRGBA);

    

    CCLayerRGBA();

    virtual ~CCLayerRGBA();

    

    virtual bool init();

    

    virtual GLubyte getOpacity(); //透明度相关

    virtual GLubyte getDisplayedOpacity();

    virtual void setOpacity(GLubyte opacity);

    virtual void updateDisplayedOpacity(GLubyte parentOpacity);

    virtual bool isCascadeOpacityEnabled();

    virtual void setCascadeOpacityEnabled(bool cascadeOpacityEnabled);

    

    virtual const ccColor3B& getColor(); //颜色相关

    virtual const ccColor3B& getDisplayedColor();

    virtual void setColor(const ccColor3B& color);

    virtual void updateDisplayedColor(const ccColor3B& parentColor);

    virtual bool isCascadeColorEnabled();

    virtual void setCascadeColorEnabled(bool cascadeColorEnabled);

    

    virtual void setOpacityModifyRGB(bool bValue) {}

    virtual bool isOpacityModifyRGB() { return false; }

protected:

GLubyte_displayedOpacity, _realOpacity;

ccColor3B_displayedColor, _realColor;

bool_cascadeOpacityEnabled, _cascadeColorEnabled;

};


//

// CCLayerColor

//

/** @brief CCLayerColor is a subclass of CCLayer that implements the CCRGBAProtocol protocol.


All features from CCLayer are valid, plus the following new features:

- opacity

- RGB colors

*/

class CC_DLL CCLayerColor : public CCLayerRGBA, public CCBlendProtocol //颜色layer  继承自CCLayerRGBA  扩展了CCLayerRGBA(只能设置颜色 透明度)的功能  可以改尺寸 改颜色

#ifdef EMSCRIPTEN

, public CCGLBufferedNode

#endif // EMSCRIPTEN

{

protected:

    ccVertex2F m_pSquareVertices[4];

    ccColor4F  m_pSquareColors[4];


public:

    CCLayerColor();

    virtual ~CCLayerColor();


    virtual void draw();

    virtual void setContentSize(const CCSize & var);

    

    static CCLayerColor* create();

    

    /** creates a CCLayer with color, width and height in Points */

    static CCLayerColor * create(const ccColor4B& color, GLfloat width, GLfloat height);

    /** creates a CCLayer with color. Width and height are the window size. */

    static CCLayerColor * create(const ccColor4B& color);


    virtual bool init();

    /** initializes a CCLayer with color, width and height in Points */

    virtual bool initWithColor(const ccColor4B& color, GLfloat width, GLfloat height);

    /** initializes a CCLayer with color. Width and height are the window size. */

    virtual bool initWithColor(const ccColor4B& color);


    /** change width in Points*/

    void changeWidth(GLfloat w);

    /** change height in Points*/

    void changeHeight(GLfloat h);

    /** change width and height in Points

    @since v0.8

    */

    void changeWidthAndHeight(GLfloat w ,GLfloat h);


    /** BlendFunction. Conforms to CCBlendProtocol protocol */

    CC_PROPERTY(ccBlendFunc, m_tBlendFunc, BlendFunc)


    virtual void setOpacityModifyRGB(bool bValue) {CC_UNUSED_PARAM(bValue);}

    virtual bool isOpacityModifyRGB(void) { return false;}

    virtual void setColor(const ccColor3B &color);

    virtual void setOpacity(GLubyte opacity);


protected:

    virtual void updateColor();

};


//

// CCLayerGradient

//

/** @brief CCLayerGradient is a subclass of CCLayerColor that draws gradients across the background.


All features from CCLayerColor are valid, plus the following new features:

- direction

- final color

- interpolation mode


Color is interpolated between the startColor and endColor along the given

vector (starting at the origin, ending at the terminus).  If no vector is

supplied, it defaults to (0, -1) -- a fade from top to bottom.


If 'compressedInterpolation' is disabled, you will not see either the start or end color for

non-cardinal vectors; a smooth gradient implying both end points will be still

be drawn, however.


If ' compressedInterpolation' is enabled (default mode) you will see both the start and end colors of the gradient.


@since v0.99.5

*/

class CC_DLL CCLayerGradient : public CCLayerColor  //有颜色渐变的 CCLayerColor

{

public:


    /** Creates a full-screen CCLayer with a gradient between start and end. */

    static CCLayerGradient* create(const ccColor4B& start, const ccColor4B& end);


    /** Creates a full-screen CCLayer with a gradient between start and end in the direction of v. */

    static CCLayerGradient* create(const ccColor4B& start, const ccColor4B& end, const CCPoint& v);


    virtual bool init();

    /** Initializes the CCLayer with a gradient between start and end. */

    virtual bool initWithColor(const ccColor4B& start, const ccColor4B& end);


    /** Initializes the CCLayer with a gradient between start and end in the direction of v. */

    virtual bool initWithColor(const ccColor4B& start, const ccColor4B& end, const CCPoint& v);


    CC_PROPERTY_PASS_BY_REF(ccColor3B, m_startColor, StartColor)

    CC_PROPERTY_PASS_BY_REF(ccColor3B, m_endColor, EndColor)

    CC_PROPERTY(GLubyte, m_cStartOpacity, StartOpacity)

    CC_PROPERTY(GLubyte, m_cEndOpacity, EndOpacity)

    CC_PROPERTY_PASS_BY_REF(CCPoint, m_AlongVector, Vector)


    /** 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: YES

    */

protected:

    bool m_bCompressedInterpolation;

public:

    virtual void setCompressedInterpolation(bool bCompressedInterpolation); //设置压缩插值

    virtual bool isCompressedInterpolation();

    

    static CCLayerGradient* create();


protected:

    virtual void updateColor();

};



/** @brief CCMultipleLayer is a CCLayer with the ability to multiplex it's children.

Features:

- It supports one or more children

- Only one children will be active a time

*/

class CC_DLL CCLayerMultiplex : public CCLayer //多样化 层  可以通过多个child层初始化创建 通过switchTo(unsigned int n)--remove当前层 add参数层               switchToAndReleaseMe(unsigned int n)---remove当前层 并把这个层从m_pLayers数组中剔除 add参数层  

{

protected:

    unsigned int m_nEnabledLayer;  //当前child层id

    CCArray*     m_pLayers;   //所有可以索引到得层数组

public:

    CCLayerMultiplex();

    virtual ~CCLayerMultiplex();

    

    static CCLayerMultiplex* create();

    

    /** creates a CCMultiplexLayer with an array of layers.

     @since v2.1

     */

    static CCLayerMultiplex* createWithArray(CCArray* arrayOfLayers);


    /** creates a CCLayerMultiplex with one or more layers using a variable argument list. */

    static CCLayerMultiplex * create(CCLayer* layer, ... );


    /**

     * lua script can not init with undetermined number of variables

     * so add these functions to be used with lua.

     */

    static CCLayerMultiplex * createWithLayer(CCLayer* layer);


    void addLayer(CCLayer* layer);


    /** initializes a MultiplexLayer with one or more layers using a variable argument list. */

    bool initWithLayers(CCLayer* layer, va_list params);

    /** switches to a certain layer indexed by n. 

    The current (old) layer will be removed from it's parent with 'cleanup:YES'.

    */


    /** initializes a CCMultiplexLayer with an array of layers

    @since v2.1

    */

    bool initWithArray(CCArray* arrayOfLayers);


    void switchTo(unsigned int n);

{

    CCAssert( n < m_pLayers->count(), "Invalid index in MultiplexLayer switchTo message" );


    this->removeChild((CCNode*)m_pLayers->objectAtIndex(m_nEnabledLayer), true);


    m_nEnabledLayer = n;


    this->addChild((CCNode*)m_pLayers->objectAtIndex(n));

}


    /** release the current layer and switches to another layer indexed by n.

    The current (old) layer will be removed from it's parent with 'cleanup:YES'.

    */

    void switchToAndReleaseMe(unsigned int n);

{

    CCAssert( n < m_pLayers->count(), "Invalid index in MultiplexLayer switchTo message" );


    this->removeChild((CCNode*)m_pLayers->objectAtIndex(m_nEnabledLayer), true);


    //[layers replaceObjectAtIndex:enabledLayer withObject:[NSNull null]];

    m_pLayers->replaceObjectAtIndex(m_nEnabledLayer, NULL);


    m_nEnabledLayer = n;


    this->addChild((CCNode*)m_pLayers->objectAtIndex(n));

}


};



// end of layer group

/// @}


NS_CC_END


#endif // __CCLAYER_H__


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值