void EventDispatcher::dispatchEvent(Event * event){ string type = event->type(); // Leave if no event registered if( !hasEventListener(type) ){ return; } event->_target = this; _runningDispatch = true; { // A reference to keep code clean std::map<int, std::list<IEventListener*> > &list = _listenerList[event->type()]; // Iterate through all functions in the event, from high proproty to low for( std::map<int, std::list<IEventListener*> >::reverse_iterator it=list.rbegin(); it!=list.rend(); ++it ){ std::list<IEventListener*> &funcList = it->second; std::list<IEventListener*>::iterator it_f=funcList.begin(); std::list<IEventListener*>::iterator end_f=funcList.end(); // Execute callbacks while( it_f != end_f ){ (*it_f++)->exec(event); // return immediately if cancel() called. if( event->_canceled == true ){ _runningDispatch = false; return; } } } } _runningDispatch = false; // If call removeEventListener during dispatchEvent. Remove listener after dispatch. if( 0 < _removeList.size() ){ // Iterate through all functions in the event, from high proproty to low for( std::list<IEventListener*>::iterator it=_removeList.begin(); it!=_removeList.end(); ++it ){ removeEventListener( type, (*it) ); } _removeList.clear(); } };
//-------------------------------------------------------------- void jppevent::EventDispatcher::removeEventListener(jppevent::EventType type, jppevent::EventHandler handler) { if (!hasEventListener(type)) return; std::vector<jppevent::EventListener> *list = &listeners_[type]; std::vector<jppevent::EventListener>::iterator it; for (it = list->begin(); it != list->end(); ++it) { if (*it->handler == handler) { list->erase(it); if (list->size() == 0) listeners_.erase(type); return; } } }
void EventDispatcher::dispatchEvent(const Event &event) { // Leave if no event registered if (!hasEventListener(event.m_type)) return; // A reference to keep code clean std::vector<eventFunctionPtr > &allFunctions = m_eventHandlerList[event.m_type]; // Iterate through all functions in the event and execute them for (std::vector<eventFunctionPtr>::iterator i = allFunctions.begin(); i != allFunctions.end(); ++i) { (*i)(event); } }
void EventDispatcher::removeEventListener(const std::string &type, eventFunctionPtr listener) { if (hasEventListener(type)) { std::vector<eventFunctionPtr>& listeners = m_eventHandlerList[type]; for (std::vector<eventFunctionPtr>::iterator it = listeners.begin(); it != listeners.end(); ++it) { if (*it == listener) { m_eventHandlerList[type].erase(it); break; } } } }
void callback(int type, void *event) { if (type == GGEOLOCATION_LOCATION_UPDATE_EVENT && !isUpdatingLocation_) return; if (type == GGEOLOCATION_HEADING_UPDATE_EVENT && !isUpdatingHeading_) return; if (type == GGEOLOCATION_ERROR_EVENT && !isUpdatingLocation_) return; if (type == GGEOLOCATION_LOCATION_UPDATE_EVENT && !hasEventListener(LOCATION_UPDATE)) return; if (type == GGEOLOCATION_HEADING_UPDATE_EVENT && !hasEventListener(HEADING_UPDATE)) return; if (type == GGEOLOCATION_ERROR_EVENT && !hasEventListener(ERROR)) return; luaL_rawgetptr(L, LUA_REGISTRYINDEX, &key); luaL_rawgetptr(L, -1, this); if (lua_isnil(L, -1)) { lua_pop(L, 2); return; } lua_getfield(L, -1, "dispatchEvent"); lua_pushvalue(L, -2); // create copy of geolocation if (type == GGEOLOCATION_LOCATION_UPDATE_EVENT) { lua_getfield(L, -1, "__updateLocationEvent"); ggeolocation_LocationUpdateEvent *event2 = (ggeolocation_LocationUpdateEvent*)event; lua_pushnumber(L, event2->latitude); lua_setfield(L, -2, "latitude"); lua_pushnumber(L, event2->longitude); lua_setfield(L, -2, "longitude"); lua_pushnumber(L, event2->altitude); lua_setfield(L, -2, "altitude"); } else if (type == GGEOLOCATION_HEADING_UPDATE_EVENT) { lua_getfield(L, -1, "__updateHeadingEvent"); ggeolocation_HeadingUpdateEvent *event2 = (ggeolocation_HeadingUpdateEvent*)event; lua_pushnumber(L, event2->magneticHeading); lua_setfield(L, -2, "magneticHeading"); lua_pushnumber(L, event2->trueHeading); lua_setfield(L, -2, "trueHeading"); } else if (type == GGEOLOCATION_ERROR_EVENT) { lua_getfield(L, -1, "__errorEvent"); } lua_call(L, 2, 0); // call geolocation:dispatchEvent(event) lua_pop(L, 2); }
bool DisplayObject::acceptTouchEvent(TouchEvent &event) { if (!_mouseEnabled) { return false; } // 没有该事件并且也没有 TOUCH_OUT TOUCH_HIT RIGHT_MOUSE_CLICK MIDDLE_MOUSE_CLICK if (!hasEventListener(event.type) && !hasEventListener(TouchEvent::TOUCH_IN) && !hasEventListener(TouchEvent::TOUCH_OUT) && !hasEventListener(TouchEvent::TOUCH_HIT) && !hasEventListener(TouchEvent::RIGHT_MOUSE_CLICK) && !hasEventListener(TouchEvent::MIDDLE_MOUSE_CLICK) ) { return false; } // 多点触控 bool isIn = false; for (int i = 0; i < event.size; i++) { isIn = hitTestPoint(event.points[i].x, event.points[i].y); if (!isIn && event.size > 1) { // 多点触控,如果多点都未在显示对象内部,则视为未选中 _touchBegan = false; _touchIn = false; return false; } } // TOUCH_OUT事件 if (_touchIn && !isIn) { if (hasEventListener(TouchEvent::TOUCH_OUT)) { TouchEvent touchOutEvent(TouchEvent::TOUCH_OUT, true, event.points, event.size); dispatchEvent(touchOutEvent); _touchIn = false; _touchBegan = false; return true; // TOUCH_OUT事件,事件接受成功。 } } // TOUCH_IN事件 if (!_touchIn && isIn) { if (hasEventListener(TouchEvent::TOUCH_IN)) { TouchEvent touchInEvent(TouchEvent::TOUCH_IN, true, event.points, event.size); dispatchEvent(touchInEvent); } } // not in, 不派发事件 if (!isIn) { _touchIn = false; _touchBegan = false; return false; } // Touch_Began if (event.type == TouchEvent::TOUCH_BEGAN || event.type == TouchEvent::RIGHT_MOUSE_DOWN || event.type == TouchEvent::MIDDLE_MOUSE_DOWN) { _touchBegan = true; } // in 派发事件 dispatchEvent(event); // click事件 if (_touchBegan && event.type == TouchEvent::TOUCH_END && hasEventListener(TouchEvent::TOUCH_HIT)) { TouchEvent touchHitEvent(TouchEvent::TOUCH_HIT, true, event.points, event.size); dispatchEvent(touchHitEvent); } else if (_touchBegan && event.type == TouchEvent::RIGHT_MOUSE_UP && hasEventListener(TouchEvent::RIGHT_MOUSE_CLICK)) { TouchEvent rightClickEvnet(TouchEvent::RIGHT_MOUSE_CLICK, true, event.points, event.size); dispatchEvent(rightClickEvnet); } else if (_touchBegan && event.type == TouchEvent::MIDDLE_MOUSE_UP && hasEventListener(TouchEvent::MIDDLE_MOUSE_CLICK)) { TouchEvent middleClickEvent(TouchEvent::MIDDLE_MOUSE_CLICK, true, event.points, event.size); dispatchEvent(middleClickEvent); } _touchIn = true; return true; }