Skip to content

Instantly share code, notes, and snippets.

@jeffquach
Last active November 27, 2016 23:44
Show Gist options
  • Save jeffquach/30516614f7cf974dd8f6f081e03db0be to your computer and use it in GitHub Desktop.
Save jeffquach/30516614f7cf974dd8f6f081e03db0be to your computer and use it in GitHub Desktop.
#include "FacebookShare.h"
FacebookShare* FacebookShare::create()
{
FacebookShare* fb = new (std::nothrow) FacebookShare();
if(fb && fb->init()){
fb->autorelease();
PluginFacebook::setListener(fb);
PluginFacebook::init();
auto visibleSize = Director::getInstance()->getVisibleSize();
auto origin = Director::getInstance()->getVisibleOrigin();
auto center = Vec2(origin.x + visibleSize.width * 0.5, origin.y + visibleSize.height * 0.5);
auto fbBtn = ui::Button::create("square.png", "squarePressed.png");
fbBtn->setPosition(center);
fbBtn->addClickEventListener(CC_CALLBACK_1(FacebookShare::onShareLink, fb));
fb->addChild(fbBtn);
return fb;
}
CC_SAFE_DELETE(fb);
return nullptr;
}
bool FacebookShare::init(){
if(!Node::init()) return false;
return true;
}
void FacebookShare::onShareLink(cocos2d::Ref* sender)
{
if(PluginFacebook::isLoggedIn()){
fbShare();
}else{
PluginFacebook::login();
}
}
void FacebookShare::onLogin(bool isLogin, const std::string& error)
{
if(isLogin) fbShare();
}
void FacebookShare::fbShare()
{
FBShareInfo info;
info.type = FB_LINK;
info.link = "http://www.cocos2d-x.org";
info.title = "Cocos2DX";
info.text = "This game engine is da bomb d8wg!";
info.image = "http://cocos2d-x.org/images/logo.png";
PluginFacebook::dialog(info);
}
#ifndef _FACEBOOK_SHARE_H_
#define _FACEBOOK_SHARE_H_
#include "cocos2d.h"
#ifdef SDKBOX_ENABLED
#include "PluginFacebook/PluginFacebook.h"
#endif
USING_NS_CC;
using namespace sdkbox;
class FacebookShare : public Node, FacebookListener
{
public:
static FacebookShare* create();
virtual bool init();
private:
void onShareLink(cocos2d::Ref* sender); // FB share
void fbShare();
// FB callbacks, only 'onLogin' used but all have to be implemented!
void onLogin(bool isLogin, const string& msg);
void onPermission(bool isLogin, const string& msg) {};
void onAPI(const string& tag, const string& jsonData) {};
void onSharedSuccess(const string& message) {};
void onSharedFailed(const string& message) {};
void onSharedCancel() {};
void onFetchFriends(bool ok, const string& msg) {};
void onRequestInvitableFriends(const FBInvitableFriendsInfo& friends) {};
void onInviteFriendsWithInviteIdsResult(bool result, const string& msg) {};
void onInviteFriendsResult(bool result, const string& msg) {};
void onGetUserInfo(const FBGraphUser& userInfo) {};
};
#endif // _FACEBOOK_SHARE_H_
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment