From 4ca166e10957dea5245321baf3ac2ca7c3e4a112 Mon Sep 17 00:00:00 2001 From: leda Date: Thu, 6 Sep 2018 16:39:45 +0800 Subject: [PATCH] dispatch show/hide event to js, included "cocos simulator" (#1500) * add dispatchEnterBackgroundEvent for simulator * more stable when set canvas size on win32 * add isNan check for float & double * simplify event dispatch on AppDelegate --- .../scripting/js-bindings/event/EventDispatcher.cpp | 13 +++++++++++++ .../js-bindings/manual/jsb_conversions.cpp | 8 +++++--- .../scripting/js-bindings/manual/jsb_global.cpp | 2 +- .../frameworks/runtime-src/Classes/AppDelegate.cpp | 7 ------- .../frameworks/runtime-src/Classes/AppDelegate.cpp | 7 ------- .../frameworks/runtime-src/Classes/AppDelegate.cpp | 9 ++------- 6 files changed, 21 insertions(+), 25 deletions(-) diff --git a/native/cocos/scripting/js-bindings/event/EventDispatcher.cpp b/native/cocos/scripting/js-bindings/event/EventDispatcher.cpp index d6e362712f..d8e9ce791c 100644 --- a/native/cocos/scripting/js-bindings/event/EventDispatcher.cpp +++ b/native/cocos/scripting/js-bindings/event/EventDispatcher.cpp @@ -26,6 +26,7 @@ #include "cocos/scripting/js-bindings/jswrapper/SeApi.h" #include "cocos/scripting/js-bindings/manual/jsb_global.h" +#include "cocos/scripting/js-bindings/event/CustomEventTypes.h" namespace { se::Value _tickVal; @@ -326,11 +327,23 @@ static void dispatchEnterBackgroundOrForegroundEvent(const char* funcName) void EventDispatcher::dispatchEnterBackgroundEvent() { + // dispatch to Native + CustomEvent event; + event.name = EVENT_COME_TO_BACKGROUND; + EventDispatcher::dispatchCustomEvent(event); + + // dispatch to JavaScript dispatchEnterBackgroundOrForegroundEvent("onHide"); } void EventDispatcher::dispatchEnterForegroundEvent() { + // dispatch to Native + CustomEvent event; + event.name = EVENT_COME_TO_FOREGROUND; + EventDispatcher::dispatchCustomEvent(event); + + // dispatch to JavaScript dispatchEnterBackgroundOrForegroundEvent("onShow"); } diff --git a/native/cocos/scripting/js-bindings/manual/jsb_conversions.cpp b/native/cocos/scripting/js-bindings/manual/jsb_conversions.cpp index 1954fb2cbd..e1aa6b7c33 100644 --- a/native/cocos/scripting/js-bindings/manual/jsb_conversions.cpp +++ b/native/cocos/scripting/js-bindings/manual/jsb_conversions.cpp @@ -34,7 +34,7 @@ bool seval_to_int32(const se::Value& v, int32_t* ret) assert(ret != nullptr); if (v.isNumber()) { - *ret = v.toInt32(); //IDEA: need to check isNan? + *ret = v.toInt32(); return true; } else if (v.isBoolean()) @@ -169,7 +169,8 @@ bool seval_to_float(const se::Value& v, float* ret) if (v.isNumber()) { *ret = v.toFloat(); - return true; + if (!std::isnan(*ret)) + return true; } *ret = 0.0f; return false; @@ -180,7 +181,8 @@ bool seval_to_double(const se::Value& v, double* ret) if (v.isNumber()) { *ret = v.toNumber(); - return true; + if (!std::isnan(*ret)) + return true; } *ret = 0.0; return false; diff --git a/native/cocos/scripting/js-bindings/manual/jsb_global.cpp b/native/cocos/scripting/js-bindings/manual/jsb_global.cpp index a980bff5bf..d7754dda35 100644 --- a/native/cocos/scripting/js-bindings/manual/jsb_global.cpp +++ b/native/cocos/scripting/js-bindings/manual/jsb_global.cpp @@ -868,7 +868,7 @@ static bool js_loadImage(se::State& s) } SE_BIND_FUNC(js_loadImage) -//pixels(RGBA), width, height, filePath(*.png) +//pixels(RGBA), width, height, fullFilePath(*.png/*.jpg) static bool js_saveImageData(se::State& s) { const auto& args = s.args(); diff --git a/native/templates/js-template-default/frameworks/runtime-src/Classes/AppDelegate.cpp b/native/templates/js-template-default/frameworks/runtime-src/Classes/AppDelegate.cpp index 29b443ff08..a595186287 100755 --- a/native/templates/js-template-default/frameworks/runtime-src/Classes/AppDelegate.cpp +++ b/native/templates/js-template-default/frameworks/runtime-src/Classes/AppDelegate.cpp @@ -31,7 +31,6 @@ #include "cocos/scripting/js-bindings/manual/jsb_global.h" #include "cocos/scripting/js-bindings/jswrapper/SeApi.h" #include "cocos/scripting/js-bindings/event/EventDispatcher.h" -#include "cocos/scripting/js-bindings/event/CustomEventTypes.h" #include "cocos/scripting/js-bindings/manual/jsb_classtype.hpp" #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS) && PACKAGE_AS @@ -98,17 +97,11 @@ bool AppDelegate::applicationDidFinishLaunching() // This function will be called when the app is inactive. When comes a phone call,it's be invoked too void AppDelegate::applicationDidEnterBackground() { - CustomEvent event; - event.name = EVENT_COME_TO_BACKGROUND; - EventDispatcher::dispatchCustomEvent(event); EventDispatcher::dispatchEnterBackgroundEvent(); } // this function will be called when the app is active again void AppDelegate::applicationWillEnterForeground() { - CustomEvent event; - event.name = EVENT_COME_TO_FOREGROUND; - EventDispatcher::dispatchCustomEvent(event); EventDispatcher::dispatchEnterForegroundEvent(); } diff --git a/native/templates/js-template-link/frameworks/runtime-src/Classes/AppDelegate.cpp b/native/templates/js-template-link/frameworks/runtime-src/Classes/AppDelegate.cpp index 29b443ff08..a595186287 100755 --- a/native/templates/js-template-link/frameworks/runtime-src/Classes/AppDelegate.cpp +++ b/native/templates/js-template-link/frameworks/runtime-src/Classes/AppDelegate.cpp @@ -31,7 +31,6 @@ #include "cocos/scripting/js-bindings/manual/jsb_global.h" #include "cocos/scripting/js-bindings/jswrapper/SeApi.h" #include "cocos/scripting/js-bindings/event/EventDispatcher.h" -#include "cocos/scripting/js-bindings/event/CustomEventTypes.h" #include "cocos/scripting/js-bindings/manual/jsb_classtype.hpp" #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS) && PACKAGE_AS @@ -98,17 +97,11 @@ bool AppDelegate::applicationDidFinishLaunching() // This function will be called when the app is inactive. When comes a phone call,it's be invoked too void AppDelegate::applicationDidEnterBackground() { - CustomEvent event; - event.name = EVENT_COME_TO_BACKGROUND; - EventDispatcher::dispatchCustomEvent(event); EventDispatcher::dispatchEnterBackgroundEvent(); } // this function will be called when the app is active again void AppDelegate::applicationWillEnterForeground() { - CustomEvent event; - event.name = EVENT_COME_TO_FOREGROUND; - EventDispatcher::dispatchCustomEvent(event); EventDispatcher::dispatchEnterForegroundEvent(); } diff --git a/native/tools/simulator/frameworks/runtime-src/Classes/AppDelegate.cpp b/native/tools/simulator/frameworks/runtime-src/Classes/AppDelegate.cpp index 3958b0824b..6e45865c87 100755 --- a/native/tools/simulator/frameworks/runtime-src/Classes/AppDelegate.cpp +++ b/native/tools/simulator/frameworks/runtime-src/Classes/AppDelegate.cpp @@ -27,7 +27,6 @@ #include "cocos2d.h" #include "cocos/scripting/js-bindings/event/EventDispatcher.h" -#include "cocos/scripting/js-bindings/event/CustomEventTypes.h" #include "ide-support/CodeIDESupport.h" #include "runtime/Runtime.h" @@ -64,15 +63,11 @@ bool AppDelegate::applicationDidFinishLaunching() // This function will be called when the app is inactive. When comes a phone call,it's be invoked too void AppDelegate::applicationDidEnterBackground() { - CustomEvent event; - event.name = EVENT_COME_TO_BACKGROUND; - EventDispatcher::dispatchCustomEvent(event); + EventDispatcher::dispatchEnterBackgroundEvent(); } // this function will be called when the app is active again void AppDelegate::applicationWillEnterForeground() { - CustomEvent event; - event.name = EVENT_COME_TO_FOREGROUND; - EventDispatcher::dispatchCustomEvent(event); + EventDispatcher::dispatchEnterForegroundEvent(); }