Disable all user interaction

Hi there!
At this moment, I have been disabling all user interaction by using the event dispatcher with:
Director::getInstance()->getEventDispatcher()->setEnabled(false);

It worked flawlessly in the past but now I have faced a use case where I need the user interaction disabled but the event dispatcher enabled (because it must send some other type of events).

Is there any elegant solution for this? (note: placing a layer on top of the scene to swallow touches is not an option because it requires a lot of extra logic, I’m using the disable feature a lot).

Maybe it would be nice to add a method to the event dispatcher to only disable this kind of events.

Thanks in advance :slight_smile:
fryderyk

1 Like

can you be more specific?

You want to disable all events that a user could respond to, but keep the director processing what ever it is doing (animations, actions, sequences, etc)?

  1. Director::getInstance()->getEventDispatcher()->pauseEventListenersForTarget(zzzz);

  2. remove all events you dont want from the EventDispatcher

  3. Like you suggest a layer that swallows everything. It’s not really any work, less than 30 lines of code, I think.

The thing is that I’m using some code to enable/disable the user interaction:


void NavigationManager::enableUserInteraction()
{
    if(_disabledUserInteractionCounter > 0)
    {
        _disabledUserInteractionCounter--;
        this->changeEventDispatcherState();
    }
}

void NavigationManager::disableUserInteraction()
{
    _disabledUserInteractionCounter++;
    this->changeEventDispatcherState();
}

void NavigationManager::changeEventDispatcherState()
{
    if(_disabledUserInteractionCounter == 0)
        Director::getInstance()->getEventDispatcher()->setEnabled(true);
    else
        Director::getInstance()->getEventDispatcher()->setEnabled(false);
}

I use everywhere the NavigationManager methods enableUserInteraction() and disableUserInteraction() (I made a quick search and I got 32 instances of disableUserInteraction()). Recently I have discovered that I cannot use the EventDispatcher to disable touches because then I disable all other events the EventDispatcher send (for example, Spine uses events to do its own things).

I was searching an easy way to change my NavigationManager::changeEventDispatcherState() and not have to change my code everywhere. Something similar to the old cocos2d versions that had a separated TouchDispatcher. Is it possible to do this?

Thanks!

Well, I have implemented a custom solution that is working as my requirements.

If someone needs this feature, it is very simple. I added a new boolean variable _isTouchEnabled to the EventDispatcher. When it is false, the dispatchEvent method is quick returned if the event type is Event::Type::TOUCH.

Hope it helps anyone! :slight_smile: