From 21e4f80fbdc612e0334b415bfce7b725d2a83d91 Mon Sep 17 00:00:00 2001 From: James Chen Date: Tue, 6 Nov 2012 14:18:13 +0800 Subject: [PATCH 1/3] Added "executeAccelerometerEvent" for CCScriptEngineProtocol class. Exported Accelerometer event to JS. --- .../CCLayer.cpp | 15 ++++---- cocos2dx/platform/CCAccelerometerDelegate.h | 5 ++- cocos2dx/script_support/CCScriptSupport.h | 6 ++- .../javascript/bindings/ScriptingCore.cpp | 38 +++++++++++++++++++ scripting/javascript/bindings/ScriptingCore.h | 1 + scripting/lua/cocos2dx_support/CCLuaEngine.h | 3 +- 6 files changed, 56 insertions(+), 12 deletions(-) diff --git a/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp b/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp index ffcfc94d84..2fdb2575b1 100644 --- a/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp +++ b/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp @@ -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); + } } diff --git a/cocos2dx/platform/CCAccelerometerDelegate.h b/cocos2dx/platform/CCAccelerometerDelegate.h index 37524cfa15..6890bb2e6c 100644 --- a/cocos2dx/platform/CCAccelerometerDelegate.h +++ b/cocos2dx/platform/CCAccelerometerDelegate.h @@ -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 diff --git a/cocos2dx/script_support/CCScriptSupport.h b/cocos2dx/script_support/CCScriptSupport.h index 822c09f6cb..ce2c11932c 100644 --- a/cocos2dx/script_support/CCScriptSupport.h +++ b/cocos2dx/script_support/CCScriptSupport.h @@ -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; }; /** diff --git a/scripting/javascript/bindings/ScriptingCore.cpp b/scripting/javascript/bindings/ScriptingCore.cpp index 5a294deefe..5c79b232ca 100644 --- a/scripting/javascript/bindings/ScriptingCore.cpp +++ b/scripting/javascript/bindings/ScriptingCore.cpp @@ -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; diff --git a/scripting/javascript/bindings/ScriptingCore.h b/scripting/javascript/bindings/ScriptingCore.h index 9f8b4068de..326338e3e1 100644 --- a/scripting/javascript/bindings/ScriptingCore.h +++ b/scripting/javascript/bindings/ScriptingCore.h @@ -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); diff --git a/scripting/lua/cocos2dx_support/CCLuaEngine.h b/scripting/lua/cocos2dx_support/CCLuaEngine.h index fad8936c38..e557e7db89 100644 --- a/scripting/lua/cocos2dx_support/CCLuaEngine.h +++ b/scripting/lua/cocos2dx_support/CCLuaEngine.h @@ -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. From 26af5355213810b6e92addd77f7934a1b09079e7 Mon Sep 17 00:00:00 2001 From: James Chen Date: Tue, 6 Nov 2012 14:19:27 +0800 Subject: [PATCH 2/3] issue #1549: Updated cocos2dx.ini. Renamed some functions. --- tools/tojs/cocos2dx.ini | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/tools/tojs/cocos2dx.ini b/tools/tojs/cocos2dx.ini index af68df68f1..2f522ce1ae 100644 --- a/tools/tojs/cocos2dx.ini +++ b/tools/tojs/cocos2dx.ini @@ -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] From 449c719d96abaa9ea97846a56f7c075f0b364510 Mon Sep 17 00:00:00 2001 From: James Chen Date: Tue, 6 Nov 2012 16:11:45 +0800 Subject: [PATCH 3/3] issue #1549: Added "ccdictionary_to_jsval" function, now TMXOrthoObjectsTest and TMXIsoObjectsTest don't crash. --- .../javascript/bindings/ScriptingCore.cpp | 42 +++++++++++++++++++ scripting/javascript/bindings/ScriptingCore.h | 1 + 2 files changed, 43 insertions(+) diff --git a/scripting/javascript/bindings/ScriptingCore.cpp b/scripting/javascript/bindings/ScriptingCore.cpp index 5c79b232ca..a0e746dde3 100644 --- a/scripting/javascript/bindings/ScriptingCore.cpp +++ b/scripting/javascript/bindings/ScriptingCore.cpp @@ -1062,8 +1062,16 @@ jsval ccarray_to_jsval(JSContext* cx, CCArray *arr) { CCObject *obj = arr->objectAtIndex(i); CCString *testString = dynamic_cast(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(obj)) { + arrElement = ccdictionary_to_jsval(cx, testDict); + } else if (testArray = dynamic_cast(obj)) { + arrElement = ccarray_to_jsval(cx, testArray); } else { js_proxy_t *proxy = js_get_or_create_proxy(cx, obj); arrElement = OBJECT_TO_JSVAL(proxy->obj); @@ -1076,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(pElement->getObject()); + + CCString *testString = dynamic_cast(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(obj)) { + dictElement = ccdictionary_to_jsval(cx, testDict); + } else if (testArray = dynamic_cast(obj)) { + dictElement = ccarray_to_jsval(cx, testArray); + } else { + js_proxy_t *proxy = js_get_or_create_proxy(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); diff --git a/scripting/javascript/bindings/ScriptingCore.h b/scripting/javascript/bindings/ScriptingCore.h index 326338e3e1..9e064464b5 100644 --- a/scripting/javascript/bindings/ScriptingCore.h +++ b/scripting/javascript/bindings/ScriptingCore.h @@ -189,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);