classEvent
{
protected:
Event(conststd::string& type);
virtual~Event();
inlineconststd::string& getType()const{return_type; };
inlinevoidstopPropagation() {_isstopped=true; };
inlineboolisstopped()const{return_isstopped; };
inlineNode* getCurrentTarget() {return_currentTarget; };
inlinevoidsetCurrentTarget(Node* target) {_currentTarget= target; };std::string_type;
bool_isstopped;
Node* _currentTarget;
classEventListener :publicObject
{
protected:
EventListener();
boolinit(conststd::string& t,std::function<void(Event*)>callback);
virtual~EventListener();
virtualboolcheckAvaiable() =0;
virtualEventListener* clone() =0;
std::function<void(Event*)> _onEvent;
bool_isRegistered;friendclassEventdispatcher;
friendclassNode;
classCollisionEvent:publicEvent
{
public:
staticconstchar* COLLISION_EVENT_TYPE;
CollisionEvent(Entity* l,Entity* r);
Entity* getLeft(){return_left;}
Entity* getRight(){return_right;}
private:
Entity* _left;
Entity* _right;
classCollisionListener :publicEventListener
{
public:
staticCollisionListener* create(std::function<void(CollisionEvent*)> callback);
virtualboolcheckAvaiable() override;
virtualCollisionListener* clone() override;
protected:
CollisionListener();
boolinit(std::function<void(CollisionEvent*)> callback);
std::function<void(CollisionEvent*)> _onCollisionEvent;
voidHitSystem::configure()
{
autolistener=CollisionListener::create(
[this](CollisionEvent* event){
this->hit(event);
});
Eventdispatcher::getInstance()->addEventListenerWithFixedPriority(listener,1);
}
voidCollisionSystem::update(floatdt)
{
CollisionEvent* event=newCollisionEvent(entity,collisionEntity);
Eventdispatcher::getInstance()->dispatchEvent(event);
}
voidaddEventListenerWithSceneGraPHPriority(EventListener* listener,Node* node);
structEventListenerItem
{
int fixedPriority;
Node* node;
EventListener* listener;
~EventListenerItem();
inlinevoidupdateEventPriorityIndex() {
_oldEventPriority=_eventPriority;
_eventPriority= ++_globalEventPriorityIndex;
if(_oldEventPriority!=_eventPriority)
{
setDirtyForAllEventListeners();
}
classEventTouch :publicEvent
{
public:
enumclassEventCode{
BEGAN,
MOVED,
ENDED,
CANCELLED
};
EventCodegetEventCode() {return_eventCode; };
std::vector<Touch*> gettouches() {return_touches; };
#if TOUCH_PERF_DEBUG
voidsetEventCode(EventCodeeventCode) {_eventCode= eventCode; };
voidsettouches(conststd::vector<Touch*>& touches) {_touches= touches; };
#endif
};
classCC_DLLTouch :publicObject
{
public:
/** 触摸点在OpenGL坐标系中的位置 */
PointgetLocation()const;
/** 触摸点在OpenGL坐标系中的上一个位置 */
PointgetPrevIoUsLocation()const;
/** 触摸点在OpenGL坐标系的起点位置 */
PointgetStartLocation()const;
/** 在OpenGL坐标系中当前位置与上一个位置的差 */
PointgetDelta()const;
/** 触摸点在屏幕坐标系中的位置 */
PointgetLocationInView()const;
/** 触摸点在屏幕坐标系中的上一个位置 */
PointgetPrevIoUsLocationInView()const;
/** 触摸点在屏幕坐标系的起点位置 */
PointgetStartLocationInView()const;
intgetID()const{return_id;}
classEventListenerTouch :publicEventListener
{
public:
std::function<bool(Touch*,Event*)> onTouchBegan;
std::function<void(Touch*,Event*)> onTouchMoved;
std::function<void(Touch*,Event*)> onTouchEnded;
std::function<void(Touch*,Event*)> onTouchCancelled;
std::function<void(conststd::vector<Touch*>&,Event*)> ontouchesBegan;
std::function<void(conststd::vector<Touch*>&,Event*)> ontouchesMoved;
std::function<void(conststd::vector<Touch*>&,Event*)> ontouchesEnded;
std::function<void(conststd::vector<Touch*>&,Event*)> ontouchesCancelled;
voidsetSwallowtouches(boolneedSwallow);
private:
bool_needSwallow;
Touch::dispatchMode_dispatchMode;
boolHelloWorld::init()
{
if( !cclayer::init()){
returnfalse;
}
setTouchMode(Touch::dispatchMode::ONE_BY_ONE);
setTouchEnabled(true);
returntrue;
classHelloWorld :publiccocos2d::Layer
{
public:
virtualboolonTouchBegan(Touch*touch,Event*event);
virtualvoidonTouchMoved(Touch*touch,Event*event);
virtualvoidonTouchEnded(Touch*touch,Event*event);
boolMenu::onTouchBegan(Touch* touch,Event* event)
{
if(_state!=Menu::State::WAITING|| !_visible|| !_enabled){
returnfalse;
}
for(Node*c =this->_parent; c !=NULL; c = c->getParent()){
if(c->isVisible() ==false){
returnfalse;
}
}
_selectedItem=this->itemForTouch(touch);
if(_selectedItem){
_state=Menu::State::TRACKING_TOUCH;
_selectedItem->selected();
returntrue;
}
returnfalse;
MenuItem*Menu::itemForTouch(Touch*touch)
{
PointtouchLocation = touch->getLocation();
if(_children&&_children->count() >0)
{
Object* pObject =NULL;
CCARRAY_FOREACH_REVERSE(_children,pObject)
{
MenuItem* child =dynamic_cast<MenuItem*>(pObject);
if(child && child->isVisible() && child->isEnabled())
{
Pointlocal = child->convertToNodeSpace(touchLocation);
Rectr = child->rect();
r.origin=Point::ZERO;
if(r.containsPoint(local)){
returnchild;
}
}
}
}
returnNULL;
classEventKeyboard :publicEvent
{
EventKeyboard(KeyCodekeyCode,boolispressed)
:Event(EVENT_TYPE)
,_keyCode(keyCode)
,_ispressed(ispressed)
{};
private:
KeyCode_keyCode;
bool_ispressed;
friendclassEventListenerKeyboard;
classEventListenerKeyboard :publicEventListener
{
public:
std::function<void(EventKeyboard::KeyCode,Event* event)> onKeypressed;
std::function<void(EventKeyboard::KeyCode,Event* event)> onkeyreleased;
boolEventListenerKeyboard::init()
{
autolistener = [this](Event* event){
autokeyboardEvent =static_cast<EventKeyboard*>(event);
if(keyboardEvent->_ispressed){
if(onKeypressed!=nullptr)
onKeypressed(keyboardEvent->_keyCode,event);
}
else{
if(onkeyreleased!=nullptr)
onkeyreleased(keyboardEvent->_keyCode,event);
}
};
if(EventListener::init(EventKeyboard::EVENT_TYPE,listener)){
returntrue;
}
returnfalse;
原文:http://hielvis.com/2013/11/16/cocos2d-x-event/