[Dispatcher] Fixing touch bindings for JSB.

This commit is contained in:
James Chen 2013-09-12 16:02:16 +08:00
parent 891bcba459
commit b77cac8731
4 changed files with 77 additions and 60 deletions

View File

@ -106,40 +106,41 @@ static void executeJSFunctionFromReservedSpot(JSContext *cx, JSObject *obj,
}
}
static void getTouchesFuncName(int eventType, std::string &funcName) {
switch(eventType) {
case CCTOUCHBEGAN:
static void getTouchesFuncName(TouchEvent::EventCode eventCode, std::string &funcName)
{
switch(eventCode)
{
case TouchEvent::EventCode::BEGAN:
funcName = "onTouchesBegan";
break;
case CCTOUCHENDED:
case TouchEvent::EventCode::ENDED:
funcName = "onTouchesEnded";
break;
case CCTOUCHMOVED:
case TouchEvent::EventCode::MOVED:
funcName = "onTouchesMoved";
break;
case CCTOUCHCANCELLED:
case TouchEvent::EventCode::CANCELLED:
funcName = "onTouchesCancelled";
break;
}
}
static void getTouchFuncName(int eventType, std::string &funcName) {
switch(eventType) {
case CCTOUCHBEGAN:
static void getTouchFuncName(TouchEvent::EventCode eventCode, std::string &funcName)
{
switch(eventCode) {
case TouchEvent::EventCode::BEGAN:
funcName = "onTouchBegan";
break;
case CCTOUCHENDED:
case TouchEvent::EventCode::ENDED:
funcName = "onTouchEnded";
break;
case CCTOUCHMOVED:
case TouchEvent::EventCode::MOVED:
funcName = "onTouchMoved";
break;
case CCTOUCHCANCELLED:
case TouchEvent::EventCode::CANCELLED:
funcName = "onTouchCancelled";
break;
}
}
static void rootObject(JSContext *cx, JSObject *obj) {
@ -834,12 +835,12 @@ int ScriptingCore::handleTouchesEvent(void* data)
return 0;
TouchesScriptData* touchesScriptData = static_cast<TouchesScriptData*>(data);
if (NULL == touchesScriptData->nativeObject || NULL == touchesScriptData->touches)
if (NULL == touchesScriptData->nativeObject || touchesScriptData->touches.empty())
return 0;
Layer* pLayer = static_cast<Layer*>(touchesScriptData->nativeObject);
int eventType = touchesScriptData->actionType;
Set *pTouches = touchesScriptData->touches;
TouchEvent::EventCode eventType = touchesScriptData->actionType;
const std::vector<Touch*>& touches = touchesScriptData->touches;
std::string funcName = "";
getTouchesFuncName(eventType, funcName);
@ -848,21 +849,26 @@ int ScriptingCore::handleTouchesEvent(void* data)
JS_AddNamedObjectRoot(this->cx_, &jsretArr, "touchArray");
int count = 0;
for(SetIterator it = pTouches->begin(); it != pTouches->end(); ++it, ++count) {
for (auto& touch : touches)
{
jsval jsret;
getJSTouchObject(this->cx_, (Touch *) *it, jsret);
if (!JS_SetElement(this->cx_, jsretArr, count, &jsret)) {
getJSTouchObject(this->cx_, touch, jsret);
if (!JS_SetElement(this->cx_, jsretArr, count, &jsret))
{
break;
}
++count;
}
executeFunctionWithObjectData(pLayer, funcName.c_str(), jsretArr);
JS_RemoveObjectRoot(this->cx_, &jsretArr);
for(SetIterator it = pTouches->begin(); it != pTouches->end(); ++it, ++count) {
for (auto& touch : touches)
{
jsval jsret;
removeJSTouchObject(this->cx_, (Touch *) *it, jsret);
removeJSTouchObject(this->cx_, touch, jsret);
}
return 1;
@ -878,7 +884,7 @@ int ScriptingCore::handleTouchEvent(void* data)
return 0;
Layer* pLayer = static_cast<Layer*>(touchScriptData->nativeObject);
int eventType = touchScriptData->actionType;
TouchEvent::EventCode eventType = touchScriptData->actionType;
Touch *pTouch = touchScriptData->touch;
@ -978,17 +984,19 @@ int ScriptingCore::handleKeypadEvent(void* data)
js_proxy_t * p = jsb_get_native_proxy(keypadScriptData->nativeObject);
if (p){
switch(action){
case kTypeBackClicked:
executeFunctionWithOwner(OBJECT_TO_JSVAL(p->obj), "backClicked");
break;
case kTypeMenuClicked:
executeFunctionWithOwner(OBJECT_TO_JSVAL(p->obj), "menuClicked");
break;
default:
break;
}
if (p)
{
// FIXME: switch(action)
// {
// case kTypeBackClicked:
// executeFunctionWithOwner(OBJECT_TO_JSVAL(p->obj), "backClicked");
// break;
// case kTypeMenuClicked:
// executeFunctionWithOwner(OBJECT_TO_JSVAL(p->obj), "menuClicked");
// break;
// default:
// break;
// }
return 1;
}
@ -996,8 +1004,8 @@ int ScriptingCore::handleKeypadEvent(void* data)
}
int ScriptingCore::executeCustomTouchesEvent(int eventType,
Set *pTouches, JSObject *obj)
int ScriptingCore::executeCustomTouchesEvent(TouchEvent::EventCode eventType,
const std::vector<Touch*>& touches, JSObject *obj)
{
jsval retval;
std::string funcName;
@ -1006,29 +1014,33 @@ int ScriptingCore::executeCustomTouchesEvent(int eventType,
JSObject *jsretArr = JS_NewArrayObject(this->cx_, 0, NULL);
JS_AddNamedObjectRoot(this->cx_, &jsretArr, "touchArray");
int count = 0;
for(SetIterator it = pTouches->begin(); it != pTouches->end(); ++it, ++count) {
for (auto& touch : touches)
{
jsval jsret;
getJSTouchObject(this->cx_, (Touch *) *it, jsret);
getJSTouchObject(this->cx_, touch, jsret);
if (!JS_SetElement(this->cx_, jsretArr, count, &jsret)) {
break;
}
++count;
}
jsval jsretArrVal = OBJECT_TO_JSVAL(jsretArr);
executeFunctionWithOwner(OBJECT_TO_JSVAL(obj), funcName.c_str(), 1, &jsretArrVal, &retval);
JS_RemoveObjectRoot(this->cx_, &jsretArr);
for(SetIterator it = pTouches->begin(); it != pTouches->end(); ++it, ++count) {
for (auto& touch : touches)
{
jsval jsret;
removeJSTouchObject(this->cx_, (Touch *) *it, jsret);
removeJSTouchObject(this->cx_, touch, jsret);
}
return 1;
}
int ScriptingCore::executeCustomTouchEvent(int eventType,
Touch *pTouch, JSObject *obj) {
int ScriptingCore::executeCustomTouchEvent(TouchEvent::EventCode eventType,
Touch *pTouch, JSObject *obj)
{
jsval retval;
std::string funcName;
getTouchFuncName(eventType, funcName);
@ -1045,9 +1057,10 @@ int ScriptingCore::executeCustomTouchEvent(int eventType,
}
int ScriptingCore::executeCustomTouchEvent(int eventType,
int ScriptingCore::executeCustomTouchEvent(TouchEvent::EventCode eventType,
Touch *pTouch, JSObject *obj,
jsval &retval) {
jsval &retval)
{
std::string funcName;
getTouchFuncName(eventType, funcName);

View File

@ -140,12 +140,12 @@ public:
static void removeAllRoots(JSContext *cx);
int executeCustomTouchEvent(int eventType,
int executeCustomTouchEvent(TouchEvent::EventCode eventType,
Touch *pTouch, JSObject *obj, jsval &retval);
int executeCustomTouchEvent(int eventType,
int executeCustomTouchEvent(TouchEvent::EventCode eventType,
Touch *pTouch, JSObject *obj);
int executeCustomTouchesEvent(int eventType,
Set *pTouches, JSObject *obj);
int executeCustomTouchesEvent(TouchEvent::EventCode eventType,
const std::vector<Touch*>& touches, JSObject *obj);
/**
* @return the global context
*/

View File

@ -1 +1 @@
cbe012e3979f31ec4001d6a0b65ba46823b497d7
39c26e0e9bf1e4245143b34732b05dddb43aa4f4

View File

@ -180,9 +180,12 @@ protected:
};
class JSTouchDelegate: public Object, public TouchDelegate
class JSTouchDelegate: public Object
{
public:
JSTouchDelegate();
~JSTouchDelegate();
// Set the touch delegate to map by using the key (pJSObj).
static void setDelegateForJSObject(JSObject* pJSObj, JSTouchDelegate* pDelegate);
// Get the touch delegate by the key (pJSObj).
@ -191,30 +194,31 @@ public:
static void removeDelegateForJSObject(JSObject* pJSObj);
void setJSObject(JSObject *obj);
void registerStandardDelegate();
void registerStandardDelegate(int priority);
void registerTargettedDelegate(int priority, bool swallowsTouches);
// unregister touch delegate.
// Normally, developer should invoke cc.unregisterTouchDelegate() in when the scene exits.
// So this function need to be binded.
void unregisterTouchDelegate();
bool ccTouchBegan(Touch *pTouch, Event *pEvent);
void ccTouchMoved(Touch *pTouch, Event *pEvent);
void ccTouchEnded(Touch *pTouch, Event *pEvent);
void ccTouchCancelled(Touch *pTouch, Event *pEvent);
bool onTouchBegan(Touch *touch, Event *event);
void onTouchMoved(Touch *touch, Event *event);
void onTouchEnded(Touch *touch, Event *event);
void onTouchCancelled(Touch *touch, Event *event);
// optional
void ccTouchesBegan(Set *pTouches, Event *pEvent);
void ccTouchesMoved(Set *pTouches, Event *pEvent);
void ccTouchesEnded(Set *pTouches, Event *pEvent);
void ccTouchesCancelled(Set *pTouches, Event *pEvent);
void onTouchesBegan(const std::vector<Touch*>& touches, Event *event);
void onTouchesMoved(const std::vector<Touch*>& touches, Event *event);
void onTouchesEnded(const std::vector<Touch*>& touches, Event *event);
void onTouchesCancelled(const std::vector<Touch*>& touches, Event *event);
private:
JSObject *_mObj;
JSObject* _obj;
typedef std::map<JSObject*, JSTouchDelegate*> TouchDelegateMap;
typedef std::pair<JSObject*, JSTouchDelegate*> TouchDelegatePair;
static TouchDelegateMap sTouchDelegateMap;
bool _needUnroot;
int _listenerId;
};