mirror of https://github.com/axmolengine/axmol.git
Merge pull request #1704 from minggo/cclayer_script_support
Cclayer script support
This commit is contained in:
commit
bbcbfa71ef
|
@ -46,7 +46,9 @@ CCLayer::CCLayer()
|
|||
: m_bTouchEnabled(false)
|
||||
, m_bAccelerometerEnabled(false)
|
||||
, m_bKeypadEnabled(false)
|
||||
, m_pScriptHandlerEntry(NULL)
|
||||
,m_pScriptTouchHandlerEntry(NULL)
|
||||
,m_pScriptKeypadHandlerEntry(NULL)
|
||||
,m_pScriptAccelerateHandlerEntry(NULL)
|
||||
, m_eTouchMode(kCCTouchesAllAtOnce)
|
||||
, m_nTouchPriority(0)
|
||||
{
|
||||
|
@ -57,6 +59,8 @@ CCLayer::CCLayer()
|
|||
CCLayer::~CCLayer()
|
||||
{
|
||||
unregisterScriptTouchHandler();
|
||||
unregisterScriptKeypadHandler();
|
||||
unregisterScriptAccelerateHandler();
|
||||
}
|
||||
|
||||
bool CCLayer::init()
|
||||
|
@ -102,19 +106,19 @@ void CCLayer::registerWithTouchDispatcher()
|
|||
CCTouchDispatcher* pDispatcher = CCDirector::sharedDirector()->getTouchDispatcher();
|
||||
|
||||
// Using LuaBindings
|
||||
if (m_pScriptHandlerEntry)
|
||||
if (m_pScriptTouchHandlerEntry)
|
||||
{
|
||||
if (m_pScriptHandlerEntry->isMultiTouches())
|
||||
if (m_pScriptTouchHandlerEntry->isMultiTouches())
|
||||
{
|
||||
pDispatcher->addStandardDelegate(this, 0);
|
||||
LUALOG("[LUA] Add multi-touches event handler: %d", m_pScriptHandlerEntry->getHandler());
|
||||
LUALOG("[LUA] Add multi-touches event handler: %d", m_pScriptTouchHandlerEntry->getHandler());
|
||||
}
|
||||
else
|
||||
{
|
||||
pDispatcher->addTargetedDelegate(this,
|
||||
m_pScriptHandlerEntry->getPriority(),
|
||||
m_pScriptHandlerEntry->getSwallowsTouches());
|
||||
LUALOG("[LUA] Add touch event handler: %d", m_pScriptHandlerEntry->getHandler());
|
||||
m_pScriptTouchHandlerEntry->getPriority(),
|
||||
m_pScriptTouchHandlerEntry->getSwallowsTouches());
|
||||
LUALOG("[LUA] Add touch event handler: %d", m_pScriptTouchHandlerEntry->getHandler());
|
||||
}
|
||||
}
|
||||
else
|
||||
|
@ -130,13 +134,13 @@ void CCLayer::registerWithTouchDispatcher()
|
|||
void CCLayer::registerScriptTouchHandler(int nHandler, bool bIsMultiTouches, int nPriority, bool bSwallowsTouches)
|
||||
{
|
||||
unregisterScriptTouchHandler();
|
||||
m_pScriptHandlerEntry = CCTouchScriptHandlerEntry::create(nHandler, bIsMultiTouches, nPriority, bSwallowsTouches);
|
||||
m_pScriptHandlerEntry->retain();
|
||||
m_pScriptTouchHandlerEntry = CCTouchScriptHandlerEntry::create(nHandler, bIsMultiTouches, nPriority, bSwallowsTouches);
|
||||
m_pScriptTouchHandlerEntry->retain();
|
||||
}
|
||||
|
||||
void CCLayer::unregisterScriptTouchHandler(void)
|
||||
{
|
||||
CC_SAFE_RELEASE_NULL(m_pScriptHandlerEntry);
|
||||
CC_SAFE_RELEASE_NULL(m_pScriptTouchHandlerEntry);
|
||||
}
|
||||
|
||||
int CCLayer::excuteScriptTouchHandler(int nEventType, CCTouch *pTouch)
|
||||
|
@ -262,6 +266,17 @@ void CCLayer::didAccelerate(CCAcceleration* pAccelerationValue)
|
|||
}
|
||||
}
|
||||
|
||||
void CCLayer::registerScriptAccelerateHandler(int nHandler)
|
||||
{
|
||||
unregisterScriptAccelerateHandler();
|
||||
m_pScriptAccelerateHandlerEntry = CCScriptHandlerEntry::create(nHandler);
|
||||
m_pScriptAccelerateHandlerEntry->retain();
|
||||
}
|
||||
|
||||
void CCLayer::unregisterScriptAccelerateHandler(void)
|
||||
{
|
||||
CC_SAFE_RELEASE_NULL(m_pScriptAccelerateHandlerEntry);
|
||||
}
|
||||
|
||||
/// isKeypadEnabled getter
|
||||
bool CCLayer::isKeypadEnabled()
|
||||
|
@ -290,6 +305,34 @@ void CCLayer::setKeypadEnabled(bool enabled)
|
|||
}
|
||||
}
|
||||
|
||||
void CCLayer::registerScriptKeypadHandler(int nHandler)
|
||||
{
|
||||
unregisterScriptKeypadHandler();
|
||||
m_pScriptKeypadHandlerEntry = CCScriptHandlerEntry::create(nHandler);
|
||||
m_pScriptKeypadHandlerEntry->retain();
|
||||
}
|
||||
|
||||
void CCLayer::unregisterScriptKeypadHandler(void)
|
||||
{
|
||||
CC_SAFE_RELEASE_NULL(m_pScriptKeypadHandlerEntry);
|
||||
}
|
||||
|
||||
void CCLayer::keyBackClicked(void)
|
||||
{
|
||||
if (m_pScriptKeypadHandlerEntry)
|
||||
{
|
||||
CCScriptEngineManager::sharedManager()->getScriptEngine()->executeLayerKeypadEvent(this, kTypeBackClicked);
|
||||
}
|
||||
}
|
||||
|
||||
void CCLayer::keyMenuClicked(void)
|
||||
{
|
||||
if (m_pScriptKeypadHandlerEntry)
|
||||
{
|
||||
CCScriptEngineManager::sharedManager()->getScriptEngine()->executeLayerKeypadEvent(this, kTypeMenuClicked);
|
||||
}
|
||||
}
|
||||
|
||||
/// Callbacks
|
||||
void CCLayer::onEnter()
|
||||
{
|
||||
|
|
|
@ -86,6 +86,8 @@ public:
|
|||
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.
|
||||
|
@ -133,8 +135,18 @@ public:
|
|||
*/
|
||||
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* getScriptHandlerEntry() { return m_pScriptHandlerEntry; };
|
||||
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;
|
||||
|
@ -142,7 +154,9 @@ protected:
|
|||
|
||||
private:
|
||||
// Script touch events handler
|
||||
CCTouchScriptHandlerEntry* m_pScriptHandlerEntry;
|
||||
CCTouchScriptHandlerEntry* m_pScriptTouchHandlerEntry;
|
||||
CCScriptHandlerEntry* m_pScriptKeypadHandlerEntry;
|
||||
CCScriptHandlerEntry* m_pScriptAccelerateHandlerEntry;
|
||||
|
||||
int m_nTouchPriority;
|
||||
ccTouchesMode m_eTouchMode;
|
||||
|
@ -338,9 +352,9 @@ public:
|
|||
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
|
||||
*/
|
||||
/** initializes a CCMultiplexLayer with an array of layers
|
||||
@since v2.1
|
||||
*/
|
||||
bool initWithArray(CCArray* arrayOfLayers);
|
||||
|
||||
void switchTo(unsigned int n);
|
||||
|
@ -353,8 +367,8 @@ public:
|
|||
static CCLayerMultiplex* node();
|
||||
|
||||
static CCLayerMultiplex* create();
|
||||
/** creates a CCMultiplexLayer with an array of layers.
|
||||
@since v2.1
|
||||
/** creates a CCMultiplexLayer with an array of layers.
|
||||
@since v2.1
|
||||
*/
|
||||
static CCLayerMultiplex* createWithArray(CCArray* arrayOfLayers);
|
||||
};
|
||||
|
|
|
@ -29,6 +29,7 @@
|
|||
#include "CCAccelerometer.h"
|
||||
#include "touch_dispatcher/CCTouch.h"
|
||||
#include "cocoa/CCSet.h"
|
||||
#include "CCAccelerometer.h"
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <list>
|
||||
|
@ -226,6 +227,9 @@ public:
|
|||
virtual int executeLayerTouchesEvent(CCLayer* pLayer, int eventType, CCSet *pTouches) = 0;
|
||||
virtual int executeLayerTouchEvent(CCLayer* pLayer, int eventType, CCTouch *pTouch) = 0;
|
||||
|
||||
/** functions for keypad event */
|
||||
virtual int executeLayerKeypadEvent(CCLayer* pLayer, int eventType) = 0;
|
||||
|
||||
/** execute a accelerometer event */
|
||||
virtual int executeAccelerometerEvent(CCLayer* pLayer, CCAcceleration* pAccelerationValue) = 0;
|
||||
};
|
||||
|
|
|
@ -1 +1 @@
|
|||
a9d5159141c92d549c9d9e8c9fed53517792589d
|
||||
277dcf61e5fb78a98293b107aa71f6d71b836f93
|
|
@ -809,6 +809,11 @@ int ScriptingCore::executeAccelerometerEvent(CCLayer *pLayer, CCAcceleration *pA
|
|||
return 1;
|
||||
}
|
||||
|
||||
int ScriptingCore::executeLayerKeypadEvent(CCLayer* pLayer, int eventType)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int ScriptingCore::executeCustomTouchesEvent(int eventType,
|
||||
CCSet *pTouches, JSObject *obj)
|
||||
|
@ -925,39 +930,39 @@ CCAcceleration jsval_to_ccacceleration(JSContext *cx, jsval v) {
|
|||
return ret;
|
||||
}
|
||||
|
||||
CCArray* jsvals_variadic_to_ccarray( JSContext *cx, jsval *vp, int argc)
|
||||
{
|
||||
JSBool ok = JS_FALSE;
|
||||
CCArray* pArray = CCArray::create();
|
||||
for( int i=0; i < argc; i++ )
|
||||
{
|
||||
double num = 0.0;
|
||||
// optimization: JS_ValueToNumber is expensive. And can convert an string like "12" to a number
|
||||
if( JSVAL_IS_NUMBER(*vp)) {
|
||||
ok = JS_ValueToNumber(cx, *vp, &num );
|
||||
if (!ok) {
|
||||
break;
|
||||
}
|
||||
pArray->addObject(CCInteger::create((int)num));
|
||||
}
|
||||
CCArray* jsvals_variadic_to_ccarray( JSContext *cx, jsval *vp, int argc)
|
||||
{
|
||||
JSBool ok = JS_FALSE;
|
||||
CCArray* pArray = CCArray::create();
|
||||
for( int i=0; i < argc; i++ )
|
||||
{
|
||||
double num = 0.0;
|
||||
// optimization: JS_ValueToNumber is expensive. And can convert an string like "12" to a number
|
||||
if( JSVAL_IS_NUMBER(*vp)) {
|
||||
ok = JS_ValueToNumber(cx, *vp, &num );
|
||||
if (!ok) {
|
||||
break;
|
||||
}
|
||||
pArray->addObject(CCInteger::create((int)num));
|
||||
}
|
||||
else if (JSVAL_IS_STRING(*vp))
|
||||
{
|
||||
JSStringWrapper str(JSVAL_TO_STRING(*vp), cx);
|
||||
pArray->addObject(CCString::create(str));
|
||||
}
|
||||
else
|
||||
{
|
||||
js_proxy_t* p;
|
||||
JSObject* obj = JSVAL_TO_OBJECT(*vp);
|
||||
JS_GET_NATIVE_PROXY(p, obj);
|
||||
if (p) {
|
||||
pArray->addObject((CCObject*)p->ptr);
|
||||
}
|
||||
}
|
||||
// next
|
||||
vp++;
|
||||
}
|
||||
return pArray;
|
||||
}
|
||||
else
|
||||
{
|
||||
js_proxy_t* p;
|
||||
JSObject* obj = JSVAL_TO_OBJECT(*vp);
|
||||
JS_GET_NATIVE_PROXY(p, obj);
|
||||
if (p) {
|
||||
pArray->addObject((CCObject*)p->ptr);
|
||||
}
|
||||
}
|
||||
// next
|
||||
vp++;
|
||||
}
|
||||
return pArray;
|
||||
}
|
||||
|
||||
CCRect jsval_to_ccrect(JSContext *cx, jsval v) {
|
||||
|
@ -1498,26 +1503,26 @@ JSBool jsSocketClose(JSContext* cx, unsigned argc, jsval* vp)
|
|||
return JS_TRUE;
|
||||
}
|
||||
|
||||
JSBool jsb_set_reserved_slot(JSObject *obj, uint32_t idx, jsval value)
|
||||
{
|
||||
JSClass *klass = JS_GetClass(obj);
|
||||
unsigned int slots = JSCLASS_RESERVED_SLOTS(klass);
|
||||
if( idx >= slots )
|
||||
return JS_FALSE;
|
||||
|
||||
JS_SetReservedSlot(obj, idx, value);
|
||||
|
||||
return JS_TRUE;
|
||||
JSBool jsb_set_reserved_slot(JSObject *obj, uint32_t idx, jsval value)
|
||||
{
|
||||
JSClass *klass = JS_GetClass(obj);
|
||||
unsigned int slots = JSCLASS_RESERVED_SLOTS(klass);
|
||||
if( idx >= slots )
|
||||
return JS_FALSE;
|
||||
|
||||
JS_SetReservedSlot(obj, idx, value);
|
||||
|
||||
return JS_TRUE;
|
||||
}
|
||||
|
||||
JSBool jsb_get_reserved_slot(JSObject *obj, uint32_t idx, jsval& ret)
|
||||
{
|
||||
JSClass *klass = JS_GetClass(obj);
|
||||
unsigned int slots = JSCLASS_RESERVED_SLOTS(klass);
|
||||
if( idx >= slots )
|
||||
return JS_FALSE;
|
||||
|
||||
ret = JS_GetReservedSlot(obj, idx);
|
||||
|
||||
return JS_TRUE;
|
||||
JSBool jsb_get_reserved_slot(JSObject *obj, uint32_t idx, jsval& ret)
|
||||
{
|
||||
JSClass *klass = JS_GetClass(obj);
|
||||
unsigned int slots = JSCLASS_RESERVED_SLOTS(klass);
|
||||
if( idx >= slots )
|
||||
return JS_FALSE;
|
||||
|
||||
ret = JS_GetReservedSlot(obj, idx);
|
||||
|
||||
return JS_TRUE;
|
||||
}
|
||||
|
|
|
@ -84,6 +84,7 @@ public:
|
|||
virtual int executeLayerTouchesEvent(CCLayer* pLayer, int eventType, CCSet *pTouches);
|
||||
virtual int executeLayerTouchEvent(CCLayer* pLayer, int eventType, CCTouch *pTouch);
|
||||
virtual int executeAccelerometerEvent(CCLayer* pLayer, CCAcceleration* pAccelerationValue);
|
||||
virtual int executeLayerKeypadEvent(CCLayer* pLayer, int eventType);
|
||||
|
||||
bool executeFunctionWithObjectData(CCNode *self, const char *name, JSObject *obj);
|
||||
int executeFunctionWithOwner(jsval owner, const char *name, jsval data);
|
||||
|
|
|
@ -395,7 +395,7 @@ int CCLuaEngine::executeLayerTouchEvent(CCLayer* pLayer, int eventType, CCTouch
|
|||
int ret = 0;
|
||||
do
|
||||
{
|
||||
CCTouchScriptHandlerEntry* pScriptHandlerEntry = pLayer->getScriptHandlerEntry();
|
||||
CCTouchScriptHandlerEntry* pScriptHandlerEntry = pLayer->getScriptTouchHandlerEntry();
|
||||
CC_BREAK_IF(NULL == pScriptHandlerEntry);
|
||||
int nScriptHandler = pScriptHandlerEntry->getHandler();
|
||||
CC_BREAK_IF(0 == nScriptHandler);
|
||||
|
@ -415,7 +415,7 @@ int CCLuaEngine::executeLayerTouchesEvent(CCLayer* pLayer, int eventType, CCSet
|
|||
int ret = 0;
|
||||
do
|
||||
{
|
||||
CCTouchScriptHandlerEntry* pScriptHandlerEntry = pLayer->getScriptHandlerEntry();
|
||||
CCTouchScriptHandlerEntry* pScriptHandlerEntry = pLayer->getScriptTouchHandlerEntry();
|
||||
CC_BREAK_IF(NULL == pScriptHandlerEntry);
|
||||
int nScriptHandler = pScriptHandlerEntry->getHandler();
|
||||
CC_BREAK_IF(0 == nScriptHandler);
|
||||
|
@ -445,6 +445,45 @@ int CCLuaEngine::executeLayerTouchesEvent(CCLayer* pLayer, int eventType, CCSet
|
|||
return ret;
|
||||
}
|
||||
|
||||
int CCLuaEngine::executeLayerKeypadEvent(CCLayer* pLayer, int eventType)
|
||||
{
|
||||
int ret = 0;
|
||||
do
|
||||
{
|
||||
CCScriptHandlerEntry* pScriptHandlerEntry = pLayer->getScriptKeypadHandlerEntry();
|
||||
CC_BREAK_IF(NULL == pScriptHandlerEntry);
|
||||
int nScriptHandler = pScriptHandlerEntry->getHandler();
|
||||
CC_BREAK_IF(0 == nScriptHandler);
|
||||
|
||||
cleanStack();
|
||||
lua_newtable(m_state);
|
||||
lua_pushinteger(m_state, eventType);
|
||||
ret = executeFunctionByHandler(nScriptHandler, 1);
|
||||
} while (0);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int CCLuaEngine::executeAccelerometerEvent(CCLayer* pLayer, CCAcceleration* pAccelerationValue)
|
||||
{
|
||||
int ret = 0;
|
||||
do
|
||||
{
|
||||
CCScriptHandlerEntry* pScriptHandlerEntry = pLayer->getScriptAccelerateHandlerEntry();
|
||||
CC_BREAK_IF(NULL == pScriptHandlerEntry);
|
||||
int nScriptHandler = pScriptHandlerEntry->getHandler();
|
||||
CC_BREAK_IF(0 == nScriptHandler);
|
||||
|
||||
cleanStack();
|
||||
lua_newtable(m_state);
|
||||
lua_pushnumber(m_state, pAccelerationValue->x);
|
||||
lua_pushnumber(m_state, pAccelerationValue->y);
|
||||
lua_pushnumber(m_state, pAccelerationValue->z);
|
||||
lua_pushnumber(m_state, pAccelerationValue->timestamp);
|
||||
ret = executeFunctionByHandler(nScriptHandler, 4);
|
||||
} while (0);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int CCLuaEngine::executeFunctionByHandler(int nHandler, int numArgs)
|
||||
{
|
||||
if (pushFunction(nHandler)) /* stack: ... arg1 arg2 ... func */
|
||||
|
|
|
@ -195,8 +195,9 @@ public:
|
|||
virtual int executeSchedule(CCTimer* pTimer, float dt, CCNode* pNode = NULL);
|
||||
virtual int executeLayerTouchesEvent(CCLayer* pLayer, int eventType, CCSet *pTouches);
|
||||
virtual int executeLayerTouchEvent(CCLayer* pLayer, int eventType, CCTouch *pTouch);
|
||||
virtual int executeLayerKeypadEvent(CCLayer* pLayer, int eventType);
|
||||
/** execute a accelerometer event */
|
||||
virtual int executeAccelerometerEvent(CCLayer* pLayer, CCAcceleration* pAccelerationValue){ return 0;};
|
||||
virtual int executeAccelerometerEvent(CCLayer* pLayer, CCAcceleration* pAccelerationValue);
|
||||
/**
|
||||
@brief Method used to get a pointer to the lua_State that the script module is attached to.
|
||||
@return A pointer to the lua_State that the script module is attached to.
|
||||
|
|
Loading…
Reference in New Issue