Merge pull request #2714 from samuele3hu/bind_cocosbuilder

add cocosbuilder lua_binding
This commit is contained in:
minggo 2013-06-03 19:33:19 -07:00
commit f9de0d2fbe
18 changed files with 2487 additions and 7 deletions

View File

@ -443,6 +443,7 @@ CCObject * CCCallFunc::copyWithZone(CCZone *pZone) {
CCActionInstant::copyWithZone(pZone);
pRet->initWithTarget(m_pSelectorTarget);
pRet->m_pCallFunc = m_pCallFunc;
pRet->m_nScriptHandler = m_nScriptHandler;
CC_SAFE_DELETE(pNewZone);
return pRet;
}

View File

@ -3,6 +3,7 @@
#include "AppDelegate.h"
#include "CCLuaEngine.h"
#include "SimpleAudioEngine.h"
#include "Lua_extensions_CCB.h"
using namespace CocosDenshion;
@ -28,10 +29,14 @@ bool AppDelegate::applicationDidFinishLaunching()
// set FPS. the default value is 1.0/60 if you don't call this
pDirector->setAnimationInterval(1.0 / 60);
// register lua engine
CCLuaEngine* pEngine = CCLuaEngine::defaultEngine();
CCScriptEngineManager::sharedManager()->setScriptEngine(pEngine);
CCLuaStack *pStack = pEngine->getLuaStack();
lua_State *tolua_s = pStack->getLuaState();
tolua_extensions_ccb_open(tolua_s);
#if CC_TARGET_PLATFORM == CC_PLATFORM_BLACKBERRY
std::vector<std::string> searchPaths;

View File

@ -87,7 +87,7 @@ local _allTests = {
{ isSupported = false, name = "TextureCacheTest" , create_func= TextureCacheTestMain },
{ isSupported = true, name = "ExtensionsTest" , create_func= ExtensionsTestMain },
{ isSupported = false, name = "ShaderTest" , create_func= ShaderTestMain },
{ isSupported = false, name = "MutiTouchTest" , create_func= MutiTouchTestMain }
{ isSupported = false, name = "MutiTouchTest" , create_func= MutiTouchTestMain }
}
local TESTS_COUNT = table.getn(_allTests)

View File

@ -98,6 +98,18 @@ if [ -f "$file" ]; then
fi
done
# copy common luaScript
for file in "$APP_ROOT"/../../../scripting/lua/script/*
do
if [ -d "$file" ]; then
cp -rf "$file" "$APP_ANDROID_ROOT"/assets
fi
if [ -f "$file" ]; then
cp "$file" "$APP_ANDROID_ROOT"/assets
fi
done
# remove test_image_rgba4444.pvr.gz
rm -f "$APP_ANDROID_ROOT"/assets/Images/test_image_rgba4444.pvr.gz
rm -f "$APP_ANDROID_ROOT"/assets/Images/test_1021x1024_rgba8888.pvr.gz

View File

@ -16,6 +16,7 @@ include $(COCOS_ROOT)/cocos2dx/proj.emscripten/cocos2dx.mk
$(TARGET): $(OBJECTS) $(STATICLIBS) $(COCOS_LIBS) $(CORE_MAKEFILE_LIST)
@mkdir -p $(@D)
cp -a ../../../../samples/Cpp/TestCpp/Resources ../../../../samples/Lua/TestLua
cp -r ../../../../scripting/lua/script/ ../../../../samples/Lua/TestLua/Resources
$(CXX) $(CXXFLAGS) $(OBJECTS) -o $@ $(SHAREDLIBS) $(STATICLIBS) $(LIBS)
$(OBJ_DIR)/%.o: ../%.cpp $(CORE_MAKEFILE_LIST)

View File

@ -1 +1 @@
787967eac1074652820df1aff330aea90a904788
925905c236dd9e939cbc4fea65f7177db300d593

View File

@ -16,6 +16,7 @@ include $(COCOS_ROOT)/cocos2dx/proj.linux/cocos2dx.mk
$(TARGET): $(OBJECTS) $(STATICLIBS) $(COCOS_LIBS) $(CORE_MAKEFILE_LIST)
@mkdir -p $(@D)
cp -R -n ../../../../samples/Cpp/TestCpp/Resources ../../../../samples/Lua/TestLua
cp -r ../../../../scripting/lua/script/ ../../../../samples/Lua/TestLua/Resources
$(LOG_LINK)$(CXX) $(CXXFLAGS) $(OBJECTS) -o $@ $(SHAREDLIBS) $(STATICLIBS) $(LIBS)
$(OBJ_DIR)/%.o: ../%.cpp $(CORE_MAKEFILE_LIST)

View File

@ -44,3 +44,5 @@ files
postbuild "${MARMALADE_ROOT}/s3e/python/python ./cccopy.py -s ../../../../samples/Cpp/TestCpp/Resources/ -d ../../../../samples/Lua/TestLua/Resources/"
postbuild "${MARMALADE_ROOT}/s3e/python/python ./cccopy.py -s ../../../../scripting/lua/script/ -d ../../../../samples/Lua/TestLua/Resources/"

View File

@ -18,7 +18,9 @@ SOURCES = main.cpp \
$(COCOS_ROOT)/scripting/lua/cocos2dx_support/CCLuaStack.cpp \
$(COCOS_ROOT)/scripting/lua/cocos2dx_support/Cocos2dxLuaLoader.cpp \
$(COCOS_ROOT)/scripting/lua/cocos2dx_support/LuaCocos2d.cpp \
$(COCOS_ROOT)/scripting/lua/cocos2dx_support/tolua_fix.c
$(COCOS_ROOT)/scripting/lua/cocos2dx_support/tolua_fix.c \
$(COCOS_ROOT)/scripting/lua/cocos2dx_support/CCBProxy.cpp \
$(COCOS_ROOT)/scripting/lua/cocos2dx_support/Lua_extensions_CCB.cpp \
include $(COCOS2DX_PATH)/../extensions/proj.nacl/Makefile
SOURCES += $(addprefix $(COCOS_ROOT)/extensions/, $(EXTENSIONS_SOURCES))

View File

@ -0,0 +1,161 @@
/****************************************************************************
Copyright (c) 2013 cocos2d-x.org
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "CCBProxy.h"
CCBReader* CCBProxy::createCCBreader()
{
CCNodeLoaderLibrary *ccNodeLoaderLibrary = CCNodeLoaderLibrary::sharedCCNodeLoaderLibrary();
ccNodeLoaderLibrary->registerCCNodeLoader("", CCBLayerLoader::loader());
CCBReader * pCCBReader = new CCBReader(ccNodeLoaderLibrary);
pCCBReader->autorelease();
return pCCBReader;
}
CCNode* CCBProxy::readCCBFromFile(const char *pszFileName,CCBReader* pCCBReader,bool bSetOwner)
{
if (NULL == pCCBReader || NULL == pszFileName || 0 == strlen(pszFileName)) {
return NULL;
}
CCNode *pNode = NULL;
if (bSetOwner) {
pNode = pCCBReader->readNodeGraphFromFile(pszFileName,this);
}
else
{
pNode = pCCBReader->readNodeGraphFromFile(pszFileName);
}
return pNode;
}
const char* CCBProxy::getNodeTypeName(CCNode* pNode)
{
if (NULL == pNode) {
return NULL;
}
if (NULL != dynamic_cast<CCSprite*>(pNode)) {
return "CCSprite";
}
if (NULL != dynamic_cast<CCControlButton*>(pNode)) {
return "CCControlButton";
}
if (NULL != dynamic_cast<CCLayer*>(pNode)) {
return "CCLayer";
}
if (NULL != dynamic_cast<CCLayerColor*>(pNode)) {
return "CCLayerColor";
}
if (NULL != dynamic_cast<CCLayerGradient*>(pNode)) {
return "CCLayerGradient";
}
if (NULL != dynamic_cast<CCScale9Sprite*>(pNode)) {
return "CCLayerGradient";
}
if (NULL != dynamic_cast<CCLabelTTF*>(pNode)) {
return "CCLabelTTF";
}
if (NULL != dynamic_cast<CCLabelBMFont*>(pNode)) {
return "CCLabelBMFont";
}
if (NULL != dynamic_cast<CCMenu*>(pNode)) {
return "CCMenu";
}
if (NULL != dynamic_cast<CCMenuItem*>(pNode)) {
return "CCMenuItem";
}
if (NULL != dynamic_cast<CCMenuItemLabel*>(pNode)) {
return "CCMenuItemLabel";
}
if (NULL != dynamic_cast<CCMenuItemAtlasFont*>(pNode)) {
return "CCMenuItemAtlasFont";
}
if (NULL != dynamic_cast<CCMenuItemFont*>(pNode)) {
return "CCMenuItemFont";
}
if (NULL != dynamic_cast<CCMenuItemSprite*>(pNode)) {
return "CCMenuItemSprite";
}
if (NULL != dynamic_cast<CCMenuItemImage*>(pNode)) {
return "CCMenuItemImage";
}
if (NULL != dynamic_cast<CCMenuItemToggle*>(pNode)) {
return "CCMenuItemToggle";
}
if (NULL != dynamic_cast<CCString*>(pNode)) {
return "CCString";
}
if (NULL != dynamic_cast<CCParticleSystemQuad*>(pNode)) {
return "CCParticleSystemQuad";
}
return "No Support";
}
void CCBProxy::setCallback(CCNode* pNode,int nHandle)
{
if (NULL == pNode) {
return;
}
if (strcmp(this->getNodeTypeName(pNode), "CCMenuItem") == 0)
{
CCMenuItem *pMenuItem = dynamic_cast<CCMenuItem*>(pNode);
if (NULL != pMenuItem) {
pMenuItem->registerScriptTapHandler(nHandle);
}
}
else if (strcmp(this->getNodeTypeName(pNode), "CCControlButton") == 0)
{
CCControlButton *pBtnItem = dynamic_cast<CCControlButton*>(pNode);
if (NULL != pBtnItem) {
//UNOD,need Btn Pros to addHanldeOfControlEvent
pBtnItem->addHandleOfControlEvent(nHandle,CCControlEventTouchUpInside);
}
}
}

View File

@ -0,0 +1,28 @@
#ifndef __CCBPROXY_H_
#define __CCBPROXY_H_
#include "cocos2d.h"
#include "CCLuaEngine.h"
#include "SimpleAudioEngine.h"
#include "cocos-ext.h"
USING_NS_CC;
USING_NS_CC_EXT;
class CCBProxy : public CCLayer{
public:
CCBProxy() { }
virtual ~ CCBProxy(){ }
CCB_STATIC_NEW_AUTORELEASE_OBJECT_WITH_INIT_METHOD(CCBProxy, create);
CCBReader* createCCBreader();
CCNode* readCCBFromFile(const char *pszFileName,CCBReader* pCCBReader,bool bSetOwner = false);
const char* getNodeTypeName(CCNode* pNode);
void setCallback(CCNode* pNode,int nHandle);
};
class CCBLayerLoader:public CCLayerLoader{
public:
CCB_STATIC_NEW_AUTORELEASE_OBJECT_METHOD(CCBLayerLoader, loader);
};
#endif // __CCBPROXY_H_

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,14 @@
#ifndef __LUA_EXTENSIONS_CCB_H__
#define __LUA_EXTENSIONS_CCB_H__
#ifdef __cplusplus
extern "C" {
#endif
#include "tolua++.h"
#ifdef __cplusplus
}
#endif
TOLUA_API int tolua_extensions_ccb_open(lua_State* tolua_S);
#endif //__LUA_CCB_H__

View File

@ -11,6 +11,8 @@ LOCAL_SRC_FILES := ../cocos2dx_support/CCLuaBridge.cpp \
../cocos2dx_support/CCLuaValue.cpp \
../cocos2dx_support/Cocos2dxLuaLoader.cpp \
../cocos2dx_support/LuaCocos2d.cpp \
../cocos2dx_support/CCBProxy.cpp \
../cocos2dx_support/Lua_extensions_CCB.cpp \
../tolua/tolua_event.c \
../tolua/tolua_is.c \
../tolua/tolua_map.c \

View File

@ -44,7 +44,9 @@ SOURCES = ../lua/lapi.o \
../cocos2dx_support/CCLuaStack.cpp \
../cocos2dx_support/CCLuaValue.cpp \
../cocos2dx_support/Cocos2dxLuaLoader.cpp \
../cocos2dx_support/LuaCocos2d.cpp
../cocos2dx_support/LuaCocos2d.cpp \
../cocos2dx_support/CCBProxy.cpp \
../cocos2dx_support/Lua_extensions_CCB.cpp
include ../../../cocos2dx/proj.emscripten/cocos2dx.mk

View File

@ -44,7 +44,9 @@ SOURCES = ../lua/lapi.o \
../cocos2dx_support/CCLuaStack.cpp \
../cocos2dx_support/CCLuaValue.cpp \
../cocos2dx_support/Cocos2dxLuaLoader.cpp \
../cocos2dx_support/LuaCocos2d.cpp
../cocos2dx_support/LuaCocos2d.cpp \
../cocos2dx_support/CCBProxy.cpp \
../cocos2dx_support/Lua_extensions_CCB.cpp \
include ../../../cocos2dx/proj.linux/cocos2dx.mk

View File

@ -34,7 +34,7 @@ SOURCES = ../lua/lapi.c \
../tolua/tolua_is.c \
../tolua/tolua_map.c \
../tolua/tolua_push.c \
../tolua/tolua_to.c
../tolua/tolua_to.c
COCOS_ROOT = ../../..
@ -51,3 +51,7 @@ $(TARGET): $(OBJECTS) $(CORE_MAKEFILE_LIST)
$(OBJ_DIR)/%.o: ../%.c $(CORE_MAKEFILE_LIST)
@mkdir -p $(@D)
$(LOG_CC)$(NACL_CC) -MMD $(CCFLAGS) $(INCLUDES) $(DEFINES) $(VISIBILITY) -c $< -o $@
$(OBJ_DIR)/%.o: ../%.cpp $(CORE_MAKEFILE_LIST)
@mkdir -p $(@D)
$(LOG_CXX)$(NACL_CXX) -MMD $(CXXFLAGS) $(INCLUDES) $(DEFINES) -c $< -o $@

View File

@ -0,0 +1,100 @@
ccb = ccb or {}
function CCBReaderLoad(strFilePath,proxy,bSetOwner,strOwnerName)
if nil == proxy then
return
end
local ccbReader = proxy:createCCBReader()
local node = proxy:readCCBFromFile(strFilePath,ccbReader,bSetOwner)
local owner = ccbReader:getOwner()
local rootName = ""
--owner set in readCCBFromFile is proxy
if nil ~= owner then
--Callbacks
local ownerCallbackNames = tolua.cast(ccbReader:getOwnerCallbackNames(),"CCArray")
local ownerCallbackNodes = tolua.cast(ccbReader:getOwnerCallbackNodes(),"CCArray")
local i = 1
for i = 1,ownerCallbackNames:count() do
local callbackName = tolua.cast(ownerCallbackNames:objectAtIndex(i - 1),"CCString")
local callbackNode = tolua.cast(ownerCallbackNodes:objectAtIndex(i - 1),"CCNode")
if "" ~= strOwnerName and nil ~= ccb[strOwnerName] then
proxy:setCallback(callbackNode,ccb[strOwnerName][callbackName:getCString()])
end
end
--Variables
local ownerOutletNames = tolua.cast(ccbReader:getOwnerOutletNames(),"CCArray")
local ownerOutletNodes = tolua.cast(ccbReader:getOwnerOutletNodes(),"CCArray")
for i = 1, ownerOutletNames:count() do
local outletName = tolua.cast(ownerOutletNames:objectAtIndex(i - 1),"CCString")
local outletNode = tolua.cast(ownerOutletNodes:objectAtIndex(i - 1),"CCNode")
if "" ~= strOwnerName and nil ~= ccb[strOwnerName] then
ccb[strOwnerName][outletName:getCString()] = outletNode
end
end
end
local nodesWithAnimationManagers = tolua.cast(ccbReader:getNodesWithAnimationManagers(),"CCArray")
local animationManagersForNodes = tolua.cast(ccbReader:getAnimationManagersForNodes(),"CCArray")
for i = 1 , nodesWithAnimationManagers:count() do
local innerNode = tolua.cast(nodesWithAnimationManagers:objectAtIndex(i - 1),"CCNode")
local animationManager = tolua.cast(animationManagersForNodes:objectAtIndex(i - 1),"CCBAnimationManager")
local documentControllerName = animationManager:getDocumentControllerName()
if "" == documentControllerName then
end
--Callbacks
local documentCallbackNames = tolua.cast(animationManager:getDocumentCallbackNames(),"CCArray")
local documentCallbackNodes = tolua.cast(animationManager:getDocumentCallbackNodes(),"CCArray")
for i = 1,documentCallbackNames:count() do
local callbackName = tolua.cast(documentCallbackNames:objectAtIndex(i - 1),"CCString")
local callbackNode = tolua.cast(documentCallbackNodes:objectAtIndex(i - 1),"CCNode")
if "" ~= documentControllerName and nil ~= ccb[documentControllerName] then
proxy:setCallback(callbackNode,ccb[documentControllerName][callbackName:getCString()])
end
--local nHandler = _G[callbackName:getCString()]
--proxy:setCallback(callbackNode,nHandler)
--callbackNode.setCallback(controller[callbackName], controller);
end
--Variables
local documentOutletNames = tolua.cast(animationManager:getDocumentOutletNames(),"CCArray")
local documentOutletNodes = tolua.cast(animationManager:getDocumentOutletNodes(),"CCArray")
for i = 1, documentOutletNames:count() do
local outletName = tolua.cast(documentOutletNames:objectAtIndex(i - 1),"CCString")
local outletNode = tolua.cast(documentOutletNodes:objectAtIndex(i - 1),"CCNode")
if nil ~= ccb[documentControllerName] then
ccb[documentControllerName][outletName:getCString()] = outletNode
end
end
--[[
if (typeof(controller.onDidLoadFromCCB) == "function")
controller.onDidLoadFromCCB();
]]--
--Setup timeline callbacks
local keyframeCallbacks = animationManager:getKeyframeCallbacks()
for i = 1 , keyframeCallbacks:count() do
local callbackCombine = tolua.cast(keyframeCallbacks:objectAtIndex(i - 1),"CCString"):getCString()
local beignIndex,endIndex = string.find(callbackCombine,":")
local callbackType = tonumber(string.sub(callbackCombine,1,beignIndex - 1))
local callbackName = string.sub(callbackCombine,endIndex + 1, -1)
--Document callback
if 1 == callbackType and nil ~= ccb[documentControllerName] then
local callfunc = CCCallFunc:create(ccb[documentControllerName][callbackName])
animationManager:setCallFuncForLuaCallbackNamed(callfunc, callbackCombine);
elseif 2 == callbackType and nil ~= owner then --Owner callback
local callfunc = CCCallFunc:create(ccb[strOwnerName][callbackName])
animationManager:setCallFuncForLuaCallbackNamed(callfunc, callbackCombine)
end
end
--start animation
local autoPlaySeqId = animationManager:getAutoPlaySequenceId()
if -1 ~= autoPlaySeqId then
animationManager:runAnimationsForSequenceIdTweenDuration(autoPlaySeqId, 0)
end
end
return node
end