From 5fc72581cb3079331b426ca2f5e96a2b83928ba7 Mon Sep 17 00:00:00 2001 From: James Chen Date: Wed, 2 May 2012 17:50:26 +0800 Subject: [PATCH 1/4] issue #1204: Removed CCApplication::initInstance. --- Box2D/proj.win32/Box2D.win32.vcproj | 4 +- HelloLua/Classes/AppDelegate.cpp | 38 ---------- HelloLua/Classes/AppDelegate.h | 5 -- HelloLua/proj.android/jni/helloworld/main.cpp | 5 +- HelloLua/proj.win32/HelloLua.win32.vcproj | 4 +- HelloLua/proj.win32/main.cpp | 15 ++-- HelloWorld/Classes/AppDelegate.cpp | 69 +------------------ HelloWorld/Classes/AppDelegate.h | 5 -- .../gen/org/cocos2dx/application/R.java | 52 +++++++------- .../proj.android/jni/helloworld/main.cpp | 5 +- HelloWorld/proj.win32/HelloWorld.win32.vcproj | 4 +- HelloWorld/proj.win32/main.cpp | 12 +++- chipmunk/proj.win32/chipmunk.win32.vcproj | 4 +- cocos2dx/platform/CCApplicationProtocol.h | 5 -- cocos2dx/platform/CCEGLViewProtocol.cpp | 15 +++- cocos2dx/platform/CCEGLViewProtocol.h | 5 +- cocos2dx/platform/android/CCApplication.cpp | 10 +-- cocos2dx/platform/win32/CCApplication.cpp | 18 ++--- cocos2dx/platform/win32/CCEGLView.cpp | 22 ++++-- cocos2dx/platform/win32/CCEGLView.h | 2 +- cocos2dx/platform/win32/CCImage.cpp | 57 +++------------ cocos2dx/proj.win32/cocos2d-win32.vcproj | 4 +- .../LuaCocos2d.cpp.REMOVED.git-id | 2 +- lua/proj.win32/liblua.vcproj | 4 +- tests/AppDelegate.cpp | 64 ----------------- tests/AppDelegate.h | 5 -- tests/proj.android/jni/tests/main.cpp | 5 +- tests/proj.win32/main.cpp | 11 ++- tests/proj.win32/test.win32.vcproj | 4 +- .../android/jni/LuaProjectTemplate/main.cpp | 22 +++--- tools/tolua++/SimpleAudioEngine.pkg | 6 -- 31 files changed, 148 insertions(+), 335 deletions(-) diff --git a/Box2D/proj.win32/Box2D.win32.vcproj b/Box2D/proj.win32/Box2D.win32.vcproj index dc04b76b82..c5e04f29e8 100644 --- a/Box2D/proj.win32/Box2D.win32.vcproj +++ b/Box2D/proj.win32/Box2D.win32.vcproj @@ -21,7 +21,7 @@ OutputDirectory="$(SolutionDir)$(ConfigurationName).win32" IntermediateDirectory="$(ConfigurationName).win32" ConfigurationType="4" - CharacterSet="1" + CharacterSet="2" > Create(TEXT("cocos2d: Hello World"), CC_WIDTH, CC_HEIGHT)); - -#endif // CC_PLATFORM_WIN32 - -#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) - - // OpenGLView initialized in testsAppDelegate.mm on ios platform, nothing need to do here. - -#endif // CC_PLATFORM_IOS - -#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) - - // OpenGLView initialized in HelloWorld/android/jni/helloworld/main.cpp - // the default setting is to create a fullscreen view - // if you want to use auto-scale, please enable view->create(320,480) in main.cpp - -#endif // CC_PLATFORM_ANDROID - -#if (CC_TARGET_PLATFORM == CC_PLATFORM_MARMALADE) - // MaxAksenov said it's NOT a very elegant solution. I agree, haha - CCDirector::sharedDirector()->setDeviceOrientation(kCCDeviceOrientationLandscapeLeft); -#endif - bRet = true; - } while (0); - return bRet; -} - bool AppDelegate::applicationDidFinishLaunching() { // initialize director diff --git a/HelloLua/Classes/AppDelegate.h b/HelloLua/Classes/AppDelegate.h index 06c18558c4..76c1c6089f 100644 --- a/HelloLua/Classes/AppDelegate.h +++ b/HelloLua/Classes/AppDelegate.h @@ -14,11 +14,6 @@ public: AppDelegate(); virtual ~AppDelegate(); - /** - @brief Implement for initialize OpenGL instance, set source path, etc... - */ - virtual bool initInstance(); - /** @brief Implement CCDirector and CCScene init code here. @return true Initialize success, app continue. diff --git a/HelloLua/proj.android/jni/helloworld/main.cpp b/HelloLua/proj.android/jni/helloworld/main.cpp index a8d2945171..ceb2aa7b72 100644 --- a/HelloLua/proj.android/jni/helloworld/main.cpp +++ b/HelloLua/proj.android/jni/helloworld/main.cpp @@ -26,9 +26,8 @@ void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit(JNIEnv* env, jobject thi { CCEGLView *view = &CCEGLView::sharedOpenGLView(); view->setFrameSize(w, h); - // if you want to run in WVGA with HVGA resource, set it - // view->create(480, 320); - CCDirector::sharedDirector()->setOpenGLView(view); + // set the design resolution screen size, if you want to use Design Resoulution scaled to current screen, please uncomment next line. + // view.setDesignResolutionSize(480, 320); AppDelegate *pAppDelegate = new AppDelegate(); CCApplication::sharedApplication().run(); diff --git a/HelloLua/proj.win32/HelloLua.win32.vcproj b/HelloLua/proj.win32/HelloLua.win32.vcproj index b6eb719bba..f5455bac3f 100644 --- a/HelloLua/proj.win32/HelloLua.win32.vcproj +++ b/HelloLua/proj.win32/HelloLua.win32.vcproj @@ -20,7 +20,7 @@ OutputDirectory="$(SolutionDir)$(ConfigurationName).win32" IntermediateDirectory="$(ConfigurationName).win32" ConfigurationType="1" - CharacterSet="1" + CharacterSet="2" > Create(TEXT("cocos2d: Hello World"), 480, 320)); - - // set the design resolution screen size, if you want to use Design Resoulution scaled to current screen, please uncomment next line. - //pMainWnd->setDesignResolutionSize(480, 320); - -#endif // CC_PLATFORM_WIN32 -#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) - - // OpenGLView initialized in testsAppDelegate.mm on ios platform, nothing need to do here. - -#endif // CC_PLATFORM_IOS -#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) - - // OpenGLView initialized in HelloWorld/android/jni/helloworld/main.cpp - // the default setting is to create a fullscreen view - // if you want to use auto-scale, please enable view->create(320,480) in main.cpp - // if the resources under '/sdcard" or other writeable path, set it. - // warning: the audio source should in assets/ - // cocos2d::CCFileUtils::setResourcePath("/sdcard"); - -#endif // CC_PLATFORM_ANDROID - -#if (CC_TARGET_PLATFORM == CC_PLATFORM_MARMALADE) - // MaxAksenov said it's NOT a very elegant solution. I agree, haha - CCDirector::sharedDirector()->setDeviceOrientation(kCCDeviceOrientationLandscapeLeft); -#endif -#if (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX) - - // Initialize OpenGLView instance, that release by CCDirector when application terminate. - // The HelloWorld is designed as HVGA. - CCEGLView * pMainWnd = new CCEGLView(); - CC_BREAK_IF(! pMainWnd - || ! pMainWnd->Create("cocos2d: Hello World", 480, 320 ,480, 320)); - - CCFileUtils::setResourcePath("../Resources/"); - -#endif // CC_PLATFORM_LINUX - -#if (CC_TARGET_PLATFORM == CC_PLATFORM_BADA) - - CCEGLView * pMainWnd = new CCEGLView(); - CC_BREAK_IF(! pMainWnd|| ! pMainWnd->Create(this, 480, 320)); - pMainWnd->setDeviceOrientation(Osp::Ui::ORIENTATION_LANDSCAPE); - CCFileUtils::setResourcePath("/Res/"); - -#endif // CC_PLATFORM_BADA - -#if (CC_TARGET_PLATFORM == CC_PLATFORM_QNX) - CCEGLView * pMainWnd = new CCEGLView(); - CC_BREAK_IF(! pMainWnd|| ! pMainWnd->Create(1024, 600)); - CCFileUtils::setResourcePath("app/native/Resources"); -#endif // CC_PLATFORM_QNX - bRet = true; - } while (0); - return bRet; +AppDelegate::~AppDelegate() +{ } bool AppDelegate::applicationDidFinishLaunching() { diff --git a/HelloWorld/Classes/AppDelegate.h b/HelloWorld/Classes/AppDelegate.h index 06c18558c4..76c1c6089f 100644 --- a/HelloWorld/Classes/AppDelegate.h +++ b/HelloWorld/Classes/AppDelegate.h @@ -14,11 +14,6 @@ public: AppDelegate(); virtual ~AppDelegate(); - /** - @brief Implement for initialize OpenGL instance, set source path, etc... - */ - virtual bool initInstance(); - /** @brief Implement CCDirector and CCScene init code here. @return true Initialize success, app continue. diff --git a/HelloWorld/proj.android/gen/org/cocos2dx/application/R.java b/HelloWorld/proj.android/gen/org/cocos2dx/application/R.java index 056c00f791..979d1ae7fc 100644 --- a/HelloWorld/proj.android/gen/org/cocos2dx/application/R.java +++ b/HelloWorld/proj.android/gen/org/cocos2dx/application/R.java @@ -1,26 +1,26 @@ -/* AUTO-GENERATED FILE. DO NOT MODIFY. - * - * This class was automatically generated by the - * aapt tool from the resource data it found. It - * should not be modified by hand. - */ - -package org.cocos2dx.application; - -public final class R { - public static final class attr { - } - public static final class drawable { - public static final int icon=0x7f020000; - } - public static final class id { - public static final int helloworld_gl_surfaceview=0x7f050001; - public static final int textField=0x7f050000; - } - public static final class layout { - public static final int helloworld_demo=0x7f030000; - } - public static final class string { - public static final int app_name=0x7f040000; - } -} +/* AUTO-GENERATED FILE. DO NOT MODIFY. + * + * This class was automatically generated by the + * aapt tool from the resource data it found. It + * should not be modified by hand. + */ + +package org.cocos2dx.application; + +public final class R { + public static final class attr { + } + public static final class drawable { + public static final int icon=0x7f020000; + } + public static final class id { + public static final int helloworld_gl_surfaceview=0x7f050001; + public static final int textField=0x7f050000; + } + public static final class layout { + public static final int helloworld_demo=0x7f030000; + } + public static final class string { + public static final int app_name=0x7f040000; + } +} diff --git a/HelloWorld/proj.android/jni/helloworld/main.cpp b/HelloWorld/proj.android/jni/helloworld/main.cpp index f1bfd6cedd..dce5fa5213 100644 --- a/HelloWorld/proj.android/jni/helloworld/main.cpp +++ b/HelloWorld/proj.android/jni/helloworld/main.cpp @@ -29,9 +29,8 @@ void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit(JNIEnv* env, jobject thi { CCEGLView *view = &CCEGLView::sharedOpenGLView(); view->setFrameSize(w, h); - // if you want to run in WVGA with HVGA resource, set it - // view->setDesignResolutionSize(480, 320); Please change it to (320, 480) if you're in portrait mode. - CCDirector::sharedDirector()->setOpenGLView(view); + // set the design resolution screen size, if you want to use Design Resoulution scaled to current screen, please uncomment next line. + // view.setDesignResolutionSize(480, 320); AppDelegate *pAppDelegate = new AppDelegate(); CCApplication::sharedApplication().run(); diff --git a/HelloWorld/proj.win32/HelloWorld.win32.vcproj b/HelloWorld/proj.win32/HelloWorld.win32.vcproj index d9580a04f8..815b029c91 100644 --- a/HelloWorld/proj.win32/HelloWorld.win32.vcproj +++ b/HelloWorld/proj.win32/HelloWorld.win32.vcproj @@ -21,7 +21,7 @@ OutputDirectory="$(SolutionDir)$(ConfigurationName).win32" IntermediateDirectory="$(ConfigurationName).win32" ConfigurationType="1" - CharacterSet="1" + CharacterSet="2" > 0) + { + strncpy(m_szViewName, pszViewName, sizeof(m_szViewName)); + } +} + +const char* CCEGLViewProtocol::getViewName() +{ + return m_szViewName; +} + void CCEGLViewProtocol::handleTouchesBegin(int num, int ids[], float xs[], float ys[]) { CCSet set; diff --git a/cocos2dx/platform/CCEGLViewProtocol.h b/cocos2dx/platform/CCEGLViewProtocol.h index 3e707da2bc..561a57540f 100644 --- a/cocos2dx/platform/CCEGLViewProtocol.h +++ b/cocos2dx/platform/CCEGLViewProtocol.h @@ -33,7 +33,9 @@ public: virtual void setViewPortInPoints(float x , float y , float w , float h); virtual void setScissorInPoints(float x , float y , float w , float h); virtual float getMainScreenScale(); - + virtual void setViewName(const char* pszViewName); + const char* getViewName(); + /** handle touch events by default, if you want to custom your handles, please override these functions */ virtual void handleTouchesBegin(int num, int ids[], float xs[], float ys[]); virtual void handleTouchesMove(int num, int ids[], float xs[], float ys[]); @@ -49,6 +51,7 @@ protected: CCSize m_sSizeInPixel; CCSize m_sSizeInPoint; CCRect m_rcViewPort; + char m_szViewName[50]; }; NS_CC_END diff --git a/cocos2dx/platform/android/CCApplication.cpp b/cocos2dx/platform/android/CCApplication.cpp index 59e32286b8..684dfa0b3f 100644 --- a/cocos2dx/platform/android/CCApplication.cpp +++ b/cocos2dx/platform/android/CCApplication.cpp @@ -30,11 +30,11 @@ CCApplication::~CCApplication() int CCApplication::run() { - // Initialize instance and cocos2d. - if (! initInstance() || ! applicationDidFinishLaunching()) - { - return 0; - } + // Initialize instance and cocos2d. + if (! applicationDidFinishLaunching()) + { + return 0; + } return -1; } diff --git a/cocos2dx/platform/win32/CCApplication.cpp b/cocos2dx/platform/win32/CCApplication.cpp index 7ccc363786..a4f2ec87a0 100644 --- a/cocos2dx/platform/win32/CCApplication.cpp +++ b/cocos2dx/platform/win32/CCApplication.cpp @@ -43,7 +43,7 @@ int CCApplication::run() QueryPerformanceCounter(&nLast); // Initialize instance and cocos2d. - if (! initInstance() || ! applicationDidFinishLaunching()) + if (!applicationDidFinishLaunching()) { return 0; } @@ -160,17 +160,17 @@ static void PVRFrameEnableControlWindow(bool bEnable) return; } - const wchar_t * wszValue = L"hide_gui"; - const wchar_t * wszNewData = (bEnable) ? L"NO" : L"YES"; - wchar_t wszOldData[256] = {0}; - DWORD dwSize = sizeof(wszOldData); - LSTATUS status = RegQueryValueExW(hKey, wszValue, 0, NULL, (LPBYTE)wszOldData, &dwSize); + const char * szValue = "hide_gui"; + const char * szNewData = (bEnable) ? "NO" : "YES"; + char szOldData[256] = {0}; + DWORD dwSize = sizeof(szOldData); + LSTATUS status = RegQueryValueEx(hKey, szValue, 0, NULL, (LPBYTE)szOldData, &dwSize); if (ERROR_FILE_NOT_FOUND == status // the key not exist || (ERROR_SUCCESS == status // or the hide_gui value is exist - && 0 != wcscmp(wszNewData, wszOldData))) // but new data and old data not equal + && 0 != strcmp(szNewData, szOldData))) // but new data and old data not equal { - dwSize = sizeof(wchar_t) * (wcslen(wszNewData) + 1); - RegSetValueEx(hKey, wszValue, 0, REG_SZ, (const BYTE *)wszNewData, dwSize); + dwSize = sizeof(wchar_t) * (strlen(szNewData) + 1); + RegSetValueEx(hKey, szValue, 0, REG_SZ, (const BYTE *)szNewData, dwSize); } RegCloseKey(hKey); diff --git a/cocos2dx/platform/win32/CCEGLView.cpp b/cocos2dx/platform/win32/CCEGLView.cpp index f7a781c1df..8ecd34d320 100644 --- a/cocos2dx/platform/win32/CCEGLView.cpp +++ b/cocos2dx/platform/win32/CCEGLView.cpp @@ -165,8 +165,8 @@ private: ////////////////////////////////////////////////////////////////////////// // impliment CCEGLView ////////////////////////////////////////////////////////////////////////// -static CCEGLView * s_pMainWindow; -static const WCHAR * kWindowClassName = L"Cocos2dxWin32"; +static CCEGLView* s_pMainWindow = NULL; +static const char* kWindowClassName = "Cocos2dxWin32"; static LRESULT CALLBACK _WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { @@ -225,7 +225,7 @@ bool CCEGLView::Create(LPCTSTR pTitle, int w, int h) m_hWnd = CreateWindowEx( WS_EX_APPWINDOW | WS_EX_WINDOWEDGE, // Extended Style For The Window kWindowClassName, // Class Name - pTitle, // Window Title + m_szViewName, // Window Title WS_CAPTION | WS_POPUPWINDOW | WS_MINIMIZEBOX, // Defined Window Style 0, 0, // Window Position 0, // Window Width @@ -408,7 +408,6 @@ void CCEGLView::end() } s_pMainWindow = NULL; UnregisterClass(kWindowClassName, GetModuleHandle(NULL)); - delete this; } @@ -459,7 +458,12 @@ void CCEGLView::resize(int width, int height) m_pEGL->resizeSurface(); } - setFrameSize(width, height); + CCEGLViewProtocol::setFrameSize(width, height); +} + +void CCEGLView::setFrameSize(float width, float height) +{ + Create((LPCTSTR)m_szViewName, width, height); } void CCEGLView::centerWindow() @@ -509,8 +513,12 @@ void CCEGLView::setContentScaleFactor(float contentScaleFactor) CCEGLView& CCEGLView::sharedOpenGLView() { - CC_ASSERT(s_pMainWindow); - return *s_pMainWindow; + static CCEGLView* s_pEglView = NULL; + if (s_pEglView == NULL) + { + s_pEglView = new CCEGLView(); + } + return *s_pEglView; } NS_CC_END diff --git a/cocos2dx/platform/win32/CCEGLView.h b/cocos2dx/platform/win32/CCEGLView.h index 5da7f47d08..ad05e5d99b 100644 --- a/cocos2dx/platform/win32/CCEGLView.h +++ b/cocos2dx/platform/win32/CCEGLView.h @@ -48,7 +48,7 @@ public: bool canSetContentScaleFactor(); void setContentScaleFactor(float contentScaleFactor); - + virtual void setFrameSize(float width, float height); virtual bool Create(LPCTSTR pTitle, int w, int h); virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam); diff --git a/cocos2dx/platform/win32/CCImage.cpp b/cocos2dx/platform/win32/CCImage.cpp index 93ca901bfe..6b22ebb5a5 100644 --- a/cocos2dx/platform/win32/CCImage.cpp +++ b/cocos2dx/platform/win32/CCImage.cpp @@ -60,39 +60,11 @@ public: // release temp font resource if (m_curFontPath.size() > 0) { - wchar_t * pwszBuffer = utf8ToUtf16(m_curFontPath); - if (pwszBuffer) - { - RemoveFontResource(pwszBuffer); - SendMessage( m_hWnd, WM_FONTCHANGE, 0, 0); - delete [] pwszBuffer; - pwszBuffer = NULL; - } + RemoveFontResource(m_curFontPath.c_str()); + SendMessage( m_hWnd, WM_FONTCHANGE, 0, 0); } } - wchar_t * utf8ToUtf16(std::string nString) - { - wchar_t * pwszBuffer = NULL; - do - { - if (nString.size() < 0) - { - break; - } - // utf-8 to utf-16 - int nLen = nString.size(); - int nBufLen = nLen + 1; - pwszBuffer = new wchar_t[nBufLen]; - CC_BREAK_IF(! pwszBuffer); - memset(pwszBuffer,0,nBufLen); - nLen = MultiByteToWideChar(CP_UTF8, 0, nString.c_str(), nLen, pwszBuffer, nBufLen); - pwszBuffer[nLen] = '\0'; - } while (0); - return pwszBuffer; - - } - bool setFont(const char * pFontName = NULL, int nSize = 0) { bool bRet = false; @@ -141,31 +113,20 @@ public: // release old font register if (m_curFontPath.size() > 0) { - wchar_t * pwszBuffer = utf8ToUtf16(m_curFontPath); - if (pwszBuffer) + if(RemoveFontResource(m_curFontPath.c_str())) { - if(RemoveFontResource(pwszBuffer)) - { - SendMessage( m_hWnd, WM_FONTCHANGE, 0, 0); - } - delete [] pwszBuffer; - pwszBuffer = NULL; - } + SendMessage( m_hWnd, WM_FONTCHANGE, 0, 0); + } } + fontPath.size()>0?(m_curFontPath = fontPath):(m_curFontPath.clear()); // register temp font if (m_curFontPath.size() > 0) { - wchar_t * pwszBuffer = utf8ToUtf16(m_curFontPath); - if (pwszBuffer) + if(AddFontResource(m_curFontPath.c_str())) { - if(AddFontResource(pwszBuffer)) - { - SendMessage( m_hWnd, WM_FONTCHANGE, 0, 0); - } - delete [] pwszBuffer; - pwszBuffer = NULL; - } + SendMessage( m_hWnd, WM_FONTCHANGE, 0, 0); + } } } m_hFont = NULL; diff --git a/cocos2dx/proj.win32/cocos2d-win32.vcproj b/cocos2dx/proj.win32/cocos2d-win32.vcproj index f441c84c31..76795d7a50 100644 --- a/cocos2dx/proj.win32/cocos2d-win32.vcproj +++ b/cocos2dx/proj.win32/cocos2d-win32.vcproj @@ -21,7 +21,7 @@ OutputDirectory="$(SolutionDir)$(ConfigurationName).win32" IntermediateDirectory="$(ConfigurationName).win32" ConfigurationType="2" - CharacterSet="1" + CharacterSet="2" > Create(TEXT("cocos2d: tests"), 480, 320)); - -#endif // CC_PLATFORM_WIN32 - -#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) - - // OpenGLView initialized in testsAppDelegate.mm on ios platform, nothing need to do here. - -#endif // CC_PLATFORM_IOS - -#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) - - // Android doesn't need to do anything. - -#endif // CC_PLATFORM_ANDROID - -#if (CC_TARGET_PLATFORM == CC_PLATFORM_MARMALADE) - CCDirector::sharedDirector()->setDeviceOrientation(CCDeviceOrientationLandscapeLeft); -#endif - -#if (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX) - - // Initialize OpenGLView instance, that release by CCDirector when application terminate. - // The HelloWorld is designed as HVGA. - CCEGLView * pMainWnd = new CCEGLView(); - CC_BREAK_IF(! pMainWnd - || ! pMainWnd->Create("cocos2d: tests", 480, 320, 480, 320)); - - //set the base resource folder pay attention to add "/" - CCFileUtils::setResourcePath("../Resources/"); - -#endif // CC_PLATFORM_LINUX - -#if (CC_TARGET_PLATFORM == CC_PLATFORM_BADA) - - CCEGLView * pMainWnd = new CCEGLView(); - CC_BREAK_IF(! pMainWnd|| ! pMainWnd->Create(this, 480, 320)); - pMainWnd->setDeviceOrientation(Osp::Ui::ORIENTATION_LANDSCAPE); - CCFileUtils::setResourcePath("/Res/"); - -#endif // CC_PLATFORM_BADA - -#if (CC_TARGET_PLATFORM == CC_PLATFORM_QNX) - CCEGLView * pMainWnd = new CCEGLView(); - CC_BREAK_IF(! pMainWnd|| ! pMainWnd->Create(1024, 600)); - CCFileUtils::setResourcePath("app/native/Resources"); -#endif // CC_PLATFORM_QNX - - bRet = true; - } while (0); - return bRet; -} - bool AppDelegate::applicationDidFinishLaunching() { // initialize director diff --git a/tests/AppDelegate.h b/tests/AppDelegate.h index 06c18558c4..76c1c6089f 100644 --- a/tests/AppDelegate.h +++ b/tests/AppDelegate.h @@ -14,11 +14,6 @@ public: AppDelegate(); virtual ~AppDelegate(); - /** - @brief Implement for initialize OpenGL instance, set source path, etc... - */ - virtual bool initInstance(); - /** @brief Implement CCDirector and CCScene init code here. @return true Initialize success, app continue. diff --git a/tests/proj.android/jni/tests/main.cpp b/tests/proj.android/jni/tests/main.cpp index aeea16f4fe..e2ce2ef055 100644 --- a/tests/proj.android/jni/tests/main.cpp +++ b/tests/proj.android/jni/tests/main.cpp @@ -27,9 +27,8 @@ void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit(JNIEnv* env, jobject thi { CCEGLView *view = &CCEGLView::sharedOpenGLView(); view->setFrameSize(w, h); - // if you want to run in WVGA with HVGA resource, set it - // view->setDesignResolutionSize(480, 320); - CCDirector::sharedDirector()->setOpenGLView(view); + // set the design resolution screen size, if you want to use Design Resoulution scaled to current screen, please uncomment next line. + // view.setDesignResolutionSize(480, 320); AppDelegate *pAppDelegate = new AppDelegate(); CCApplication::sharedApplication().run(); diff --git a/tests/proj.win32/main.cpp b/tests/proj.win32/main.cpp index 5fb377fb07..6737f297f4 100644 --- a/tests/proj.win32/main.cpp +++ b/tests/proj.win32/main.cpp @@ -1,6 +1,8 @@ #include "main.h" - #include "AppDelegate.h" +#include "CCEGLView.h" + +USING_NS_CC; int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, @@ -12,6 +14,11 @@ int APIENTRY _tWinMain(HINSTANCE hInstance, // create the application instance AppDelegate app; + CCEGLView& eglView = CCEGLView::sharedOpenGLView(); + eglView.setViewName("Hello Tests"); + eglView.setFrameSize(800, 480); + // set the design resolution screen size, if you want to use Design Resoulution scaled to current screen, please uncomment next line. + // eglView.setDesignResolutionSize(480, 320); - return cocos2d::CCApplication::sharedApplication().run(); + return CCApplication::sharedApplication().run(); } diff --git a/tests/proj.win32/test.win32.vcproj b/tests/proj.win32/test.win32.vcproj index b8b7e9649b..f79ec75caa 100644 --- a/tests/proj.win32/test.win32.vcproj +++ b/tests/proj.win32/test.win32.vcproj @@ -21,7 +21,7 @@ OutputDirectory="$(SolutionDir)$(ConfigurationName).win32" IntermediateDirectory="$(ConfigurationName).win32" ConfigurationType="1" - CharacterSet="1" + CharacterSet="2" > getOpenGLView()) + if (!CCDirector::sharedDirector()->getOpenGLView()) { - cocos2d::CCEGLView *view = &cocos2d::CCEGLView::sharedOpenGLView(); - view->setFrameWidthAndHeight(w, h); - // if you want to run in WVGA with HVGA resource, set it - view->create(480, 320); - cocos2d::CCDirector::sharedDirector()->setOpenGLView(view); + CCEGLView *view = &CCEGLView::sharedOpenGLView(); + view->setFrameSize(w, h); + // set the design resolution screen size, if you want to use Design Resoulution scaled to current screen, please uncomment next line. + // view.setDesignResolutionSize(480, 320); AppDelegate *pAppDelegate = new AppDelegate(); - cocos2d::CCApplication::sharedApplication().run(); + CCApplication::sharedApplication().run(); } else { - cocos2d::CCTextureCache::reloadAllTextures(); - cocos2d::CCDirector::sharedDirector()->setGLDefaultValues(); + CCShaderCache::sharedShaderCache()->reloadDefaultShaders(); + CCTextureCache::reloadAllTextures(); + CCNotificationCenter::sharedNotificationCenter()->postNotification(EVNET_COME_TO_FOREGROUND, NULL); + CCDirector::sharedDirector()->setGLDefaultValues(); } } + + } diff --git a/tools/tolua++/SimpleAudioEngine.pkg b/tools/tolua++/SimpleAudioEngine.pkg index 4c011350dd..47002d2fdc 100644 --- a/tools/tolua++/SimpleAudioEngine.pkg +++ b/tools/tolua++/SimpleAudioEngine.pkg @@ -30,12 +30,6 @@ class SimpleAudioEngine */ static SimpleAudioEngine* sharedEngine(); - /** - @brief Set the zip file name - @param pszZipFileName The relative path of the .zip file - */ - static void setResource(const char* pszZipFileName); - /** @brief Preload background music @param pszFilePath The path of the background music file,or the FileName of T_SoundResInfo From 4580fffed7ddf5858e71462e534e3b1fceb3626a Mon Sep 17 00:00:00 2001 From: James Chen Date: Wed, 2 May 2012 18:18:43 +0800 Subject: [PATCH 2/4] issue #1204: Updated CCApplication.mm and ios template. --- cocos2dx/platform/ios/CCApplication.mm | 2 +- .../Classes/AppDelegate.cpp | 30 ---------------- .../cocos2dx.xctemplate/Classes/AppDelegate.h | 5 --- .../Classes/AppDelegate.cpp | 30 ---------------- .../Classes/AppDelegate.h | 4 --- .../Classes/AppDelegate.cpp | 30 ---------------- .../Classes/AppDelegate.h | 5 --- .../Classes/AppDelegate.cpp | 34 ------------------- .../Classes/AppDelegate.h | 5 --- 9 files changed, 1 insertion(+), 144 deletions(-) diff --git a/cocos2dx/platform/ios/CCApplication.mm b/cocos2dx/platform/ios/CCApplication.mm index f4ca5337eb..80aa8225c9 100644 --- a/cocos2dx/platform/ios/CCApplication.mm +++ b/cocos2dx/platform/ios/CCApplication.mm @@ -47,7 +47,7 @@ CCApplication::~CCApplication() int CCApplication::run() { - if (initInstance() && applicationDidFinishLaunching()) + if (applicationDidFinishLaunching()) { [[CCDirectorCaller sharedDirectorCaller] startMainLoop]; } diff --git a/template/xcode4/cocos2dx.xctemplate/Classes/AppDelegate.cpp b/template/xcode4/cocos2dx.xctemplate/Classes/AppDelegate.cpp index 9e688d9826..339020ed9b 100644 --- a/template/xcode4/cocos2dx.xctemplate/Classes/AppDelegate.cpp +++ b/template/xcode4/cocos2dx.xctemplate/Classes/AppDelegate.cpp @@ -22,36 +22,6 @@ AppDelegate::~AppDelegate() { } -bool AppDelegate::initInstance() -{ - bool bRet = false; - do - { -#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) - - // Initialize OpenGLView instance, that release by CCDirector when application terminate. - // The HelloWorld is designed as HVGA. - CCEGLView * pMainWnd = new CCEGLView(); - CC_BREAK_IF(! pMainWnd - || ! pMainWnd->Create(TEXT("cocos2d: Hello World"), 480, 320)); - -#endif // CC_PLATFORM_WIN32 - -#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) - // OpenGLView is initialized in AppDelegate.mm on ios platform, nothing need to do here. -#endif // CC_PLATFORM_IOS - -#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) - // OpenGLView is initialized in HelloWorld/android/jni/helloworld/main.cpp - // the default setting is to create a fullscreen view - // if you want to use auto-scale, please enable view->create(320,480) in main.cpp -#endif // CC_PLATFORM_ANDROID - - bRet = true; - } while (0); - return bRet; -} - bool AppDelegate::applicationDidFinishLaunching() { // initialize director diff --git a/template/xcode4/cocos2dx.xctemplate/Classes/AppDelegate.h b/template/xcode4/cocos2dx.xctemplate/Classes/AppDelegate.h index 4c4e61e966..d02100efd0 100644 --- a/template/xcode4/cocos2dx.xctemplate/Classes/AppDelegate.h +++ b/template/xcode4/cocos2dx.xctemplate/Classes/AppDelegate.h @@ -22,11 +22,6 @@ public: AppDelegate(); virtual ~AppDelegate(); - /** - @brief Implement for initialize OpenGL instance, set source path, etc... - */ - virtual bool initInstance(); - /** @brief Implement CCDirector and CCScene init code here. @return true Initialize success, app continue. diff --git a/template/xcode4/cocos2dx_box2d.xctemplate/Classes/AppDelegate.cpp b/template/xcode4/cocos2dx_box2d.xctemplate/Classes/AppDelegate.cpp index 9e688d9826..339020ed9b 100644 --- a/template/xcode4/cocos2dx_box2d.xctemplate/Classes/AppDelegate.cpp +++ b/template/xcode4/cocos2dx_box2d.xctemplate/Classes/AppDelegate.cpp @@ -22,36 +22,6 @@ AppDelegate::~AppDelegate() { } -bool AppDelegate::initInstance() -{ - bool bRet = false; - do - { -#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) - - // Initialize OpenGLView instance, that release by CCDirector when application terminate. - // The HelloWorld is designed as HVGA. - CCEGLView * pMainWnd = new CCEGLView(); - CC_BREAK_IF(! pMainWnd - || ! pMainWnd->Create(TEXT("cocos2d: Hello World"), 480, 320)); - -#endif // CC_PLATFORM_WIN32 - -#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) - // OpenGLView is initialized in AppDelegate.mm on ios platform, nothing need to do here. -#endif // CC_PLATFORM_IOS - -#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) - // OpenGLView is initialized in HelloWorld/android/jni/helloworld/main.cpp - // the default setting is to create a fullscreen view - // if you want to use auto-scale, please enable view->create(320,480) in main.cpp -#endif // CC_PLATFORM_ANDROID - - bRet = true; - } while (0); - return bRet; -} - bool AppDelegate::applicationDidFinishLaunching() { // initialize director diff --git a/template/xcode4/cocos2dx_box2d.xctemplate/Classes/AppDelegate.h b/template/xcode4/cocos2dx_box2d.xctemplate/Classes/AppDelegate.h index 4c4e61e966..e12494db39 100644 --- a/template/xcode4/cocos2dx_box2d.xctemplate/Classes/AppDelegate.h +++ b/template/xcode4/cocos2dx_box2d.xctemplate/Classes/AppDelegate.h @@ -22,10 +22,6 @@ public: AppDelegate(); virtual ~AppDelegate(); - /** - @brief Implement for initialize OpenGL instance, set source path, etc... - */ - virtual bool initInstance(); /** @brief Implement CCDirector and CCScene init code here. diff --git a/template/xcode4/cocos2dx_chipmunk.xctemplate/Classes/AppDelegate.cpp b/template/xcode4/cocos2dx_chipmunk.xctemplate/Classes/AppDelegate.cpp index 39764a0e5d..339020ed9b 100644 --- a/template/xcode4/cocos2dx_chipmunk.xctemplate/Classes/AppDelegate.cpp +++ b/template/xcode4/cocos2dx_chipmunk.xctemplate/Classes/AppDelegate.cpp @@ -22,36 +22,6 @@ AppDelegate::~AppDelegate() { } -bool AppDelegate::initInstance() -{ - bool bRet = false; - do - { -#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) - - // Initialize OpenGLView instance, that release by CCDirector when application terminate. - // The HelloWorld is designed as HVGA. - CCEGLView * pMainWnd = new CCEGLView(); - CC_BREAK_IF(! pMainWnd - || ! pMainWnd->Create(TEXT("cocos2d: Hello World"), 480, 320)); - -#endif // CC_PLATFORM_WIN32 - -#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) - // OpenGLView is initialized in AppDelegate.mm on ios platform, nothing need to do here. -#endif // CC_PLATFORM_IOS - -#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) - // OpenGLView is initialized in HelloWorld/android/jni/helloworld/main.cpp - // the default setting is to create a fullscreen view - // if you want to use auto-scale, please enable view->create(320,480) in main.cpp -#endif // CC_PLATFORM_ANDROID - - bRet = true; - } while (0); - return bRet; -} - bool AppDelegate::applicationDidFinishLaunching() { // initialize director diff --git a/template/xcode4/cocos2dx_chipmunk.xctemplate/Classes/AppDelegate.h b/template/xcode4/cocos2dx_chipmunk.xctemplate/Classes/AppDelegate.h index 4c4e61e966..d02100efd0 100644 --- a/template/xcode4/cocos2dx_chipmunk.xctemplate/Classes/AppDelegate.h +++ b/template/xcode4/cocos2dx_chipmunk.xctemplate/Classes/AppDelegate.h @@ -22,11 +22,6 @@ public: AppDelegate(); virtual ~AppDelegate(); - /** - @brief Implement for initialize OpenGL instance, set source path, etc... - */ - virtual bool initInstance(); - /** @brief Implement CCDirector and CCScene init code here. @return true Initialize success, app continue. diff --git a/template/xcode4/cocos2dx_lua.xctemplate/Classes/AppDelegate.cpp b/template/xcode4/cocos2dx_lua.xctemplate/Classes/AppDelegate.cpp index e3e1bb345f..e353113b47 100644 --- a/template/xcode4/cocos2dx_lua.xctemplate/Classes/AppDelegate.cpp +++ b/template/xcode4/cocos2dx_lua.xctemplate/Classes/AppDelegate.cpp @@ -35,40 +35,6 @@ AppDelegate::~AppDelegate() CCScriptEngineManager::purgeSharedManager(); } -bool AppDelegate::initInstance() -{ - bool bRet = false; - do - { -#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) - - // Initialize OpenGLView instance, that release by CCDirector when application terminate. - // The HelloWorld is designed as HVGA. - CCEGLView * pMainWnd = new CCEGLView(); - CC_BREAK_IF(! pMainWnd - || ! pMainWnd->Create(TEXT("cocos2d: Hello World"), CC_WIDTH, CC_HEIGHT)); - -#endif // CC_PLATFORM_WIN32 - -#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) - - // OpenGLView initialized in testsAppDelegate.mm on ios platform, nothing need to do here. - -#endif // CC_PLATFORM_IOS - -#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) - - // OpenGLView initialized in HelloWorld/android/jni/helloworld/main.cpp - // the default setting is to create a fullscreen view - // if you want to use auto-scale, please enable view->create(320,480) in main.cpp - -#endif // CC_PLATFORM_ANDROID - - bRet = true; - } while (0); - return bRet; -} - bool AppDelegate::applicationDidFinishLaunching() { // initialize director diff --git a/template/xcode4/cocos2dx_lua.xctemplate/Classes/AppDelegate.h b/template/xcode4/cocos2dx_lua.xctemplate/Classes/AppDelegate.h index 06c18558c4..76c1c6089f 100644 --- a/template/xcode4/cocos2dx_lua.xctemplate/Classes/AppDelegate.h +++ b/template/xcode4/cocos2dx_lua.xctemplate/Classes/AppDelegate.h @@ -14,11 +14,6 @@ public: AppDelegate(); virtual ~AppDelegate(); - /** - @brief Implement for initialize OpenGL instance, set source path, etc... - */ - virtual bool initInstance(); - /** @brief Implement CCDirector and CCScene init code here. @return true Initialize success, app continue. From 136866bc1297e76a2a2828bc1d6fa1c9e12f82d1 Mon Sep 17 00:00:00 2001 From: James Chen Date: Wed, 2 May 2012 18:56:03 +0800 Subject: [PATCH 3/4] issue #1204: Updated tests/proj.win32/main.cpp --- tests/proj.win32/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/proj.win32/main.cpp b/tests/proj.win32/main.cpp index 6737f297f4..e8f14f2568 100644 --- a/tests/proj.win32/main.cpp +++ b/tests/proj.win32/main.cpp @@ -16,7 +16,7 @@ int APIENTRY _tWinMain(HINSTANCE hInstance, AppDelegate app; CCEGLView& eglView = CCEGLView::sharedOpenGLView(); eglView.setViewName("Hello Tests"); - eglView.setFrameSize(800, 480); + eglView.setFrameSize(480, 320); // set the design resolution screen size, if you want to use Design Resoulution scaled to current screen, please uncomment next line. // eglView.setDesignResolutionSize(480, 320); From 82439733c56e385dd25f2eb8bfdd69c1bdfd14b6 Mon Sep 17 00:00:00 2001 From: James Chen Date: Thu, 3 May 2012 10:12:00 +0800 Subject: [PATCH 4/4] fixed #1204: Made CCEGLView::Create(on win32) as a private function. Updated ExtensionsTest. And updated the format of source files(changed linebreak symbol to UNIX format ('\n'),replaced 'tab' with four spaces). --- .../android/jni/SimpleAudioEngineJni.cpp | 640 +-- HelloLua/proj.win32/main.cpp | 56 +- HelloWorld/proj.win32/main.cpp | 2 +- cocos2dx/CCDirector.cpp | 20 +- cocos2dx/CCDirector.h | 8 +- cocos2dx/CCScheduler.cpp | 16 +- .../extensions/CCListView/CCControlDefine.h | 84 +- cocos2dx/extensions/CCListView/CCListView.cpp | 4312 ++++++++--------- cocos2dx/extensions/CCListView/CCListView.h | 377 +- .../extensions/CCListView/CCListViewCell.cpp | 184 +- .../extensions/CCListView/CCListViewCell.h | 108 +- .../CCTextureWatcher/CCTextureWatcher.cpp | 482 +- .../CCTextureWatcher/CCTextureWatcher.h | 106 +- cocos2dx/platform/android/CCCommon.cpp | 2 +- cocos2dx/platform/ios/CCCommon.mm | 2 +- cocos2dx/platform/win32/CCCommon.cpp | 6 +- cocos2dx/platform/win32/CCEGLView.h | 21 +- tests/proj.win32/main.cpp | 2 +- tests/tests/ExtensionsTest/ExtensionsTest.cpp | 23 +- 19 files changed, 3230 insertions(+), 3221 deletions(-) diff --git a/CocosDenshion/android/jni/SimpleAudioEngineJni.cpp b/CocosDenshion/android/jni/SimpleAudioEngineJni.cpp index 634c4e58a1..13891d18a5 100644 --- a/CocosDenshion/android/jni/SimpleAudioEngineJni.cpp +++ b/CocosDenshion/android/jni/SimpleAudioEngineJni.cpp @@ -17,18 +17,18 @@ typedef struct JniMethodInfo_ extern "C" { - // get env and cache it - static JNIEnv* getJNIEnv(void) - { + // get env and cache it + static JNIEnv* getJNIEnv(void) + { - JavaVM* jvm = cocos2d::JniHelper::getJavaVM(); + JavaVM* jvm = cocos2d::JniHelper::getJavaVM(); if (NULL == jvm) { LOGD("Failed to get JNIEnv. JniHelper::getJavaVM() is NULL"); return NULL; } - JNIEnv *env = NULL; - // get jni environment + JNIEnv *env = NULL; + // get jni environment jint ret = jvm->GetEnv((void**)&env, JNI_VERSION_1_4); switch (ret) { @@ -59,33 +59,33 @@ extern "C" LOGD("Failed to get the environment using GetEnv()"); return NULL; } - } + } - // get class and make it a global reference, release it at endJni(). - static jclass getClassID(JNIEnv *pEnv) - { - jclass ret = pEnv->FindClass(CLASS_NAME); - if (! ret) - { - LOGD("Failed to find class of %s", CLASS_NAME); - } - - return ret; - } - - static bool getStaticMethodInfo(JniMethodInfo &methodinfo, const char *methodName, const char *paramCode) + // get class and make it a global reference, release it at endJni(). + static jclass getClassID(JNIEnv *pEnv) { - jmethodID methodID = 0; - JNIEnv *pEnv = 0; - bool bRet = false; + jclass ret = pEnv->FindClass(CLASS_NAME); + if (! ret) + { + LOGD("Failed to find class of %s", CLASS_NAME); + } + + return ret; + } + + static bool getStaticMethodInfo(JniMethodInfo &methodinfo, const char *methodName, const char *paramCode) + { + jmethodID methodID = 0; + JNIEnv *pEnv = 0; + bool bRet = false; do { - pEnv = getJNIEnv(); - if (! pEnv) - { - break; - } + pEnv = getJNIEnv(); + if (! pEnv) + { + break; + } jclass classID = getClassID(pEnv); @@ -96,348 +96,348 @@ extern "C" break; } - methodinfo.classID = classID; - methodinfo.env = pEnv; - methodinfo.methodID = methodID; + methodinfo.classID = classID; + methodinfo.env = pEnv; + methodinfo.methodID = methodID; - bRet = true; + bRet = true; } while (0); return bRet; } - void preloadBackgroundMusicJNI(const char *path) - { - // void playBackgroundMusic(String,boolean) - JniMethodInfo methodInfo; + void preloadBackgroundMusicJNI(const char *path) + { + // void playBackgroundMusic(String,boolean) + JniMethodInfo methodInfo; - if (! getStaticMethodInfo(methodInfo, "preloadBackgroundMusic", "(Ljava/lang/String;)V")) - { - return; - } + if (! getStaticMethodInfo(methodInfo, "preloadBackgroundMusic", "(Ljava/lang/String;)V")) + { + return; + } - jstring stringArg = methodInfo.env->NewStringUTF(path); - methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID, stringArg); - methodInfo.env->DeleteLocalRef(stringArg); - methodInfo.env->DeleteLocalRef(methodInfo.classID); - } - - void playBackgroundMusicJNI(const char *path, bool isLoop) - { - // void playBackgroundMusic(String,boolean) - - JniMethodInfo methodInfo; - - if (! getStaticMethodInfo(methodInfo, "playBackgroundMusic", "(Ljava/lang/String;Z)V")) - { - return; - } - - jstring stringArg = methodInfo.env->NewStringUTF(path); - methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID, stringArg, isLoop); - methodInfo.env->DeleteLocalRef(stringArg); - methodInfo.env->DeleteLocalRef(methodInfo.classID); - } - - void stopBackgroundMusicJNI() - { - // void stopBackgroundMusic() - - JniMethodInfo methodInfo; - - if (! getStaticMethodInfo(methodInfo, "stopBackgroundMusic", "()V")) - { - return; - } - - methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID); - methodInfo.env->DeleteLocalRef(methodInfo.classID); - } - - void pauseBackgroundMusicJNI() - { - // void pauseBackgroundMusic() - - JniMethodInfo methodInfo; - - if (! getStaticMethodInfo(methodInfo, "pauseBackgroundMusic", "()V")) - { - return; - } - - methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID); - methodInfo.env->DeleteLocalRef(methodInfo.classID); - } - - void resumeBackgroundMusicJNI() - { - // void resumeBackgroundMusic() - - JniMethodInfo methodInfo; - - if (! getStaticMethodInfo(methodInfo, "resumeBackgroundMusic", "()V")) - { - return; - } - - methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID); - methodInfo.env->DeleteLocalRef(methodInfo.classID); - } - - void rewindBackgroundMusicJNI() - { - // void rewindBackgroundMusic() - - JniMethodInfo methodInfo; - - if (! getStaticMethodInfo(methodInfo, "rewindBackgroundMusic", "()V")) - { - return; - } - - methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID); - methodInfo.env->DeleteLocalRef(methodInfo.classID); - } - - bool isBackgroundMusicPlayingJNI() - { - // boolean rewindBackgroundMusic() - - JniMethodInfo methodInfo; - jboolean ret = false; - - if (! getStaticMethodInfo(methodInfo, "isBackgroundMusicPlaying", "()Z")) - { - return ret; - } - - ret = methodInfo.env->CallStaticBooleanMethod(methodInfo.classID, methodInfo.methodID); - methodInfo.env->DeleteLocalRef(methodInfo.classID); - - return ret; - } - - float getBackgroundMusicVolumeJNI() - { - // float getBackgroundMusicVolume() - - JniMethodInfo methodInfo; - jfloat ret = -1.0; - - if (! getStaticMethodInfo(methodInfo, "getBackgroundMusicVolume", "()F")) - { - return ret; - } - - ret = methodInfo.env->CallStaticFloatMethod(methodInfo.classID, methodInfo.methodID); - methodInfo.env->DeleteLocalRef(methodInfo.classID); - - return ret; - } - - void setBackgroundMusicVolumeJNI(float volume) - { - // void setBackgroundMusicVolume() - - JniMethodInfo methodInfo; - - if (! getStaticMethodInfo(methodInfo, "setBackgroundMusicVolume", "(F)V")) - { - return ; - } - - methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID, volume); - methodInfo.env->DeleteLocalRef(methodInfo.classID); - } - - unsigned int playEffectJNI(const char* path, bool bLoop) - { - // int playEffect(String) - - JniMethodInfo methodInfo; - int ret = 0; - - if (! getStaticMethodInfo(methodInfo, "playEffect", "(Ljava/lang/String;Z)I")) - { - return ret; - } - - jstring stringArg = methodInfo.env->NewStringUTF(path); - ret = methodInfo.env->CallStaticIntMethod(methodInfo.classID, methodInfo.methodID, stringArg, bLoop); + jstring stringArg = methodInfo.env->NewStringUTF(path); + methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID, stringArg); methodInfo.env->DeleteLocalRef(stringArg); - methodInfo.env->DeleteLocalRef(methodInfo.classID); - - return (unsigned int)ret; - } + methodInfo.env->DeleteLocalRef(methodInfo.classID); + } - void stopEffectJNI(unsigned int nSoundId) - { - // void stopEffect(int) + void playBackgroundMusicJNI(const char *path, bool isLoop) + { + // void playBackgroundMusic(String,boolean) - JniMethodInfo methodInfo; + JniMethodInfo methodInfo; - if (! getStaticMethodInfo(methodInfo, "stopEffect", "(I)V")) - { - return ; - } + if (! getStaticMethodInfo(methodInfo, "playBackgroundMusic", "(Ljava/lang/String;Z)V")) + { + return; + } - methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID, (int)nSoundId); - methodInfo.env->DeleteLocalRef(methodInfo.classID); - } + jstring stringArg = methodInfo.env->NewStringUTF(path); + methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID, stringArg, isLoop); + methodInfo.env->DeleteLocalRef(stringArg); + methodInfo.env->DeleteLocalRef(methodInfo.classID); + } - void endJNI() - { - // void end() + void stopBackgroundMusicJNI() + { + // void stopBackgroundMusic() - JniMethodInfo methodInfo; + JniMethodInfo methodInfo; - if (! getStaticMethodInfo(methodInfo, "end", "()V")) - { - return ; - } + if (! getStaticMethodInfo(methodInfo, "stopBackgroundMusic", "()V")) + { + return; + } - methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID); - methodInfo.env->DeleteLocalRef(methodInfo.classID); - } + methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID); + methodInfo.env->DeleteLocalRef(methodInfo.classID); + } - float getEffectsVolumeJNI() - { - // float getEffectsVolume() + void pauseBackgroundMusicJNI() + { + // void pauseBackgroundMusic() - JniMethodInfo methodInfo; - jfloat ret = -1.0; + JniMethodInfo methodInfo; - if (! getStaticMethodInfo(methodInfo, "getEffectsVolume", "()F")) - { - return ret; - } + if (! getStaticMethodInfo(methodInfo, "pauseBackgroundMusic", "()V")) + { + return; + } - ret = methodInfo.env->CallStaticFloatMethod(methodInfo.classID, methodInfo.methodID); - methodInfo.env->DeleteLocalRef(methodInfo.classID); - - return ret; - } + methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID); + methodInfo.env->DeleteLocalRef(methodInfo.classID); + } - void setEffectsVolumeJNI(float volume) - { - // void setEffectsVolume(float) - JniMethodInfo methodInfo; + void resumeBackgroundMusicJNI() + { + // void resumeBackgroundMusic() - if (! getStaticMethodInfo(methodInfo, "setEffectsVolume", "(F)V")) - { - return ; - } + JniMethodInfo methodInfo; - methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID, volume); - methodInfo.env->DeleteLocalRef(methodInfo.classID); - } + if (! getStaticMethodInfo(methodInfo, "resumeBackgroundMusic", "()V")) + { + return; + } + + methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID); + methodInfo.env->DeleteLocalRef(methodInfo.classID); + } - void preloadEffectJNI(const char *path) - { - // void preloadEffect(String) + void rewindBackgroundMusicJNI() + { + // void rewindBackgroundMusic() - JniMethodInfo methodInfo; + JniMethodInfo methodInfo; - if (! getStaticMethodInfo(methodInfo, "preloadEffect", "(Ljava/lang/String;)V")) - { - return ; - } + if (! getStaticMethodInfo(methodInfo, "rewindBackgroundMusic", "()V")) + { + return; + } - jstring stringArg = methodInfo.env->NewStringUTF(path); - methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID, stringArg); - methodInfo.env->DeleteLocalRef(stringArg); - methodInfo.env->DeleteLocalRef(methodInfo.classID); - } + methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID); + methodInfo.env->DeleteLocalRef(methodInfo.classID); + } - void unloadEffectJNI(const char* path) - { - // void unloadEffect(String) + bool isBackgroundMusicPlayingJNI() + { + // boolean rewindBackgroundMusic() - JniMethodInfo methodInfo; + JniMethodInfo methodInfo; + jboolean ret = false; - if (! getStaticMethodInfo(methodInfo, "unloadEffect", "(Ljava/lang/String;)V")) - { - return ; - } + if (! getStaticMethodInfo(methodInfo, "isBackgroundMusicPlaying", "()Z")) + { + return ret; + } - jstring stringArg = methodInfo.env->NewStringUTF(path); - methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID, stringArg); - methodInfo.env->DeleteLocalRef(stringArg); - methodInfo.env->DeleteLocalRef(methodInfo.classID); - } + ret = methodInfo.env->CallStaticBooleanMethod(methodInfo.classID, methodInfo.methodID); + methodInfo.env->DeleteLocalRef(methodInfo.classID); + + return ret; + } - void pauseEffectJNI(unsigned int nSoundId) - { - // void pauseEffect(int) + float getBackgroundMusicVolumeJNI() + { + // float getBackgroundMusicVolume() - JniMethodInfo methodInfo; + JniMethodInfo methodInfo; + jfloat ret = -1.0; - if (! getStaticMethodInfo(methodInfo, "pauseEffect", "(I)V")) - { - return ; - } + if (! getStaticMethodInfo(methodInfo, "getBackgroundMusicVolume", "()F")) + { + return ret; + } - methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID, (int)nSoundId); - methodInfo.env->DeleteLocalRef(methodInfo.classID); - } + ret = methodInfo.env->CallStaticFloatMethod(methodInfo.classID, methodInfo.methodID); + methodInfo.env->DeleteLocalRef(methodInfo.classID); + + return ret; + } - void pauseAllEffectsJNI() - { - // void pauseAllEffects() + void setBackgroundMusicVolumeJNI(float volume) + { + // void setBackgroundMusicVolume() - JniMethodInfo methodInfo; + JniMethodInfo methodInfo; - if (! getStaticMethodInfo(methodInfo, "pauseAllEffects", "()V")) - { - return ; - } + if (! getStaticMethodInfo(methodInfo, "setBackgroundMusicVolume", "(F)V")) + { + return ; + } - methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID); - methodInfo.env->DeleteLocalRef(methodInfo.classID); - } + methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID, volume); + methodInfo.env->DeleteLocalRef(methodInfo.classID); + } - void resumeEffectJNI(unsigned int nSoundId) - { - // void resumeEffect(int) + unsigned int playEffectJNI(const char* path, bool bLoop) + { + // int playEffect(String) - JniMethodInfo methodInfo; + JniMethodInfo methodInfo; + int ret = 0; - if (! getStaticMethodInfo(methodInfo, "resumeEffect", "(I)V")) - { - return ; - } + if (! getStaticMethodInfo(methodInfo, "playEffect", "(Ljava/lang/String;Z)I")) + { + return ret; + } - methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID, (int)nSoundId); - methodInfo.env->DeleteLocalRef(methodInfo.classID); - } + jstring stringArg = methodInfo.env->NewStringUTF(path); + ret = methodInfo.env->CallStaticIntMethod(methodInfo.classID, methodInfo.methodID, stringArg, bLoop); + methodInfo.env->DeleteLocalRef(stringArg); + methodInfo.env->DeleteLocalRef(methodInfo.classID); + + return (unsigned int)ret; + } - void resumeAllEffectsJNI() - { - // void resumeAllEffects() + void stopEffectJNI(unsigned int nSoundId) + { + // void stopEffect(int) - JniMethodInfo methodInfo; + JniMethodInfo methodInfo; - if (! getStaticMethodInfo(methodInfo, "resumeAllEffects", "()V")) - { - return ; - } + if (! getStaticMethodInfo(methodInfo, "stopEffect", "(I)V")) + { + return ; + } - methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID); - methodInfo.env->DeleteLocalRef(methodInfo.classID); - } + methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID, (int)nSoundId); + methodInfo.env->DeleteLocalRef(methodInfo.classID); + } - void stopAllEffectsJNI() - { - // void stopAllEffects() + void endJNI() + { + // void end() - JniMethodInfo methodInfo; + JniMethodInfo methodInfo; - if (! getStaticMethodInfo(methodInfo, "stopAllEffects", "()V")) - { - return ; - } + if (! getStaticMethodInfo(methodInfo, "end", "()V")) + { + return ; + } - methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID); - methodInfo.env->DeleteLocalRef(methodInfo.classID); - } + methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID); + methodInfo.env->DeleteLocalRef(methodInfo.classID); + } + + float getEffectsVolumeJNI() + { + // float getEffectsVolume() + + JniMethodInfo methodInfo; + jfloat ret = -1.0; + + if (! getStaticMethodInfo(methodInfo, "getEffectsVolume", "()F")) + { + return ret; + } + + ret = methodInfo.env->CallStaticFloatMethod(methodInfo.classID, methodInfo.methodID); + methodInfo.env->DeleteLocalRef(methodInfo.classID); + + return ret; + } + + void setEffectsVolumeJNI(float volume) + { + // void setEffectsVolume(float) + JniMethodInfo methodInfo; + + if (! getStaticMethodInfo(methodInfo, "setEffectsVolume", "(F)V")) + { + return ; + } + + methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID, volume); + methodInfo.env->DeleteLocalRef(methodInfo.classID); + } + + void preloadEffectJNI(const char *path) + { + // void preloadEffect(String) + + JniMethodInfo methodInfo; + + if (! getStaticMethodInfo(methodInfo, "preloadEffect", "(Ljava/lang/String;)V")) + { + return ; + } + + jstring stringArg = methodInfo.env->NewStringUTF(path); + methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID, stringArg); + methodInfo.env->DeleteLocalRef(stringArg); + methodInfo.env->DeleteLocalRef(methodInfo.classID); + } + + void unloadEffectJNI(const char* path) + { + // void unloadEffect(String) + + JniMethodInfo methodInfo; + + if (! getStaticMethodInfo(methodInfo, "unloadEffect", "(Ljava/lang/String;)V")) + { + return ; + } + + jstring stringArg = methodInfo.env->NewStringUTF(path); + methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID, stringArg); + methodInfo.env->DeleteLocalRef(stringArg); + methodInfo.env->DeleteLocalRef(methodInfo.classID); + } + + void pauseEffectJNI(unsigned int nSoundId) + { + // void pauseEffect(int) + + JniMethodInfo methodInfo; + + if (! getStaticMethodInfo(methodInfo, "pauseEffect", "(I)V")) + { + return ; + } + + methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID, (int)nSoundId); + methodInfo.env->DeleteLocalRef(methodInfo.classID); + } + + void pauseAllEffectsJNI() + { + // void pauseAllEffects() + + JniMethodInfo methodInfo; + + if (! getStaticMethodInfo(methodInfo, "pauseAllEffects", "()V")) + { + return ; + } + + methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID); + methodInfo.env->DeleteLocalRef(methodInfo.classID); + } + + void resumeEffectJNI(unsigned int nSoundId) + { + // void resumeEffect(int) + + JniMethodInfo methodInfo; + + if (! getStaticMethodInfo(methodInfo, "resumeEffect", "(I)V")) + { + return ; + } + + methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID, (int)nSoundId); + methodInfo.env->DeleteLocalRef(methodInfo.classID); + } + + void resumeAllEffectsJNI() + { + // void resumeAllEffects() + + JniMethodInfo methodInfo; + + if (! getStaticMethodInfo(methodInfo, "resumeAllEffects", "()V")) + { + return ; + } + + methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID); + methodInfo.env->DeleteLocalRef(methodInfo.classID); + } + + void stopAllEffectsJNI() + { + // void stopAllEffects() + + JniMethodInfo methodInfo; + + if (! getStaticMethodInfo(methodInfo, "stopAllEffects", "()V")) + { + return ; + } + + methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID); + methodInfo.env->DeleteLocalRef(methodInfo.classID); + } } diff --git a/HelloLua/proj.win32/main.cpp b/HelloLua/proj.win32/main.cpp index 6c777b68c7..87ac0ed30a 100644 --- a/HelloLua/proj.win32/main.cpp +++ b/HelloLua/proj.win32/main.cpp @@ -4,37 +4,37 @@ USING_NS_CC; -// uncomment below line, open debug console -#define USE_WIN32_CONSOLE - -int APIENTRY _tWinMain(HINSTANCE hInstance, - HINSTANCE hPrevInstance, - LPTSTR lpCmdLine, - int nCmdShow) -{ - UNREFERENCED_PARAMETER(hPrevInstance); - UNREFERENCED_PARAMETER(lpCmdLine); - -#ifdef USE_WIN32_CONSOLE - AllocConsole(); - freopen("CONIN$", "r", stdin); - freopen("CONOUT$", "w", stdout); - freopen("CONOUT$", "w", stderr); -#endif - +// uncomment below line, open debug console +#define USE_WIN32_CONSOLE + +int APIENTRY _tWinMain(HINSTANCE hInstance, + HINSTANCE hPrevInstance, + LPTSTR lpCmdLine, + int nCmdShow) +{ + UNREFERENCED_PARAMETER(hPrevInstance); + UNREFERENCED_PARAMETER(lpCmdLine); + +#ifdef USE_WIN32_CONSOLE + AllocConsole(); + freopen("CONIN$", "r", stdin); + freopen("CONOUT$", "w", stdout); + freopen("CONOUT$", "w", stderr); +#endif + // create the application instance AppDelegate app; CCEGLView& eglView = CCEGLView::sharedOpenGLView(); eglView.setViewName("Hello Lua"); eglView.setFrameSize(480, 320); - // set the design resolution screen size, if you want to use Design Resoulution scaled to current screen, please uncomment next line. - // eglView.setDesignResolutionSize(480, 320); - - int ret = CCApplication::sharedApplication().run(); - -#ifdef USE_WIN32_CONSOLE - FreeConsole(); -#endif - - return ret; + // set the design resolution screen size, if you want to use Design Resoulution scaled to current screen, please uncomment next line. + // eglView.setDesignResolutionSize(480, 320); + + int ret = CCApplication::sharedApplication().run(); + +#ifdef USE_WIN32_CONSOLE + FreeConsole(); +#endif + + return ret; } diff --git a/HelloWorld/proj.win32/main.cpp b/HelloWorld/proj.win32/main.cpp index 6cbbc54a3d..8019b13ba0 100644 --- a/HelloWorld/proj.win32/main.cpp +++ b/HelloWorld/proj.win32/main.cpp @@ -17,7 +17,7 @@ int APIENTRY _tWinMain(HINSTANCE hInstance, CCEGLView& eglView = CCEGLView::sharedOpenGLView(); eglView.setViewName("Hello World"); eglView.setFrameSize(480, 320); - // set the design resolution screen size, if you want to use Design Resoulution scaled to current screen, please uncomment next line. + // set the design resolution screen size, if you want to use Design Resoulution scaled to current screen, please uncomment next line. // eglView.setDesignResolutionSize(480, 320); return CCApplication::sharedApplication().run(); } diff --git a/cocos2dx/CCDirector.cpp b/cocos2dx/CCDirector.cpp index b867fd8270..5646e41aa0 100644 --- a/cocos2dx/CCDirector.cpp +++ b/cocos2dx/CCDirector.cpp @@ -131,8 +131,8 @@ bool CCDirector::init(void) m_fContentScaleFactor = 1; m_bIsContentScaleSupported = false; - m_pWatcherFun = NULL; - m_pWatcherSender = NULL; + m_pWatcherFun = NULL; + m_pWatcherSender = NULL; // scheduler m_pScheduler = new CCScheduler(); @@ -235,9 +235,9 @@ void CCDirector::drawScene(void) showStats(); } - if (m_pWatcherFun && m_pWatcherSender) - { - (*m_pWatcherFun)(m_pWatcherSender); + if (m_pWatcherFun && m_pWatcherSender) + { + (*m_pWatcherFun)(m_pWatcherSender); } kmGLPopMatrix(); @@ -941,11 +941,11 @@ void CCDisplayLinkDirector::setAnimationInterval(double dValue) } } -void CCDirector::setWatcherCallbackFun(void *pSender, WatcherCallbackFun fun) -{ - m_pWatcherFun = fun; - m_pWatcherSender = pSender; -} +void CCDirector::setWatcherCallbackFun(void *pSender, WatcherCallbackFun fun) +{ + m_pWatcherFun = fun; + m_pWatcherSender = pSender; +} NS_CC_END diff --git a/cocos2dx/CCDirector.h b/cocos2dx/CCDirector.h index be8ea21778..b0a6fce352 100644 --- a/cocos2dx/CCDirector.h +++ b/cocos2dx/CCDirector.h @@ -273,7 +273,7 @@ public: void setContentScaleFactor(CCFloat scaleFactor); CCFloat getContentScaleFactor(void); - typedef void(*WatcherCallbackFun)(void *pSender); + typedef void(*WatcherCallbackFun)(void *pSender); void setWatcherCallbackFun(void *pSender, WatcherCallbackFun fun); public: @@ -391,9 +391,9 @@ protected: /* contentScaleFactor could be simulated */ bool m_bIsContentScaleSupported; - - WatcherCallbackFun m_pWatcherFun; - void *m_pWatcherSender; + + WatcherCallbackFun m_pWatcherFun; + void *m_pWatcherSender; }; diff --git a/cocos2dx/CCScheduler.cpp b/cocos2dx/CCScheduler.cpp index 0290d6584f..a7bf3fb721 100644 --- a/cocos2dx/CCScheduler.cpp +++ b/cocos2dx/CCScheduler.cpp @@ -179,10 +179,10 @@ void CCTimer::update(ccTime dt) { (m_pTarget->*m_pfnSelector)(m_fElapsed); } - if (m_nScriptHandler) - { - CCScriptEngineManager::sharedManager()->getScriptEngine()->executeSchedule(m_nScriptHandler, m_fElapsed); - } + if (m_nScriptHandler) + { + CCScriptEngineManager::sharedManager()->getScriptEngine()->executeSchedule(m_nScriptHandler, m_fElapsed); + } m_fElapsed = m_fElapsed - m_fDelay; m_nTimesExecuted+=1; m_bUseDelay = false; @@ -196,10 +196,10 @@ void CCTimer::update(ccTime dt) { (m_pTarget->*m_pfnSelector)(m_fElapsed); } - if (m_nScriptHandler) - { - CCScriptEngineManager::sharedManager()->getScriptEngine()->executeSchedule(m_nScriptHandler, m_fElapsed); - } + if (m_nScriptHandler) + { + CCScriptEngineManager::sharedManager()->getScriptEngine()->executeSchedule(m_nScriptHandler, m_fElapsed); + } m_fElapsed = 0; m_nTimesExecuted += 1; diff --git a/cocos2dx/extensions/CCListView/CCControlDefine.h b/cocos2dx/extensions/CCListView/CCControlDefine.h index c6ff2de4a2..30bdaabce0 100644 --- a/cocos2dx/extensions/CCListView/CCControlDefine.h +++ b/cocos2dx/extensions/CCListView/CCControlDefine.h @@ -1,42 +1,42 @@ -#ifndef __CCCONTROL_DEFINE_H__ -#define __CCCONTROL_DEFINE_H__ - -NS_CC_EXT_BEGIN - -typedef enum -{ - PARENT_CENTER, - VERTICAL_TOP, - VERTICAL_BOTTOM, - HORIZONTAL_LEFT, - HORIZONTAL_RIGHT, - ABS_WITH_PIXEL, - ABS_WITH_PERCENT, - REF_PREV_X_INC, - REF_PREV_X_DEC, - REF_PREV_Y_INC, - REF_PREV_Y_DEC, - REL_FLOW -} LAYOUT_TYPE; - -typedef struct -{ - LAYOUT_TYPE t; - union - { - float pixel_val; - float percent_val; - } val; -} LayoutParamVal; - -typedef struct -{ - LayoutParamVal val_x; - LayoutParamVal val_y; - float padding; - bool wrap; -} LayoutParam; - -NS_CC_EXT_END - -#endif /* __CCCONTROL_DEFINE_H__ */ +#ifndef __CCCONTROL_DEFINE_H__ +#define __CCCONTROL_DEFINE_H__ + +NS_CC_EXT_BEGIN + +typedef enum +{ + PARENT_CENTER, + VERTICAL_TOP, + VERTICAL_BOTTOM, + HORIZONTAL_LEFT, + HORIZONTAL_RIGHT, + ABS_WITH_PIXEL, + ABS_WITH_PERCENT, + REF_PREV_X_INC, + REF_PREV_X_DEC, + REF_PREV_Y_INC, + REF_PREV_Y_DEC, + REL_FLOW +} LAYOUT_TYPE; + +typedef struct +{ + LAYOUT_TYPE t; + union + { + float pixel_val; + float percent_val; + } val; +} LayoutParamVal; + +typedef struct +{ + LayoutParamVal val_x; + LayoutParamVal val_y; + float padding; + bool wrap; +} LayoutParam; + +NS_CC_EXT_END + +#endif /* __CCCONTROL_DEFINE_H__ */ diff --git a/cocos2dx/extensions/CCListView/CCListView.cpp b/cocos2dx/extensions/CCListView/CCListView.cpp index 21f6b21ce7..7ca1b6db6c 100644 --- a/cocos2dx/extensions/CCListView/CCListView.cpp +++ b/cocos2dx/extensions/CCListView/CCListView.cpp @@ -1,2156 +1,2156 @@ -#include "CCListView.h" -//#include "../cocos2dx_support/LuaEngineImpl.h" -#include "cocos2d.h" - -using namespace std; - -NS_CC_EXT_BEGIN - -#define ND_LISTVIEW_ACTION_INTERVAL 0.6666 -/****************************************** -**************Public Functions************* -*******************************************/ -CCListView* CCListView::viewWithMode(CCListViewMode mode) -{ - CCListView *pRet = new CCListView(); - if (pRet && pRet->initWithMode(mode)) - { - pRet->autorelease(); - return pRet; - } - else - { - CC_SAFE_DELETE(pRet); - return NULL; - } -} - -bool CCListView::initWithMode(CCListViewMode mode) -{ - bool bRet = false; - m_nMode = mode; - m_layerPanel = CCLayer::node(); - this->addChild(m_layerPanel); - - bRet = CCLayerColor::initWithColor(ccc4(255, 255, 255, 0), 0, 0); - setIsTouchEnabled(true); - return bRet; -} - -CCListView::CCListView(void) - :m_nMode(CCListViewModeVertical) - ,m_nState(CCListViewStateWatting) - ,m_nSlideDir(CCListViewSlideDirNone) - ,m_layerPanel(NULL) - ,m_nSeparatorStyle(CCListViewCellSeparatorStyleSingleLine) - ,m_nSelectedRow(-1) - ,m_nCurrentRow(-1) - ,m_fActionDuration(ND_LISTVIEW_ACTION_INTERVAL) - ,m_pListViewParent(NULL) - ,m_bIsEnabled(true) - ,m_pDelegate(NULL) - ,m_nNumberOfRows(0) - ,m_bIsOnTouch(false) -{ - m_drawedRows = CCRangeMake(0, 0); - m_visibleRows = CCRangeMake(0, 0); - m_bIsTouchEnabled = true; -} - -CCListView::~CCListView(void) -{ -} - -void CCListView::setDelegateName(const char* pszName) -{ - if (pszName) - { - this->m_strDeletegate = string(pszName) + "."; - } - else - { - this->m_strDeletegate.clear(); - } -} - -void CCListView::selectCellAtRow(unsigned int nRow) -{ - CCListViewCell *cell = cellAtRow(nRow); - if (cell) - { - cell->selected(); - } -} - -void CCListView::unselectCellAtRow(unsigned int nRow) -{ - if (nRow == m_nSelectedRow) - { - m_nSelectedRow = -1; - } - CCListViewCell *cell = cellAtRow(nRow); - if (cell) - { - cell->unselected(); - } -} - -void CCListView::reload(void) -{ - m_layerPanel->removeAllChildrenWithCleanup(true); - m_layerPanel->setPosition(CCPointZero); - m_visibleRows = CCRangeMake(0, 0); - m_drawedRows = CCRangeMake(0, 0); - m_nNumberOfRows = triggerNumberOfCells(); - this->displayVisibleRows(); -} - -void CCListView::insertCellsAtRow(unsigned int nRow, unsigned int nCount) -{ - if (nRow >= m_nNumberOfRows) - { - if (m_nNumberOfRows > 0) - { - nRow = m_nNumberOfRows - 1; - } - else - { - nRow = 0; - } - } - - m_layerPanel->pauseSchedulerAndActions(); - if (m_nNumberOfRows == 0) - { - m_nNumberOfRows = this->triggerNumberOfCells(); - this->displayVisibleRows(); - } - else - { - m_nNumberOfRows = this->triggerNumberOfCells(); - - if (!this->isFullFill()) - { - this->displayVisibleRows(); - } - } - m_layerPanel->resumeSchedulerAndActions(); -} - -void CCListView::deleteCellsAtRow(unsigned int nRow, unsigned int nCount) -{ - if (m_nNumberOfRows == 0) - { - return; - } - if (nRow >= m_nNumberOfRows) - { - if (m_nNumberOfRows > 0) - { - nRow = m_nNumberOfRows - 1; - } - else - { - nRow = 0; - } - } - - m_layerPanel->pauseSchedulerAndActions(); - - - m_nNumberOfRows = this->triggerNumberOfCells(); - - for (unsigned int n = nRow;n < nRow + nCount; n++) - { - if (n >= m_drawedRows.location && n <= CCRange::CCMaxRange(m_drawedRows)) - { - CCListViewCell *cell = this->cellAtRow(n); - if (cell) - { - CCPoint pos = cell->getPosition(); - pos.y += cell->getContentSize().height; - m_layerPanel->removeChild(cell, true); - for (unsigned int i = n +1; i <= CCRange::CCMaxRange(m_drawedRows); i++) - { - cell = this->cellAtRow(i); - if (cell) - { - int tag = cell->getTag(); - cell->setTag(tag - 1); - if (CCListViewModeHorizontal == m_nMode) - { - cell->setPosition(pos); - pos.x += cell->getContentSize().width; - } - else if (CCListViewModeVertical == m_nMode) - { - pos.y -= cell->getContentSize().height; - cell->setPosition(pos); - } - } - } - if (m_drawedRows.length > 0) - { - m_drawedRows.length--; - unsigned int nLastRow = CCRange::CCMaxRange(m_drawedRows); - this->appendRowToBack(nLastRow + 1); - } - } - } - else - { - for (unsigned int i = m_drawedRows.location; i <= CCRange::CCMaxRange(m_drawedRows); i++) - { - CCListViewCell *cell = this->cellAtRow(i); - if (cell) - { - int tag = cell->getTag(); - cell->setTag(tag - 1); - } - } - if (m_drawedRows.location > 0) - { - m_drawedRows.location--; - } - } - } - - m_layerPanel->resumeSchedulerAndActions(); -} - -void CCListView::scrollCellToFront(unsigned int nRow, bool bAnimated) -{ - if (!isFullFill()) - { - return; - } - if (m_nNumberOfRows == 0) - { - return; - } - if (CCListViewStateWatting != m_nState) - { - this->stopActionImmediately(); - } - - if (nRow >= m_nNumberOfRows) - { - nRow = m_nNumberOfRows - 1; - } - - float disX = 0; - float disY = 0; - m_nSlideDir = CCListViewSlideDirNone; - if (CCListViewModeHorizontal == m_nMode) - { - float dis = 0; - unsigned int nCount = 0; - CCListViewCell *cell = NULL; - if (nRow > m_visibleRows.location) - { - m_nSlideDir = CCListViewSlideDirLeft; - } - else - { - m_nSlideDir = CCListViewSlideDirRight; - } - - while(1) - { - if (dis >= this->getContentSize().width || nRow + nCount >= m_nNumberOfRows) - { - break; - } - - if (CCListViewSlideDirRight == m_nSlideDir) - { - cell = appendRowToFront(nRow + nCount); - } - else if (CCListViewSlideDirLeft == m_nSlideDir) - { - cell = appendRowToBack(nRow + nCount); - } - - if (cell) - { - nCount++; - dis += cell->getContentSize().width; - } - else - { - CCLog("CCListView cell == NULL at line %d", __LINE__); - } - } - - if (CCListViewSlideDirLeft == m_nSlideDir && dis < this->getContentSize().width) - { - // at last - while(1) - { - if (dis >= this->getContentSize().width) - { - break; - } - cell = appendRowToBack(nRow - 1); - if (cell) - { - nRow--; - dis += cell->getContentSize().width; - nCount++; - } - else - { - CCLog("CCListView cell == NULL at line %d", __LINE__); - } - } - } - - if (CCListViewSlideDirRight == m_nSlideDir) - { - CCListViewCell* cell = NULL; - unsigned nLast = 0; - if (CCRange::CCLocationInRange(nRow + nCount - 1, m_visibleRows)) - { - cell = cellAtRow(nRow + nCount - 1); - nLast = nRow + nCount - 2; - } - else - { - cell = cellAtRow(m_visibleRows.location); - nLast = nRow + nCount - 1; - } - if (cell) - { - CCPoint pos = cell->getPosition(); - for (int i = nLast; i >= (int)nRow; i--) - { - cell = cellAtRow(i); - if (cell) - { - pos.x -= cell->getContentSize().width; - cell->setPosition(pos); - } - else - { - CCLog("CCListView cell == NULL at line %d", __LINE__); - } - } - } - else - { - CCLog("CCListView cell == NULL at line %d", __LINE__); - } - } - else if (CCListViewSlideDirLeft == m_nSlideDir) - { - CCListViewCell* cell = NULL; - unsigned nFirst = 0; - if (CCRange::CCLocationInRange(nRow, m_visibleRows)) - { - cell = cellAtRow(nRow); - nFirst = nRow + 1; - } - else - { - cell = cellAtRow(CCRange::CCMaxRange(m_visibleRows)); - nFirst = nRow; - } - if (cell) - { - CCPoint pos = cell->getPosition(); - pos.x += cell->getContentSize().width; - for (unsigned int i = nFirst; i < nRow + nCount; i++) - { - cell = cellAtRow(i); - if (cell) - { - cell->setPosition(pos); - pos.x += cell->getContentSize().width; - - } - else - { - CCLog("CCListView cell == NULL at line %d", __LINE__); - } - } - } - else - { - CCLog("CCListView cell == NULL at line %d", __LINE__); - } - } - - - cell = cellAtRow(nRow); - CCPoint ptCell = cell->convertToWorldSpace(CCPointZero); - CCPoint ptView = this->convertToWorldSpace(CCPointZero); - disX = ptView.x - ptCell.x; - } - else if (CCListViewModeVertical == m_nMode) - { - float dis = 0; - unsigned int nCount = 0; - CCListViewCell *cell = NULL; - if (nRow > m_visibleRows.location) - { - m_nSlideDir = CCListViewSlideDirUp; - } - else - { - m_nSlideDir = CCListViewSlideDirDown; - } - - while(1) - { - if (dis >= this->getContentSize().height || nRow + nCount >= m_nNumberOfRows) - { - break; - } - - if (CCListViewSlideDirDown == m_nSlideDir) - { - cell = appendRowToFront(nRow + nCount); - } - else if (CCListViewSlideDirUp == m_nSlideDir) - { - cell = appendRowToBack(nRow + nCount); - } - - if (cell) - { - nCount++; - dis += cell->getContentSize().height; - } - else - { - CCLog("CCListView cell == NULL at line %d", __LINE__); - } - } - - if (CCListViewSlideDirUp == m_nSlideDir && dis < this->getContentSize().height) - { - // at last - while(1) - { - if (dis >= this->getContentSize().height) - { - break; - } - cell = appendRowToBack(nRow - 1); - if (cell) - { - nRow--; - dis += cell->getContentSize().height; - nCount++; - } - else - { - CCLog("CCListView cell == NULL at line %d", __LINE__); - } - } - } - - if (CCListViewSlideDirDown == m_nSlideDir) - { - CCListViewCell* cell = NULL; - unsigned nLast = 0; - if (CCRange::CCLocationInRange(nRow + nCount - 1, m_visibleRows)) - { - cell = cellAtRow(nRow + nCount - 1); - nLast = nRow + nCount - 2; - } - else - { - cell = cellAtRow(m_visibleRows.location); - nLast = nRow + nCount - 1; - } - if (cell) - { - CCPoint pos = cell->getPosition(); - pos.y += cell->getContentSize().height; - for (int i = nLast; i >= (int)nRow; i--) - { - cell = cellAtRow(i); - if (cell) - { - cell->setPosition(pos); - pos.y += cell->getContentSize().height; - } - else - { - CCLog("CCListView cell == NULL at line %d", __LINE__); - } - } - } - else - { - CCLog("CCListView cell == NULL at line %d", __LINE__); - } - } - else if (CCListViewSlideDirUp == m_nSlideDir) - { - CCListViewCell* cell = NULL; - unsigned nFirst = 0; - if (CCRange::CCLocationInRange(nRow, m_visibleRows)) - { - cell = cellAtRow(nRow); - nFirst = nRow + 1; - } - else - { - cell = cellAtRow(CCRange::CCMaxRange(m_visibleRows)); - nFirst = nRow; - } - if (cell) - { - CCPoint pos = cell->getPosition(); - for (unsigned int i = nFirst; i < nRow + nCount; i++) - { - cell = cellAtRow(i); - if (cell) - { - pos.y -= cell->getContentSize().height; - cell->setPosition(pos); - } - else - { - CCLog("CCListView cell == NULL at line %d", __LINE__); - } - } - } - else - { - CCLog("CCListView cell == NULL at line %d", __LINE__); - } - } - - - cell = cellAtRow(nRow); - CCPoint ptCell = cell->convertToWorldSpace(CCPointZero); - CCPoint ptView = this->convertToWorldSpace(CCPointZero); - disY = ptView.y + this->getContentSize().height - (ptCell.y + cell->getContentSize().height); - } - - m_ptDestination = m_layerPanel->getPosition(); - m_ptDestination.x += disX; - m_ptDestination.y += disY; - m_nState = CCListViewStateScroll; - - if (bAnimated) - { - CCMoveBy *moveBy = CCMoveBy::actionWithDuration(m_fActionDuration, CCPointMake(disX, disY)); - CCEaseOut *ease = CCEaseOut::actionWithAction(moveBy, 3); - CCFiniteTimeAction *actions = CCSequence::actions(ease, CCCallFunc::actionWithTarget(this, callfunc_selector(CCListView::finishScroll)), NULL); - m_layerPanel->runAction(actions); - } - else - { - stopActionImmediately(); - } -} - -void CCListView::scrollCellToBack(unsigned int nRow, bool bAnimated) -{ - if (!isFullFill()) - { - return; - } - if (m_nNumberOfRows == 0) - { - return; - } - if (CCListViewStateWatting != m_nState) - { - this->stopActionImmediately(); - } - - if (nRow >= m_nNumberOfRows) - { - nRow = m_nNumberOfRows - 1; - } - - float disX = 0; - float disY = 0; - m_nSlideDir = CCListViewSlideDirNone; - if (CCListViewModeHorizontal == m_nMode) - { - float dis = 0; - int nCount = 0; - CCListViewCell *cell = NULL; - if (nRow > CCRange::CCMaxRange(m_visibleRows)) - { - m_nSlideDir = CCListViewSlideDirLeft; - } - else - { - m_nSlideDir = CCListViewSlideDirRight; - } - - while(1) - { - if (dis >= this->getContentSize().width || (int)nRow - nCount < 0) - { - break; - } - - if (CCListViewSlideDirRight == m_nSlideDir) - { - cell = appendRowToFront(nRow - nCount); - } - else if (CCListViewSlideDirLeft == m_nSlideDir) - { - cell = appendRowToBack(nRow - nCount); - } - - if (cell) - { - nCount++; - dis += cell->getContentSize().width; - } - else - { - CCLog("CCListView cell == NULL at line %d", __LINE__); - } - } - - if (CCListViewSlideDirRight == m_nSlideDir && dis < this->getContentSize().width) - { - // at first - while(1) - { - if (dis >= this->getContentSize().width) - { - break; - } - cell = appendRowToBack(nRow + 1); - if (cell) - { - nRow++; - dis += cell->getContentSize().width; - nCount++; - } - else - { - CCLog("CCListView cell == NULL at line %d", __LINE__); - } - } - } - - if (CCListViewSlideDirRight == m_nSlideDir) - { - CCListViewCell* cell = NULL; - unsigned nLast = 0; - if (CCRange::CCLocationInRange(nRow, m_visibleRows)) - { - cell = cellAtRow(nRow); - nLast = nRow - 1; - } - else - { - cell = cellAtRow(m_visibleRows.location); - nLast = nRow; - } - if (cell) - { - CCPoint pos = cell->getPosition(); - for (int i = nLast; i >= (int)(nRow - nCount + 1); i--) - { - cell = cellAtRow(i); - if (cell) - { - pos.x -= cell->getContentSize().width; - cell->setPosition(pos); - } - else - { - CCLog("CCListView cell == NULL at line %d", __LINE__); - } - } - } - else - { - CCLog("CCListView cell == NULL at line %d", __LINE__); - } - } - else if (CCListViewSlideDirLeft == m_nSlideDir) - { - CCListViewCell* cell = NULL; - unsigned nFirst = 0; - if (CCRange::CCLocationInRange(nRow - nCount + 1, m_visibleRows)) - { - cell = cellAtRow(nRow - nCount + 1); - nFirst = nRow - nCount + 2; - nCount--; - } - else - { - cell = cellAtRow(CCRange::CCMaxRange(m_visibleRows)); - nFirst = nRow - nCount + 1; - } - if (cell) - { - CCPoint pos = cell->getPosition(); - pos.x += cell->getContentSize().width; - for (unsigned int i = nFirst; i < nFirst + nCount; i++) - { - cell = cellAtRow(i); - if (cell) - { - cell->setPosition(pos); - pos.x += cell->getContentSize().width; - - } - else - { - CCLog("CCListView cell == NULL at line %d", __LINE__); - } - } - } - else - { - CCLog("CCListView cell == NULL at line %d", __LINE__); - } - } - - - cell = cellAtRow(nRow); - CCPoint ptCell = cell->convertToWorldSpace(CCPointZero); - CCPoint ptView = this->convertToWorldSpace(CCPointZero); - disX = ptView.x + this->getContentSize().width - (ptCell.x + cell->getContentSize().width); - } - else if (CCListViewModeVertical == m_nMode) - { - float dis = 0; - int nCount = 0; - CCListViewCell *cell = NULL; - if (nRow > CCRange::CCMaxRange(m_visibleRows)) - { - m_nSlideDir = CCListViewSlideDirUp; - } - else - { - m_nSlideDir = CCListViewSlideDirDown; - } - - while(1) - { - if (dis >= this->getContentSize().height || (int)nRow - nCount < 0) - { - break; - } - - if (CCListViewSlideDirDown == m_nSlideDir) - { - cell = appendRowToFront(nRow - nCount); - } - else if (CCListViewSlideDirUp == m_nSlideDir) - { - cell = appendRowToBack(nRow - nCount); - } - - if (cell) - { - nCount++; - dis += cell->getContentSize().height; - } - else - { - CCLog("CCListView cell == NULL at line %d", __LINE__); - } - } - - if (CCListViewSlideDirDown == m_nSlideDir && dis < this->getContentSize().height) - { - // at first - while(1) - { - if (dis >= this->getContentSize().height) - { - break; - } - cell = appendRowToBack(nRow + 1); - if (cell) - { - nRow++; - dis += cell->getContentSize().height; - nCount++; - } - else - { - CCLog("CCListView cell == NULL at line %d", __LINE__); - } - } - } - - if (CCListViewSlideDirDown == m_nSlideDir) - { - CCListViewCell* cell = NULL; - unsigned nLast = 0; - if (CCRange::CCLocationInRange(nRow, m_visibleRows)) - { - cell = cellAtRow(nRow); - nLast = nRow - 1; - } - else - { - cell = cellAtRow(m_visibleRows.location); - nLast = nRow; - } - if (cell) - { - CCPoint pos = cell->getPosition(); - pos.y += cell->getContentSize().height; - for (int i = nLast; i >= (int)(nRow - nCount + 1); i--) - { - cell = cellAtRow(i); - if (cell) - { - cell->setPosition(pos); - pos.y += cell->getContentSize().height; - } - else - { - CCLog("CCListView cell == NULL at line %d", __LINE__); - } - } - } - else - { - CCLog("CCListView cell == NULL at line %d", __LINE__); - } - } - else if (CCListViewSlideDirUp == m_nSlideDir) - { - CCListViewCell* cell = NULL; - unsigned nFirst = 0; - if (CCRange::CCLocationInRange(nRow - nCount + 1, m_visibleRows)) - { - cell = cellAtRow(nRow - nCount + 1); - nFirst = nRow - nCount + 2; - nCount--; - } - else - { - cell = cellAtRow(CCRange::CCMaxRange(m_visibleRows)); - nFirst = nRow - nCount + 1; - } - if (cell) - { - CCPoint pos = cell->getPosition(); - for (unsigned int i = nFirst; i < nFirst + nCount; i++) - { - cell = cellAtRow(i); - if (cell) - { - pos.y -= cell->getContentSize().height; - cell->setPosition(pos); - } - else - { - CCLog("CCListView cell == NULL at line %d", __LINE__); - } - } - } - else - { - CCLog("CCListView cell == NULL at line %d", __LINE__); - } - } - - - cell = cellAtRow(nRow); - CCPoint ptCell = cell->convertToWorldSpace(CCPointZero); - CCPoint ptView = this->convertToWorldSpace(CCPointZero); - disY = ptView.y - ptCell.y; - } - - m_ptDestination = m_layerPanel->getPosition(); - m_ptDestination.x += disX; - m_ptDestination.y += disY; - m_nState = CCListViewStateScroll; - - if (bAnimated) - { - CCMoveBy *moveBy = CCMoveBy::actionWithDuration(m_fActionDuration, CCPointMake(disX, disY)); - CCEaseOut *ease = CCEaseOut::actionWithAction(moveBy, 3); - CCFiniteTimeAction *actions = CCSequence::actions(ease, CCCallFunc::actionWithTarget(this, callfunc_selector(CCListView::finishScroll)), NULL); - m_layerPanel->runAction(actions); - } - else - { - stopActionImmediately(); - } -} - -CCListViewSlideDir CCListView::getSlideDir(CCPoint ptTouchBegan, CCPoint ptTouchEnd) -{ - CCListViewSlideDir nSlideDir = CCListViewSlideDirNone; - - int nOffsetX = ptTouchEnd.x - ptTouchBegan.x; - int nOffsetY = ptTouchEnd.y - ptTouchBegan.y; - - int disMin = CCDirector::sharedDirector()->getWinSize().height / 100; - - if(CCListViewModeHorizontal == m_nMode) - { - if(nOffsetX >= disMin) - { - nSlideDir = CCListViewSlideDirRight; - } - else if (nOffsetX <= -disMin) - { - nSlideDir = CCListViewSlideDirLeft; - } - } - else if (CCListViewModeVertical == m_nMode) - { - if(nOffsetY >= disMin) - { - nSlideDir = CCListViewSlideDirUp; - } - else if (nOffsetY <= -disMin) - { - nSlideDir = CCListViewSlideDirDown; - } - } - return nSlideDir; -} - -/****************************************** -**************Private Functions************ -*******************************************/ -int CCListView::rowForTouch(cocos2d::CCTouch *touch) -{ - CCPoint touchLocation = touch->locationInView(); - touchLocation = CCDirector::sharedDirector()->convertToGL(touchLocation); - - CCArray *pChildren = m_layerPanel->getChildren(); - if (pChildren && pChildren->count() > 0) - { - CCObject* pObject = NULL; - CCARRAY_FOREACH(pChildren, pObject) - { - CCNode* pChild = (CCNode*) pObject; - if (pChild && pChild->getIsVisible()) - { - CCPoint local = pChild->convertToNodeSpace(touchLocation); - CCRect r = CCRectZero; - r.size = pChild->getContentSize(); - - if (CCRect::CCRectContainsPoint(r, local)) - { - return pChild->getTag(); - } - } - } - - } - - return -1; -} - -void CCListView::finishFix(void) -{ - if(m_pListViewParent) - { - m_pListViewParent->setIsEnabled(true); - } - m_nState = CCListViewStateWatting; - m_nSlideDir = CCListViewSlideDirNone; - clearUnvisibleRows(); - triggerDidScrollToRow(m_visibleRows.location); - - CCArray *children = m_layerPanel->getChildren(); - int nCount = 0; - if (children) - { - nCount = children->count(); - } - //CCLog("row num left:%d [%d, %d]", nCount, m_drawedRows.location, CCRange::CCMaxRange(m_drawedRows)); -} - -void CCListView::finishScroll(void) -{ - finishFix(); -} - -void CCListView::finishEaseOut(void) -{ - bool bNeedFix = false; - - if (CCListViewModeHorizontal == m_nMode) - { - bool bFullFill = isFullFill(); - if (CCListViewSlideDirLeft == m_nSlideDir && bFullFill) - { - CCListViewCell *cell = cellAtRow(m_nNumberOfRows - 1); - if (cell) - { - CCPoint ptCell = cell->convertToWorldSpace(CCPointZero); - CCPoint ptView = this->convertToWorldSpace(CCPointZero); - if (ptCell.x + cell->getContentSize().width < ptView.x + this->getContentSize().width) - { - bNeedFix = true; - fixLastRow(); - } - } - } - else - { - CCListViewCell *cell = cellAtRow(0); - if (cell) - { - CCPoint ptCell = cell->convertToWorldSpace(CCPointZero); - CCPoint ptView = this->convertToWorldSpace(CCPointZero); - if (ptCell.x > ptView.x || !bFullFill) - { - bNeedFix = true; - fixFirstRow(); - } - } - } - } - else if (CCListViewModeVertical == m_nMode) - { - bool bFullFill = this->isFullFill(); - if (CCListViewSlideDirUp == m_nSlideDir && bFullFill) - { - CCListViewCell *cell = cellAtRow(m_nNumberOfRows - 1); - if (cell) - { - CCPoint ptCell = cell->convertToWorldSpace(CCPointZero); - CCPoint ptView = this->convertToWorldSpace(CCPointZero); - if (ptCell.y > ptView.y) - { - bNeedFix = true; - fixLastRow(); - } - } - } - else - { - CCListViewCell *cell = cellAtRow(0); - if (cell) - { - CCPoint ptCell = cell->convertToWorldSpace(CCPointZero); - CCPoint ptView = this->convertToWorldSpace(CCPointZero); - if (ptCell.y + cell->getContentSize().height < ptView.y + this->getContentSize().height || !bFullFill) - { - bNeedFix = true; - fixFirstRow(); - } - } - } - } - - if (!bNeedFix) - { - finishFix(); - } -} - -bool CCListView::isTouchInside(CCTouch *touch) -{ - CCPoint point; - if (m_pListViewParent) - { - point = m_pListViewParent->convertTouchToNodeSpace(touch); - } - else - { - point = this->convertTouchToNodeSpace(touch); - } - CCRect bounds = CCRectMake(0, 0, this->getContentSize().width, this->getContentSize().height); - bool bIn = CCRect::CCRectContainsPoint(bounds, point); - return bIn; -} - -bool CCListView::isFullFill(void) -{ - bool bRet = false; - float length = 0; - for (unsigned int i = m_drawedRows.location; i <= CCRange::CCMaxRange(m_drawedRows); i++) - { - CCListViewCell *cell = cellAtRow(i); - if (cell) - { - if (CCListViewModeHorizontal == m_nMode) - { - length += cell->getContentSize().width; - if (length >= this->getContentSize().width) - { - bRet = true; - break; - } - } - else if (CCListViewModeVertical == m_nMode) - { - length += cell->getContentSize().height; - if (length >= this->getContentSize().height) - { - bRet = true; - break; - } - } - } - } - return bRet; -} - -CCListViewCell *CCListView::cellAtRow(unsigned int nRow) -{ - CCListViewCell *cell = (CCListViewCell*)m_layerPanel->getChildByTag(nRow); - return cell; -} - -void CCListView::stopActionImmediately(void) -{ - m_layerPanel->stopAllActions(); - if (CCListViewStateScroll == m_nState) - { - m_layerPanel->setPosition(m_ptDestination); - finishScroll(); - } -} - -unsigned int CCListView::triggerNumberOfCells(void) -{ - unsigned int nRow = 0; - CCListViewProtrolData data; - - if (m_strDeletegate.size() > 0) - { - CCScriptEngineProtocol* scriptEngine = CCScriptEngineManager::sharedManager()->getScriptEngine(); - if (scriptEngine) - { - std::string script; - script = m_strDeletegate + "CCListView_numberOfCells"; -//TODO: scriptEngine->executeCallFuncNTDT(script.c_str(), this, "NdCxControl::CCListView", &data, "NdCxControl::CCListViewProtrolData"); - nRow = data.nNumberOfRows; - } - } - - if (m_pDelegate) - { - m_pDelegate->CCListView_numberOfCells(this, &data); - nRow = data.nNumberOfRows; - } - return nRow; -} - -CCListViewCell *CCListView::triggerCellForRow(unsigned int nRow) -{ - CCListViewCell *cell = NULL; - CCListViewProtrolData data; - data.nRow = nRow; - data.cell = NULL; - if (m_strDeletegate.size() > 0) - { - CCScriptEngineProtocol* scriptEngine = CCScriptEngineManager::sharedManager()->getScriptEngine(); - if (scriptEngine) - { - std::string script; - script = m_strDeletegate + "CCListView_cellForRow"; -//TODO: scriptEngine->executeCallFuncNTDT(script.c_str(), this, "NdCxControl::CCListView", &data, "NdCxControl::CCListViewProtrolData"); - cell = data.cell; - } - } - if (m_pDelegate) - { - m_pDelegate->CCListView_cellForRow(this, &data); - cell = data.cell; - } - return cell; -} - -void CCListView::triggerDidClickCellAtRow(unsigned int nRow) -{ - CCListViewProtrolData data; - data.nRow = nRow; - if (m_strDeletegate.size() > 0) - { - CCScriptEngineProtocol* scriptEngine = CCScriptEngineManager::sharedManager()->getScriptEngine(); - if (scriptEngine) - { - std::string script; - script = m_strDeletegate + "CCListView_didClickCellAtRow"; -//TODO: scriptEngine->executeCallFuncNTDT(script.c_str(), this, "NdCxControl::CCListView", &data, "NdCxControl::CCListViewProtrolData"); - } - } - - if (m_pDelegate) - { - m_pDelegate->CCListView_didClickCellAtRow(this, &data); - } -} - -void CCListView::triggerDidScrollToRow(unsigned int nRow) -{ - CCListViewProtrolData data; - data.nRow = nRow; - if (m_strDeletegate.size() > 0) - { - CCScriptEngineProtocol* scriptEngine = CCScriptEngineManager::sharedManager()->getScriptEngine(); - if (scriptEngine) - { - std::string script; - script = m_strDeletegate + "CCListView_didScrollToRow"; -//TODO: scriptEngine->executeCallFuncNTDT(script.c_str(), this, "NdCxControl::CCListView", &data, "NdCxControl::CCListViewProtrolData"); - } - } - if (m_pDelegate) - { - m_pDelegate->CCListView_didScrollToRow(this, &data); - } -} - -void CCListView::displayVisibleRows(void) -{ - CCSize size = getContentSize(); - float posPannel, boundPannel; - unsigned int rowCount = m_drawedRows.location; - - CCListViewCell *cell = cellAtRow(rowCount); - - if (m_nMode == CCListViewModeHorizontal) - { - if (cell) - { - posPannel = cell->getPosition().x; - } - else - { - posPannel = m_layerPanel->getPosition().x; - } - boundPannel = posPannel + size.width; - } - else if (m_nMode == CCListViewModeVertical) - { - if (cell) - { - posPannel = cell->getPosition().y + cell->getContentSize().height; - } - else - { - posPannel = size.height - m_layerPanel->getPosition().y; - } - boundPannel = posPannel - size.height; - } - - CCListViewCell *lastCell = NULL; - while(1) - { - // row condition - if (rowCount >= m_nNumberOfRows) - break; - - // size condition - if (m_nMode == CCListViewModeHorizontal) - { - if (posPannel >= boundPannel) - break; - } - else if (m_nMode == CCListViewModeVertical) - { - if (posPannel <= boundPannel) - break; - } - - // get cell - CCListViewCell *cell = cellAtRow(rowCount); - - if (NULL == cell) - { - cell = triggerCellForRow(rowCount); - if (cell) - { - CCSize cellSize = cell->getContentSize(); - if (m_nMode == CCListViewModeHorizontal) - { - cell->setPosition(CCPointMake(posPannel, 0)); - cell->setContentSize(CCSizeMake(cellSize.width, size.height)); - } - else if (m_nMode == CCListViewModeVertical) - { - cell->setPosition(CCPointMake(0, posPannel - cellSize.height)); - cell->setContentSize(CCSizeMake(size.width, cellSize.height)); - } - m_layerPanel->addChild(cell, rowCount, rowCount); - } - } - - if (cell) - { - if (m_nMode == CCListViewModeHorizontal) - { - posPannel += cell->getContentSize().width; - } - else if (m_nMode == CCListViewModeVertical) - { - posPannel -= cell->getContentSize().height; - } - cell->setSeparatorStyle(m_nSeparatorStyle); - lastCell = cell; - } - - ++rowCount; - } - m_drawedRows.length = rowCount - m_drawedRows.location; - // 设置可视范围 - m_visibleRows = m_drawedRows; - - if (lastCell) - { - lastCell->setSeparatorStyle(CCListViewCellSeparatorStyleNone); - } -} - -CCListViewCell* CCListView::appendRowToBack(unsigned int nRow) -{ - if (nRow >= m_nNumberOfRows) - { - return NULL; - } - CCListViewCell *cell = cellAtRow(nRow); - if (cell == NULL) - { - cell = triggerCellForRow(nRow); - if (cell) - { - CCSize size = this->getContentSize(); - CCSize cellSize = cell->getContentSize(); - float pos; - unsigned int nLastRow = CCRange::CCMaxRange(m_drawedRows); - CCListViewCell *lastCell = cellAtRow(nLastRow); - if (lastCell) - { - if (m_nMode == CCListViewModeHorizontal) - { - pos = lastCell->getPosition().x + lastCell->getContentSize().width; - cell->setPosition(CCPointMake(pos, 0)); - cell->setContentSize(CCSizeMake(cellSize.width, size.height)); - } - else if (m_nMode == CCListViewModeVertical) - { - pos = lastCell->getPosition().y - cell->getContentSize().height; - cell->setPosition(CCPointMake(0, pos)); - cell->setContentSize(CCSizeMake(size.width, cellSize.height)); - } - - if (nRow == m_nSelectedRow) - { - cell->selected(); - } - m_layerPanel->addChild(cell, nRow, nRow); - if (nRow > CCRange::CCMaxRange(m_drawedRows)) - { - cell->setSeparatorStyle(CCListViewCellSeparatorStyleNone); - lastCell->setSeparatorStyle(m_nSeparatorStyle); - m_drawedRows.length += nRow - CCRange::CCMaxRange(m_drawedRows); - } - else - { - cell->setSeparatorStyle(m_nSeparatorStyle); - } - } - else - { - CCLog("CCListView cell == NULL at line %d", __LINE__); - } - } - else - { - CCLog("CCListView cell == NULL at line %d", __LINE__); - } - } - return cell; -} - -CCListViewCell* CCListView::appendRowToFront(unsigned int nRow) -{ - CCListViewCell *cell = cellAtRow(nRow); - if (cell == NULL) - { - cell = triggerCellForRow(nRow); - if (cell) - { - CCSize size = this->getContentSize(); - CCSize cellSize = cell->getContentSize(); - float pos; - unsigned int nFirstRow = m_drawedRows.location; - CCListViewCell *firstCell = cellAtRow(nFirstRow); - if (firstCell) - { - if (m_nMode == CCListViewModeHorizontal) - { - pos = firstCell->getPosition().x - cell->getContentSize().width; - cell->setPosition(CCPointMake(pos, 0)); - cell->setContentSize(CCSizeMake(cellSize.width, size.height)); - } - else if (m_nMode == CCListViewModeVertical) - { - pos = firstCell->getPosition().y + firstCell->getContentSize().height; - cell->setPosition(CCPointMake(0, pos)); - cell->setContentSize(CCSizeMake(size.width, cellSize.height)); - } - cell->setSeparatorStyle(m_nSeparatorStyle); - if (nRow == m_nSelectedRow) - { - cell->selected(); - } - m_layerPanel->addChild(cell, nRow, nRow); - if (nRow < m_drawedRows.location) - { - m_drawedRows.length += m_drawedRows.location - nRow; - m_drawedRows.location = nRow; - } - } - else - { - CCLog("CCListView cell == NULL at line %d", __LINE__); - } - } - else - { - CCLog("CCListView cell == NULL at line %d", __LINE__); - } - } - return cell; -} -// 对齐末行 -void CCListView::fixFirstRow(void) -{ - unsigned int location = m_drawedRows.location; - - CCListViewCell *cell = cellAtRow(location); - if (cell) - { - float disX = 0; - float disY = 0; - CCPoint posCell = cell->convertToWorldSpace(CCPointZero); - CCPoint posView = this->convertToWorldSpace(CCPointZero); - if (CCListViewModeHorizontal == m_nMode) - { - float dis = posCell.x - posView.x; - dis = -dis; - - disX = dis; - disY = 0; - } - else if (CCListViewModeVertical == m_nMode) - { - float dis = posCell.y + cell->getContentSize().height - (posView.y + this->getContentSize().height); - dis = -dis; - - disX = 0; - disY = dis; - } - - m_nState = CCListViewStateFix; - CCMoveBy *moveBy = CCMoveBy::actionWithDuration(m_fActionDuration, CCPointMake(disX, disY)); - CCEaseInOut *ease = CCEaseInOut::actionWithAction(moveBy, 2); - CCFiniteTimeAction *actions = CCSequence::actions(ease, CCCallFunc::actionWithTarget(this, callfunc_selector(CCListView::finishFix)), NULL); - m_layerPanel->runAction(actions); - } - else - { - this->finishFix(); - } -} -// 对齐首行 -void CCListView::fixLastRow(void) -{ - unsigned int nLastRow = CCRange::CCMaxRange(m_drawedRows); - CCListViewCell *cell = cellAtRow(nLastRow); - if (cell) - { - float disX = 0; - float disY = 0; - CCPoint posCell = cell->convertToWorldSpace(CCPointZero); - CCPoint posView = this->convertToWorldSpace(CCPointZero); - if (CCListViewModeHorizontal == m_nMode) - { - float dis = posCell.x + cell->getContentSize().width - (posView.x + this->getContentSize().width); - dis = -dis; - - disX = dis; - disY = 0; - } - else if (CCListViewModeVertical == m_nMode) - { - float dis = posCell.y - posView.y; - dis = -dis; - - disX = 0; - disY = dis; - } - - m_nState = CCListViewStateFix; - CCMoveBy *moveBy = CCMoveBy::actionWithDuration(m_fActionDuration, CCPointMake(disX, disY)); - CCEaseInOut *ease = CCEaseInOut::actionWithAction(moveBy, 2); - CCFiniteTimeAction *actions = CCSequence::actions(ease, CCCallFunc::actionWithTarget(this, callfunc_selector(CCListView::finishFix)), NULL); - m_layerPanel->runAction(actions); - } - else - { - finishFix(); - } -} -// 手势滑动界面,减速效果 -void CCListView::easeOutWithDistance(float dis) -{ - float disX = 0; - float disY = 0; - - if (CCListViewModeHorizontal == m_nMode) - { - if (CCListViewSlideDirLeft == m_nSlideDir) - { - // drag Left - while(1) - { - unsigned int nLastRow = CCRange::CCMaxRange(m_drawedRows); - CCListViewCell *cell = cellAtRow(nLastRow); - if (cell) - { - CCPoint ptCell = cell->convertToWorldSpace(CCPointZero); - CCPoint ptView = this->convertToWorldSpace(CCPointZero); - if (nLastRow < m_nNumberOfRows - 1) - { - float offset = ptView.x + this->getContentSize().width - (ptCell.x + cell->getContentSize().width); - if (offset > dis) - { - appendRowToBack(nLastRow + 1); - } - else - { - dis = offset; - break; - } - } - else - { - // already is the last row - float leftMin = cell->getContentSize().width / 5; - float offset = ptCell.x + cell->getContentSize().width + dis - ptView.x; - if (offset < -leftMin) - { - dis = ptView.x + leftMin - (ptCell.x + cell->getContentSize().width); - } - offset = ptCell.x + cell->getContentSize().width + dis - (ptView.x + this->getContentSize().width) ; - if (offset > 0) - { - dis = ptView.x + this->getContentSize().width - (ptCell.x + cell->getContentSize().width); - } - break; - } - } - else - { - break; - } - } - } - else - { - // drag right - while(1) - { - unsigned int nFirstRow = m_drawedRows.location; - CCListViewCell *cell = cellAtRow(nFirstRow); - if (cell) - { - CCPoint ptCell = cell->convertToWorldSpace(CCPointZero); - CCPoint ptView = this->convertToWorldSpace(CCPointZero); - if (nFirstRow > 0) - { - float offset = ptView.x - ptCell.x; - if (offset < dis) - { - appendRowToFront(nFirstRow - 1); - } - else - { - dis = offset; - break; - } - } - else - { - // already is the first row - float leftMin = cell->getContentSize().width / 5; - float offset = ptCell.x + dis - (ptView.x + this->getContentSize().width); - if (offset > leftMin) - { - dis = ptView.x + this->getContentSize().width - leftMin - ptCell.x; - } - offset = ptView.x - ptCell.x; - if (offset > 0) - { - dis = offset; - } - break; - } - } - else - { - - } - } - } - disX = dis; - disY = 0; - } - else if (CCListViewModeVertical == m_nMode) - { - if (CCListViewSlideDirUp == m_nSlideDir) - { - // drag up - while(1) - { - unsigned int nLastRow = CCRange::CCMaxRange(m_drawedRows); - CCListViewCell *cell = cellAtRow(nLastRow); - if (cell) - { - CCPoint ptCell = cell->convertToWorldSpace(CCPointZero); - CCPoint ptView = this->convertToWorldSpace(CCPointZero); - if (nLastRow < m_nNumberOfRows - 1) - { - float offset = ptView.y - ptCell.y; - if (offset < dis) - { - appendRowToBack(nLastRow + 1); - } - else - { - dis = offset; - break; - } - } - else - { - // already is the last row - m_fActionDuration *= 0.5; - float leftMin = cell->getContentSize().height / 5; - float offset = ptCell.y + dis - (ptView.y + this->getContentSize().height); - if (offset > -leftMin) - { - dis = ptView.y + this->getContentSize().height - leftMin - ptCell.y; - } - break; - } - } - else - { - break; - } - } - } - else - { - // drag down - while(1) - { - unsigned int nFirstRow = m_drawedRows.location; - CCListViewCell *cell = cellAtRow(nFirstRow); - if (cell) - { - CCPoint ptCell = cell->convertToWorldSpace(CCPointZero); - CCPoint ptView = this->convertToWorldSpace(CCPointZero); - if (nFirstRow > 0) - { - float offset = ptView.y + this->getContentSize().height - (ptCell.y + cell->getContentSize().height); - if (offset > dis) - { - appendRowToFront(nFirstRow - 1); - } - else - { - dis = offset; - break; - } - } - else - { - // already is the first row - m_fActionDuration *= 0.5; - float leftMin = cell->getContentSize().height / 5; - float offset = ptCell.y + cell->getContentSize().height + dis - ptView.y; - if (offset < leftMin) - { - dis = ptView.y + leftMin - (ptCell.y + cell->getContentSize().height); - } - break; - } - } - else - { - - } - } - } - disX = 0; - disY = dis; - } - - m_nState = CCListViewStateEaseOut; - CCMoveBy *moveBy = CCMoveBy::actionWithDuration(m_fActionDuration, CCPointMake(disX, disY)); - CCEaseOut *ease = CCEaseOut::actionWithAction(moveBy, 3); - CCFiniteTimeAction *actions = CCSequence::actions(ease, CCCallFunc::actionWithTarget(this, callfunc_selector(CCListView::finishEaseOut)), NULL); - m_layerPanel->runAction(actions); -} - -void CCListView::clearUnvisibleRows(void) -{ - CCRange oldRange = m_drawedRows; - for (unsigned int i = oldRange.location; i < oldRange.location + oldRange.length; i++) - { - CCListViewCell *cell = cellAtRow(i); - if (cell) - { - CCPoint posCell = cell->convertToWorldSpace(CCPointZero); - CCPoint posView = this->convertToWorldSpace(CCPointZero); - if (CCListViewModeHorizontal == m_nMode) - { - if (posCell.x + cell->getContentSize().width <= posView.x) - { - m_layerPanel->removeChild(cell, true); - } - else - { - break; - } - } - else if (CCListViewModeVertical == m_nMode) - { - if (posCell.y >= posView.y + this->getContentSize().height) - { - m_layerPanel->removeChild(cell, true); - } - else - { - break; - } - } - } - m_drawedRows.location++; - m_drawedRows.length--; - } - - oldRange = m_drawedRows; - for (int i = oldRange.location + oldRange.length - 1; i >= (int)oldRange.location; i--) - { - CCListViewCell *cell = cellAtRow(i); - if (cell) - { - CCPoint posCell = cell->convertToWorldSpace(CCPointZero); - CCPoint posView = this->convertToWorldSpace(CCPointZero); - if (CCListViewModeHorizontal == m_nMode) - { - if (posCell.x >= posView.x + this->getContentSize().width) - { - m_layerPanel->removeChild(cell, true); - } - else - { - break; - } - } - else if (CCListViewModeVertical == m_nMode) - { - if (posCell.y + cell->getContentSize().height <= posView.y) - { - m_layerPanel->removeChild(cell, true); - } - else - { - break; - } - } - } - m_drawedRows.length--; - } - - m_visibleRows = m_drawedRows; -} -/****************************************** -**************Virturl Functions************ -*******************************************/ -void CCListView::registerWithTouchDispatcher() -{ - CCDirector* pDirector = CCDirector::sharedDirector(); - if (m_pListViewParent) - { - pDirector->getTouchDispatcher()->addTargetedDelegate(this, kCCMenuHandlerPriority - 1, false); - } - else - { - pDirector->getTouchDispatcher()->addTargetedDelegate(this, kCCMenuHandlerPriority - 2, false); - } -} - -void CCListView::onEnter(void) -{ - if (0 == m_nNumberOfRows) - { - m_layerPanel->setPosition(CCPointZero); - m_layerPanel->setContentSize(this->getContentSize()); - // get number of rows - m_nNumberOfRows = triggerNumberOfCells(); - displayVisibleRows(); - } - - CCLayerColor::onEnter(); -} - -void CCListView::onExit(void) -{ - CCLayerColor::onExit(); -} - -void CCListView::visit(void) -{ - if (!m_pListViewParent) - { - CCRect rectSelf; - float factor = CC_CONTENT_SCALE_FACTOR(); - rectSelf.origin = convertToWorldSpace(CCPoint(0,0)); - rectSelf.origin.x *= factor; - rectSelf.origin.y *= factor; - rectSelf.size = this->getContentSize(); - rectSelf.size.width *= factor; - rectSelf.size.height *= factor; - glScissor((GLsizei)rectSelf.origin.x, (GLsizei)rectSelf.origin.y, (GLsizei)rectSelf.size.width , (GLsizei)rectSelf.size.height); - glEnable(GL_SCISSOR_TEST); - } - CCLayerColor::visit(); - if (!m_pListViewParent) - { - glDisable(GL_SCISSOR_TEST); - } -} - -bool CCListView::ccTouchBegan(CCTouch* touch, CCEvent* event) -{ - CC_UNUSED_PARAM(event); - - if (!isTouchInside(touch) || !getIsVisible() || !m_bIsEnabled) - { - return false; - } - - if (m_pListViewParent && CCListViewSlideDirNone != m_pListViewParent->getSlideDir()) - { - return false; - } - - CCArray *children = this->getChildren(); - if (!m_bIsVisible || !children || children->count() == 0) - { - return false; - } - - if (m_bIsOnTouch) - { - return false; - } - - CCPoint touchPoint = touch->locationInView(); - - m_ptTouchBegan = m_ptTouchEnd = CCDirector::sharedDirector()->convertToGL(touchPoint); - m_ptPanelOffset = m_layerPanel->getPosition(); - - m_timeTouchBegan = clock(); - - m_nCurrentRow = this->rowForTouch(touch); - if (m_nCurrentRow != -1) - { - if (CCListViewStateWatting != m_nState) - { - stopActionImmediately(); - } - - m_nState = CCListViewStateTrackingTouch; - if (CCListViewSlideDirNone == m_nSlideDir) - { - this->selectCellAtRow(m_nCurrentRow); - } - else - { - m_nCurrentRow = -1; - } - m_bIsOnTouch = true; - return true; - } - - - return false; -} - -void CCListView::ccTouchMoved(CCTouch* touch, CCEvent* event) -{ - CC_UNUSED_PARAM(event); - if (CCListViewStateTrackingTouch != m_nState || !isTouchInside(touch) || !m_bIsEnabled) - { - return; - } - - CCPoint touchPoint = touch->locationInView(); - m_ptTouchEnd = CCDirector::sharedDirector()->convertToGL(touchPoint); - - - CCListViewSlideDir nsliderDir = CCListViewSlideDirNone; - if (m_pListViewParent && CCListViewSlideDirNone != m_pListViewParent->getSlideDir(m_ptTouchBegan, m_ptTouchEnd)) - { - return; - } - else - { - nsliderDir = getSlideDir(m_ptTouchBegan, m_ptTouchEnd); - } - - - if(CCListViewModeHorizontal == m_nMode && CCListViewSlideDirNone != nsliderDir) - { - m_nSlideDir = nsliderDir; - m_layerPanel->setPosition(CCPointMake(m_ptPanelOffset.x + (m_ptTouchEnd.x - m_ptTouchBegan.x),m_ptPanelOffset.y)); - if (CCListViewSlideDirLeft == m_nSlideDir) - { - // drag left - unsigned int nLastRow = CCRange::CCMaxRange(m_drawedRows); - if (nLastRow < m_nNumberOfRows - 1) - { - CCListViewCell *cell = cellAtRow(nLastRow); - if (cell) - { - CCPoint posCell = cell->convertToWorldSpace(CCPointZero); - CCPoint posView = this->convertToWorldSpace(CCPointZero); - if (posCell.x + cell->getContentSize().width <= posView.x + this->getContentSize().width) - { - appendRowToBack(nLastRow + 1); - } - } - } - } - else - { - // drag right - unsigned int nFirstRow = m_drawedRows.location; - if (nFirstRow > 0) - { - CCListViewCell *cell = cellAtRow(nFirstRow); - CCPoint posCell = cell->convertToWorldSpace(CCPointZero); - CCPoint posView = this->convertToWorldSpace(CCPointZero); - if (posCell.x >= posView.x) - { - appendRowToFront(nFirstRow - 1); - } - } - } - } - else if (CCListViewModeVertical == m_nMode && CCListViewSlideDirNone != nsliderDir) - { - m_nSlideDir = nsliderDir; - m_layerPanel->setPosition(CCPointMake(m_ptPanelOffset.x, m_ptPanelOffset.y + (m_ptTouchEnd.y - m_ptTouchBegan.y))); - if (CCListViewSlideDirUp == m_nSlideDir) - { - // drag up - unsigned int nLastRow = CCRange::CCMaxRange(m_drawedRows); - if (nLastRow < m_nNumberOfRows - 1) - { - CCListViewCell *cell = cellAtRow(nLastRow); - CCPoint posCell = cell->convertToWorldSpace(CCPointZero); - CCPoint posView = this->convertToWorldSpace(CCPointZero); - if (posCell.y >= posView.y) - { - appendRowToBack(nLastRow + 1); - } - } - } - else - { - // drag down - unsigned int nFirstRow = m_drawedRows.location; - if (nFirstRow > 0) - { - CCListViewCell *cell = cellAtRow(nFirstRow); - CCPoint posCell = cell->convertToWorldSpace(CCPointZero); - CCPoint posView = this->convertToWorldSpace(CCPointZero); - if (posCell.y + cell->getContentSize().height <= posView.y + this->getContentSize().height) - { - appendRowToFront(nFirstRow - 1); - } - } - } - } - - if (CCListViewSlideDirNone != m_nSlideDir && m_nCurrentRow != -1 && m_nCurrentRow != m_nSelectedRow) - { - this->unselectCellAtRow(m_nCurrentRow); - } - - if (CCListViewSlideDirNone != m_nSlideDir && m_pListViewParent) - { - m_pListViewParent->setIsEnabled(false); - } -} - -void CCListView::ccTouchEnded(CCTouch* touch, CCEvent* event) -{ - CC_UNUSED_PARAM(touch); - CC_UNUSED_PARAM(event); - - if(m_nState != CCListViewStateTrackingTouch || !m_bIsEnabled) - { - m_bIsOnTouch = false; - return; - } - - m_fActionDuration = ND_LISTVIEW_ACTION_INTERVAL; - clock_t timeElapse = clock() - m_timeTouchBegan; -#if CC_TARGET_PLATFORM != CC_PLATFORM_WIN32 && CC_TARGET_PLATFORM != CC_PLATFORM_MAC - // 手机平台用时较长,此系数可能需要根据不同平台做相应调整 - timeElapse /= 200; -#endif - if(CCListViewSlideDirLeft == m_nSlideDir || CCListViewSlideDirRight == m_nSlideDir) - { - float dis = m_ptTouchEnd.x - m_ptTouchBegan.x; - float speed = dis / timeElapse; - if (fabs(speed) > 0.1 && timeElapse < 300) - { - easeOutWithDistance(dis * 3); - } - else - { - if (CCListViewSlideDirLeft == m_nSlideDir && isFullFill()) - { - // drag up - fixLastRow(); - } - else - { - // drag down - fixFirstRow(); - } - } - } - else if (CCListViewSlideDirUp == m_nSlideDir || CCListViewSlideDirDown == m_nSlideDir) - { - float dis = m_ptTouchEnd.y - m_ptTouchBegan.y; - float speed = dis / timeElapse; - if (fabs(speed) > 0.1 && timeElapse < 300) - { - easeOutWithDistance(dis * 3); - } - else - { - if (CCListViewSlideDirUp == m_nSlideDir && isFullFill()) - { - // drag up - fixLastRow(); - } - else - { - // drag down - fixFirstRow(); - } - } - } - else - { - finishFix(); - } - - int currentRow = this->rowForTouch(touch); - if (currentRow != -1 && isTouchInside(touch)) - { - if (currentRow == m_nCurrentRow) - { - if (CCListViewSlideDirNone == m_nSlideDir) - { - if (m_pListViewParent == NULL || (m_pListViewParent && CCListViewSlideDirNone == m_pListViewParent->getSlideDir())) - { - if (m_nSelectedRow != -1 && m_nSelectedRow != m_nCurrentRow) - { - this->unselectCellAtRow(m_nSelectedRow); - } - m_nSelectedRow = m_nCurrentRow; - - if (!this->isMenuTouch(touch, this->cellAtRow(m_nSelectedRow))) - { - triggerDidClickCellAtRow(m_nSelectedRow); - } - } - } - else - { - if (m_nSelectedRow != m_nCurrentRow) - { - this->unselectCellAtRow(m_nCurrentRow); - } - } - } - else - { - if (-1 != m_nCurrentRow) - { - this->unselectCellAtRow(m_nCurrentRow); - } - } - } - else - { - if (-1 != m_nCurrentRow) - { - this->unselectCellAtRow(m_nCurrentRow); - m_nCurrentRow = -1; - } - } - m_bIsOnTouch = false; -} - -bool CCListView::isMenuTouch(CCTouch *touch, CCNode *parent) -{ - if (dynamic_cast(parent)) - { - CCPoint touchPoint = touch->locationInView(); - touchPoint.y = cocos2d::CCDirector::sharedDirector()->getWinSize().height - touchPoint.y; - touchPoint = parent->convertToNodeSpace(touchPoint); - CCRect rect = CCRectZero; - rect.size = parent->getContentSize(); - return CCRect::CCRectContainsPoint(rect, touchPoint); - } - else - { - CCArray *pChildrens = parent->getChildren(); - if (pChildrens && pChildrens->count() > 0) - { - for (int i = 0; i < pChildrens->count(); i++) - { - CCNode *pChildren = (CCNode*)pChildrens->objectAtIndex(i); - if (this->isMenuTouch(touch, pChildren)) - { - return true; - } - } - } - } - return false; -} - -void CCListView::ccTouchCancelled(CCTouch *touch, CCEvent *event) -{ - CC_UNUSED_PARAM(touch); - CC_UNUSED_PARAM(event); - - CCAssert(m_nState == CCListViewStateTrackingTouch, "[NdListview ccTouchCancelled] -- invalid state"); - - finishFix(); - m_bIsOnTouch = false; -} - -NS_CC_EXT_END - - - - +#include "CCListView.h" +//#include "../cocos2dx_support/LuaEngineImpl.h" +#include "cocos2d.h" + +using namespace std; + +NS_CC_EXT_BEGIN + +#define ND_LISTVIEW_ACTION_INTERVAL 0.6666 +/****************************************** +**************Public Functions************* +*******************************************/ +CCListView* CCListView::viewWithMode(CCListViewMode mode) +{ + CCListView *pRet = new CCListView(); + if (pRet && pRet->initWithMode(mode)) + { + pRet->autorelease(); + return pRet; + } + else + { + CC_SAFE_DELETE(pRet); + return NULL; + } +} + +bool CCListView::initWithMode(CCListViewMode mode) +{ + bool bRet = false; + m_nMode = mode; + m_layerPanel = CCLayer::node(); + this->addChild(m_layerPanel); + + bRet = CCLayerColor::initWithColor(ccc4(255, 255, 255, 0), 0, 0); + setIsTouchEnabled(true); + return bRet; +} + +CCListView::CCListView(void) + :m_nMode(CCListViewModeVertical) + ,m_nState(CCListViewStateWatting) + ,m_nSlideDir(CCListViewSlideDirNone) + ,m_layerPanel(NULL) + ,m_nSeparatorStyle(CCListViewCellSeparatorStyleSingleLine) + ,m_nSelectedRow(-1) + ,m_nCurrentRow(-1) + ,m_fActionDuration(ND_LISTVIEW_ACTION_INTERVAL) + ,m_pListViewParent(NULL) + ,m_bIsEnabled(true) + ,m_pDelegate(NULL) + ,m_nNumberOfRows(0) + ,m_bIsOnTouch(false) +{ + m_drawedRows = CCRangeMake(0, 0); + m_visibleRows = CCRangeMake(0, 0); + m_bIsTouchEnabled = true; +} + +CCListView::~CCListView(void) +{ +} + +void CCListView::setDelegateName(const char* pszName) +{ + if (pszName) + { + this->m_strDeletegate = string(pszName) + "."; + } + else + { + this->m_strDeletegate.clear(); + } +} + +void CCListView::selectCellAtRow(unsigned int nRow) +{ + CCListViewCell *cell = cellAtRow(nRow); + if (cell) + { + cell->selected(); + } +} + +void CCListView::unselectCellAtRow(unsigned int nRow) +{ + if (nRow == m_nSelectedRow) + { + m_nSelectedRow = -1; + } + CCListViewCell *cell = cellAtRow(nRow); + if (cell) + { + cell->unselected(); + } +} + +void CCListView::reload(void) +{ + m_layerPanel->removeAllChildrenWithCleanup(true); + m_layerPanel->setPosition(CCPointZero); + m_visibleRows = CCRangeMake(0, 0); + m_drawedRows = CCRangeMake(0, 0); + m_nNumberOfRows = triggerNumberOfCells(); + this->displayVisibleRows(); +} + +void CCListView::insertCellsAtRow(unsigned int nRow, unsigned int nCount) +{ + if (nRow >= m_nNumberOfRows) + { + if (m_nNumberOfRows > 0) + { + nRow = m_nNumberOfRows - 1; + } + else + { + nRow = 0; + } + } + + m_layerPanel->pauseSchedulerAndActions(); + if (m_nNumberOfRows == 0) + { + m_nNumberOfRows = this->triggerNumberOfCells(); + this->displayVisibleRows(); + } + else + { + m_nNumberOfRows = this->triggerNumberOfCells(); + + if (!this->isFullFill()) + { + this->displayVisibleRows(); + } + } + m_layerPanel->resumeSchedulerAndActions(); +} + +void CCListView::deleteCellsAtRow(unsigned int nRow, unsigned int nCount) +{ + if (m_nNumberOfRows == 0) + { + return; + } + if (nRow >= m_nNumberOfRows) + { + if (m_nNumberOfRows > 0) + { + nRow = m_nNumberOfRows - 1; + } + else + { + nRow = 0; + } + } + + m_layerPanel->pauseSchedulerAndActions(); + + + m_nNumberOfRows = this->triggerNumberOfCells(); + + for (unsigned int n = nRow;n < nRow + nCount; n++) + { + if (n >= m_drawedRows.location && n <= CCRange::CCMaxRange(m_drawedRows)) + { + CCListViewCell *cell = this->cellAtRow(n); + if (cell) + { + CCPoint pos = cell->getPosition(); + pos.y += cell->getContentSize().height; + m_layerPanel->removeChild(cell, true); + for (unsigned int i = n +1; i <= CCRange::CCMaxRange(m_drawedRows); i++) + { + cell = this->cellAtRow(i); + if (cell) + { + int tag = cell->getTag(); + cell->setTag(tag - 1); + if (CCListViewModeHorizontal == m_nMode) + { + cell->setPosition(pos); + pos.x += cell->getContentSize().width; + } + else if (CCListViewModeVertical == m_nMode) + { + pos.y -= cell->getContentSize().height; + cell->setPosition(pos); + } + } + } + if (m_drawedRows.length > 0) + { + m_drawedRows.length--; + unsigned int nLastRow = CCRange::CCMaxRange(m_drawedRows); + this->appendRowToBack(nLastRow + 1); + } + } + } + else + { + for (unsigned int i = m_drawedRows.location; i <= CCRange::CCMaxRange(m_drawedRows); i++) + { + CCListViewCell *cell = this->cellAtRow(i); + if (cell) + { + int tag = cell->getTag(); + cell->setTag(tag - 1); + } + } + if (m_drawedRows.location > 0) + { + m_drawedRows.location--; + } + } + } + + m_layerPanel->resumeSchedulerAndActions(); +} + +void CCListView::scrollCellToFront(unsigned int nRow, bool bAnimated) +{ + if (!isFullFill()) + { + return; + } + if (m_nNumberOfRows == 0) + { + return; + } + if (CCListViewStateWatting != m_nState) + { + this->stopActionImmediately(); + } + + if (nRow >= m_nNumberOfRows) + { + nRow = m_nNumberOfRows - 1; + } + + float disX = 0; + float disY = 0; + m_nSlideDir = CCListViewSlideDirNone; + if (CCListViewModeHorizontal == m_nMode) + { + float dis = 0; + unsigned int nCount = 0; + CCListViewCell *cell = NULL; + if (nRow > m_visibleRows.location) + { + m_nSlideDir = CCListViewSlideDirLeft; + } + else + { + m_nSlideDir = CCListViewSlideDirRight; + } + + while(1) + { + if (dis >= this->getContentSize().width || nRow + nCount >= m_nNumberOfRows) + { + break; + } + + if (CCListViewSlideDirRight == m_nSlideDir) + { + cell = appendRowToFront(nRow + nCount); + } + else if (CCListViewSlideDirLeft == m_nSlideDir) + { + cell = appendRowToBack(nRow + nCount); + } + + if (cell) + { + nCount++; + dis += cell->getContentSize().width; + } + else + { + CCLog("CCListView cell == NULL at line %d", __LINE__); + } + } + + if (CCListViewSlideDirLeft == m_nSlideDir && dis < this->getContentSize().width) + { + // at last + while(1) + { + if (dis >= this->getContentSize().width) + { + break; + } + cell = appendRowToBack(nRow - 1); + if (cell) + { + nRow--; + dis += cell->getContentSize().width; + nCount++; + } + else + { + CCLog("CCListView cell == NULL at line %d", __LINE__); + } + } + } + + if (CCListViewSlideDirRight == m_nSlideDir) + { + CCListViewCell* cell = NULL; + unsigned nLast = 0; + if (CCRange::CCLocationInRange(nRow + nCount - 1, m_visibleRows)) + { + cell = cellAtRow(nRow + nCount - 1); + nLast = nRow + nCount - 2; + } + else + { + cell = cellAtRow(m_visibleRows.location); + nLast = nRow + nCount - 1; + } + if (cell) + { + CCPoint pos = cell->getPosition(); + for (int i = nLast; i >= (int)nRow; i--) + { + cell = cellAtRow(i); + if (cell) + { + pos.x -= cell->getContentSize().width; + cell->setPosition(pos); + } + else + { + CCLog("CCListView cell == NULL at line %d", __LINE__); + } + } + } + else + { + CCLog("CCListView cell == NULL at line %d", __LINE__); + } + } + else if (CCListViewSlideDirLeft == m_nSlideDir) + { + CCListViewCell* cell = NULL; + unsigned nFirst = 0; + if (CCRange::CCLocationInRange(nRow, m_visibleRows)) + { + cell = cellAtRow(nRow); + nFirst = nRow + 1; + } + else + { + cell = cellAtRow(CCRange::CCMaxRange(m_visibleRows)); + nFirst = nRow; + } + if (cell) + { + CCPoint pos = cell->getPosition(); + pos.x += cell->getContentSize().width; + for (unsigned int i = nFirst; i < nRow + nCount; i++) + { + cell = cellAtRow(i); + if (cell) + { + cell->setPosition(pos); + pos.x += cell->getContentSize().width; + + } + else + { + CCLog("CCListView cell == NULL at line %d", __LINE__); + } + } + } + else + { + CCLog("CCListView cell == NULL at line %d", __LINE__); + } + } + + + cell = cellAtRow(nRow); + CCPoint ptCell = cell->convertToWorldSpace(CCPointZero); + CCPoint ptView = this->convertToWorldSpace(CCPointZero); + disX = ptView.x - ptCell.x; + } + else if (CCListViewModeVertical == m_nMode) + { + float dis = 0; + unsigned int nCount = 0; + CCListViewCell *cell = NULL; + if (nRow > m_visibleRows.location) + { + m_nSlideDir = CCListViewSlideDirUp; + } + else + { + m_nSlideDir = CCListViewSlideDirDown; + } + + while(1) + { + if (dis >= this->getContentSize().height || nRow + nCount >= m_nNumberOfRows) + { + break; + } + + if (CCListViewSlideDirDown == m_nSlideDir) + { + cell = appendRowToFront(nRow + nCount); + } + else if (CCListViewSlideDirUp == m_nSlideDir) + { + cell = appendRowToBack(nRow + nCount); + } + + if (cell) + { + nCount++; + dis += cell->getContentSize().height; + } + else + { + CCLog("CCListView cell == NULL at line %d", __LINE__); + } + } + + if (CCListViewSlideDirUp == m_nSlideDir && dis < this->getContentSize().height) + { + // at last + while(1) + { + if (dis >= this->getContentSize().height) + { + break; + } + cell = appendRowToBack(nRow - 1); + if (cell) + { + nRow--; + dis += cell->getContentSize().height; + nCount++; + } + else + { + CCLog("CCListView cell == NULL at line %d", __LINE__); + } + } + } + + if (CCListViewSlideDirDown == m_nSlideDir) + { + CCListViewCell* cell = NULL; + unsigned nLast = 0; + if (CCRange::CCLocationInRange(nRow + nCount - 1, m_visibleRows)) + { + cell = cellAtRow(nRow + nCount - 1); + nLast = nRow + nCount - 2; + } + else + { + cell = cellAtRow(m_visibleRows.location); + nLast = nRow + nCount - 1; + } + if (cell) + { + CCPoint pos = cell->getPosition(); + pos.y += cell->getContentSize().height; + for (int i = nLast; i >= (int)nRow; i--) + { + cell = cellAtRow(i); + if (cell) + { + cell->setPosition(pos); + pos.y += cell->getContentSize().height; + } + else + { + CCLog("CCListView cell == NULL at line %d", __LINE__); + } + } + } + else + { + CCLog("CCListView cell == NULL at line %d", __LINE__); + } + } + else if (CCListViewSlideDirUp == m_nSlideDir) + { + CCListViewCell* cell = NULL; + unsigned nFirst = 0; + if (CCRange::CCLocationInRange(nRow, m_visibleRows)) + { + cell = cellAtRow(nRow); + nFirst = nRow + 1; + } + else + { + cell = cellAtRow(CCRange::CCMaxRange(m_visibleRows)); + nFirst = nRow; + } + if (cell) + { + CCPoint pos = cell->getPosition(); + for (unsigned int i = nFirst; i < nRow + nCount; i++) + { + cell = cellAtRow(i); + if (cell) + { + pos.y -= cell->getContentSize().height; + cell->setPosition(pos); + } + else + { + CCLog("CCListView cell == NULL at line %d", __LINE__); + } + } + } + else + { + CCLog("CCListView cell == NULL at line %d", __LINE__); + } + } + + + cell = cellAtRow(nRow); + CCPoint ptCell = cell->convertToWorldSpace(CCPointZero); + CCPoint ptView = this->convertToWorldSpace(CCPointZero); + disY = ptView.y + this->getContentSize().height - (ptCell.y + cell->getContentSize().height); + } + + m_ptDestination = m_layerPanel->getPosition(); + m_ptDestination.x += disX; + m_ptDestination.y += disY; + m_nState = CCListViewStateScroll; + + if (bAnimated) + { + CCMoveBy *moveBy = CCMoveBy::actionWithDuration(m_fActionDuration, CCPointMake(disX, disY)); + CCEaseOut *ease = CCEaseOut::actionWithAction(moveBy, 3); + CCFiniteTimeAction *actions = CCSequence::actions(ease, CCCallFunc::actionWithTarget(this, callfunc_selector(CCListView::finishScroll)), NULL); + m_layerPanel->runAction(actions); + } + else + { + stopActionImmediately(); + } +} + +void CCListView::scrollCellToBack(unsigned int nRow, bool bAnimated) +{ + if (!isFullFill()) + { + return; + } + if (m_nNumberOfRows == 0) + { + return; + } + if (CCListViewStateWatting != m_nState) + { + this->stopActionImmediately(); + } + + if (nRow >= m_nNumberOfRows) + { + nRow = m_nNumberOfRows - 1; + } + + float disX = 0; + float disY = 0; + m_nSlideDir = CCListViewSlideDirNone; + if (CCListViewModeHorizontal == m_nMode) + { + float dis = 0; + int nCount = 0; + CCListViewCell *cell = NULL; + if (nRow > CCRange::CCMaxRange(m_visibleRows)) + { + m_nSlideDir = CCListViewSlideDirLeft; + } + else + { + m_nSlideDir = CCListViewSlideDirRight; + } + + while(1) + { + if (dis >= this->getContentSize().width || (int)nRow - nCount < 0) + { + break; + } + + if (CCListViewSlideDirRight == m_nSlideDir) + { + cell = appendRowToFront(nRow - nCount); + } + else if (CCListViewSlideDirLeft == m_nSlideDir) + { + cell = appendRowToBack(nRow - nCount); + } + + if (cell) + { + nCount++; + dis += cell->getContentSize().width; + } + else + { + CCLog("CCListView cell == NULL at line %d", __LINE__); + } + } + + if (CCListViewSlideDirRight == m_nSlideDir && dis < this->getContentSize().width) + { + // at first + while(1) + { + if (dis >= this->getContentSize().width) + { + break; + } + cell = appendRowToBack(nRow + 1); + if (cell) + { + nRow++; + dis += cell->getContentSize().width; + nCount++; + } + else + { + CCLog("CCListView cell == NULL at line %d", __LINE__); + } + } + } + + if (CCListViewSlideDirRight == m_nSlideDir) + { + CCListViewCell* cell = NULL; + unsigned nLast = 0; + if (CCRange::CCLocationInRange(nRow, m_visibleRows)) + { + cell = cellAtRow(nRow); + nLast = nRow - 1; + } + else + { + cell = cellAtRow(m_visibleRows.location); + nLast = nRow; + } + if (cell) + { + CCPoint pos = cell->getPosition(); + for (int i = nLast; i >= (int)(nRow - nCount + 1); i--) + { + cell = cellAtRow(i); + if (cell) + { + pos.x -= cell->getContentSize().width; + cell->setPosition(pos); + } + else + { + CCLog("CCListView cell == NULL at line %d", __LINE__); + } + } + } + else + { + CCLog("CCListView cell == NULL at line %d", __LINE__); + } + } + else if (CCListViewSlideDirLeft == m_nSlideDir) + { + CCListViewCell* cell = NULL; + unsigned nFirst = 0; + if (CCRange::CCLocationInRange(nRow - nCount + 1, m_visibleRows)) + { + cell = cellAtRow(nRow - nCount + 1); + nFirst = nRow - nCount + 2; + nCount--; + } + else + { + cell = cellAtRow(CCRange::CCMaxRange(m_visibleRows)); + nFirst = nRow - nCount + 1; + } + if (cell) + { + CCPoint pos = cell->getPosition(); + pos.x += cell->getContentSize().width; + for (unsigned int i = nFirst; i < nFirst + nCount; i++) + { + cell = cellAtRow(i); + if (cell) + { + cell->setPosition(pos); + pos.x += cell->getContentSize().width; + + } + else + { + CCLog("CCListView cell == NULL at line %d", __LINE__); + } + } + } + else + { + CCLog("CCListView cell == NULL at line %d", __LINE__); + } + } + + + cell = cellAtRow(nRow); + CCPoint ptCell = cell->convertToWorldSpace(CCPointZero); + CCPoint ptView = this->convertToWorldSpace(CCPointZero); + disX = ptView.x + this->getContentSize().width - (ptCell.x + cell->getContentSize().width); + } + else if (CCListViewModeVertical == m_nMode) + { + float dis = 0; + int nCount = 0; + CCListViewCell *cell = NULL; + if (nRow > CCRange::CCMaxRange(m_visibleRows)) + { + m_nSlideDir = CCListViewSlideDirUp; + } + else + { + m_nSlideDir = CCListViewSlideDirDown; + } + + while(1) + { + if (dis >= this->getContentSize().height || (int)nRow - nCount < 0) + { + break; + } + + if (CCListViewSlideDirDown == m_nSlideDir) + { + cell = appendRowToFront(nRow - nCount); + } + else if (CCListViewSlideDirUp == m_nSlideDir) + { + cell = appendRowToBack(nRow - nCount); + } + + if (cell) + { + nCount++; + dis += cell->getContentSize().height; + } + else + { + CCLog("CCListView cell == NULL at line %d", __LINE__); + } + } + + if (CCListViewSlideDirDown == m_nSlideDir && dis < this->getContentSize().height) + { + // at first + while(1) + { + if (dis >= this->getContentSize().height) + { + break; + } + cell = appendRowToBack(nRow + 1); + if (cell) + { + nRow++; + dis += cell->getContentSize().height; + nCount++; + } + else + { + CCLog("CCListView cell == NULL at line %d", __LINE__); + } + } + } + + if (CCListViewSlideDirDown == m_nSlideDir) + { + CCListViewCell* cell = NULL; + unsigned nLast = 0; + if (CCRange::CCLocationInRange(nRow, m_visibleRows)) + { + cell = cellAtRow(nRow); + nLast = nRow - 1; + } + else + { + cell = cellAtRow(m_visibleRows.location); + nLast = nRow; + } + if (cell) + { + CCPoint pos = cell->getPosition(); + pos.y += cell->getContentSize().height; + for (int i = nLast; i >= (int)(nRow - nCount + 1); i--) + { + cell = cellAtRow(i); + if (cell) + { + cell->setPosition(pos); + pos.y += cell->getContentSize().height; + } + else + { + CCLog("CCListView cell == NULL at line %d", __LINE__); + } + } + } + else + { + CCLog("CCListView cell == NULL at line %d", __LINE__); + } + } + else if (CCListViewSlideDirUp == m_nSlideDir) + { + CCListViewCell* cell = NULL; + unsigned nFirst = 0; + if (CCRange::CCLocationInRange(nRow - nCount + 1, m_visibleRows)) + { + cell = cellAtRow(nRow - nCount + 1); + nFirst = nRow - nCount + 2; + nCount--; + } + else + { + cell = cellAtRow(CCRange::CCMaxRange(m_visibleRows)); + nFirst = nRow - nCount + 1; + } + if (cell) + { + CCPoint pos = cell->getPosition(); + for (unsigned int i = nFirst; i < nFirst + nCount; i++) + { + cell = cellAtRow(i); + if (cell) + { + pos.y -= cell->getContentSize().height; + cell->setPosition(pos); + } + else + { + CCLog("CCListView cell == NULL at line %d", __LINE__); + } + } + } + else + { + CCLog("CCListView cell == NULL at line %d", __LINE__); + } + } + + + cell = cellAtRow(nRow); + CCPoint ptCell = cell->convertToWorldSpace(CCPointZero); + CCPoint ptView = this->convertToWorldSpace(CCPointZero); + disY = ptView.y - ptCell.y; + } + + m_ptDestination = m_layerPanel->getPosition(); + m_ptDestination.x += disX; + m_ptDestination.y += disY; + m_nState = CCListViewStateScroll; + + if (bAnimated) + { + CCMoveBy *moveBy = CCMoveBy::actionWithDuration(m_fActionDuration, CCPointMake(disX, disY)); + CCEaseOut *ease = CCEaseOut::actionWithAction(moveBy, 3); + CCFiniteTimeAction *actions = CCSequence::actions(ease, CCCallFunc::actionWithTarget(this, callfunc_selector(CCListView::finishScroll)), NULL); + m_layerPanel->runAction(actions); + } + else + { + stopActionImmediately(); + } +} + +CCListViewSlideDir CCListView::getSlideDir(CCPoint ptTouchBegan, CCPoint ptTouchEnd) +{ + CCListViewSlideDir nSlideDir = CCListViewSlideDirNone; + + int nOffsetX = ptTouchEnd.x - ptTouchBegan.x; + int nOffsetY = ptTouchEnd.y - ptTouchBegan.y; + + int disMin = CCDirector::sharedDirector()->getWinSize().height / 100; + + if(CCListViewModeHorizontal == m_nMode) + { + if(nOffsetX >= disMin) + { + nSlideDir = CCListViewSlideDirRight; + } + else if (nOffsetX <= -disMin) + { + nSlideDir = CCListViewSlideDirLeft; + } + } + else if (CCListViewModeVertical == m_nMode) + { + if(nOffsetY >= disMin) + { + nSlideDir = CCListViewSlideDirUp; + } + else if (nOffsetY <= -disMin) + { + nSlideDir = CCListViewSlideDirDown; + } + } + return nSlideDir; +} + +/****************************************** +**************Private Functions************ +*******************************************/ +int CCListView::rowForTouch(cocos2d::CCTouch *touch) +{ + CCPoint touchLocation = touch->locationInView(); + touchLocation = CCDirector::sharedDirector()->convertToGL(touchLocation); + + CCArray *pChildren = m_layerPanel->getChildren(); + if (pChildren && pChildren->count() > 0) + { + CCObject* pObject = NULL; + CCARRAY_FOREACH(pChildren, pObject) + { + CCNode* pChild = (CCNode*) pObject; + if (pChild && pChild->getIsVisible()) + { + CCPoint local = pChild->convertToNodeSpace(touchLocation); + CCRect r = CCRectZero; + r.size = pChild->getContentSize(); + + if (CCRect::CCRectContainsPoint(r, local)) + { + return pChild->getTag(); + } + } + } + + } + + return -1; +} + +void CCListView::finishFix(void) +{ + if(m_pListViewParent) + { + m_pListViewParent->setIsEnabled(true); + } + m_nState = CCListViewStateWatting; + m_nSlideDir = CCListViewSlideDirNone; + clearUnvisibleRows(); + triggerDidScrollToRow(m_visibleRows.location); + + CCArray *children = m_layerPanel->getChildren(); + int nCount = 0; + if (children) + { + nCount = children->count(); + } + //CCLog("row num left:%d [%d, %d]", nCount, m_drawedRows.location, CCRange::CCMaxRange(m_drawedRows)); +} + +void CCListView::finishScroll(void) +{ + finishFix(); +} + +void CCListView::finishEaseOut(void) +{ + bool bNeedFix = false; + + if (CCListViewModeHorizontal == m_nMode) + { + bool bFullFill = isFullFill(); + if (CCListViewSlideDirLeft == m_nSlideDir && bFullFill) + { + CCListViewCell *cell = cellAtRow(m_nNumberOfRows - 1); + if (cell) + { + CCPoint ptCell = cell->convertToWorldSpace(CCPointZero); + CCPoint ptView = this->convertToWorldSpace(CCPointZero); + if (ptCell.x + cell->getContentSize().width < ptView.x + this->getContentSize().width) + { + bNeedFix = true; + fixLastRow(); + } + } + } + else + { + CCListViewCell *cell = cellAtRow(0); + if (cell) + { + CCPoint ptCell = cell->convertToWorldSpace(CCPointZero); + CCPoint ptView = this->convertToWorldSpace(CCPointZero); + if (ptCell.x > ptView.x || !bFullFill) + { + bNeedFix = true; + fixFirstRow(); + } + } + } + } + else if (CCListViewModeVertical == m_nMode) + { + bool bFullFill = this->isFullFill(); + if (CCListViewSlideDirUp == m_nSlideDir && bFullFill) + { + CCListViewCell *cell = cellAtRow(m_nNumberOfRows - 1); + if (cell) + { + CCPoint ptCell = cell->convertToWorldSpace(CCPointZero); + CCPoint ptView = this->convertToWorldSpace(CCPointZero); + if (ptCell.y > ptView.y) + { + bNeedFix = true; + fixLastRow(); + } + } + } + else + { + CCListViewCell *cell = cellAtRow(0); + if (cell) + { + CCPoint ptCell = cell->convertToWorldSpace(CCPointZero); + CCPoint ptView = this->convertToWorldSpace(CCPointZero); + if (ptCell.y + cell->getContentSize().height < ptView.y + this->getContentSize().height || !bFullFill) + { + bNeedFix = true; + fixFirstRow(); + } + } + } + } + + if (!bNeedFix) + { + finishFix(); + } +} + +bool CCListView::isTouchInside(CCTouch *touch) +{ + CCPoint point; + if (m_pListViewParent) + { + point = m_pListViewParent->convertTouchToNodeSpace(touch); + } + else + { + point = this->convertTouchToNodeSpace(touch); + } + CCRect bounds = CCRectMake(0, 0, this->getContentSize().width, this->getContentSize().height); + bool bIn = CCRect::CCRectContainsPoint(bounds, point); + return bIn; +} + +bool CCListView::isFullFill(void) +{ + bool bRet = false; + float length = 0; + for (unsigned int i = m_drawedRows.location; i <= CCRange::CCMaxRange(m_drawedRows); i++) + { + CCListViewCell *cell = cellAtRow(i); + if (cell) + { + if (CCListViewModeHorizontal == m_nMode) + { + length += cell->getContentSize().width; + if (length >= this->getContentSize().width) + { + bRet = true; + break; + } + } + else if (CCListViewModeVertical == m_nMode) + { + length += cell->getContentSize().height; + if (length >= this->getContentSize().height) + { + bRet = true; + break; + } + } + } + } + return bRet; +} + +CCListViewCell *CCListView::cellAtRow(unsigned int nRow) +{ + CCListViewCell *cell = (CCListViewCell*)m_layerPanel->getChildByTag(nRow); + return cell; +} + +void CCListView::stopActionImmediately(void) +{ + m_layerPanel->stopAllActions(); + if (CCListViewStateScroll == m_nState) + { + m_layerPanel->setPosition(m_ptDestination); + finishScroll(); + } +} + +unsigned int CCListView::triggerNumberOfCells(void) +{ + unsigned int nRow = 0; + CCListViewProtrolData data; + + if (m_strDeletegate.size() > 0) + { + CCScriptEngineProtocol* scriptEngine = CCScriptEngineManager::sharedManager()->getScriptEngine(); + if (scriptEngine) + { + std::string script; + script = m_strDeletegate + "CCListView_numberOfCells"; +//TODO: scriptEngine->executeCallFuncNTDT(script.c_str(), this, "NdCxControl::CCListView", &data, "NdCxControl::CCListViewProtrolData"); + nRow = data.nNumberOfRows; + } + } + + if (m_pDelegate) + { + m_pDelegate->CCListView_numberOfCells(this, &data); + nRow = data.nNumberOfRows; + } + return nRow; +} + +CCListViewCell *CCListView::triggerCellForRow(unsigned int nRow) +{ + CCListViewCell *cell = NULL; + CCListViewProtrolData data; + data.nRow = nRow; + data.cell = NULL; + if (m_strDeletegate.size() > 0) + { + CCScriptEngineProtocol* scriptEngine = CCScriptEngineManager::sharedManager()->getScriptEngine(); + if (scriptEngine) + { + std::string script; + script = m_strDeletegate + "CCListView_cellForRow"; +//TODO: scriptEngine->executeCallFuncNTDT(script.c_str(), this, "NdCxControl::CCListView", &data, "NdCxControl::CCListViewProtrolData"); + cell = data.cell; + } + } + if (m_pDelegate) + { + m_pDelegate->CCListView_cellForRow(this, &data); + cell = data.cell; + } + return cell; +} + +void CCListView::triggerDidClickCellAtRow(unsigned int nRow) +{ + CCListViewProtrolData data; + data.nRow = nRow; + if (m_strDeletegate.size() > 0) + { + CCScriptEngineProtocol* scriptEngine = CCScriptEngineManager::sharedManager()->getScriptEngine(); + if (scriptEngine) + { + std::string script; + script = m_strDeletegate + "CCListView_didClickCellAtRow"; +//TODO: scriptEngine->executeCallFuncNTDT(script.c_str(), this, "NdCxControl::CCListView", &data, "NdCxControl::CCListViewProtrolData"); + } + } + + if (m_pDelegate) + { + m_pDelegate->CCListView_didClickCellAtRow(this, &data); + } +} + +void CCListView::triggerDidScrollToRow(unsigned int nRow) +{ + CCListViewProtrolData data; + data.nRow = nRow; + if (m_strDeletegate.size() > 0) + { + CCScriptEngineProtocol* scriptEngine = CCScriptEngineManager::sharedManager()->getScriptEngine(); + if (scriptEngine) + { + std::string script; + script = m_strDeletegate + "CCListView_didScrollToRow"; +//TODO: scriptEngine->executeCallFuncNTDT(script.c_str(), this, "NdCxControl::CCListView", &data, "NdCxControl::CCListViewProtrolData"); + } + } + if (m_pDelegate) + { + m_pDelegate->CCListView_didScrollToRow(this, &data); + } +} + +void CCListView::displayVisibleRows(void) +{ + CCSize size = getContentSize(); + float posPannel, boundPannel; + unsigned int rowCount = m_drawedRows.location; + + CCListViewCell *cell = cellAtRow(rowCount); + + if (m_nMode == CCListViewModeHorizontal) + { + if (cell) + { + posPannel = cell->getPosition().x; + } + else + { + posPannel = m_layerPanel->getPosition().x; + } + boundPannel = posPannel + size.width; + } + else if (m_nMode == CCListViewModeVertical) + { + if (cell) + { + posPannel = cell->getPosition().y + cell->getContentSize().height; + } + else + { + posPannel = size.height - m_layerPanel->getPosition().y; + } + boundPannel = posPannel - size.height; + } + + CCListViewCell *lastCell = NULL; + while(1) + { + // row condition + if (rowCount >= m_nNumberOfRows) + break; + + // size condition + if (m_nMode == CCListViewModeHorizontal) + { + if (posPannel >= boundPannel) + break; + } + else if (m_nMode == CCListViewModeVertical) + { + if (posPannel <= boundPannel) + break; + } + + // get cell + CCListViewCell *cell = cellAtRow(rowCount); + + if (NULL == cell) + { + cell = triggerCellForRow(rowCount); + if (cell) + { + CCSize cellSize = cell->getContentSize(); + if (m_nMode == CCListViewModeHorizontal) + { + cell->setPosition(CCPointMake(posPannel, 0)); + cell->setContentSize(CCSizeMake(cellSize.width, size.height)); + } + else if (m_nMode == CCListViewModeVertical) + { + cell->setPosition(CCPointMake(0, posPannel - cellSize.height)); + cell->setContentSize(CCSizeMake(size.width, cellSize.height)); + } + m_layerPanel->addChild(cell, rowCount, rowCount); + } + } + + if (cell) + { + if (m_nMode == CCListViewModeHorizontal) + { + posPannel += cell->getContentSize().width; + } + else if (m_nMode == CCListViewModeVertical) + { + posPannel -= cell->getContentSize().height; + } + cell->setSeparatorStyle(m_nSeparatorStyle); + lastCell = cell; + } + + ++rowCount; + } + m_drawedRows.length = rowCount - m_drawedRows.location; + // 设置可视范围 + m_visibleRows = m_drawedRows; + + if (lastCell) + { + lastCell->setSeparatorStyle(CCListViewCellSeparatorStyleNone); + } +} + +CCListViewCell* CCListView::appendRowToBack(unsigned int nRow) +{ + if (nRow >= m_nNumberOfRows) + { + return NULL; + } + CCListViewCell *cell = cellAtRow(nRow); + if (cell == NULL) + { + cell = triggerCellForRow(nRow); + if (cell) + { + CCSize size = this->getContentSize(); + CCSize cellSize = cell->getContentSize(); + float pos; + unsigned int nLastRow = CCRange::CCMaxRange(m_drawedRows); + CCListViewCell *lastCell = cellAtRow(nLastRow); + if (lastCell) + { + if (m_nMode == CCListViewModeHorizontal) + { + pos = lastCell->getPosition().x + lastCell->getContentSize().width; + cell->setPosition(CCPointMake(pos, 0)); + cell->setContentSize(CCSizeMake(cellSize.width, size.height)); + } + else if (m_nMode == CCListViewModeVertical) + { + pos = lastCell->getPosition().y - cell->getContentSize().height; + cell->setPosition(CCPointMake(0, pos)); + cell->setContentSize(CCSizeMake(size.width, cellSize.height)); + } + + if (nRow == m_nSelectedRow) + { + cell->selected(); + } + m_layerPanel->addChild(cell, nRow, nRow); + if (nRow > CCRange::CCMaxRange(m_drawedRows)) + { + cell->setSeparatorStyle(CCListViewCellSeparatorStyleNone); + lastCell->setSeparatorStyle(m_nSeparatorStyle); + m_drawedRows.length += nRow - CCRange::CCMaxRange(m_drawedRows); + } + else + { + cell->setSeparatorStyle(m_nSeparatorStyle); + } + } + else + { + CCLog("CCListView cell == NULL at line %d", __LINE__); + } + } + else + { + CCLog("CCListView cell == NULL at line %d", __LINE__); + } + } + return cell; +} + +CCListViewCell* CCListView::appendRowToFront(unsigned int nRow) +{ + CCListViewCell *cell = cellAtRow(nRow); + if (cell == NULL) + { + cell = triggerCellForRow(nRow); + if (cell) + { + CCSize size = this->getContentSize(); + CCSize cellSize = cell->getContentSize(); + float pos; + unsigned int nFirstRow = m_drawedRows.location; + CCListViewCell *firstCell = cellAtRow(nFirstRow); + if (firstCell) + { + if (m_nMode == CCListViewModeHorizontal) + { + pos = firstCell->getPosition().x - cell->getContentSize().width; + cell->setPosition(CCPointMake(pos, 0)); + cell->setContentSize(CCSizeMake(cellSize.width, size.height)); + } + else if (m_nMode == CCListViewModeVertical) + { + pos = firstCell->getPosition().y + firstCell->getContentSize().height; + cell->setPosition(CCPointMake(0, pos)); + cell->setContentSize(CCSizeMake(size.width, cellSize.height)); + } + cell->setSeparatorStyle(m_nSeparatorStyle); + if (nRow == m_nSelectedRow) + { + cell->selected(); + } + m_layerPanel->addChild(cell, nRow, nRow); + if (nRow < m_drawedRows.location) + { + m_drawedRows.length += m_drawedRows.location - nRow; + m_drawedRows.location = nRow; + } + } + else + { + CCLog("CCListView cell == NULL at line %d", __LINE__); + } + } + else + { + CCLog("CCListView cell == NULL at line %d", __LINE__); + } + } + return cell; +} +// 对齐末行 +void CCListView::fixFirstRow(void) +{ + unsigned int location = m_drawedRows.location; + + CCListViewCell *cell = cellAtRow(location); + if (cell) + { + float disX = 0; + float disY = 0; + CCPoint posCell = cell->convertToWorldSpace(CCPointZero); + CCPoint posView = this->convertToWorldSpace(CCPointZero); + if (CCListViewModeHorizontal == m_nMode) + { + float dis = posCell.x - posView.x; + dis = -dis; + + disX = dis; + disY = 0; + } + else if (CCListViewModeVertical == m_nMode) + { + float dis = posCell.y + cell->getContentSize().height - (posView.y + this->getContentSize().height); + dis = -dis; + + disX = 0; + disY = dis; + } + + m_nState = CCListViewStateFix; + CCMoveBy *moveBy = CCMoveBy::actionWithDuration(m_fActionDuration, CCPointMake(disX, disY)); + CCEaseInOut *ease = CCEaseInOut::actionWithAction(moveBy, 2); + CCFiniteTimeAction *actions = CCSequence::actions(ease, CCCallFunc::actionWithTarget(this, callfunc_selector(CCListView::finishFix)), NULL); + m_layerPanel->runAction(actions); + } + else + { + this->finishFix(); + } +} +// 对齐首行 +void CCListView::fixLastRow(void) +{ + unsigned int nLastRow = CCRange::CCMaxRange(m_drawedRows); + CCListViewCell *cell = cellAtRow(nLastRow); + if (cell) + { + float disX = 0; + float disY = 0; + CCPoint posCell = cell->convertToWorldSpace(CCPointZero); + CCPoint posView = this->convertToWorldSpace(CCPointZero); + if (CCListViewModeHorizontal == m_nMode) + { + float dis = posCell.x + cell->getContentSize().width - (posView.x + this->getContentSize().width); + dis = -dis; + + disX = dis; + disY = 0; + } + else if (CCListViewModeVertical == m_nMode) + { + float dis = posCell.y - posView.y; + dis = -dis; + + disX = 0; + disY = dis; + } + + m_nState = CCListViewStateFix; + CCMoveBy *moveBy = CCMoveBy::actionWithDuration(m_fActionDuration, CCPointMake(disX, disY)); + CCEaseInOut *ease = CCEaseInOut::actionWithAction(moveBy, 2); + CCFiniteTimeAction *actions = CCSequence::actions(ease, CCCallFunc::actionWithTarget(this, callfunc_selector(CCListView::finishFix)), NULL); + m_layerPanel->runAction(actions); + } + else + { + finishFix(); + } +} +// 手势滑动界面,减速效果 +void CCListView::easeOutWithDistance(float dis) +{ + float disX = 0; + float disY = 0; + + if (CCListViewModeHorizontal == m_nMode) + { + if (CCListViewSlideDirLeft == m_nSlideDir) + { + // drag Left + while(1) + { + unsigned int nLastRow = CCRange::CCMaxRange(m_drawedRows); + CCListViewCell *cell = cellAtRow(nLastRow); + if (cell) + { + CCPoint ptCell = cell->convertToWorldSpace(CCPointZero); + CCPoint ptView = this->convertToWorldSpace(CCPointZero); + if (nLastRow < m_nNumberOfRows - 1) + { + float offset = ptView.x + this->getContentSize().width - (ptCell.x + cell->getContentSize().width); + if (offset > dis) + { + appendRowToBack(nLastRow + 1); + } + else + { + dis = offset; + break; + } + } + else + { + // already is the last row + float leftMin = cell->getContentSize().width / 5; + float offset = ptCell.x + cell->getContentSize().width + dis - ptView.x; + if (offset < -leftMin) + { + dis = ptView.x + leftMin - (ptCell.x + cell->getContentSize().width); + } + offset = ptCell.x + cell->getContentSize().width + dis - (ptView.x + this->getContentSize().width) ; + if (offset > 0) + { + dis = ptView.x + this->getContentSize().width - (ptCell.x + cell->getContentSize().width); + } + break; + } + } + else + { + break; + } + } + } + else + { + // drag right + while(1) + { + unsigned int nFirstRow = m_drawedRows.location; + CCListViewCell *cell = cellAtRow(nFirstRow); + if (cell) + { + CCPoint ptCell = cell->convertToWorldSpace(CCPointZero); + CCPoint ptView = this->convertToWorldSpace(CCPointZero); + if (nFirstRow > 0) + { + float offset = ptView.x - ptCell.x; + if (offset < dis) + { + appendRowToFront(nFirstRow - 1); + } + else + { + dis = offset; + break; + } + } + else + { + // already is the first row + float leftMin = cell->getContentSize().width / 5; + float offset = ptCell.x + dis - (ptView.x + this->getContentSize().width); + if (offset > leftMin) + { + dis = ptView.x + this->getContentSize().width - leftMin - ptCell.x; + } + offset = ptView.x - ptCell.x; + if (offset > 0) + { + dis = offset; + } + break; + } + } + else + { + + } + } + } + disX = dis; + disY = 0; + } + else if (CCListViewModeVertical == m_nMode) + { + if (CCListViewSlideDirUp == m_nSlideDir) + { + // drag up + while(1) + { + unsigned int nLastRow = CCRange::CCMaxRange(m_drawedRows); + CCListViewCell *cell = cellAtRow(nLastRow); + if (cell) + { + CCPoint ptCell = cell->convertToWorldSpace(CCPointZero); + CCPoint ptView = this->convertToWorldSpace(CCPointZero); + if (nLastRow < m_nNumberOfRows - 1) + { + float offset = ptView.y - ptCell.y; + if (offset < dis) + { + appendRowToBack(nLastRow + 1); + } + else + { + dis = offset; + break; + } + } + else + { + // already is the last row + m_fActionDuration *= 0.5; + float leftMin = cell->getContentSize().height / 5; + float offset = ptCell.y + dis - (ptView.y + this->getContentSize().height); + if (offset > -leftMin) + { + dis = ptView.y + this->getContentSize().height - leftMin - ptCell.y; + } + break; + } + } + else + { + break; + } + } + } + else + { + // drag down + while(1) + { + unsigned int nFirstRow = m_drawedRows.location; + CCListViewCell *cell = cellAtRow(nFirstRow); + if (cell) + { + CCPoint ptCell = cell->convertToWorldSpace(CCPointZero); + CCPoint ptView = this->convertToWorldSpace(CCPointZero); + if (nFirstRow > 0) + { + float offset = ptView.y + this->getContentSize().height - (ptCell.y + cell->getContentSize().height); + if (offset > dis) + { + appendRowToFront(nFirstRow - 1); + } + else + { + dis = offset; + break; + } + } + else + { + // already is the first row + m_fActionDuration *= 0.5; + float leftMin = cell->getContentSize().height / 5; + float offset = ptCell.y + cell->getContentSize().height + dis - ptView.y; + if (offset < leftMin) + { + dis = ptView.y + leftMin - (ptCell.y + cell->getContentSize().height); + } + break; + } + } + else + { + + } + } + } + disX = 0; + disY = dis; + } + + m_nState = CCListViewStateEaseOut; + CCMoveBy *moveBy = CCMoveBy::actionWithDuration(m_fActionDuration, CCPointMake(disX, disY)); + CCEaseOut *ease = CCEaseOut::actionWithAction(moveBy, 3); + CCFiniteTimeAction *actions = CCSequence::actions(ease, CCCallFunc::actionWithTarget(this, callfunc_selector(CCListView::finishEaseOut)), NULL); + m_layerPanel->runAction(actions); +} + +void CCListView::clearUnvisibleRows(void) +{ + CCRange oldRange = m_drawedRows; + for (unsigned int i = oldRange.location; i < oldRange.location + oldRange.length; i++) + { + CCListViewCell *cell = cellAtRow(i); + if (cell) + { + CCPoint posCell = cell->convertToWorldSpace(CCPointZero); + CCPoint posView = this->convertToWorldSpace(CCPointZero); + if (CCListViewModeHorizontal == m_nMode) + { + if (posCell.x + cell->getContentSize().width <= posView.x) + { + m_layerPanel->removeChild(cell, true); + } + else + { + break; + } + } + else if (CCListViewModeVertical == m_nMode) + { + if (posCell.y >= posView.y + this->getContentSize().height) + { + m_layerPanel->removeChild(cell, true); + } + else + { + break; + } + } + } + m_drawedRows.location++; + m_drawedRows.length--; + } + + oldRange = m_drawedRows; + for (int i = oldRange.location + oldRange.length - 1; i >= (int)oldRange.location; i--) + { + CCListViewCell *cell = cellAtRow(i); + if (cell) + { + CCPoint posCell = cell->convertToWorldSpace(CCPointZero); + CCPoint posView = this->convertToWorldSpace(CCPointZero); + if (CCListViewModeHorizontal == m_nMode) + { + if (posCell.x >= posView.x + this->getContentSize().width) + { + m_layerPanel->removeChild(cell, true); + } + else + { + break; + } + } + else if (CCListViewModeVertical == m_nMode) + { + if (posCell.y + cell->getContentSize().height <= posView.y) + { + m_layerPanel->removeChild(cell, true); + } + else + { + break; + } + } + } + m_drawedRows.length--; + } + + m_visibleRows = m_drawedRows; +} +/****************************************** +**************Virturl Functions************ +*******************************************/ +void CCListView::registerWithTouchDispatcher() +{ + CCDirector* pDirector = CCDirector::sharedDirector(); + if (m_pListViewParent) + { + pDirector->getTouchDispatcher()->addTargetedDelegate(this, kCCMenuHandlerPriority - 1, false); + } + else + { + pDirector->getTouchDispatcher()->addTargetedDelegate(this, kCCMenuHandlerPriority - 2, false); + } +} + +void CCListView::onEnter(void) +{ + if (0 == m_nNumberOfRows) + { + m_layerPanel->setPosition(CCPointZero); + m_layerPanel->setContentSize(this->getContentSize()); + // get number of rows + m_nNumberOfRows = triggerNumberOfCells(); + displayVisibleRows(); + } + + CCLayerColor::onEnter(); +} + +void CCListView::onExit(void) +{ + CCLayerColor::onExit(); +} + +void CCListView::visit(void) +{ + if (!m_pListViewParent) + { + CCRect rectSelf; + float factor = CC_CONTENT_SCALE_FACTOR(); + rectSelf.origin = convertToWorldSpace(CCPoint(0,0)); + rectSelf.origin.x *= factor; + rectSelf.origin.y *= factor; + rectSelf.size = this->getContentSize(); + rectSelf.size.width *= factor; + rectSelf.size.height *= factor; + glScissor((GLsizei)rectSelf.origin.x, (GLsizei)rectSelf.origin.y, (GLsizei)rectSelf.size.width , (GLsizei)rectSelf.size.height); + glEnable(GL_SCISSOR_TEST); + } + CCLayerColor::visit(); + if (!m_pListViewParent) + { + glDisable(GL_SCISSOR_TEST); + } +} + +bool CCListView::ccTouchBegan(CCTouch* touch, CCEvent* event) +{ + CC_UNUSED_PARAM(event); + + if (!isTouchInside(touch) || !getIsVisible() || !m_bIsEnabled) + { + return false; + } + + if (m_pListViewParent && CCListViewSlideDirNone != m_pListViewParent->getSlideDir()) + { + return false; + } + + CCArray *children = this->getChildren(); + if (!m_bIsVisible || !children || children->count() == 0) + { + return false; + } + + if (m_bIsOnTouch) + { + return false; + } + + CCPoint touchPoint = touch->locationInView(); + + m_ptTouchBegan = m_ptTouchEnd = CCDirector::sharedDirector()->convertToGL(touchPoint); + m_ptPanelOffset = m_layerPanel->getPosition(); + + m_timeTouchBegan = clock(); + + m_nCurrentRow = this->rowForTouch(touch); + if (m_nCurrentRow != -1) + { + if (CCListViewStateWatting != m_nState) + { + stopActionImmediately(); + } + + m_nState = CCListViewStateTrackingTouch; + if (CCListViewSlideDirNone == m_nSlideDir) + { + this->selectCellAtRow(m_nCurrentRow); + } + else + { + m_nCurrentRow = -1; + } + m_bIsOnTouch = true; + return true; + } + + + return false; +} + +void CCListView::ccTouchMoved(CCTouch* touch, CCEvent* event) +{ + CC_UNUSED_PARAM(event); + if (CCListViewStateTrackingTouch != m_nState || !isTouchInside(touch) || !m_bIsEnabled) + { + return; + } + + CCPoint touchPoint = touch->locationInView(); + m_ptTouchEnd = CCDirector::sharedDirector()->convertToGL(touchPoint); + + + CCListViewSlideDir nsliderDir = CCListViewSlideDirNone; + if (m_pListViewParent && CCListViewSlideDirNone != m_pListViewParent->getSlideDir(m_ptTouchBegan, m_ptTouchEnd)) + { + return; + } + else + { + nsliderDir = getSlideDir(m_ptTouchBegan, m_ptTouchEnd); + } + + + if(CCListViewModeHorizontal == m_nMode && CCListViewSlideDirNone != nsliderDir) + { + m_nSlideDir = nsliderDir; + m_layerPanel->setPosition(CCPointMake(m_ptPanelOffset.x + (m_ptTouchEnd.x - m_ptTouchBegan.x),m_ptPanelOffset.y)); + if (CCListViewSlideDirLeft == m_nSlideDir) + { + // drag left + unsigned int nLastRow = CCRange::CCMaxRange(m_drawedRows); + if (nLastRow < m_nNumberOfRows - 1) + { + CCListViewCell *cell = cellAtRow(nLastRow); + if (cell) + { + CCPoint posCell = cell->convertToWorldSpace(CCPointZero); + CCPoint posView = this->convertToWorldSpace(CCPointZero); + if (posCell.x + cell->getContentSize().width <= posView.x + this->getContentSize().width) + { + appendRowToBack(nLastRow + 1); + } + } + } + } + else + { + // drag right + unsigned int nFirstRow = m_drawedRows.location; + if (nFirstRow > 0) + { + CCListViewCell *cell = cellAtRow(nFirstRow); + CCPoint posCell = cell->convertToWorldSpace(CCPointZero); + CCPoint posView = this->convertToWorldSpace(CCPointZero); + if (posCell.x >= posView.x) + { + appendRowToFront(nFirstRow - 1); + } + } + } + } + else if (CCListViewModeVertical == m_nMode && CCListViewSlideDirNone != nsliderDir) + { + m_nSlideDir = nsliderDir; + m_layerPanel->setPosition(CCPointMake(m_ptPanelOffset.x, m_ptPanelOffset.y + (m_ptTouchEnd.y - m_ptTouchBegan.y))); + if (CCListViewSlideDirUp == m_nSlideDir) + { + // drag up + unsigned int nLastRow = CCRange::CCMaxRange(m_drawedRows); + if (nLastRow < m_nNumberOfRows - 1) + { + CCListViewCell *cell = cellAtRow(nLastRow); + CCPoint posCell = cell->convertToWorldSpace(CCPointZero); + CCPoint posView = this->convertToWorldSpace(CCPointZero); + if (posCell.y >= posView.y) + { + appendRowToBack(nLastRow + 1); + } + } + } + else + { + // drag down + unsigned int nFirstRow = m_drawedRows.location; + if (nFirstRow > 0) + { + CCListViewCell *cell = cellAtRow(nFirstRow); + CCPoint posCell = cell->convertToWorldSpace(CCPointZero); + CCPoint posView = this->convertToWorldSpace(CCPointZero); + if (posCell.y + cell->getContentSize().height <= posView.y + this->getContentSize().height) + { + appendRowToFront(nFirstRow - 1); + } + } + } + } + + if (CCListViewSlideDirNone != m_nSlideDir && m_nCurrentRow != -1 && m_nCurrentRow != m_nSelectedRow) + { + this->unselectCellAtRow(m_nCurrentRow); + } + + if (CCListViewSlideDirNone != m_nSlideDir && m_pListViewParent) + { + m_pListViewParent->setIsEnabled(false); + } +} + +void CCListView::ccTouchEnded(CCTouch* touch, CCEvent* event) +{ + CC_UNUSED_PARAM(touch); + CC_UNUSED_PARAM(event); + + if(m_nState != CCListViewStateTrackingTouch || !m_bIsEnabled) + { + m_bIsOnTouch = false; + return; + } + + m_fActionDuration = ND_LISTVIEW_ACTION_INTERVAL; + clock_t timeElapse = clock() - m_timeTouchBegan; +#if CC_TARGET_PLATFORM != CC_PLATFORM_WIN32 && CC_TARGET_PLATFORM != CC_PLATFORM_MAC + // 手机平台用时较长,此系数可能需要根据不同平台做相应调整 + timeElapse /= 200; +#endif + if(CCListViewSlideDirLeft == m_nSlideDir || CCListViewSlideDirRight == m_nSlideDir) + { + float dis = m_ptTouchEnd.x - m_ptTouchBegan.x; + float speed = dis / timeElapse; + if (fabs(speed) > 0.1 && timeElapse < 300) + { + easeOutWithDistance(dis * 3); + } + else + { + if (CCListViewSlideDirLeft == m_nSlideDir && isFullFill()) + { + // drag up + fixLastRow(); + } + else + { + // drag down + fixFirstRow(); + } + } + } + else if (CCListViewSlideDirUp == m_nSlideDir || CCListViewSlideDirDown == m_nSlideDir) + { + float dis = m_ptTouchEnd.y - m_ptTouchBegan.y; + float speed = dis / timeElapse; + if (fabs(speed) > 0.1 && timeElapse < 300) + { + easeOutWithDistance(dis * 3); + } + else + { + if (CCListViewSlideDirUp == m_nSlideDir && isFullFill()) + { + // drag up + fixLastRow(); + } + else + { + // drag down + fixFirstRow(); + } + } + } + else + { + finishFix(); + } + + int currentRow = this->rowForTouch(touch); + if (currentRow != -1 && isTouchInside(touch)) + { + if (currentRow == m_nCurrentRow) + { + if (CCListViewSlideDirNone == m_nSlideDir) + { + if (m_pListViewParent == NULL || (m_pListViewParent && CCListViewSlideDirNone == m_pListViewParent->getSlideDir())) + { + if (m_nSelectedRow != -1 && m_nSelectedRow != m_nCurrentRow) + { + this->unselectCellAtRow(m_nSelectedRow); + } + m_nSelectedRow = m_nCurrentRow; + + if (!this->isMenuTouch(touch, this->cellAtRow(m_nSelectedRow))) + { + triggerDidClickCellAtRow(m_nSelectedRow); + } + } + } + else + { + if (m_nSelectedRow != m_nCurrentRow) + { + this->unselectCellAtRow(m_nCurrentRow); + } + } + } + else + { + if (-1 != m_nCurrentRow) + { + this->unselectCellAtRow(m_nCurrentRow); + } + } + } + else + { + if (-1 != m_nCurrentRow) + { + this->unselectCellAtRow(m_nCurrentRow); + m_nCurrentRow = -1; + } + } + m_bIsOnTouch = false; +} + +bool CCListView::isMenuTouch(CCTouch *touch, CCNode *parent) +{ + if (dynamic_cast(parent)) + { + CCPoint touchPoint = touch->locationInView(); + touchPoint.y = cocos2d::CCDirector::sharedDirector()->getWinSize().height - touchPoint.y; + touchPoint = parent->convertToNodeSpace(touchPoint); + CCRect rect = CCRectZero; + rect.size = parent->getContentSize(); + return CCRect::CCRectContainsPoint(rect, touchPoint); + } + else + { + CCArray *pChildrens = parent->getChildren(); + if (pChildrens && pChildrens->count() > 0) + { + for (int i = 0; i < pChildrens->count(); i++) + { + CCNode *pChildren = (CCNode*)pChildrens->objectAtIndex(i); + if (this->isMenuTouch(touch, pChildren)) + { + return true; + } + } + } + } + return false; +} + +void CCListView::ccTouchCancelled(CCTouch *touch, CCEvent *event) +{ + CC_UNUSED_PARAM(touch); + CC_UNUSED_PARAM(event); + + CCAssert(m_nState == CCListViewStateTrackingTouch, "[NdListview ccTouchCancelled] -- invalid state"); + + finishFix(); + m_bIsOnTouch = false; +} + +NS_CC_EXT_END + + + + diff --git a/cocos2dx/extensions/CCListView/CCListView.h b/cocos2dx/extensions/CCListView/CCListView.h index 0ed5b885dd..9e1d244439 100644 --- a/cocos2dx/extensions/CCListView/CCListView.h +++ b/cocos2dx/extensions/CCListView/CCListView.h @@ -1,188 +1,191 @@ -#ifndef __CC_LIST_VIEW_H__ -#define __CC_LIST_VIEW_H__ - -#include -#include "platform.h" -#include -#include -//#include "../lua/cocos2dx_support/CCLuaEngine.h" -#include "CCListViewCell.h" - -NS_CC_EXT_BEGIN - -class CC_DLL CCRange -{ -public: - CCRange() - { - this->location = 0; - this->length = 0; - } - - CCRange(unsigned int loc, unsigned int len) - { - this->location = loc; - this->length = len; - } - - static unsigned int CCMaxRange(CCRange range) - { - return (range.location + range.length-1); - } - - static bool CCLocationInRange(unsigned int loc, CCRange range) - { - return (loc - range.location <= range.length); - } - - static bool CCEqualRanges(CCRange range1, CCRange range2) { return (range1.location == range2.location && range1.length == range2.length); } - - unsigned int length; - unsigned int location; -}; - -#define CCRangeMake(__min__, __max__) CCRange((__min__), (__max__)) - -typedef enum -{ - CCListViewSlideDirNone, - CCListViewSlideDirUp, - CCListViewSlideDirDown, - CCListViewSlideDirLeft, - CCListViewSlideDirRight, -} CCListViewSlideDir; - -typedef enum -{ - CCListViewStateWatting, - CCListViewStateTrackingTouch, - CCListViewStateEaseOut, - CCListViewStateFix, - CCListViewStateScroll, -} CCListViewState; - -typedef enum -{ - CCListViewModeHorizontal, - CCListViewModeVertical, -} CCListViewMode; - -typedef struct _CCListViewProtrolData -{ - unsigned int nNumberOfRows; - unsigned int nRow; - CCListViewCell *cell; -} CCListViewProtrolData; - -class CC_DLL CCListViewDelegate -{ -public : - CCListViewDelegate(){}; - virtual ~CCListViewDelegate(){}; - - virtual void CCListView_numberOfCells(CCListView *listView, CCListViewProtrolData *data)=0; - virtual void CCListView_cellForRow(CCListView *listView, CCListViewProtrolData *data)=0; - virtual void CCListView_didClickCellAtRow(CCListView *listView, CCListViewProtrolData *data)=0; - virtual void CCListView_didScrollToRow(CCListView *listView, CCListViewProtrolData *data)=0; -}; - - -class CC_DLL CCListView : public CCLayerColor -{ -public: - virtual ~CCListView(void); - CCListView(void); - - static CCListView* viewWithMode(CCListViewMode mode); - bool initWithMode(CCListViewMode mode); - - void setDelegateName(const char* pszName); - void selectCellAtRow(unsigned int nRow); - void unselectCellAtRow(unsigned int nRow); - void scrollCellToFront(unsigned int nRow, bool bAnimated); - void scrollCellToBack(unsigned int nRow, bool bAnimated); - void reload(void); - void insertCellsAtRow(unsigned int nRow, unsigned int nCount); - void deleteCellsAtRow(unsigned int nRow, unsigned int nCount); - CCListViewCell *cellAtRow(unsigned int nRow); - - CCListViewSlideDir getSlideDir(CCPoint ptTouchBegan, CCPoint ptTouchEnd); - inline CCListViewSlideDir getSlideDir(void) { return m_nSlideDir; } - - inline CCListViewCellSeparatorStyle getSeparatorStyle(void) { return m_nSeparatorStyle; } - inline void setSeparatorStyle(CCListViewCellSeparatorStyle style) { m_nSeparatorStyle = style; } - inline CCListViewMode getMode(void) { return m_nMode; } - - inline void setListViewParent(CCListView *pParent) { m_pListViewParent = pParent; } - inline CCListView *getListViewParent(void) { return m_pListViewParent; } - - inline void setIsEnabled(bool bEnabled) { m_bIsEnabled = bEnabled; } - inline bool getIsEnabled(void) { return m_bIsEnabled; } - - // un - void setDelegate(const CCListViewDelegate *pDelegate) { m_pDelegate = const_cast(pDelegate);} - void finishFix(void); - void finishScroll(void); - void finishEaseOut(void); - -public: - virtual bool ccTouchBegan(CCTouch* touch, CCEvent* event); - virtual void ccTouchEnded(CCTouch* touch, CCEvent* event); - virtual void ccTouchCancelled(CCTouch *touch, CCEvent* event); - virtual void ccTouchMoved(CCTouch* touch, CCEvent* event); - - virtual void onEnter(void); - virtual void onExit(void); - - virtual void registerWithTouchDispatcher(void); - virtual void visit(void); - -protected: - void displayVisibleRows(void); - CCListViewCell* appendRowToBack(unsigned int nRow); - CCListViewCell* appendRowToFront(unsigned int nRow); - void fixFirstRow(void); - void fixLastRow(void); - void easeOutWithDistance(float dis); - void clearUnvisibleRows(void); - - int rowForTouch(cocos2d::CCTouch *touch); - bool isTouchInside(CCTouch *touch); - bool isFullFill(void); - - void stopActionImmediately(void); - - unsigned int triggerNumberOfCells(void); - CCListViewCell *triggerCellForRow(unsigned int nRow); - void triggerDidClickCellAtRow(unsigned int nRow); - void triggerDidScrollToRow(unsigned int nRow); - bool isMenuTouch(CCTouch *touch, CCNode *parent); - -private: - CCListViewState m_nState; - CCListViewMode m_nMode; - CCListViewSlideDir m_nSlideDir; - CCListViewCellSeparatorStyle m_nSeparatorStyle; - unsigned int m_nNumberOfRows; - float m_fActionDuration; - clock_t m_timeTouchBegan; - CCRange m_drawedRows; //所有已绘制的cell - CCRange m_visibleRows; //所有可见的cell - CCPoint m_ptTouchBegan; - CCPoint m_ptTouchEnd; - CCPoint m_ptPanelOffset; - CCPoint m_ptDestination; - std::string m_strDeletegate; - CCListViewDelegate* m_pDelegate; - CCLayer* m_layerPanel; - CCListView* m_pListViewParent; - int m_nSelectedRow; - int m_nCurrentRow; - bool m_bIsEnabled; - bool m_bIsOnTouch; -}; - -NS_CC_EXT_END - - +#ifndef __CC_LIST_VIEW_H__ +#define __CC_LIST_VIEW_H__ + +#include +#include "platform.h" +#include +#include +//#include "../lua/cocos2dx_support/CCLuaEngine.h" +#include "CCListViewCell.h" + +NS_CC_EXT_BEGIN + +class CC_DLL CCRange +{ +public: + CCRange() + { + this->location = 0; + this->length = 0; + } + + CCRange(unsigned int loc, unsigned int len) + { + this->location = loc; + this->length = len; + } + + static unsigned int CCMaxRange(CCRange range) + { + return (range.location + range.length-1); + } + + static bool CCLocationInRange(unsigned int loc, CCRange range) + { + return (loc - range.location <= range.length); + } + + static bool CCEqualRanges(CCRange range1, CCRange range2) + { + return (range1.location == range2.location && range1.length == range2.length); + } + + unsigned int length; + unsigned int location; +}; + +#define CCRangeMake(__min__, __max__) CCRange((__min__), (__max__)) + +typedef enum +{ + CCListViewSlideDirNone, + CCListViewSlideDirUp, + CCListViewSlideDirDown, + CCListViewSlideDirLeft, + CCListViewSlideDirRight, +} CCListViewSlideDir; + +typedef enum +{ + CCListViewStateWatting, + CCListViewStateTrackingTouch, + CCListViewStateEaseOut, + CCListViewStateFix, + CCListViewStateScroll, +} CCListViewState; + +typedef enum +{ + CCListViewModeHorizontal, + CCListViewModeVertical, +} CCListViewMode; + +typedef struct _CCListViewProtrolData +{ + unsigned int nNumberOfRows; + unsigned int nRow; + CCListViewCell *cell; +} CCListViewProtrolData; + +class CC_DLL CCListViewDelegate +{ +public : + CCListViewDelegate(){}; + virtual ~CCListViewDelegate(){}; + + virtual void CCListView_numberOfCells(CCListView *listView, CCListViewProtrolData *data)=0; + virtual void CCListView_cellForRow(CCListView *listView, CCListViewProtrolData *data)=0; + virtual void CCListView_didClickCellAtRow(CCListView *listView, CCListViewProtrolData *data)=0; + virtual void CCListView_didScrollToRow(CCListView *listView, CCListViewProtrolData *data)=0; +}; + + +class CC_DLL CCListView : public CCLayerColor +{ +public: + virtual ~CCListView(void); + CCListView(void); + + static CCListView* viewWithMode(CCListViewMode mode); + bool initWithMode(CCListViewMode mode); + + void setDelegateName(const char* pszName); + void selectCellAtRow(unsigned int nRow); + void unselectCellAtRow(unsigned int nRow); + void scrollCellToFront(unsigned int nRow, bool bAnimated); + void scrollCellToBack(unsigned int nRow, bool bAnimated); + void reload(void); + void insertCellsAtRow(unsigned int nRow, unsigned int nCount); + void deleteCellsAtRow(unsigned int nRow, unsigned int nCount); + CCListViewCell *cellAtRow(unsigned int nRow); + + CCListViewSlideDir getSlideDir(CCPoint ptTouchBegan, CCPoint ptTouchEnd); + inline CCListViewSlideDir getSlideDir(void) { return m_nSlideDir; } + + inline CCListViewCellSeparatorStyle getSeparatorStyle(void) { return m_nSeparatorStyle; } + inline void setSeparatorStyle(CCListViewCellSeparatorStyle style) { m_nSeparatorStyle = style; } + inline CCListViewMode getMode(void) { return m_nMode; } + + inline void setListViewParent(CCListView *pParent) { m_pListViewParent = pParent; } + inline CCListView *getListViewParent(void) { return m_pListViewParent; } + + inline void setIsEnabled(bool bEnabled) { m_bIsEnabled = bEnabled; } + inline bool getIsEnabled(void) { return m_bIsEnabled; } + + // un + void setDelegate(const CCListViewDelegate *pDelegate) { m_pDelegate = const_cast(pDelegate);} + void finishFix(void); + void finishScroll(void); + void finishEaseOut(void); + +public: + virtual bool ccTouchBegan(CCTouch* touch, CCEvent* event); + virtual void ccTouchEnded(CCTouch* touch, CCEvent* event); + virtual void ccTouchCancelled(CCTouch *touch, CCEvent* event); + virtual void ccTouchMoved(CCTouch* touch, CCEvent* event); + + virtual void onEnter(void); + virtual void onExit(void); + + virtual void registerWithTouchDispatcher(void); + virtual void visit(void); + +protected: + void displayVisibleRows(void); + CCListViewCell* appendRowToBack(unsigned int nRow); + CCListViewCell* appendRowToFront(unsigned int nRow); + void fixFirstRow(void); + void fixLastRow(void); + void easeOutWithDistance(float dis); + void clearUnvisibleRows(void); + + int rowForTouch(cocos2d::CCTouch *touch); + bool isTouchInside(CCTouch *touch); + bool isFullFill(void); + + void stopActionImmediately(void); + + unsigned int triggerNumberOfCells(void); + CCListViewCell *triggerCellForRow(unsigned int nRow); + void triggerDidClickCellAtRow(unsigned int nRow); + void triggerDidScrollToRow(unsigned int nRow); + bool isMenuTouch(CCTouch *touch, CCNode *parent); + +private: + CCListViewState m_nState; + CCListViewMode m_nMode; + CCListViewSlideDir m_nSlideDir; + CCListViewCellSeparatorStyle m_nSeparatorStyle; + unsigned int m_nNumberOfRows; + float m_fActionDuration; + clock_t m_timeTouchBegan; + CCRange m_drawedRows; //所有已绘制的cell + CCRange m_visibleRows; //所有可见的cell + CCPoint m_ptTouchBegan; + CCPoint m_ptTouchEnd; + CCPoint m_ptPanelOffset; + CCPoint m_ptDestination; + std::string m_strDeletegate; + CCListViewDelegate* m_pDelegate; + CCLayer* m_layerPanel; + CCListView* m_pListViewParent; + int m_nSelectedRow; + int m_nCurrentRow; + bool m_bIsEnabled; + bool m_bIsOnTouch; +}; + +NS_CC_EXT_END + + #endif // __CC_LIST_VIEW_H__ \ No newline at end of file diff --git a/cocos2dx/extensions/CCListView/CCListViewCell.cpp b/cocos2dx/extensions/CCListView/CCListViewCell.cpp index bda9aafa5c..8f303f1b8f 100644 --- a/cocos2dx/extensions/CCListView/CCListViewCell.cpp +++ b/cocos2dx/extensions/CCListView/CCListViewCell.cpp @@ -1,96 +1,96 @@ -#include "CCListView.h" -#include "CCListViewCell.h" -#include "cocos2d.h" - -NS_CC_EXT_BEGIN - -const int TOUCHBEGIN = 1; -const int TOUCHEND = 2; -const int TOUCHMOVING = 3; -const int TOUCHCANCELLED= 4; - - -/****************************************** -**************Public Functions************* -*******************************************/ -CCListViewCell::CCListViewCell(void) - :m_nSeparatorStyle(CCListViewCellSeparatorStyleNone) - ,m_bIsSelected(false) -{ - setIsTouchEnabled(true); - m_selectionColor = ccc4(0, 0, 255, 255); - m_separatorLineColor = ccc3(128, 128, 128); -} - -CCListViewCell::~CCListViewCell(void) -{ - -} - -CCListViewCell *CCListViewCell::node(void) -{ - CCListViewCell *pRet = new CCListViewCell(); - pRet->initWithColorWidthHeight(ccc4(255, 255, 255, 255), 0, 0); - pRet->autorelease(); - return pRet; -} - -void CCListViewCell::selected(void) -{ - m_bIsSelected = true; - CCLayerColor::setColor(ccc3(m_selectionColor.r, m_selectionColor.g, m_selectionColor.b)); - CCLayerColor::setOpacity(m_selectionColor.a); -} - -void CCListViewCell::unselected(void) -{ - m_bIsSelected = false; - CCLayerColor::setColor(ccc3(m_normalColor.r, m_normalColor.g, m_normalColor.b)); - CCLayerColor::setOpacity(m_normalColor.a); -} - -/****************************************** -**************Virturl Functions************ -*******************************************/ +#include "CCListView.h" +#include "CCListViewCell.h" +#include "cocos2d.h" + +NS_CC_EXT_BEGIN + +const int TOUCHBEGIN = 1; +const int TOUCHEND = 2; +const int TOUCHMOVING = 3; +const int TOUCHCANCELLED= 4; + + +/****************************************** +**************Public Functions************* +*******************************************/ +CCListViewCell::CCListViewCell(void) + :m_nSeparatorStyle(CCListViewCellSeparatorStyleNone) + ,m_bIsSelected(false) +{ + setIsTouchEnabled(true); + m_selectionColor = ccc4(0, 0, 255, 255); + m_separatorLineColor = ccc3(128, 128, 128); +} + +CCListViewCell::~CCListViewCell(void) +{ + +} + +CCListViewCell *CCListViewCell::node(void) +{ + CCListViewCell *pRet = new CCListViewCell(); + pRet->initWithColorWidthHeight(ccc4(255, 255, 255, 255), 0, 0); + pRet->autorelease(); + return pRet; +} + +void CCListViewCell::selected(void) +{ + m_bIsSelected = true; + CCLayerColor::setColor(ccc3(m_selectionColor.r, m_selectionColor.g, m_selectionColor.b)); + CCLayerColor::setOpacity(m_selectionColor.a); +} + +void CCListViewCell::unselected(void) +{ + m_bIsSelected = false; + CCLayerColor::setColor(ccc3(m_normalColor.r, m_normalColor.g, m_normalColor.b)); + CCLayerColor::setOpacity(m_normalColor.a); +} + +/****************************************** +**************Virturl Functions************ +*******************************************/ bool CCListViewCell::initWithColorWidthHeight(ccColor4B color, GLfloat width, GLfloat height) -{ - this->m_normalColor = color; - return CCLayerColor::initWithColor(color, width, height); -} - -void CCListViewCell::draw(void) -{ - CCLayerColor::draw(); - CCSize size = this->getContentSize(); - CCListView *owner = this->getOwner(); - if (CCListViewCellSeparatorStyleSingleLine == m_nSeparatorStyle) - { - glLineWidth(1.0f); - ccDrawColor4B(m_separatorLineColor.r, m_separatorLineColor.g, m_separatorLineColor.b, 255); - - if (CCListViewModeHorizontal == owner->getMode()) - { - ccDrawLine(CCPointMake(size.width, 0), CCPointMake(size.width, size.height)); - } - else if (CCListViewModeVertical == owner->getMode()) - { - ccDrawLine(CCPointMake(0, 0), CCPointMake(size.width, 0)); - } - } -} - +{ + this->m_normalColor = color; + return CCLayerColor::initWithColor(color, width, height); +} + +void CCListViewCell::draw(void) +{ + CCLayerColor::draw(); + CCSize size = this->getContentSize(); + CCListView *owner = this->getOwner(); + if (CCListViewCellSeparatorStyleSingleLine == m_nSeparatorStyle) + { + glLineWidth(1.0f); + ccDrawColor4B(m_separatorLineColor.r, m_separatorLineColor.g, m_separatorLineColor.b, 255); + + if (CCListViewModeHorizontal == owner->getMode()) + { + ccDrawLine(CCPointMake(size.width, 0), CCPointMake(size.width, size.height)); + } + else if (CCListViewModeVertical == owner->getMode()) + { + ccDrawLine(CCPointMake(0, 0), CCPointMake(size.width, 0)); + } + } +} + void CCListViewCell::setColor(ccColor3B var) { - m_normalColor.r = var.r; - m_normalColor.g = var.g; - m_normalColor.b = var.b; - CCLayerColor::setColor(var); -} - -void CCListViewCell::setOpacity(GLubyte var) -{ - m_normalColor.a = var; - CCLayerColor::setOpacity(var); -} - + m_normalColor.r = var.r; + m_normalColor.g = var.g; + m_normalColor.b = var.b; + CCLayerColor::setColor(var); +} + +void CCListViewCell::setOpacity(GLubyte var) +{ + m_normalColor.a = var; + CCLayerColor::setOpacity(var); +} + NS_CC_EXT_END \ No newline at end of file diff --git a/cocos2dx/extensions/CCListView/CCListViewCell.h b/cocos2dx/extensions/CCListView/CCListViewCell.h index 713cf91f83..4ee263ed09 100644 --- a/cocos2dx/extensions/CCListView/CCListViewCell.h +++ b/cocos2dx/extensions/CCListView/CCListViewCell.h @@ -1,55 +1,55 @@ -#ifndef __CC_LIST_VIEW_CELL_H_ -#define __CC_LIST_VIEW_CELL_H_ - -#include "CCControlDefine.h" -#include "CCLayer.h" - -NS_CC_EXT_BEGIN - -class CCListView; -typedef enum -{ - CCListViewCellSeparatorStyleNone, - CCListViewCellSeparatorStyleSingleLine, -}CCListViewCellSeparatorStyle; - -class CC_DLL CCListViewCell : public CCLayerColor -{ - public: - CCListViewCell(void); - virtual ~CCListViewCell(void); - - static CCListViewCell *node(void); - - void selected(void); - void unselected(void); - - inline CCListViewCellSeparatorStyle getSeparatorStyle(void) { return m_nSeparatorStyle; } - inline void setSeparatorStyle(CCListViewCellSeparatorStyle style) { m_nSeparatorStyle = style; } - - inline ccColor4B getSelectionColor(void) { return m_selectionColor; } - inline void setSelectionColor(ccColor4B color) { m_selectionColor = color; } - - inline ccColor3B getSeparatorLineColor(void) { return m_separatorLineColor; } - inline void setSeparatorLineColor(ccColor3B color) { m_separatorLineColor = color; } - - public: - virtual bool initWithColorWidthHeight(ccColor4B color, GLfloat width, GLfloat height); - virtual void draw(void); - virtual void setColor(ccColor3B color); - virtual void setOpacity(GLubyte var); - - private: - inline CCListView *getOwner(void) { return (CCListView*)(this->getParent()->getParent()); } - - private: - CCListViewCellSeparatorStyle m_nSeparatorStyle; - bool m_bIsSelected; - ccColor4B m_selectionColor; - ccColor4B m_normalColor; - ccColor3B m_separatorLineColor; -}; - -NS_CC_EXT_END - +#ifndef __CC_LIST_VIEW_CELL_H_ +#define __CC_LIST_VIEW_CELL_H_ + +#include "CCControlDefine.h" +#include "CCLayer.h" + +NS_CC_EXT_BEGIN + +class CCListView; +typedef enum +{ + CCListViewCellSeparatorStyleNone, + CCListViewCellSeparatorStyleSingleLine, +}CCListViewCellSeparatorStyle; + +class CC_DLL CCListViewCell : public CCLayerColor +{ + public: + CCListViewCell(void); + virtual ~CCListViewCell(void); + + static CCListViewCell *node(void); + + void selected(void); + void unselected(void); + + inline CCListViewCellSeparatorStyle getSeparatorStyle(void) { return m_nSeparatorStyle; } + inline void setSeparatorStyle(CCListViewCellSeparatorStyle style) { m_nSeparatorStyle = style; } + + inline ccColor4B getSelectionColor(void) { return m_selectionColor; } + inline void setSelectionColor(ccColor4B color) { m_selectionColor = color; } + + inline ccColor3B getSeparatorLineColor(void) { return m_separatorLineColor; } + inline void setSeparatorLineColor(ccColor3B color) { m_separatorLineColor = color; } + + public: + virtual bool initWithColorWidthHeight(ccColor4B color, GLfloat width, GLfloat height); + virtual void draw(void); + virtual void setColor(ccColor3B color); + virtual void setOpacity(GLubyte var); + + private: + inline CCListView *getOwner(void) { return (CCListView*)(this->getParent()->getParent()); } + + private: + CCListViewCellSeparatorStyle m_nSeparatorStyle; + bool m_bIsSelected; + ccColor4B m_selectionColor; + ccColor4B m_normalColor; + ccColor3B m_separatorLineColor; +}; + +NS_CC_EXT_END + #endif // __CC_LIST_VIEW_CELL_H_ \ No newline at end of file diff --git a/cocos2dx/extensions/CCTextureWatcher/CCTextureWatcher.cpp b/cocos2dx/extensions/CCTextureWatcher/CCTextureWatcher.cpp index 7c88ef71a3..9ce9704a64 100644 --- a/cocos2dx/extensions/CCTextureWatcher/CCTextureWatcher.cpp +++ b/cocos2dx/extensions/CCTextureWatcher/CCTextureWatcher.cpp @@ -5,82 +5,82 @@ using namespace std; NS_CC_EXT_BEGIN -#define NUM_PER_PAGE 4 +#define NUM_PER_PAGE 4 CCTextureWatcher::CCTextureWatcher() { - m_bHide = false; - m_nCurrnetPage = 1; - m_nTotalPage = 0; - m_bFresh = true; - m_pTextures = NULL; - m_pszString = NULL; - m_pLayer = CCLayerColor::layerWithColor(ccc4(128, 128, 128, 128)); - m_pLayer->retain(); + m_bHide = false; + m_nCurrnetPage = 1; + m_nTotalPage = 0; + m_bFresh = true; + m_pTextures = NULL; + m_pszString = NULL; + m_pLayer = CCLayerColor::layerWithColor(ccc4(128, 128, 128, 128)); + m_pLayer->retain(); - // layer - CCSize size = CCDirector::sharedDirector()->getWinSize(); - size.height *= 0.6; - m_pLayer->setContentSize(size); + // layer + CCSize size = CCDirector::sharedDirector()->getWinSize(); + size.height *= 0.6; + m_pLayer->setContentSize(size); - // 屏蔽点击事件的menu - //* - CCLabelTTF *label = CCLabelTTF::labelWithString(" ", size, CCTextAlignmentLeft, "Arial", 12); - CCMenuItemLabel *menuItem = CCMenuItemLabel::itemWithLabel(label); - menuItem->setAnchorPoint(CCPoint(0, 0)); - menuItem->setPosition(CCPoint(0, 0)); - - CCMenu *menu = CCMenu::menuWithItem(menuItem); - menu->setAnchorPoint(CCPoint(0, 0)); - menu->setPosition(CCPoint(0, 0)); - m_pLayer->addChild(menu); - //*/ + // 屏蔽点击事件的menu + //* + CCLabelTTF *label = CCLabelTTF::labelWithString(" ", size, CCTextAlignmentLeft, "Arial", 12); + CCMenuItemLabel *menuItem = CCMenuItemLabel::itemWithLabel(label); + menuItem->setAnchorPoint(CCPoint(0, 0)); + menuItem->setPosition(CCPoint(0, 0)); + + CCMenu *menu = CCMenu::menuWithItem(menuItem); + menu->setAnchorPoint(CCPoint(0, 0)); + menu->setPosition(CCPoint(0, 0)); + m_pLayer->addChild(menu); + //*/ - // list - CCListView *list = CCListView::viewWithMode(CCListViewModeHorizontal); - list->setContentSize(size); - list->setDelegate(this); - list->setSeparatorStyle(CCListViewCellSeparatorStyleNone); - m_pLayer->addChild(list); - m_pList = list; + // list + CCListView *list = CCListView::viewWithMode(CCListViewModeHorizontal); + list->setContentSize(size); + list->setDelegate(this); + list->setSeparatorStyle(CCListViewCellSeparatorStyleNone); + m_pLayer->addChild(list); + m_pList = list; - // 隐藏按钮 - CCLabelTTF *labelHide = CCLabelTTF::labelWithString("Hide ", "Arial", 24); - labelHide->setColor(ccc3(255, 0, 0)); - CCMenuItemLabel *menuItem2 = CCMenuItemLabel::itemWithLabel(labelHide, this, menu_selector(CCTextureWatcher::actionHide)); - menuItem2->setAnchorPoint(CCPoint(0, 0)); - menuItem2->setPosition(CCPoint(0, 0)); + // 隐藏按钮 + CCLabelTTF *labelHide = CCLabelTTF::labelWithString("Hide ", "Arial", 24); + labelHide->setColor(ccc3(255, 0, 0)); + CCMenuItemLabel *menuItem2 = CCMenuItemLabel::itemWithLabel(labelHide, this, menu_selector(CCTextureWatcher::actionHide)); + menuItem2->setAnchorPoint(CCPoint(0, 0)); + menuItem2->setPosition(CCPoint(0, 0)); - CCMenu *menu2 = CCMenu::menuWithItem(menuItem2); - menu2->setAnchorPoint(CCPoint(0, 0)); - menu2->setPosition(CCPoint(size.width - menuItem2->getContentSize().width, 0)); + CCMenu *menu2 = CCMenu::menuWithItem(menuItem2); + menu2->setAnchorPoint(CCPoint(0, 0)); + menu2->setPosition(CCPoint(size.width - menuItem2->getContentSize().width, 0)); - m_labelHide = labelHide; - m_menuHide = menu2; - m_menuHide->retain(); + m_labelHide = labelHide; + m_menuHide = menu2; + m_menuHide->retain(); - // 更新按钮 - CCLabelTTF *labelFresh = CCLabelTTF::labelWithString("Fresh", "Arial", 24); - labelFresh->setColor(ccc3(255, 0, 0)); - CCMenuItemLabel *menuItem1 = CCMenuItemLabel::itemWithLabel(labelFresh, this, menu_selector(CCTextureWatcher::actionFresh)); - menuItem1->setAnchorPoint(CCPoint(0, 0)); - menuItem1->setPosition(CCPoint(0, 0)); + // 更新按钮 + CCLabelTTF *labelFresh = CCLabelTTF::labelWithString("Fresh", "Arial", 24); + labelFresh->setColor(ccc3(255, 0, 0)); + CCMenuItemLabel *menuItem1 = CCMenuItemLabel::itemWithLabel(labelFresh, this, menu_selector(CCTextureWatcher::actionFresh)); + menuItem1->setAnchorPoint(CCPoint(0, 0)); + menuItem1->setPosition(CCPoint(0, 0)); - CCMenu *menu1 = CCMenu::menuWithItem(menuItem1); - menu1->setAnchorPoint(CCPoint(0, 0)); - menu1->setPosition(CCPoint(size.width - menuItem1->getContentSize().width - menuItem2->getContentSize().width * 1.5, 0)); - m_pLayer->addChild(menu1); - - // label page - m_labelPage = CCLabelTTF::labelWithString(" ", CCSizeMake(size.width * 0.1, labelFresh->getContentSize().height), CCTextAlignmentCenter, "Arial", 16); - m_labelPage->setAnchorPoint(CCPoint(0.5, 0)); - m_labelPage->setPosition(CCPoint(size.width/2.0, 0)); - m_pLayer->addChild(m_labelPage, 0); + CCMenu *menu1 = CCMenu::menuWithItem(menuItem1); + menu1->setAnchorPoint(CCPoint(0, 0)); + menu1->setPosition(CCPoint(size.width - menuItem1->getContentSize().width - menuItem2->getContentSize().width * 1.5, 0)); + m_pLayer->addChild(menu1); + + // label page + m_labelPage = CCLabelTTF::labelWithString(" ", CCSizeMake(size.width * 0.1, labelFresh->getContentSize().height), CCTextAlignmentCenter, "Arial", 16); + m_labelPage->setAnchorPoint(CCPoint(0.5, 0)); + m_labelPage->setPosition(CCPoint(size.width/2.0, 0)); + m_pLayer->addChild(m_labelPage, 0); } CCTextureWatcher::~CCTextureWatcher() { - if (m_menuHide) + if (m_menuHide) { m_menuHide->removeFromParentAndCleanup(true); m_menuHide->release(); @@ -92,101 +92,101 @@ CCTextureWatcher::~CCTextureWatcher() } if (m_pTextures) m_pTextures->release(); - if (m_pszString) delete []m_pszString; + if (m_pszString) delete []m_pszString; } void CCTextureWatcher::actionFresh(CCObject* object) { - CCTextureWatcher::sharedTextureWatcher()->fresh(); + CCTextureWatcher::sharedTextureWatcher()->fresh(); } void CCTextureWatcher::actionHide(CCObject *object) { - CCTextureWatcher::sharedTextureWatcher()->hide(); + CCTextureWatcher::sharedTextureWatcher()->hide(); } void CCTextureWatcher::fresh() { - m_nCurrnetPage = 1; - m_bFresh = true; + m_nCurrnetPage = 1; + m_bFresh = true; } void CCTextureWatcher::hide() { - m_bHide = !m_bHide; - if (m_bHide) - { - m_labelHide->setString("Show"); - m_pLayer->setPosition(CCPoint(0, -m_pLayer->getContentSize().height)); - } - else - { - m_labelHide->setString("Hide"); - m_pLayer->setPosition(CCPoint(0, 0)); - } + m_bHide = !m_bHide; + if (m_bHide) + { + m_labelHide->setString("Show"); + m_pLayer->setPosition(CCPoint(0, -m_pLayer->getContentSize().height)); + } + else + { + m_labelHide->setString("Hide"); + m_pLayer->setPosition(CCPoint(0, 0)); + } } void CCTextureWatcher::dovisit() { - if (m_bFresh) - { - if (m_pTextures) - { - m_pTextures->removeAllObjects(); - m_pTextures->release(); - } + if (m_bFresh) + { + if (m_pTextures) + { + m_pTextures->removeAllObjects(); + m_pTextures->release(); + } - CCTextureCache::sharedTextureCache()->removeUnusedTextures(); - m_pTextures = CCTextureCache::sharedTextureCache()->snapshotTextures(); - m_nTotalPage = (m_pTextures->count() + NUM_PER_PAGE - 1) / NUM_PER_PAGE; - if (m_pTextures->count() > 0) - { - m_bFresh = false; - m_pList->reload(); - } - } - CCNode *pParent = m_pLayer->getParent(); - if (pParent) - { - if (pParent != CCDirector::sharedDirector()->getRunningScene()) - { - pParent->removeChild(m_pLayer, true); - CCDirector::sharedDirector()->getRunningScene()->addChild(m_pLayer, 9998); - m_bFresh = true; - } - } - else - { - CCDirector::sharedDirector()->getRunningScene()->addChild(m_pLayer, 9998); - } + CCTextureCache::sharedTextureCache()->removeUnusedTextures(); + m_pTextures = CCTextureCache::sharedTextureCache()->snapshotTextures(); + m_nTotalPage = (m_pTextures->count() + NUM_PER_PAGE - 1) / NUM_PER_PAGE; + if (m_pTextures->count() > 0) + { + m_bFresh = false; + m_pList->reload(); + } + } + CCNode *pParent = m_pLayer->getParent(); + if (pParent) + { + if (pParent != CCDirector::sharedDirector()->getRunningScene()) + { + pParent->removeChild(m_pLayer, true); + CCDirector::sharedDirector()->getRunningScene()->addChild(m_pLayer, 9998); + m_bFresh = true; + } + } + else + { + CCDirector::sharedDirector()->getRunningScene()->addChild(m_pLayer, 9998); + } - pParent = m_menuHide->getParent(); - if (pParent) - { - if (pParent != CCDirector::sharedDirector()->getRunningScene()) - { - pParent->removeChild(m_menuHide, true); - CCDirector::sharedDirector()->getRunningScene()->addChild(m_menuHide, 9999); - } - } - else - { - CCDirector::sharedDirector()->getRunningScene()->addChild(m_menuHide, 9999); - } + pParent = m_menuHide->getParent(); + if (pParent) + { + if (pParent != CCDirector::sharedDirector()->getRunningScene()) + { + pParent->removeChild(m_menuHide, true); + CCDirector::sharedDirector()->getRunningScene()->addChild(m_menuHide, 9999); + } + } + else + { + CCDirector::sharedDirector()->getRunningScene()->addChild(m_menuHide, 9999); + } } void CCTextureWatcher::visit(void* pSender) { - CCTextureWatcher *wartcher = (CCTextureWatcher*)pSender; - wartcher->dovisit(); + CCTextureWatcher *wartcher = (CCTextureWatcher*)pSender; + wartcher->dovisit(); } static CCTextureWatcher *g_sharedTextureWatcher = NULL; CCTextureWatcher* CCTextureWatcher::sharedTextureWatcher() { - if (!g_sharedTextureWatcher) + if (!g_sharedTextureWatcher) { - g_sharedTextureWatcher = new CCTextureWatcher(); + g_sharedTextureWatcher = new CCTextureWatcher(); } - return g_sharedTextureWatcher; + return g_sharedTextureWatcher; } void CCTextureWatcher::purgeTextureWatcher() @@ -199,141 +199,141 @@ void CCTextureWatcher::purgeTextureWatcher() void CCTextureWatcher::setDisplayWatcher(bool bDisplayWatcher) { - m_bDisplayWatcher = bDisplayWatcher; - if (m_bDisplayWatcher) - { - if (m_pszString == NULL) - { - m_pszString = new char[64]; - } - CCDirector::sharedDirector()->setWatcherCallbackFun(this, &CCTextureWatcher::visit); - } - else - { - CCDirector::sharedDirector()->setWatcherCallbackFun(NULL, NULL); + m_bDisplayWatcher = bDisplayWatcher; + if (m_bDisplayWatcher) + { + if (m_pszString == NULL) + { + m_pszString = new char[64]; + } + CCDirector::sharedDirector()->setWatcherCallbackFun(this, &CCTextureWatcher::visit); + } + else + { + CCDirector::sharedDirector()->setWatcherCallbackFun(NULL, NULL); purgeTextureWatcher(); - } + } } -void CCTextureWatcher::CCListView_numberOfCells(CCListView *listView, CCListViewProtrolData *data) -{ - data->nNumberOfRows = m_nTotalPage; -} - -void CCTextureWatcher::CCListView_cellForRow(CCListView *listView, CCListViewProtrolData *data) +void CCTextureWatcher::CCListView_numberOfCells(CCListView *listView, CCListViewProtrolData *data) { - m_nCurrnetPage = data->nRow + 1; - CCListViewCell *cell = CCListViewCell::node(); - cell->setOpacity(0); - cell->setContentSize(m_pList->getContentSize()); - cell->setSelectionColor(ccc4(0, 0, 0, 0)); - data->cell = cell; + data->nNumberOfRows = m_nTotalPage; +} - CCSize listItemSize = CCSize(m_pList->getContentSize().width / NUM_PER_PAGE, m_pList->getContentSize().height); +void CCTextureWatcher::CCListView_cellForRow(CCListView *listView, CCListViewProtrolData *data) +{ + m_nCurrnetPage = data->nRow + 1; + CCListViewCell *cell = CCListViewCell::node(); + cell->setOpacity(0); + cell->setContentSize(m_pList->getContentSize()); + cell->setSelectionColor(ccc4(0, 0, 0, 0)); + data->cell = cell; - CCSize size = CCSize(listItemSize.width * 0.9, listItemSize.height * 0.6); + CCSize listItemSize = CCSize(m_pList->getContentSize().width / NUM_PER_PAGE, m_pList->getContentSize().height); - sprintf(m_pszString, "%d/%d", m_nCurrnetPage, m_nTotalPage); - m_labelPage->setString(m_pszString); + CCSize size = CCSize(listItemSize.width * 0.9, listItemSize.height * 0.6); - float offX = 0, offY = 0, offsetX = 0, offsetY = 0; - CC_UNUSED_PARAM(offsetY); - int nCount = 0; - int nStart = (m_nCurrnetPage - 1) * NUM_PER_PAGE; - int nEnd = nStart + NUM_PER_PAGE; + sprintf(m_pszString, "%d/%d", m_nCurrnetPage, m_nTotalPage); + m_labelPage->setString(m_pszString); + + float offX = 0, offY = 0, offsetX = 0, offsetY = 0; + CC_UNUSED_PARAM(offsetY); + int nCount = 0; + int nStart = (m_nCurrnetPage - 1) * NUM_PER_PAGE; + int nEnd = nStart + NUM_PER_PAGE; CCDictElement* pElement = NULL; CCDICT_FOREACH(m_pTextures, pElement) { - if (nCount >= nStart && nCount < nEnd) - { - string key = pElement->getStrKey(); - CCTexture2D* textrue = (CCTexture2D*)pElement->getObject(); - //textrue = m_pTextures->objectForKey(*it); - if (textrue) - { - // 引用数 - sprintf(m_pszString, "[%d]", textrue->retainCount() - 2); - CCLabelTTF *labelCount = CCLabelTTF::labelWithString(m_pszString, "Arial", 16); - if (textrue->retainCount() - 2 > 0) - { - labelCount->setColor(ccc3(0, 255, 0)); - } - else - { - labelCount->setColor(ccc3(255, 0, 0)); - } - offX = offsetX + listItemSize.width * 0.5 - labelCount->getContentSize().width * 0.5; - offY = (listItemSize.height - size.height) * 0.5 - labelCount->getContentSize().height; - labelCount->setPosition(CCPoint(offX, offY)); - labelCount->setAnchorPoint(CCPoint(0, 0)); - cell->addChild(labelCount); + if (nCount >= nStart && nCount < nEnd) + { + string key = pElement->getStrKey(); + CCTexture2D* textrue = (CCTexture2D*)pElement->getObject(); + //textrue = m_pTextures->objectForKey(*it); + if (textrue) + { + // 引用数 + sprintf(m_pszString, "[%d]", textrue->retainCount() - 2); + CCLabelTTF *labelCount = CCLabelTTF::labelWithString(m_pszString, "Arial", 16); + if (textrue->retainCount() - 2 > 0) + { + labelCount->setColor(ccc3(0, 255, 0)); + } + else + { + labelCount->setColor(ccc3(255, 0, 0)); + } + offX = offsetX + listItemSize.width * 0.5 - labelCount->getContentSize().width * 0.5; + offY = (listItemSize.height - size.height) * 0.5 - labelCount->getContentSize().height; + labelCount->setPosition(CCPoint(offX, offY)); + labelCount->setAnchorPoint(CCPoint(0, 0)); + cell->addChild(labelCount); - // 大小 - sprintf(m_pszString, "%.0f*%.0f", textrue->getContentSize().width, textrue->getContentSize().height); - CCLabelTTF *labelSize = CCLabelTTF::labelWithString(m_pszString, "Arial", 16); - offX = offsetX + listItemSize.width * 0.5; - offY = (listItemSize.height - size.height) * 0.5 + size.height; - labelSize->setPosition(CCPoint(offX, offY)); - labelSize->setAnchorPoint(CCPoint(0.5, 0)); - cell->addChild(labelSize); + // 大小 + sprintf(m_pszString, "%.0f*%.0f", textrue->getContentSize().width, textrue->getContentSize().height); + CCLabelTTF *labelSize = CCLabelTTF::labelWithString(m_pszString, "Arial", 16); + offX = offsetX + listItemSize.width * 0.5; + offY = (listItemSize.height - size.height) * 0.5 + size.height; + labelSize->setPosition(CCPoint(offX, offY)); + labelSize->setAnchorPoint(CCPoint(0.5, 0)); + cell->addChild(labelSize); - // 名称 - int len = key.length(); - int pos = 0; + // 名称 + int len = key.length(); + int pos = 0; #if defined(ND_MAC) || defined(ND_IPHONE) - pos = key.rfind('/') + 1; + pos = key.rfind('/') + 1; #else - pos = key.rfind('\\') + 1; - int pos2 = key.rfind('/') + 1; - pos = pos > pos2 ? pos : pos2; + pos = key.rfind('\\') + 1; + int pos2 = key.rfind('/') + 1; + pos = pos > pos2 ? pos : pos2; #endif - string name = key.substr(pos, len - pos); - sprintf(m_pszString, "%s", name.c_str()); - CCSize dimensions = CCSizeMake(listItemSize.width * 0.9, labelSize->getContentSize().height); - CCLabelTTF *labelName = CCLabelTTF::labelWithString(m_pszString, dimensions, CCTextAlignmentCenter, "Arial", 16); - offX = offsetX + listItemSize.width * 0.5; - offY = offY + labelName->getContentSize().height; - labelName->setPosition(CCPoint(offX, offY)); - labelName->setAnchorPoint(CCPoint(0.5, 0)); - cell->addChild(labelName); + string name = key.substr(pos, len - pos); + sprintf(m_pszString, "%s", name.c_str()); + CCSize dimensions = CCSizeMake(listItemSize.width * 0.9, labelSize->getContentSize().height); + CCLabelTTF *labelName = CCLabelTTF::labelWithString(m_pszString, dimensions, CCTextAlignmentCenter, "Arial", 16); + offX = offsetX + listItemSize.width * 0.5; + offY = offY + labelName->getContentSize().height; + labelName->setPosition(CCPoint(offX, offY)); + labelName->setAnchorPoint(CCPoint(0.5, 0)); + cell->addChild(labelName); - CCSprite *sprite = CCSprite::spriteWithTexture(textrue); - sprite->setAnchorPoint(CCPoint(0, 0)); + CCSprite *sprite = CCSprite::spriteWithTexture(textrue); + sprite->setAnchorPoint(CCPoint(0, 0)); + + CCSize spriteSize = sprite->getContentSize(); + float scale; + if (spriteSize.width < size.width && spriteSize.height < size.height) + { + scale = 1; + } + else if (spriteSize.width * size.height >= spriteSize.height * size.width) + { + scale = size.width / spriteSize.width; + } + else + { + scale = size.height / spriteSize.height; + } + sprite->setScale(scale); + spriteSize.width *= scale; + spriteSize.height *= scale; + offX = offsetX + (listItemSize.width - spriteSize.width) * 0.5; + offY = (listItemSize.height - spriteSize.height) * 0.5; + sprite->setPosition(CCPoint(offX, offY)); + cell->addChild(sprite); + offsetX += listItemSize.width; + } + } + ++nCount; + } +} + +void CCTextureWatcher::CCListView_didClickCellAtRow(CCListView *listView, CCListViewProtrolData *data) +{ + +} - CCSize spriteSize = sprite->getContentSize(); - float scale; - if (spriteSize.width < size.width && spriteSize.height < size.height) - { - scale = 1; - } - else if (spriteSize.width * size.height >= spriteSize.height * size.width) - { - scale = size.width / spriteSize.width; - } - else - { - scale = size.height / spriteSize.height; - } - sprite->setScale(scale); - spriteSize.width *= scale; - spriteSize.height *= scale; - offX = offsetX + (listItemSize.width - spriteSize.width) * 0.5; - offY = (listItemSize.height - spriteSize.height) * 0.5; - sprite->setPosition(CCPoint(offX, offY)); - cell->addChild(sprite); - offsetX += listItemSize.width; - } - } - ++nCount; - } -} - -void CCTextureWatcher::CCListView_didClickCellAtRow(CCListView *listView, CCListViewProtrolData *data) -{ - -} - void CCTextureWatcher::CCListView_didScrollToRow(CCListView *listView, CCListViewProtrolData *data) { diff --git a/cocos2dx/extensions/CCTextureWatcher/CCTextureWatcher.h b/cocos2dx/extensions/CCTextureWatcher/CCTextureWatcher.h index 5fa7b5ecad..0f9125f901 100644 --- a/cocos2dx/extensions/CCTextureWatcher/CCTextureWatcher.h +++ b/cocos2dx/extensions/CCTextureWatcher/CCTextureWatcher.h @@ -1,53 +1,53 @@ -#ifndef __CCMEMLAYER_H__ -#define __CCMEMLAYER_H__ - -#include "extensions/CCListView/CCListView.h" - -namespace cocos2d { class CCDictionary; } -namespace cocos2d { class CCLabelTTF; } -namespace cocos2d { class CCMenu; } -namespace cocos2d { class CCLayerColor; } - - -NS_CC_EXT_BEGIN - -class CC_DLL CCTextureWatcher :public CCObject, public CCListViewDelegate -{ -private: - CCTextureWatcher(); -public: - virtual ~CCTextureWatcher(); - - static CCTextureWatcher* sharedTextureWatcher(); - static void purgeTextureWatcher(); - void setDisplayWatcher(bool bDisplayWatcher); - void fresh(void); -protected: - void actionFresh(CCObject* object); - void actionHide(CCObject* object); - void hide(void); - void dovisit(void); - static void visit(void* pSender); -protected: - virtual void CCListView_numberOfCells(CCListView *listView, CCListViewProtrolData *data); - virtual void CCListView_cellForRow(CCListView *listView, CCListViewProtrolData *data); - virtual void CCListView_didClickCellAtRow(CCListView *listView, CCListViewProtrolData *data); - virtual void CCListView_didScrollToRow(CCListView *listView, CCListViewProtrolData *data); -private: - bool m_bDisplayWatcher; - bool m_bFresh; - bool m_bHide; - CCDictionary *m_pTextures; - char *m_pszString; - int m_nCurrnetPage; - int m_nTotalPage; - CCLabelTTF *m_labelHide; - CCLabelTTF *m_labelPage; - CCMenu *m_menuHide; - CCLayerColor *m_pLayer; - CCListView *m_pList; -}; - -NS_CC_EXT_END - -#endif +#ifndef __CCMEMLAYER_H__ +#define __CCMEMLAYER_H__ + +#include "extensions/CCListView/CCListView.h" + +namespace cocos2d { class CCDictionary; } +namespace cocos2d { class CCLabelTTF; } +namespace cocos2d { class CCMenu; } +namespace cocos2d { class CCLayerColor; } + + +NS_CC_EXT_BEGIN + +class CC_DLL CCTextureWatcher :public CCObject, public CCListViewDelegate +{ +private: + CCTextureWatcher(); +public: + virtual ~CCTextureWatcher(); + + static CCTextureWatcher* sharedTextureWatcher(); + static void purgeTextureWatcher(); + void setDisplayWatcher(bool bDisplayWatcher); + void fresh(void); +protected: + void actionFresh(CCObject* object); + void actionHide(CCObject* object); + void hide(void); + void dovisit(void); + static void visit(void* pSender); +protected: + virtual void CCListView_numberOfCells(CCListView *listView, CCListViewProtrolData *data); + virtual void CCListView_cellForRow(CCListView *listView, CCListViewProtrolData *data); + virtual void CCListView_didClickCellAtRow(CCListView *listView, CCListViewProtrolData *data); + virtual void CCListView_didScrollToRow(CCListView *listView, CCListViewProtrolData *data); +private: + bool m_bDisplayWatcher; + bool m_bFresh; + bool m_bHide; + CCDictionary *m_pTextures; + char *m_pszString; + int m_nCurrnetPage; + int m_nTotalPage; + CCLabelTTF *m_labelHide; + CCLabelTTF *m_labelPage; + CCMenu *m_menuHide; + CCLayerColor *m_pLayer; + CCListView *m_pList; +}; + +NS_CC_EXT_END + +#endif diff --git a/cocos2dx/platform/android/CCCommon.cpp b/cocos2dx/platform/android/CCCommon.cpp index 89879a29f9..eb5e82dd8b 100644 --- a/cocos2dx/platform/android/CCCommon.cpp +++ b/cocos2dx/platform/android/CCCommon.cpp @@ -52,7 +52,7 @@ void CCMessageBox(const char * pszMsg, const char * pszTitle) void CCLuaLog(const char * pszFormat) { - CCLog(pszFormat); + CCLog(pszFormat); } NS_CC_END diff --git a/cocos2dx/platform/ios/CCCommon.mm b/cocos2dx/platform/ios/CCCommon.mm index 9a476fc9ba..a2c2a7d17f 100644 --- a/cocos2dx/platform/ios/CCCommon.mm +++ b/cocos2dx/platform/ios/CCCommon.mm @@ -60,7 +60,7 @@ void CCMessageBox(const char * pszMsg, const char * pszTitle) void CCLuaLog(const char * pszFormat) { - CCLog(pszFormat); + CCLog(pszFormat); } NS_CC_END diff --git a/cocos2dx/platform/win32/CCCommon.cpp b/cocos2dx/platform/win32/CCCommon.cpp index f8b4f0cf97..3133f44abb 100644 --- a/cocos2dx/platform/win32/CCCommon.cpp +++ b/cocos2dx/platform/win32/CCCommon.cpp @@ -51,9 +51,9 @@ void CCMessageBox(const char * pszMsg, const char * pszTitle) MessageBoxA(NULL, pszMsg, pszTitle, MB_OK); } -void CCLuaLog(const char * pszFormat) -{ - CCLog(pszFormat); +void CCLuaLog(const char * pszFormat) +{ + CCLog(pszFormat); } NS_CC_END diff --git a/cocos2dx/platform/win32/CCEGLView.h b/cocos2dx/platform/win32/CCEGLView.h index ad05e5d99b..a6ede92c5a 100644 --- a/cocos2dx/platform/win32/CCEGLView.h +++ b/cocos2dx/platform/win32/CCEGLView.h @@ -37,25 +37,23 @@ class CCEGL; class CC_DLL CCEGLView : public CCEGLViewProtocol { public: - CCEGLView(); virtual ~CCEGLView(); - bool isOpenGLReady(); - void end(); - - void swapBuffers(); - - bool canSetContentScaleFactor(); - void setContentScaleFactor(float contentScaleFactor); + /* override functions */ + virtual bool isOpenGLReady(); + virtual void end(); + virtual void swapBuffers(); + virtual bool canSetContentScaleFactor(); + virtual void setContentScaleFactor(float contentScaleFactor); virtual void setFrameSize(float width, float height); + virtual void setIMEKeyboardState(bool bOpen); +private: virtual bool Create(LPCTSTR pTitle, int w, int h); +public: virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam); - - void setIMEKeyboardState(bool bOpen); - // win32 platform function HWND getHWnd(); void resize(int width, int height); @@ -65,7 +63,6 @@ public: void setAccelerometerKeyHook( LPFN_ACCELEROMETER_KEYHOOK lpfnAccelerometerKeyHook ); // static function - /** @brief get the shared main open gl window */ diff --git a/tests/proj.win32/main.cpp b/tests/proj.win32/main.cpp index e8f14f2568..1dfe69ec15 100644 --- a/tests/proj.win32/main.cpp +++ b/tests/proj.win32/main.cpp @@ -17,7 +17,7 @@ int APIENTRY _tWinMain(HINSTANCE hInstance, CCEGLView& eglView = CCEGLView::sharedOpenGLView(); eglView.setViewName("Hello Tests"); eglView.setFrameSize(480, 320); - // set the design resolution screen size, if you want to use Design Resoulution scaled to current screen, please uncomment next line. + // set the design resolution screen size, if you want to use Design Resoulution scaled to current screen, please uncomment next line. // eglView.setDesignResolutionSize(480, 320); return CCApplication::sharedApplication().run(); diff --git a/tests/tests/ExtensionsTest/ExtensionsTest.cpp b/tests/tests/ExtensionsTest/ExtensionsTest.cpp index cd3b34b7c2..fb5c21ccdb 100644 --- a/tests/tests/ExtensionsTest/ExtensionsTest.cpp +++ b/tests/tests/ExtensionsTest/ExtensionsTest.cpp @@ -5,12 +5,19 @@ enum { - MAX_COUNT = 3, LINE_SPACE = 40, kItemTagBasic = 1000, }; -static const std::string testsName[MAX_COUNT] = +enum +{ + TEST_NOTIFICATIONCENTER = 0, + TEST_CCCONTROLBUTTON, + TEST_TEXTUREWATCHER, + TEST_MAX_COUNT +}; + +static const std::string testsName[TEST_MAX_COUNT] = { "NotificationCenterTest", "CCControlButtonTest", @@ -32,7 +39,7 @@ void ExtensionsMainLayer::onEnter() pMenu->setPosition( CCPointZero ); CCMenuItemFont::setFontName("Arial"); CCMenuItemFont::setFontSize(24); - for (int i = 0; i < MAX_COUNT; ++i) + for (int i = 0; i < TEST_MAX_COUNT; ++i) { CCMenuItemFont* pItem = CCMenuItemFont::itemWithString(testsName[i].c_str(), this, menu_selector(ExtensionsMainLayer::menuCallback)); @@ -50,17 +57,19 @@ void ExtensionsMainLayer::menuCallback(CCObject* pSender) switch (nIndex) { - case 0: - runNotificationCenterTest(); + case TEST_NOTIFICATIONCENTER: + { + runNotificationCenterTest(); + } break; - case 1: + case TEST_CCCONTROLBUTTON: { CCControlSceneManager* pManager = CCControlSceneManager::sharedControlSceneManager(); CCScene* pScene = pManager->currentControlScene(); CCDirector::sharedDirector()->replaceScene(pScene); } break; - case 2: + case TEST_TEXTUREWATCHER: { static bool s_bOpened = false; s_bOpened = !s_bOpened;