Cocos2d-x study notes (19) Control Invocation

1 Introduction

The callback function provides touch control of its subclasses, the subclass triggered when EventType related events, calls the associated callback function.

control object receives event by type Class 9:

    enum class CC_EX_DLL EventType
    {
        TOUCH_DOWN           = 1 << 0,  
        DRAG_INSIDE          = 1 << 1,  
        DRAG_OUTSIDE         = 1 << 2, 
        DRAG_ENTER           = 1 << 3, 
        DRAG_EXIT            = 1 << 4, 
        TOUCH_UP_INSIDE      = 1 << 5, 
        TOUCH_UP_OUTSIDE     = 1 << 6,   
        TOUCH_CANCEL         = 1 << 7,    
        VALUE_CHANGED        = 1 << 8 
    };

It represents a single event type, description of these types can occur simultaneously with different bit binary. We splicing events with multiple or single operation to form a comprehensive event. When determining the type of event, and the binary bit computing, educe a comprehensive event which included a single event.

In which the control state divided into four kinds:

    enum class State
    {
        NORMAL         = 1 << 0, 
        HIGH_LIGHTED   = 1 << 1, 
        DISABLED       = 1 << 2, 
        SELECTED       = 1 << 3 
    };

std::unordered_map<int, Vector<Invocation*>*> _dispatchTable

The hash table key is binary EventType int, value of container Vector, which is stored in the EventType Invocation same.

Constructor not create variable modified:

_hasVisibleParents(false)
_isOpacityModifyRGB(false)
_state(State::NORMAL)

2. Invocation

Invocation direct successor Ref, born to control, action control function is stored on the object in some EventType.

create(Ref* target, Control::Handler action, Control::EventType controlEvent)

create its constructor method call, the three parameters assigned Invocation of _target _action _controlEvent.

3. member method

- Control* create()

create method calls the init method of the parent class Layer, setContentSize for the screen size. After calling the init method, _state set to NORMAL, _enabled is true, _selected _highlighted is false, defines a single touch listeners and four callbacks and event listeners added distributor management.

control is enabled (enabled), if control is selected (Selected), if control is highlighted (Highlighted) has set get the corresponding method:

    virtual void setEnabled(bool bEnabled); //_enabled 会同步修改_state为NORMAL或DISABLED
    virtual bool isEnabled() const;

    virtual void setSelected(bool bSelected); //_selected
    virtual bool isSelected() const;

    virtual void setHighlighted(bool bHighlighted); //_highlighted
    virtual bool isHighlighted() const;

Be performed before the end of three methods set needsLayout (), the specific method is implemented by subclasses.

- Vector<Invocation*>& dispatchListforControlEvent(EventType controlEvent)

Briefly, the method is to find all the Invocation with EventType parameter EventType.

The method EventType parameter, i.e. the received touch control type, _dispatchTable acquired according to EventType Vector <Invocation *> *, the Vector return. If there is no corresponding EventType Vector, the new Vector, and new entries in the _dispatchTable.

- void removeTargetWithActionForControlEvent(Ref* target, Handler action, EventType controlEvent)

Briefly, the method deletes meet the requirements of the Invocation parameters, thereby deleting the relationship between the three.

1. The first call dispatchListforControlEvent, get Vector <Invocation *> according to EventType. When the target and action parameters are empty, on the implementation of clear empty Vector.

2. Next, the Vector is traversed to traverse each Invocation target and action parameters by determining whether to meet the requirements, if they meet the requirements, was added to the vessel vector tobeRemovedInvocations.

3. traversing the end, delete tobeRemovedInvocations in each Invocation from Vector.

- void removeTargetWithActionForControlEvents(Ref* target, Handler action, EventType controlEvents)

kControlEventTotalNumber is the number EventType type, Control header file is defined as 9.

1. controlEvents binary value of nine kinds of bitwise binary value detection EventType, stars "Events" which contains "Event" respectively.

2. Each EventType exist as a parameter, called removeTargetWithActionForControlEvent the above method.

- void addTargetWithActionForControlEvent(Ref* target, Handler action, EventType controlEvent)

Briefly, the method is a new Invocation and storage.

The three parameters create a method Invocation, then call dispatchListforControlEvent, EventType parameter acquisition Vector <Invocation *>, then added to the Vector Invocation.

- void addTargetWithActionForControlEvents(Ref* target, Handler action, EventType controlEvents)

EventType detected by bitwise controlEvents contain, for each parameter and the target action EventType call addTargetWithActionForControlEvent new Invocation.

- Vec2 getTouchLocation (Touch * touch) returns control relative coordinates of the touch point.

- bool isTouchInside (Touch * touch) determines whether the Touch control range.

- bool hasVisibleParents()

When all the parent control are visible, it returns true, returns false when there is no visible parent.

- void sendActionsForControlEvents(EventType controlEvents)

EventType get to controlEvents each invocation, calling invocation-> invoke (this).

Guess you like

Origin www.cnblogs.com/deepcho/p/cocos2dx-control-invocation.html