最近写的一些1.x扩展, 鼠标/键盘事件, iOS的手势识

  • 本帖最后由 robertbu 于 2012-7-11 22:53 编辑 *

代码和简单的两个例子:https://github.com/darkfall/cocos2dx-extensions

手势识别(iOS Only)
CCGestureRecognizer: 单件, 用于分发手势事件, 基于UIGestureRecognizer*
CCGestureListener: 手势事件监听器, 继承自这个类然后向CCGestureRecognizer里注册自身来监听手势事件

简单的例子:
class MyGestureListener: public CCNode, public CCGestureListener {
public:
MyGestureRecognizer() {
CCGestureRecognizer::Instance().attachListener(this);

    mPinchRecognizer = CCGestureRecognizer::Instance().addPinchRecognizer();
}
virtual ~MyGestureRecognizer() {
    CCGestureRecognizer::Instance().removeListener(this);

    CCGestureRecognizer::Instance().removeRecognizer(mPinchRecognizer);
}

virtual void onPinch(unsigned long which, float v, float scale, float x, float y) {
    printf("on pinch!");
}

private:
unsigned long mPinchRecognizer;
};

鼠标/键盘事件(OS X & Windows Only)
来源于我自己的引擎, 稍微修改了一下
需要修改2dx本身的相应platform的实现(在platform文件夹里)

cocos2dx for mac来自http://www.cocos2d-x.org/boards/6/topics/10352

CCInputEvent: 输入事件(CCKeyEvent = 键盘, CCMouseEvent = 鼠标)
CCInputDispathcer: 单件, 负责分发输入事件, 平台实现调用这个类来分发事件
CCInputListener: 输入事件监听器, 继承这个类然后向CCInputDispatcher里注册自身来监听

注意CCInputListener的接口返回值是bool, 代表这个listener是否独占这个event, 如果返回true, 则这个event不会继续向后分发

例子:
class MyInputListener: public cocos2d::CCInputListener, public cocos2d::CCNode {
public:
MyInputListener();
virtual ~MyInputListener();

virtual bool onMouseEvent(const cocos2d::CCMouseEvent& evt);
virtual bool onKeyEvent(const cocos2d::CCKeyEvent& evt);

};

MyInputListener::MyInputListener() {
CCInputDispatcher::Instance().addListener(this, 1);
}

MyInputListener::~MyInputListener() {
CCInputDispatcher::Instance().removeListener(this);
}

bool MyInputListener::onMouseEvent(const CCMouseEvent& evt) {
switch(evt.state) {
case CCMouse::Press:
printf("mouse button %d pressed!
", (int)evt.button);
break;

    case CCMouse::Release:
        printf("mouse button %d released!

", (int)evt.button);
break;

    default:
        break;
}
/* return true here to occupy the event */
return false;

}

bool MyInputListener::onKeyEvent(const CCKeyEvent& evt) {
switch (evt.state) {
case CCKey::Press:
printf("key %c pressed!
", evt.toCharCode());
break;

    case CCKey::Release:
        printf("key %c released!

", evt.toCharCode());
break;

    default:
        break;
}
/* return true here to occupy the event */
return false;

}

CCTexture2DMutable & CCRenderTextureMutable:
(iOS, OS X, Windows测试过)
CCTexture2DMutable: Created by Lam Hoang Pham, improved by Manuel Martinez-Almeida. 从论坛翻来直接用的, 细微做了些修改
CCRenderTextureMutable: 使用CCTexture2DMutable的CCRenderTexture, 这样在RTT之后就可以对Texture做些修改了, 例如做做后期效果

例子, 简单的灰度图:
CCTexture2DMutable* texture = mRenderLayer->getTexture();
int width = texture->getContentSize().width;
int height = texture->getContentSize().height;
unsigned int* data = texture->getDataRGBA();
assert(data);
for(int y = 0; y < height; ++y) {
for(int x = 0; x < width; ++x) {
uint32 color =
data;

    uint8 r = COLOR_GETR_RGBA(color);
    uint8 g = COLOR_GETG_RGBA(color);
    uint8 b = COLOR_GETB_RGBA(color);
    uint8 a = COLOR_GETA_RGBA(color);

    if(a == 0)
        continue;

    uint8 avg = 0.3 * r + 0.59 * g + 0.11 * b;

    data = COLOR_RGBA(avg, avg, avg, a);
}

}
texture->putDataRGBA();

支持下啊~

谢谢分享,留着以后用{:soso_e179:}

马上试试~~~~~~~~~~速度

收藏下。

这个不错 , 键盘, 鼠标 啊。

谢谢了,以后应该会用上的

支持下啊~

谢谢分享,留着以后用{:soso_e179:}

这个不错 , 键盘, 鼠标 啊。

谢谢了,以后应该会用上的