Skip to content

Add support for necessary events to be sent regardless of event dispatcher enabled state #1940

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions core/base/EventDispatcher.cpp
Original file line number Diff line number Diff line change
@@ -959,9 +959,9 @@ void EventDispatcher::dispatchTouchEventToListeners(EventListenerVector* listene
}
}

void EventDispatcher::dispatchEvent(Event* event)
void EventDispatcher::dispatchEvent(Event* event, bool forced)
{
if (!_isEnabled)
if (!_isEnabled && !forced)
return;

updateDirtyFlagForSceneGraph();
@@ -1000,11 +1000,11 @@ void EventDispatcher::dispatchEvent(Event* event)
updateListeners(event);
}

void EventDispatcher::dispatchCustomEvent(std::string_view eventName, void* optionalUserData)
void EventDispatcher::dispatchCustomEvent(std::string_view eventName, void* optionalUserData, bool forced)
{
EventCustom ev(eventName);
ev.setUserData(optionalUserData);
dispatchEvent(&ev);
dispatchEvent(&ev, forced);
}

bool EventDispatcher::hasEventListener(std::string_view listenerID) const
6 changes: 4 additions & 2 deletions core/base/EventDispatcher.h
Original file line number Diff line number Diff line change
@@ -169,15 +169,17 @@ class AX_DLL EventDispatcher : public Object
* event dispatcher list.
*
* @param event The event needs to be dispatched.
* @param forced If the event should be sent out regardless of enabled state
*/
void dispatchEvent(Event* event);
void dispatchEvent(Event* event, bool forced = false);

/** Dispatches a Custom Event with a event name an optional user data.
*
* @param eventName The name of the event which needs to be dispatched.
* @param optionalUserData The optional user data, it's a void*, the default value is nullptr.
* @param forced If the event should be sent out regardless of enabled state
*/
void dispatchCustomEvent(std::string_view eventName, void* optionalUserData = nullptr);
void dispatchCustomEvent(std::string_view eventName, void* optionalUserData = nullptr, bool forced = false);

/** Query whether the specified event listener id has been added.
*
4 changes: 2 additions & 2 deletions core/platform/android/javaactivity-android.cpp
Original file line number Diff line number Diff line change
@@ -99,7 +99,7 @@ JNIEXPORT void JNICALL Java_org_axmol_lib_AxmolRenderer_nativeInit(JNIEnv*, jcla
backend::DriverBase::getInstance()->resetState();
ax::Director::getInstance()->resetMatrixStack();
ax::EventCustom recreatedEvent(EVENT_RENDERER_RECREATED);
director->getEventDispatcher()->dispatchEvent(&recreatedEvent);
director->getEventDispatcher()->dispatchEvent(&recreatedEvent, true);
director->setGLDefaultValues();
#if AX_ENABLE_CACHE_TEXTURE_DATA
ax::VolatileTextureMgr::reloadAllTextures();
@@ -112,7 +112,7 @@ JNIEXPORT void JNICALL Java_org_axmol_lib_AxmolRenderer_nativeOnContextLost(JNIE
#if AX_ENABLE_RESTART_APPLICATION_ON_CONTEXT_LOST
auto director = ax::Director::getInstance();
ax::EventCustom recreatedEvent(EVENT_APP_RESTARTING);
director->getEventDispatcher()->dispatchEvent(&recreatedEvent);
director->getEventDispatcher()->dispatchEvent(&recreatedEvent, true);

// Pop to root scene, replace with an empty scene, and clear all cached data before restarting
director->popToRootScene();
Original file line number Diff line number Diff line change
@@ -49,7 +49,7 @@ JNIEXPORT void JNICALL Java_org_axmol_lib_AxmolRenderer_nativeOnPause(JNIEnv*, j
{
Application::getInstance()->applicationDidEnterBackground();
ax::EventCustom backgroundEvent(EVENT_COME_TO_BACKGROUND);
ax::Director::getInstance()->getEventDispatcher()->dispatchEvent(&backgroundEvent);
ax::Director::getInstance()->getEventDispatcher()->dispatchEvent(&backgroundEvent, true);
}
}

@@ -64,7 +64,7 @@ JNIEXPORT void JNICALL Java_org_axmol_lib_AxmolRenderer_nativeOnResume(JNIEnv*,
Application::getInstance()->applicationWillEnterForeground();

ax::EventCustom foregroundEvent(EVENT_COME_TO_FOREGROUND);
ax::Director::getInstance()->getEventDispatcher()->dispatchEvent(&foregroundEvent);
ax::Director::getInstance()->getEventDispatcher()->dispatchEvent(&foregroundEvent, true);

firstTime = false;
}
8 changes: 4 additions & 4 deletions core/platform/winrt/xaml/AxmolRenderer.cpp
Original file line number Diff line number Diff line change
@@ -71,7 +71,7 @@ void AxmolRenderer::Resume()
{
Application::getInstance()->applicationWillEnterForeground();
ax::EventCustom foregroundEvent(EVENT_COME_TO_FOREGROUND);
ax::Director::getInstance()->getEventDispatcher()->dispatchEvent(&foregroundEvent);
ax::Director::getInstance()->getEventDispatcher()->dispatchEvent(&foregroundEvent, true);
}
}

@@ -81,7 +81,7 @@ void AxmolRenderer::Pause()
{
Application::getInstance()->applicationDidEnterBackground();
ax::EventCustom backgroundEvent(EVENT_COME_TO_BACKGROUND);
ax::Director::getInstance()->getEventDispatcher()->dispatchEvent(&backgroundEvent);
ax::Director::getInstance()->getEventDispatcher()->dispatchEvent(&backgroundEvent, true);
}
}

@@ -104,12 +104,12 @@ void AxmolRenderer::DeviceLost()
// ax::VolatileTextureMgr::reloadAllTextures();

ax::EventCustom recreatedEvent(EVENT_RENDERER_RECREATED);
director->getEventDispatcher()->dispatchEvent(&recreatedEvent);
director->getEventDispatcher()->dispatchEvent(&recreatedEvent, true);
director->setGLDefaultValues();

Application::getInstance()->applicationWillEnterForeground();
ax::EventCustom foregroundEvent(EVENT_COME_TO_FOREGROUND);
ax::Director::getInstance()->getEventDispatcher()->dispatchEvent(&foregroundEvent);
ax::Director::getInstance()->getEventDispatcher()->dispatchEvent(&foregroundEvent, true);
}
}

Original file line number Diff line number Diff line change
@@ -1447,7 +1447,7 @@ Issue4129::Issue4129() : _bugFixed(false)
this->addChild(menu2);

// Simulate to dispatch 'come to background' event
_eventDispatcher->dispatchCustomEvent(EVENT_COME_TO_BACKGROUND);
_eventDispatcher->dispatchCustomEvent(EVENT_COME_TO_BACKGROUND, nullptr, true);
});

removeAllTouchItem->setFontSizeObj(16);