Cocos2d-x study notes (15.3) EventDispatcher DirtyFlag dirty flag

1. Definitions

Enumeration defines four types of dirty flag.

    enum class DirtyFlag
    {
        NONE = 0,
        FIXED_PRIORITY = 1 << 0,
        SCENE_GRAPH_PRIORITY = 1 << 1,
        ALL = FIXED_PRIORITY | SCENE_GRAPH_PRIORITY
    };

Here represented by four types of two binary values:

  • 00 two containers are not dirty,
  • 01 Only custom priority listener container is dirty,
  • Scene 10 only dirty FIG listener container,
  • 11 are dirty.

2. setDirty

Listener parameter ID, is set to the value of the dirty.

_PriorityDirtyFlagMap storage container using the dirty flag information, key: Listener ID, value: dirty value.

When the value changes to the dirty, or bitwise operations.

 

3. setDirtyForNode

This parameter is the node and all of its child nodes is added _dirtyNodes vessel. When the event began distribution method, it would be associated with all of these node ID set of listeners dirty mark SCENE_GRAPH_PRIORITY.

4. usefulness

4.1 event

When you add a listener to listener container, according to whether the listener is a listener scene graph, opposed to different listenerID affected vessel dirty flag.

    if (listener->getFixedPriority() == 0)
    {
        setDirty(listenerID, DirtyFlag::SCENE_GRAPH_PRIORITY);
        //...
    }
    else
    {
        setDirty(listenerID, DirtyFlag::FIXED_PRIORITY);
    }    

Remove from the container when the listener, different markers affected dirty container ID is set.

     removeListenerInVector(sceneGraphPriorityListeners);
        if (isFound)
        {
            setDirty(listener->getListenerID(), DirtyFlag::SCENE_GRAPH_PRIORITY);
        }
        else
        {
            removeListenerInVector(fixedPriorityListeners);
            if (isFound)
            {
                setDirty(listener->getListenerID(), DirtyFlag::FIXED_PRIORITY);
            }
        }

When setPriority designated listener to set the specified priority, the need to set the dirty flag FIXED_PRIORITY the container ID.

                if (listener->getFixedPriority() != fixedPriority)
                {
                    listener->setFixedPriority(fixedPriority);
                    setDirty(listener->getListenerID(), DirtyFlag::FIXED_PRIORITY);
                }

 When using the specified ID sortEventListeners sort the container, if the dirty tag ID to NONE, no sort is required.

    DirtyFlag dirtyFlag = DirtyFlag::NONE;
    
    auto dirtyIter = _priorityDirtyFlagMap.find(listenerID);
    if (dirtyIter != _priorityDirtyFlagMap.end())
    {
        dirtyFlag = dirtyIter->second;
    }
    
    if (dirtyFlag != DirtyFlag::NONE)
    {
        //排序操作
    }

Distribution function at the start of the event, calling updateDirtyFlagForSceneGraph (), all listeners to the associated node _dirtyNodes ID set in the dirty flag SCENE_GRAPH_PRIORITY.

Only use resumeEventListenersForTarget method, restore all listeners of the specified node, the node will join _dirtyNodes.

4.2 Node in

sortAllChildren setGlobalZOrder setLocalZOrder three methods will perform setDirtyForNode parameters node, node because these priorities may change, need to re-order them before distributing events to reorder the listener container.

5. Summary

Dirty flag is to determine whether it is necessary to sort the container provided listener. In the event distribution, and when there is added a delete operation, the listener needs to be sorted before the listener container vessel, the sequence of events distributed to container sorted in the listener, event processing by the listener.

After the node listener is suspended resume, pause over these listeners containers also need to reorder.

When the node priority may change, call setDirtyForNode.

Guess you like

Origin www.cnblogs.com/deepcho/p/cocos2dx-eventdispatcher-dirtyflag.html