mirror of https://github.com/axmolengine/axmol.git
Merge branch 'develop' of https://github.com/cocos2d/cocos2d-x into develop
This commit is contained in:
commit
ba47e666c1
|
@ -825,7 +825,7 @@ void Node::visit()
|
|||
}
|
||||
// self draw
|
||||
this->draw();
|
||||
_eventPriority = ++_globalEventPriorityIndex;
|
||||
updateEventPriorityIndex();
|
||||
|
||||
for( ; i < _children->count(); i++ )
|
||||
{
|
||||
|
@ -837,7 +837,7 @@ void Node::visit()
|
|||
else
|
||||
{
|
||||
this->draw();
|
||||
_eventPriority = ++_globalEventPriorityIndex;
|
||||
updateEventPriorityIndex();
|
||||
}
|
||||
|
||||
// reset for next frame
|
||||
|
@ -1317,6 +1317,16 @@ void Node::removeAllEventListeners()
|
|||
}
|
||||
}
|
||||
|
||||
void Node::setDirtyForAllEventListeners()
|
||||
{
|
||||
auto dispatcher = EventDispatcher::getInstance();
|
||||
|
||||
for (auto& listener : _eventlisteners)
|
||||
{
|
||||
dispatcher->setDirtyForEventType(listener->type, true);
|
||||
}
|
||||
}
|
||||
|
||||
// NodeRGBA
|
||||
NodeRGBA::NodeRGBA()
|
||||
: _displayedOpacity(255)
|
||||
|
|
|
@ -1382,11 +1382,21 @@ private:
|
|||
protected:
|
||||
|
||||
/// Upates event priority for this node.
|
||||
inline void updateEventPriorityIndex() { _eventPriority = ++_globalEventPriorityIndex; };
|
||||
inline void updateEventPriorityIndex() {
|
||||
_oldEventPriority = _eventPriority;
|
||||
_eventPriority = ++_globalEventPriorityIndex;
|
||||
if (_oldEventPriority != _eventPriority)
|
||||
{
|
||||
setDirtyForAllEventListeners();
|
||||
}
|
||||
};
|
||||
|
||||
/// Removes all event listeners that associated with this node.
|
||||
void removeAllEventListeners();
|
||||
|
||||
/// Sets dirty for event listener.
|
||||
void setDirtyForAllEventListeners();
|
||||
|
||||
/// lazy allocs
|
||||
void childrenAlloc(void);
|
||||
|
||||
|
|
|
@ -28,8 +28,10 @@ NS_CC_BEGIN
|
|||
|
||||
const char* AccelerationEvent::EVENT_TYPE = "AccelerometerEvent";
|
||||
|
||||
AccelerationEvent::AccelerationEvent()
|
||||
AccelerationEvent::AccelerationEvent(Acceleration acc)
|
||||
: Event(EVENT_TYPE)
|
||||
{}
|
||||
, _acc(acc)
|
||||
{
|
||||
}
|
||||
|
||||
NS_CC_END
|
||||
|
|
|
@ -35,8 +35,11 @@ class AccelerationEvent : public Event
|
|||
public:
|
||||
static const char* EVENT_TYPE;
|
||||
|
||||
AccelerationEvent();
|
||||
Acceleration acc;
|
||||
AccelerationEvent(Acceleration acc);
|
||||
|
||||
private:
|
||||
Acceleration _acc;
|
||||
friend class AccelerationEventListener;
|
||||
};
|
||||
|
||||
NS_CC_END
|
||||
|
|
|
@ -56,7 +56,7 @@ bool AccelerationEventListener::init(std::function<void(Acceleration*, Event* ev
|
|||
{
|
||||
auto listener = [this](Event* event){
|
||||
auto accEvent = static_cast<AccelerationEvent*>(event);
|
||||
this->onAccelerationEvent(&accEvent->acc, event);
|
||||
this->onAccelerationEvent(&accEvent->_acc, event);
|
||||
};
|
||||
|
||||
if (EventListener::init(AccelerationEvent::EVENT_TYPE, listener))
|
||||
|
|
|
@ -30,6 +30,7 @@ Event::Event(const std::string& type)
|
|||
: _type(type)
|
||||
, _isStopped(false)
|
||||
, _currentTarget(nullptr)
|
||||
, _userData(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -37,14 +38,5 @@ Event::~Event()
|
|||
{
|
||||
}
|
||||
|
||||
Node* Event::getCurrentTarget()
|
||||
{
|
||||
return _currentTarget;
|
||||
}
|
||||
|
||||
void Event::setCurrentTarget(Node* target)
|
||||
{
|
||||
_currentTarget = target;
|
||||
}
|
||||
|
||||
NS_CC_END
|
||||
|
|
|
@ -48,28 +48,35 @@ public:
|
|||
virtual ~Event();
|
||||
|
||||
/** Gets the event type */
|
||||
const std::string& getType() const { return _type; };
|
||||
inline const std::string& getType() const { return _type; };
|
||||
|
||||
/** Stops propagation for current event */
|
||||
void stopPropagation() { _isStopped = true; };
|
||||
inline void stopPropagation() { _isStopped = true; };
|
||||
|
||||
/** Checks whether the event has been stopped */
|
||||
bool isStopped() const { return _isStopped; };
|
||||
inline bool isStopped() const { return _isStopped; };
|
||||
|
||||
/** @brief Gets current target of the event
|
||||
* @return The target with which the event associates.
|
||||
* @note It onlys be available when the event listener is associated with node.
|
||||
* It returns 0 when the listener is associated with fixed priority.
|
||||
*/
|
||||
Node* getCurrentTarget();
|
||||
inline Node* getCurrentTarget() { return _currentTarget; };
|
||||
|
||||
/** Set user data */
|
||||
inline void setUserData(void* data) { _userData = data; };
|
||||
|
||||
/** Get user data */
|
||||
inline void* getUserData() const { return _userData; };
|
||||
|
||||
protected:
|
||||
/** Sets current target */
|
||||
void setCurrentTarget(Node* target);
|
||||
inline void setCurrentTarget(Node* target) { _currentTarget = target; };
|
||||
|
||||
std::string _type; /// Event type
|
||||
bool _isStopped; /// whether the event has been stopped.
|
||||
Node* _currentTarget; /// Current target
|
||||
std::string _type; ///< Event type
|
||||
bool _isStopped; ///< whether the event has been stopped.
|
||||
Node* _currentTarget; ///< Current target
|
||||
void* _userData; ///< User data
|
||||
|
||||
friend class EventDispatcher;
|
||||
};
|
||||
|
|
|
@ -67,7 +67,6 @@ EventDispatcher::EventListenerItem::~EventListenerItem()
|
|||
|
||||
EventDispatcher::EventDispatcher()
|
||||
: _inDispatch(0)
|
||||
, _listeners(nullptr)
|
||||
, _isEnabled(true)
|
||||
{
|
||||
_toAddedListeners.reserve(50);
|
||||
|
@ -95,28 +94,25 @@ void EventDispatcher::destroyInstance()
|
|||
|
||||
void EventDispatcher::addEventListenerWithItem(EventListenerItem* item)
|
||||
{
|
||||
if (!_listeners)
|
||||
{
|
||||
_listeners = new std::map<std::string, std::vector<EventListenerItem*>*>();
|
||||
}
|
||||
|
||||
if (_inDispatch == 0)
|
||||
{
|
||||
std::vector<EventListenerItem*>* listenerList = nullptr;
|
||||
|
||||
auto itr = _listeners->find(item->listener->type);
|
||||
if (itr == _listeners->end())
|
||||
auto iter = _listeners.find(item->listener->type);
|
||||
if (iter == _listeners.end())
|
||||
{
|
||||
listenerList = new std::vector<EventListenerItem*>();
|
||||
listenerList->reserve(100);
|
||||
_listeners->insert(std::make_pair(item->listener->type, listenerList));
|
||||
_listeners.insert(std::make_pair(item->listener->type, listenerList));
|
||||
}
|
||||
else
|
||||
{
|
||||
listenerList = itr->second;
|
||||
listenerList = iter->second;
|
||||
}
|
||||
|
||||
listenerList->insert(listenerList->begin(), item);
|
||||
|
||||
setDirtyForEventType(item->listener->type, true);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -166,20 +162,21 @@ void EventDispatcher::addEventListenerWithFixedPriority(EventListener* listener,
|
|||
|
||||
void EventDispatcher::removeEventListener(EventListener* listener)
|
||||
{
|
||||
if (_listeners == nullptr || listener == nullptr)
|
||||
if (listener == nullptr)
|
||||
return;
|
||||
|
||||
bool isFound = false;
|
||||
|
||||
for (auto iter = _listeners->begin(); iter != _listeners->end();)
|
||||
for (auto iter = _listeners.begin(); iter != _listeners.end();)
|
||||
{
|
||||
for (auto itemIter = iter->second->begin(); itemIter != iter->second->end(); ++itemIter)
|
||||
{
|
||||
if ((*itemIter)->listener == listener)
|
||||
{
|
||||
(*itemIter)->listener->_isRegistered = false;
|
||||
(*itemIter)->listener->release();
|
||||
if (_inDispatch == 0)
|
||||
{
|
||||
(*itemIter)->listener->release();
|
||||
delete (*itemIter);
|
||||
iter->second->erase(itemIter);
|
||||
}
|
||||
|
@ -196,8 +193,10 @@ void EventDispatcher::removeEventListener(EventListener* listener)
|
|||
if (iter->second->empty())
|
||||
{
|
||||
auto list = iter->second;
|
||||
iter = _listeners->erase(iter);
|
||||
iter = _listeners.erase(iter);
|
||||
CC_SAFE_DELETE(list);
|
||||
|
||||
_priorityDirtyFlagMap.erase(listener->type);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -207,53 +206,26 @@ void EventDispatcher::removeEventListener(EventListener* listener)
|
|||
if (isFound)
|
||||
break;
|
||||
}
|
||||
|
||||
if (_listeners->empty())
|
||||
{
|
||||
CC_SAFE_DELETE(_listeners);
|
||||
}
|
||||
}
|
||||
|
||||
void EventDispatcher::setPriorityWithSceneGraph(EventListener* listener, Node* node)
|
||||
void EventDispatcher::setPriority(EventListener* listener, int fixedPriority)
|
||||
{
|
||||
if (_listeners == nullptr || listener == nullptr || node == nullptr)
|
||||
if (listener == nullptr)
|
||||
return;
|
||||
|
||||
for (auto iter = _listeners->begin(); iter != _listeners->end(); ++iter)
|
||||
for (auto iter = _listeners.begin(); iter != _listeners.end(); ++iter)
|
||||
{
|
||||
for (auto itemIter = iter->second->begin(); itemIter != iter->second->end(); ++itemIter)
|
||||
{
|
||||
auto item = *itemIter;
|
||||
if (item->listener == listener)
|
||||
{
|
||||
item->fixedPriority = 0;
|
||||
CCASSERT(item->node, "Can't set fixed priority with scene graph based listener.");
|
||||
|
||||
CC_SAFE_RETAIN(node);
|
||||
CC_SAFE_RELEASE(item->node);
|
||||
item->node = node;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void EventDispatcher::setPriorityWithFixedValue(EventListener* listener, int fixedPriority)
|
||||
{
|
||||
if (_listeners == nullptr || listener == nullptr)
|
||||
return;
|
||||
|
||||
for (auto iter = _listeners->begin(); iter != _listeners->end(); ++iter)
|
||||
{
|
||||
for (auto itemIter = iter->second->begin(); itemIter != iter->second->end(); ++itemIter)
|
||||
{
|
||||
auto item = *itemIter;
|
||||
if (item->listener == listener)
|
||||
{
|
||||
item->fixedPriority = fixedPriority;
|
||||
if (item->node != nullptr)
|
||||
if (item->fixedPriority != fixedPriority)
|
||||
{
|
||||
item->node->dissociateEventListener(listener);
|
||||
CC_SAFE_RELEASE_NULL(item->node);
|
||||
item->fixedPriority = fixedPriority;
|
||||
setDirtyForEventType(listener->type, true);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
@ -263,12 +235,24 @@ void EventDispatcher::setPriorityWithFixedValue(EventListener* listener, int fix
|
|||
|
||||
void EventDispatcher::dispatchEvent(Event* event, bool forceSortListeners)
|
||||
{
|
||||
if (_listeners == nullptr || !_isEnabled)
|
||||
if (!_isEnabled)
|
||||
return;
|
||||
|
||||
if (forceSortListeners)
|
||||
bool isDirty = false;
|
||||
auto dirtyIter = _priorityDirtyFlagMap.find(event->_type);
|
||||
if (dirtyIter != _priorityDirtyFlagMap.end())
|
||||
{
|
||||
isDirty = dirtyIter->second;
|
||||
}
|
||||
|
||||
if (forceSortListeners || isDirty)
|
||||
{
|
||||
sortAllEventListenerItemsForType(event->_type);
|
||||
// Sets the dirty flag to false
|
||||
if (isDirty)
|
||||
{
|
||||
dirtyIter->second = false;
|
||||
}
|
||||
}
|
||||
|
||||
DispatchGuard guard(_inDispatch);
|
||||
|
@ -279,22 +263,19 @@ void EventDispatcher::dispatchEvent(Event* event, bool forceSortListeners)
|
|||
return;
|
||||
}
|
||||
|
||||
if (_listeners)
|
||||
auto iter = _listeners.find(event->getType());
|
||||
if (iter != _listeners.end())
|
||||
{
|
||||
auto iter = _listeners->find(event->getType());
|
||||
if (iter != _listeners->end())
|
||||
auto listenerList = iter->second;
|
||||
for (auto& item : *listenerList)
|
||||
{
|
||||
auto listenerList = iter->second;
|
||||
for (auto& item : *listenerList)
|
||||
{
|
||||
CCASSERT(item, "listener item is invalid.");
|
||||
CCASSERT(item, "listener item is invalid.");
|
||||
|
||||
event->setCurrentTarget(item->node);
|
||||
item->listener->onEvent(event);
|
||||
event->setCurrentTarget(item->node);
|
||||
item->listener->onEvent(event);
|
||||
|
||||
if (event->isStopped())
|
||||
break;
|
||||
}
|
||||
if (event->isStopped())
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -500,11 +481,8 @@ void EventDispatcher::dispatchTouchEvent(TouchEvent* event)
|
|||
|
||||
void EventDispatcher::updateListenerItems()
|
||||
{
|
||||
if (!_listeners)
|
||||
return;
|
||||
|
||||
auto listenerItemIter = _listeners->begin();
|
||||
while (listenerItemIter != _listeners->end())
|
||||
auto listenerItemIter = _listeners.begin();
|
||||
while (listenerItemIter != _listeners.end())
|
||||
{
|
||||
for (auto iter = listenerItemIter->second->begin(); iter != listenerItemIter->second->end();)
|
||||
{
|
||||
|
@ -521,8 +499,9 @@ void EventDispatcher::updateListenerItems()
|
|||
|
||||
if (listenerItemIter->second->empty())
|
||||
{
|
||||
_priorityDirtyFlagMap.erase(listenerItemIter->first);
|
||||
delete listenerItemIter->second;
|
||||
listenerItemIter = _listeners->erase(listenerItemIter);
|
||||
listenerItemIter = _listeners.erase(listenerItemIter);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -536,12 +515,12 @@ void EventDispatcher::updateListenerItems()
|
|||
|
||||
for (auto& item : _toAddedListeners)
|
||||
{
|
||||
auto itr = _listeners->find(item->listener->type);
|
||||
if (itr == _listeners->end())
|
||||
auto itr = _listeners.find(item->listener->type);
|
||||
if (itr == _listeners.end())
|
||||
{
|
||||
listenerList = new std::vector<EventListenerItem*>();
|
||||
listenerList->reserve(100);
|
||||
_listeners->insert(std::make_pair(item->listener->type, listenerList));
|
||||
_listeners.insert(std::make_pair(item->listener->type, listenerList));
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -549,20 +528,16 @@ void EventDispatcher::updateListenerItems()
|
|||
}
|
||||
|
||||
listenerList->push_back(item);
|
||||
|
||||
setDirtyForEventType(item->listener->type, true);
|
||||
}
|
||||
_toAddedListeners.clear();
|
||||
}
|
||||
|
||||
if (_listeners->empty())
|
||||
{
|
||||
delete _listeners;
|
||||
_listeners = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void EventDispatcher::sortAllEventListenerItemsForType(const std::string &eventType)
|
||||
{
|
||||
if (_listeners == nullptr)
|
||||
if (eventType.empty())
|
||||
return;
|
||||
|
||||
auto listenerList = getListenerItemsForType(eventType);
|
||||
|
@ -610,13 +585,10 @@ void EventDispatcher::sortAllEventListenerItemsForType(const std::string &eventT
|
|||
|
||||
std::vector<EventDispatcher::EventListenerItem*>* EventDispatcher::getListenerItemsForType(const std::string &eventType)
|
||||
{
|
||||
if (_listeners != nullptr)
|
||||
auto iter = _listeners.find(eventType);
|
||||
if (iter != _listeners.end())
|
||||
{
|
||||
auto iter = _listeners->find(eventType);
|
||||
if (iter != _listeners->end())
|
||||
{
|
||||
return iter->second;
|
||||
}
|
||||
return iter->second;
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
|
@ -624,14 +596,15 @@ std::vector<EventDispatcher::EventListenerItem*>* EventDispatcher::getListenerIt
|
|||
|
||||
void EventDispatcher::removeListenersForEventType(const std::string& eventType)
|
||||
{
|
||||
if (_listeners == nullptr)
|
||||
if (eventType.empty())
|
||||
return;
|
||||
|
||||
auto listenerItemIter = _listeners->find(eventType);
|
||||
if (listenerItemIter != _listeners->end())
|
||||
auto listenerItemIter = _listeners.find(eventType);
|
||||
if (listenerItemIter != _listeners.end())
|
||||
{
|
||||
for (auto iter = listenerItemIter->second->begin(); iter != listenerItemIter->second->end(); ++iter)
|
||||
{
|
||||
(*iter)->listener->_isRegistered = false;
|
||||
(*iter)->listener->release();
|
||||
if (_inDispatch)
|
||||
{
|
||||
|
@ -647,20 +620,19 @@ void EventDispatcher::removeListenersForEventType(const std::string& eventType)
|
|||
{
|
||||
listenerItemIter->second->clear();
|
||||
delete listenerItemIter->second;
|
||||
_listeners->erase(listenerItemIter);
|
||||
_listeners.erase(listenerItemIter);
|
||||
_priorityDirtyFlagMap.erase(eventType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void EventDispatcher::removeAllListeners()
|
||||
{
|
||||
if (_listeners == nullptr)
|
||||
return;
|
||||
|
||||
for (auto listenerItemIter = _listeners->begin(); listenerItemIter != _listeners->end(); ++listenerItemIter)
|
||||
for (auto listenerItemIter = _listeners.begin(); listenerItemIter != _listeners.end(); ++listenerItemIter)
|
||||
{
|
||||
for (auto iter = listenerItemIter->second->begin(); iter != listenerItemIter->second->end(); ++iter)
|
||||
{
|
||||
(*iter)->listener->_isRegistered = false;
|
||||
(*iter)->listener->release();
|
||||
if (_inDispatch)
|
||||
{
|
||||
|
@ -676,13 +648,14 @@ void EventDispatcher::removeAllListeners()
|
|||
{
|
||||
listenerItemIter->second->clear();
|
||||
delete listenerItemIter->second;
|
||||
|
||||
_priorityDirtyFlagMap.clear();
|
||||
}
|
||||
}
|
||||
|
||||
if (!_inDispatch)
|
||||
{
|
||||
delete _listeners;
|
||||
_listeners = nullptr;
|
||||
_listeners.clear();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -697,4 +670,29 @@ bool EventDispatcher::isEnabled() const
|
|||
return _isEnabled;
|
||||
}
|
||||
|
||||
void EventDispatcher::setDirtyForEventType(const std::string& eventType, bool isDirty)
|
||||
{
|
||||
auto iter = _priorityDirtyFlagMap.find(eventType);
|
||||
if (iter == _priorityDirtyFlagMap.end())
|
||||
{
|
||||
_priorityDirtyFlagMap.insert(std::make_pair(eventType, isDirty));
|
||||
}
|
||||
else
|
||||
{
|
||||
iter->second = isDirty;
|
||||
}
|
||||
}
|
||||
|
||||
bool EventDispatcher::isDirtyForEventType(const std::string& eventType)
|
||||
{
|
||||
bool isDirty = false;
|
||||
auto iter = _priorityDirtyFlagMap.find(eventType);
|
||||
if (iter != _priorityDirtyFlagMap.end())
|
||||
{
|
||||
isDirty = iter->second;
|
||||
}
|
||||
|
||||
return isDirty;
|
||||
}
|
||||
|
||||
NS_CC_END
|
||||
|
|
|
@ -85,11 +85,8 @@ public:
|
|||
/** Removes all listeners */
|
||||
void removeAllListeners();
|
||||
|
||||
/** Sets listener's priority with node's draw order. */
|
||||
void setPriorityWithSceneGraph(EventListener* listener, Node* node);
|
||||
|
||||
/** Sets listener's priority with fixed value. */
|
||||
void setPriorityWithFixedValue(EventListener* listener, int fixedPriority);
|
||||
void setPriority(EventListener* listener, int fixedPriority);
|
||||
|
||||
/** Whether to enable dispatching events */
|
||||
void setEnabled(bool isEnabled);
|
||||
|
@ -101,7 +98,11 @@ public:
|
|||
* Also removes all EventListeners marked for deletion from the
|
||||
* event dispatcher list.
|
||||
*/
|
||||
void dispatchEvent(Event* event, bool forceSortListeners = true);
|
||||
void dispatchEvent(Event* event, bool forceSortListeners = false);
|
||||
|
||||
void setDirtyForEventType(const std::string& eventType, bool isDirty);
|
||||
|
||||
bool isDirtyForEventType(const std::string& eventType);
|
||||
|
||||
public:
|
||||
/** Destructor of EventDispatcher */
|
||||
|
@ -141,7 +142,11 @@ private:
|
|||
/**
|
||||
* Listeners map.
|
||||
*/
|
||||
std::map<std::string, std::vector<EventListenerItem*>*>* _listeners;
|
||||
std::map<std::string, std::vector<EventListenerItem*>*> _listeners;
|
||||
|
||||
/// Priority dirty flag
|
||||
std::map<std::string, bool> _priorityDirtyFlagMap;
|
||||
|
||||
std::vector<EventListenerItem*> _toAddedListeners;
|
||||
|
||||
int _inDispatch; ///< Whether it's in dispatching event
|
||||
|
|
|
@ -87,6 +87,7 @@ protected:
|
|||
bool _isRegistered; /// Whether the listener has been added to dispatcher.
|
||||
|
||||
friend class EventDispatcher;
|
||||
friend class Node;
|
||||
};
|
||||
|
||||
NS_CC_END
|
||||
|
|
|
@ -198,9 +198,17 @@ public:
|
|||
|
||||
static const char* EVENT_TYPE;
|
||||
|
||||
KeyboardEvent() : Event(EVENT_TYPE) {};
|
||||
KeyboardEvent(KeyCode keyCode, bool isPressed)
|
||||
: Event(EVENT_TYPE)
|
||||
, _keyCode(keyCode)
|
||||
, _isPressed(isPressed)
|
||||
{};
|
||||
|
||||
private:
|
||||
KeyCode _keyCode;
|
||||
bool _isPressed;
|
||||
|
||||
friend class KeyboardEventListener;
|
||||
};
|
||||
|
||||
NS_CC_END
|
||||
|
|
|
@ -394,31 +394,27 @@ static int32_t handle_touch_input(AInputEvent *event) {
|
|||
*/
|
||||
static int32_t handle_key_input(AInputEvent *event)
|
||||
{
|
||||
if (AKeyEvent_getAction(event) == AKEY_EVENT_ACTION_UP)
|
||||
{
|
||||
switch (AKeyEvent_getKeyCode(event))
|
||||
{
|
||||
case AKEYCODE_BACK:
|
||||
{
|
||||
cocos2d::KeyboardEvent event;
|
||||
event._keyCode = cocos2d::KeyboardEvent::KeyCode::KEY_BACKSPACE;
|
||||
cocos2d::EventDispatcher::getInstance()->dispatchEvent(&event);
|
||||
LOGI("AKEYCODE_BACK");
|
||||
}
|
||||
return 1;
|
||||
case AKEYCODE_MENU:
|
||||
{
|
||||
cocos2d::KeyboardEvent event;
|
||||
event._keyCode = cocos2d::KeyboardEvent::KeyCode::KEY_MENU;
|
||||
cocos2d::EventDispatcher::getInstance()->dispatchEvent(&event);
|
||||
LOGI("AKEYCODE_MENU");
|
||||
}
|
||||
return 1;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
if (AKeyEvent_getAction(event) == AKEY_EVENT_ACTION_UP)
|
||||
{
|
||||
switch (AKeyEvent_getKeyCode(event))
|
||||
{
|
||||
case AKEYCODE_BACK:
|
||||
{
|
||||
cocos2d::KeyboardEvent event(cocos2d::KeyboardEvent::KeyCode::KEY_BACKSPACE, false);
|
||||
cocos2d::EventDispatcher::getInstance()->dispatchEvent(&event);
|
||||
}
|
||||
return 1;
|
||||
case AKEYCODE_MENU:
|
||||
{
|
||||
cocos2d::KeyboardEvent event(cocos2d::KeyboardEvent::KeyCode::KEY_MENU, false);
|
||||
cocos2d::EventDispatcher::getInstance()->dispatchEvent(&event);
|
||||
}
|
||||
return 1;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -601,21 +597,24 @@ void android_main(struct android_app* state) {
|
|||
// ACONFIGURATION_ORIENTATION_ANY
|
||||
// ACONFIGURATION_ORIENTATION_PORT
|
||||
// ACONFIGURATION_ORIENTATION_SQUARE
|
||||
cocos2d::AccelerationEvent accEvent;
|
||||
accEvent.acc.x = event.acceleration.x;
|
||||
accEvent.acc.y = event.acceleration.y;
|
||||
accEvent.acc.z = event.acceleration.z;
|
||||
accEvent.acc.timestamp = 0;
|
||||
cocos2d::Acceleration acc;
|
||||
acc.x = event.acceleration.x;
|
||||
acc.y = event.acceleration.y;
|
||||
acc.z = event.acceleration.z;
|
||||
acc.timestamp = 0;
|
||||
cocos2d::AccelerationEvent accEvent(acc);
|
||||
|
||||
cocos2d::EventDispatcher::getInstance()->dispatchEvent(&accEvent);
|
||||
} else {
|
||||
// ACONFIGURATION_ORIENTATION_LAND
|
||||
// swap x and y parameters
|
||||
cocos2d::Acceleration acc;
|
||||
acc.x = -event.acceleration.y;
|
||||
acc.y = event.acceleration.x;
|
||||
acc.z = event.acceleration.z;
|
||||
acc.timestamp = 0;
|
||||
cocos2d::AccelerationEvent accEvent(acc);
|
||||
|
||||
cocos2d::AccelerationEvent accEvent;
|
||||
accEvent.acc.x = -event.acceleration.y;
|
||||
accEvent.acc.y = event.acceleration.x;
|
||||
accEvent.acc.z = event.acceleration.z;
|
||||
accEvent.acc.timestamp = 0;
|
||||
cocos2d::EventDispatcher::getInstance()->dispatchEvent(&accEvent);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -307,18 +307,20 @@ std::string FileUtilsApple::getFullPathForDirectoryAndFilename(const std::string
|
|||
Dictionary* FileUtilsApple::createDictionaryWithContentsOfFile(const std::string& filename)
|
||||
{
|
||||
std::string fullPath = fullPathForFilename(filename);
|
||||
NSString* pPath = [NSString stringWithUTF8String:fullPath.c_str()];
|
||||
NSDictionary* pDict = [NSDictionary dictionaryWithContentsOfFile:pPath];
|
||||
NSString* path = [NSString stringWithUTF8String:fullPath.c_str()];
|
||||
NSDictionary* dict = [NSDictionary dictionaryWithContentsOfFile:path];
|
||||
|
||||
if (pDict != nil)
|
||||
if (dict != nil)
|
||||
{
|
||||
Dictionary* pRet = Dictionary::create();
|
||||
for (id key in [pDict allKeys]) {
|
||||
id value = [pDict objectForKey:key];
|
||||
addValueToDict(key, value, pRet);
|
||||
auto ret = new Dictionary();
|
||||
ret->init();
|
||||
|
||||
for (id key in [dict allKeys]) {
|
||||
id value = [dict objectForKey:key];
|
||||
addValueToDict(key, value, ret);
|
||||
}
|
||||
|
||||
return pRet;
|
||||
return ret;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
|
@ -95,8 +95,7 @@ static CCAccelerometerDispatcher* s_pAccelerometerDispatcher;
|
|||
break;
|
||||
}
|
||||
|
||||
cocos2d::AccelerationEvent event;
|
||||
event.acc = *_acceleration;
|
||||
cocos2d::AccelerationEvent event(*_acceleration);
|
||||
cocos2d::EventDispatcher::getInstance()->dispatchEvent(&event);
|
||||
}
|
||||
|
||||
|
|
|
@ -60,7 +60,13 @@ int Application::run()
|
|||
usleep((_animationInterval - iCurTime+iLastTime)*1000);
|
||||
}
|
||||
}
|
||||
|
||||
/* Only work on Desktop
|
||||
* Director::mainLoop is really one frame logic
|
||||
* when we want to close the window, we should call Director::end();
|
||||
* then call Director::mainLoop to do release of internal resources
|
||||
*/
|
||||
Director::getInstance()->end();
|
||||
Director::getInstance()->mainLoop();
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
|
|
@ -222,9 +222,7 @@ void EGLViewEventHandler::OnGLFWMouseMoveCallBack(GLFWwindow* window, double x,
|
|||
|
||||
void EGLViewEventHandler::OnGLFWKeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods)
|
||||
{
|
||||
KeyboardEvent event;
|
||||
event._keyCode = g_keyCodeMap[key];
|
||||
event._isPressed = (GLFW_PRESS == action);
|
||||
KeyboardEvent event(g_keyCodeMap[key], GLFW_PRESS == action);
|
||||
EventDispatcher::getInstance()->dispatchEvent(&event);
|
||||
}
|
||||
|
||||
|
|
|
@ -60,6 +60,14 @@ int Application::run()
|
|||
Director::getInstance()->mainLoop();
|
||||
pMainWnd->pollEvents();
|
||||
}
|
||||
|
||||
/* Only work on Desktop
|
||||
* Director::mainLoop is really one frame logic
|
||||
* when we want to close the window, we should call Director::end();
|
||||
* then call Director::mainLoop to do release of internal resources
|
||||
*/
|
||||
Director::getInstance()->end();
|
||||
Director::getInstance()->mainLoop();
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -238,9 +238,7 @@ void EGLViewEventHandler::OnGLFWMouseMoveCallBack(GLFWwindow* window, double x,
|
|||
|
||||
void EGLViewEventHandler::OnGLFWKeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods)
|
||||
{
|
||||
KeyboardEvent event;
|
||||
event._keyCode = g_keyCodeMap[key];
|
||||
event._isPressed = (GLFW_PRESS == action);
|
||||
KeyboardEvent event(g_keyCodeMap[key], GLFW_PRESS == action);
|
||||
EventDispatcher::getInstance()->dispatchEvent(&event);
|
||||
}
|
||||
|
||||
|
|
|
@ -65,6 +65,13 @@ int Application::run()
|
|||
}
|
||||
}
|
||||
|
||||
/* Only work on Desktop
|
||||
* Director::mainLoop is really one frame logic
|
||||
* when we want to close the window, we should call Director::end();
|
||||
* then call Director::mainLoop to do release of internal resources
|
||||
*/
|
||||
Director::getInstance()->end();
|
||||
Director::getInstance()->mainLoop();
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -340,9 +340,7 @@ void EGLViewEventHandler::OnGLFWMouseMoveCallBack(GLFWwindow* window, double x,
|
|||
|
||||
void EGLViewEventHandler::OnGLFWKeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods)
|
||||
{
|
||||
KeyboardEvent event;
|
||||
event._keyCode = g_keyCodeMap[key];
|
||||
event._isPressed = (GLFW_PRESS == action);
|
||||
KeyboardEvent event(g_keyCodeMap[key], GLFW_PRESS == action);
|
||||
EventDispatcher::getInstance()->dispatchEvent(&event);
|
||||
}
|
||||
|
||||
|
|
|
@ -362,6 +362,7 @@ void ActionNode::stopAction()
|
|||
int ActionNode::getFirstFrameIndex()
|
||||
{
|
||||
int frameindex = 99999;
|
||||
bool bFindFrame = false;
|
||||
for (int n = 0; n < _frameArrayNum; n++)
|
||||
{
|
||||
Array* cArray = (Array*)(_frameArray->getObjectAtIndex(n));
|
||||
|
@ -369,7 +370,7 @@ int ActionNode::getFirstFrameIndex()
|
|||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
bFindFrame = true;
|
||||
ActionFrame* frame = (ActionFrame*)(cArray->getObjectAtIndex(0));
|
||||
int iFrameIndex = frame->getFrameIndex();
|
||||
|
||||
|
@ -378,13 +379,17 @@ int ActionNode::getFirstFrameIndex()
|
|||
frameindex = iFrameIndex;
|
||||
}
|
||||
}
|
||||
|
||||
if (!bFindFrame)
|
||||
{
|
||||
frameindex = 0;
|
||||
}
|
||||
return frameindex;
|
||||
}
|
||||
|
||||
int ActionNode::getLastFrameIndex()
|
||||
{
|
||||
int frameindex = -1;
|
||||
bool bFindFrame = false;
|
||||
for (int n = 0; n < _frameArrayNum; n++)
|
||||
{
|
||||
Array* cArray = (Array*)(_frameArray->getObjectAtIndex(n));
|
||||
|
@ -392,6 +397,7 @@ int ActionNode::getLastFrameIndex()
|
|||
{
|
||||
continue;
|
||||
}
|
||||
bFindFrame = true;
|
||||
int lastInex = cArray->count() - 1;
|
||||
ActionFrame* frame = (ActionFrame*)(cArray->getObjectAtIndex(lastInex));
|
||||
int iFrameIndex = frame->getFrameIndex();
|
||||
|
@ -401,7 +407,10 @@ int ActionNode::getLastFrameIndex()
|
|||
frameindex = iFrameIndex;
|
||||
}
|
||||
}
|
||||
|
||||
if (!bFindFrame)
|
||||
{
|
||||
frameindex = 0;
|
||||
}
|
||||
return frameindex;
|
||||
}
|
||||
bool ActionNode::updateActionToTimeLine(float fTime)
|
||||
|
|
|
@ -118,6 +118,7 @@ void ActionObject::addActionNode(ActionNode* node)
|
|||
return;
|
||||
}
|
||||
_actionNodeList->addObject(node);
|
||||
node->setUnitTime(_fUnitTime);
|
||||
}
|
||||
void ActionObject::removeActionNode(ActionNode* node)
|
||||
{
|
||||
|
|
|
@ -24,8 +24,8 @@ int APIENTRY _tWinMain(HINSTANCE hInstance,
|
|||
|
||||
// create the application instance
|
||||
AppDelegate app;
|
||||
EGLView* eglView = new EGLView();
|
||||
eglView->init("TestCPP",900,640);
|
||||
EGLView eglView;
|
||||
eglView.init("AssetsManagerTest",900,640);
|
||||
|
||||
int ret = Application::getInstance()->run();
|
||||
|
||||
|
|
|
@ -12,7 +12,7 @@ int main(int argc, char **argv)
|
|||
{
|
||||
// create the application instance
|
||||
AppDelegate app;
|
||||
EGLView* eglView = new EGLView();
|
||||
eglView->init("TestCPP",900,640);
|
||||
EGLView eglView;
|
||||
eglView.init("HelloCpp",900,640);
|
||||
return Application::getInstance()->run();
|
||||
}
|
||||
|
|
|
@ -30,8 +30,8 @@ USING_NS_CC;
|
|||
int main(int argc, char *argv[])
|
||||
{
|
||||
AppDelegate app;
|
||||
EGLView* eglView = new EGLView();
|
||||
eglView->init("TestCPP",900,640);
|
||||
EGLView eglView;
|
||||
eglView.init("HelloCpp",900,640);
|
||||
return Application::getInstance()->run();
|
||||
}
|
||||
|
||||
|
|
|
@ -14,7 +14,7 @@ int APIENTRY _tWinMain(HINSTANCE hInstance,
|
|||
|
||||
// create the application instance
|
||||
AppDelegate app;
|
||||
EGLView* eglView = new EGLView();
|
||||
eglView->init("TestCPP",900,640);
|
||||
EGLView eglView;
|
||||
eglView.init("HelloCpp",900,640);
|
||||
return Application::getInstance()->run();
|
||||
}
|
||||
|
|
|
@ -12,7 +12,7 @@ int main(int argc, char **argv)
|
|||
{
|
||||
// create the application instance
|
||||
AppDelegate app;
|
||||
EGLView* eglView = new EGLView();
|
||||
eglView->init("TestCPP",900,640);
|
||||
EGLView eglView;
|
||||
eglView.init("SimpleGame",900,640);
|
||||
return Application::getInstance()->run();
|
||||
}
|
||||
|
|
|
@ -30,8 +30,8 @@ USING_NS_CC;
|
|||
int main(int argc, char *argv[])
|
||||
{
|
||||
AppDelegate app;
|
||||
EGLView* eglView = new EGLView();
|
||||
eglView->init("TestCPP",900,640);
|
||||
EGLView eglView;
|
||||
eglView.init("SimpleGame",900,640);
|
||||
return Application::getInstance()->run();
|
||||
}
|
||||
|
||||
|
|
|
@ -7,13 +7,18 @@
|
|||
//
|
||||
|
||||
#include "NewEventDispatcherTest.h"
|
||||
#include "testResource.h"
|
||||
|
||||
namespace {
|
||||
|
||||
std::function<Layer*()> createFunctions[] =
|
||||
{
|
||||
CL(TouchableSpriteTest),
|
||||
CL(FixedPriorityTest)
|
||||
CL(FixedPriorityTest),
|
||||
CL(RemoveListenerWhenDispatching),
|
||||
CL(CustomEventTest),
|
||||
CL(LabelKeyboardEventTest),
|
||||
CL(SpriteAccelerationEventTest)
|
||||
};
|
||||
|
||||
unsigned int TEST_CASE_COUNT = sizeof(createFunctions) / sizeof(createFunctions[0]);
|
||||
|
@ -148,7 +153,7 @@ void TouchableSpriteTest::onEnter()
|
|||
|
||||
if (rect.containsPoint(locationInNode))
|
||||
{
|
||||
log("sprite tag %d, began... x = %f, y = %f", target->getTag(), locationInNode.x, locationInNode.y);
|
||||
log("sprite began... x = %f, y = %f", locationInNode.x, locationInNode.y);
|
||||
target->setOpacity(180);
|
||||
return true;
|
||||
}
|
||||
|
@ -162,7 +167,7 @@ void TouchableSpriteTest::onEnter()
|
|||
|
||||
listener1->onTouchEnded = [=](Touch* touch, Event* event){
|
||||
auto target = static_cast<Sprite*>(event->getCurrentTarget());
|
||||
log("sprite tag %d onTouchesEnded.. ", target->getTag());
|
||||
log("sprite onTouchesEnded.. ");
|
||||
target->setOpacity(255);
|
||||
if (target == sprite2)
|
||||
{
|
||||
|
@ -186,7 +191,7 @@ std::string TouchableSpriteTest::title()
|
|||
|
||||
std::string TouchableSpriteTest::subtitle()
|
||||
{
|
||||
return "";
|
||||
return "Please drag the blocks";
|
||||
}
|
||||
|
||||
// FixedPriorityChangedTest
|
||||
|
@ -213,7 +218,7 @@ public:
|
|||
auto listener = TouchEventListener::create(Touch::DispatchMode::ONE_BY_ONE);
|
||||
listener->setSwallowTouches(true);
|
||||
|
||||
listener->onTouchBegan = [&](Touch* touch, Event* event){
|
||||
listener->onTouchBegan = [=](Touch* touch, Event* event){
|
||||
|
||||
Point locationInNode = this->convertToNodeSpace(touch->getLocation());
|
||||
Size s = this->getContentSize();
|
||||
|
@ -227,11 +232,11 @@ public:
|
|||
return false;
|
||||
};
|
||||
|
||||
listener->onTouchMoved = [&](Touch* touch, Event* event){
|
||||
listener->onTouchMoved = [=](Touch* touch, Event* event){
|
||||
//this->setPosition(this->getPosition() + touch->getDelta());
|
||||
};
|
||||
|
||||
listener->onTouchEnded = [&](Touch* touch, Event* event){
|
||||
listener->onTouchEnded = [=](Touch* touch, Event* event){
|
||||
this->setColor(Color3B::WHITE);
|
||||
};
|
||||
|
||||
|
@ -299,3 +304,236 @@ std::string FixedPriorityTest::subtitle()
|
|||
{
|
||||
return "Fixed Priority, Blue: 30, Red: 20, Yellow: 10\n The lower value the higher priority will be.";
|
||||
}
|
||||
|
||||
// RemoveListenerWhenDispatching
|
||||
void RemoveListenerWhenDispatching::onEnter()
|
||||
{
|
||||
EventDispatcherTestDemo::onEnter();
|
||||
|
||||
auto dispatcher = EventDispatcher::getInstance();
|
||||
|
||||
Point origin = Director::getInstance()->getVisibleOrigin();
|
||||
Size size = Director::getInstance()->getVisibleSize();
|
||||
|
||||
auto sprite1 = Sprite::create("Images/CyanSquare.png");
|
||||
sprite1->setPosition(origin+Point(size.width/2, size.height/2));
|
||||
addChild(sprite1, 10);
|
||||
|
||||
// Make sprite1 touchable
|
||||
auto listener1 = TouchEventListener::create(Touch::DispatchMode::ONE_BY_ONE);
|
||||
listener1->setSwallowTouches(true);
|
||||
setUserObject(listener1);
|
||||
|
||||
std::shared_ptr<bool> firstClick(new bool(true));
|
||||
|
||||
listener1->onTouchBegan = [=](Touch* touch, Event* event){
|
||||
Point locationInNode = sprite1->convertToNodeSpace(touch->getLocation());
|
||||
Size s = sprite1->getContentSize();
|
||||
Rect rect = Rect(0, 0, s.width, s.height);
|
||||
|
||||
if (rect.containsPoint(locationInNode))
|
||||
{
|
||||
sprite1->setColor(Color3B::RED);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
listener1->onTouchEnded = [=](Touch* touch, Event* event){
|
||||
sprite1->setColor(Color3B::WHITE);
|
||||
};
|
||||
|
||||
dispatcher->addEventListenerWithSceneGraphPriority(listener1, sprite1);
|
||||
|
||||
auto statusLabel = LabelTTF::create("The sprite could be touched!", "", 20);
|
||||
statusLabel->setPosition(origin + Point(size.width/2, size.height-90));
|
||||
addChild(statusLabel);
|
||||
std::shared_ptr<bool> enable(new bool(true));
|
||||
// Enable/Disable item
|
||||
auto toggleItem = MenuItemToggle::createWithCallback([=](Object* sender){
|
||||
if (*enable)
|
||||
{
|
||||
dispatcher->removeEventListener(listener1);
|
||||
statusLabel->setString("The sprite could not be touched!");
|
||||
|
||||
(*enable) = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
dispatcher->addEventListenerWithSceneGraphPriority(listener1, sprite1);
|
||||
statusLabel->setString("The sprite could be touched!");
|
||||
|
||||
(*enable) = true;
|
||||
}
|
||||
}, MenuItemFont::create("Enabled"), MenuItemFont::create("Disabled"), NULL);
|
||||
|
||||
toggleItem->setPosition(origin + Point(size.width/2, 80));
|
||||
auto menu = Menu::create(toggleItem, nullptr);
|
||||
menu->setPosition(Point(0, 0));
|
||||
menu->setAnchorPoint(Point(0, 0));
|
||||
addChild(menu, -1);
|
||||
}
|
||||
|
||||
std::string RemoveListenerWhenDispatching::title()
|
||||
{
|
||||
return "Add and remove listener\n when dispatching event";
|
||||
}
|
||||
|
||||
std::string RemoveListenerWhenDispatching::subtitle()
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
// CustomEventTest
|
||||
void CustomEventTest::onEnter()
|
||||
{
|
||||
EventDispatcherTestDemo::onEnter();
|
||||
|
||||
auto dispatcher = EventDispatcher::getInstance();
|
||||
|
||||
Point origin = Director::getInstance()->getVisibleOrigin();
|
||||
Size size = Director::getInstance()->getVisibleSize();
|
||||
|
||||
auto statusLabel = LabelTTF::create("No custom event received!", "", 20);
|
||||
statusLabel->setPosition(origin + Point(size.width/2, size.height-90));
|
||||
addChild(statusLabel);
|
||||
|
||||
_listener = EventListener::create("game_custom_event", [=](Event* event){
|
||||
std::string str("Custom event received, ");
|
||||
char* buf = static_cast<char*>(event->getUserData());
|
||||
str += buf;
|
||||
str += " times";
|
||||
statusLabel->setString(str.c_str());
|
||||
delete[] buf;
|
||||
});
|
||||
|
||||
dispatcher->addEventListenerWithFixedPriority(_listener, 1);
|
||||
|
||||
auto sendItem = MenuItemFont::create("Send Custom Event", [=](Object* sender){
|
||||
static int count = 0;
|
||||
++count;
|
||||
char* buf = new char[10];
|
||||
sprintf(buf, "%d", count);
|
||||
Event event("game_custom_event");
|
||||
event.setUserData(buf);
|
||||
dispatcher->dispatchEvent(&event);
|
||||
});
|
||||
sendItem->setPosition(origin + Point(size.width/2, size.height/2));
|
||||
auto menu = Menu::create(sendItem, nullptr);
|
||||
menu->setPosition(Point(0, 0));
|
||||
menu->setAnchorPoint(Point(0, 0));
|
||||
addChild(menu, -1);
|
||||
}
|
||||
|
||||
void CustomEventTest::onExit()
|
||||
{
|
||||
EventDispatcher::getInstance()->removeEventListener(_listener);
|
||||
EventDispatcherTestDemo::onExit();
|
||||
}
|
||||
|
||||
std::string CustomEventTest::title()
|
||||
{
|
||||
return "Send custom event";
|
||||
}
|
||||
|
||||
std::string CustomEventTest::subtitle()
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
// LabelKeyboardEventTest
|
||||
void LabelKeyboardEventTest::onEnter()
|
||||
{
|
||||
EventDispatcherTestDemo::onEnter();
|
||||
|
||||
auto dispatcher = EventDispatcher::getInstance();
|
||||
|
||||
Point origin = Director::getInstance()->getVisibleOrigin();
|
||||
Size size = Director::getInstance()->getVisibleSize();
|
||||
|
||||
auto statusLabel = LabelTTF::create("No keyboard event received!", "", 20);
|
||||
statusLabel->setPosition(origin + Point(size.width/2, size.height/2));
|
||||
addChild(statusLabel);
|
||||
|
||||
auto listener = KeyboardEventListener::create();
|
||||
listener->onKeyPressed = [](KeyboardEvent::KeyCode keyCode, Event* event){
|
||||
char buf[100] = {0};
|
||||
sprintf(buf, "Key %d was pressed!", (int)keyCode);
|
||||
auto label = static_cast<LabelTTF*>(event->getCurrentTarget());
|
||||
label->setString(buf);
|
||||
};
|
||||
|
||||
listener->onKeyReleased = [](KeyboardEvent::KeyCode keyCode, Event* event){
|
||||
char buf[100] = {0};
|
||||
sprintf(buf, "Key %d was released!", (int)keyCode);
|
||||
auto label = static_cast<LabelTTF*>(event->getCurrentTarget());
|
||||
label->setString(buf);
|
||||
};
|
||||
|
||||
dispatcher->addEventListenerWithSceneGraphPriority(listener, statusLabel);
|
||||
}
|
||||
|
||||
std::string LabelKeyboardEventTest::title()
|
||||
{
|
||||
return "Label Receives Keyboard Event";
|
||||
}
|
||||
|
||||
std::string LabelKeyboardEventTest::subtitle()
|
||||
{
|
||||
return "Please click keyboard\n(Only available on Desktop and Android)";
|
||||
}
|
||||
|
||||
// SpriteAccelerationEventTest
|
||||
void SpriteAccelerationEventTest::onEnter()
|
||||
{
|
||||
#define FIX_POS(_pos, _min, _max) \
|
||||
if (_pos < _min) \
|
||||
_pos = _min; \
|
||||
else if (_pos > _max) \
|
||||
_pos = _max; \
|
||||
|
||||
EventDispatcherTestDemo::onEnter();
|
||||
|
||||
auto dispatcher = EventDispatcher::getInstance();
|
||||
|
||||
Point origin = Director::getInstance()->getVisibleOrigin();
|
||||
Size size = Director::getInstance()->getVisibleSize();
|
||||
|
||||
Device::setAccelerometerEnabled(true);
|
||||
|
||||
auto sprite = Sprite::create(s_Ball);
|
||||
sprite->setPosition(origin + Point(size.width/2, size.height/2));
|
||||
addChild(sprite);
|
||||
|
||||
auto listener = AccelerationEventListener::create([=](Acceleration* acc, Event* event){
|
||||
auto ballSize = sprite->getContentSize();
|
||||
|
||||
auto ptNow = sprite->getPosition();
|
||||
|
||||
log("acc: x = %lf, y = %lf", acc->x, acc->y);
|
||||
|
||||
ptNow.x += acc->x * 9.81f;
|
||||
ptNow.y += acc->y * 9.81f;
|
||||
|
||||
FIX_POS(ptNow.x, (VisibleRect::left().x+ballSize.width / 2.0), (VisibleRect::right().x - ballSize.width / 2.0));
|
||||
FIX_POS(ptNow.y, (VisibleRect::bottom().y+ballSize.height / 2.0), (VisibleRect::top().y - ballSize.height / 2.0));
|
||||
sprite->setPosition(ptNow);
|
||||
});
|
||||
|
||||
dispatcher->addEventListenerWithSceneGraphPriority(listener, sprite);
|
||||
}
|
||||
|
||||
void SpriteAccelerationEventTest::onExit()
|
||||
{
|
||||
Device::setAccelerometerEnabled(false);
|
||||
}
|
||||
|
||||
std::string SpriteAccelerationEventTest::title()
|
||||
{
|
||||
return "Sprite Receives Acceleration Event";
|
||||
}
|
||||
|
||||
std::string SpriteAccelerationEventTest::subtitle()
|
||||
{
|
||||
return "Please move your device\n(Only available on mobile)";
|
||||
}
|
|
@ -47,5 +47,40 @@ public:
|
|||
virtual std::string subtitle();
|
||||
};
|
||||
|
||||
class RemoveListenerWhenDispatching : public EventDispatcherTestDemo
|
||||
{
|
||||
public:
|
||||
virtual void onEnter();
|
||||
virtual std::string title();
|
||||
virtual std::string subtitle();
|
||||
};
|
||||
|
||||
class CustomEventTest : public EventDispatcherTestDemo
|
||||
{
|
||||
public:
|
||||
virtual void onEnter() override;
|
||||
virtual void onExit() override;
|
||||
virtual std::string title() override;
|
||||
virtual std::string subtitle() override;
|
||||
private:
|
||||
EventListener* _listener;
|
||||
};
|
||||
|
||||
class LabelKeyboardEventTest : public EventDispatcherTestDemo
|
||||
{
|
||||
public:
|
||||
virtual void onEnter() override;
|
||||
virtual std::string title() override;
|
||||
virtual std::string subtitle() override;
|
||||
};
|
||||
|
||||
class SpriteAccelerationEventTest : public EventDispatcherTestDemo
|
||||
{
|
||||
public:
|
||||
virtual void onEnter() override;
|
||||
virtual void onExit() override;
|
||||
virtual std::string title() override;
|
||||
virtual std::string subtitle() override;
|
||||
};
|
||||
|
||||
#endif /* defined(__samples__NewEventDispatcherTest__) */
|
||||
|
|
|
@ -260,7 +260,7 @@ void TouchesPerformTest3::onEnter()
|
|||
{
|
||||
CC_PROFILER_START(TOUCH_PROFILER_NAME);
|
||||
|
||||
dispatcher->dispatchEvent(&event, true);
|
||||
dispatcher->dispatchEvent(&event, false);
|
||||
|
||||
CC_PROFILER_STOP(TOUCH_PROFILER_NAME);
|
||||
}
|
||||
|
|
|
@ -14,8 +14,6 @@ struct {
|
|||
const char *test_name;
|
||||
std::function<TestScene*()> callback;
|
||||
} g_aTestNames[] = {
|
||||
|
||||
{ "NewEventDispatcherTest", []() { return new EventDispatcherTestScene(); } },
|
||||
{ "Accelerometer", []() { return new AccelerometerTestScene(); } },
|
||||
{ "ActionManagerTest", [](){return new ActionManagerTestScene(); } },
|
||||
{ "ActionsEaseTest", [](){return new ActionsEaseTestScene();} },
|
||||
|
@ -44,6 +42,7 @@ struct {
|
|||
#endif
|
||||
{ "CurrentLanguageTest", []() { return new CurrentLanguageTestScene(); } },
|
||||
{ "DrawPrimitivesTest", [](){return new DrawPrimitivesTestScene();} },
|
||||
{ "EventDispatcherTest(NEW)", []() { return new EventDispatcherTestScene(); } },
|
||||
{ "EffectAdvancedTest", []() { return new EffectAdvanceScene(); } },
|
||||
{ "EffectsTest", [](){return new EffectTestScene();} },
|
||||
{ "ExtensionsTest", []() { return new ExtensionsTestScene(); } },
|
||||
|
|
|
@ -13,7 +13,7 @@ int main(int argc, char **argv)
|
|||
{
|
||||
// create the application instance
|
||||
AppDelegate app;
|
||||
EGLView* eglView = new EGLView();
|
||||
eglView->init("TestCPP",900,640);
|
||||
EGLView eglView;
|
||||
eglView.init("TestCPP",900,640);
|
||||
return Application::getInstance()->run();
|
||||
}
|
||||
|
|
|
@ -30,7 +30,7 @@ USING_NS_CC;
|
|||
int main(int argc, char *argv[])
|
||||
{
|
||||
AppDelegate app;
|
||||
EGLView* eglView = new EGLView();
|
||||
eglView->init("TestCPP",900,640);
|
||||
EGLView eglView;
|
||||
eglView.init("TestCPP",900,640);
|
||||
return Application::getInstance()->run();
|
||||
}
|
||||
|
|
|
@ -14,7 +14,7 @@ int APIENTRY _tWinMain(HINSTANCE hInstance,
|
|||
|
||||
// create the application instance
|
||||
AppDelegate app;
|
||||
EGLView* eglView = new EGLView();
|
||||
eglView->init("TestCPP",960,640);
|
||||
EGLView eglView;
|
||||
eglView.init("TestCPP",960,640);
|
||||
return Application::getInstance()->run();
|
||||
}
|
||||
|
|
|
@ -30,7 +30,7 @@ USING_NS_CC;
|
|||
int main(int argc, char *argv[])
|
||||
{
|
||||
AppDelegate app;
|
||||
EGLView* eglView = new EGLView();
|
||||
eglView->init("TestCPP",900,640);
|
||||
EGLView eglView;
|
||||
eglView.init("CocosDragonJS",900,640);
|
||||
return Application::getInstance()->run();
|
||||
}
|
||||
|
|
|
@ -24,8 +24,8 @@ int APIENTRY _tWinMain(HINSTANCE hInstance,
|
|||
|
||||
// create the application instance
|
||||
AppDelegate app;
|
||||
EGLView* eglView = new EGLView();
|
||||
eglView->init("TestCPP",900,640);
|
||||
EGLView eglView;
|
||||
eglView.init("CocosDragonJS",900,640);
|
||||
|
||||
int ret = Application::getInstance()->run();
|
||||
|
||||
|
|
|
@ -30,7 +30,7 @@ USING_NS_CC;
|
|||
int main(int argc, char *argv[])
|
||||
{
|
||||
AppDelegate app;
|
||||
EGLView* eglView = new EGLView();
|
||||
eglView->init("TestCPP",900,640);
|
||||
EGLView eglView;
|
||||
eglView.init("CrystalCraze",900,640);
|
||||
return Application::getInstance()->run();
|
||||
}
|
||||
|
|
|
@ -24,8 +24,8 @@ int APIENTRY _tWinMain(HINSTANCE hInstance,
|
|||
|
||||
// create the application instance
|
||||
AppDelegate app;
|
||||
EGLView* eglView = new EGLView();
|
||||
eglView->init("TestCPP",900,640);
|
||||
EGLView eglView;
|
||||
eglView.init("CrystalCraze",900,640);
|
||||
|
||||
int ret = Application::getInstance()->run();
|
||||
|
||||
|
|
|
@ -30,8 +30,8 @@ USING_NS_CC;
|
|||
int main(int argc, char *argv[])
|
||||
{
|
||||
AppDelegate app;
|
||||
EGLView* eglView = new EGLView();
|
||||
eglView->init("TestCPP",900,640);
|
||||
EGLView eglView;
|
||||
eglView.init("MoonWarriors",900,640);
|
||||
return Application::getInstance()->run();
|
||||
}
|
||||
|
||||
|
|
|
@ -24,8 +24,8 @@ int APIENTRY _tWinMain(HINSTANCE hInstance,
|
|||
|
||||
// create the application instance
|
||||
AppDelegate app;
|
||||
EGLView* eglView = new EGLView();
|
||||
eglView->init("TestCPP",900,640);
|
||||
EGLView eglView;
|
||||
eglView.init("MoonWarriors",900,640);
|
||||
|
||||
int ret = Application::getInstance()->run();
|
||||
|
||||
|
|
|
@ -1 +1 @@
|
|||
Subproject commit 022292761c6a9d056e25cc6e844430650208513f
|
||||
Subproject commit 6d8e58221b9753981e2ee65738d8b93671835b28
|
|
@ -30,8 +30,8 @@ USING_NS_CC;
|
|||
int main(int argc, char *argv[])
|
||||
{
|
||||
AppDelegate app;
|
||||
EGLView* eglView = new EGLView();
|
||||
eglView->init("TestCPP",900,640);
|
||||
EGLView eglView;
|
||||
eglView.init("TestJavascript",900,640);
|
||||
return Application::getInstance()->run();
|
||||
}
|
||||
|
||||
|
|
|
@ -24,8 +24,8 @@ int APIENTRY _tWinMain(HINSTANCE hInstance,
|
|||
|
||||
// create the application instance
|
||||
AppDelegate app;
|
||||
EGLView* eglView = new EGLView();
|
||||
eglView->init("TestCPP",900,640);
|
||||
EGLView eglView;
|
||||
eglView.init("TestJavascript",900,640);
|
||||
|
||||
int ret = Application::getInstance()->run();
|
||||
|
||||
|
|
|
@ -30,8 +30,8 @@ USING_NS_CC;
|
|||
int main(int argc, char *argv[])
|
||||
{
|
||||
AppDelegate app;
|
||||
EGLView* eglView = new EGLView();
|
||||
eglView->init("TestCPP",900,640);
|
||||
EGLView eglView;
|
||||
eglView.init("WatermelonWithMe",900,640);
|
||||
return Application::getInstance()->run();
|
||||
}
|
||||
|
||||
|
|
|
@ -24,8 +24,8 @@ int APIENTRY _tWinMain(HINSTANCE hInstance,
|
|||
|
||||
// create the application instance
|
||||
AppDelegate app;
|
||||
EGLView* eglView = new EGLView();
|
||||
eglView->init("TestCPP",900,640);
|
||||
EGLView eglView;
|
||||
eglView.init("WatermelonWithMe",900,640);
|
||||
|
||||
int ret = Application::getInstance()->run();
|
||||
|
||||
|
|
|
@ -12,7 +12,7 @@ int main(int argc, char **argv)
|
|||
{
|
||||
// create the application instance
|
||||
AppDelegate app;
|
||||
EGLView* eglView = new EGLView();
|
||||
eglView->init("TestCPP",900,640);
|
||||
EGLView eglView;
|
||||
eglView.init("HelloLua",900,640);
|
||||
return Application::getInstance()->run();
|
||||
}
|
||||
|
|
|
@ -30,7 +30,7 @@ USING_NS_CC;
|
|||
int main(int argc, char *argv[])
|
||||
{
|
||||
AppDelegate app;
|
||||
EGLView* eglView = new EGLView();
|
||||
eglView->init("TestCPP",900,640);
|
||||
EGLView eglView;
|
||||
eglView.init("HelloLua",900,640);
|
||||
return Application::getInstance()->run();
|
||||
}
|
||||
|
|
|
@ -24,8 +24,8 @@ int APIENTRY _tWinMain(HINSTANCE hInstance,
|
|||
|
||||
// create the application instance
|
||||
AppDelegate app;
|
||||
EGLView* eglView = new EGLView();
|
||||
eglView->init("TestCPP",900,640);
|
||||
EGLView eglView;
|
||||
eglView.init("HelloLua",900,640);
|
||||
|
||||
int ret = Application::getInstance()->run();
|
||||
|
||||
|
|
|
@ -1,62 +1,57 @@
|
|||
local function AccelerometerMainLayer()
|
||||
|
||||
local function title()
|
||||
return "AccelerometerTest"
|
||||
end
|
||||
local pLayer = cc.Layer:create()
|
||||
local function title()
|
||||
return "AccelerometerTest"
|
||||
end
|
||||
local layer = cc.Layer:create()
|
||||
|
||||
pLayer:setAccelerometerEnabled(true)
|
||||
layer:setAccelerometerEnabled(true)
|
||||
|
||||
local pLabel = cc.LabelTTF:create(title(), "Arial", 32)
|
||||
pLayer:addChild(pLabel, 1)
|
||||
pLabel:setPosition( cc.p(VisibleRect:center().x, VisibleRect:top().y - 50) )
|
||||
local label = cc.LabelTTF:create(title(), "Arial", 32)
|
||||
layer:addChild(label, 1)
|
||||
label:setPosition( cc.p(VisibleRect:center().x, VisibleRect:top().y - 50) )
|
||||
|
||||
local pBall = cc.Sprite:create("Images/ball.png")
|
||||
pBall:setPosition(cc.p(VisibleRect:center().x, VisibleRect:center().y))
|
||||
pLayer:addChild(pBall)
|
||||
local ball = cc.Sprite:create("Images/ball.png")
|
||||
ball:setPosition(cc.p(VisibleRect:center().x, VisibleRect:center().y))
|
||||
layer:addChild(ball)
|
||||
|
||||
pBall:retain()
|
||||
ball:retain()
|
||||
|
||||
local function didAccelerate(x,y,z,timestamp)
|
||||
local pDir = cc.Director:getInstance()
|
||||
|
||||
if nil == pBall then
|
||||
return
|
||||
end
|
||||
if nil == ball then
|
||||
return
|
||||
end
|
||||
|
||||
local szBall = pBall:getContentSize()
|
||||
local ptNowX,ptNowY = pBall:getPosition()
|
||||
local szBall = ball:getContentSize()
|
||||
local ptNowX,ptNowY = ball:getPosition()
|
||||
|
||||
local ptTmp = pDir:convertToUI(cc.p(ptNowX,ptNowY))
|
||||
ptTmp.x = ptTmp.x + x * 9.81
|
||||
ptTmp.y = ptTmp.y - y * 9.81
|
||||
ptNowX = ptNowX - x
|
||||
ptNowY = ptNowY - y
|
||||
|
||||
|
||||
local ptNext = pDir:convertToGL(cc.p(ptTmp.x,ptTmp.y))
|
||||
local nMinX = math.floor(VisibleRect:left().x + szBall.width / 2.0)
|
||||
local nMaxX = math.floor(VisibleRect:right().x - szBall.width / 2.0)
|
||||
if ptNext.x < nMinX then
|
||||
ptNext.x = nMinX
|
||||
elseif ptNext.x > nMaxX then
|
||||
ptNext.x = nMaxX
|
||||
end
|
||||
|
||||
local nMinY = math.floor(VisibleRect:bottom().y + szBall.height / 2.0)
|
||||
local nMaxY = math.floor(VisibleRect:top().y - szBall.height / 2.0)
|
||||
if ptNext.y < nMinY then
|
||||
ptNext.y = nMinY
|
||||
elseif ptNext.y > nMaxY then
|
||||
ptNext.y = nMaxY
|
||||
end
|
||||
|
||||
pBall:setPosition(cc.p(ptNext.x,ptNext.y))
|
||||
local minX = math.floor(VisibleRect:left().x + szBall.width / 2.0)
|
||||
local maxX = math.floor(VisibleRect:right().x - szBall.width / 2.0)
|
||||
if ptNowX < minX then
|
||||
ptNowX = minX
|
||||
elseif ptNowX > maxX then
|
||||
ptNowX = maxX
|
||||
end
|
||||
|
||||
local minY = math.floor(VisibleRect:bottom().y + szBall.height / 2.0)
|
||||
local maxY = math.floor(VisibleRect:top().y - szBall.height / 2.0)
|
||||
if ptNowY < minY then
|
||||
ptNowY = minY
|
||||
elseif ptNowY > maxY then
|
||||
ptNowY = maxY
|
||||
end
|
||||
|
||||
ball:setPosition(cc.p(ptNowX, ptNowY ))
|
||||
end
|
||||
|
||||
pLayer:registerScriptAccelerateHandler(didAccelerate)
|
||||
layer:registerScriptAccelerateHandler(didAccelerate)
|
||||
|
||||
return pLayer
|
||||
return layer
|
||||
end
|
||||
|
||||
|
||||
|
|
|
@ -13,7 +13,7 @@ int main(int argc, char **argv)
|
|||
{
|
||||
// create the application instance
|
||||
AppDelegate app;
|
||||
EGLView* eglView = new EGLView();
|
||||
eglView->init("TestCPP",900,640);
|
||||
EGLView eglView;
|
||||
eglView.init("TestLua",900,640);
|
||||
return Application::getInstance()->run();
|
||||
}
|
||||
|
|
|
@ -30,7 +30,7 @@ USING_NS_CC;
|
|||
int main(int argc, char *argv[])
|
||||
{
|
||||
AppDelegate app;
|
||||
EGLView* eglView = new EGLView();
|
||||
eglView->init("TestCPP",900,640);
|
||||
EGLView eglView;
|
||||
eglView.init("TestLua",900,640);
|
||||
return Application::getInstance()->run();
|
||||
}
|
||||
|
|
|
@ -24,8 +24,8 @@ int APIENTRY _tWinMain(HINSTANCE hInstance,
|
|||
|
||||
// create the application instance
|
||||
AppDelegate app;
|
||||
EGLView* eglView = new EGLView();
|
||||
eglView->init("TestCPP",900,640);
|
||||
EGLView eglView;
|
||||
eglView.init("TestLua",900,640);
|
||||
|
||||
int ret = Application::getInstance()->run();
|
||||
|
||||
|
|
|
@ -117,6 +117,11 @@ function CCMenuDeprecated.createWithItem(self,...)
|
|||
return self:create(...)
|
||||
end
|
||||
rawset(CCMenu,"createWithItem",CCMenuDeprecated.createWithItem)
|
||||
|
||||
function CCMenuDeprecated.setHandlerPriority(self)
|
||||
print("\n********** \n".."setHandlerPriority was deprecated in 3.0. \n**********")
|
||||
end
|
||||
rawset(CCMenu,"setHandlerPriority",CCMenuDeprecated.setHandlerPriority)
|
||||
--functions of CCMenu will be deprecated end
|
||||
|
||||
--functions of CCNode will be deprecated begin
|
||||
|
@ -1015,3 +1020,17 @@ function CCSpriteDeprecated.setFlipY(self,flag)
|
|||
end
|
||||
rawset(cc.Sprite, "setFlipY", CCSpriteDeprecated.setFlipY)
|
||||
--functions of Sprite will be deprecated end
|
||||
|
||||
|
||||
--functions of Layer will be deprecated begin
|
||||
local CCLayerDeprecated = {}
|
||||
function CCLayerDeprecated.setKeypadEnabled( self, enabled)
|
||||
return self:setKeyboardEnabled(enabled)
|
||||
end
|
||||
rawset(cc.Layer, "setKeypadEnabled", CCLayerDeprecated.setKeypadEnabled )
|
||||
|
||||
function CCLayerDeprecated.isKeypadEnabled(self)
|
||||
return self:isKeyboardEnabled()
|
||||
end
|
||||
rawset(cc.Layer, "isKeypadEnabled", CCLayerDeprecated.isKeypadEnabled )
|
||||
--functions of Layer will be deprecated end
|
||||
|
|
|
@ -13,7 +13,7 @@ int main(int argc, char **argv)
|
|||
{
|
||||
// create the application instance
|
||||
AppDelegate app;
|
||||
EGLView* eglView = new EGLView();
|
||||
eglView->init("TestCPP",900,640);
|
||||
EGLView eglView;
|
||||
eglView.init("TestCPP",900,640);
|
||||
return Application::getInstance()->run();
|
||||
}
|
||||
|
|
|
@ -30,7 +30,7 @@ USING_NS_CC;
|
|||
int main(int argc, char *argv[])
|
||||
{
|
||||
AppDelegate app;
|
||||
EGLView* eglView = new EGLView();
|
||||
eglView->init("TestCPP",900,640);
|
||||
EGLView eglView;
|
||||
eglView.init("TestCPP",900,640);
|
||||
return Application::getInstance()->run();
|
||||
}
|
||||
|
|
|
@ -14,7 +14,7 @@ int APIENTRY _tWinMain(HINSTANCE hInstance,
|
|||
|
||||
// create the application instance
|
||||
AppDelegate app;
|
||||
EGLView* eglView = new EGLView();
|
||||
eglView->init("TestCPP",900,640);
|
||||
EGLView eglView;
|
||||
eglView.init("TestCPP",900,640);
|
||||
return Application::getInstance()->run();
|
||||
}
|
||||
|
|
|
@ -24,8 +24,8 @@ int APIENTRY _tWinMain(HINSTANCE hInstance,
|
|||
|
||||
// create the application instance
|
||||
AppDelegate app;
|
||||
EGLView* eglView = new EGLView();
|
||||
eglView->init("TestCPP",900,640);
|
||||
EGLView eglView;
|
||||
eglView.init("TestCPP",900,640);
|
||||
|
||||
int ret = Application::getInstance()->run();
|
||||
|
||||
|
|
|
@ -13,7 +13,7 @@ int main(int argc, char **argv)
|
|||
{
|
||||
// create the application instance
|
||||
AppDelegate app;
|
||||
EGLView* eglView = new EGLView();
|
||||
eglView->init("TestCPP",900,640);
|
||||
EGLView eglView;
|
||||
eglView.init("TestCPP",900,640);
|
||||
return Application::getInstance()->run();
|
||||
}
|
||||
|
|
|
@ -30,7 +30,7 @@ USING_NS_CC;
|
|||
int main(int argc, char *argv[])
|
||||
{
|
||||
AppDelegate app;
|
||||
EGLView* eglView = new EGLView();
|
||||
eglView->init("TestCPP",900,640);
|
||||
EGLView eglView;
|
||||
eglView.init("TestCPP",900,640);
|
||||
return Application::getInstance()->run();
|
||||
}
|
|
@ -24,8 +24,8 @@ int APIENTRY _tWinMain(HINSTANCE hInstance,
|
|||
|
||||
// create the application instance
|
||||
AppDelegate app;
|
||||
EGLView* eglView = new EGLView();
|
||||
eglView->init("TestCPP",900,640);
|
||||
EGLView eglView;
|
||||
eglView.init("TestCPP",900,640);
|
||||
|
||||
int ret = Application::getInstance()->run();
|
||||
|
||||
|
|
Loading…
Reference in New Issue