diff --git a/frameworks/js-bindings/bindings/Android.mk b/frameworks/js-bindings/bindings/Android.mk index c0819bbb7e..28270ee293 100644 --- a/frameworks/js-bindings/bindings/Android.mk +++ b/frameworks/js-bindings/bindings/Android.mk @@ -16,6 +16,7 @@ LOCAL_SRC_FILES := auto/jsb_cocos2dx_auto.cpp \ auto/jsb_cocos2dx_ui_auto.cpp \ auto/jsb_cocos2dx_3d_auto.cpp \ auto/jsb_cocos2dx_3d_extension_auto.cpp \ + auto/jsb_cocos2dx_experimental_webView_auto.cpp \ manual/ScriptingCore.cpp \ manual/cocos2d_specifics.cpp \ manual/js_manual_conversions.cpp \ @@ -41,7 +42,8 @@ LOCAL_SRC_FILES := auto/jsb_cocos2dx_auto.cpp \ manual/network/XMLHTTPRequest.cpp \ manual/spine/jsb_cocos2dx_spine_manual.cpp \ manual/ui/jsb_cocos2dx_ui_manual.cpp \ - manual/3d/jsb_cocos2dx_3d_manual.cpp + manual/3d/jsb_cocos2dx_3d_manual.cpp \ + manual/experimental/jsb_cocos2dx_experimental_webView_manual.cpp LOCAL_CFLAGS := -DCOCOS2D_JAVASCRIPT diff --git a/frameworks/js-bindings/bindings/manual/experimental/jsb_cocos2dx_experimental_webView_manual.cpp b/frameworks/js-bindings/bindings/manual/experimental/jsb_cocos2dx_experimental_webView_manual.cpp new file mode 100644 index 0000000000..9e57d3f3c0 --- /dev/null +++ b/frameworks/js-bindings/bindings/manual/experimental/jsb_cocos2dx_experimental_webView_manual.cpp @@ -0,0 +1,138 @@ +#include "jsb_cocos2dx_experimental_webView_manual.h" + +#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS) + +#include "UIWebView.h" +#include "ScriptingCore.h" +#include "cocos2d_specifics.hpp" + +using namespace cocos2d; + + +static bool jsb_cocos2dx_experimental_webView_setOnShouldStartLoading(JSContext *cx, uint32_t argc, jsval *vp) +{ + JSObject *obj = JS_THIS_OBJECT(cx, vp); + js_proxy_t *proxy = jsb_get_js_proxy(obj); + experimental::ui::WebView* cobj = (experimental::ui::WebView *)(proxy ? proxy->ptr : NULL); + JSB_PRECONDITION2( cobj, cx, false, "Invalid Native Object"); + + if(argc == 1){ + JS::CallArgs args = JS::CallArgsFromVp(argc, vp); + std::shared_ptr func(new JSFunctionWrapper(cx, obj, args.get(0))); + cobj->setOnShouldStartLoading([=](experimental::ui::WebView *sender, const std::string &url)->bool{ + jsval arg[2]; + js_proxy_t *proxy = js_get_or_create_proxy(cx, sender); + if(proxy) + arg[0] = OBJECT_TO_JSVAL(proxy->obj); + else + arg[0] = JSVAL_NULL; + arg[1] = std_string_to_jsval(cx, url); + JS::RootedValue rval(cx); + + bool ok = func->invoke(2, arg, &rval); + if (!ok && JS_IsExceptionPending(cx)) { + JS_ReportPendingException(cx); + } + return true; + }); + return true; + } +} + +static bool jsb_cocos2dx_experimental_webView_setOnDidFinishLoading(JSContext *cx, uint32_t argc, jsval *vp) +{ + JSObject *obj = JS_THIS_OBJECT(cx, vp); + js_proxy_t *proxy = jsb_get_js_proxy(obj); + experimental::ui::WebView* cobj = (experimental::ui::WebView *)(proxy ? proxy->ptr : NULL); + JSB_PRECONDITION2( cobj, cx, false, "Invalid Native Object"); + + if(argc == 1){ + JS::CallArgs args = JS::CallArgsFromVp(argc, vp); + std::shared_ptr func(new JSFunctionWrapper(cx, obj, args.get(0))); + cobj->setOnDidFinishLoading([=](experimental::ui::WebView *sender, const std::string &url)->void{ + jsval arg[2]; + js_proxy_t *proxy = js_get_or_create_proxy(cx, sender); + if(proxy) + arg[0] = OBJECT_TO_JSVAL(proxy->obj); + else + arg[0] = JSVAL_NULL; + arg[1] = std_string_to_jsval(cx, url); + JS::RootedValue rval(cx); + + bool ok = func->invoke(2, arg, &rval); + if (!ok && JS_IsExceptionPending(cx)) { + JS_ReportPendingException(cx); + } + }); + return true; + } +} + +static bool jsb_cocos2dx_experimental_webView_setOnDidFailLoading(JSContext *cx, uint32_t argc, jsval *vp) +{ + JSObject *obj = JS_THIS_OBJECT(cx, vp); + js_proxy_t *proxy = jsb_get_js_proxy(obj); + experimental::ui::WebView* cobj = (experimental::ui::WebView *)(proxy ? proxy->ptr : NULL); + JSB_PRECONDITION2( cobj, cx, false, "Invalid Native Object"); + + if(argc == 1){ + JS::CallArgs args = JS::CallArgsFromVp(argc, vp); + std::shared_ptr func(new JSFunctionWrapper(cx, obj, args.get(0))); + cobj->setOnDidFailLoading([=](experimental::ui::WebView *sender, const std::string &url)->void{ + jsval arg[2]; + js_proxy_t *proxy = js_get_or_create_proxy(cx, sender); + if(proxy) + arg[0] = OBJECT_TO_JSVAL(proxy->obj); + else + arg[0] = JSVAL_NULL; + arg[1] = std_string_to_jsval(cx, url); + JS::RootedValue rval(cx); + + bool ok = func->invoke(2, arg, &rval); + if (!ok && JS_IsExceptionPending(cx)) { + JS_ReportPendingException(cx); + } + }); + return true; + } +} + +static bool jsb_cocos2dx_experimental_webView_setOnJSCallback(JSContext *cx, uint32_t argc, jsval *vp) +{ + JSObject *obj = JS_THIS_OBJECT(cx, vp); + js_proxy_t *proxy = jsb_get_js_proxy(obj); + experimental::ui::WebView* cobj = (experimental::ui::WebView *)(proxy ? proxy->ptr : NULL); + JSB_PRECONDITION2( cobj, cx, false, "Invalid Native Object"); + + if(argc == 1){ + JS::CallArgs args = JS::CallArgsFromVp(argc, vp); + std::shared_ptr func(new JSFunctionWrapper(cx, obj, args.get(0))); + cobj->setOnJSCallback([=](experimental::ui::WebView *sender, const std::string &url)->void{ + jsval arg[2]; + js_proxy_t *proxy = js_get_or_create_proxy(cx, sender); + if(proxy) + arg[0] = OBJECT_TO_JSVAL(proxy->obj); + else + arg[0] = JSVAL_NULL; + arg[1] = std_string_to_jsval(cx, url); + JS::RootedValue rval(cx); + + bool ok = func->invoke(2, arg, &rval); + if (!ok && JS_IsExceptionPending(cx)) { + JS_ReportPendingException(cx); + } + }); + return true; + } +} +extern JSObject* jsb_cocos2d_experimental_ui_WebView_prototype; + +void register_all_cocos2dx_experimental_webView_manual(JSContext* cx, JS::HandleObject global) +{ + JS_DefineFunction(cx, JS::RootedObject(cx, jsb_cocos2d_experimental_ui_WebView_prototype), "setOnShouldStartLoading", jsb_cocos2dx_experimental_webView_setOnShouldStartLoading, 1, JSPROP_ENUMERATE | JSPROP_PERMANENT); + JS_DefineFunction(cx, JS::RootedObject(cx, jsb_cocos2d_experimental_ui_WebView_prototype), "setOnDidFinishLoading", jsb_cocos2dx_experimental_webView_setOnDidFinishLoading, 1, JSPROP_ENUMERATE | JSPROP_PERMANENT); + JS_DefineFunction(cx, JS::RootedObject(cx, jsb_cocos2d_experimental_ui_WebView_prototype), "setOnDidFailLoading", jsb_cocos2dx_experimental_webView_setOnDidFailLoading, 1, JSPROP_ENUMERATE | JSPROP_PERMANENT); + JS_DefineFunction(cx, JS::RootedObject(cx, jsb_cocos2d_experimental_ui_WebView_prototype), "setOnJSCallback", jsb_cocos2dx_experimental_webView_setOnJSCallback, 1, JSPROP_ENUMERATE | JSPROP_PERMANENT); +} + +#endif diff --git a/frameworks/js-bindings/bindings/manual/experimental/jsb_cocos2dx_experimental_webView_manual.h b/frameworks/js-bindings/bindings/manual/experimental/jsb_cocos2dx_experimental_webView_manual.h new file mode 100644 index 0000000000..f9d536f634 --- /dev/null +++ b/frameworks/js-bindings/bindings/manual/experimental/jsb_cocos2dx_experimental_webView_manual.h @@ -0,0 +1,9 @@ +#ifndef __jsb_cocos2dx_experimental_webView_manual__ +#define __jsb_cocos2dx_experimental_webView_manual__ + +#include "jsapi.h" +#include "jsfriendapi.h" + +void register_all_cocos2dx_experimental_webView_manual(JSContext* cx, JS::HandleObject global); + +#endif /* defined(__jsb_cocos2dx_experimental_webView_manual__) */ diff --git a/frameworks/js-bindings/bindings/proj.ios_mac/cocos2d_js_bindings.xcodeproj/project.pbxproj b/frameworks/js-bindings/bindings/proj.ios_mac/cocos2d_js_bindings.xcodeproj/project.pbxproj index 20f2298872..13ef9b49ac 100644 --- a/frameworks/js-bindings/bindings/proj.ios_mac/cocos2d_js_bindings.xcodeproj/project.pbxproj +++ b/frameworks/js-bindings/bindings/proj.ios_mac/cocos2d_js_bindings.xcodeproj/project.pbxproj @@ -146,6 +146,12 @@ 420BBCF81AA48EE900493976 /* jsb_cocos2dx_3d_manual.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 420BBCF51AA48EE900493976 /* jsb_cocos2dx_3d_manual.cpp */; }; 420BBCF91AA48EE900493976 /* jsb_cocos2dx_3d_manual.h in Headers */ = {isa = PBXBuildFile; fileRef = 420BBCF61AA48EE900493976 /* jsb_cocos2dx_3d_manual.h */; }; 420BBCFA1AA48EE900493976 /* jsb_cocos2dx_3d_manual.h in Headers */ = {isa = PBXBuildFile; fileRef = 420BBCF61AA48EE900493976 /* jsb_cocos2dx_3d_manual.h */; }; + 4BE089E41ADF965E00D65D4B /* jsb_cocos2dx_experimental_webView_auto.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4BE089E01ADF965E00D65D4B /* jsb_cocos2dx_experimental_webView_auto.cpp */; }; + 4BE089E51ADF965E00D65D4B /* jsb_cocos2dx_experimental_webView_auto.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 4BE089E11ADF965E00D65D4B /* jsb_cocos2dx_experimental_webView_auto.hpp */; }; + 4BE089E61ADF965E00D65D4B /* jsb_cocos2dx_experimental_video_auto.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4BE089E21ADF965E00D65D4B /* jsb_cocos2dx_experimental_video_auto.cpp */; }; + 4BE089E71ADF965E00D65D4B /* jsb_cocos2dx_experimental_video_auto.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 4BE089E31ADF965E00D65D4B /* jsb_cocos2dx_experimental_video_auto.hpp */; }; + 4BE089EB1ADF967400D65D4B /* jsb_cocos2dx_experimental_webView_manual.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4BE089E91ADF967400D65D4B /* jsb_cocos2dx_experimental_webView_manual.cpp */; }; + 4BE089EC1ADF967400D65D4B /* jsb_cocos2dx_experimental_webView_manual.h in Headers */ = {isa = PBXBuildFile; fileRef = 4BE089EA1ADF967400D65D4B /* jsb_cocos2dx_experimental_webView_manual.h */; }; 83A5661918DA878400FC31A0 /* jsb_socketio.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 83A5661718DA878400FC31A0 /* jsb_socketio.cpp */; }; 83A5661A18DA878400FC31A0 /* jsb_socketio.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 83A5661718DA878400FC31A0 /* jsb_socketio.cpp */; }; 83A5661B18DA878400FC31A0 /* jsb_socketio.h in Headers */ = {isa = PBXBuildFile; fileRef = 83A5661818DA878400FC31A0 /* jsb_socketio.h */; }; @@ -256,6 +262,12 @@ 420BBCEF1AA48EDE00493976 /* jsb_cocos2dx_3d_auto.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = jsb_cocos2dx_3d_auto.hpp; sourceTree = ""; }; 420BBCF51AA48EE900493976 /* jsb_cocos2dx_3d_manual.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = jsb_cocos2dx_3d_manual.cpp; sourceTree = ""; }; 420BBCF61AA48EE900493976 /* jsb_cocos2dx_3d_manual.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = jsb_cocos2dx_3d_manual.h; sourceTree = ""; }; + 4BE089E01ADF965E00D65D4B /* jsb_cocos2dx_experimental_webView_auto.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = jsb_cocos2dx_experimental_webView_auto.cpp; sourceTree = ""; }; + 4BE089E11ADF965E00D65D4B /* jsb_cocos2dx_experimental_webView_auto.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = jsb_cocos2dx_experimental_webView_auto.hpp; sourceTree = ""; }; + 4BE089E21ADF965E00D65D4B /* jsb_cocos2dx_experimental_video_auto.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = jsb_cocos2dx_experimental_video_auto.cpp; sourceTree = ""; }; + 4BE089E31ADF965E00D65D4B /* jsb_cocos2dx_experimental_video_auto.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = jsb_cocos2dx_experimental_video_auto.hpp; sourceTree = ""; }; + 4BE089E91ADF967400D65D4B /* jsb_cocos2dx_experimental_webView_manual.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = jsb_cocos2dx_experimental_webView_manual.cpp; sourceTree = ""; }; + 4BE089EA1ADF967400D65D4B /* jsb_cocos2dx_experimental_webView_manual.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = jsb_cocos2dx_experimental_webView_manual.h; sourceTree = ""; }; 83A5661718DA878400FC31A0 /* jsb_socketio.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = jsb_socketio.cpp; sourceTree = ""; }; 83A5661818DA878400FC31A0 /* jsb_socketio.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = jsb_socketio.h; sourceTree = ""; }; BA4095C01A6F730A005E53F6 /* jsb_cocos2dx_studio_conversions.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = jsb_cocos2dx_studio_conversions.cpp; sourceTree = ""; }; @@ -323,6 +335,10 @@ 1A119E2E18BDF19200352BAA /* auto */ = { isa = PBXGroup; children = ( + 4BE089E01ADF965E00D65D4B /* jsb_cocos2dx_experimental_webView_auto.cpp */, + 4BE089E11ADF965E00D65D4B /* jsb_cocos2dx_experimental_webView_auto.hpp */, + 4BE089E21ADF965E00D65D4B /* jsb_cocos2dx_experimental_video_auto.cpp */, + 4BE089E31ADF965E00D65D4B /* jsb_cocos2dx_experimental_video_auto.hpp */, 420BBCEE1AA48EDE00493976 /* jsb_cocos2dx_3d_auto.cpp */, 420BBCEF1AA48EDE00493976 /* jsb_cocos2dx_3d_auto.hpp */, BAEE4D6F1AC3FFAD003BEB0F /* jsb_cocos2dx_3d_extension_auto.cpp */, @@ -349,6 +365,7 @@ 1A119E4118BDF19200352BAA /* manual */ = { isa = PBXGroup; children = ( + 4BE089E81ADF967400D65D4B /* experimental */, 420BBCF41AA48EE900493976 /* 3d */, 0541A74C1973876100E45470 /* ios */, 1A119E4218BDF19200352BAA /* chipmunk */, @@ -525,6 +542,15 @@ path = 3d; sourceTree = ""; }; + 4BE089E81ADF967400D65D4B /* experimental */ = { + isa = PBXGroup; + children = ( + 4BE089E91ADF967400D65D4B /* jsb_cocos2dx_experimental_webView_manual.cpp */, + 4BE089EA1ADF967400D65D4B /* jsb_cocos2dx_experimental_webView_manual.h */, + ); + path = experimental; + sourceTree = ""; + }; BA623DFB191A192700761F37 /* pluginx */ = { isa = PBXGroup; children = ( @@ -604,6 +630,7 @@ 1A119F0218BDF19200352BAA /* ScriptingCore.h in Headers */, 1A119EBE18BDF19200352BAA /* cocos2d_specifics.hpp in Headers */, 83A5661C18DA878400FC31A0 /* jsb_socketio.h in Headers */, + 4BE089E71ADF965E00D65D4B /* jsb_cocos2dx_experimental_video_auto.hpp in Headers */, 1A1D3B7B18C44FD000922D3C /* jsb_event_dispatcher_manual.h in Headers */, 1A119EF618BDF19200352BAA /* js_bindings_system_registration.h in Headers */, BA4095C51A6F730A005E53F6 /* jsb_cocos2dx_studio_conversions.h in Headers */, @@ -614,6 +641,7 @@ 1A119EFA18BDF19200352BAA /* jsb_websocket.h in Headers */, 1A119E9218BDF19200352BAA /* jsb_cocos2dx_extension_auto.hpp in Headers */, 1A119E8C18BDF19200352BAA /* jsb_cocos2dx_builder_auto.hpp in Headers */, + 4BE089E51ADF965E00D65D4B /* jsb_cocos2dx_experimental_webView_auto.hpp in Headers */, BA623E19191A196F00761F37 /* jsb_cocos2dx_pluginx_auto.hpp in Headers */, 1A119EAA18BDF19200352BAA /* js_bindings_chipmunk_auto_classes.h in Headers */, 1A119E9E18BDF19200352BAA /* jsb_cocos2dx_spine_auto.hpp in Headers */, @@ -640,6 +668,7 @@ 420BBCF31AA48EDE00493976 /* jsb_cocos2dx_3d_auto.hpp in Headers */, 1A119EBA18BDF19200352BAA /* js_bindings_chipmunk_registration.h in Headers */, 1A119EB018BDF19200352BAA /* js_bindings_chipmunk_functions.h in Headers */, + 4BE089EC1ADF967400D65D4B /* jsb_cocos2dx_experimental_webView_manual.h in Headers */, BA623E0E191A195F00761F37 /* jsb_pluginx_manual_callback.h in Headers */, 1A119EB218BDF19200352BAA /* js_bindings_chipmunk_functions_registration.h in Headers */, 1A119EF218BDF19200352BAA /* js_bindings_system_functions_registration.h in Headers */, @@ -768,8 +797,10 @@ 1A119EEE18BDF19200352BAA /* js_bindings_system_functions.cpp in Sources */, 1A119EB418BDF19200352BAA /* js_bindings_chipmunk_manual.cpp in Sources */, 1A119EEA18BDF19200352BAA /* jsb_opengl_registration.cpp in Sources */, + 4BE089E61ADF965E00D65D4B /* jsb_cocos2dx_experimental_video_auto.cpp in Sources */, 1A119EF818BDF19200352BAA /* jsb_websocket.cpp in Sources */, 1A119ED418BDF19200352BAA /* js_bindings_core.cpp in Sources */, + 4BE089EB1ADF967400D65D4B /* jsb_cocos2dx_experimental_webView_manual.cpp in Sources */, 420BBCF11AA48EDE00493976 /* jsb_cocos2dx_3d_auto.cpp in Sources */, 1A119EC218BDF19200352BAA /* js_bindings_ccbreader.cpp in Sources */, 1A119EFC18BDF19200352BAA /* XMLHTTPRequest.cpp in Sources */, @@ -778,6 +809,7 @@ 1A119ED818BDF19200352BAA /* js_bindings_opengl.cpp in Sources */, 1AB5E62C18D05BC80088DAA4 /* jsb_cocos2dx_ui_auto.cpp in Sources */, 1A119E9C18BDF19200352BAA /* jsb_cocos2dx_spine_auto.cpp in Sources */, + 4BE089E41ADF965E00D65D4B /* jsb_cocos2dx_experimental_webView_auto.cpp in Sources */, 1A119EF418BDF19200352BAA /* js_bindings_system_registration.cpp in Sources */, 1A119EBC18BDF19200352BAA /* cocos2d_specifics.cpp in Sources */, 1A119EE618BDF19200352BAA /* jsb_opengl_manual.cpp in Sources */, diff --git a/samples/js-tests/project.json b/samples/js-tests/project.json index df4c38b167..f272d489e0 100644 --- a/samples/js-tests/project.json +++ b/samples/js-tests/project.json @@ -168,6 +168,7 @@ "src/GUITest/UITextFieldTest/UITextFieldTest.js", "src/GUITest/UIRichTextTest/UIRichTextTest.js", "src/GUITest/UITextTest/UITextTest.js", + "src/GUITest/UIWebViewTest/UIWebViewTest.js", "src/CocoStudioTest/SceneTest/TriggerCode/Acts.js", "src/CocoStudioTest/SceneTest/TriggerCode/Cons.js", diff --git a/samples/js-tests/project/Classes/AppDelegate.cpp b/samples/js-tests/project/Classes/AppDelegate.cpp index 9640e35aa0..0394e3b1ce 100644 --- a/samples/js-tests/project/Classes/AppDelegate.cpp +++ b/samples/js-tests/project/Classes/AppDelegate.cpp @@ -42,6 +42,11 @@ #include "js_Effect3D_bindings.h" #endif +#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) +#include "jsb_cocos2dx_experimental_webView_auto.hpp" +#include "experimental/jsb_cocos2dx_experimental_webView_manual.h" +#endif + USING_NS_CC; USING_NS_CC_EXT; using namespace CocosDenshion; @@ -126,6 +131,11 @@ bool AppDelegate::applicationDidFinishLaunching() sc->addRegisterCallback(register_Effect3D_bindings); #endif +#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) + sc->addRegisterCallback(register_all_cocos2dx_experimental_webView); + sc->addRegisterCallback(register_all_cocos2dx_experimental_webView_manual); +#endif + sc->start(); sc->runScript("script/jsb_boot.js"); #if defined(COCOS2D_DEBUG) && (COCOS2D_DEBUG > 0) diff --git a/samples/js-tests/res/cocosui/Test.html b/samples/js-tests/res/cocosui/Test.html new file mode 100644 index 0000000000..1e90fb526f --- /dev/null +++ b/samples/js-tests/res/cocosui/Test.html @@ -0,0 +1,5 @@ + + + Hello World + + \ No newline at end of file diff --git a/samples/js-tests/src/GUITest/UISceneManager.js b/samples/js-tests/src/GUITest/UISceneManager.js index 61105998dc..40cc9fdec8 100644 --- a/samples/js-tests/src/GUITest/UISceneManager.js +++ b/samples/js-tests/src/GUITest/UISceneManager.js @@ -28,6 +28,14 @@ var currentTestingArray = null; var testingItems = { + "UIWebViewTest": [ + { + title: "UIWebViewTest", + func: function () { + return new UIWebViewTest(); + } + } + ], "UIButton": [ { title: "UIButtonTest", diff --git a/samples/js-tests/src/GUITest/UIWebViewTest/UIWebViewTest.js b/samples/js-tests/src/GUITest/UIWebViewTest/UIWebViewTest.js new file mode 100644 index 0000000000..0e18dae14a --- /dev/null +++ b/samples/js-tests/src/GUITest/UIWebViewTest/UIWebViewTest.js @@ -0,0 +1,155 @@ +/**************************************************************************** + Copyright (c) 2011-2012 cocos2d-x.org + Copyright (c) 2013-2014 Chukong Technologies Inc. + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ + +//2015-01-14 +var UIWebViewTest = UIScene.extend({ + _webView: null, + init: function () { + if (this._super()) { + var self = this; + var winSize = cc.size(480, 320); + + if (!((cc.sys.os == cc.sys.OS_ANDROID || cc.sys.os == cc.sys.OS_IOS || cc.sys.os == cc.sys.OS_WINDOWS) && cc.sys.isNative)) + { + var showErrorLabel = new cc.LabelTTF("Only run in android, ios and windows ", "Arial", 20); + showErrorLabel.setPosition(cc.p(winSize.width / 2 , winSize.height / 2)); + this._mainNode.addChild(showErrorLabel); + return; + } + + this._webView = new ccui.WebView(); + this._webView.setAnchorPoint(cc.p(0.5, 0.5)); + this._webView.setPosition(cc.p(winSize.width / 2, winSize.height / 2)); + this._webView.setContentSize(cc.size(winSize.width * 0.5, winSize.height * 0.5)); + this._webView.loadURL("http://www.baidu.com"); + this._webView.setScalesPageToFit(true); + + this._webView.setOnShouldStartLoading(this.onWebViewShouldStartLoading); + this._webView.setOnDidFinishLoading(this.onWebViewDidFinishLoading); + this._webView.setOnDidFailLoading(this.onWebViewDidFailLoading); + + this._mainNode.addChild(this._webView); + + var urlTextField = new ccui.TextField("Input a URL here", "Arial", 20); + urlTextField.setPlaceHolderColor(cc.color(255, 0, 0)); + urlTextField.setPosition(cc.p(winSize.width / 2 - 80, winSize.height / 2 + + this._webView.getContentSize().height / 2 + urlTextField.getContentSize().height / 2 + 10)); + this._mainNode.addChild(urlTextField); + + var textFieldPos = urlTextField.getPosition(); + + var httpLabel = new cc.LabelTTF("http:// ", "Arial", 20); + httpLabel.setFontFillColor(cc.color(0, 255, 0)); + httpLabel.setAnchorPoint(cc.p(1.0, 0.5)); + httpLabel.setPosition(cc.p(textFieldPos.x - urlTextField.getContentSize().width / 2, textFieldPos.y)); + this._mainNode.addChild(httpLabel); + + + var resetBtn = new ccui.Button("res/cocosui/animationbuttonnormal.png", + "res/cocosui/animationbuttonpressed.png"); + resetBtn.setTitleText("Visit URL"); + resetBtn.setPosition(cc.p(winSize.width / 2 + 50, winSize.height / 2 + this._webView.getContentSize().height / 2 + + resetBtn.getContentSize().height / 2 + 10)); + resetBtn.addClickEventListener(function () { + if (urlTextField.getString().length != 0) { + self._webView.loadURL("http://" + urlTextField.getString()); + } + }); + this._mainNode.addChild(resetBtn); + + var reloadBtn = new ccui.Button("res/cocosui/animationbuttonnormal.png", + "res/cocosui/animationbuttonpressed.png"); + reloadBtn.setTitleText("Reload"); + reloadBtn.setPosition(cc.p(winSize.width / 2 + this._webView.getContentSize().width / 2 + + reloadBtn.getContentSize().width / 2 + 10, winSize.height / 2 + 50)); + reloadBtn.addClickEventListener(function () { + self._webView.reload(); + }); + this._mainNode.addChild(reloadBtn); + + var forwardBtn = new ccui.Button("res/cocosui/animationbuttonnormal.png", + "res/cocosui/animationbuttonpressed.png"); + forwardBtn.setTitleText("Forward"); + forwardBtn.setPosition(cc.p(winSize.width / 2 + this._webView.getContentSize().width / 2 + + forwardBtn.getContentSize().width / 2 + 10, winSize.height / 2)); + forwardBtn.addClickEventListener(function () { + self._webView.goForward(); + }); + this._mainNode.addChild(forwardBtn); + + var backBtn = new ccui.Button("res/cocosui/animationbuttonnormal.png", + "res/cocosui/animationbuttonpressed.png"); + backBtn.setTitleText("Back"); + backBtn.setPosition(cc.p(winSize.width / 2 + this._webView.getContentSize().width / 2 + + backBtn.getContentSize().width / 2 + 10, winSize.height / 2 - 50)); + backBtn.addClickEventListener(function () { + self._webView.goBack(); + }); + this._mainNode.addChild(backBtn); + + + var loadFileBtn = new ccui.Button("res/cocosui/animationbuttonnormal.png", + "res/cocosui/animationbuttonpressed.png"); + loadFileBtn.setTitleText("Load FILE"); + loadFileBtn.setPosition(cc.p(winSize.width / 2 - this._webView.getContentSize().width / 2 - + loadFileBtn.getContentSize().width / 2 - 10, winSize.height / 2 - 50)); + loadFileBtn.addClickEventListener(function () { + self._webView.loadFile("res/cocosui/Test.html"); + }); + this._mainNode.addChild(loadFileBtn); + + var loadHTMLBtn = new ccui.Button("res/cocosui/animationbuttonnormal.png", + "res/cocosui/animationbuttonpressed.png"); + loadHTMLBtn.setTitleText("Load Data"); + loadHTMLBtn.setPosition(cc.p(winSize.width / 2 - this._webView.getContentSize().width / 2 - + loadHTMLBtn.getContentSize().width / 2 - 10, winSize.height / 2)); + loadHTMLBtn.addClickEventListener(function () { + self._webView.loadHTMLString("Hello World ", "Images/"); + }); + this._mainNode.addChild(loadHTMLBtn); + + var evalJsBtn = new ccui.Button("res/cocosui/animationbuttonnormal.png", + "res/cocosui/animationbuttonpressed.png"); + evalJsBtn.setTitleText("Evaluate JS"); + evalJsBtn.setPosition(cc.p(winSize.width / 2 - this._webView.getContentSize().width / 2 - + evalJsBtn.getContentSize().width / 2 - 10, winSize.height / 2 + 50)); + evalJsBtn.addClickEventListener(function () { + self._webView.evaluateJS("alert(\"hello\")"); + }); + this._mainNode.addChild(evalJsBtn); + + return true; + } + }, + onWebViewShouldStartLoading: function (sender, url) { + cc.log("onWebViewShouldStartLoading, url is " + url); + }, + onWebViewDidFinishLoading: function (sender, url) { + cc.log("onWebViewDidFinishLoading, url is " + url); + }, + onWebViewDidFailLoading: function (sender, url) { + cc.log("onWebViewDidFailLoading, url is " + url); + } +}); diff --git a/tools/tojs/cocos2dx_experimental_webView.ini b/tools/tojs/cocos2dx_experimental_webView.ini new file mode 100644 index 0000000000..4bb1ff6d1a --- /dev/null +++ b/tools/tojs/cocos2dx_experimental_webView.ini @@ -0,0 +1,62 @@ +[cocos2dx_experimental_webView] +# the prefix to be added to the generated functions. You might or might not use this in your own +# templates +prefix = cocos2dx_experimental_webView + +# create a target namespace (in javascript, this would create some code like the equiv. to `ns = ns || {}`) +# all classes will be embedded in that namespace +target_namespace = ccui + +macro_judgement = #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS) + +android_headers = -I%(androidndkdir)s/platforms/android-14/arch-arm/usr/include -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.7/libs/armeabi-v7a/include -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.7/include -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.8/libs/armeabi-v7a/include -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.8/include +android_flags = -D_SIZE_T_DEFINED_ + +clang_headers = -I%(clangllvmdir)s/lib/clang/3.3/include +clang_flags = -nostdinc -x c++ -std=c++11 -U __SSE__ + +cocos_headers = -I%(cocosdir)s/cocos -I%(cocosdir)s/cocos/editor-support -I%(cocosdir)s/cocos/platform/android + +cocos_flags = -DANDROID + +cxxgenerator_headers = + +# extra arguments for clang +extra_arguments = %(android_headers)s %(clang_headers)s %(cxxgenerator_headers)s %(cocos_headers)s %(android_flags)s %(clang_flags)s %(cocos_flags)s %(extra_flags)s + +# what headers to parse +headers = %(cocosdir)s/cocos/ui/UIWebView.h + +# what classes to produce code for. You can use regular expressions here. When testing the regular +# expression, it will be enclosed in "^$", like this: "^Menu*$". +classes = WebView + +# what should we skip? in the format ClassName::[function function] +# ClassName is a regular expression, but will be used like this: "^ClassName$" functions are also +# regular expressions, they will not be surrounded by "^$". If you want to skip a whole class, just +# add a single "*" as functions. See bellow for several examples. A special class name is "*", which +# will apply to all class names. This is a convenience wildcard to be able to skip similar named +# functions from all classes. + +skip = WebView::[setOnShouldStartLoading setOnDidFinishLoading setOnDidFailLoading setOnJSCallback loadData] + +rename_functions = + +rename_classes = + +# for all class names, should we remove something when registering in the target VM? +remove_prefix = + +# classes for which there will be no "parent" lookup +classes_have_no_parents = + +# base classes which will be skipped when their sub-classes found them. +base_classes_to_skip = + +# classes that create no constructor +# Set is special and we will use a hand-written constructor +abstract_classes = + +# Determining whether to use script object(js object) to control the lifecycle of native(cpp) object or the other way around. Supported values are 'yes' or 'no'. +script_control_cpp = no + diff --git a/tools/tojs/genbindings.py b/tools/tojs/genbindings.py index 01fc58d944..41b7120cdc 100755 --- a/tools/tojs/genbindings.py +++ b/tools/tojs/genbindings.py @@ -141,7 +141,8 @@ def main(): 'cocos2dx_studio.ini' : ('cocos2dx_studio', 'jsb_cocos2dx_studio_auto'), \ 'cocos2dx_spine.ini' : ('cocos2dx_spine', 'jsb_cocos2dx_spine_auto'), \ 'cocos2dx_3d.ini' : ('cocos2dx_3d', 'jsb_cocos2dx_3d_auto'), \ - 'cocos2dx_3d_ext.ini' : ('cocos2dx_3d_extension', 'jsb_cocos2dx_3d_extension_auto') + 'cocos2dx_3d_ext.ini' : ('cocos2dx_3d_extension', 'jsb_cocos2dx_3d_extension_auto'), \ + 'cocos2dx_experimental_webView.ini' : ('cocos2dx_experimental_webView', 'jsb_cocos2dx_experimental_webView_auto') } target = 'spidermonkey' generator_py = '%s/generator.py' % cxx_generator_root