closed #2416, Add social plugin : SocialNd91 & add sample code for social plugins.

This commit is contained in:
zhangbin 2013-07-23 14:29:11 +08:00
parent e35d06ae26
commit 9edc1dff80
15 changed files with 782 additions and 34 deletions

View File

@ -0,0 +1,241 @@
/****************************************************************************
Copyright (c) 2013 cocos2d-x.org
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
package org.cocos2dx.plugin;
import java.util.Hashtable;
import com.nd.commplatform.NdCallbackListener;
import com.nd.commplatform.NdCommplatform;
import com.nd.commplatform.NdErrorCode;
import com.nd.commplatform.NdMiscCallbackListener.OnLoginProcessListener;
import com.nd.commplatform.entry.NdAchieveUnlockInfo;
import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
public class SocialNd91 implements InterfaceSocial {
private static final String LOG_TAG = "UserNd91";
private static Activity mContext = null;
private static SocialNd91 mNd91 = null;
private static boolean bDebug = false;
private static NdCallbackListener<Object> submitListener = null;
private static NdCallbackListener<Object> unlockListener = null;
protected static void LogE(String msg, Exception e) {
Log.e(LOG_TAG, msg, e);
e.printStackTrace();
}
protected static void LogD(String msg) {
if (bDebug) {
Log.d(LOG_TAG, msg);
}
}
public SocialNd91(Context context) {
mContext = (Activity) context;
mNd91 = this;
submitListener = new NdCallbackListener<Object>(){
@Override
public void callback(int arg0, Object arg1) {
int nRet = SocialWrapper.SOCIAL_SUBMITSCORE_FAILED;
String msg = "Unknow Error";
switch (arg0) {
case NdCommplatform.SCORE_SUBMIT_SUCCESS:
nRet = SocialWrapper.SOCIAL_SUBMITSCORE_SUCCESS;
msg = "Submit Success";
break;
case NdCommplatform.SCORE_SAVE_LOCAL:
nRet = SocialWrapper.SOCIAL_SUBMITSCORE_FAILED;
msg = "Score saved locally";
break;
case NdCommplatform.LEADERBOARD_NOT_EXIST:
nRet = SocialWrapper.SOCIAL_SUBMITSCORE_FAILED;
msg = "The leaderboard not exist";
break;
default:
nRet = SocialWrapper.SOCIAL_SUBMITSCORE_FAILED;
break;
}
SocialWrapper.onSocialResult(mNd91, nRet, msg);
}
};
unlockListener = new NdCallbackListener<Object>(){
@Override
public void callback(int arg0, Object arg1) {
int nRet = SocialWrapper.SOCIAL_UNLOCKACH_FAILED;
String msg = "Unknow Error";
switch (arg0) {
case NdErrorCode.ND_COM_PLATFORM_SUCCESS:
nRet = SocialWrapper.SOCIAL_UNLOCKACH_SUCCESS;
msg = "Unlock Success";
break;
case NdErrorCode.ND_COM_PLATFORM_ERROR_SERVER_RETURN_ERROR:
nRet = SocialWrapper.SOCIAL_UNLOCKACH_FAILED;
msg = "Server return error";
break;
case NdErrorCode.ND_COM_PLATFORM_ERROR_ACHIEVEMENT_NO_EXIST:
nRet = SocialWrapper.SOCIAL_UNLOCKACH_FAILED;
msg = "Achievement not exist";
break;
default:
nRet = SocialWrapper.SOCIAL_UNLOCKACH_FAILED;
break;
}
SocialWrapper.onSocialResult(mNd91, nRet, msg);
}
};
}
@Override
public void configDeveloperInfo(Hashtable<String, String> cpInfo) {
LogD("initDeveloperInfo invoked " + cpInfo.toString());
final Hashtable<String, String> curCPInfo = cpInfo;
PluginWrapper.runOnMainThread(new Runnable() {
@Override
public void run() {
try {
String appId = curCPInfo.get("Nd91AppId");
String appKey = curCPInfo.get("Nd91AppKey");
int id = Integer.parseInt(appId);
String orientation = curCPInfo.get("Nd91Orientation");
Nd91Wrapper.initSDK(mContext, id, appKey, orientation);
} catch (Exception e) {
LogE("Developer info is wrong!", e);
}
}
});
}
@Override
public void submitScore(String leaderboardID, long score) {
final String curID = leaderboardID;
final long curScore = score;
PluginWrapper.runOnMainThread(new Runnable() {
@Override
public void run() {
NdCommplatform.getInstance().ndSubmitScore(curID, (int) curScore, null, (Activity) mContext, submitListener);
}
});
}
@Override
public void showLeaderboard(String leaderboardID) {
final String curID = leaderboardID;
PluginWrapper.runOnMainThread(new Runnable() {
@Override
public void run() {
if (Nd91Wrapper.isLogined()) {
NdCommplatform.getInstance().ndOpenLeaderBoard(mContext, curID, 0);
} else {
Nd91Wrapper.userLogin(mContext, new OnLoginProcessListener() {
@Override
public void finishLoginProcess(int code) {
if (code == NdErrorCode.ND_COM_PLATFORM_SUCCESS) {
NdCommplatform.getInstance().ndOpenLeaderBoard(mContext, curID, 0);
} else {
LogD("User should login first");
}
}
});
}
}
});
}
@Override
public void unlockAchievement(Hashtable<String, String> achInfo) {
final Hashtable<String, String> curInfo = achInfo;
PluginWrapper.runOnMainThread(new Runnable() {
@Override
public void run() {
try {
String achID = curInfo.get("AchievementID");
if (achID == null || TextUtils.isEmpty(achID))
{
SocialWrapper.onSocialResult(mNd91, SocialWrapper.SOCIAL_UNLOCKACH_FAILED, "Achievement info error");
return;
}
String displayText = curInfo.get("NDDisplayText");
String strScore = curInfo.get("NDScore");
long score = Long.parseLong(strScore);
NdAchieveUnlockInfo info = new NdAchieveUnlockInfo();
info.setAchievementId(achID);
info.setCurrentValue(score);
info.setDisplayText(displayText);
NdCommplatform.getInstance().ndUnlockAchievement(info, mContext, unlockListener);
} catch (Exception e) {
LogE("Unknown Error!", e);
}
}
});
}
@Override
public void showAchievements() {
PluginWrapper.runOnMainThread(new Runnable() {
@Override
public void run() {
if (Nd91Wrapper.isLogined()) {
NdCommplatform.getInstance().ndOpenAchievement(mContext, 0);
} else {
Nd91Wrapper.userLogin(mContext, new OnLoginProcessListener() {
@Override
public void finishLoginProcess(int code) {
if (code == NdErrorCode.ND_COM_PLATFORM_SUCCESS) {
NdCommplatform.getInstance().ndOpenAchievement(mContext, 0);
} else {
LogD("User should login first");
}
}
});
}
}
});
}
@Override
public void setDebugMode(boolean debug) {
bDebug = debug;
}
@Override
public String getSDKVersion() {
return Nd91Wrapper.getSDKVersion();
}
@Override
public String getPluginVersion() {
return Nd91Wrapper.getPluginVersion();
}
}

View File

@ -5,6 +5,7 @@
#include "TestIAPScene.h"
#include "TestIAPOnlineScene.h"
#include "TestUserScene.h"
#include "TestSocialScene.h"
USING_NS_CC;
@ -16,6 +17,7 @@ std::string g_testCases[] = {
"Test IAP",
"Test IAP Online",
"Test User",
"Test Social",
#endif
};
@ -57,22 +59,22 @@ bool HelloWorld::init()
"CloseSelected.png",
CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,
pCloseItem->setPosition(Point(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,
origin.y + pCloseItem->getContentSize().height/2));
// create menu, it's an autorelease object
Menu* pMenu = Menu::create(pCloseItem, NULL);
pMenu->setPosition(PointZero);
pMenu->setPosition(Point::ZERO);
this->addChild(pMenu, 1);
Point beginPos = ccp(origin.x + visibleSize.width / 2, origin.y + visibleSize.height - 50);
Point beginPos = Point(origin.x + visibleSize.width / 2, origin.y + visibleSize.height - 50);
float step = 60.0f;
int nCaseCount = sizeof(g_testCases) / sizeof(std::string);
for (int i = 0; i < nCaseCount; ++i) {
std::string caseName = g_testCases[i];
MenuItemFont *pItem = MenuItemFont::create(caseName.c_str(), CC_CALLBACK_1(HelloWorld::menuCallback, this));
pItem->setTag(i);
pItem->setPosition(ccp(beginPos.x, beginPos.y - i * step));
pItem->setPosition(Point(beginPos.x, beginPos.y - i * step));
pMenu->addChild(pItem);
}
return true;
@ -101,6 +103,8 @@ void HelloWorld::menuCallback(Object* pSender)
case 5:
newScene = TestUser::scene();
break;
case 6:
newScene = TestSocial::scene();
default:
break;
}

View File

@ -103,7 +103,7 @@ bool TestAds::init()
// create menu, it's an autorelease object
Menu* pMenu = Menu::create(pBackItem, NULL);
pMenu->setPosition( PointZero );
pMenu->setPosition( Point::ZERO );
LabelTTF* label1 = LabelTTF::create("ShowAds", "Arial", 24);
MenuItemLabel* pItemShow = MenuItemLabel::create(label1, CC_CALLBACK_1(TestAds::testShow, this));

View File

@ -102,7 +102,7 @@ bool TestAnalytics::init()
// create menu, it's an autorelease object
Menu* pMenu = Menu::create(pBackItem, NULL);
pMenu->setPosition( PointZero );
pMenu->setPosition( Point::ZERO );
this->addChild(pMenu, 1);
float yPos = 0;

View File

@ -87,7 +87,7 @@ bool TestIAP::init()
// create menu, it's an autorelease object
Menu* pMenu = Menu::create(pBackItem, NULL);
pMenu->setPosition( PointZero );
pMenu->setPosition( Point::ZERO );
this->addChild(pMenu, 1);
Point posStep = ccp(220, -150);

View File

@ -81,21 +81,21 @@ bool TestIAPOnline::init()
// you may modify it.
EGLView* pEGLView = EGLView::getInstance();
Point posBR = ccp(pEGLView->getVisibleOrigin().x + pEGLView->getVisibleSize().width, pEGLView->getVisibleOrigin().y);
Point posTL = ccp(pEGLView->getVisibleOrigin().x, pEGLView->getVisibleOrigin().y + pEGLView->getVisibleSize().height);
Point posBR = Point(pEGLView->getVisibleOrigin().x + pEGLView->getVisibleSize().width, pEGLView->getVisibleOrigin().y);
Point posTL = Point(pEGLView->getVisibleOrigin().x, pEGLView->getVisibleOrigin().y + pEGLView->getVisibleSize().height);
// add a "close" icon to exit the progress. it's an autorelease object
MenuItemFont *pBackItem = MenuItemFont::create("Back", CC_CALLBACK_1(TestIAPOnline::menuBackCallback, this));
Size backSize = pBackItem->getContentSize();
pBackItem->setPosition(ccpAdd(posBR, ccp(- backSize.width / 2, backSize.height / 2)));
pBackItem->setPosition(posBR + Point(- backSize.width / 2, backSize.height / 2));
// create menu, it's an autorelease object
Menu* pMenu = Menu::create(pBackItem, NULL);
pMenu->setPosition( PointZero );
pMenu->setPosition( Point::ZERO );
this->addChild(pMenu, 1);
Point posStep = ccp(220, -150);
Point beginPos = ccpAdd(posTL, ccpMult(posStep, 0.5f));
Point posStep = Point(220, -150);
Point beginPos = posTL + (posStep * 0.5f);
int line = 0;
int row = 0;
for (int i = 0; i < sizeof(s_EventMenuItem)/sizeof(s_EventMenuItem[0]); i++) {
@ -103,13 +103,13 @@ bool TestIAPOnline::init()
CC_CALLBACK_1(TestIAPOnline::eventMenuCallback, this));
pMenu->addChild(pMenuItem, 0, s_EventMenuItem[i].tag);
Point pos = ccpAdd(beginPos, ccp(posStep.x * row, posStep.y * line));
Point pos = beginPos + Point(posStep.x * row, posStep.y * line);
Size itemSize = pMenuItem->getContentSize();
if ((pos.x + itemSize.width / 2) > posBR.x)
{
line += 1;
row = 0;
pos = ccpAdd(beginPos, ccp(posStep.x * row, posStep.y * line));
pos = beginPos + Point(posStep.x * row, posStep.y * line);
}
row += 1;
pMenuItem->setPosition(pos);

View File

@ -77,34 +77,34 @@ bool TestShare::init()
// you may modify it.
EGLView* pEGLView = EGLView::getInstance();
Point posBR = ccp(pEGLView->getVisibleOrigin().x + pEGLView->getVisibleSize().width, pEGLView->getVisibleOrigin().y);
Point posTL = ccp(pEGLView->getVisibleOrigin().x, pEGLView->getVisibleOrigin().y + pEGLView->getVisibleSize().height);
Point posBR = Point(pEGLView->getVisibleOrigin().x + pEGLView->getVisibleSize().width, pEGLView->getVisibleOrigin().y);
Point posTL = Point(pEGLView->getVisibleOrigin().x, pEGLView->getVisibleOrigin().y + pEGLView->getVisibleSize().height);
// add a "close" icon to exit the progress. it's an autorelease object
MenuItemFont *pBackItem = MenuItemFont::create("Back", CC_CALLBACK_1(TestShare::menuBackCallback, this));
Size backSize = pBackItem->getContentSize();
pBackItem->setPosition(ccpAdd(posBR, ccp(- backSize.width / 2, backSize.height / 2)));
pBackItem->setPosition(posBR + Point(- backSize.width / 2, backSize.height / 2));
// create menu, it's an autorelease object
Menu* pMenu = Menu::create(pBackItem, NULL);
pMenu->setPosition( PointZero );
pMenu->setPosition( Point::ZERO );
this->addChild(pMenu, 1);
Point posStep = ccp(150, -150);
Point beginPos = ccpAdd(posTL, ccpMult(posStep, 0.5f));
Point posStep = Point(150, -150);
Point beginPos = posTL + (posStep * 0.5f);
int line = 0;
int row = 0;
for (int i = 0; i < sizeof(s_EventMenuItem)/sizeof(s_EventMenuItem[0]); i++) {
MenuItemImage* pMenuItem = MenuItemImage::create(s_EventMenuItem[i].id.c_str(), s_EventMenuItem[i].id.c_str(), CC_CALLBACK_1(TestShare::eventMenuCallback, this));
pMenu->addChild(pMenuItem, 0, s_EventMenuItem[i].tag);
Point pos = ccpAdd(beginPos, ccp(posStep.x * row, posStep.y * line));
Point pos = beginPos + Point(posStep.x * row, posStep.y * line);
Size itemSize = pMenuItem->getContentSize();
if ((pos.x + itemSize.width / 2) > posBR.x)
{
line += 1;
row = 0;
pos = ccpAdd(beginPos, ccp(posStep.x * row, posStep.y * line));
pos = beginPos + Point(posStep.x * row, posStep.y * line);
}
row += 1;
pMenuItem->setPosition(pos);

View File

@ -0,0 +1,170 @@
/****************************************************************************
Copyright (c) 2013 cocos2d-x.org
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "MySocialManager.h"
#include "PluginManager.h"
#include "cocos2d.h"
using namespace cocos2d::plugin;
using namespace cocos2d;
MySocialManager* MySocialManager::s_pManager = NULL;
MySocialManager::MySocialManager()
: _pNd91(NULL)
{
}
MySocialManager::~MySocialManager()
{
unloadPlugins();
}
MySocialManager* MySocialManager::sharedManager()
{
if (s_pManager == NULL) {
s_pManager = new MySocialManager();
}
return s_pManager;
}
void MySocialManager::purgeManager()
{
if (s_pManager)
{
delete s_pManager;
s_pManager = NULL;
}
PluginManager::end();
}
void MySocialManager::loadPlugins()
{
{
_pNd91 = dynamic_cast<ProtocolSocial*>(PluginManager::getInstance()->loadPlugin("SocialNd91"));
if (NULL != _pNd91)
{
TSocialDeveloperInfo pNdInfo;
pNdInfo["Nd91AppId"] = "100010";
pNdInfo["Nd91AppKey"] = "C28454605B9312157C2F76F27A9BCA2349434E546A6E9C75";
pNdInfo["Nd91Orientation"] = "landscape";
if (pNdInfo.empty()) {
char msg[256] = { 0 };
sprintf(msg, "Developer info is empty. PLZ fill your Nd91 info in %s(nearby line %d)", __FILE__, __LINE__);
MessageBox(msg, "Nd91 Warning");
}
_pNd91->configDeveloperInfo(pNdInfo);
_pNd91->setDebugMode(true);
_pNd91->setListener(this);
}
}
}
void MySocialManager::submitScore(MySocialMode mode, const char* leaderboardID, long score)
{
ProtocolSocial* pSocial = NULL;
switch (mode)
{
case eNd91:
pSocial = _pNd91;
break;
default:
break;
}
if (NULL != pSocial)
{
pSocial->submitScore(leaderboardID, score);
}
}
void MySocialManager::showLeaderboard(MySocialMode mode, const char* leaderboardID)
{
ProtocolSocial* pSocial = NULL;
switch (mode)
{
case eNd91:
pSocial = _pNd91;
break;
default:
break;
}
if (NULL != pSocial)
{
pSocial->showLeaderboard(leaderboardID);
}
}
void MySocialManager::unlockAchievement(MySocialMode mode, TAchievementInfo info)
{
ProtocolSocial* pSocial = NULL;
switch (mode)
{
case eNd91:
pSocial = _pNd91;
break;
default:
break;
}
if (NULL != pSocial)
{
pSocial->unlockAchievement(info);
}
}
void MySocialManager::showAchievement(MySocialMode mode)
{
ProtocolSocial* pSocial = NULL;
switch (mode)
{
case eNd91:
pSocial = _pNd91;
break;
default:
break;
}
if (NULL != pSocial)
{
pSocial->showAchievements();
}
}
void MySocialManager::unloadPlugins()
{
if (_pNd91)
{
PluginManager::getInstance()->unloadPlugin("SocialNd91");
_pNd91 = NULL;
}
}
void MySocialManager::onSocialResult(SocialRetCode code, const char* msg)
{
char socialStatus[1024] = { 0 };
sprintf(socialStatus, "Social code %d", code);
MessageBox(msg, socialStatus);
}

View File

@ -0,0 +1,59 @@
/****************************************************************************
Copyright (c) 2013 cocos2d-x.org
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#ifndef __MY_SOCIAL_MANAGER_H__
#define __MY_SOCIAL_MANAGER_H__
#include "ProtocolSocial.h"
class MySocialManager : public cocos2d::plugin::SocialListener
{
public:
static MySocialManager* sharedManager();
static void purgeManager();
typedef enum {
eNoneMode = 0,
eNd91,
} MySocialMode;
void unloadPlugins();
void loadPlugins();
void submitScore(MySocialMode mode, const char* leaderboardID, long score);
void showLeaderboard(MySocialMode mode, const char* leaderboardID);
void unlockAchievement(MySocialMode mode, cocos2d::plugin::TAchievementInfo info);
void showAchievement(MySocialMode mode);
virtual void onSocialResult(cocos2d::plugin::SocialRetCode code, const char* msg);
private:
MySocialManager();
virtual ~MySocialManager();
static MySocialManager* s_pManager;
cocos2d::plugin::ProtocolSocial* _pNd91;
};
#endif // __MY_SOCIAL_MANAGER_H__

View File

@ -0,0 +1,152 @@
/****************************************************************************
Copyright (c) 2013 cocos2d-x.org
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "TestSocialScene.h"
#include "PluginManager.h"
#include "AppDelegate.h"
#include "MySocialManager.h"
#include "HelloWorldScene.h"
using namespace cocos2d;
using namespace cocos2d::plugin;
const std::string s_aTestCases[] = {
"Nd91",
};
Scene* TestSocial::scene()
{
// 'scene' is an autorelease object
Scene *scene = Scene::create();
// 'layer' is an autorelease object
TestSocial *layer = TestSocial::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool TestSocial::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
}
MySocialManager::sharedManager()->loadPlugins();
Size visibleSize = Director::getInstance()->getVisibleSize();
Point origin = Director::getInstance()->getVisibleOrigin();
Point posMid = Point(origin.x + visibleSize.width / 2, origin.y + visibleSize.height / 2);
Point posBR = Point(origin.x + visibleSize.width, origin.y);
MenuItemFont *pBackItem = MenuItemFont::create("Back", CC_CALLBACK_1(TestSocial::menuBackCallback, this));
Size backSize = pBackItem->getContentSize();
pBackItem->setPosition(posBR + Point(- backSize.width / 2, backSize.height / 2));
// create menu, it's an autorelease object
Menu* pMenu = Menu::create(pBackItem, NULL);
pMenu->setPosition( Point::ZERO );
LabelTTF* label1 = LabelTTF::create("Submit Score", "Arial", 28);
MenuItemLabel* pItemSubmit = MenuItemLabel::create(label1, CC_CALLBACK_1(TestSocial::testSubmit, this));
pItemSubmit->setAnchorPoint(Point(0.5f, 0));
pMenu->addChild(pItemSubmit, 0);
pItemSubmit->setPosition(posMid + Point(-140, -60));
LabelTTF* label2 = LabelTTF::create("Unlock Achievement", "Arial", 28);
MenuItemLabel* pItemUnlock = MenuItemLabel::create(label2, CC_CALLBACK_1(TestSocial::testUnlock, this));
pItemUnlock->setAnchorPoint(Point(0.5f, 0));
pMenu->addChild(pItemUnlock, 0);
pItemUnlock->setPosition(posMid + Point(140, -60));
LabelTTF* label3 = LabelTTF::create("Show Leaderboard", "Arial", 28);
MenuItemLabel* pItemLeader = MenuItemLabel::create(label3, CC_CALLBACK_1(TestSocial::testLeaderboard, this));
pItemLeader->setAnchorPoint(Point(0.5f, 0));
pMenu->addChild(pItemLeader, 0);
pItemLeader->setPosition(posMid + Point(-140, -120));
LabelTTF* label4 = LabelTTF::create("Show Achievement", "Arial", 28);
MenuItemLabel* pItemAchi = MenuItemLabel::create(label4, CC_CALLBACK_1(TestSocial::testAchievement, this));
pItemAchi->setAnchorPoint(Point(0.5f, 0));
pMenu->addChild(pItemAchi, 0);
pItemAchi->setPosition(posMid + Point(140, -120));
// create optional menu
// cases item
_caseItem = MenuItemToggle::createWithCallback(NULL,
MenuItemFont::create( s_aTestCases[0].c_str() ),
NULL );
int caseLen = sizeof(s_aTestCases) / sizeof(std::string);
for (int i = 1; i < caseLen; ++i)
{
_caseItem->getSubItems()->addObject( MenuItemFont::create( s_aTestCases[i].c_str() ) );
}
_caseItem->setPosition(posMid + Point(0, 120));
pMenu->addChild(_caseItem);
this->addChild(pMenu, 1);
return true;
}
void TestSocial::testSubmit(Object* pSender)
{
int nIdx = _caseItem->getSelectedIndex();
MySocialManager::sharedManager()->submitScore((MySocialManager::MySocialMode)(nIdx + 1), "0", 30000);
}
void TestSocial::testUnlock(Object* pSender)
{
int nIdx = _caseItem->getSelectedIndex();
TAchievementInfo info;
info["AchievementID"] = "MyAchiID";
info["NDDisplayText"] = "Fighter";
info["NDScore"] = "100";
MySocialManager::sharedManager()->unlockAchievement((MySocialManager::MySocialMode)(nIdx + 1), info);
}
void TestSocial::testLeaderboard(Object* pSender)
{
int nIdx = _caseItem->getSelectedIndex();
MySocialManager::sharedManager()->showLeaderboard((MySocialManager::MySocialMode)(nIdx + 1), "0");
}
void TestSocial::testAchievement(Object* pSender)
{
int nIdx = _caseItem->getSelectedIndex();
MySocialManager::sharedManager()->showAchievement((MySocialManager::MySocialMode)(nIdx + 1));
}
void TestSocial::menuBackCallback(Object* pSender)
{
MySocialManager::purgeManager();
Scene* newScene = HelloWorld::scene();
Director::getInstance()->replaceScene(newScene);
}

View File

@ -0,0 +1,52 @@
/****************************************************************************
Copyright (c) 2013 cocos2d-x.org
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#ifndef __TEST_SOCIAL_SCENE_H__
#define __TEST_SOCIAL_SCENE_H__
#include "cocos2d.h"
class TestSocial : public cocos2d::Layer
{
public:
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();
// there's no 'id' in cpp, so we recommand to return the exactly class pointer
static cocos2d::Scene* scene();
// a selector callback
void menuBackCallback(Object* pSender);
void testSubmit(Object* pSender);
void testUnlock(Object* pSender);
void testLeaderboard(Object* pSender);
void testAchievement(Object* pSender);
// implement the "static node()" method manually
CREATE_FUNC(TestSocial);
private:
cocos2d::MenuItemToggle* _caseItem;
};
#endif // __TEST_SOCIAL_SCENE_H__

View File

@ -1,3 +1,26 @@
/****************************************************************************
Copyright (c) 2013 cocos2d-x.org
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "TestUserScene.h"
#include "MyUserManager.h"
#include "HelloWorldScene.h"
@ -40,8 +63,8 @@ bool TestUser::init()
MyUserManager::sharedManager()->loadPlugin();
Size visibleSize = Director::getInstance()->getVisibleSize();
Point origin = Director::getInstance()->getVisibleOrigin();
Point posMid = ccp(origin.x + visibleSize.width / 2, origin.y + visibleSize.height / 2);
Point posBR = ccp(origin.x + visibleSize.width, origin.y);
Point posMid = Point(origin.x + visibleSize.width / 2, origin.y + visibleSize.height / 2);
Point posBR = Point(origin.x + visibleSize.width, origin.y);
/////////////////////////////
// 2. add a menu item with "X" image, which is clicked to quit the program
@ -50,23 +73,23 @@ bool TestUser::init()
// add a "close" icon to exit the progress. it's an autorelease object
MenuItemFont *pBackItem = MenuItemFont::create("Back", CC_CALLBACK_1(TestUser::menuBackCallback, this));
Size backSize = pBackItem->getContentSize();
pBackItem->setPosition(ccpAdd(posBR, ccp(- backSize.width / 2, backSize.height / 2)));
pBackItem->setPosition(posBR + Point(- backSize.width / 2, backSize.height / 2));
// create menu, it's an autorelease object
Menu* pMenu = Menu::create(pBackItem, NULL);
pMenu->setPosition(PointZero);
pMenu->setPosition(Point::ZERO);
LabelTTF* label1 = LabelTTF::create("Login", "Arial", 32);
MenuItemLabel* pItemLogin = MenuItemLabel::create(label1, CC_CALLBACK_1(TestUser::testLogin, this));
pItemLogin->setAnchorPoint(ccp(0.5f, 0));
pItemLogin->setAnchorPoint(Point(0.5f, 0));
pMenu->addChild(pItemLogin, 0);
pItemLogin->setPosition(ccpAdd(posMid, ccp(-100, -120)));
pItemLogin->setPosition(posMid + Point(-100, -120));
LabelTTF* label2 = LabelTTF::create("Logout", "Arial", 32);
MenuItemLabel* pItemLogout = MenuItemLabel::create(label2, CC_CALLBACK_1(TestUser::testLogout, this));
pItemLogout->setAnchorPoint(ccp(0.5f, 0));
pItemLogout->setAnchorPoint(Point(0.5f, 0));
pMenu->addChild(pItemLogout, 0);
pItemLogout->setPosition(ccpAdd(posMid, ccp(100, -120)));
pItemLogout->setPosition(posMid + Point(100, -120));
// create optional menu
// cases item
@ -78,7 +101,7 @@ bool TestUser::init()
{
_caseItem->getSubItems()->addObject( MenuItemFont::create( s_aTestCases[i].c_str() ) );
}
_caseItem->setPosition(ccpAdd(posMid, ccp(0, 120)));
_caseItem->setPosition(posMid + Point(0, 120));
pMenu->addChild(_caseItem);
_selectedCase = 0;

View File

@ -1,3 +1,26 @@
/****************************************************************************
Copyright (c) 2013 cocos2d-x.org
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#ifndef __TEST_USER_SCENE_H__
#define __TEST_USER_SCENE_H__

View File

@ -18,7 +18,9 @@ LOCAL_SRC_FILES := hellocpp/main.cpp \
../../Classes/TestUser/TestUserScene.cpp \
../../Classes/TestUser/MyUserManager.cpp \
../../Classes/TestIAPOnline/TestIAPOnlineScene.cpp \
../../Classes/TestIAPOnline/MyIAPOLManager.cpp
../../Classes/TestIAPOnline/MyIAPOLManager.cpp \
../../Classes/TestSocial/TestSocialScene.cpp \
../../Classes/TestSocial/MySocialManager.cpp
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes \
$(LOCAL_PATH)/../../Classes/TestAds \
@ -26,7 +28,8 @@ LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes \
$(LOCAL_PATH)/../../Classes/TestIAP \
$(LOCAL_PATH)/../../Classes/TestShare \
$(LOCAL_PATH)/../../Classes/TestUser \
$(LOCAL_PATH)/../../Classes/TestIAPOnline
$(LOCAL_PATH)/../../Classes/TestIAPOnline \
$(LOCAL_PATH)/../../Classes/TestSocial
LOCAL_WHOLE_STATIC_LIBRARIES += cocos2dx_static \
PluginProtocolStatic

View File

@ -57,6 +57,8 @@
FAD55528177D2C2000968F54 /* BtnND91.png in Resources */ = {isa = PBXBuildFile; fileRef = FAD55524177D2C2000968F54 /* BtnND91.png */; };
FAD55529177D2C2000968F54 /* twitter.jpeg in Resources */ = {isa = PBXBuildFile; fileRef = FAD55525177D2C2000968F54 /* twitter.jpeg */; };
FAD5552A177D2C2000968F54 /* weibo.png in Resources */ = {isa = PBXBuildFile; fileRef = FAD55526177D2C2000968F54 /* weibo.png */; };
FADBF8A2179E559900F59B1D /* MySocialManager.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FADBF89E179E559900F59B1D /* MySocialManager.cpp */; };
FADBF8A3179E559900F59B1D /* TestSocialScene.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FADBF8A0179E559900F59B1D /* TestSocialScene.cpp */; };
/* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */
@ -236,6 +238,10 @@
FAD55524177D2C2000968F54 /* BtnND91.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = BtnND91.png; sourceTree = "<group>"; };
FAD55525177D2C2000968F54 /* twitter.jpeg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = twitter.jpeg; sourceTree = "<group>"; };
FAD55526177D2C2000968F54 /* weibo.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = weibo.png; sourceTree = "<group>"; };
FADBF89E179E559900F59B1D /* MySocialManager.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = MySocialManager.cpp; sourceTree = "<group>"; };
FADBF89F179E559900F59B1D /* MySocialManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MySocialManager.h; sourceTree = "<group>"; };
FADBF8A0179E559900F59B1D /* TestSocialScene.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = TestSocialScene.cpp; sourceTree = "<group>"; };
FADBF8A1179E559900F59B1D /* TestSocialScene.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TestSocialScene.h; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@ -289,6 +295,7 @@
15AA9C4015B7EC450033D6C2 /* Classes */ = {
isa = PBXGroup;
children = (
FADBF89D179E559900F59B1D /* TestSocial */,
FA3D264717867219009B234A /* TestIAPOnline */,
FAD5550E177D1EE900968F54 /* TestIAP */,
FAD55513177D1EE900968F54 /* TestUser */,
@ -464,6 +471,18 @@
path = ../Classes/TestUser;
sourceTree = "<group>";
};
FADBF89D179E559900F59B1D /* TestSocial */ = {
isa = PBXGroup;
children = (
FADBF89E179E559900F59B1D /* MySocialManager.cpp */,
FADBF89F179E559900F59B1D /* MySocialManager.h */,
FADBF8A0179E559900F59B1D /* TestSocialScene.cpp */,
FADBF8A1179E559900F59B1D /* TestSocialScene.h */,
);
name = TestSocial;
path = ../Classes/TestSocial;
sourceTree = "<group>";
};
/* End PBXGroup section */
/* Begin PBXNativeTarget section */
@ -662,6 +681,8 @@
FAD5551B177D1EE900968F54 /* TestUserScene.cpp in Sources */,
FA3D264D17867219009B234A /* MyIAPOLManager.cpp in Sources */,
FA3D264E17867219009B234A /* TestIAPOnlineScene.cpp in Sources */,
FADBF8A2179E559900F59B1D /* MySocialManager.cpp in Sources */,
FADBF8A3179E559900F59B1D /* TestSocialScene.cpp in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};