1
2
3
4
5
6
7
8
9
10
11
12
|
// add a "close" icon to exit the progress. it's an autorelease object
var
closeItem = cc.MenuItemImage.create(
"res/CloseNormal.png"
,
"res/CloseSelected.png"
cc.log(
"close button was clicked."
);
},
this
);
closeItem.setAnchorPoint(cc.p(0.5,0.5));
menu = cc.Menu.create(closeItem);
menu.setPosition(cc.p(0,0));
.addChild(menu,1);
closeItem.setPosition(cc.p(size.width - 20,20));
|
// "create" in JS
// cc.MenuItemImage.create( normalImage,selectedImage,[disabledImage],callback_fn,[this]
JSBool js_cocos2dx_CCMenuItemImage_create(JSContext *cx,uint32_t argc,jsval *vp)
{
if
(argc >= 2 && argc <= 5) {
jsval *argv = JS_ARGV(cx,vp);
JSStringWrapper arg0(argv[0]);
JSStringWrapper arg1(argv[1]);
JSStringWrapper arg2;
bool
thirdArgIsString =
true
;
jsval jsCallback = JSVAL_VOID;
jsval jsThis = JSVAL_VOID;
int
last = 2;
(argc >= 3) {
thirdArgIsString = argv[2].isString();
(thirdArgIsString) {
arg2.set(argv[2],cx);
last = 3;
}
}
cocos2d::MenuItemImage* ret = cocos2d::MenuItemImage::create(arg0.get(),arg1.get(),std::string(arg2.get()));
(argc >= 3) {
(!thirdArgIsString) {
//cc.MenuItemImage.create( normalImage,[this] )
jsCallback = argv[last++];
(argc == 4) {
jsThis = argv[last];
}
}
else
{
(argc >= 4) {
jsCallback = argv[last++];
(argc == 5) {
jsThis = argv[last];
}
}
}
}
JSObject *obj = bind_menu_item<cocos2d::MenuItemImage>(cx,ret,jsCallback,jsThis);
JS_SET_RVAL(cx,vp,OBJECT_TO_JSVAL(obj));
return
JS_TRUE;
}
JS_ReportError(cx,
"Invalid number of arguments. Expecting: 2 <= args <= 5"
);
JS_FALSE;
}
(argc >= 3) {
(!thirdArgIsString) {
jsCallback = argv[last++];
(argc == 4) {
jsThis = argv[last];
}
}
{
(argc >= 4) {
jsCallback = argv[last++];
(argc == 5) {
jsThis = argv[last];
}
}
}
}
<
class
T>
JSObject* bind_menu_item(JSContext *cx,T* nativeObj,jsval callback,jsval thisObj) {
js_proxy_t *p = jsb_get_native_proxy(nativeObj);
(p) {
addCallBackAndThis(p->obj,callback,thisObj);
p->obj;
}
{
js_type_class_t *classType = js_get_type_from_native<T>(nativeObj);
assert
(classType);
JSObject *tmp = JS_NewObject(cx,classType->jsclass,classType->proto,classType->parentProto);
// bind nativeObj <-> JSObject
js_proxy_t *proxy = jsb_new_proxy(nativeObj,tmp);
JS_AddNamedObjectRoot(cx,&proxy->obj,
typeid
(*nativeObj).name());
addCallBackAndThis(tmp,thisObj);
tmp;
}
}
static void addCallBackAndThis(JSObject *obj,jsval &thisObj)
{
if
(callback != JSVAL_VOID) {
ScriptingCore::getInstance()->setReservedSpot(0,obj,callback);
}
(thisObj != JSVAL_VOID) {
ScriptingCore::getInstance()->setReservedSpot(1,thisObj);
}
}
JS_SetReservedSlot(obj,i,value);
JS_TRUE;
}
MenuItem::activate()
{
(_enabled)
( _callback )
{
_callback(
this
);
(kScriptTypeNone != _scriptType)
{
BasicScriptData data(
);
ScriptEvent scriptEvent(kMenuClickedEvent,&data);
ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&scriptEvent);
}
(NULL == evt)
return
0;
JSAutoCompartment ac(_cx,_global);
switch
(evt->type)
{
case
kNodeEvent:
{
handleNodeEvent(evt->data);
}
break
;
kMenuClickedEvent:
{
handleMenuClickedEvent(evt->data);
}
;
kTouchEvent:
{
handleTouchEvent(evt->data);
}
;
kTouchesEvent:
{
handleTouchesEvent(evt->data);
}
;
kKeypadEvent:
{
handleKeypadEvent(evt->data);
}
;
kAccelerometerEvent:
{
handleAccelerometerEvent(evt->data);
}
;
default
:
;
}
0;
}
(NULL == data)
BasicScriptData* basicScriptData = static_cast<BasicScriptData*>(data);
(NULL == basicScriptData->nativeObject)
0;
MenuItem* menuItem = static_cast<MenuItem*>(basicScriptData->nativeObject);
js_proxy_t * p = jsb_get_native_proxy(menuItem);
(!p)
0;
jsval retval;
jsval dataVal;
js_proxy_t *proxy = jsb_get_native_proxy(menuItem);
dataVal = (proxy ? OBJECT_TO_JSVAL(proxy->obj) : JSVAL_NULL);
executeJSFunctionFromReservedSpot(
->_cx,p->obj,dataVal,retval);
1;
}
executeJSFunctionFromReservedSpot(JSContext *cx,
jsval &dataVal,jsval &retval) {
jsval func = JS_GetReservedSlot(obj,0);
(func == JSVAL_VOID) {
; }
jsval thisObj = JS_GetReservedSlot(obj,1);
JSAutoCompartment ac(cx,obj);
(thisObj == JSVAL_VOID) {
JS_CallFunctionValue(cx,func,1,&dataVal,&retval);
{
(!JSVAL_IS_PRIMITIVE(thisObj));
}
}