Merge pull request #2704 from simpliplant/develop

Hardware keyboard support
This commit is contained in:
minggo 2013-06-11 06:48:33 -07:00
commit 9404f3e0ce
20 changed files with 350 additions and 9 deletions

1
.gitignore vendored
View File

@ -80,6 +80,7 @@ Device-Release/
# Ignore vim swaps
*.swp
*.swo
# Ignore config files in javascript bindings generator
tools/tojs/user.cfg

View File

@ -64,7 +64,9 @@ THE SOFTWARE.
#include "platform/CCImage.h"
#include "CCEGLView.h"
#include "CCConfiguration.h"
#ifdef KEYBOARD_SUPPORT
#include "keyboard_dispatcher/CCKeyboardDispatcher.h"
#endif
/**
@ -155,6 +157,11 @@ bool CCDirector::init(void)
m_pTouchDispatcher = new CCTouchDispatcher();
m_pTouchDispatcher->init();
#ifdef KEYBOARD_SUPPORT
// KeyboardDispatcher
m_pKeyboardDispatcher = new CCKeyboardDispatcher();
#endif
// KeypadDispatcher
m_pKeypadDispatcher = new CCKeypadDispatcher();
@ -181,6 +188,9 @@ CCDirector::~CCDirector(void)
CC_SAFE_RELEASE(m_pScheduler);
CC_SAFE_RELEASE(m_pActionManager);
CC_SAFE_RELEASE(m_pTouchDispatcher);
#ifdef KEYBOARD_SUPPORT
CC_SAFE_RELEASE(m_pKeyboardDispatcher);
#endif
CC_SAFE_RELEASE(m_pKeypadDispatcher);
CC_SAFE_DELETE(m_pAccelerometer);
@ -961,6 +971,20 @@ CCTouchDispatcher* CCDirector::getTouchDispatcher()
return m_pTouchDispatcher;
}
#ifdef KEYBOARD_SUPPORT
void CCDirector::setKeyboardDispatcher(CCKeyboardDispatcher* pKeyboardDispatcher)
{
CC_SAFE_RETAIN(pKeyboardDispatcher);
CC_SAFE_RELEASE(m_pKeyboardDispatcher);
m_pKeyboardDispatcher = pKeyboardDispatcher;
}
CCKeyboardDispatcher* CCDirector::getKeyboardDispatcher()
{
return m_pKeyboardDispatcher;
}
#endif
void CCDirector::setKeypadDispatcher(CCKeypadDispatcher* pKeypadDispatcher)
{
CC_SAFE_RETAIN(pKeypadDispatcher);

View File

@ -71,6 +71,9 @@ class CCNode;
class CCScheduler;
class CCActionManager;
class CCTouchDispatcher;
#ifdef KEYBOARD_SUPPORT
class CCKeyboardDispatcher;
#endif
class CCKeypadDispatcher;
class CCAccelerometer;
@ -322,6 +325,12 @@ public:
*/
CC_PROPERTY(CCTouchDispatcher*, m_pTouchDispatcher, TouchDispatcher);
#ifdef KEYBOARD_SUPPORT
/** CCKeyboardDispatcher associated with this director
@since v?.?
*/
CC_PROPERTY(CCKeyboardDispatcher*, m_pKeyboardDispatcher, KeyboardDispatcher);
#endif
/** CCKeypadDispatcher associated with this director
@since v2.0
*/

View File

@ -0,0 +1,73 @@
/****************************************************************************
Copyright (c) 2010 cocos2d-x.org
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "CCKeyboardDispatcher.h"
#include "support/data_support/ccCArray.h"
NS_CC_BEGIN
//------------------------------------------------------------------
//
// CCKeyboardDispatcher
//
//------------------------------------------------------------------
CCKeyboardDispatcher::CCKeyboardDispatcher()
: m_keyPressDelegate(NULL)
, m_keyReleaseDelegate(NULL)
{
}
CCKeyboardDispatcher::~CCKeyboardDispatcher()
{
}
void CCKeyboardDispatcher::setKeyPressDelegate(CCKeyboardDelegate delegate)
{
m_keyPressDelegate = delegate;
}
void CCKeyboardDispatcher::setKeyReleaseDelegate(CCKeyboardDelegate delegate)
{
m_keyReleaseDelegate = delegate;
}
bool CCKeyboardDispatcher::dispatchKeyboardEvent(int keyCode, bool pressed)
{
if (m_keyPressDelegate != NULL && pressed)
{
m_keyPressDelegate(keyCode);
}
else if (m_keyReleaseDelegate != NULL)
{
m_keyReleaseDelegate(keyCode);
}
else
{
return false;
}
return true;
}
NS_CC_END

View File

@ -0,0 +1,76 @@
/****************************************************************************
Copyright (c) 2010 cocos2d-x.org
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#ifndef __CCKEYBOARD_DISPATCHER_H__
#define __CCKEYBOARD_DISPATCHER_H__
#include <vector>
#include <functional>
#include "cocoa/CCArray.h"
NS_CC_BEGIN
/**
* @addtogroup input
* @{
*/
typedef std::function<void(int)> CCKeyboardDelegate;
/**
@class CCKeyboardDispatcher
@brief Dispatch the keypad message from the phone
*/
class CC_DLL CCKeyboardDispatcher : public CCObject
{
public:
CCKeyboardDispatcher();
~CCKeyboardDispatcher();
/**
@brief set delagate to key press event
*/
void setKeyPressDelegate(CCKeyboardDelegate delegate);
/**
@brief set delagate to key release event
*/
void setKeyReleaseDelegate(CCKeyboardDelegate delegate);
/**
@brief dispatch the key stroke event
*/
bool dispatchKeyboardEvent(int keyCode, bool pressed);
protected:
CCKeyboardDelegate m_keyPressDelegate;
CCKeyboardDelegate m_keyReleaseDelegate;
};
// end of input group
/// @}
NS_CC_END
#endif //__CCKEYBOARD_DISPATCHER_H__

View File

@ -38,6 +38,9 @@ THE SOFTWARE.
#include "support/TransformUtils.h"
// extern
#include "kazmath/GL/matrix.h"
#ifdef KEYBOARD_SUPPORT
#include "keyboard_dispatcher/CCKeyboardDispatcher.h"
#endif
NS_CC_BEGIN
@ -45,6 +48,9 @@ NS_CC_BEGIN
CCLayer::CCLayer()
: m_bTouchEnabled(false)
, m_bAccelerometerEnabled(false)
#ifdef KEYBOARD_SUPPORT
, m_bKeyboardEnabled(false)
#endif
, m_bKeypadEnabled(false)
, m_pScriptTouchHandlerEntry(NULL)
, m_pScriptKeypadHandlerEntry(NULL)
@ -273,6 +279,34 @@ void CCLayer::unregisterScriptAccelerateHandler(void)
CC_SAFE_RELEASE_NULL(m_pScriptAccelerateHandlerEntry);
}
#ifdef KEYBOARD_SUPPORT
/// isKeyboardEnabled getter
bool CCLayer::isKeyboardEnabled()
{
return m_bKeyboardEnabled;
}
/// isKeyboardEnabled setter
void CCLayer::setKeyboardEnabled(bool enabled)
{
if (enabled != m_bKeyboardEnabled)
{
m_bKeyboardEnabled = enabled;
CCDirector* pDirector = CCDirector::sharedDirector();
if (enabled)
{
pDirector->getKeyboardDispatcher()->setKeyPressDelegate(std::bind(&CCLayer::keyPressed, this, std::placeholders::_1));
pDirector->getKeyboardDispatcher()->setKeyReleaseDelegate(std::bind(&CCLayer::keyReleased, this, std::placeholders::_1));
}
else
{
pDirector->getKeyboardDispatcher()->setKeyPressDelegate(NULL);
pDirector->getKeyboardDispatcher()->setKeyReleaseDelegate(NULL);
}
}
}
#endif
/// isKeypadEnabled getter
bool CCLayer::isKeypadEnabled()
{

View File

@ -130,10 +130,16 @@ public:
virtual void setAccelerometerEnabled(bool value);
virtual void setAccelerometerInterval(double interval);
/** whether or not it will receive keypad events
/** whether or not it will receive keyboard or keypad events
You can enable / disable accelerometer events with this property.
it's new in cocos2d-x
*/
#ifdef KEYBOARD_SUPPORT
virtual bool isKeyboardEnabled();
virtual void setKeyboardEnabled(bool value);
virtual void keyPressed(int keyCode) {};
virtual void keyReleased(int keyCode) {};
#endif
virtual bool isKeypadEnabled();
virtual void setKeypadEnabled(bool value);
@ -151,6 +157,9 @@ public:
protected:
bool m_bTouchEnabled;
bool m_bAccelerometerEnabled;
#ifdef KEYBOARD_SUPPORT
bool m_bKeyboardEnabled;
#endif
bool m_bKeypadEnabled;
private:

View File

@ -13,6 +13,9 @@
#include "touch_dispatcher/CCTouch.h"
#include "touch_dispatcher/CCTouchDispatcher.h"
#include "text_input_node/CCIMEDispatcher.h"
#ifdef KEYBOARD_SUPPORT
#include "keyboard_dispatcher/CCKeyboardDispatcher.h"
#endif
PFNGLGENFRAMEBUFFERSEXTPROC glGenFramebuffersEXT = NULL;
PFNGLDELETEFRAMEBUFFERSEXTPROC glDeleteFramebuffersEXT = NULL;
@ -154,6 +157,23 @@ int closeEventHandle() {
return GL_TRUE;
}
#ifdef KEYBOARD_SUPPORT
void GLFWCALL keyboardEventHandle(int keyCode, int action)
{
CCKeyboardDispatcher *kbDisp = CCDirector::sharedDirector()->getKeyboardDispatcher();
switch (action)
{
case GLFW_PRESS:
kbDisp->dispatchKeyboardEvent(keyCode, true);
break;
case GLFW_RELEASE:
kbDisp->dispatchKeyboardEvent(keyCode, false);
break;
}
}
#endif
void CCEGLView::setFrameSize(float width, float height)
{
bool eResult = false;
@ -228,6 +248,10 @@ void CCEGLView::setFrameSize(float width, float height)
glfwSetMouseButtonCallback(mouseButtonEventHandle);
//register the glfw mouse pos event
glfwSetMousePosCallback(mousePosEventHandle);
#ifdef KEYBOARD_SUPPORT
//register the glfw keyboard event
glfwSetKeyCallback(keyboardEventHandle);
#endif
glfwSetWindowCloseCallback(closeEventHandle);

View File

@ -31,6 +31,9 @@
#import "ccConfig.h"
#include "support/data_support/utlist.h"
#include "CCDirector.h"
#include "keyboard_dispatcher/CCKeyboardDispatcher.h"
//NS_CC_BEGIN;
static CCEventDispatcher *sharedDispatcher = nil;
@ -502,7 +505,9 @@ static int eventQueueCount;
{
if( dispatchEvents_ ) {
tListEntry *entry, *tmp;
cocos2d::CCKeyboardDispatcher *kbDisp = cocos2d::CCDirector::sharedDirector()->getKeyboardDispatcher();
kbDisp->dispatchKeyboardEvent(event.keyCode, true);
DL_FOREACH_SAFE( keyboardDelegates_, entry, tmp ) {
if ( entry->flags & kCCImplementsKeyDown ) {
void *swallows = [entry->delegate performSelector:@selector(ccKeyDown:) withObject:event];
@ -518,6 +523,8 @@ static int eventQueueCount;
if( dispatchEvents_ ) {
tListEntry *entry, *tmp;
cocos2d::CCKeyboardDispatcher *kbDisp = cocos2d::CCDirector::sharedDirector()->getKeyboardDispatcher();
kbDisp->dispatchKeyboardEvent(event.keyCode, true);
DL_FOREACH_SAFE( keyboardDelegates_, entry, tmp ) {
if ( entry->flags & kCCImplementsKeyUp ) {
void *swallows = [entry->delegate performSelector:@selector(ccKeyUp:) withObject:event];

View File

@ -42,6 +42,7 @@ SOURCES = ../actions/CCAction.cpp \
../effects/CCGrid.cpp \
../keypad_dispatcher/CCKeypadDelegate.cpp \
../keypad_dispatcher/CCKeypadDispatcher.cpp \
../keyboard_dispatcher/CCKeyboardDispatcher.cpp \
../label_nodes/CCLabelAtlas.cpp \
../label_nodes/CCLabelBMFont.cpp \
../label_nodes/CCLabelTTF.cpp \

View File

@ -11,7 +11,7 @@ CCFLAGS += -MMD -Werror -Wno-deprecated-declarations -fPIC
CXXFLAGS += -MMD -Werror -Wno-deprecated-declarations -fPIC -std=gnu++0x
ARFLAGS = cr
DEFINES += -DLINUX
DEFINES += -DLINUX -DKEYBOARD_SUPPORT
ifdef USE_BOX2D
DEFINES += -DCC_ENABLE_BOX2D_INTEGRATION=1

View File

@ -1 +1 @@
5b8ea64c349dcc515312773df1d8d0d5d20abfbe
4f421e1ed8425f53876db5971e3deac238edb4d3

View File

@ -48,6 +48,10 @@
namespace cocos2d {
#ifndef OF
#define OF _Z_OF
#endif
#if defined(USE_FILE32API)
#define fopen64 fopen
#define ftello64 ftell

View File

@ -60,10 +60,10 @@ char* _readFile (const char* path, int* length) {
fseek(file, 0, SEEK_SET);
data = MALLOC(char, *length);
fread(data, 1, *length, file);
size_t ret = fread(data, 1, *length, file);
fclose(file);
return data;
}
}} // namespace cocos2d { namespace extension {
}} // namespace cocos2d { namespace extension {

View File

@ -180,9 +180,11 @@ void TestSearchPath::onEnter()
FILE* fp = fopen(fileName.c_str(), "wb");
if (fp)
{
fwrite(szBuf, 1, strlen(szBuf), fp);
size_t ret = fwrite(szBuf, 1, strlen(szBuf), fp);
CCAssert(ret == 0, "fwrite function returned nonzero value");
fclose(fp);
CCLog("Writing file to writable path succeed.");
if (ret == 0)
CCLog("Writing file to writable path succeed.");
}
searchPaths.insert(searchPaths.begin(), writablePath);

View File

@ -0,0 +1,42 @@
#include "KeyboardTest.h"
KeyboardTest::KeyboardTest()
{
CCSize s = CCDirector::sharedDirector()->getWinSize();
CCLabelTTF* label = CCLabelTTF::create("Keyboard Test", "Arial", 28);
addChild(label, 0);
label->setPosition( ccp(s.width/2, s.height-50) );
setKeyboardEnabled(true);
// create a label to display the tip string
m_pLabel = CCLabelTTF::create("Please press any key and see console log...", "Arial", 22);
m_pLabel->setPosition(ccp(s.width / 2, s.height / 2));
addChild(m_pLabel, 0);
m_pLabel->retain();
}
KeyboardTest::~KeyboardTest()
{
m_pLabel->release();
}
void KeyboardTest::keyPressed(int keyCode)
{
CCLog("Key with keycode %d pressed", keyCode);
}
void KeyboardTest::keyReleased(int keyCode)
{
CCLog("Key with keycode %d released", keyCode);
}
void KeyboardTestScene::runThisTest()
{
CCLayer* pLayer = new KeyboardTest();
addChild(pLayer);
CCDirector::sharedDirector()->replaceScene(this);
pLayer->release();
}

View File

@ -0,0 +1,26 @@
#ifndef _KEYBOARD_TEST_H_
#define _KEYBOARD_TEST_H_
#include "cocos2d.h"
#include "../testBasic.h"
class KeyboardTest : public CCLayer
{
public:
KeyboardTest();
~KeyboardTest();
virtual void keyPressed(int keyCode);
virtual void keyReleased(int keyCode);
private:
CCLabelTTF* m_pLabel;
};
class KeyboardTestScene : public TestScene
{
public:
virtual void runThisTest();
};
#endif

View File

@ -47,6 +47,9 @@ struct {
{ "FileUtilsTest", []() { return new FileUtilsTestScene(); } },
{ "FontTest", []() { return new FontTestScene(); } },
{ "IntervalTest", [](){return new IntervalTestScene(); } },
#ifdef KEYBOARD_SUPPORT
{ "KeyboardTest", []() { return new KeyboardTestScene(); } },
#endif
#if (CC_TARGET_PLATFORM != CC_PLATFORM_BADA)
{ "KeypadTest", []() { return new KeypadTestScene(); } },
#endif

View File

@ -29,6 +29,9 @@
#include "EffectsAdvancedTest/EffectsAdvancedTest.h"
#include "AccelerometerTest/AccelerometerTest.h"
#include "KeypadTest/KeypadTest.h"
#ifdef KEYBOARD_SUPPORT
#include "KeyboardTest/KeyboardTest.h"
#endif
#include "PerformanceTest/PerformanceTest.h"
#include "ZwoptexTest/ZwoptexTest.h"
#include "CocosDenshionTest/CocosDenshionTest.h"

View File

@ -1,5 +1,7 @@
EXECUTABLE = TestCpp
DEFINES += -DKEYBOARD_SUPPORT
SOURCES = ../Classes/AccelerometerTest/AccelerometerTest.cpp \
../Classes/ActionManagerTest/ActionManagerTest.cpp \
../Classes/ActionsEaseTest/ActionsEaseTest.cpp \
@ -52,6 +54,7 @@ SOURCES = ../Classes/AccelerometerTest/AccelerometerTest.cpp \
../Classes/ExtensionsTest/NetworkTest/HttpClientTest.cpp \
../Classes/FontTest/FontTest.cpp \
../Classes/IntervalTest/IntervalTest.cpp \
../Classes/KeyboardTest/KeyboardTest.cpp \
../Classes/KeypadTest/KeypadTest.cpp \
../Classes/LabelTest/LabelTest.cpp \
../Classes/LayerTest/LayerTest.cpp \