Skip to content

Instantly share code, notes, and snippets.

@darkdukey
Last active March 20, 2019 06:20
Show Gist options
  • Save darkdukey/4df4b26627b210f28277 to your computer and use it in GitHub Desktop.
Save darkdukey/4df4b26627b210f28277 to your computer and use it in GitHub Desktop.
Cocos Studio Event Callback example
#include "MyWidget.h"
#include "ui/UIText.h"
USING_NS_CC;
using namespace std;
using namespace cocos2d::ui;
MyWidget::MyWidget(){}
Widget::ccWidgetTouchCallback MyWidget::onLocateTouchCallback(const string &callBackName)
{
if (callBackName == "onTouch")
{
return CC_CALLBACK_2(MyWidget::onTouch, this);
}
return nullptr;
}
Widget::ccWidgetClickCallback MyWidget::onLocateClickCallback(const string &callBackName)
{
if (callBackName == "onClick")
{
return CC_CALLBACK_1(MyWidget::onClick, this);
}
return nullptr;
}
Widget::ccWidgetEventCallback MyWidget::onLocateEventCallback(const string &callBackName)
{
if (callBackName == "onEvent")
{
return CC_CALLBACK_2(MyWidget::onEvent, this);
}
return nullptr;
}
void MyWidget::onTouch(cocos2d::Ref* object, cocos2d::ui::Widget::TouchEventType type)
{
CCLOG("onTouch");
}
void MyWidget::onClick(cocos2d::Ref* sender)
{
CCLOG("onClick");
}
void MyWidget::onEvent(cocos2d::Ref* sender, int eventType)
{
CCLOG("onEvent");
}
#include "cocos2d.h"
#include "cocostudio/CocoStudio.h"
#include "cocostudio/WidgetCallBackHandlerProtocol.h"
class MyWidget: public cocos2d::Node, public cocostudio::WidgetCallBackHandlerProtocol
{
public:
CREATE_FUNC(MyWidget)
MyWidget();
virtual cocos2d::ui::Widget::ccWidgetTouchCallback onLocateTouchCallback(const std::string &callBackName);
virtual cocos2d::ui::Widget::ccWidgetClickCallback onLocateClickCallback(const std::string &callBackName);
virtual cocos2d::ui::Widget::ccWidgetEventCallback onLocateEventCallback(const std::string &callBackName);
void onTouch(cocos2d::Ref* sender, cocos2d::ui::Widget::TouchEventType type);
void onClick(cocos2d::Ref* sender);
void onEvent(cocos2d::Ref* sender, int eventType);
private:
std::vector<std::string> _touchTypes;
std::string _click;
std::vector<std::string> _eventTypes;
};
#include "MyWidgetReader.h"
#include "MyWidget.h"
USING_NS_CC;
static MyWidgetReader* _instanceMyWidgetReader = nullptr;
MyWidgetReader* MyWidgetReader::getInstance()
{
if (!_instanceMyWidgetReader)
{
_instanceMyWidgetReader = new MyWidgetReader();
}
return _instanceMyWidgetReader;
}
void MyWidgetReader::purge()
{
CC_SAFE_DELETE(_instanceMyWidgetReader);
}
Node* MyWidgetReader::createNodeWithFlatBuffers(const flatbuffers::Table *nodeOptions)
{
MyWidget* node = MyWidget::create();
setPropsWithFlatBuffers(node, nodeOptions);
return node;
}
#include "cocos2d.h"
#include "cocostudio/CocosStudioExport.h"
#include "cocostudio/WidgetReader/NodeReader/NodeReader.h"
class MyWidgetReader : public cocostudio::NodeReader
{
public:
MyWidgetReader() {};
~MyWidgetReader() {};
static MyWidgetReader* getInstance();
static void purge();
cocos2d::Node* createNodeWithFlatBuffers(const flatbuffers::Table* nodeOptions);
};
CSLoader* instance = CSLoader::getInstance();
instance->registReaderObject("MyWidgetReader ",(ObjectFactory::Instance)MyWidgetReader::getInstance);
@lishunan246
Copy link

instance->registReaderObject("MyWidgetReader ",(ObjectFactory::Instance)MyWidgetReader::getInstance);

Should "MyWidgetReader " be "MyWidgetReader"?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment