Merge pull request #1554 from dumganhar/gles20

issue #1549: Fix some issues.
[1]Added "executeAccelerometerEvent" for CCScriptEngineProtocol class. Exported Accelerometer event to JS.
[2]Updated cocos2dx.ini. Renamed some functions.
[3]Added "ccdictionary_to_jsval" function, now TMXOrthoObjectsTest and TMXIsoObjectsTest don't crash.
This commit is contained in:
James Chen 2012-11-06 01:00:31 -08:00
commit 2193940b27
7 changed files with 106 additions and 17 deletions

View File

@ -249,14 +249,13 @@ void CCLayer::setAccelerometerInterval(double interval) {
}
void CCLayer::didAccelerate(CCAcceleration* pAccelerationValue) {
CC_UNUSED_PARAM(pAccelerationValue);
//
// if ( m_eScriptType != kScriptTypeNone)
// {
// CCScriptEngineManager::sharedManager()->getScriptEngine()->executeAccelerometerEvent(this, pAccelerationValue);
// }
void CCLayer::didAccelerate(CCAcceleration* pAccelerationValue)
{
CC_UNUSED_PARAM(pAccelerationValue);
if ( m_eScriptType != kScriptTypeNone)
{
CCScriptEngineManager::sharedManager()->getScriptEngine()->executeAccelerometerEvent(this, pAccelerationValue);
}
}

View File

@ -31,14 +31,15 @@ NS_CC_BEGIN
/**
@brief The device accelerometer reports values for each axis in units of g-force
*/
typedef struct
class CCAcceleration
{
public:
double x;
double y;
double z;
double timestamp;
} CCAcceleration;
};
/**
@brief

View File

@ -41,6 +41,7 @@ class CCLayer;
class CCMenuItem;
class CCNotificationCenter;
class CCCallFunc;
class CCAcceleration;
enum ccScriptType {
kScriptTypeNone = 0,
@ -220,9 +221,12 @@ public:
/** execute a schedule function */
virtual int executeSchedule(CCTimer* pTimer, float dt, CCNode* pNode = NULL) = 0;
/** functions for execute touch event */
/** functions for executing touch event */
virtual int executeLayerTouchesEvent(CCLayer* pLayer, int eventType, CCSet *pTouches) = 0;
virtual int executeLayerTouchEvent(CCLayer* pLayer, int eventType, CCTouch *pTouch) = 0;
/** execute a accelerometer event */
virtual int executeAccelerometerEvent(CCLayer* pLayer, CCAcceleration* pAccelerationValue) = 0;
};
/**

View File

@ -754,6 +754,44 @@ int ScriptingCore::executeLayerTouchEvent(CCLayer* pLayer, int eventType, CCTouc
return 1;
}
int ScriptingCore::executeAccelerometerEvent(CCLayer* pLayer, CCAcceleration* pAccelerationValue)
{
js_proxy_t * p;
JS_GET_PROXY(p, pLayer);
if (!p) return 0;
jsval retval;
JSBool found;
JS_HasProperty(this->cx_, p->obj, "onAccelerometer", &found);
if (found == JS_TRUE) {
jsval rval, fval;
double time = pAccelerationValue->timestamp;
double x = pAccelerationValue->x;
double y = pAccelerationValue->y;
double z = pAccelerationValue->z;
// Create an JS object with x,y,z,timestamp as properties
JSObject *object = JS_NewObject(this->cx_, NULL, NULL, NULL );
if( !object)
return 0;
if (!JS_DefineProperty(this->cx_, object, "x", DOUBLE_TO_JSVAL(x), NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT) ||
!JS_DefineProperty(this->cx_, object, "y", DOUBLE_TO_JSVAL(y), NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT) ||
!JS_DefineProperty(this->cx_, object, "z", DOUBLE_TO_JSVAL(z), NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT) ||
!JS_DefineProperty(this->cx_, object, "timestamp", DOUBLE_TO_JSVAL(time), NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT) )
return 0;
jsval argv = OBJECT_TO_JSVAL(object);
JS_GetProperty(this->cx_, p->obj, "onAccelerometer", &fval);
JS_CallFunctionValue(this->cx_, p->obj, fval, 1, &argv, &rval);
}
return 1;
}
int ScriptingCore::executeFunctionWithObjectData(CCNode *self, const char *name, JSObject *obj) {
js_proxy_t * p;
@ -1024,8 +1062,16 @@ jsval ccarray_to_jsval(JSContext* cx, CCArray *arr) {
CCObject *obj = arr->objectAtIndex(i);
CCString *testString = dynamic_cast<cocos2d::CCString *>(obj);
CCDictionary* testDict = NULL;
CCArray* testArray = NULL;
// XXX: Only supports string, since all data read from plist files will be stored as string in cocos2d-x
// Do we need to convert string to js base type ?
if(testString) {
arrElement = c_string_to_jsval(cx, testString->getCString());
} else if (testDict = dynamic_cast<cocos2d::CCDictionary*>(obj)) {
arrElement = ccdictionary_to_jsval(cx, testDict);
} else if (testArray = dynamic_cast<cocos2d::CCArray*>(obj)) {
arrElement = ccarray_to_jsval(cx, testArray);
} else {
js_proxy_t *proxy = js_get_or_create_proxy<cocos2d::CCObject>(cx, obj);
arrElement = OBJECT_TO_JSVAL(proxy->obj);
@ -1038,6 +1084,40 @@ jsval ccarray_to_jsval(JSContext* cx, CCArray *arr) {
return OBJECT_TO_JSVAL(jsretArr);
}
jsval ccdictionary_to_jsval(JSContext* cx, CCDictionary* dict)
{
JSObject* jsRet = JS_NewObject(cx, NULL, NULL, NULL);
CCDictElement* pElement = NULL;
CCDICT_FOREACH(dict, pElement)
{
jsval dictElement;
CCString* obj = dynamic_cast<CCString*>(pElement->getObject());
CCString *testString = dynamic_cast<cocos2d::CCString *>(obj);
CCDictionary* testDict = NULL;
CCArray* testArray = NULL;
// XXX: Only supports string, since all data read from plist files will be stored as string in cocos2d-x
// Do we need to convert string to js base type ?
if(testString) {
dictElement = c_string_to_jsval(cx, testString->getCString());
} else if (testDict = dynamic_cast<cocos2d::CCDictionary*>(obj)) {
dictElement = ccdictionary_to_jsval(cx, testDict);
} else if (testArray = dynamic_cast<cocos2d::CCArray*>(obj)) {
dictElement = ccarray_to_jsval(cx, testArray);
} else {
js_proxy_t *proxy = js_get_or_create_proxy<cocos2d::CCObject>(cx, obj);
dictElement = OBJECT_TO_JSVAL(proxy->obj);
}
const char* key = pElement->getStrKey();
if (key && strlen(key) > 0)
{
JS_SetProperty(cx, jsRet, key, &dictElement);
}
}
return OBJECT_TO_JSVAL(jsRet);
}
jsval long_long_to_jsval(JSContext* cx, long long v) {
JSObject *tmp = JS_NewUint32Array(cx, 2);
uint32_t *data = (uint32_t *)JS_GetArrayBufferViewData(tmp, cx);

View File

@ -82,6 +82,7 @@ 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 executeAccelerometerEvent(CCLayer* pLayer, CCAcceleration* pAccelerationValue);
int executeFunctionWithObjectData(CCNode *self, const char *name, JSObject *obj);
int executeFunctionWithOwner(jsval owner, const char *name, jsval data);
@ -188,6 +189,7 @@ ccColor3B jsval_to_cccolor3b(JSContext *cx, jsval v);
JSBool jsval_to_ccarray_of_CCPoint(JSContext* cx, jsval v, CCPoint **points, int *numPoints);
CCArray* jsval_to_ccarray(JSContext* cx, jsval v);
jsval ccarray_to_jsval(JSContext* cx, CCArray *arr);
jsval ccdictionary_to_jsval(JSContext* cx, CCDictionary* dict);
// from native
jsval long_long_to_jsval(JSContext* cx, long long v);
jsval std_string_to_jsval(JSContext* cx, std::string& v);

View File

@ -195,7 +195,8 @@ 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);
/** execute a accelerometer event */
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.

View File

@ -36,14 +36,14 @@ classes = CCSprite.* CCScene CCNode CCDirector CCLayer.* CCMenu.* CCTouch CC.*Ac
# functions from all classes.
skip = CCNode::[.*Transform convertToWindowSpace getChildren setPosition getGrid setGLServerState description getShaderProgram getUserObject .*UserData getGLServerState],
CCSprite::[getQuad displayFrame getTexture getBlendFunc setPosition setBlendFunc getTextureAtlas setSpriteBatchNode getSpriteBatchNode],
CCSprite::[getQuad displayFrame getBlendFunc setPosition setBlendFunc setSpriteBatchNode getSpriteBatchNode],
CCSpriteBatchNode::[getBlendFunc setBlendFunc],
CCMotionStreak::[getBlendFunc setBlendFunc],
CCAtlasNode::[getBlendFunc setBlendFunc],
CCParticleBatchNode::[getBlendFunc setBlendFunc],
CCLayerColor::[getBlendFunc setBlendFunc],
CCParticleSystem::[getBlendFunc setBlendFunc],
CCDirector::[getAccelerometer getKeypadDispatcher getTouchDispatcher setWatcherCallbackFun getOpenGLView getScheduler getProjection],
CCDirector::[getAccelerometer getKeypadDispatcher getTouchDispatcher setWatcherCallbackFun getOpenGLView getProjection],
CCLayer.*::[didAccelerate (g|s)etBlendFunc],
CCMenu.*::[.*Target getSubItems create initWithItems alignItemsInRows alignItemsInColumns],
CCMenuItem.*::[create],
@ -52,7 +52,7 @@ skip = CCNode::[.*Transform convertToWindowSpace getChildren setPosition getGrid
CC.*Protocol::[*],
CC.*Delegate::[*],
CCPoolManager::[*],
CCTexture2D::[initWithPVRTCData releaseData setTexParameters initWithData keepData],
CCTexture2D::[initWithPVRTCData addPVRTCImage releaseData setTexParameters initWithData keepData],
CCSet::[begin end],
CCIMEDispatcher::[*],
CCSAXParser::[*],
@ -87,7 +87,6 @@ skip = CCNode::[.*Transform convertToWindowSpace getChildren setPosition getGrid
CCTiledGrid3DAction::[create actionWith.*],
CCTMXMapInfo::[startElement endElement textHandler],
CCParticleSystemQuad::[postStep setBatchNode draw setTexture$ setTotalParticles updateQuadWithParticle setupIndices listenBackToForeground initWithTotalParticles particleWithFile node],
CCTexture2D::[initWithPVRTCData addPVRTCImage],
CCLayerMultiplex::[*],
CCCatmullRom.*::[create actionWithDuration],
CCBezier.*::[create actionWithDuration],
@ -108,11 +107,14 @@ rename_functions = CCDirector::[sharedDirector=getInstance],
CCAnimationCache::[sharedAnimationCache=getInstance addAnimationsWithFile=addAnimations animationByName=getAnimation removeAnimationByName=removeAnimation],
CCLayerGradient::[initWithColor=init],
CCLayerColor::[initWithColor=init],
CCNode::[boundingBox=getBoundingBox removeFromParentAndCleanup=removeFromParent],
CCNode::[boundingBox=getBoundingBox removeFromParentAndCleanup=removeFromParent removeAllChildrenWithCleanup=removeAllChildren unscheduleAllSelectors=unscheduleAllCallbacks],
CCLabelAtlas::[create=_create],
CCTMXLayer::[tileAt=getTileAt tileGIDAt=getTileGIDAt propertyNamed=getProperty],
CCTileMapAtlas::[tileAt=getTileAt],
CCTMXTiledMap::[layerNamed=getLayer objectGroupNamed=getObjectGroup],
CCSprite::[isFlipX=isFlippedX isFlipY=isFlippedY],
CCTouch::[getID=getId],
CCScheduler::[unscheduleAllSelectorsForTarget=unscheduleAllCallbacksForTarget unscheduleAllSelectors=unscheduleAllCallbacks],
SimpleAudioEngine::[sharedEngine=getInstance preloadBackgroundMusic=preloadMusic setBackgroundMusicVolume=setMusicVolume getBackgroundMusicVolume=getMusicVolume playBackgroundMusic=playMusic stopBackgroundMusic=stopMusic pauseBackgroundMusic=pauseMusic resumeBackgroundMusic=resumeMusic rewindBackgroundMusic=rewindMusic isBackgroundMusicPlaying=isMusicPlaying willPlayBackgroundMusic=willPlayMusic],
CCCamera.*::[setUpXYZ=setUp getUpXYZ=getUp setEyeXYZ=setEye getEyeXYZ=getEye setCenterXYZ=setCenter getCenterXYZ=getCenter],
CCBReader::[getAnimationManager=getActionManager setAnimationManager=setActionManager]