From de429591c3c0c963debd5cd81d570172fe5d4ce8 Mon Sep 17 00:00:00 2001 From: gin0606 Date: Mon, 25 Aug 2014 14:44:38 +0900 Subject: [PATCH 01/14] Add WebView to libui iOS --- build/cocos2d_libs.xcodeproj/project.pbxproj | 28 ++++ cocos/ui/UIWebViewWrapper.h | 47 ++++++ cocos/ui/UIWebViewWrapper.mm | 156 +++++++++++++++++++ cocos/ui/WebView-inl.h | 91 +++++++++++ cocos/ui/WebView.h | 143 +++++++++++++++++ cocos/ui/WebView.mm | 7 + cocos/ui/WebViewImpl_iOS.h | 63 ++++++++ cocos/ui/WebViewImpl_iOS.mm | 134 ++++++++++++++++ 8 files changed, 669 insertions(+) create mode 100644 cocos/ui/UIWebViewWrapper.h create mode 100644 cocos/ui/UIWebViewWrapper.mm create mode 100644 cocos/ui/WebView-inl.h create mode 100644 cocos/ui/WebView.h create mode 100644 cocos/ui/WebView.mm create mode 100644 cocos/ui/WebViewImpl_iOS.h create mode 100644 cocos/ui/WebViewImpl_iOS.mm diff --git a/build/cocos2d_libs.xcodeproj/project.pbxproj b/build/cocos2d_libs.xcodeproj/project.pbxproj index 1bc96d79ab9a..b82b9e196a2e 100644 --- a/build/cocos2d_libs.xcodeproj/project.pbxproj +++ b/build/cocos2d_libs.xcodeproj/project.pbxproj @@ -634,6 +634,13 @@ 1AD71EF5180E27CF00808F54 /* CCPhysicsSprite.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1AD71EEE180E27CF00808F54 /* CCPhysicsSprite.cpp */; }; 1AD71EF6180E27CF00808F54 /* CCPhysicsSprite.h in Headers */ = {isa = PBXBuildFile; fileRef = 1AD71EEF180E27CF00808F54 /* CCPhysicsSprite.h */; }; 1AD71EF7180E27CF00808F54 /* CCPhysicsSprite.h in Headers */ = {isa = PBXBuildFile; fileRef = 1AD71EEF180E27CF00808F54 /* CCPhysicsSprite.h */; }; + 1F36C41832F21B0EB4B6E581 /* WebView.h in Headers */ = {isa = PBXBuildFile; fileRef = 1F36C97CCD87CFB096B921D5 /* WebView.h */; }; + 1F36C5DC6B9AD57C010B5FAA /* UIWebViewWrapper.h in Headers */ = {isa = PBXBuildFile; fileRef = 1F36C89CE6DA969A8AF515AD /* UIWebViewWrapper.h */; }; + 1F36C63198F935DE23451317 /* WebView.mm in Sources */ = {isa = PBXBuildFile; fileRef = 1F36C92BF6113FEFC19554AA /* WebView.mm */; }; + 1F36C6456029931BAEC90A06 /* WebViewImpl_iOS.mm in Sources */ = {isa = PBXBuildFile; fileRef = 1F36CF6F04510B2F6F74EC8A /* WebViewImpl_iOS.mm */; }; + 1F36C7010C074699AF6C9371 /* WebViewImpl_iOS.h in Headers */ = {isa = PBXBuildFile; fileRef = 1F36C64BC5CD5BA16A6AB1E9 /* WebViewImpl_iOS.h */; }; + 1F36C8D2A011ABC37E2A5673 /* UIWebViewWrapper.mm in Sources */ = {isa = PBXBuildFile; fileRef = 1F36CDAC69B487B7531C782F /* UIWebViewWrapper.mm */; }; + 1F36CFD339EDB3FD6F3C6755 /* WebView-inl.h in Headers */ = {isa = PBXBuildFile; fileRef = 1F36C3CA177A0FBE289B6BD5 /* WebView-inl.h */; }; 2958244B19873D8E00F9746D /* UIScale9Sprite.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2958244919873D8E00F9746D /* UIScale9Sprite.cpp */; }; 2958244C19873D8E00F9746D /* UIScale9Sprite.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2958244919873D8E00F9746D /* UIScale9Sprite.cpp */; }; 2958244D19873D8E00F9746D /* UIScale9Sprite.h in Headers */ = {isa = PBXBuildFile; fileRef = 2958244A19873D8E00F9746D /* UIScale9Sprite.h */; }; @@ -2450,6 +2457,13 @@ 1AD71EED180E27CF00808F54 /* CCPhysicsDebugNode.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCPhysicsDebugNode.h; sourceTree = ""; }; 1AD71EEE180E27CF00808F54 /* CCPhysicsSprite.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CCPhysicsSprite.cpp; sourceTree = ""; }; 1AD71EEF180E27CF00808F54 /* CCPhysicsSprite.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCPhysicsSprite.h; sourceTree = ""; }; + 1F36C3CA177A0FBE289B6BD5 /* WebView-inl.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "WebView-inl.h"; sourceTree = ""; }; + 1F36C64BC5CD5BA16A6AB1E9 /* WebViewImpl_iOS.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WebViewImpl_iOS.h; sourceTree = ""; }; + 1F36C89CE6DA969A8AF515AD /* UIWebViewWrapper.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UIWebViewWrapper.h; sourceTree = ""; }; + 1F36C92BF6113FEFC19554AA /* WebView.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = WebView.mm; sourceTree = ""; }; + 1F36C97CCD87CFB096B921D5 /* WebView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WebView.h; sourceTree = ""; }; + 1F36CDAC69B487B7531C782F /* UIWebViewWrapper.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = UIWebViewWrapper.mm; sourceTree = ""; }; + 1F36CF6F04510B2F6F74EC8A /* WebViewImpl_iOS.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = WebViewImpl_iOS.mm; sourceTree = ""; }; 2905F9E918CF08D000240AA3 /* CocosGUI.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CocosGUI.cpp; sourceTree = ""; }; 2905F9EA18CF08D000240AA3 /* CocosGUI.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CocosGUI.h; sourceTree = ""; }; 2905F9EB18CF08D000240AA3 /* GUIDefine.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GUIDefine.h; sourceTree = ""; }; @@ -4457,6 +4471,13 @@ 2905F9F318CF08D000240AA3 /* UICheckBox.h */, 2905F9F618CF08D000240AA3 /* UIImageView.cpp */, 2905F9F718CF08D000240AA3 /* UIImageView.h */, + 1F36C97CCD87CFB096B921D5 /* WebView.h */, + 1F36C3CA177A0FBE289B6BD5 /* WebView-inl.h */, + 1F36C64BC5CD5BA16A6AB1E9 /* WebViewImpl_iOS.h */, + 1F36C92BF6113FEFC19554AA /* WebView.mm */, + 1F36CF6F04510B2F6F74EC8A /* WebViewImpl_iOS.mm */, + 1F36CDAC69B487B7531C782F /* UIWebViewWrapper.mm */, + 1F36C89CE6DA969A8AF515AD /* UIWebViewWrapper.h */, ); name = widgets; sourceTree = ""; @@ -6174,6 +6195,10 @@ B2C59A4A19777ECF00B452DF /* UILayout.h in Headers */, B2C59A4B19777ECF00B452DF /* UILayoutParameter.h in Headers */, B2C59A4C19777ECF00B452DF /* UILayoutManager.h in Headers */, + 1F36C41832F21B0EB4B6E581 /* WebView.h in Headers */, + 1F36CFD339EDB3FD6F3C6755 /* WebView-inl.h in Headers */, + 1F36C7010C074699AF6C9371 /* WebViewImpl_iOS.h in Headers */, + 1F36C5DC6B9AD57C010B5FAA /* UIWebViewWrapper.h in Headers */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -7644,6 +7669,9 @@ B2C59A3219777E8C00B452DF /* UILayoutParameter.cpp in Sources */, B2C59A3319777E8C00B452DF /* UILayoutManager.cpp in Sources */, B2C59A2919777E7A00B452DF /* CocosGUI.cpp in Sources */, + 1F36C63198F935DE23451317 /* WebView.mm in Sources */, + 1F36C6456029931BAEC90A06 /* WebViewImpl_iOS.mm in Sources */, + 1F36C8D2A011ABC37E2A5673 /* UIWebViewWrapper.mm in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/cocos/ui/UIWebViewWrapper.h b/cocos/ui/UIWebViewWrapper.h new file mode 100644 index 000000000000..2311f24d4527 --- /dev/null +++ b/cocos/ui/UIWebViewWrapper.h @@ -0,0 +1,47 @@ +// +// Created by gin0606 on 2014/07/29. +// + + +#include +#include +#include + + +@interface UIWebViewWrapper : NSObject +@property (nonatomic) std::function shouldStartLoading; +@property (nonatomic) std::function didFinishLoading; +@property (nonatomic) std::function didFailLoading; +@property (nonatomic) std::function onJsCallback; + +@property(nonatomic, readonly, getter=canGoBack) BOOL canGoBack; +@property(nonatomic, readonly, getter=canGoForward) BOOL canGoForward; + ++ (instancetype)webViewWrapper; + +- (void)setVisible:(bool)visible; + +- (void)setFrameWithX:(float)x y:(float)y width:(float)width height:(float)height; + +- (void)setJavascriptInterfaceScheme:(const std::string &)scheme; + +- (void)loadData:(const std::string &)data MIMEType:(const std::string &)MIMEType textEncodingName:(const std::string &)encodingName baseURL:(const std::string &)baseURL; + +- (void)loadHTMLString:(const std::string &)string baseURL:(const std::string &)baseURL; + +- (void)loadUrl:(const std::string &)urlString; + +- (void)loadFile:(const std::string &)filePath; + +- (void)stopLoading; + +- (void)reload; + +- (void)evaluateJS:(const std::string &)js; + +- (void)goBack; + +- (void)goForward; + +- (void)setScalesPageToFit:(const bool)scalesPageToFit; +@end diff --git a/cocos/ui/UIWebViewWrapper.mm b/cocos/ui/UIWebViewWrapper.mm new file mode 100644 index 000000000000..600d8290f842 --- /dev/null +++ b/cocos/ui/UIWebViewWrapper.mm @@ -0,0 +1,156 @@ +// +// Created by gin0606 on 2014/07/29. +// + + +#import "UIWebViewWrapper.h" +#import "WebView.h" +#import "CCGLView.h" +#import "CCEAGLView.h" +#import "CCDirector.h" + + +@interface UIWebViewWrapper () +@property(nonatomic, retain) UIWebView *uiWebView; +@property(nonatomic, copy) NSString *jsScheme; +@end + +@implementation UIWebViewWrapper { + +} + ++ (instancetype)webViewWrapper { + return [[[self alloc] init] autorelease]; +} + +- (instancetype)init { + self = [super init]; + if (self) { + self.uiWebView = nil; + self.shouldStartLoading = nullptr; + self.didFinishLoading = nullptr; + self.didFailLoading = nullptr; + } + return self; +} + +- (void)dealloc { + [self.uiWebView removeFromSuperview]; + self.jsScheme = nil; + [super dealloc]; +} + +- (void)setupWebView { + if (!self.uiWebView) { + self.uiWebView = [[[UIWebView alloc] init] autorelease]; + self.uiWebView.delegate = self; + } + if (!self.uiWebView.superview) { + auto view = cocos2d::Director::getInstance()->getOpenGLView(); + auto eaglview = (CCEAGLView *) view->getEAGLView(); + [eaglview addSubview:self.uiWebView]; + } +} + +- (void)setVisible:(bool)visible { + self.uiWebView.hidden = !visible; +} + +- (void)setFrameWithX:(float)x y:(float)y width:(float)width height:(float)height { + if (!self.uiWebView) {[self setupWebView];} + CGRect newFrame = CGRectMake(x, y, width, height); + if (!CGRectEqualToRect(self.uiWebView.frame, newFrame)) { + self.uiWebView.frame = CGRectMake(x, y, width, height); + } +} + +- (void)setJavascriptInterfaceScheme:(const std::string &)scheme { + self.jsScheme = @(scheme.c_str()); +} + +- (void)loadData:(const std::string &)data MIMEType:(const std::string &)MIMEType textEncodingName:(const std::string &)encodingName baseURL:(const std::string &)baseURL { + [self.uiWebView loadData:[NSData dataWithBytes:data.c_str() length:data.length()] + MIMEType:@(MIMEType.c_str()) + textEncodingName:@(encodingName.c_str()) + baseURL:[NSURL URLWithString:@(baseURL.c_str())]]; +} + +- (void)loadHTMLString:(const std::string &)string baseURL:(const std::string &)baseURL { + [self.uiWebView loadHTMLString:@(string.c_str()) baseURL:[NSURL URLWithString:@(baseURL.c_str())]]; +} + +- (void)loadUrl:(const std::string &)urlString { + if (!self.uiWebView) {[self setupWebView];} + NSURL *url = [NSURL URLWithString:@(urlString.c_str())]; + NSURLRequest *request = [NSURLRequest requestWithURL:url]; + [self.uiWebView loadRequest:request]; +} + +- (void)loadFile:(const std::string &)filePath { + if (!self.uiWebView) {[self setupWebView];} + NSURL *url = [NSURL fileURLWithPath:@(filePath.c_str())]; + NSURLRequest *request = [NSURLRequest requestWithURL:url]; + [self.uiWebView loadRequest:request]; +} + +- (void)stopLoading { + [self.uiWebView stopLoading]; +} + +- (void)reload { + [self.uiWebView reload]; +} + +- (BOOL)canGoForward { + return self.uiWebView.canGoForward; +} + +- (BOOL)canGoBack { + return self.uiWebView.canGoBack; +} + +- (void)goBack { + [self.uiWebView goBack]; +} + +- (void)goForward { + [self.uiWebView goForward]; +} + +- (void)evaluateJS:(const std::string &)js { + if (!self.uiWebView) {[self setupWebView];} + [self.uiWebView stringByEvaluatingJavaScriptFromString:@(js.c_str())]; +} + +- (void)setScalesPageToFit:(const bool)scalesPageToFit { + self.uiWebView.scalesPageToFit = scalesPageToFit; +} + +#pragma mark - UIWebViewDelegate +- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { + NSString *url = [[request URL] absoluteString]; + if ([[[request URL] scheme] isEqualToString:self.jsScheme]) { + self.onJsCallback([url UTF8String]); + return NO; + } + if (self.shouldStartLoading) { + return self.shouldStartLoading([url UTF8String]); + } + return YES; +} + +- (void)webViewDidFinishLoad:(UIWebView *)webView { + if (self.didFinishLoading) { + NSString *url = [[webView.request URL] absoluteString]; + self.didFinishLoading([url UTF8String]); + } +} + +- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { + if (self.didFailLoading) { + NSString *url = error.userInfo[NSURLErrorFailingURLStringErrorKey]; + self.didFailLoading([url UTF8String]); + } +} + +@end diff --git a/cocos/ui/WebView-inl.h b/cocos/ui/WebView-inl.h new file mode 100644 index 000000000000..d044cf118953 --- /dev/null +++ b/cocos/ui/WebView-inl.h @@ -0,0 +1,91 @@ +#ifndef __CC_WebView_INL_H_ +#define __CC_WebView_INL_H_ + +#include "WebView.h" +#include "CCGLView.h" +#include "base/CCDirector.h" +#include "platform/CCFileUtils.h" + +namespace cocos2d { +namespace plugin { +WebView::WebView() : _impl(new WebViewImpl(this)) { +} + +WebView::~WebView() { + delete _impl; +} + +WebView *WebView::create() { + auto pRet = new WebView(); + if (pRet->init()) { + pRet->autorelease(); + return pRet; + } + return nullptr; +} + +void WebView::setJavascriptInterfaceScheme(const std::string &scheme) { + _impl->setJavascriptInterfaceScheme(scheme); +} + +void WebView::loadData(const cocos2d::Data &data, const std::string &MIMEType, const std::string &encoding, const std::string &baseURL) { + _impl->loadData(data, MIMEType, encoding, baseURL); +} + +void WebView::loadHTMLString(const std::string &string, const std::string &baseURL) { + _impl->loadHTMLString(string, baseURL); +} + +void WebView::loadUrl(const std::string &url) { + _impl->loadUrl(url); +} + +void WebView::loadFile(const std::string &fileName) { + _impl->loadFile(fileName); +} + +void WebView::stopLoading() { + _impl->stopLoading(); +} + +void WebView::reload() { + _impl->reload(); +} + +bool WebView::canGoBack() { + return _impl->canGoBack(); +} + +bool WebView::canGoForward() { + return _impl->canGoForward(); +} + +void WebView::goBack() { + _impl->goBack(); +} + +void WebView::goForward() { + _impl->goForward(); +} + +void WebView::evaluateJS(const std::string &js) { + _impl->evaluateJS(js); +} + +void WebView::setScalesPageToFit(bool const scalesPageToFit) { + _impl->setScalesPageToFit(scalesPageToFit); +} + +void WebView::draw(cocos2d::Renderer *renderer, cocos2d::Mat4 const &transform, uint32_t flags) { + cocos2d::ui::Widget::draw(renderer, transform, flags); + _impl->draw(renderer, transform, flags); +} + +void WebView::setVisible(bool visible) { + Node::setVisible(visible); + _impl->setVisible(visible); +} +} // namespace cocos2d +} // namespace plugin + +#endif diff --git a/cocos/ui/WebView.h b/cocos/ui/WebView.h new file mode 100644 index 000000000000..72f9053c47c6 --- /dev/null +++ b/cocos/ui/WebView.h @@ -0,0 +1,143 @@ +// +// Created by gin0606 on 2014/07/29. +// + +#ifndef __Cocos2d_Plugin_WebView_H_ +#define __Cocos2d_Plugin_WebView_H_ + +#include "ui/UIWidget.h" +#include "base/CCData.h" + +namespace cocos2d { +namespace plugin { +class WebViewImpl; + +class WebView : public cocos2d::ui::Widget { +public: + /** + * Allocates and initializes a WebView. + */ + static WebView *create(); + + /** + * Default constructor + */ + WebView(); + + /** + * Default destructor + */ + virtual ~WebView(); + + /** + * Call before a web view begins loading. + * @param sender The web view that is about to load new content. + * @param url content URL. + * @return YES if the web view should begin loading content; otherwise, NO . + */ + std::function shouldStartLoading; + /** + * Call after a web view finishes loading. + * @param sender The web view that has finished loading. + * @param url content URL. + */ + std::function didFinishLoading; + /** + * Call if a web view failed to load content. + * @param sender The web view that has failed loading. + * @param url content URL. + */ + std::function didFailLoading; + + /** + * Set javascript interface scheme. + * @see #onJsCallback + */ + void setJavascriptInterfaceScheme(const std::string &scheme); + + /** + * This callback called when load URL that start with javascript interface scheme. + */ + std::function onJsCallback; + + /** + * Sets the main page contents, MIME type, content encoding, and base URL. + * @param data The content for the main page. + * @param MIMEType The MIME type of the data. + * @param encoding the encoding of the data. + * @param baseURL The base URL for the content. + */ + void loadData(const cocos2d::Data &data, const std::string &MIMEType, const std::string &encoding, const std::string &baseURL); + + /** + * Sets the main page content and base URL. + * @param string The content for the main page. + * @param baseURL The base URL for the content. + */ + void loadHTMLString(const std::string &string, const std::string &baseURL); + + /** + * Loads the given URL. + * @param url content URL + */ + void loadUrl(const std::string &url); + + /** + * Loads the given fileName. + * @param fileName content fileName + */ + void loadFile(const std::string &fileName); + + /** + * Stops the current load. + */ + void stopLoading(); + + /** + * Reloads the current URL. + */ + void reload(); + + /** + * Gets whether this WebView has a back history item. + * @return web view has a back history item. + */ + bool canGoBack(); + + /** + * Gets whether this WebView has a forward history item. + * @return web view has a forward history item. + */ + bool canGoForward(); + + /** + * Goes back in the history. + */ + void goBack(); + + /** + * Goes forward in the history. + */ + void goForward(); + + /** + * evaluates JavaScript in the context of the currently displayed page + */ + void evaluateJS(const std::string &js); + + /** + * Set WebView should support zooming. The default value is false. + */ + void setScalesPageToFit(const bool scalesPageToFit); + + virtual void draw(cocos2d::Renderer *renderer, cocos2d::Mat4 const &transform, uint32_t flags) override; + + virtual void setVisible(bool visible) override; + +private: + cocos2d::plugin::WebViewImpl *_impl; +}; +} // namespace cocos2d +} // namespace plugin + +#endif //__Cocos2d_Plugin_WebView_H_ diff --git a/cocos/ui/WebView.mm b/cocos/ui/WebView.mm new file mode 100644 index 000000000000..c0ed30d1e787 --- /dev/null +++ b/cocos/ui/WebView.mm @@ -0,0 +1,7 @@ +// +// Created by gin0606 on 2014/07/29. +// + + +#include "WebViewImpl_iOS.h" +#include "WebView-inl.h" diff --git a/cocos/ui/WebViewImpl_iOS.h b/cocos/ui/WebViewImpl_iOS.h new file mode 100644 index 000000000000..a2f317235e48 --- /dev/null +++ b/cocos/ui/WebViewImpl_iOS.h @@ -0,0 +1,63 @@ +// +// Created by gin0606 on 2014/07/30. +// + +#ifndef __cocos2d_plugin_WebViewImpl_IOS_H_ +#define __cocos2d_plugin_WebViewImpl_IOS_H_ + +#include + +@class UIWebViewWrapper; + +namespace cocos2d { +class Data; +class Renderer; +class Mat4; +namespace plugin { +class WebView; + +class WebViewImpl { +public: + WebViewImpl(cocos2d::plugin::WebView *webView); + + virtual ~WebViewImpl(); + + void setJavascriptInterfaceScheme(const std::string &scheme); + + void loadData(const cocos2d::Data &data, const std::string &MIMEType, const std::string &encoding, const std::string &baseURL); + + void loadHTMLString(const std::string &string, const std::string &baseURL); + + void loadUrl(const std::string &url); + + void loadFile(const std::string &fileName); + + void stopLoading(); + + void reload(); + + bool canGoBack(); + + bool canGoForward(); + + void goBack(); + + void goForward(); + + void evaluateJS(const std::string &js); + + void setScalesPageToFit(const bool scalesPageToFit); + + virtual void draw(cocos2d::Renderer *renderer, cocos2d::Mat4 const &transform, uint32_t flags); + + virtual void setVisible(bool visible); + +private: + UIWebViewWrapper *_uiWebViewWrapper; + WebView *_webView; +}; + +} // namespace cocos2d +} // namespace plugin + +#endif //__cocos2d_plugin_WebViewImpl_IOS_H_ diff --git a/cocos/ui/WebViewImpl_iOS.mm b/cocos/ui/WebViewImpl_iOS.mm new file mode 100644 index 000000000000..7788f3dd1ffd --- /dev/null +++ b/cocos/ui/WebViewImpl_iOS.mm @@ -0,0 +1,134 @@ +// +// Created by gin0606 on 2014/07/30. +// + +#if CC_TARGET_PLATFORM == CC_PLATFORM_IOS + +#include "WebViewImpl_iOS.h" +#import "UIWebViewWrapper.h" +#include "renderer/CCRenderer.h" +#include "WebView.h" +#include "CCDirector.h" +#include "CCGLView.h" +#include "CCEAGLView.h" +#include "platform/CCFileUtils.h" + +namespace cocos2d { +namespace plugin { + +WebViewImpl::WebViewImpl(WebView *webView) + : _uiWebViewWrapper([UIWebViewWrapper webViewWrapper]), _webView(webView) { + [_uiWebViewWrapper retain]; + _uiWebViewWrapper.shouldStartLoading = [this](std::string url) { + if (this->_webView->shouldStartLoading) { + return this->_webView->shouldStartLoading(this->_webView, url); + } + return true; + }; + _uiWebViewWrapper.didFinishLoading = [this](std::string url) { + if (this->_webView->didFinishLoading) { + this->_webView->didFinishLoading(this->_webView, url); + } + }; + _uiWebViewWrapper.didFailLoading = [this](std::string url) { + if (this->_webView->didFailLoading) { + this->_webView->didFailLoading(this->_webView, url); + } + }; + _uiWebViewWrapper.onJsCallback = [this](std::string url) { + if (this->_webView->onJsCallback) { + this->_webView->onJsCallback(this->_webView, url); + } + }; +} + +WebViewImpl::~WebViewImpl() { + [_uiWebViewWrapper release]; + _uiWebViewWrapper = nullptr; +} + +void WebViewImpl::setJavascriptInterfaceScheme(const std::string &scheme) { + [_uiWebViewWrapper setJavascriptInterfaceScheme:scheme]; +} + +void WebViewImpl::loadData(const Data &data, const std::string &MIMEType, const std::string &encoding, const std::string &baseURL) { + std::string dataString(reinterpret_cast(data.getBytes()), static_cast(data.getSize())); + [_uiWebViewWrapper loadData:dataString MIMEType:MIMEType textEncodingName:encoding baseURL:baseURL]; +} + +void WebViewImpl::loadHTMLString(const std::string &string, const std::string &baseURL) { + [_uiWebViewWrapper loadHTMLString:string baseURL:baseURL]; +} + +void WebViewImpl::loadUrl(const std::string &url) { + [_uiWebViewWrapper loadUrl:url]; +} + +void WebViewImpl::loadFile(const std::string &fileName) { + auto fullPath = cocos2d::FileUtils::getInstance()->fullPathForFilename(fileName); + [_uiWebViewWrapper loadFile:fullPath]; +} + +void WebViewImpl::stopLoading() { + [_uiWebViewWrapper stopLoading]; +} + +void WebViewImpl::reload() { + [_uiWebViewWrapper reload]; +} + +bool WebViewImpl::canGoBack() { + return _uiWebViewWrapper.canGoBack; +} + +bool WebViewImpl::canGoForward() { + return _uiWebViewWrapper.canGoForward; +} + +void WebViewImpl::goBack() { + [_uiWebViewWrapper goBack]; +} + +void WebViewImpl::goForward() { + [_uiWebViewWrapper goForward]; +} + +void WebViewImpl::evaluateJS(const std::string &js) { + [_uiWebViewWrapper evaluateJS:js]; +} + +void WebViewImpl::setScalesPageToFit(const bool scalesPageToFit) { + [_uiWebViewWrapper setScalesPageToFit:scalesPageToFit]; +} + +void WebViewImpl::draw(cocos2d::Renderer *renderer, cocos2d::Mat4 const &transform, uint32_t flags) { + if (flags & cocos2d::Node::FLAGS_TRANSFORM_DIRTY) { + auto direcrot = cocos2d::Director::getInstance(); + auto glView = direcrot->getOpenGLView(); + auto frameSize = glView->getFrameSize(); + auto scaleFactor = [static_cast(glView->getEAGLView()) contentScaleFactor]; + + auto winSize = direcrot->getWinSize(); + + auto leftBottom = this->_webView->convertToWorldSpace(cocos2d::Vec2::ZERO); + auto rightTop = this->_webView->convertToWorldSpace(cocos2d::Vec2(this->_webView->getContentSize().width, this->_webView->getContentSize().height)); + + auto x = (frameSize.width / 2 + (leftBottom.x - winSize.width / 2) * glView->getScaleX()) / scaleFactor; + auto y = (frameSize.height / 2 - (rightTop.y - winSize.height / 2) * glView->getScaleY()) / scaleFactor; + auto width = (rightTop.x - leftBottom.x) * glView->getScaleX() / scaleFactor; + auto height = (rightTop.y - leftBottom.y) * glView->getScaleY() / scaleFactor; + + [_uiWebViewWrapper setFrameWithX:x + y:y + width:width + height:height]; + } +} + +void WebViewImpl::setVisible(bool visible) { + [_uiWebViewWrapper setVisible:visible]; +} +} // namespace cocos2d +} // namespace plugin + +#endif // CC_TARGET_PLATFORM == CC_PLATFORM_IOS From a3f4164b643a3cda5819fc88304f343ef4ce9775 Mon Sep 17 00:00:00 2001 From: gin0606 Date: Mon, 25 Aug 2014 14:46:26 +0900 Subject: [PATCH 02/14] Fix compile error --- build/cocos2d_libs.xcodeproj/project.pbxproj | 2 ++ 1 file changed, 2 insertions(+) diff --git a/build/cocos2d_libs.xcodeproj/project.pbxproj b/build/cocos2d_libs.xcodeproj/project.pbxproj index b82b9e196a2e..f1f482648b58 100644 --- a/build/cocos2d_libs.xcodeproj/project.pbxproj +++ b/build/cocos2d_libs.xcodeproj/project.pbxproj @@ -8733,6 +8733,7 @@ ALWAYS_SEARCH_USER_PATHS = YES; ARCHS = "$(ARCHS_STANDARD)"; EXECUTABLE_PREFIX = ""; + GCC_PREFIX_HEADER = "../cocos/cocos2d-prefix.pch"; GCC_PREPROCESSOR_DEFINITIONS = ( "$(inherited)", CC_TARGET_OS_IPHONE, @@ -8755,6 +8756,7 @@ ARCHS = "$(ARCHS_STANDARD)"; EXECUTABLE_PREFIX = ""; GCC_GENERATE_DEBUGGING_SYMBOLS = NO; + GCC_PREFIX_HEADER = "../cocos/cocos2d-prefix.pch"; GCC_PREPROCESSOR_DEFINITIONS = ( "$(inherited)", CC_TARGET_OS_IPHONE, From 41011824c147653a9aa2abce2473111728ef8ca7 Mon Sep 17 00:00:00 2001 From: gin0606 Date: Mon, 25 Aug 2014 15:01:18 +0900 Subject: [PATCH 03/14] Add WebView to libui Android --- cocos/ui/Android.mk | 3 + cocos/ui/WebView-inl.h | 2 +- cocos/ui/WebView.cpp | 7 + cocos/ui/WebViewImpl_android.cpp | 334 ++++++++++++++++++ cocos/ui/WebViewImpl_android.h | 72 ++++ ...org_cocos2dx_lib_Cocos2dxWebViewHelper.cpp | 35 ++ .../org_cocos2dx_lib_Cocos2dxWebViewHelper.h | 59 ++++ 7 files changed, 511 insertions(+), 1 deletion(-) create mode 100644 cocos/ui/WebView.cpp create mode 100644 cocos/ui/WebViewImpl_android.cpp create mode 100644 cocos/ui/WebViewImpl_android.h create mode 100644 cocos/ui/org_cocos2dx_lib_Cocos2dxWebViewHelper.cpp create mode 100644 cocos/ui/org_cocos2dx_lib_Cocos2dxWebViewHelper.h diff --git a/cocos/ui/Android.mk b/cocos/ui/Android.mk index ab417448f1fa..5dc94fe248fe 100644 --- a/cocos/ui/Android.mk +++ b/cocos/ui/Android.mk @@ -31,6 +31,9 @@ UIRelativeBox.cpp \ UIVideoPlayerAndroid.cpp \ UIDeprecated.cpp \ UIScale9Sprite.cpp \ +WebView.cpp \ +WebViewImpl_android.cpp \ +org_cocos2dx_lib_Cocos2dxWebViewHelper.cpp \ LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/.. \ $(LOCAL_PATH)/../editor-support diff --git a/cocos/ui/WebView-inl.h b/cocos/ui/WebView-inl.h index d044cf118953..b37323a3d41c 100644 --- a/cocos/ui/WebView-inl.h +++ b/cocos/ui/WebView-inl.h @@ -2,7 +2,7 @@ #define __CC_WebView_INL_H_ #include "WebView.h" -#include "CCGLView.h" +#include "platform/CCGLView.h" #include "base/CCDirector.h" #include "platform/CCFileUtils.h" diff --git a/cocos/ui/WebView.cpp b/cocos/ui/WebView.cpp new file mode 100644 index 000000000000..b8144927e735 --- /dev/null +++ b/cocos/ui/WebView.cpp @@ -0,0 +1,7 @@ +// +// Created by gin0606 on 2014/08/05. +// + + +#include "WebViewImpl_android.h" +#include "WebView-inl.h" diff --git a/cocos/ui/WebViewImpl_android.cpp b/cocos/ui/WebViewImpl_android.cpp new file mode 100644 index 000000000000..261db76781eb --- /dev/null +++ b/cocos/ui/WebViewImpl_android.cpp @@ -0,0 +1,334 @@ +// +// Created by gin0606 on 2014/07/30. +// + +#include "WebViewImpl_android.h" +#include "WebView.h" +#include "org_cocos2dx_lib_Cocos2dxWebViewHelper.h" +#include "jni/JniHelper.h" +#include "platform/CCGLView.h" +#include "base/CCDirector.h" +#include "platform/CCFileUtils.h" +#include + +#define CLASS_NAME "org/cocos2dx/lib/Cocos2dxWebViewHelper" + +namespace { +int createWebViewJNI() { + cocos2d::JniMethodInfo t; + if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "createWebView", "()I")) { + jint viewTag = t.env->CallStaticIntMethod(t.classID, t.methodID); + t.env->DeleteLocalRef(t.classID); + return viewTag; + } + return -1; +} + +void removeWebViewJNI(const int index) { + cocos2d::JniMethodInfo t; + if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "removeWebView", "(I)V")) { + t.env->CallStaticVoidMethod(t.classID, t.methodID, index); + t.env->DeleteLocalRef(t.classID); + } +} + +void setWebViewRectJNI(const int index, const int left, const int top, const int width, const int height) { + cocos2d::JniMethodInfo t; + if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "setWebViewRect", "(IIIII)V")) { + t.env->CallStaticVoidMethod(t.classID, t.methodID, index, left, top, width, height); + t.env->DeleteLocalRef(t.classID); + } +} + +void setJavascriptInterfaceSchemeJNI(const int index, const std::string &scheme) { + cocos2d::JniMethodInfo t; + if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "setJavascriptInterfaceScheme", "(ILjava/lang/String;)V")) { + jstring jScheme = t.env->NewStringUTF(scheme.c_str()); + t.env->CallStaticVoidMethod(t.classID, t.methodID, index, jScheme); + + t.env->DeleteLocalRef(jScheme); + t.env->DeleteLocalRef(t.classID); + } +} + +void loadDataJNI(const int index, const std::string &data, const std::string &MIMEType, const std::string &encoding, const std::string &baseURL) { + cocos2d::JniMethodInfo t; + if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "loadData", "(ILjava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V")) { + jstring jData = t.env->NewStringUTF(data.c_str()); + jstring jMIMEType = t.env->NewStringUTF(MIMEType.c_str()); + jstring jEncoding = t.env->NewStringUTF(encoding.c_str()); + jstring jBaseURL = t.env->NewStringUTF(baseURL.c_str()); + t.env->CallStaticVoidMethod(t.classID, t.methodID, index, jData, jMIMEType, jEncoding, jBaseURL); + + t.env->DeleteLocalRef(jData); + t.env->DeleteLocalRef(jMIMEType); + t.env->DeleteLocalRef(jEncoding); + t.env->DeleteLocalRef(jBaseURL); + t.env->DeleteLocalRef(t.classID); + } +} + +void loadHTMLStringJNI(const int index, const std::string &string, const std::string &baseURL) { + cocos2d::JniMethodInfo t; + if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "loadHTMLString", "(ILjava/lang/String;Ljava/lang/String;)V")) { + jstring jString = t.env->NewStringUTF(string.c_str()); + jstring jBaseURL = t.env->NewStringUTF(baseURL.c_str()); + t.env->CallStaticVoidMethod(t.classID, t.methodID, index, jString, jBaseURL); + + t.env->DeleteLocalRef(jString); + t.env->DeleteLocalRef(jBaseURL); + t.env->DeleteLocalRef(t.classID); + } +} + +void loadUrlJNI(const int index, const std::string &url) { + cocos2d::JniMethodInfo t; + if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "loadUrl", "(ILjava/lang/String;)V")) { + jstring jUrl = t.env->NewStringUTF(url.c_str()); + t.env->CallStaticVoidMethod(t.classID, t.methodID, index, jUrl); + + t.env->DeleteLocalRef(jUrl); + t.env->DeleteLocalRef(t.classID); + } +} + +void loadFileJNI(const int index, const std::string &filePath) { + cocos2d::JniMethodInfo t; + if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "loadFile", "(ILjava/lang/String;)V")) { + jstring jFilePath = t.env->NewStringUTF(filePath.c_str()); + t.env->CallStaticVoidMethod(t.classID, t.methodID, index, jFilePath); + + t.env->DeleteLocalRef(jFilePath); + t.env->DeleteLocalRef(t.classID); + } +} + +void stopLoadingJNI(const int index) { + cocos2d::JniMethodInfo t; + if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "stopLoading", "(I)V")) { + t.env->CallStaticVoidMethod(t.classID, t.methodID, index); + t.env->DeleteLocalRef(t.classID); + } +} + +void reloadJNI(const int index) { + cocos2d::JniMethodInfo t; + if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "reload", "(I)V")) { + t.env->CallStaticVoidMethod(t.classID, t.methodID, index); + t.env->DeleteLocalRef(t.classID); + } +} + +bool canGoBackJNI(const int index) { + cocos2d::JniMethodInfo t; + if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "canGoBack", "(I)Z")) { + jboolean ret = t.env->CallStaticBooleanMethod(t.classID, t.methodID, index); + t.env->DeleteLocalRef(t.classID); + return ret; + } + return false; +} + +bool canGoForwardJNI(const int index) { + cocos2d::JniMethodInfo t; + if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "canGoForward", "(I)Z")) { + jboolean ret = t.env->CallStaticBooleanMethod(t.classID, t.methodID, index); + t.env->DeleteLocalRef(t.classID); + return ret; + } + return false; +} + +void goBackJNI(const int index) { + cocos2d::JniMethodInfo t; + if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "goBack", "(I)V")) { + t.env->CallStaticVoidMethod(t.classID, t.methodID, index); + t.env->DeleteLocalRef(t.classID); + } +} + +void goForwardJNI(const int index) { + cocos2d::JniMethodInfo t; + if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "goForward", "(I)V")) { + t.env->CallStaticVoidMethod(t.classID, t.methodID, index); + t.env->DeleteLocalRef(t.classID); + } +} + +void evaluateJSJNI(const int index, const std::string &js) { + cocos2d::JniMethodInfo t; + if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "evaluateJS", "(ILjava/lang/String;)V")) { + jstring jjs = t.env->NewStringUTF(js.c_str()); + t.env->CallStaticVoidMethod(t.classID, t.methodID, index, jjs); + + t.env->DeleteLocalRef(jjs); + t.env->DeleteLocalRef(t.classID); + } +} + +void setScalesPageToFitJNI(const int index, const bool scalesPageToFit) { + cocos2d::JniMethodInfo t; + if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "setScalesPageToFit", "(IZ)V")) { + t.env->CallStaticVoidMethod(t.classID, t.methodID, index, scalesPageToFit); + t.env->DeleteLocalRef(t.classID); + } +} + +void setWebViewVisibleJNI(const int index, const bool visible) { + cocos2d::JniMethodInfo t; + if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "setVisible", "(IZ)V")) { + t.env->CallStaticVoidMethod(t.classID, t.methodID, index, visible); + t.env->DeleteLocalRef(t.classID); + } +} + +std::string getUrlStringByFileName(const std::string &fileName) { + const std::string basePath("file:///android_asset/"); + std::string fullPath = cocos2d::FileUtils::getInstance()->fullPathForFilename(fileName); + const std::string assetsPath("assets/"); + + std::string urlString; + if (fullPath.find(assetsPath) != std::string::npos) { + urlString = fullPath.replace(fullPath.find_first_of(assetsPath), assetsPath.length(), basePath); + } else { + urlString = fullPath; + } + + return urlString; +} +} // namespace + +namespace cocos2d { +namespace plugin { +static std::unordered_map s_WebViewImpls; + +WebViewImpl::WebViewImpl(WebView *webView) : _viewTag(-1), _webView(webView) { + _viewTag = createWebViewJNI(); + s_WebViewImpls[_viewTag] = this; +} + +WebViewImpl::~WebViewImpl() { + removeWebViewJNI(_viewTag); + s_WebViewImpls.erase(_viewTag); +} + +void WebViewImpl::loadData(const Data &data, const std::string &MIMEType, const std::string &encoding, const std::string &baseURL) { + std::string dataString(reinterpret_cast(data.getBytes()), static_cast(data.getSize())); + loadDataJNI(_viewTag, dataString, MIMEType, encoding, baseURL); +} + +void WebViewImpl::loadHTMLString(const std::string &string, const std::string &baseURL) { + loadHTMLStringJNI(_viewTag, string, baseURL); +} + +void WebViewImpl::loadUrl(const std::string &url) { + loadUrlJNI(_viewTag, url); +} + +void WebViewImpl::loadFile(const std::string &fileName) { + auto fullPath = getUrlStringByFileName(fileName); + loadFileJNI(_viewTag, fullPath); +} + +void WebViewImpl::stopLoading() { + stopLoadingJNI(_viewTag); +} + +void WebViewImpl::reload() { + reloadJNI(_viewTag); +} + +bool WebViewImpl::canGoBack() { + return canGoBackJNI(_viewTag); +} + +bool WebViewImpl::canGoForward() { + return canGoForwardJNI(_viewTag); +} + +void WebViewImpl::goBack() { + goBackJNI(_viewTag); +} + +void WebViewImpl::goForward() { + goForwardJNI(_viewTag); +} + +void WebViewImpl::setJavascriptInterfaceScheme(const std::string &scheme) { + setJavascriptInterfaceSchemeJNI(_viewTag, scheme); +} + +void WebViewImpl::evaluateJS(const std::string &js) { + evaluateJSJNI(_viewTag, js); +} + +void WebViewImpl::setScalesPageToFit(const bool scalesPageToFit) { + setScalesPageToFitJNI(_viewTag, scalesPageToFit); +} + +bool WebViewImpl::shouldStartLoading(const int viewTag, const std::string &url) { + auto it = s_WebViewImpls.find(viewTag); + if (it != s_WebViewImpls.end()) { + auto webView = s_WebViewImpls[viewTag]->_webView; + if (webView->shouldStartLoading) { + return webView->shouldStartLoading(webView, url); + } + } + return true; +} + +void WebViewImpl::didFinishLoading(const int viewTag, const std::string &url){ + auto it = s_WebViewImpls.find(viewTag); + if (it != s_WebViewImpls.end()) { + auto webView = s_WebViewImpls[viewTag]->_webView; + if (webView->didFinishLoading) { + webView->didFinishLoading(webView, url); + } + } +} + +void WebViewImpl::didFailLoading(const int viewTag, const std::string &url){ + auto it = s_WebViewImpls.find(viewTag); + if (it != s_WebViewImpls.end()) { + auto webView = s_WebViewImpls[viewTag]->_webView; + if (webView->didFailLoading) { + webView->didFailLoading(webView, url); + } + } +} + +void WebViewImpl::onJsCallback(const int viewTag, const std::string &message){ + auto it = s_WebViewImpls.find(viewTag); + if (it != s_WebViewImpls.end()) { + auto webView = s_WebViewImpls[viewTag]->_webView; + if (webView->onJsCallback) { + webView->onJsCallback(webView, message); + } + } +} + +void WebViewImpl::draw(cocos2d::Renderer *renderer, cocos2d::Mat4 const &transform, uint32_t flags) { + if (flags & cocos2d::Node::FLAGS_TRANSFORM_DIRTY) { + auto directorInstance = cocos2d::Director::getInstance(); + auto glView = directorInstance->getOpenGLView(); + auto frameSize = glView->getFrameSize(); + + auto winSize = directorInstance->getWinSize(); + + auto leftBottom = this->_webView->convertToWorldSpace(cocos2d::Point::ZERO); + auto rightTop = this->_webView->convertToWorldSpace(cocos2d::Point(this->_webView->getContentSize().width,this->_webView->getContentSize().height)); + + auto uiLeft = frameSize.width / 2 + (leftBottom.x - winSize.width / 2 ) * glView->getScaleX(); + auto uiTop = frameSize.height /2 - (rightTop.y - winSize.height / 2) * glView->getScaleY(); + + setWebViewRectJNI(_viewTag,uiLeft,uiTop, + (rightTop.x - leftBottom.x) * glView->getScaleX(), + (rightTop.y - leftBottom.y) * glView->getScaleY()); + } +} + +void WebViewImpl::setVisible(bool visible) { + setWebViewVisibleJNI(_viewTag, visible); +} +} // namespace cocos2d +} // namespace plugin diff --git a/cocos/ui/WebViewImpl_android.h b/cocos/ui/WebViewImpl_android.h new file mode 100644 index 000000000000..bc43f7228251 --- /dev/null +++ b/cocos/ui/WebViewImpl_android.h @@ -0,0 +1,72 @@ +// +// Created by gin0606 on 2014/07/30. +// + +#ifndef __cocos2d_plugin_WebViewImpl_android_H_ +#define __cocos2d_plugin_WebViewImpl_android_H_ + +#include + +namespace cocos2d { +class Data; +class Renderer; +class Mat4; + +namespace plugin { +class WebView; +} +} + +namespace cocos2d { +namespace plugin { + +class WebViewImpl { +public: + WebViewImpl(cocos2d::plugin::WebView *webView); + + virtual ~WebViewImpl(); + + void setJavascriptInterfaceScheme(const std::string &scheme); + + void loadData(const cocos2d::Data &data, const std::string &MIMEType, const std::string &encoding, const std::string &baseURL); + + void loadHTMLString(const std::string &string, const std::string &baseURL); + + void loadUrl(const std::string &url); + + void loadFile(const std::string &fileName); + + void stopLoading(); + + void reload(); + + bool canGoBack(); + + bool canGoForward(); + + void goBack(); + + void goForward(); + + void evaluateJS(const std::string &js); + + void setScalesPageToFit(const bool scalesPageToFit); + + virtual void draw(cocos2d::Renderer *renderer, cocos2d::Mat4 const &transform, uint32_t flags); + + virtual void setVisible(bool visible); + + static bool shouldStartLoading(const int viewTag, const std::string &url); + static void didFinishLoading(const int viewTag, const std::string &url); + static void didFailLoading(const int viewTag, const std::string &url); + static void onJsCallback(const int viewTag, const std::string &message); + +private: + int _viewTag; + WebView *_webView; +}; + +} // namespace cocos2d +} // namespace plugin + +#endif //__cocos2d_plugin_WebViewImpl_android_H_ diff --git a/cocos/ui/org_cocos2dx_lib_Cocos2dxWebViewHelper.cpp b/cocos/ui/org_cocos2dx_lib_Cocos2dxWebViewHelper.cpp new file mode 100644 index 000000000000..203bd4398730 --- /dev/null +++ b/cocos/ui/org_cocos2dx_lib_Cocos2dxWebViewHelper.cpp @@ -0,0 +1,35 @@ +#include "org_cocos2dx_lib_Cocos2dxWebViewHelper.h" +#include "WebViewImpl_android.h" +#include "WebView.h" +#include +#include +#include + + +JNIEXPORT jboolean JNICALL Java_org_cocos2dx_lib_Cocos2dxWebViewHelper_shouldStartLoading(JNIEnv *env, jclass, jint index, jstring jurl) { + auto charUrl = env->GetStringUTFChars(jurl, NULL); + std::string url = charUrl; + env->ReleaseStringUTFChars(jurl, charUrl); + return cocos2d::plugin::WebViewImpl::shouldStartLoading(index, url); +} + +JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxWebViewHelper_didFinishLoading(JNIEnv *env, jclass, jint index, jstring jurl) { + auto charUrl = env->GetStringUTFChars(jurl, NULL); + std::string url = charUrl; + env->ReleaseStringUTFChars(jurl, charUrl); + cocos2d::plugin::WebViewImpl::didFinishLoading(index, url); +} + +JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxWebViewHelper_didFailLoading(JNIEnv *env, jclass, jint index, jstring jurl) { + auto charUrl = env->GetStringUTFChars(jurl, NULL); + std::string url = charUrl; + env->ReleaseStringUTFChars(jurl, charUrl); + cocos2d::plugin::WebViewImpl::didFailLoading(index, url); +} + +JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxWebViewHelper_onJsCallback(JNIEnv *env, jclass, jint index, jstring jmessage) { + auto charMessage = env->GetStringUTFChars(jmessage, NULL); + std::string message = charMessage; + env->ReleaseStringUTFChars(jmessage, charMessage); + cocos2d::plugin::WebViewImpl::onJsCallback(index, message); +} diff --git a/cocos/ui/org_cocos2dx_lib_Cocos2dxWebViewHelper.h b/cocos/ui/org_cocos2dx_lib_Cocos2dxWebViewHelper.h new file mode 100644 index 000000000000..288bf797655a --- /dev/null +++ b/cocos/ui/org_cocos2dx_lib_Cocos2dxWebViewHelper.h @@ -0,0 +1,59 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include +/* Header for class org_cocos2dx_lib_Cocos2dxWebViewHelper */ + +#ifndef _Included_org_cocos2dx_lib_Cocos2dxWebViewHelper +#define _Included_org_cocos2dx_lib_Cocos2dxWebViewHelper +#ifdef __cplusplus +extern "C" { +#endif +/* Inaccessible static: handler */ +/* Inaccessible static: viewTag */ +#undef org_cocos2dx_lib_Cocos2dxWebViewHelper_kWebViewTaskCreate +#define org_cocos2dx_lib_Cocos2dxWebViewHelper_kWebViewTaskCreate 0L +#undef org_cocos2dx_lib_Cocos2dxWebViewHelper_kWebViewTaskRemove +#define org_cocos2dx_lib_Cocos2dxWebViewHelper_kWebViewTaskRemove 1L +#undef org_cocos2dx_lib_Cocos2dxWebViewHelper_kWebViewTaskSetRect +#define org_cocos2dx_lib_Cocos2dxWebViewHelper_kWebViewTaskSetRect 2L +#undef org_cocos2dx_lib_Cocos2dxWebViewHelper_kWebViewTaskLoadURI +#define org_cocos2dx_lib_Cocos2dxWebViewHelper_kWebViewTaskLoadURI 3L +#undef org_cocos2dx_lib_Cocos2dxWebViewHelper_kWebViewTaskEvaluateJS +#define org_cocos2dx_lib_Cocos2dxWebViewHelper_kWebViewTaskEvaluateJS 4L +#undef org_cocos2dx_lib_Cocos2dxWebViewHelper_kWebViewTaskSetVisible +#define org_cocos2dx_lib_Cocos2dxWebViewHelper_kWebViewTaskSetVisible 5L +/* + * Class: org_cocos2dx_lib_Cocos2dxWebViewHelper + * Method: shouldStartLoading + * Signature: (ILjava/lang/String;)Z + */ +JNIEXPORT jboolean JNICALL Java_org_cocos2dx_lib_Cocos2dxWebViewHelper_shouldStartLoading + (JNIEnv *, jclass, jint, jstring); + +/* + * Class: org_cocos2dx_lib_Cocos2dxWebViewHelper + * Method: didFinishLoading + * Signature: (ILjava/lang/String;)V + */ +JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxWebViewHelper_didFinishLoading + (JNIEnv *, jclass, jint, jstring); + +/* + * Class: org_cocos2dx_lib_Cocos2dxWebViewHelper + * Method: didFailLoading + * Signature: (ILjava/lang/String;)V + */ +JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxWebViewHelper_didFailLoading + (JNIEnv *, jclass, jint, jstring); + +/* + * Class: org_cocos2dx_lib_Cocos2dxWebViewHelper + * Method: onJsCallback + * Signature: (ILjava/lang/String;)V + */ +JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxWebViewHelper_onJsCallback + (JNIEnv *, jclass, jint, jstring); + +#ifdef __cplusplus +} +#endif +#endif From 2226d4b420ff947346434d06726cd5d2adf17d82 Mon Sep 17 00:00:00 2001 From: andyque Date: Tue, 26 Aug 2014 11:39:56 +0800 Subject: [PATCH 04/14] change WebView namespace and add WebViewTest --- build/cocos2d_libs.xcodeproj/project.pbxproj | 24 +++++ build/cocos2d_tests.xcodeproj/project.pbxproj | 14 +++ cocos/ui/CocosGUI.h | 1 + cocos/ui/UIWebViewWrapper.mm | 2 + cocos/ui/WebView-inl.h | 91 ------------------- cocos/ui/WebView.cpp | 3 +- cocos/ui/WebView.h | 17 +++- cocos/ui/WebView.mm | 90 +++++++++++++++++- cocos/ui/WebViewImpl_iOS.h | 11 ++- cocos/ui/WebViewImpl_iOS.mm | 13 ++- .../CocoStudioGUITest/CocosGUIScene.cpp | 14 +++ .../CocoStudioGUITest/UISceneManager.cpp | 4 + .../UITest/CocoStudioGUITest/UISceneManager.h | 1 + .../UIWebViewTest/UIWebViewTest.cpp | 39 ++++++++ .../UIWebViewTest/UIWebViewTest.h | 46 ++++++++++ 15 files changed, 265 insertions(+), 105 deletions(-) delete mode 100644 cocos/ui/WebView-inl.h create mode 100644 tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIWebViewTest/UIWebViewTest.cpp create mode 100644 tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIWebViewTest/UIWebViewTest.h diff --git a/build/cocos2d_libs.xcodeproj/project.pbxproj b/build/cocos2d_libs.xcodeproj/project.pbxproj index 6178a6bb8770..80fff3af8ab6 100644 --- a/build/cocos2d_libs.xcodeproj/project.pbxproj +++ b/build/cocos2d_libs.xcodeproj/project.pbxproj @@ -1351,6 +1351,12 @@ 1AC0269C1914068200FA920D /* ConvertUTF.h in Headers */ = {isa = PBXBuildFile; fileRef = 1AC026991914068200FA920D /* ConvertUTF.h */; }; 1AC0269D1914068200FA920D /* ConvertUTF.h in Headers */ = {isa = PBXBuildFile; fileRef = 1AC026991914068200FA920D /* ConvertUTF.h */; }; 2986667F18B1B246000E39CA /* CCTweenFunction.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2986667818B1B079000E39CA /* CCTweenFunction.cpp */; }; + 298D7F6419AC2EFD00FF096D /* UIWebViewWrapper.h in Headers */ = {isa = PBXBuildFile; fileRef = 298D7F5C19AC2EFD00FF096D /* UIWebViewWrapper.h */; }; + 298D7F6519AC2EFD00FF096D /* UIWebViewWrapper.mm in Sources */ = {isa = PBXBuildFile; fileRef = 298D7F5D19AC2EFD00FF096D /* UIWebViewWrapper.mm */; }; + 298D7F6819AC2EFD00FF096D /* WebView.h in Headers */ = {isa = PBXBuildFile; fileRef = 298D7F6019AC2EFD00FF096D /* WebView.h */; }; + 298D7F6919AC2EFD00FF096D /* WebView.mm in Sources */ = {isa = PBXBuildFile; fileRef = 298D7F6119AC2EFD00FF096D /* WebView.mm */; }; + 298D7F6A19AC2EFD00FF096D /* WebViewImpl_iOS.h in Headers */ = {isa = PBXBuildFile; fileRef = 298D7F6219AC2EFD00FF096D /* WebViewImpl_iOS.h */; }; + 298D7F6B19AC2EFD00FF096D /* WebViewImpl_iOS.mm in Sources */ = {isa = PBXBuildFile; fileRef = 298D7F6319AC2EFD00FF096D /* WebViewImpl_iOS.mm */; }; 299754F4193EC95400A54AC3 /* ObjectFactory.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 299754F2193EC95400A54AC3 /* ObjectFactory.cpp */; }; 299754F5193EC95400A54AC3 /* ObjectFactory.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 299754F2193EC95400A54AC3 /* ObjectFactory.cpp */; }; 299754F6193EC95400A54AC3 /* ObjectFactory.h in Headers */ = {isa = PBXBuildFile; fileRef = 299754F3193EC95400A54AC3 /* ObjectFactory.h */; }; @@ -2300,6 +2306,12 @@ 2958244A19873D8E00F9746D /* UIScale9Sprite.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UIScale9Sprite.h; sourceTree = ""; }; 2986667818B1B079000E39CA /* CCTweenFunction.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CCTweenFunction.cpp; sourceTree = ""; }; 2986667918B1B079000E39CA /* CCTweenFunction.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCTweenFunction.h; sourceTree = ""; }; + 298D7F5C19AC2EFD00FF096D /* UIWebViewWrapper.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UIWebViewWrapper.h; sourceTree = ""; }; + 298D7F5D19AC2EFD00FF096D /* UIWebViewWrapper.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = UIWebViewWrapper.mm; sourceTree = ""; }; + 298D7F6019AC2EFD00FF096D /* WebView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WebView.h; sourceTree = ""; }; + 298D7F6119AC2EFD00FF096D /* WebView.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = WebView.mm; sourceTree = ""; }; + 298D7F6219AC2EFD00FF096D /* WebViewImpl_iOS.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WebViewImpl_iOS.h; sourceTree = ""; }; + 298D7F6319AC2EFD00FF096D /* WebViewImpl_iOS.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = WebViewImpl_iOS.mm; sourceTree = ""; }; 299754F2193EC95400A54AC3 /* ObjectFactory.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = ObjectFactory.cpp; path = ../base/ObjectFactory.cpp; sourceTree = ""; }; 299754F3193EC95400A54AC3 /* ObjectFactory.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ObjectFactory.h; path = ../base/ObjectFactory.h; sourceTree = ""; }; 299CF1F919A434BC00C378C1 /* ccRandom.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = ccRandom.cpp; path = ../base/ccRandom.cpp; sourceTree = ""; }; @@ -4016,6 +4028,12 @@ 29CB8F521929D65500C841D6 /* experimental */ = { isa = PBXGroup; children = ( + 298D7F5C19AC2EFD00FF096D /* UIWebViewWrapper.h */, + 298D7F5D19AC2EFD00FF096D /* UIWebViewWrapper.mm */, + 298D7F6019AC2EFD00FF096D /* WebView.h */, + 298D7F6119AC2EFD00FF096D /* WebView.mm */, + 298D7F6219AC2EFD00FF096D /* WebViewImpl_iOS.h */, + 298D7F6319AC2EFD00FF096D /* WebViewImpl_iOS.mm */, 3EA0FB69191C841D00B170C8 /* UIVideoPlayer.h */, 3EA0FB6A191C841D00B170C8 /* UIVideoPlayerIOS.mm */, ); @@ -5454,6 +5472,7 @@ 46A171061807CECB005B8026 /* CCPhysicsWorld.h in Headers */, 50ABBDB41925AB4100A911A9 /* ccShaders.h in Headers */, 15AE19B319AAD39700C27E9E /* SliderReader.h in Headers */, + 298D7F6419AC2EFD00FF096D /* UIWebViewWrapper.h in Headers */, 15AE1B7919AADA9A00C27E9E /* UIRichText.h in Headers */, 15AE1A4A19AAD3D500C27E9E /* b2CircleShape.h in Headers */, 50ABBE861925AB6F00A911A9 /* ccFPSImages.h in Headers */, @@ -5465,6 +5484,7 @@ 15AE1A4019AAD3D500C27E9E /* b2Collision.h in Headers */, 5034CA40191D591100CE6051 /* ccShader_Position_uColor.vert in Headers */, 15AE184719AAD2F700C27E9E /* CCSprite3DMaterial.h in Headers */, + 298D7F6819AC2EFD00FF096D /* WebView.h in Headers */, 15AE1BFC19AAE01E00C27E9E /* CCControlUtils.h in Headers */, 15AE193519AAD35100C27E9E /* CCActionObject.h in Headers */, 15AE1AA919AAD40300C27E9E /* b2TimeStep.h in Headers */, @@ -5839,6 +5859,7 @@ 15AE1B0919AAD42500C27E9E /* cpPolyShape.h in Headers */, 15AE193919AAD35100C27E9E /* CCArmatureAnimation.h in Headers */, 15AE1AA419AAD40300C27E9E /* b2ContactManager.h in Headers */, + 298D7F6A19AC2EFD00FF096D /* WebViewImpl_iOS.h in Headers */, B276EF601988D1D500CD400F /* CCVertexIndexData.h in Headers */, 50ABBD5F1925AB0000A911A9 /* Vec3.h in Headers */, 50ABBE921925AB6F00A911A9 /* CCPlatformMacros.h in Headers */, @@ -6672,6 +6693,7 @@ 15AE1A4319AAD3D500C27E9E /* b2DynamicTree.cpp in Sources */, 15AE1BFF19AAE01E00C27E9E /* CCScale9Sprite.cpp in Sources */, 15AE1A3A19AAD3D500C27E9E /* b2BroadPhase.cpp in Sources */, + 298D7F6519AC2EFD00FF096D /* UIWebViewWrapper.mm in Sources */, 15AE19B619AAD39700C27E9E /* TextBMFontReader.cpp in Sources */, 15AE1BFD19AAE01E00C27E9E /* CCInvocation.cpp in Sources */, B24AA98A195A675C007B4522 /* CCFastTMXTiledMap.cpp in Sources */, @@ -6710,6 +6732,7 @@ 1A5702C9180BCE370088DEC7 /* CCTextFieldTTF.cpp in Sources */, 15AE1A0519AAD3A700C27E9E /* Bone.cpp in Sources */, 15AE1B3D19AAD43700C27E9E /* cpCollision.c in Sources */, + 298D7F6919AC2EFD00FF096D /* WebView.mm in Sources */, 15AE1C1719AAE2C700C27E9E /* CCPhysicsSprite.cpp in Sources */, 1A5702EB180BCE750088DEC7 /* CCTileMapAtlas.cpp in Sources */, 1A5702EF180BCE750088DEC7 /* CCTMXLayer.cpp in Sources */, @@ -6762,6 +6785,7 @@ 50ABBE5E1925AB6F00A911A9 /* CCEventListener.cpp in Sources */, 15AE1BC719AAE00000C27E9E /* AssetsManager.cpp in Sources */, 50ABBEA81925AB6F00A911A9 /* CCTouch.cpp in Sources */, + 298D7F6B19AC2EFD00FF096D /* WebViewImpl_iOS.mm in Sources */, 15AE1A9619AAD40300C27E9E /* b2Draw.cpp in Sources */, 503DD8E91926736A00CD74DD /* CCES2Renderer.m in Sources */, 5027253D190BF1B900AAF4ED /* cocos2d.cpp in Sources */, diff --git a/build/cocos2d_tests.xcodeproj/project.pbxproj b/build/cocos2d_tests.xcodeproj/project.pbxproj index 977187abace2..a0dc2b00f60a 100644 --- a/build/cocos2d_tests.xcodeproj/project.pbxproj +++ b/build/cocos2d_tests.xcodeproj/project.pbxproj @@ -865,6 +865,7 @@ 290E94B6196FC16900694919 /* CocostudioParserTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 290E94B3196FC16900694919 /* CocostudioParserTest.cpp */; }; 295824591987415900F9746D /* UIScale9SpriteTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 295824571987415900F9746D /* UIScale9SpriteTest.cpp */; }; 2958245A1987415900F9746D /* UIScale9SpriteTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 295824571987415900F9746D /* UIScale9SpriteTest.cpp */; }; + 298D7F6F19AC31F300FF096D /* UIWebViewTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 298D7F6D19AC31F300FF096D /* UIWebViewTest.cpp */; }; 29FBBBFE196A9ECD00E65826 /* CocostudioParserJsonTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 29FBBBFC196A9ECD00E65826 /* CocostudioParserJsonTest.cpp */; }; 29FBBBFF196A9ECD00E65826 /* CocostudioParserJsonTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 29FBBBFC196A9ECD00E65826 /* CocostudioParserJsonTest.cpp */; }; 38FA2E73194AEBE100FF2BE4 /* ActionTimelineTestScene.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 38FA2E71194AEBE100FF2BE4 /* ActionTimelineTestScene.cpp */; }; @@ -2886,6 +2887,8 @@ 290E94B4196FC16900694919 /* CocostudioParserTest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CocostudioParserTest.h; path = ../CocostudioParserTest.h; sourceTree = ""; }; 295824571987415900F9746D /* UIScale9SpriteTest.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = UIScale9SpriteTest.cpp; sourceTree = ""; }; 295824581987415900F9746D /* UIScale9SpriteTest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UIScale9SpriteTest.h; sourceTree = ""; }; + 298D7F6D19AC31F300FF096D /* UIWebViewTest.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = UIWebViewTest.cpp; path = UIWebViewTest/UIWebViewTest.cpp; sourceTree = ""; }; + 298D7F6E19AC31F300FF096D /* UIWebViewTest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = UIWebViewTest.h; path = UIWebViewTest/UIWebViewTest.h; sourceTree = ""; }; 29FBBBFC196A9ECD00E65826 /* CocostudioParserJsonTest.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CocostudioParserJsonTest.cpp; sourceTree = ""; }; 29FBBBFD196A9ECD00E65826 /* CocostudioParserJsonTest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CocostudioParserJsonTest.h; sourceTree = ""; }; 38FA2E71194AEBE100FF2BE4 /* ActionTimelineTestScene.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ActionTimelineTestScene.cpp; sourceTree = ""; }; @@ -6907,6 +6910,15 @@ path = UIWidgetAddNodeTest; sourceTree = ""; }; + 298D7F6C19AC31C000FF096D /* UIWebViewTest */ = { + isa = PBXGroup; + children = ( + 298D7F6D19AC31F300FF096D /* UIWebViewTest.cpp */, + 298D7F6E19AC31F300FF096D /* UIWebViewTest.h */, + ); + name = UIWebViewTest; + sourceTree = ""; + }; 29B97314FDCFA39411CA2CEA /* CustomTemplate */ = { isa = PBXGroup; children = ( @@ -6988,6 +7000,7 @@ 29FBBC00196A9F0D00E65826 /* UIAndEditorTests */ = { isa = PBXGroup; children = ( + 298D7F6C19AC31C000FF096D /* UIWebViewTest */, 295824571987415900F9746D /* UIScale9SpriteTest.cpp */, 295824581987415900F9746D /* UIScale9SpriteTest.h */, 29080D1F191B595E0066F8DF /* CocosGUIScene.cpp */, @@ -8316,6 +8329,7 @@ 1AC35B3E18CECF0C00F37B72 /* Bug-422.cpp in Sources */, 29080DAC191B595E0066F8DF /* UIFocusTest.cpp in Sources */, 1AC35BF618CECF0C00F37B72 /* HttpClientTest.cpp in Sources */, + 298D7F6F19AC31F300FF096D /* UIWebViewTest.cpp in Sources */, 29080DA6191B595E0066F8DF /* UIButtonTest_Editor.cpp in Sources */, 1AC35B5018CECF0C00F37B72 /* ClickAndMoveTest.cpp in Sources */, ); diff --git a/cocos/ui/CocosGUI.h b/cocos/ui/CocosGUI.h index 5fc08abefcae..b4516a3512db 100644 --- a/cocos/ui/CocosGUI.h +++ b/cocos/ui/CocosGUI.h @@ -47,6 +47,7 @@ THE SOFTWARE. #include "ui/UIRelativeBox.h" #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS) #include "ui/UIVideoPlayer.h" +#include "ui/WebView.h" #endif #include "ui/UIDeprecated.h" #include "ui/GUIExport.h" diff --git a/cocos/ui/UIWebViewWrapper.mm b/cocos/ui/UIWebViewWrapper.mm index 600d8290f842..f5083dbeac7d 100644 --- a/cocos/ui/UIWebViewWrapper.mm +++ b/cocos/ui/UIWebViewWrapper.mm @@ -9,6 +9,8 @@ #import "CCEAGLView.h" #import "CCDirector.h" +using namespace cocos2d::experimental::ui; + @interface UIWebViewWrapper () @property(nonatomic, retain) UIWebView *uiWebView; diff --git a/cocos/ui/WebView-inl.h b/cocos/ui/WebView-inl.h deleted file mode 100644 index b37323a3d41c..000000000000 --- a/cocos/ui/WebView-inl.h +++ /dev/null @@ -1,91 +0,0 @@ -#ifndef __CC_WebView_INL_H_ -#define __CC_WebView_INL_H_ - -#include "WebView.h" -#include "platform/CCGLView.h" -#include "base/CCDirector.h" -#include "platform/CCFileUtils.h" - -namespace cocos2d { -namespace plugin { -WebView::WebView() : _impl(new WebViewImpl(this)) { -} - -WebView::~WebView() { - delete _impl; -} - -WebView *WebView::create() { - auto pRet = new WebView(); - if (pRet->init()) { - pRet->autorelease(); - return pRet; - } - return nullptr; -} - -void WebView::setJavascriptInterfaceScheme(const std::string &scheme) { - _impl->setJavascriptInterfaceScheme(scheme); -} - -void WebView::loadData(const cocos2d::Data &data, const std::string &MIMEType, const std::string &encoding, const std::string &baseURL) { - _impl->loadData(data, MIMEType, encoding, baseURL); -} - -void WebView::loadHTMLString(const std::string &string, const std::string &baseURL) { - _impl->loadHTMLString(string, baseURL); -} - -void WebView::loadUrl(const std::string &url) { - _impl->loadUrl(url); -} - -void WebView::loadFile(const std::string &fileName) { - _impl->loadFile(fileName); -} - -void WebView::stopLoading() { - _impl->stopLoading(); -} - -void WebView::reload() { - _impl->reload(); -} - -bool WebView::canGoBack() { - return _impl->canGoBack(); -} - -bool WebView::canGoForward() { - return _impl->canGoForward(); -} - -void WebView::goBack() { - _impl->goBack(); -} - -void WebView::goForward() { - _impl->goForward(); -} - -void WebView::evaluateJS(const std::string &js) { - _impl->evaluateJS(js); -} - -void WebView::setScalesPageToFit(bool const scalesPageToFit) { - _impl->setScalesPageToFit(scalesPageToFit); -} - -void WebView::draw(cocos2d::Renderer *renderer, cocos2d::Mat4 const &transform, uint32_t flags) { - cocos2d::ui::Widget::draw(renderer, transform, flags); - _impl->draw(renderer, transform, flags); -} - -void WebView::setVisible(bool visible) { - Node::setVisible(visible); - _impl->setVisible(visible); -} -} // namespace cocos2d -} // namespace plugin - -#endif diff --git a/cocos/ui/WebView.cpp b/cocos/ui/WebView.cpp index b8144927e735..43772dc2c195 100644 --- a/cocos/ui/WebView.cpp +++ b/cocos/ui/WebView.cpp @@ -2,6 +2,5 @@ // Created by gin0606 on 2014/08/05. // - -#include "WebViewImpl_android.h" +#include "WebViewImpl_iOS.h" #include "WebView-inl.h" diff --git a/cocos/ui/WebView.h b/cocos/ui/WebView.h index 72f9053c47c6..325a1276d8db 100644 --- a/cocos/ui/WebView.h +++ b/cocos/ui/WebView.h @@ -5,11 +5,16 @@ #ifndef __Cocos2d_Plugin_WebView_H_ #define __Cocos2d_Plugin_WebView_H_ +#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS) + + #include "ui/UIWidget.h" #include "base/CCData.h" -namespace cocos2d { -namespace plugin { +NS_CC_BEGIN +namespace experimental{ + namespace ui{ + class WebViewImpl; class WebView : public cocos2d::ui::Widget { @@ -135,9 +140,13 @@ class WebView : public cocos2d::ui::Widget { virtual void setVisible(bool visible) override; private: - cocos2d::plugin::WebViewImpl *_impl; + WebViewImpl *_impl; }; + } // namespace cocos2d -} // namespace plugin +} // namespace experimental +}//namespace ui + +#endif #endif //__Cocos2d_Plugin_WebView_H_ diff --git a/cocos/ui/WebView.mm b/cocos/ui/WebView.mm index c0ed30d1e787..b670bc2b8893 100644 --- a/cocos/ui/WebView.mm +++ b/cocos/ui/WebView.mm @@ -4,4 +4,92 @@ #include "WebViewImpl_iOS.h" -#include "WebView-inl.h" +#include "WebView.h" +#include "platform/CCGLView.h" +#include "base/CCDirector.h" +#include "platform/CCFileUtils.h" + +NS_CC_BEGIN +namespace experimental{ + namespace ui{ + + WebView::WebView() : _impl(new WebViewImpl(this)) { + } + + WebView::~WebView() { + delete _impl; + } + + WebView *WebView::create() { + auto pRet = new WebView(); + if (pRet->init()) { + pRet->autorelease(); + return pRet; + } + return nullptr; + } + + void WebView::setJavascriptInterfaceScheme(const std::string &scheme) { + _impl->setJavascriptInterfaceScheme(scheme); + } + + void WebView::loadData(const cocos2d::Data &data, const std::string &MIMEType, const std::string &encoding, const std::string &baseURL) { + _impl->loadData(data, MIMEType, encoding, baseURL); + } + + void WebView::loadHTMLString(const std::string &string, const std::string &baseURL) { + _impl->loadHTMLString(string, baseURL); + } + + void WebView::loadUrl(const std::string &url) { + _impl->loadUrl(url); + } + + void WebView::loadFile(const std::string &fileName) { + _impl->loadFile(fileName); + } + + void WebView::stopLoading() { + _impl->stopLoading(); + } + + void WebView::reload() { + _impl->reload(); + } + + bool WebView::canGoBack() { + return _impl->canGoBack(); + } + + bool WebView::canGoForward() { + return _impl->canGoForward(); + } + + void WebView::goBack() { + _impl->goBack(); + } + + void WebView::goForward() { + _impl->goForward(); + } + + void WebView::evaluateJS(const std::string &js) { + _impl->evaluateJS(js); + } + + void WebView::setScalesPageToFit(bool const scalesPageToFit) { + _impl->setScalesPageToFit(scalesPageToFit); + } + + void WebView::draw(cocos2d::Renderer *renderer, cocos2d::Mat4 const &transform, uint32_t flags) { + cocos2d::ui::Widget::draw(renderer, transform, flags); + _impl->draw(renderer, transform, flags); + } + + void WebView::setVisible(bool visible) { + Node::setVisible(visible); + _impl->setVisible(visible); + } + } // namespace cocos2d +} // namespace ui +} //namespace experimental \ No newline at end of file diff --git a/cocos/ui/WebViewImpl_iOS.h b/cocos/ui/WebViewImpl_iOS.h index a2f317235e48..04dce9039e4b 100644 --- a/cocos/ui/WebViewImpl_iOS.h +++ b/cocos/ui/WebViewImpl_iOS.h @@ -10,15 +10,19 @@ @class UIWebViewWrapper; namespace cocos2d { + class Data; class Renderer; class Mat4; -namespace plugin { + +namespace experimental { + namespace ui{ + class WebView; class WebViewImpl { public: - WebViewImpl(cocos2d::plugin::WebView *webView); + WebViewImpl(WebView *webView); virtual ~WebViewImpl(); @@ -58,6 +62,7 @@ class WebViewImpl { }; } // namespace cocos2d -} // namespace plugin +} // namespace experimental +}//namespace ui #endif //__cocos2d_plugin_WebViewImpl_IOS_H_ diff --git a/cocos/ui/WebViewImpl_iOS.mm b/cocos/ui/WebViewImpl_iOS.mm index 7788f3dd1ffd..d26393581d1b 100644 --- a/cocos/ui/WebViewImpl_iOS.mm +++ b/cocos/ui/WebViewImpl_iOS.mm @@ -5,16 +5,19 @@ #if CC_TARGET_PLATFORM == CC_PLATFORM_IOS #include "WebViewImpl_iOS.h" -#import "UIWebViewWrapper.h" #include "renderer/CCRenderer.h" -#include "WebView.h" #include "CCDirector.h" #include "CCGLView.h" #include "CCEAGLView.h" #include "platform/CCFileUtils.h" +#import "UIWebViewWrapper.h" +#include "ui/WebView.h" + + namespace cocos2d { -namespace plugin { +namespace experimental { + namespace ui{ WebViewImpl::WebViewImpl(WebView *webView) : _uiWebViewWrapper([UIWebViewWrapper webViewWrapper]), _webView(webView) { @@ -128,7 +131,9 @@ void WebViewImpl::setVisible(bool visible) { [_uiWebViewWrapper setVisible:visible]; } + } // namespace cocos2d -} // namespace plugin +} // namespace experimental +} //namespace ui #endif // CC_TARGET_PLATFORM == CC_PLATFORM_IOS diff --git a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/CocosGUIScene.cpp b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/CocosGUIScene.cpp index 51fb94d96704..164ca4c5d292 100644 --- a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/CocosGUIScene.cpp +++ b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/CocosGUIScene.cpp @@ -29,6 +29,20 @@ g_guisTests[] = Director::getInstance()->replaceScene(scene); } }, +#endif +#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS) + { + "WebViewTest", + [](Ref* sender) + { + UISceneManager* sceneManager = UISceneManager::sharedUISceneManager(); + sceneManager->setCurrentUISceneId(KWebViewTest); + sceneManager->setMinUISceneId(KWebViewTest); + sceneManager->setMaxUISceneId(KWebViewTest); + Scene* scene = sceneManager->currentUIScene(); + Director::getInstance()->replaceScene(scene); + } + }, #endif { "focus test", diff --git a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISceneManager.cpp b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISceneManager.cpp index 11e781e25547..bdbfc3aa5bfb 100644 --- a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISceneManager.cpp +++ b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISceneManager.cpp @@ -20,6 +20,7 @@ #include "UIFocusTest/UIFocusTest.h" #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS) #include "UIVideoPlayerTest/UIVideoPlayerTest.h" +#include "UIWebViewTest/UIWebViewTest.h" #endif #include "UIScale9SpriteTest.h" @@ -94,6 +95,7 @@ static const char* s_testArray[] = "UIFocusTest-ListView", #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS) "UIVideoPlayerTest", + "UIWebViewTest", #endif "UIScale9SpriteTest", "UIScale9SpriteHierarchialTest", @@ -328,6 +330,8 @@ Scene *UISceneManager::currentUIScene() #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS) case kUIVideoPlayerTest: return VideoPlayerTest::sceneWithTitle(s_testArray[_currentUISceneId]); + case KWebViewTest: + return WebViewTest::sceneWithTitle(s_testArray[_currentUISceneId]); #endif case kUIScale9SpriteTest: return UIScale9SpriteTest::sceneWithTitle(s_testArray[_currentUISceneId]); diff --git a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISceneManager.h b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISceneManager.h index 4361de611fb4..6bf7c2c2c93b 100644 --- a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISceneManager.h +++ b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISceneManager.h @@ -92,6 +92,7 @@ enum kUIFocusTest_ListView, #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS) kUIVideoPlayerTest, + KWebViewTest, #endif kUIScale9SpriteTest, kUIScale9SpriteHierarchialTest, diff --git a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIWebViewTest/UIWebViewTest.cpp b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIWebViewTest/UIWebViewTest.cpp new file mode 100644 index 000000000000..847bbdcb9237 --- /dev/null +++ b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIWebViewTest/UIWebViewTest.cpp @@ -0,0 +1,39 @@ +/**************************************************************************** + 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. + ****************************************************************************/ + +#include "UIWebViewTest.h" + +bool WebViewTest::init() +{ + if (UIScene::init()) { + _webView = experimental::ui::WebView::create(); + _webView->setPosition(Director::getInstance()->getVisibleSize()/2); + _webView->setContentSize(Director::getInstance()->getVisibleSize() * 0.5); + _webView->loadUrl("http://www.baidu.com"); + this->addChild(_webView); + + return true; + } + return false; +} \ No newline at end of file diff --git a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIWebViewTest/UIWebViewTest.h b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIWebViewTest/UIWebViewTest.h new file mode 100644 index 000000000000..9365412eb03d --- /dev/null +++ b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIWebViewTest/UIWebViewTest.h @@ -0,0 +1,46 @@ +/**************************************************************************** + 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. + ****************************************************************************/ + +#ifndef __cocos2d_tests__UIWebViewTest__ +#define __cocos2d_tests__UIWebViewTest__ + +#include "../UIScene.h" + +USING_NS_CC; + +class WebViewTest : public UIScene +{ +public: + UI_SCENE_CREATE_FUNC(WebViewTest); + + virtual bool init(); + + + +private: + cocos2d::experimental::ui::WebView *_webView; + +}; + +#endif /* defined(__cocos2d_tests__UIWebViewTest__) */ From e12db468664bb074caad4321b2f32c6594cd3619 Mon Sep 17 00:00:00 2001 From: andyque Date: Tue, 26 Aug 2014 12:08:26 +0800 Subject: [PATCH 05/14] fix style --- build/cocos2d_libs.xcodeproj/project.pbxproj | 12 - cocos/ui/UIWebViewWrapper.h | 47 ---- cocos/ui/UIWebViewWrapper.mm | 158 ------------ cocos/ui/WebView.h | 36 ++- cocos/ui/WebView.mm | 105 +++++--- cocos/ui/WebViewImpl_iOS.h | 35 ++- cocos/ui/WebViewImpl_iOS.mm | 231 +++++++++++++++++- .../UIWebViewTest/UIWebViewTest.cpp | 1 + 8 files changed, 358 insertions(+), 267 deletions(-) delete mode 100644 cocos/ui/UIWebViewWrapper.h delete mode 100644 cocos/ui/UIWebViewWrapper.mm diff --git a/build/cocos2d_libs.xcodeproj/project.pbxproj b/build/cocos2d_libs.xcodeproj/project.pbxproj index 80fff3af8ab6..dfa251f9db47 100644 --- a/build/cocos2d_libs.xcodeproj/project.pbxproj +++ b/build/cocos2d_libs.xcodeproj/project.pbxproj @@ -1351,8 +1351,6 @@ 1AC0269C1914068200FA920D /* ConvertUTF.h in Headers */ = {isa = PBXBuildFile; fileRef = 1AC026991914068200FA920D /* ConvertUTF.h */; }; 1AC0269D1914068200FA920D /* ConvertUTF.h in Headers */ = {isa = PBXBuildFile; fileRef = 1AC026991914068200FA920D /* ConvertUTF.h */; }; 2986667F18B1B246000E39CA /* CCTweenFunction.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2986667818B1B079000E39CA /* CCTweenFunction.cpp */; }; - 298D7F6419AC2EFD00FF096D /* UIWebViewWrapper.h in Headers */ = {isa = PBXBuildFile; fileRef = 298D7F5C19AC2EFD00FF096D /* UIWebViewWrapper.h */; }; - 298D7F6519AC2EFD00FF096D /* UIWebViewWrapper.mm in Sources */ = {isa = PBXBuildFile; fileRef = 298D7F5D19AC2EFD00FF096D /* UIWebViewWrapper.mm */; }; 298D7F6819AC2EFD00FF096D /* WebView.h in Headers */ = {isa = PBXBuildFile; fileRef = 298D7F6019AC2EFD00FF096D /* WebView.h */; }; 298D7F6919AC2EFD00FF096D /* WebView.mm in Sources */ = {isa = PBXBuildFile; fileRef = 298D7F6119AC2EFD00FF096D /* WebView.mm */; }; 298D7F6A19AC2EFD00FF096D /* WebViewImpl_iOS.h in Headers */ = {isa = PBXBuildFile; fileRef = 298D7F6219AC2EFD00FF096D /* WebViewImpl_iOS.h */; }; @@ -2306,8 +2304,6 @@ 2958244A19873D8E00F9746D /* UIScale9Sprite.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UIScale9Sprite.h; sourceTree = ""; }; 2986667818B1B079000E39CA /* CCTweenFunction.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CCTweenFunction.cpp; sourceTree = ""; }; 2986667918B1B079000E39CA /* CCTweenFunction.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCTweenFunction.h; sourceTree = ""; }; - 298D7F5C19AC2EFD00FF096D /* UIWebViewWrapper.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UIWebViewWrapper.h; sourceTree = ""; }; - 298D7F5D19AC2EFD00FF096D /* UIWebViewWrapper.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = UIWebViewWrapper.mm; sourceTree = ""; }; 298D7F6019AC2EFD00FF096D /* WebView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WebView.h; sourceTree = ""; }; 298D7F6119AC2EFD00FF096D /* WebView.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = WebView.mm; sourceTree = ""; }; 298D7F6219AC2EFD00FF096D /* WebViewImpl_iOS.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WebViewImpl_iOS.h; sourceTree = ""; }; @@ -2354,8 +2350,6 @@ 3EA3EDBB1991CDFA00645534 /* CCCamera.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CCCamera.h; path = ../base/CCCamera.h; sourceTree = ""; }; 464AD6E3197EBB1400E502D8 /* pvr.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = pvr.cpp; path = ../base/pvr.cpp; sourceTree = ""; }; 464AD6E4197EBB1400E502D8 /* pvr.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = pvr.h; path = ../base/pvr.h; sourceTree = ""; }; - 46633BC2199DDB2F00F6E838 /* CCModuleManager.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = CCModuleManager.cpp; path = ../base/CCModuleManager.cpp; sourceTree = ""; }; - 46633BC3199DDB2F00F6E838 /* CCModuleManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CCModuleManager.h; path = ../base/CCModuleManager.h; sourceTree = ""; }; 46A15FCC1807A544005B8026 /* AUTHORS */ = {isa = PBXFileReference; lastKnownFileType = text; name = AUTHORS; path = ../AUTHORS; sourceTree = ""; }; 46A15FCE1807A544005B8026 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = text; name = README.md; path = ../README.md; sourceTree = ""; }; 46A15FE11807A56F005B8026 /* Export.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Export.h; sourceTree = ""; }; @@ -3078,8 +3072,6 @@ 1A570095180BC5B00088DEC7 /* base-nodes */ = { isa = PBXGroup; children = ( - 46633BC2199DDB2F00F6E838 /* CCModuleManager.cpp */, - 46633BC3199DDB2F00F6E838 /* CCModuleManager.h */, 15EFA20F198A2BB5000C57D3 /* CCProtectedNode.cpp */, 15EFA210198A2BB5000C57D3 /* CCProtectedNode.h */, 1A57009C180BC5D20088DEC7 /* CCNode.cpp */, @@ -4028,8 +4020,6 @@ 29CB8F521929D65500C841D6 /* experimental */ = { isa = PBXGroup; children = ( - 298D7F5C19AC2EFD00FF096D /* UIWebViewWrapper.h */, - 298D7F5D19AC2EFD00FF096D /* UIWebViewWrapper.mm */, 298D7F6019AC2EFD00FF096D /* WebView.h */, 298D7F6119AC2EFD00FF096D /* WebView.mm */, 298D7F6219AC2EFD00FF096D /* WebViewImpl_iOS.h */, @@ -5472,7 +5462,6 @@ 46A171061807CECB005B8026 /* CCPhysicsWorld.h in Headers */, 50ABBDB41925AB4100A911A9 /* ccShaders.h in Headers */, 15AE19B319AAD39700C27E9E /* SliderReader.h in Headers */, - 298D7F6419AC2EFD00FF096D /* UIWebViewWrapper.h in Headers */, 15AE1B7919AADA9A00C27E9E /* UIRichText.h in Headers */, 15AE1A4A19AAD3D500C27E9E /* b2CircleShape.h in Headers */, 50ABBE861925AB6F00A911A9 /* ccFPSImages.h in Headers */, @@ -6693,7 +6682,6 @@ 15AE1A4319AAD3D500C27E9E /* b2DynamicTree.cpp in Sources */, 15AE1BFF19AAE01E00C27E9E /* CCScale9Sprite.cpp in Sources */, 15AE1A3A19AAD3D500C27E9E /* b2BroadPhase.cpp in Sources */, - 298D7F6519AC2EFD00FF096D /* UIWebViewWrapper.mm in Sources */, 15AE19B619AAD39700C27E9E /* TextBMFontReader.cpp in Sources */, 15AE1BFD19AAE01E00C27E9E /* CCInvocation.cpp in Sources */, B24AA98A195A675C007B4522 /* CCFastTMXTiledMap.cpp in Sources */, diff --git a/cocos/ui/UIWebViewWrapper.h b/cocos/ui/UIWebViewWrapper.h deleted file mode 100644 index 2311f24d4527..000000000000 --- a/cocos/ui/UIWebViewWrapper.h +++ /dev/null @@ -1,47 +0,0 @@ -// -// Created by gin0606 on 2014/07/29. -// - - -#include -#include -#include - - -@interface UIWebViewWrapper : NSObject -@property (nonatomic) std::function shouldStartLoading; -@property (nonatomic) std::function didFinishLoading; -@property (nonatomic) std::function didFailLoading; -@property (nonatomic) std::function onJsCallback; - -@property(nonatomic, readonly, getter=canGoBack) BOOL canGoBack; -@property(nonatomic, readonly, getter=canGoForward) BOOL canGoForward; - -+ (instancetype)webViewWrapper; - -- (void)setVisible:(bool)visible; - -- (void)setFrameWithX:(float)x y:(float)y width:(float)width height:(float)height; - -- (void)setJavascriptInterfaceScheme:(const std::string &)scheme; - -- (void)loadData:(const std::string &)data MIMEType:(const std::string &)MIMEType textEncodingName:(const std::string &)encodingName baseURL:(const std::string &)baseURL; - -- (void)loadHTMLString:(const std::string &)string baseURL:(const std::string &)baseURL; - -- (void)loadUrl:(const std::string &)urlString; - -- (void)loadFile:(const std::string &)filePath; - -- (void)stopLoading; - -- (void)reload; - -- (void)evaluateJS:(const std::string &)js; - -- (void)goBack; - -- (void)goForward; - -- (void)setScalesPageToFit:(const bool)scalesPageToFit; -@end diff --git a/cocos/ui/UIWebViewWrapper.mm b/cocos/ui/UIWebViewWrapper.mm deleted file mode 100644 index f5083dbeac7d..000000000000 --- a/cocos/ui/UIWebViewWrapper.mm +++ /dev/null @@ -1,158 +0,0 @@ -// -// Created by gin0606 on 2014/07/29. -// - - -#import "UIWebViewWrapper.h" -#import "WebView.h" -#import "CCGLView.h" -#import "CCEAGLView.h" -#import "CCDirector.h" - -using namespace cocos2d::experimental::ui; - - -@interface UIWebViewWrapper () -@property(nonatomic, retain) UIWebView *uiWebView; -@property(nonatomic, copy) NSString *jsScheme; -@end - -@implementation UIWebViewWrapper { - -} - -+ (instancetype)webViewWrapper { - return [[[self alloc] init] autorelease]; -} - -- (instancetype)init { - self = [super init]; - if (self) { - self.uiWebView = nil; - self.shouldStartLoading = nullptr; - self.didFinishLoading = nullptr; - self.didFailLoading = nullptr; - } - return self; -} - -- (void)dealloc { - [self.uiWebView removeFromSuperview]; - self.jsScheme = nil; - [super dealloc]; -} - -- (void)setupWebView { - if (!self.uiWebView) { - self.uiWebView = [[[UIWebView alloc] init] autorelease]; - self.uiWebView.delegate = self; - } - if (!self.uiWebView.superview) { - auto view = cocos2d::Director::getInstance()->getOpenGLView(); - auto eaglview = (CCEAGLView *) view->getEAGLView(); - [eaglview addSubview:self.uiWebView]; - } -} - -- (void)setVisible:(bool)visible { - self.uiWebView.hidden = !visible; -} - -- (void)setFrameWithX:(float)x y:(float)y width:(float)width height:(float)height { - if (!self.uiWebView) {[self setupWebView];} - CGRect newFrame = CGRectMake(x, y, width, height); - if (!CGRectEqualToRect(self.uiWebView.frame, newFrame)) { - self.uiWebView.frame = CGRectMake(x, y, width, height); - } -} - -- (void)setJavascriptInterfaceScheme:(const std::string &)scheme { - self.jsScheme = @(scheme.c_str()); -} - -- (void)loadData:(const std::string &)data MIMEType:(const std::string &)MIMEType textEncodingName:(const std::string &)encodingName baseURL:(const std::string &)baseURL { - [self.uiWebView loadData:[NSData dataWithBytes:data.c_str() length:data.length()] - MIMEType:@(MIMEType.c_str()) - textEncodingName:@(encodingName.c_str()) - baseURL:[NSURL URLWithString:@(baseURL.c_str())]]; -} - -- (void)loadHTMLString:(const std::string &)string baseURL:(const std::string &)baseURL { - [self.uiWebView loadHTMLString:@(string.c_str()) baseURL:[NSURL URLWithString:@(baseURL.c_str())]]; -} - -- (void)loadUrl:(const std::string &)urlString { - if (!self.uiWebView) {[self setupWebView];} - NSURL *url = [NSURL URLWithString:@(urlString.c_str())]; - NSURLRequest *request = [NSURLRequest requestWithURL:url]; - [self.uiWebView loadRequest:request]; -} - -- (void)loadFile:(const std::string &)filePath { - if (!self.uiWebView) {[self setupWebView];} - NSURL *url = [NSURL fileURLWithPath:@(filePath.c_str())]; - NSURLRequest *request = [NSURLRequest requestWithURL:url]; - [self.uiWebView loadRequest:request]; -} - -- (void)stopLoading { - [self.uiWebView stopLoading]; -} - -- (void)reload { - [self.uiWebView reload]; -} - -- (BOOL)canGoForward { - return self.uiWebView.canGoForward; -} - -- (BOOL)canGoBack { - return self.uiWebView.canGoBack; -} - -- (void)goBack { - [self.uiWebView goBack]; -} - -- (void)goForward { - [self.uiWebView goForward]; -} - -- (void)evaluateJS:(const std::string &)js { - if (!self.uiWebView) {[self setupWebView];} - [self.uiWebView stringByEvaluatingJavaScriptFromString:@(js.c_str())]; -} - -- (void)setScalesPageToFit:(const bool)scalesPageToFit { - self.uiWebView.scalesPageToFit = scalesPageToFit; -} - -#pragma mark - UIWebViewDelegate -- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { - NSString *url = [[request URL] absoluteString]; - if ([[[request URL] scheme] isEqualToString:self.jsScheme]) { - self.onJsCallback([url UTF8String]); - return NO; - } - if (self.shouldStartLoading) { - return self.shouldStartLoading([url UTF8String]); - } - return YES; -} - -- (void)webViewDidFinishLoad:(UIWebView *)webView { - if (self.didFinishLoading) { - NSString *url = [[webView.request URL] absoluteString]; - self.didFinishLoading([url UTF8String]); - } -} - -- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { - if (self.didFailLoading) { - NSString *url = error.userInfo[NSURLErrorFailingURLStringErrorKey]; - self.didFailLoading([url UTF8String]); - } -} - -@end diff --git a/cocos/ui/WebView.h b/cocos/ui/WebView.h index 325a1276d8db..e0977d2e858b 100644 --- a/cocos/ui/WebView.h +++ b/cocos/ui/WebView.h @@ -1,6 +1,26 @@ -// -// Created by gin0606 on 2014/07/29. -// +/**************************************************************************** + Copyright (c) 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. + ****************************************************************************/ #ifndef __Cocos2d_Plugin_WebView_H_ #define __Cocos2d_Plugin_WebView_H_ @@ -72,7 +92,11 @@ class WebView : public cocos2d::ui::Widget { * @param encoding the encoding of the data. * @param baseURL The base URL for the content. */ - void loadData(const cocos2d::Data &data, const std::string &MIMEType, const std::string &encoding, const std::string &baseURL); + void loadData(const cocos2d::Data &data, + const std::string &MIMEType, + const std::string &encoding, + const std::string &baseURL); + /** * Sets the main page content and base URL. @@ -143,8 +167,8 @@ class WebView : public cocos2d::ui::Widget { WebViewImpl *_impl; }; -} // namespace cocos2d -} // namespace experimental + } // namespace cocos2d + } // namespace experimental }//namespace ui #endif diff --git a/cocos/ui/WebView.mm b/cocos/ui/WebView.mm index b670bc2b8893..3be09fdb805f 100644 --- a/cocos/ui/WebView.mm +++ b/cocos/ui/WebView.mm @@ -1,95 +1,144 @@ -// -// Created by gin0606 on 2014/07/29. -// +/**************************************************************************** + Copyright (c) 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. + ****************************************************************************/ +#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS) -#include "WebViewImpl_iOS.h" #include "WebView.h" #include "platform/CCGLView.h" #include "base/CCDirector.h" #include "platform/CCFileUtils.h" +#include "WebViewImpl_iOS.h" + + NS_CC_BEGIN namespace experimental{ namespace ui{ - WebView::WebView() : _impl(new WebViewImpl(this)) { + WebView::WebView() + : _impl(new WebViewImpl(this)) + { } - WebView::~WebView() { - delete _impl; + WebView::~WebView() + { + CC_SAFE_DELETE(_impl); } - WebView *WebView::create() { - auto pRet = new WebView(); - if (pRet->init()) { - pRet->autorelease(); - return pRet; + WebView *WebView::create() + { + auto webView = new(std::nothrow) WebView(); + if (webView && webView->init()) + { + webView->autorelease(); + return webView; } + CC_SAFE_DELETE(webView); return nullptr; } - void WebView::setJavascriptInterfaceScheme(const std::string &scheme) { + void WebView::setJavascriptInterfaceScheme(const std::string &scheme) + { _impl->setJavascriptInterfaceScheme(scheme); } - void WebView::loadData(const cocos2d::Data &data, const std::string &MIMEType, const std::string &encoding, const std::string &baseURL) { + void WebView::loadData(const cocos2d::Data &data, + const std::string &MIMEType, + const std::string &encoding, + const std::string &baseURL) + { _impl->loadData(data, MIMEType, encoding, baseURL); } - void WebView::loadHTMLString(const std::string &string, const std::string &baseURL) { + void WebView::loadHTMLString(const std::string &string, const std::string &baseURL) + { _impl->loadHTMLString(string, baseURL); } - void WebView::loadUrl(const std::string &url) { + void WebView::loadUrl(const std::string &url) + { _impl->loadUrl(url); } - void WebView::loadFile(const std::string &fileName) { + void WebView::loadFile(const std::string &fileName) + { _impl->loadFile(fileName); } - void WebView::stopLoading() { + void WebView::stopLoading() + { _impl->stopLoading(); } - void WebView::reload() { + void WebView::reload() + { _impl->reload(); } - bool WebView::canGoBack() { + bool WebView::canGoBack() + { return _impl->canGoBack(); } - bool WebView::canGoForward() { + bool WebView::canGoForward() + { return _impl->canGoForward(); } - void WebView::goBack() { + void WebView::goBack() + { _impl->goBack(); } - void WebView::goForward() { + void WebView::goForward() + { _impl->goForward(); } - void WebView::evaluateJS(const std::string &js) { + void WebView::evaluateJS(const std::string &js) + { _impl->evaluateJS(js); } - void WebView::setScalesPageToFit(bool const scalesPageToFit) { + void WebView::setScalesPageToFit(bool const scalesPageToFit) + { _impl->setScalesPageToFit(scalesPageToFit); } - void WebView::draw(cocos2d::Renderer *renderer, cocos2d::Mat4 const &transform, uint32_t flags) { + void WebView::draw(cocos2d::Renderer *renderer, cocos2d::Mat4 const &transform, uint32_t flags) + { cocos2d::ui::Widget::draw(renderer, transform, flags); _impl->draw(renderer, transform, flags); } - void WebView::setVisible(bool visible) { + void WebView::setVisible(bool visible) + { Node::setVisible(visible); _impl->setVisible(visible); } } // namespace cocos2d } // namespace ui -} //namespace experimental \ No newline at end of file +} //namespace experimental + +#endif \ No newline at end of file diff --git a/cocos/ui/WebViewImpl_iOS.h b/cocos/ui/WebViewImpl_iOS.h index 04dce9039e4b..fb371ed424de 100644 --- a/cocos/ui/WebViewImpl_iOS.h +++ b/cocos/ui/WebViewImpl_iOS.h @@ -1,6 +1,26 @@ -// -// Created by gin0606 on 2014/07/30. -// +/**************************************************************************** + Copyright (c) 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. + ****************************************************************************/ #ifndef __cocos2d_plugin_WebViewImpl_IOS_H_ #define __cocos2d_plugin_WebViewImpl_IOS_H_ @@ -28,7 +48,10 @@ class WebViewImpl { void setJavascriptInterfaceScheme(const std::string &scheme); - void loadData(const cocos2d::Data &data, const std::string &MIMEType, const std::string &encoding, const std::string &baseURL); + void loadData(const cocos2d::Data &data, + const std::string &MIMEType, + const std::string &encoding, + const std::string &baseURL); void loadHTMLString(const std::string &string, const std::string &baseURL); @@ -61,8 +84,8 @@ class WebViewImpl { WebView *_webView; }; -} // namespace cocos2d -} // namespace experimental + } // namespace cocos2d + } // namespace experimental }//namespace ui #endif //__cocos2d_plugin_WebViewImpl_IOS_H_ diff --git a/cocos/ui/WebViewImpl_iOS.mm b/cocos/ui/WebViewImpl_iOS.mm index d26393581d1b..22652b2e916a 100644 --- a/cocos/ui/WebViewImpl_iOS.mm +++ b/cocos/ui/WebViewImpl_iOS.mm @@ -1,6 +1,26 @@ -// -// Created by gin0606 on 2014/07/30. -// +/**************************************************************************** + Copyright (c) 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. + ****************************************************************************/ #if CC_TARGET_PLATFORM == CC_PLATFORM_IOS @@ -10,18 +30,203 @@ #include "CCGLView.h" #include "CCEAGLView.h" #include "platform/CCFileUtils.h" - -#import "UIWebViewWrapper.h" #include "ui/WebView.h" +@interface UIWebViewWrapper : NSObject +@property (nonatomic) std::function shouldStartLoading; +@property (nonatomic) std::function didFinishLoading; +@property (nonatomic) std::function didFailLoading; +@property (nonatomic) std::function onJsCallback; + +@property(nonatomic, readonly, getter=canGoBack) BOOL canGoBack; +@property(nonatomic, readonly, getter=canGoForward) BOOL canGoForward; + ++ (instancetype)webViewWrapper; + +- (void)setVisible:(bool)visible; + +- (void)setFrameWithX:(float)x y:(float)y width:(float)width height:(float)height; + +- (void)setJavascriptInterfaceScheme:(const std::string &)scheme; + +- (void)loadData:(const std::string &)data MIMEType:(const std::string &)MIMEType textEncodingName:(const std::string &)encodingName baseURL:(const std::string &)baseURL; + +- (void)loadHTMLString:(const std::string &)string baseURL:(const std::string &)baseURL; + +- (void)loadUrl:(const std::string &)urlString; + +- (void)loadFile:(const std::string &)filePath; + +- (void)stopLoading; + +- (void)reload; + +- (void)evaluateJS:(const std::string &)js; + +- (void)goBack; + +- (void)goForward; + +- (void)setScalesPageToFit:(const bool)scalesPageToFit; +@end + + +@interface UIWebViewWrapper () +@property(nonatomic, retain) UIWebView *uiWebView; +@property(nonatomic, copy) NSString *jsScheme; +@end + +@implementation UIWebViewWrapper { + +} + ++ (instancetype)webViewWrapper { + return [[[self alloc] init] autorelease]; +} + +- (instancetype)init { + self = [super init]; + if (self) { + self.uiWebView = nil; + self.shouldStartLoading = nullptr; + self.didFinishLoading = nullptr; + self.didFailLoading = nullptr; + } + return self; +} + +- (void)dealloc { + [self.uiWebView removeFromSuperview]; + self.jsScheme = nil; + [super dealloc]; +} + +- (void)setupWebView { + if (!self.uiWebView) { + self.uiWebView = [[[UIWebView alloc] init] autorelease]; + self.uiWebView.delegate = self; + } + if (!self.uiWebView.superview) { + auto view = cocos2d::Director::getInstance()->getOpenGLView(); + auto eaglview = (CCEAGLView *) view->getEAGLView(); + [eaglview addSubview:self.uiWebView]; + } +} + +- (void)setVisible:(bool)visible { + self.uiWebView.hidden = !visible; +} + +- (void)setFrameWithX:(float)x y:(float)y width:(float)width height:(float)height { + if (!self.uiWebView) {[self setupWebView];} + CGRect newFrame = CGRectMake(x, y, width, height); + if (!CGRectEqualToRect(self.uiWebView.frame, newFrame)) { + self.uiWebView.frame = CGRectMake(x, y, width, height); + } +} + +- (void)setJavascriptInterfaceScheme:(const std::string &)scheme { + self.jsScheme = @(scheme.c_str()); +} + +- (void)loadData:(const std::string &)data MIMEType:(const std::string &)MIMEType textEncodingName:(const std::string &)encodingName baseURL:(const std::string &)baseURL { + [self.uiWebView loadData:[NSData dataWithBytes:data.c_str() length:data.length()] + MIMEType:@(MIMEType.c_str()) + textEncodingName:@(encodingName.c_str()) + baseURL:[NSURL URLWithString:@(baseURL.c_str())]]; +} + +- (void)loadHTMLString:(const std::string &)string baseURL:(const std::string &)baseURL { + [self.uiWebView loadHTMLString:@(string.c_str()) baseURL:[NSURL URLWithString:@(baseURL.c_str())]]; +} + +- (void)loadUrl:(const std::string &)urlString { + if (!self.uiWebView) {[self setupWebView];} + NSURL *url = [NSURL URLWithString:@(urlString.c_str())]; + NSURLRequest *request = [NSURLRequest requestWithURL:url]; + [self.uiWebView loadRequest:request]; +} + +- (void)loadFile:(const std::string &)filePath { + if (!self.uiWebView) {[self setupWebView];} + NSURL *url = [NSURL fileURLWithPath:@(filePath.c_str())]; + NSURLRequest *request = [NSURLRequest requestWithURL:url]; + [self.uiWebView loadRequest:request]; +} + +- (void)stopLoading { + [self.uiWebView stopLoading]; +} + +- (void)reload { + [self.uiWebView reload]; +} + +- (BOOL)canGoForward { + return self.uiWebView.canGoForward; +} + +- (BOOL)canGoBack { + return self.uiWebView.canGoBack; +} + +- (void)goBack { + [self.uiWebView goBack]; +} + +- (void)goForward { + [self.uiWebView goForward]; +} + +- (void)evaluateJS:(const std::string &)js { + if (!self.uiWebView) {[self setupWebView];} + [self.uiWebView stringByEvaluatingJavaScriptFromString:@(js.c_str())]; +} + +- (void)setScalesPageToFit:(const bool)scalesPageToFit { + self.uiWebView.scalesPageToFit = scalesPageToFit; +} + +#pragma mark - UIWebViewDelegate +- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { + NSString *url = [[request URL] absoluteString]; + if ([[[request URL] scheme] isEqualToString:self.jsScheme]) { + self.onJsCallback([url UTF8String]); + return NO; + } + if (self.shouldStartLoading) { + return self.shouldStartLoading([url UTF8String]); + } + return YES; +} + +- (void)webViewDidFinishLoad:(UIWebView *)webView { + if (self.didFinishLoading) { + NSString *url = [[webView.request URL] absoluteString]; + self.didFinishLoading([url UTF8String]); + } +} + +- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { + if (self.didFailLoading) { + NSString *url = error.userInfo[NSURLErrorFailingURLStringErrorKey]; + self.didFailLoading([url UTF8String]); + } +} + +@end + + namespace cocos2d { namespace experimental { namespace ui{ WebViewImpl::WebViewImpl(WebView *webView) - : _uiWebViewWrapper([UIWebViewWrapper webViewWrapper]), _webView(webView) { + : _uiWebViewWrapper([UIWebViewWrapper webViewWrapper]), + _webView(webView) { [_uiWebViewWrapper retain]; + _uiWebViewWrapper.shouldStartLoading = [this](std::string url) { if (this->_webView->shouldStartLoading) { return this->_webView->shouldStartLoading(this->_webView, url); @@ -45,7 +250,7 @@ }; } -WebViewImpl::~WebViewImpl() { +WebViewImpl::~WebViewImpl(){ [_uiWebViewWrapper release]; _uiWebViewWrapper = nullptr; } @@ -54,7 +259,11 @@ [_uiWebViewWrapper setJavascriptInterfaceScheme:scheme]; } -void WebViewImpl::loadData(const Data &data, const std::string &MIMEType, const std::string &encoding, const std::string &baseURL) { +void WebViewImpl::loadData(const Data &data, + const std::string &MIMEType, + const std::string &encoding, + const std::string &baseURL) { + std::string dataString(reinterpret_cast(data.getBytes()), static_cast(data.getSize())); [_uiWebViewWrapper loadData:dataString MIMEType:MIMEType textEncodingName:encoding baseURL:baseURL]; } @@ -106,9 +315,11 @@ void WebViewImpl::draw(cocos2d::Renderer *renderer, cocos2d::Mat4 const &transform, uint32_t flags) { if (flags & cocos2d::Node::FLAGS_TRANSFORM_DIRTY) { + auto direcrot = cocos2d::Director::getInstance(); auto glView = direcrot->getOpenGLView(); auto frameSize = glView->getFrameSize(); + auto scaleFactor = [static_cast(glView->getEAGLView()) contentScaleFactor]; auto winSize = direcrot->getWinSize(); @@ -128,11 +339,11 @@ } } -void WebViewImpl::setVisible(bool visible) { +void WebViewImpl::setVisible(bool visible){ [_uiWebViewWrapper setVisible:visible]; } -} // namespace cocos2d + } // namespace cocos2d } // namespace experimental } //namespace ui diff --git a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIWebViewTest/UIWebViewTest.cpp b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIWebViewTest/UIWebViewTest.cpp index 847bbdcb9237..2dd49d058f3a 100644 --- a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIWebViewTest/UIWebViewTest.cpp +++ b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIWebViewTest/UIWebViewTest.cpp @@ -31,6 +31,7 @@ bool WebViewTest::init() _webView->setPosition(Director::getInstance()->getVisibleSize()/2); _webView->setContentSize(Director::getInstance()->getVisibleSize() * 0.5); _webView->loadUrl("http://www.baidu.com"); + _webView->setScalesPageToFit(true); this->addChild(_webView); return true; From 631e1a70c44f293ab2c3d59757ef81a9710728ed Mon Sep 17 00:00:00 2001 From: andyque Date: Tue, 26 Aug 2014 14:26:37 +0800 Subject: [PATCH 06/14] add WebView-inl.h and refactor Android code --- .../src/org/cocos2dx/lib/Cocos2dxWebView.java | 92 +++++ .../cocos2dx/lib/Cocos2dxWebViewHelper.java | 296 +++++++++++++++ cocos/ui/Android.mk | 1 - cocos/ui/WebView.cpp | 32 +- cocos/ui/WebView.mm | 118 +----- cocos/ui/WebViewImpl_android.cpp | 354 +++++++++++------- cocos/ui/WebViewImpl_android.h | 104 +++-- cocos/ui/Webview-inl.h | 141 +++++++ ...org_cocos2dx_lib_Cocos2dxWebViewHelper.cpp | 35 -- .../org_cocos2dx_lib_Cocos2dxWebViewHelper.h | 59 --- 10 files changed, 840 insertions(+), 392 deletions(-) create mode 100755 cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxWebView.java create mode 100755 cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxWebViewHelper.java create mode 100644 cocos/ui/Webview-inl.h delete mode 100644 cocos/ui/org_cocos2dx_lib_Cocos2dxWebViewHelper.cpp delete mode 100644 cocos/ui/org_cocos2dx_lib_Cocos2dxWebViewHelper.h diff --git a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxWebView.java b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxWebView.java new file mode 100755 index 000000000000..362cc4e6c435 --- /dev/null +++ b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxWebView.java @@ -0,0 +1,92 @@ +package org.cocos2dx.lib; + +import android.annotation.SuppressLint; +import android.content.Context; +import android.util.Log; +import android.webkit.WebView; +import android.webkit.WebViewClient; +import android.widget.FrameLayout; + +import java.lang.reflect.Method; +import java.net.URI; + +public class Cocos2dxWebView extends WebView { + private static final String TAG = Cocos2dxWebViewHelper.class.getSimpleName(); + + private int viewTag; + private String jsScheme; + + public Cocos2dxWebView(Context context) { + this(context, -1); + } + + @SuppressLint("SetJavaScriptEnabled") + public Cocos2dxWebView(Context context, int viewTag) { + super(context); + this.viewTag = viewTag; + this.jsScheme = ""; + + this.setFocusable(true); + this.setFocusableInTouchMode(true); + + this.getSettings().setSupportZoom(false); + + this.getSettings().setJavaScriptEnabled(true); + + // `searchBoxJavaBridge_` has big security risk. http://jvn.jp/en/jp/JVN53768697 + try { + Method method = this.getClass().getMethod("removeJavascriptInterface", new Class[]{String.class}); + method.invoke(this, "searchBoxJavaBridge_"); + } catch (ReflectiveOperationException e) { + Log.d(TAG, "This API level do not support `removeJavascriptInterface`"); + } + + this.setWebViewClient(new Cocos2dxWebViewClient()); + } + + public void setJavascriptInterfaceScheme(String scheme) { + this.jsScheme = scheme != null ? scheme : ""; + } + + public void setScalesPageToFit(boolean scalesPageToFit) { + this.getSettings().setSupportZoom(scalesPageToFit); + } + + class Cocos2dxWebViewClient extends WebViewClient { + @Override + public boolean shouldOverrideUrlLoading(WebView view, String urlString) { + URI uri = URI.create(urlString); + if (uri != null && uri.getScheme().equals(jsScheme)) { + Cocos2dxWebViewHelper._onJsCallback(viewTag, urlString); + return true; + } + return Cocos2dxWebViewHelper._shouldStartLoading(viewTag, urlString); + } + + @Override + public void onPageFinished(WebView view, String url) { + super.onPageFinished(view, url); + Cocos2dxWebViewHelper._didFinishLoading(viewTag, url); + } + + @Override + public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { + super.onReceivedError(view, errorCode, description, failingUrl); + Cocos2dxWebViewHelper._didFailLoading(viewTag, failingUrl); + } + } + + public void setWebViewRect(int left, int top, int maxWidth, int maxHeight) { + fixSize(left, top, maxWidth, maxHeight); + } + + private void fixSize(int left, int top, int width, int height) { + FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, + FrameLayout.LayoutParams.WRAP_CONTENT); + layoutParams.leftMargin = left; + layoutParams.topMargin = top; + layoutParams.width = width; + layoutParams.height = height; + this.setLayoutParams(layoutParams); + } +} diff --git a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxWebViewHelper.java b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxWebViewHelper.java new file mode 100755 index 000000000000..e8d45b787e44 --- /dev/null +++ b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxWebViewHelper.java @@ -0,0 +1,296 @@ +package org.cocos2dx.lib; + +import android.os.Handler; +import android.os.Looper; +import android.util.SparseArray; +import android.view.View; +import android.widget.FrameLayout; + +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.FutureTask; + + +public class Cocos2dxWebViewHelper { + private static final String TAG = Cocos2dxWebViewHelper.class.getSimpleName(); + private static Handler handler; + private static Cocos2dxActivity cocos2dxActivity; + private static FrameLayout layout; + + private static SparseArray webViews; + private static int viewTag = 0; + + public Cocos2dxWebViewHelper(FrameLayout layout) { + Cocos2dxWebViewHelper.layout = layout; + Cocos2dxWebViewHelper.handler = new Handler(Looper.myLooper()); + + Cocos2dxWebViewHelper.cocos2dxActivity = (Cocos2dxActivity) Cocos2dxActivity.getContext(); + Cocos2dxWebViewHelper.webViews = new SparseArray(); + } + + private static native boolean shouldStartLoading(int index, String message); + + public static boolean _shouldStartLoading(int index, String message) { + return !shouldStartLoading(index, message); + } + + private static native void didFinishLoading(int index, String message); + + public static void _didFinishLoading(int index, String message) { + didFinishLoading(index, message); + } + + private static native void didFailLoading(int index, String message); + + public static void _didFailLoading(int index, String message) { + didFailLoading(index, message); + } + + private static native void onJsCallback(int index, String message); + + public static void _onJsCallback(int index, String message) { + onJsCallback(index, message); + } + + @SuppressWarnings("unused") + public static int createWebView() { + final int index = viewTag; + cocos2dxActivity.runOnUiThread(new Runnable() { + @Override + public void run() { + Cocos2dxWebView webView = new Cocos2dxWebView(cocos2dxActivity, index); + FrameLayout.LayoutParams lParams = new FrameLayout.LayoutParams( + FrameLayout.LayoutParams.WRAP_CONTENT, + FrameLayout.LayoutParams.WRAP_CONTENT); + layout.addView(webView, lParams); + + webViews.put(index, webView); + } + }); + return viewTag++; + } + + @SuppressWarnings("unused") + public static void removeWebView(final int index) { + cocos2dxActivity.runOnUiThread(new Runnable() { + @Override + public void run() { + Cocos2dxWebView webView = webViews.get(index); + if (webView != null) { + webViews.remove(index); + layout.removeView(webView); + } + } + }); + } + + @SuppressWarnings("unused") + public static void setVisible(final int index, final boolean visible) { + cocos2dxActivity.runOnUiThread(new Runnable() { + @Override + public void run() { + Cocos2dxWebView webView = webViews.get(index); + if (webView != null) { + webView.setVisibility(visible ? View.VISIBLE : View.GONE); + } + } + }); + } + + @SuppressWarnings("unused") + public static void setWebViewRect(final int index, final int left, final int top, final int maxWidth, final int maxHeight) { + cocos2dxActivity.runOnUiThread(new Runnable() { + @Override + public void run() { + Cocos2dxWebView webView = webViews.get(index); + if (webView != null) { + webView.setWebViewRect(left, top, maxWidth, maxHeight); + } + } + }); + } + + @SuppressWarnings("unused") + public static void setJavascriptInterfaceScheme(final int index, final String scheme) { + cocos2dxActivity.runOnUiThread(new Runnable() { + @Override + public void run() { + Cocos2dxWebView webView = webViews.get(index); + if (webView != null) { + webView.setJavascriptInterfaceScheme(scheme); + } + } + }); + } + + @SuppressWarnings("unused") + public static void loadData(final int index, final String data, final String mimeType, final String encoding, final String baseURL) { + cocos2dxActivity.runOnUiThread(new Runnable() { + @Override + public void run() { + Cocos2dxWebView webView = webViews.get(index); + if (webView != null) { + webView.loadDataWithBaseURL(baseURL, data, mimeType, encoding, null); + } + } + }); + } + + @SuppressWarnings("unused") + public static void loadHTMLString(final int index, final String htmlString, final String mimeType, final String encoding) { + cocos2dxActivity.runOnUiThread(new Runnable() { + @Override + public void run() { + Cocos2dxWebView webView = webViews.get(index); + if (webView != null) { + webView.loadData(htmlString, mimeType, encoding); + } + } + }); + } + + @SuppressWarnings("unused") + public static void loadUrl(final int index, final String url) { + cocos2dxActivity.runOnUiThread(new Runnable() { + @Override + public void run() { + Cocos2dxWebView webView = webViews.get(index); + if (webView != null) { + webView.loadUrl(url); + } + } + }); + } + + @SuppressWarnings("unused") + public static void loadFile(final int index, final String filePath) { + cocos2dxActivity.runOnUiThread(new Runnable() { + @Override + public void run() { + Cocos2dxWebView webView = webViews.get(index); + if (webView != null) { + webView.loadUrl(filePath); + } + } + }); + } + + public static void stopLoading(final int index) { + cocos2dxActivity.runOnUiThread(new Runnable() { + @Override + public void run() { + Cocos2dxWebView webView = webViews.get(index); + if (webView != null) { + webView.stopLoading(); + } + } + }); + + } + + public static void reload(final int index) { + cocos2dxActivity.runOnUiThread(new Runnable() { + @Override + public void run() { + Cocos2dxWebView webView = webViews.get(index); + if (webView != null) { + webView.reload(); + } + } + }); + } + + public static T callInMainThread(Callable call) throws ExecutionException, InterruptedException { + FutureTask task = new FutureTask(call); + handler.post(task); + return task.get(); + } + + @SuppressWarnings("unused") + public static boolean canGoBack(final int index) { + Callable callable = new Callable() { + @Override + public Boolean call() throws Exception { + Cocos2dxWebView webView = webViews.get(index); + return webView != null && webView.canGoBack(); + } + }; + try { + return callInMainThread(callable); + } catch (ExecutionException e) { + return false; + } catch (InterruptedException e) { + return false; + } + } + + @SuppressWarnings("unused") + public static boolean canGoForward(final int index) { + Callable callable = new Callable() { + @Override + public Boolean call() throws Exception { + Cocos2dxWebView webView = webViews.get(index); + return webView != null && webView.canGoForward(); + } + }; + try { + return callInMainThread(callable); + } catch (ExecutionException e) { + return false; + } catch (InterruptedException e) { + return false; + } + } + + @SuppressWarnings("unused") + public static void goBack(final int index) { + cocos2dxActivity.runOnUiThread(new Runnable() { + @Override + public void run() { + Cocos2dxWebView webView = webViews.get(index); + if (webView != null) { + webView.goBack(); + } + } + }); + } + + @SuppressWarnings("unused") + public static void goForward(final int index) { + cocos2dxActivity.runOnUiThread(new Runnable() { + @Override + public void run() { + Cocos2dxWebView webView = webViews.get(index); + if (webView != null) { + webView.goForward(); + } + } + }); + } + + @SuppressWarnings("unused") + public static void evaluateJS(final int index, final String js) { + cocos2dxActivity.runOnUiThread(new Runnable() { + @Override + public void run() { + Cocos2dxWebView webView = webViews.get(index); + if (webView != null) { + webView.loadUrl("javascript:" + js); + } + } + }); + } + + @SuppressWarnings("unused") + public static void setScalesPageToFit(final int index, final boolean scalesPageToFit) { + cocos2dxActivity.runOnUiThread(new Runnable() { + @Override + public void run() { + Cocos2dxWebView webView = webViews.get(index); + if (webView != null) { + webView.setScalesPageToFit(scalesPageToFit); + } + } + }); + } +} diff --git a/cocos/ui/Android.mk b/cocos/ui/Android.mk index 5dc94fe248fe..e2b875b23763 100644 --- a/cocos/ui/Android.mk +++ b/cocos/ui/Android.mk @@ -33,7 +33,6 @@ UIDeprecated.cpp \ UIScale9Sprite.cpp \ WebView.cpp \ WebViewImpl_android.cpp \ -org_cocos2dx_lib_Cocos2dxWebViewHelper.cpp \ LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/.. \ $(LOCAL_PATH)/../editor-support diff --git a/cocos/ui/WebView.cpp b/cocos/ui/WebView.cpp index 43772dc2c195..c8d4441d7559 100644 --- a/cocos/ui/WebView.cpp +++ b/cocos/ui/WebView.cpp @@ -1,6 +1,30 @@ -// -// Created by gin0606 on 2014/08/05. -// +/**************************************************************************** + Copyright (c) 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. + ****************************************************************************/ +#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS) -#include "WebViewImpl_iOS.h" +#include "WebViewImpl_android.h" #include "WebView-inl.h" + + +#endif diff --git a/cocos/ui/WebView.mm b/cocos/ui/WebView.mm index 3be09fdb805f..c2a6b0c9da60 100644 --- a/cocos/ui/WebView.mm +++ b/cocos/ui/WebView.mm @@ -23,122 +23,8 @@ of this software and associated documentation files (the "Software"), to deal ****************************************************************************/ #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS) - -#include "WebView.h" -#include "platform/CCGLView.h" -#include "base/CCDirector.h" -#include "platform/CCFileUtils.h" - #include "WebViewImpl_iOS.h" +#include "WebView-inl.h" -NS_CC_BEGIN -namespace experimental{ - namespace ui{ - - WebView::WebView() - : _impl(new WebViewImpl(this)) - { - } - - WebView::~WebView() - { - CC_SAFE_DELETE(_impl); - } - - WebView *WebView::create() - { - auto webView = new(std::nothrow) WebView(); - if (webView && webView->init()) - { - webView->autorelease(); - return webView; - } - CC_SAFE_DELETE(webView); - return nullptr; - } - - void WebView::setJavascriptInterfaceScheme(const std::string &scheme) - { - _impl->setJavascriptInterfaceScheme(scheme); - } - - void WebView::loadData(const cocos2d::Data &data, - const std::string &MIMEType, - const std::string &encoding, - const std::string &baseURL) - { - _impl->loadData(data, MIMEType, encoding, baseURL); - } - - void WebView::loadHTMLString(const std::string &string, const std::string &baseURL) - { - _impl->loadHTMLString(string, baseURL); - } - - void WebView::loadUrl(const std::string &url) - { - _impl->loadUrl(url); - } - - void WebView::loadFile(const std::string &fileName) - { - _impl->loadFile(fileName); - } - - void WebView::stopLoading() - { - _impl->stopLoading(); - } - - void WebView::reload() - { - _impl->reload(); - } - - bool WebView::canGoBack() - { - return _impl->canGoBack(); - } - - bool WebView::canGoForward() - { - return _impl->canGoForward(); - } - - void WebView::goBack() - { - _impl->goBack(); - } - - void WebView::goForward() - { - _impl->goForward(); - } - - void WebView::evaluateJS(const std::string &js) - { - _impl->evaluateJS(js); - } - - void WebView::setScalesPageToFit(bool const scalesPageToFit) - { - _impl->setScalesPageToFit(scalesPageToFit); - } - - void WebView::draw(cocos2d::Renderer *renderer, cocos2d::Mat4 const &transform, uint32_t flags) - { - cocos2d::ui::Widget::draw(renderer, transform, flags); - _impl->draw(renderer, transform, flags); - } - - void WebView::setVisible(bool visible) - { - Node::setVisible(visible); - _impl->setVisible(visible); - } - } // namespace cocos2d -} // namespace ui -} //namespace experimental - -#endif \ No newline at end of file +#endif diff --git a/cocos/ui/WebViewImpl_android.cpp b/cocos/ui/WebViewImpl_android.cpp index 261db76781eb..420709016966 100644 --- a/cocos/ui/WebViewImpl_android.cpp +++ b/cocos/ui/WebViewImpl_android.cpp @@ -1,19 +1,94 @@ -// -// Created by gin0606 on 2014/07/30. -// +/**************************************************************************** + Copyright (c) 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. + ****************************************************************************/ #include "WebViewImpl_android.h" + +#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) #include "WebView.h" -#include "org_cocos2dx_lib_Cocos2dxWebViewHelper.h" #include "jni/JniHelper.h" +#include #include "platform/CCGLView.h" #include "base/CCDirector.h" #include "platform/CCFileUtils.h" #include +#include +#include +#include #define CLASS_NAME "org/cocos2dx/lib/Cocos2dxWebViewHelper" +extern "C"{ + /* + * Class: org_cocos2dx_lib_Cocos2dxWebViewHelper + * Method: shouldStartLoading + * Signature: (ILjava/lang/String;)Z + */ + JNIEXPORT jboolean JNICALL Java_org_cocos2dx_lib_Cocos2dxWebViewHelper_shouldStartLoading(JNIEnv *env, jclass, jint index, jstring jurl) { + auto charUrl = env->GetStringUTFChars(jurl, NULL); + std::string url = charUrl; + env->ReleaseStringUTFChars(jurl, charUrl); + return cocos2d::experimental::ui::WebViewImpl::shouldStartLoading(index, url); + } + + /* + * Class: org_cocos2dx_lib_Cocos2dxWebViewHelper + * Method: didFinishLoading + * Signature: (ILjava/lang/String;)V + */ + JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxWebViewHelper_didFinishLoading(JNIEnv *env, jclass, jint index, jstring jurl) { + auto charUrl = env->GetStringUTFChars(jurl, NULL); + std::string url = charUrl; + env->ReleaseStringUTFChars(jurl, charUrl); + cocos2d::experimental::ui::WebViewImpl::didFinishLoading(index, url); + } + + /* + * Class: org_cocos2dx_lib_Cocos2dxWebViewHelper + * Method: didFailLoading + * Signature: (ILjava/lang/String;)V + */ + JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxWebViewHelper_didFailLoading(JNIEnv *env, jclass, jint index, jstring jurl) { + auto charUrl = env->GetStringUTFChars(jurl, NULL); + std::string url = charUrl; + env->ReleaseStringUTFChars(jurl, charUrl); + cocos2d::experimental::ui::WebViewImpl::didFailLoading(index, url); + } + + /* + * Class: org_cocos2dx_lib_Cocos2dxWebViewHelper + * Method: onJsCallback + * Signature: (ILjava/lang/String;)V + */ + JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxWebViewHelper_onJsCallback(JNIEnv *env, jclass, jint index, jstring jmessage) { + auto charMessage = env->GetStringUTFChars(jmessage, NULL); + std::string message = charMessage; + env->ReleaseStringUTFChars(jmessage, charMessage); + cocos2d::experimental::ui::WebViewImpl::onJsCallback(index, message); + } +} namespace { + int createWebViewJNI() { cocos2d::JniMethodInfo t; if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "createWebView", "()I")) { @@ -199,136 +274,141 @@ std::string getUrlStringByFileName(const std::string &fileName) { } // namespace namespace cocos2d { -namespace plugin { -static std::unordered_map s_WebViewImpls; - -WebViewImpl::WebViewImpl(WebView *webView) : _viewTag(-1), _webView(webView) { - _viewTag = createWebViewJNI(); - s_WebViewImpls[_viewTag] = this; -} - -WebViewImpl::~WebViewImpl() { - removeWebViewJNI(_viewTag); - s_WebViewImpls.erase(_viewTag); -} - -void WebViewImpl::loadData(const Data &data, const std::string &MIMEType, const std::string &encoding, const std::string &baseURL) { - std::string dataString(reinterpret_cast(data.getBytes()), static_cast(data.getSize())); - loadDataJNI(_viewTag, dataString, MIMEType, encoding, baseURL); -} - -void WebViewImpl::loadHTMLString(const std::string &string, const std::string &baseURL) { - loadHTMLStringJNI(_viewTag, string, baseURL); -} - -void WebViewImpl::loadUrl(const std::string &url) { - loadUrlJNI(_viewTag, url); -} - -void WebViewImpl::loadFile(const std::string &fileName) { - auto fullPath = getUrlStringByFileName(fileName); - loadFileJNI(_viewTag, fullPath); -} - -void WebViewImpl::stopLoading() { - stopLoadingJNI(_viewTag); -} - -void WebViewImpl::reload() { - reloadJNI(_viewTag); -} - -bool WebViewImpl::canGoBack() { - return canGoBackJNI(_viewTag); -} - -bool WebViewImpl::canGoForward() { - return canGoForwardJNI(_viewTag); -} - -void WebViewImpl::goBack() { - goBackJNI(_viewTag); -} - -void WebViewImpl::goForward() { - goForwardJNI(_viewTag); -} - -void WebViewImpl::setJavascriptInterfaceScheme(const std::string &scheme) { - setJavascriptInterfaceSchemeJNI(_viewTag, scheme); -} - -void WebViewImpl::evaluateJS(const std::string &js) { - evaluateJSJNI(_viewTag, js); -} - -void WebViewImpl::setScalesPageToFit(const bool scalesPageToFit) { - setScalesPageToFitJNI(_viewTag, scalesPageToFit); -} - -bool WebViewImpl::shouldStartLoading(const int viewTag, const std::string &url) { - auto it = s_WebViewImpls.find(viewTag); - if (it != s_WebViewImpls.end()) { - auto webView = s_WebViewImpls[viewTag]->_webView; - if (webView->shouldStartLoading) { - return webView->shouldStartLoading(webView, url); - } - } - return true; -} - -void WebViewImpl::didFinishLoading(const int viewTag, const std::string &url){ - auto it = s_WebViewImpls.find(viewTag); - if (it != s_WebViewImpls.end()) { - auto webView = s_WebViewImpls[viewTag]->_webView; - if (webView->didFinishLoading) { - webView->didFinishLoading(webView, url); - } - } -} - -void WebViewImpl::didFailLoading(const int viewTag, const std::string &url){ - auto it = s_WebViewImpls.find(viewTag); - if (it != s_WebViewImpls.end()) { - auto webView = s_WebViewImpls[viewTag]->_webView; - if (webView->didFailLoading) { - webView->didFailLoading(webView, url); - } - } -} - -void WebViewImpl::onJsCallback(const int viewTag, const std::string &message){ - auto it = s_WebViewImpls.find(viewTag); - if (it != s_WebViewImpls.end()) { - auto webView = s_WebViewImpls[viewTag]->_webView; - if (webView->onJsCallback) { - webView->onJsCallback(webView, message); - } - } -} - -void WebViewImpl::draw(cocos2d::Renderer *renderer, cocos2d::Mat4 const &transform, uint32_t flags) { - if (flags & cocos2d::Node::FLAGS_TRANSFORM_DIRTY) { - auto directorInstance = cocos2d::Director::getInstance(); - auto glView = directorInstance->getOpenGLView(); - auto frameSize = glView->getFrameSize(); - - auto winSize = directorInstance->getWinSize(); - - auto leftBottom = this->_webView->convertToWorldSpace(cocos2d::Point::ZERO); - auto rightTop = this->_webView->convertToWorldSpace(cocos2d::Point(this->_webView->getContentSize().width,this->_webView->getContentSize().height)); - - auto uiLeft = frameSize.width / 2 + (leftBottom.x - winSize.width / 2 ) * glView->getScaleX(); - auto uiTop = frameSize.height /2 - (rightTop.y - winSize.height / 2) * glView->getScaleY(); - - setWebViewRectJNI(_viewTag,uiLeft,uiTop, - (rightTop.x - leftBottom.x) * glView->getScaleX(), - (rightTop.y - leftBottom.y) * glView->getScaleY()); - } -} - -void WebViewImpl::setVisible(bool visible) { - setWebViewVisibleJNI(_viewTag, visible); -} -} // namespace cocos2d -} // namespace plugin + namespace experimental { + namespace ui{ + + static std::unordered_map s_WebViewImpls; + + WebViewImpl::WebViewImpl(WebView *webView) : _viewTag(-1), _webView(webView) { + _viewTag = createWebViewJNI(); + s_WebViewImpls[_viewTag] = this; + } + + WebViewImpl::~WebViewImpl() { + removeWebViewJNI(_viewTag); + s_WebViewImpls.erase(_viewTag); + } + + void WebViewImpl::loadData(const Data &data, const std::string &MIMEType, const std::string &encoding, const std::string &baseURL) { + std::string dataString(reinterpret_cast(data.getBytes()), static_cast(data.getSize())); + loadDataJNI(_viewTag, dataString, MIMEType, encoding, baseURL); + } + + void WebViewImpl::loadHTMLString(const std::string &string, const std::string &baseURL) { + loadHTMLStringJNI(_viewTag, string, baseURL); + } + + void WebViewImpl::loadUrl(const std::string &url) { + loadUrlJNI(_viewTag, url); + } + + void WebViewImpl::loadFile(const std::string &fileName) { + auto fullPath = getUrlStringByFileName(fileName); + loadFileJNI(_viewTag, fullPath); + } + + void WebViewImpl::stopLoading() { + stopLoadingJNI(_viewTag); + } + + void WebViewImpl::reload() { + reloadJNI(_viewTag); + } + + bool WebViewImpl::canGoBack() { + return canGoBackJNI(_viewTag); + } + + bool WebViewImpl::canGoForward() { + return canGoForwardJNI(_viewTag); + } + + void WebViewImpl::goBack() { + goBackJNI(_viewTag); + } + + void WebViewImpl::goForward() { + goForwardJNI(_viewTag); + } + + void WebViewImpl::setJavascriptInterfaceScheme(const std::string &scheme) { + setJavascriptInterfaceSchemeJNI(_viewTag, scheme); + } + + void WebViewImpl::evaluateJS(const std::string &js) { + evaluateJSJNI(_viewTag, js); + } + + void WebViewImpl::setScalesPageToFit(const bool scalesPageToFit) { + setScalesPageToFitJNI(_viewTag, scalesPageToFit); + } + + bool WebViewImpl::shouldStartLoading(const int viewTag, const std::string &url) { + auto it = s_WebViewImpls.find(viewTag); + if (it != s_WebViewImpls.end()) { + auto webView = s_WebViewImpls[viewTag]->_webView; + if (webView->shouldStartLoading) { + return webView->shouldStartLoading(webView, url); + } + } + return true; + } + + void WebViewImpl::didFinishLoading(const int viewTag, const std::string &url){ + auto it = s_WebViewImpls.find(viewTag); + if (it != s_WebViewImpls.end()) { + auto webView = s_WebViewImpls[viewTag]->_webView; + if (webView->didFinishLoading) { + webView->didFinishLoading(webView, url); + } + } + } + + void WebViewImpl::didFailLoading(const int viewTag, const std::string &url){ + auto it = s_WebViewImpls.find(viewTag); + if (it != s_WebViewImpls.end()) { + auto webView = s_WebViewImpls[viewTag]->_webView; + if (webView->didFailLoading) { + webView->didFailLoading(webView, url); + } + } + } + + void WebViewImpl::onJsCallback(const int viewTag, const std::string &message){ + auto it = s_WebViewImpls.find(viewTag); + if (it != s_WebViewImpls.end()) { + auto webView = s_WebViewImpls[viewTag]->_webView; + if (webView->onJsCallback) { + webView->onJsCallback(webView, message); + } + } + } + + void WebViewImpl::draw(cocos2d::Renderer *renderer, cocos2d::Mat4 const &transform, uint32_t flags) { + if (flags & cocos2d::Node::FLAGS_TRANSFORM_DIRTY) { + auto directorInstance = cocos2d::Director::getInstance(); + auto glView = directorInstance->getOpenGLView(); + auto frameSize = glView->getFrameSize(); + + auto winSize = directorInstance->getWinSize(); + + auto leftBottom = this->_webView->convertToWorldSpace(cocos2d::Point::ZERO); + auto rightTop = this->_webView->convertToWorldSpace(cocos2d::Point(this->_webView->getContentSize().width,this->_webView->getContentSize().height)); + + auto uiLeft = frameSize.width / 2 + (leftBottom.x - winSize.width / 2 ) * glView->getScaleX(); + auto uiTop = frameSize.height /2 - (rightTop.y - winSize.height / 2) * glView->getScaleY(); + + setWebViewRectJNI(_viewTag,uiLeft,uiTop, + (rightTop.x - leftBottom.x) * glView->getScaleX(), + (rightTop.y - leftBottom.y) * glView->getScaleY()); + } + } + + void WebViewImpl::setVisible(bool visible) { + setWebViewVisibleJNI(_viewTag, visible); + } + } // namespace cocos2d + } // namespace experimental +} //namespace ui + +#endif diff --git a/cocos/ui/WebViewImpl_android.h b/cocos/ui/WebViewImpl_android.h index bc43f7228251..01a3fd86efb0 100644 --- a/cocos/ui/WebViewImpl_android.h +++ b/cocos/ui/WebViewImpl_android.h @@ -1,6 +1,26 @@ -// -// Created by gin0606 on 2014/07/30. -// +/**************************************************************************** + Copyright (c) 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. + ****************************************************************************/ #ifndef __cocos2d_plugin_WebViewImpl_android_H_ #define __cocos2d_plugin_WebViewImpl_android_H_ @@ -8,65 +28,69 @@ #include namespace cocos2d { -class Data; -class Renderer; -class Mat4; - -namespace plugin { -class WebView; -} + class Data; + class Renderer; + class Mat4; + + namespace experimental { + namespace ui{ + class WebView; + } + } } namespace cocos2d { -namespace plugin { + namespace experimental { + namespace ui{ -class WebViewImpl { -public: - WebViewImpl(cocos2d::plugin::WebView *webView); + class WebViewImpl { + public: + WebViewImpl(cocos2d::experimental::ui::WebView *webView); - virtual ~WebViewImpl(); + virtual ~WebViewImpl(); - void setJavascriptInterfaceScheme(const std::string &scheme); + void setJavascriptInterfaceScheme(const std::string &scheme); - void loadData(const cocos2d::Data &data, const std::string &MIMEType, const std::string &encoding, const std::string &baseURL); + void loadData(const cocos2d::Data &data, const std::string &MIMEType, const std::string &encoding, const std::string &baseURL); - void loadHTMLString(const std::string &string, const std::string &baseURL); + void loadHTMLString(const std::string &string, const std::string &baseURL); - void loadUrl(const std::string &url); + void loadUrl(const std::string &url); - void loadFile(const std::string &fileName); + void loadFile(const std::string &fileName); - void stopLoading(); + void stopLoading(); - void reload(); + void reload(); - bool canGoBack(); + bool canGoBack(); - bool canGoForward(); + bool canGoForward(); - void goBack(); + void goBack(); - void goForward(); + void goForward(); - void evaluateJS(const std::string &js); + void evaluateJS(const std::string &js); - void setScalesPageToFit(const bool scalesPageToFit); + void setScalesPageToFit(const bool scalesPageToFit); - virtual void draw(cocos2d::Renderer *renderer, cocos2d::Mat4 const &transform, uint32_t flags); + virtual void draw(cocos2d::Renderer *renderer, cocos2d::Mat4 const &transform, uint32_t flags); - virtual void setVisible(bool visible); + virtual void setVisible(bool visible); - static bool shouldStartLoading(const int viewTag, const std::string &url); - static void didFinishLoading(const int viewTag, const std::string &url); - static void didFailLoading(const int viewTag, const std::string &url); - static void onJsCallback(const int viewTag, const std::string &message); + static bool shouldStartLoading(const int viewTag, const std::string &url); + static void didFinishLoading(const int viewTag, const std::string &url); + static void didFailLoading(const int viewTag, const std::string &url); + static void onJsCallback(const int viewTag, const std::string &message); -private: - int _viewTag; - WebView *_webView; -}; + private: + int _viewTag; + WebView *_webView; + }; -} // namespace cocos2d -} // namespace plugin + } // namespace ui + } // namespace experimental +} //cocos2d #endif //__cocos2d_plugin_WebViewImpl_android_H_ diff --git a/cocos/ui/Webview-inl.h b/cocos/ui/Webview-inl.h new file mode 100644 index 000000000000..8a76365b0c8d --- /dev/null +++ b/cocos/ui/Webview-inl.h @@ -0,0 +1,141 @@ +/**************************************************************************** + Copyright (c) 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. + ****************************************************************************/ + + +#include "WebView.h" +#include "platform/CCGLView.h" +#include "base/CCDirector.h" +#include "platform/CCFileUtils.h" + + + +NS_CC_BEGIN +namespace experimental{ + namespace ui{ + + WebView::WebView() + : _impl(new WebViewImpl(this)) + { + } + + WebView::~WebView() + { + CC_SAFE_DELETE(_impl); + } + + WebView *WebView::create() + { + auto webView = new(std::nothrow) WebView(); + if (webView && webView->init()) + { + webView->autorelease(); + return webView; + } + CC_SAFE_DELETE(webView); + return nullptr; + } + + void WebView::setJavascriptInterfaceScheme(const std::string &scheme) + { + _impl->setJavascriptInterfaceScheme(scheme); + } + + void WebView::loadData(const cocos2d::Data &data, + const std::string &MIMEType, + const std::string &encoding, + const std::string &baseURL) + { + _impl->loadData(data, MIMEType, encoding, baseURL); + } + + void WebView::loadHTMLString(const std::string &string, const std::string &baseURL) + { + _impl->loadHTMLString(string, baseURL); + } + + void WebView::loadUrl(const std::string &url) + { + _impl->loadUrl(url); + } + + void WebView::loadFile(const std::string &fileName) + { + _impl->loadFile(fileName); + } + + void WebView::stopLoading() + { + _impl->stopLoading(); + } + + void WebView::reload() + { + _impl->reload(); + } + + bool WebView::canGoBack() + { + return _impl->canGoBack(); + } + + bool WebView::canGoForward() + { + return _impl->canGoForward(); + } + + void WebView::goBack() + { + _impl->goBack(); + } + + void WebView::goForward() + { + _impl->goForward(); + } + + void WebView::evaluateJS(const std::string &js) + { + _impl->evaluateJS(js); + } + + void WebView::setScalesPageToFit(bool const scalesPageToFit) + { + _impl->setScalesPageToFit(scalesPageToFit); + } + + void WebView::draw(cocos2d::Renderer *renderer, cocos2d::Mat4 const &transform, uint32_t flags) + { + cocos2d::ui::Widget::draw(renderer, transform, flags); + _impl->draw(renderer, transform, flags); + } + + void WebView::setVisible(bool visible) + { + Node::setVisible(visible); + _impl->setVisible(visible); + } + } // namespace cocos2d +} // namespace ui +} //namespace experimental + diff --git a/cocos/ui/org_cocos2dx_lib_Cocos2dxWebViewHelper.cpp b/cocos/ui/org_cocos2dx_lib_Cocos2dxWebViewHelper.cpp deleted file mode 100644 index 203bd4398730..000000000000 --- a/cocos/ui/org_cocos2dx_lib_Cocos2dxWebViewHelper.cpp +++ /dev/null @@ -1,35 +0,0 @@ -#include "org_cocos2dx_lib_Cocos2dxWebViewHelper.h" -#include "WebViewImpl_android.h" -#include "WebView.h" -#include -#include -#include - - -JNIEXPORT jboolean JNICALL Java_org_cocos2dx_lib_Cocos2dxWebViewHelper_shouldStartLoading(JNIEnv *env, jclass, jint index, jstring jurl) { - auto charUrl = env->GetStringUTFChars(jurl, NULL); - std::string url = charUrl; - env->ReleaseStringUTFChars(jurl, charUrl); - return cocos2d::plugin::WebViewImpl::shouldStartLoading(index, url); -} - -JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxWebViewHelper_didFinishLoading(JNIEnv *env, jclass, jint index, jstring jurl) { - auto charUrl = env->GetStringUTFChars(jurl, NULL); - std::string url = charUrl; - env->ReleaseStringUTFChars(jurl, charUrl); - cocos2d::plugin::WebViewImpl::didFinishLoading(index, url); -} - -JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxWebViewHelper_didFailLoading(JNIEnv *env, jclass, jint index, jstring jurl) { - auto charUrl = env->GetStringUTFChars(jurl, NULL); - std::string url = charUrl; - env->ReleaseStringUTFChars(jurl, charUrl); - cocos2d::plugin::WebViewImpl::didFailLoading(index, url); -} - -JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxWebViewHelper_onJsCallback(JNIEnv *env, jclass, jint index, jstring jmessage) { - auto charMessage = env->GetStringUTFChars(jmessage, NULL); - std::string message = charMessage; - env->ReleaseStringUTFChars(jmessage, charMessage); - cocos2d::plugin::WebViewImpl::onJsCallback(index, message); -} diff --git a/cocos/ui/org_cocos2dx_lib_Cocos2dxWebViewHelper.h b/cocos/ui/org_cocos2dx_lib_Cocos2dxWebViewHelper.h deleted file mode 100644 index 288bf797655a..000000000000 --- a/cocos/ui/org_cocos2dx_lib_Cocos2dxWebViewHelper.h +++ /dev/null @@ -1,59 +0,0 @@ -/* DO NOT EDIT THIS FILE - it is machine generated */ -#include -/* Header for class org_cocos2dx_lib_Cocos2dxWebViewHelper */ - -#ifndef _Included_org_cocos2dx_lib_Cocos2dxWebViewHelper -#define _Included_org_cocos2dx_lib_Cocos2dxWebViewHelper -#ifdef __cplusplus -extern "C" { -#endif -/* Inaccessible static: handler */ -/* Inaccessible static: viewTag */ -#undef org_cocos2dx_lib_Cocos2dxWebViewHelper_kWebViewTaskCreate -#define org_cocos2dx_lib_Cocos2dxWebViewHelper_kWebViewTaskCreate 0L -#undef org_cocos2dx_lib_Cocos2dxWebViewHelper_kWebViewTaskRemove -#define org_cocos2dx_lib_Cocos2dxWebViewHelper_kWebViewTaskRemove 1L -#undef org_cocos2dx_lib_Cocos2dxWebViewHelper_kWebViewTaskSetRect -#define org_cocos2dx_lib_Cocos2dxWebViewHelper_kWebViewTaskSetRect 2L -#undef org_cocos2dx_lib_Cocos2dxWebViewHelper_kWebViewTaskLoadURI -#define org_cocos2dx_lib_Cocos2dxWebViewHelper_kWebViewTaskLoadURI 3L -#undef org_cocos2dx_lib_Cocos2dxWebViewHelper_kWebViewTaskEvaluateJS -#define org_cocos2dx_lib_Cocos2dxWebViewHelper_kWebViewTaskEvaluateJS 4L -#undef org_cocos2dx_lib_Cocos2dxWebViewHelper_kWebViewTaskSetVisible -#define org_cocos2dx_lib_Cocos2dxWebViewHelper_kWebViewTaskSetVisible 5L -/* - * Class: org_cocos2dx_lib_Cocos2dxWebViewHelper - * Method: shouldStartLoading - * Signature: (ILjava/lang/String;)Z - */ -JNIEXPORT jboolean JNICALL Java_org_cocos2dx_lib_Cocos2dxWebViewHelper_shouldStartLoading - (JNIEnv *, jclass, jint, jstring); - -/* - * Class: org_cocos2dx_lib_Cocos2dxWebViewHelper - * Method: didFinishLoading - * Signature: (ILjava/lang/String;)V - */ -JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxWebViewHelper_didFinishLoading - (JNIEnv *, jclass, jint, jstring); - -/* - * Class: org_cocos2dx_lib_Cocos2dxWebViewHelper - * Method: didFailLoading - * Signature: (ILjava/lang/String;)V - */ -JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxWebViewHelper_didFailLoading - (JNIEnv *, jclass, jint, jstring); - -/* - * Class: org_cocos2dx_lib_Cocos2dxWebViewHelper - * Method: onJsCallback - * Signature: (ILjava/lang/String;)V - */ -JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxWebViewHelper_onJsCallback - (JNIEnv *, jclass, jint, jstring); - -#ifdef __cplusplus -} -#endif -#endif From 15815118a2ae6b60d877617910efc3222192574c Mon Sep 17 00:00:00 2001 From: andyque Date: Tue, 26 Aug 2014 16:53:37 +0800 Subject: [PATCH 07/14] finish android version --- cocos/Android.mk | 4 +- .../org/cocos2dx/lib/Cocos2dxActivity.java | 10 +++-- .../src/org/cocos2dx/lib/Cocos2dxWebView.java | 8 ++-- cocos/ui/WebViewImpl_android.cpp | 41 ++++++++++++++----- cocos/ui/Webview-inl.h | 6 +-- tests/cpp-tests/Android.mk | 1 + .../UIWebViewTest/UIWebViewTest.cpp | 14 ++++++- .../UIWebViewTest/UIWebViewTest.h | 2 + 8 files changed, 63 insertions(+), 23 deletions(-) diff --git a/cocos/Android.mk b/cocos/Android.mk index 7b2e999f32d3..086c9d07cc2c 100644 --- a/cocos/Android.mk +++ b/cocos/Android.mk @@ -349,7 +349,9 @@ LOCAL_SRC_FILES += ui/UIWidget.cpp \ ui/UIRelativeBox.cpp \ ui/UIVideoPlayerAndroid.cpp \ ui/UIDeprecated.cpp \ - ui/UIScale9Sprite.cpp + ui/UIScale9Sprite.cpp \ + ui/WebView.cpp \ + ui/WebViewImpl_android.cpp \ #extension LOCAL_SRC_FILES += ../extensions/assets-manager/AssetsManager.cpp \ diff --git a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxActivity.java b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxActivity.java index 1ad24349abef..53ea0418a364 100644 --- a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxActivity.java +++ b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxActivity.java @@ -33,11 +33,10 @@ of this software and associated documentation files (the "Software"), to deal import android.os.Build; import android.os.Bundle; import android.os.Message; -import android.view.ViewGroup; -import android.view.WindowManager; +import android.preference.PreferenceManager.OnActivityResultListener; import android.util.Log; +import android.view.ViewGroup; import android.widget.FrameLayout; -import android.preference.PreferenceManager.OnActivityResultListener; public abstract class Cocos2dxActivity extends Activity implements Cocos2dxHelperListener { // =========================================================== @@ -55,6 +54,7 @@ public abstract class Cocos2dxActivity extends Activity implements Cocos2dxHelpe private Cocos2dxHandler mHandler; private static Cocos2dxActivity sContext = null; private Cocos2dxVideoHelper mVideoHelper = null; + private Cocos2dxWebViewHelper mWebViewHelper = null; public static Context getContext() { return sContext; @@ -102,6 +102,10 @@ protected void onCreate(final Bundle savedInstanceState) { if (mVideoHelper == null) { mVideoHelper = new Cocos2dxVideoHelper(this, mFrameLayout); } + + if(mWebViewHelper == null){ + mWebViewHelper = new Cocos2dxWebViewHelper(mFrameLayout); + } } //native method,call GLViewImpl::getGLContextAttrs() to get the OpenGL ES context attributions diff --git a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxWebView.java b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxWebView.java index 362cc4e6c435..4229a21e0bbd 100755 --- a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxWebView.java +++ b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxWebView.java @@ -1,5 +1,8 @@ package org.cocos2dx.lib; +import java.lang.reflect.Method; +import java.net.URI; + import android.annotation.SuppressLint; import android.content.Context; import android.util.Log; @@ -7,9 +10,6 @@ import android.webkit.WebViewClient; import android.widget.FrameLayout; -import java.lang.reflect.Method; -import java.net.URI; - public class Cocos2dxWebView extends WebView { private static final String TAG = Cocos2dxWebViewHelper.class.getSimpleName(); @@ -37,7 +37,7 @@ public Cocos2dxWebView(Context context, int viewTag) { try { Method method = this.getClass().getMethod("removeJavascriptInterface", new Class[]{String.class}); method.invoke(this, "searchBoxJavaBridge_"); - } catch (ReflectiveOperationException e) { + } catch (Exception e) { Log.d(TAG, "This API level do not support `removeJavascriptInterface`"); } diff --git a/cocos/ui/WebViewImpl_android.cpp b/cocos/ui/WebViewImpl_android.cpp index 420709016966..67c6bb30a0eb 100644 --- a/cocos/ui/WebViewImpl_android.cpp +++ b/cocos/ui/WebViewImpl_android.cpp @@ -24,20 +24,22 @@ #include "WebViewImpl_android.h" -#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) -#include "WebView.h" +#include +#include +#include #include "jni/JniHelper.h" #include + +#include "WebView.h" #include "platform/CCGLView.h" #include "base/CCDirector.h" #include "platform/CCFileUtils.h" -#include -#include -#include -#include #define CLASS_NAME "org/cocos2dx/lib/Cocos2dxWebViewHelper" -extern "C"{ + +#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,"",__VA_ARGS__) + +extern "C" { /* * Class: org_cocos2dx_lib_Cocos2dxWebViewHelper * Method: shouldStartLoading @@ -56,6 +58,7 @@ extern "C"{ * Signature: (ILjava/lang/String;)V */ JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxWebViewHelper_didFinishLoading(JNIEnv *env, jclass, jint index, jstring jurl) { + // LOGD("didFinishLoading"); auto charUrl = env->GetStringUTFChars(jurl, NULL); std::string url = charUrl; env->ReleaseStringUTFChars(jurl, charUrl); @@ -68,6 +71,7 @@ extern "C"{ * Signature: (ILjava/lang/String;)V */ JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxWebViewHelper_didFailLoading(JNIEnv *env, jclass, jint index, jstring jurl) { + // LOGD("didFailLoading"); auto charUrl = env->GetStringUTFChars(jurl, NULL); std::string url = charUrl; env->ReleaseStringUTFChars(jurl, charUrl); @@ -80,6 +84,7 @@ extern "C"{ * Signature: (ILjava/lang/String;)V */ JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxWebViewHelper_onJsCallback(JNIEnv *env, jclass, jint index, jstring jmessage) { + // LOGD("jsCallback"); auto charMessage = env->GetStringUTFChars(jmessage, NULL); std::string message = charMessage; env->ReleaseStringUTFChars(jmessage, charMessage); @@ -92,6 +97,7 @@ namespace { int createWebViewJNI() { cocos2d::JniMethodInfo t; if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "createWebView", "()I")) { + // LOGD("error: %s,%d",__func__,__LINE__); jint viewTag = t.env->CallStaticIntMethod(t.classID, t.methodID); t.env->DeleteLocalRef(t.classID); return viewTag; @@ -100,6 +106,7 @@ int createWebViewJNI() { } void removeWebViewJNI(const int index) { + // LOGD("error: %s,%d",__func__,__LINE__); cocos2d::JniMethodInfo t; if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "removeWebView", "(I)V")) { t.env->CallStaticVoidMethod(t.classID, t.methodID, index); @@ -108,6 +115,7 @@ void removeWebViewJNI(const int index) { } void setWebViewRectJNI(const int index, const int left, const int top, const int width, const int height) { + // LOGD("error: %s,%d",__func__,__LINE__); cocos2d::JniMethodInfo t; if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "setWebViewRect", "(IIIII)V")) { t.env->CallStaticVoidMethod(t.classID, t.methodID, index, left, top, width, height); @@ -116,6 +124,7 @@ void setWebViewRectJNI(const int index, const int left, const int top, const int } void setJavascriptInterfaceSchemeJNI(const int index, const std::string &scheme) { + // LOGD("error: %s,%d",__func__,__LINE__); cocos2d::JniMethodInfo t; if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "setJavascriptInterfaceScheme", "(ILjava/lang/String;)V")) { jstring jScheme = t.env->NewStringUTF(scheme.c_str()); @@ -127,6 +136,7 @@ void setJavascriptInterfaceSchemeJNI(const int index, const std::string &scheme) } void loadDataJNI(const int index, const std::string &data, const std::string &MIMEType, const std::string &encoding, const std::string &baseURL) { + // LOGD("error: %s,%d",__func__,__LINE__); cocos2d::JniMethodInfo t; if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "loadData", "(ILjava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V")) { jstring jData = t.env->NewStringUTF(data.c_str()); @@ -144,6 +154,7 @@ void loadDataJNI(const int index, const std::string &data, const std::string &MI } void loadHTMLStringJNI(const int index, const std::string &string, const std::string &baseURL) { + // LOGD("error: %s,%d",__func__,__LINE__); cocos2d::JniMethodInfo t; if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "loadHTMLString", "(ILjava/lang/String;Ljava/lang/String;)V")) { jstring jString = t.env->NewStringUTF(string.c_str()); @@ -168,6 +179,7 @@ void loadUrlJNI(const int index, const std::string &url) { } void loadFileJNI(const int index, const std::string &filePath) { + // LOGD("error: %s,%d",__func__,__LINE__); cocos2d::JniMethodInfo t; if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "loadFile", "(ILjava/lang/String;)V")) { jstring jFilePath = t.env->NewStringUTF(filePath.c_str()); @@ -179,6 +191,7 @@ void loadFileJNI(const int index, const std::string &filePath) { } void stopLoadingJNI(const int index) { + // LOGD("error: %s,%d",__func__,__LINE__); cocos2d::JniMethodInfo t; if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "stopLoading", "(I)V")) { t.env->CallStaticVoidMethod(t.classID, t.methodID, index); @@ -187,6 +200,7 @@ void stopLoadingJNI(const int index) { } void reloadJNI(const int index) { + // LOGD("error: %s,%d",__func__,__LINE__); cocos2d::JniMethodInfo t; if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "reload", "(I)V")) { t.env->CallStaticVoidMethod(t.classID, t.methodID, index); @@ -195,6 +209,7 @@ void reloadJNI(const int index) { } bool canGoBackJNI(const int index) { + // LOGD("error: %s,%d",__func__,__LINE__); cocos2d::JniMethodInfo t; if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "canGoBack", "(I)Z")) { jboolean ret = t.env->CallStaticBooleanMethod(t.classID, t.methodID, index); @@ -205,6 +220,7 @@ bool canGoBackJNI(const int index) { } bool canGoForwardJNI(const int index) { + // LOGD("error: %s,%d",__func__,__LINE__); cocos2d::JniMethodInfo t; if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "canGoForward", "(I)Z")) { jboolean ret = t.env->CallStaticBooleanMethod(t.classID, t.methodID, index); @@ -215,6 +231,7 @@ bool canGoForwardJNI(const int index) { } void goBackJNI(const int index) { + // LOGD("error: %s,%d",__func__,__LINE__); cocos2d::JniMethodInfo t; if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "goBack", "(I)V")) { t.env->CallStaticVoidMethod(t.classID, t.methodID, index); @@ -223,6 +240,7 @@ void goBackJNI(const int index) { } void goForwardJNI(const int index) { + // LOGD("error: %s,%d",__func__,__LINE__); cocos2d::JniMethodInfo t; if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "goForward", "(I)V")) { t.env->CallStaticVoidMethod(t.classID, t.methodID, index); @@ -231,6 +249,7 @@ void goForwardJNI(const int index) { } void evaluateJSJNI(const int index, const std::string &js) { + // LOGD("error: %s,%d",__func__,__LINE__); cocos2d::JniMethodInfo t; if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "evaluateJS", "(ILjava/lang/String;)V")) { jstring jjs = t.env->NewStringUTF(js.c_str()); @@ -242,6 +261,7 @@ void evaluateJSJNI(const int index, const std::string &js) { } void setScalesPageToFitJNI(const int index, const bool scalesPageToFit) { + // LOGD("error: %s,%d",__func__,__LINE__); cocos2d::JniMethodInfo t; if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "setScalesPageToFit", "(IZ)V")) { t.env->CallStaticVoidMethod(t.classID, t.methodID, index, scalesPageToFit); @@ -250,6 +270,7 @@ void setScalesPageToFitJNI(const int index, const bool scalesPageToFit) { } void setWebViewVisibleJNI(const int index, const bool visible) { + // LOGD("error: %s,%d",__func__,__LINE__); cocos2d::JniMethodInfo t; if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "setVisible", "(IZ)V")) { t.env->CallStaticVoidMethod(t.classID, t.methodID, index, visible); @@ -258,6 +279,7 @@ void setWebViewVisibleJNI(const int index, const bool visible) { } std::string getUrlStringByFileName(const std::string &fileName) { + // LOGD("error: %s,%d",__func__,__LINE__); const std::string basePath("file:///android_asset/"); std::string fullPath = cocos2d::FileUtils::getInstance()->fullPathForFilename(fileName); const std::string assetsPath("assets/"); @@ -407,8 +429,7 @@ namespace cocos2d { void WebViewImpl::setVisible(bool visible) { setWebViewVisibleJNI(_viewTag, visible); } - } // namespace cocos2d + } // namespace ui } // namespace experimental -} //namespace ui +} //namespace cocos2d -#endif diff --git a/cocos/ui/Webview-inl.h b/cocos/ui/Webview-inl.h index 8a76365b0c8d..835d6829bb9b 100644 --- a/cocos/ui/Webview-inl.h +++ b/cocos/ui/Webview-inl.h @@ -135,7 +135,7 @@ namespace experimental{ Node::setVisible(visible); _impl->setVisible(visible); } - } // namespace cocos2d -} // namespace ui -} //namespace experimental + } // namespace ui +} // namespace experimental +} //namespace cocos2d diff --git a/tests/cpp-tests/Android.mk b/tests/cpp-tests/Android.mk index a954ff93e181..71ff77954dce 100644 --- a/tests/cpp-tests/Android.mk +++ b/tests/cpp-tests/Android.mk @@ -104,6 +104,7 @@ Classes/UITest/CocoStudioGUITest/UITextBMFontTest/UITextBMFontTest_Editor.cpp \ Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest_Editor.cpp \ Classes/UITest/CocoStudioGUITest/UIWidgetAddNodeTest/UIWidgetAddNodeTest_Editor.cpp \ Classes/UITest/CocoStudioGUITest/UIVideoPlayerTest/UIVideoPlayerTest.cpp \ +Classes/UITest/CocoStudioGUITest/UIWebViewTest/UIWebViewTest.cpp \ Classes/UITest/CocoStudioGUITest/CustomWidget/CustomImageView.cpp \ Classes/UITest/CocoStudioGUITest/CustomWidget/CustomImageViewReader.cpp \ Classes/UITest/CocoStudioGUITest/CustomWidget/CustomParticleWidget.cpp \ diff --git a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIWebViewTest/UIWebViewTest.cpp b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIWebViewTest/UIWebViewTest.cpp index 2dd49d058f3a..f7bec25f7d05 100644 --- a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIWebViewTest/UIWebViewTest.cpp +++ b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIWebViewTest/UIWebViewTest.cpp @@ -24,10 +24,20 @@ #include "UIWebViewTest.h" +WebViewTest::WebViewTest() +{ + +} + +WebViewTest::~WebViewTest() +{ + +} + bool WebViewTest::init() { if (UIScene::init()) { - _webView = experimental::ui::WebView::create(); + _webView = cocos2d::experimental::ui::WebView::create(); _webView->setPosition(Director::getInstance()->getVisibleSize()/2); _webView->setContentSize(Director::getInstance()->getVisibleSize() * 0.5); _webView->loadUrl("http://www.baidu.com"); @@ -37,4 +47,4 @@ bool WebViewTest::init() return true; } return false; -} \ No newline at end of file +} diff --git a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIWebViewTest/UIWebViewTest.h b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIWebViewTest/UIWebViewTest.h index 9365412eb03d..865ed9f02c36 100644 --- a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIWebViewTest/UIWebViewTest.h +++ b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIWebViewTest/UIWebViewTest.h @@ -32,6 +32,8 @@ USING_NS_CC; class WebViewTest : public UIScene { public: + WebViewTest(); + virtual ~WebViewTest(); UI_SCENE_CREATE_FUNC(WebViewTest); virtual bool init(); From 9449152cafc88741f89c86c720837db390e8feec Mon Sep 17 00:00:00 2001 From: andyque Date: Tue, 26 Aug 2014 22:03:55 +0800 Subject: [PATCH 08/14] finish WebView Tests --- build/cocos2d_tests.xcodeproj/project.pbxproj | 4 + cocos/ui/WebView.h | 4 +- cocos/ui/WebViewImpl_iOS.h | 4 +- cocos/ui/WebViewImpl_iOS.mm | 5 +- .../UIWebViewTest/UIWebViewTest.cpp | 124 +++++++++++++++++- .../UIWebViewTest/UIWebViewTest.h | 4 +- tests/cpp-tests/Resources/Test.html | 5 + 7 files changed, 140 insertions(+), 10 deletions(-) create mode 100644 tests/cpp-tests/Resources/Test.html diff --git a/build/cocos2d_tests.xcodeproj/project.pbxproj b/build/cocos2d_tests.xcodeproj/project.pbxproj index a0dc2b00f60a..2029a5d0ffdd 100644 --- a/build/cocos2d_tests.xcodeproj/project.pbxproj +++ b/build/cocos2d_tests.xcodeproj/project.pbxproj @@ -866,6 +866,7 @@ 295824591987415900F9746D /* UIScale9SpriteTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 295824571987415900F9746D /* UIScale9SpriteTest.cpp */; }; 2958245A1987415900F9746D /* UIScale9SpriteTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 295824571987415900F9746D /* UIScale9SpriteTest.cpp */; }; 298D7F6F19AC31F300FF096D /* UIWebViewTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 298D7F6D19AC31F300FF096D /* UIWebViewTest.cpp */; }; + 29AFEF6719ACCAA000F6B10A /* Test.html in Resources */ = {isa = PBXBuildFile; fileRef = 29AFEF6619ACCAA000F6B10A /* Test.html */; }; 29FBBBFE196A9ECD00E65826 /* CocostudioParserJsonTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 29FBBBFC196A9ECD00E65826 /* CocostudioParserJsonTest.cpp */; }; 29FBBBFF196A9ECD00E65826 /* CocostudioParserJsonTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 29FBBBFC196A9ECD00E65826 /* CocostudioParserJsonTest.cpp */; }; 38FA2E73194AEBE100FF2BE4 /* ActionTimelineTestScene.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 38FA2E71194AEBE100FF2BE4 /* ActionTimelineTestScene.cpp */; }; @@ -2889,6 +2890,7 @@ 295824581987415900F9746D /* UIScale9SpriteTest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UIScale9SpriteTest.h; sourceTree = ""; }; 298D7F6D19AC31F300FF096D /* UIWebViewTest.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = UIWebViewTest.cpp; path = UIWebViewTest/UIWebViewTest.cpp; sourceTree = ""; }; 298D7F6E19AC31F300FF096D /* UIWebViewTest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = UIWebViewTest.h; path = UIWebViewTest/UIWebViewTest.h; sourceTree = ""; }; + 29AFEF6619ACCAA000F6B10A /* Test.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; name = Test.html; path = "../tests/cpp-tests/Resources/Test.html"; sourceTree = ""; }; 29FBBBFC196A9ECD00E65826 /* CocostudioParserJsonTest.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CocostudioParserJsonTest.cpp; sourceTree = ""; }; 29FBBBFD196A9ECD00E65826 /* CocostudioParserJsonTest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CocostudioParserJsonTest.h; sourceTree = ""; }; 38FA2E71194AEBE100FF2BE4 /* ActionTimelineTestScene.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ActionTimelineTestScene.cpp; sourceTree = ""; }; @@ -6498,6 +6500,7 @@ 1AC35CB518CED84500F37B72 /* effect1.raw */, 1AC35CB618CED84500F37B72 /* effect1.wav */, 1AC35CC418CED84500F37B72 /* pew-pew-lei.wav */, + 29AFEF6619ACCAA000F6B10A /* Test.html */, ); name = Resources; sourceTree = ""; @@ -7829,6 +7832,7 @@ 1AC35D0918CED84500F37B72 /* TileMaps in Resources */, 1AC35CFF18CED84500F37B72 /* Particles in Resources */, 1AC35C8818CECF1400F37B72 /* Default-568h@2x.png in Resources */, + 29AFEF6719ACCAA000F6B10A /* Test.html in Resources */, 1AC35CD518CED84500F37B72 /* ccb in Resources */, 1AC35CE118CED84500F37B72 /* configs in Resources */, 1AC35CE918CED84500F37B72 /* extensions in Resources */, diff --git a/cocos/ui/WebView.h b/cocos/ui/WebView.h index e0977d2e858b..37c67e3d916b 100644 --- a/cocos/ui/WebView.h +++ b/cocos/ui/WebView.h @@ -167,9 +167,9 @@ class WebView : public cocos2d::ui::Widget { WebViewImpl *_impl; }; - } // namespace cocos2d + } // namespace ui } // namespace experimental -}//namespace ui +}//namespace cocos2d #endif diff --git a/cocos/ui/WebViewImpl_iOS.h b/cocos/ui/WebViewImpl_iOS.h index fb371ed424de..277ca59a589f 100644 --- a/cocos/ui/WebViewImpl_iOS.h +++ b/cocos/ui/WebViewImpl_iOS.h @@ -84,8 +84,8 @@ class WebViewImpl { WebView *_webView; }; - } // namespace cocos2d + } // namespace ui } // namespace experimental -}//namespace ui +}//namespace cocos2d #endif //__cocos2d_plugin_WebViewImpl_IOS_H_ diff --git a/cocos/ui/WebViewImpl_iOS.mm b/cocos/ui/WebViewImpl_iOS.mm index 22652b2e916a..8ef646eef9a0 100644 --- a/cocos/ui/WebViewImpl_iOS.mm +++ b/cocos/ui/WebViewImpl_iOS.mm @@ -96,6 +96,7 @@ - (instancetype)init { } - (void)dealloc { + self.uiWebView.delegate = nil; [self.uiWebView removeFromSuperview]; self.jsScheme = nil; [super dealloc]; @@ -343,8 +344,8 @@ - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { [_uiWebViewWrapper setVisible:visible]; } - } // namespace cocos2d + } // namespace ui } // namespace experimental -} //namespace ui +} //namespace cocos2d #endif // CC_TARGET_PLATFORM == CC_PLATFORM_IOS diff --git a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIWebViewTest/UIWebViewTest.cpp b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIWebViewTest/UIWebViewTest.cpp index f7bec25f7d05..a21c2afba44c 100644 --- a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIWebViewTest/UIWebViewTest.cpp +++ b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIWebViewTest/UIWebViewTest.cpp @@ -37,14 +37,132 @@ WebViewTest::~WebViewTest() bool WebViewTest::init() { if (UIScene::init()) { + Size winSize = Director::getInstance()->getVisibleSize(); + + _webView = cocos2d::experimental::ui::WebView::create(); - _webView->setPosition(Director::getInstance()->getVisibleSize()/2); - _webView->setContentSize(Director::getInstance()->getVisibleSize() * 0.5); - _webView->loadUrl("http://www.baidu.com"); + _webView->setPosition(winSize/2); + _webView->setContentSize(winSize * 0.5); + _webView->loadUrl("http://www.google.com"); _webView->setScalesPageToFit(true); + + _webView->shouldStartLoading = CC_CALLBACK_2(WebViewTest::onWebViewShouldStartLoading, this); + _webView->didFinishLoading = CC_CALLBACK_2(WebViewTest::onWebViewDidFinishLoading, this); + _webView->didFailLoading = CC_CALLBACK_2(WebViewTest::onWebViewDidFailLoading, this); + this->addChild(_webView); + TextField *urlTextField = TextField::create("Input a URL here", "Arial", 20); + urlTextField->setPlaceHolderColor(Color3B::RED); + urlTextField->setPosition(Vec2(winSize/2) + Vec2(-80, _webView->getContentSize().height/2 + + urlTextField->getContentSize().height/2 + 10)); + this->addChild(urlTextField); + + Text *httpLabel = Text::create("http:// ", "Arial", 20); + httpLabel->setTextColor(Color4B::GREEN); + httpLabel->setAnchorPoint(Vec2(1.0,0.5)); + httpLabel->setPosition(urlTextField->getPosition() - Vec2(urlTextField->getContentSize().width/2,0)); + this->addChild(httpLabel); + + + Button *resetBtn = Button::create("cocosui/animationbuttonnormal.png", + "cocosui/animationbuttonpressed.png"); + resetBtn->setTitleText("Visit URL"); + resetBtn->setPosition(Vec2(winSize/2) + Vec2(50, _webView->getContentSize().height/2 + + resetBtn->getContentSize().height/2 + 10)); + resetBtn->addClickEventListener([=](Ref*){ + _webView->loadUrl(std::string("http://") + urlTextField->getStringValue()); + }); + this->addChild(resetBtn); + + + Button *reloadBtn = Button::create("cocosui/animationbuttonnormal.png", + "cocosui/animationbuttonpressed.png"); + reloadBtn->setTitleText("Reload"); + reloadBtn->setPosition(Vec2(winSize/2) + Vec2( _webView->getContentSize().width/2 + + reloadBtn->getContentSize().width/2 + 10,50 )); + reloadBtn->addClickEventListener([=](Ref*){ + _webView->reload(); + }); + this->addChild(reloadBtn); + + Button *forwardBtn = Button::create("cocosui/animationbuttonnormal.png", + "cocosui/animationbuttonpressed.png"); + forwardBtn->setTitleText("Forward"); + forwardBtn->setPosition(Vec2(winSize/2) + Vec2( _webView->getContentSize().width/2 + + forwardBtn->getContentSize().width/2 + 10,0 )); + forwardBtn->addClickEventListener([=](Ref*){ + _webView->goForward(); + }); + this->addChild(forwardBtn); + + + + Button *backBtn = Button::create("cocosui/animationbuttonnormal.png", + "cocosui/animationbuttonpressed.png"); + backBtn->setTitleText("Back"); + backBtn->setPosition(Vec2(winSize/2) + Vec2( _webView->getContentSize().width/2 + + backBtn->getContentSize().width/2 + 10,-50 )); + backBtn->addClickEventListener([=](Ref*){ + _webView->goBack(); + }); + this->addChild(backBtn); + + + Button *loadFileBtn = Button::create("cocosui/animationbuttonnormal.png", + "cocosui/animationbuttonpressed.png"); + loadFileBtn->setTitleText("Load FILE"); + loadFileBtn->setPosition(Vec2(winSize/2) - Vec2( _webView->getContentSize().width/2 + + loadFileBtn->getContentSize().width/2 + 10,50 )); + loadFileBtn->addClickEventListener([=](Ref*){ + _webView->loadFile("Test.html"); + }); + this->addChild(loadFileBtn); + + Button *loadHTMLBtn = Button::create("cocosui/animationbuttonnormal.png", + "cocosui/animationbuttonpressed.png"); + loadHTMLBtn->setTitleText("Load HTML"); + loadHTMLBtn->setPosition(Vec2(winSize/2) - Vec2( _webView->getContentSize().width/2 + + loadHTMLBtn->getContentSize().width/2 + 10,0 )); + loadHTMLBtn->addClickEventListener([=](Ref*){ + _webView->loadHTMLString("Hello World",""); + }); + this->addChild(loadHTMLBtn); + + + + + Button *evalJsBtn = Button::create("cocosui/animationbuttonnormal.png", + "cocosui/animationbuttonpressed.png"); + evalJsBtn->setTitleText("Evaluate JS"); + evalJsBtn->setPosition(Vec2(winSize/2) - Vec2( _webView->getContentSize().width/2 + + evalJsBtn->getContentSize().width/2 + 10,-50 )); + evalJsBtn->addClickEventListener([=](Ref*){ + _webView->evaluateJS("alert(\"hello\")"); + }); + this->addChild(evalJsBtn); + return true; } return false; } + +bool WebViewTest::onWebViewShouldStartLoading(experimental::ui::WebView *sender, std::string url) +{ + CCLOG("onWebViewShouldStartLoading, url is %s", url.c_str()); + + return true; +} + +void WebViewTest::onWebViewDidFinishLoading(experimental::ui::WebView *sender, std::string url) +{ + CCLOG("onWebViewDidFinishLoading, url is %s", url.c_str()); + +} + +void WebViewTest::onWebViewDidFailLoading(experimental::ui::WebView *sender, std::string url) +{ + CCLOG("onWebViewDidFailLoading, url is %s", url.c_str()); + +} + diff --git a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIWebViewTest/UIWebViewTest.h b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIWebViewTest/UIWebViewTest.h index 865ed9f02c36..a7ae59e07591 100644 --- a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIWebViewTest/UIWebViewTest.h +++ b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIWebViewTest/UIWebViewTest.h @@ -37,8 +37,10 @@ class WebViewTest : public UIScene UI_SCENE_CREATE_FUNC(WebViewTest); virtual bool init(); + bool onWebViewShouldStartLoading(experimental::ui::WebView *sender, std::string url); + void onWebViewDidFinishLoading(experimental::ui::WebView *sender, std::string url); + void onWebViewDidFailLoading(experimental::ui::WebView *sender, std::string url); - private: cocos2d::experimental::ui::WebView *_webView; diff --git a/tests/cpp-tests/Resources/Test.html b/tests/cpp-tests/Resources/Test.html new file mode 100644 index 000000000000..1e90fb526f17 --- /dev/null +++ b/tests/cpp-tests/Resources/Test.html @@ -0,0 +1,5 @@ + + + Hello World + + \ No newline at end of file From fc3a30b9ca04e6dcafc501af5abb31b9b329e33f Mon Sep 17 00:00:00 2001 From: andyque Date: Tue, 26 Aug 2014 23:25:07 +0800 Subject: [PATCH 09/14] modify cmakeList.txt --- cocos/CMakeLists.txt | 1 + cocos/ui/WebViewImpl_android.cpp | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/cocos/CMakeLists.txt b/cocos/CMakeLists.txt index f56e2e635133..7bcfe84efb33 100644 --- a/cocos/CMakeLists.txt +++ b/cocos/CMakeLists.txt @@ -85,6 +85,7 @@ list(REMOVE_ITEM cocos2d_source_files "${CMAKE_CURRENT_SOURCE_DIR}/base/CCController-android.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/base/CCUserDefaultAndroid.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/ui/UIVideoPlayerAndroid.cpp" + "${CMAKE_CURRENT_SOURCE_DIR}/ui/WebView.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/storage/local-storage/LocalStorageAndroid.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/base/CCEventController.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/base/CCEventListenerController.cpp" diff --git a/cocos/ui/WebViewImpl_android.cpp b/cocos/ui/WebViewImpl_android.cpp index 67c6bb30a0eb..514ddaf64813 100644 --- a/cocos/ui/WebViewImpl_android.cpp +++ b/cocos/ui/WebViewImpl_android.cpp @@ -156,10 +156,10 @@ void loadDataJNI(const int index, const std::string &data, const std::string &MI void loadHTMLStringJNI(const int index, const std::string &string, const std::string &baseURL) { // LOGD("error: %s,%d",__func__,__LINE__); cocos2d::JniMethodInfo t; - if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "loadHTMLString", "(ILjava/lang/String;Ljava/lang/String;)V")) { + if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "loadHTMLString", "(ILjava/lang/String;Ljava/lang/String;Ljava/lang/String;)V")) { jstring jString = t.env->NewStringUTF(string.c_str()); jstring jBaseURL = t.env->NewStringUTF(baseURL.c_str()); - t.env->CallStaticVoidMethod(t.classID, t.methodID, index, jString, jBaseURL); + t.env->CallStaticVoidMethod(t.classID, t.methodID, index, jString, jBaseURL,nullptr); t.env->DeleteLocalRef(jString); t.env->DeleteLocalRef(jBaseURL); From 4597de069de5c9ed17e4551bf643ee3b9cb0cd39 Mon Sep 17 00:00:00 2001 From: andyque Date: Wed, 27 Aug 2014 08:02:22 +0800 Subject: [PATCH 10/14] improve test case and make program more robust --- cocos/CMakeLists.txt | 1 + cocos/ui/WebViewImpl_iOS.mm | 6 ++++-- .../CocoStudioGUITest/UIWebViewTest/UIWebViewTest.cpp | 2 +- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/cocos/CMakeLists.txt b/cocos/CMakeLists.txt index 7bcfe84efb33..90fc4dea0174 100644 --- a/cocos/CMakeLists.txt +++ b/cocos/CMakeLists.txt @@ -86,6 +86,7 @@ list(REMOVE_ITEM cocos2d_source_files "${CMAKE_CURRENT_SOURCE_DIR}/base/CCUserDefaultAndroid.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/ui/UIVideoPlayerAndroid.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/ui/WebView.cpp" + "${CMAKE_CURRENT_SOURCE_DIR}/ui/WebViewImpl_android.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/storage/local-storage/LocalStorageAndroid.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/base/CCEventController.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/base/CCEventListenerController.cpp" diff --git a/cocos/ui/WebViewImpl_iOS.mm b/cocos/ui/WebViewImpl_iOS.mm index 8ef646eef9a0..559b3ba1ae07 100644 --- a/cocos/ui/WebViewImpl_iOS.mm +++ b/cocos/ui/WebViewImpl_iOS.mm @@ -195,7 +195,7 @@ - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *) self.onJsCallback([url UTF8String]); return NO; } - if (self.shouldStartLoading) { + if (self.shouldStartLoading && url) { return self.shouldStartLoading([url UTF8String]); } return YES; @@ -211,7 +211,9 @@ - (void)webViewDidFinishLoad:(UIWebView *)webView { - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { if (self.didFailLoading) { NSString *url = error.userInfo[NSURLErrorFailingURLStringErrorKey]; - self.didFailLoading([url UTF8String]); + if (url) { + self.didFailLoading([url UTF8String]); + } } } diff --git a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIWebViewTest/UIWebViewTest.cpp b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIWebViewTest/UIWebViewTest.cpp index a21c2afba44c..4b643fbd4587 100644 --- a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIWebViewTest/UIWebViewTest.cpp +++ b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIWebViewTest/UIWebViewTest.cpp @@ -125,7 +125,7 @@ bool WebViewTest::init() loadHTMLBtn->setPosition(Vec2(winSize/2) - Vec2( _webView->getContentSize().width/2 + loadHTMLBtn->getContentSize().width/2 + 10,0 )); loadHTMLBtn->addClickEventListener([=](Ref*){ - _webView->loadHTMLString("Hello World",""); + _webView->loadHTMLString("Hello World","text/html"); }); this->addChild(loadHTMLBtn); From 892c8d4455c882d9e88530a800427ec23e5567a0 Mon Sep 17 00:00:00 2001 From: andyque Date: Fri, 29 Aug 2014 10:24:30 +0800 Subject: [PATCH 11/14] add gin0606 as AUTHORS --- AUTHORS | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS b/AUTHORS index 93f56e9cbd74..2740fd5bfc24 100644 --- a/AUTHORS +++ b/AUTHORS @@ -918,6 +918,7 @@ Developers: Fix video scale issue in iOS Fix iOS VideoPlayer memory leak Added c++11 random library support + Added WebView widget which supports iOS and Android billtt Fixed a bug that Node::setScale(float) may not work properly From 043acb2fd46bd42cdcded6bc0bf5bb97c3454f1f Mon Sep 17 00:00:00 2001 From: andyque Date: Fri, 29 Aug 2014 11:28:28 +0800 Subject: [PATCH 12/14] rename webview to UIWebView --- build/cocos2d_libs.xcodeproj/project.pbxproj | 40 +++++++++++-------- cocos/ui/Android.mk | 2 + cocos/ui/CocosGUI.h | 2 +- cocos/ui/{Webview-inl.h => UIWebView-inl.h} | 2 +- cocos/ui/{WebView.mm => UIWebView.cpp} | 4 +- cocos/ui/{WebView.h => UIWebView.h} | 0 cocos/ui/{WebView.cpp => UIWebView.mm} | 4 +- ..._android.cpp => UIWebViewImpl_android.cpp} | 4 +- ...Impl_android.h => UIWebViewImpl_android.h} | 0 ...{WebViewImpl_iOS.h => UIWebViewImpl_iOS.h} | 0 ...ebViewImpl_iOS.mm => UIWebViewImpl_iOS.mm} | 4 +- 11 files changed, 36 insertions(+), 26 deletions(-) rename cocos/ui/{Webview-inl.h => UIWebView-inl.h} (99%) rename cocos/ui/{WebView.mm => UIWebView.cpp} (95%) rename cocos/ui/{WebView.h => UIWebView.h} (100%) rename cocos/ui/{WebView.cpp => UIWebView.mm} (95%) rename cocos/ui/{WebViewImpl_android.cpp => UIWebViewImpl_android.cpp} (99%) rename cocos/ui/{WebViewImpl_android.h => UIWebViewImpl_android.h} (100%) rename cocos/ui/{WebViewImpl_iOS.h => UIWebViewImpl_iOS.h} (100%) rename cocos/ui/{WebViewImpl_iOS.mm => UIWebViewImpl_iOS.mm} (99%) diff --git a/build/cocos2d_libs.xcodeproj/project.pbxproj b/build/cocos2d_libs.xcodeproj/project.pbxproj index dfa251f9db47..d171951a4048 100644 --- a/build/cocos2d_libs.xcodeproj/project.pbxproj +++ b/build/cocos2d_libs.xcodeproj/project.pbxproj @@ -1350,11 +1350,15 @@ 1ABA68B11888D700007D1BB4 /* CCFontCharMap.h in Headers */ = {isa = PBXBuildFile; fileRef = 1ABA68AD1888D700007D1BB4 /* CCFontCharMap.h */; }; 1AC0269C1914068200FA920D /* ConvertUTF.h in Headers */ = {isa = PBXBuildFile; fileRef = 1AC026991914068200FA920D /* ConvertUTF.h */; }; 1AC0269D1914068200FA920D /* ConvertUTF.h in Headers */ = {isa = PBXBuildFile; fileRef = 1AC026991914068200FA920D /* ConvertUTF.h */; }; + 29394CF019B01DBA00D2DE1A /* UIWebView.h in Headers */ = {isa = PBXBuildFile; fileRef = 29394CEC19B01DBA00D2DE1A /* UIWebView.h */; }; + 29394CF119B01DBA00D2DE1A /* UIWebView.h in Headers */ = {isa = PBXBuildFile; fileRef = 29394CEC19B01DBA00D2DE1A /* UIWebView.h */; }; + 29394CF219B01DBA00D2DE1A /* UIWebView.mm in Sources */ = {isa = PBXBuildFile; fileRef = 29394CED19B01DBA00D2DE1A /* UIWebView.mm */; }; + 29394CF319B01DBA00D2DE1A /* UIWebView.mm in Sources */ = {isa = PBXBuildFile; fileRef = 29394CED19B01DBA00D2DE1A /* UIWebView.mm */; }; + 29394CF419B01DBA00D2DE1A /* UIWebViewImpl_iOS.h in Headers */ = {isa = PBXBuildFile; fileRef = 29394CEE19B01DBA00D2DE1A /* UIWebViewImpl_iOS.h */; }; + 29394CF519B01DBA00D2DE1A /* UIWebViewImpl_iOS.h in Headers */ = {isa = PBXBuildFile; fileRef = 29394CEE19B01DBA00D2DE1A /* UIWebViewImpl_iOS.h */; }; + 29394CF619B01DBA00D2DE1A /* UIWebViewImpl_iOS.mm in Sources */ = {isa = PBXBuildFile; fileRef = 29394CEF19B01DBA00D2DE1A /* UIWebViewImpl_iOS.mm */; }; + 29394CF719B01DBA00D2DE1A /* UIWebViewImpl_iOS.mm in Sources */ = {isa = PBXBuildFile; fileRef = 29394CEF19B01DBA00D2DE1A /* UIWebViewImpl_iOS.mm */; }; 2986667F18B1B246000E39CA /* CCTweenFunction.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2986667818B1B079000E39CA /* CCTweenFunction.cpp */; }; - 298D7F6819AC2EFD00FF096D /* WebView.h in Headers */ = {isa = PBXBuildFile; fileRef = 298D7F6019AC2EFD00FF096D /* WebView.h */; }; - 298D7F6919AC2EFD00FF096D /* WebView.mm in Sources */ = {isa = PBXBuildFile; fileRef = 298D7F6119AC2EFD00FF096D /* WebView.mm */; }; - 298D7F6A19AC2EFD00FF096D /* WebViewImpl_iOS.h in Headers */ = {isa = PBXBuildFile; fileRef = 298D7F6219AC2EFD00FF096D /* WebViewImpl_iOS.h */; }; - 298D7F6B19AC2EFD00FF096D /* WebViewImpl_iOS.mm in Sources */ = {isa = PBXBuildFile; fileRef = 298D7F6319AC2EFD00FF096D /* WebViewImpl_iOS.mm */; }; 299754F4193EC95400A54AC3 /* ObjectFactory.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 299754F2193EC95400A54AC3 /* ObjectFactory.cpp */; }; 299754F5193EC95400A54AC3 /* ObjectFactory.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 299754F2193EC95400A54AC3 /* ObjectFactory.cpp */; }; 299754F6193EC95400A54AC3 /* ObjectFactory.h in Headers */ = {isa = PBXBuildFile; fileRef = 299754F3193EC95400A54AC3 /* ObjectFactory.h */; }; @@ -2300,14 +2304,14 @@ 2905FA1318CF08D100240AA3 /* UIWidget.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = UIWidget.cpp; sourceTree = ""; }; 2905FA1418CF08D100240AA3 /* UIWidget.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UIWidget.h; sourceTree = ""; }; 29080DEB191B82CE0066F8DF /* UIDeprecated.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = UIDeprecated.h; sourceTree = ""; }; + 29394CEC19B01DBA00D2DE1A /* UIWebView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UIWebView.h; sourceTree = ""; }; + 29394CED19B01DBA00D2DE1A /* UIWebView.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = UIWebView.mm; sourceTree = ""; }; + 29394CEE19B01DBA00D2DE1A /* UIWebViewImpl_iOS.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UIWebViewImpl_iOS.h; sourceTree = ""; }; + 29394CEF19B01DBA00D2DE1A /* UIWebViewImpl_iOS.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = UIWebViewImpl_iOS.mm; sourceTree = ""; }; 2958244919873D8E00F9746D /* UIScale9Sprite.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = UIScale9Sprite.cpp; sourceTree = ""; }; 2958244A19873D8E00F9746D /* UIScale9Sprite.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UIScale9Sprite.h; sourceTree = ""; }; 2986667818B1B079000E39CA /* CCTweenFunction.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CCTweenFunction.cpp; sourceTree = ""; }; 2986667918B1B079000E39CA /* CCTweenFunction.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCTweenFunction.h; sourceTree = ""; }; - 298D7F6019AC2EFD00FF096D /* WebView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WebView.h; sourceTree = ""; }; - 298D7F6119AC2EFD00FF096D /* WebView.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = WebView.mm; sourceTree = ""; }; - 298D7F6219AC2EFD00FF096D /* WebViewImpl_iOS.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WebViewImpl_iOS.h; sourceTree = ""; }; - 298D7F6319AC2EFD00FF096D /* WebViewImpl_iOS.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = WebViewImpl_iOS.mm; sourceTree = ""; }; 299754F2193EC95400A54AC3 /* ObjectFactory.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = ObjectFactory.cpp; path = ../base/ObjectFactory.cpp; sourceTree = ""; }; 299754F3193EC95400A54AC3 /* ObjectFactory.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ObjectFactory.h; path = ../base/ObjectFactory.h; sourceTree = ""; }; 299CF1F919A434BC00C378C1 /* ccRandom.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = ccRandom.cpp; path = ../base/ccRandom.cpp; sourceTree = ""; }; @@ -4020,10 +4024,10 @@ 29CB8F521929D65500C841D6 /* experimental */ = { isa = PBXGroup; children = ( - 298D7F6019AC2EFD00FF096D /* WebView.h */, - 298D7F6119AC2EFD00FF096D /* WebView.mm */, - 298D7F6219AC2EFD00FF096D /* WebViewImpl_iOS.h */, - 298D7F6319AC2EFD00FF096D /* WebViewImpl_iOS.mm */, + 29394CEC19B01DBA00D2DE1A /* UIWebView.h */, + 29394CED19B01DBA00D2DE1A /* UIWebView.mm */, + 29394CEE19B01DBA00D2DE1A /* UIWebViewImpl_iOS.h */, + 29394CEF19B01DBA00D2DE1A /* UIWebViewImpl_iOS.mm */, 3EA0FB69191C841D00B170C8 /* UIVideoPlayer.h */, 3EA0FB6A191C841D00B170C8 /* UIVideoPlayerIOS.mm */, ); @@ -5043,6 +5047,7 @@ 1A570093180BC5A10088DEC7 /* CCActionTween.h in Headers */, 50ABBD4A1925AB0000A911A9 /* Mat4.h in Headers */, 15AE1A6919AAD40300C27E9E /* b2WorldCallbacks.h in Headers */, + 29394CF419B01DBA00D2DE1A /* UIWebViewImpl_iOS.h in Headers */, 1A57009A180BC5C10088DEC7 /* CCAtlasNode.h in Headers */, 15AE190819AAD35000C27E9E /* CCDatas.h in Headers */, 1A5700A0180BC5D20088DEC7 /* CCNode.h in Headers */, @@ -5240,6 +5245,7 @@ 15AE191019AAD35000C27E9E /* CCInputDelegate.h in Headers */, 15AE184C19AAD30800C27E9E /* SimpleAudioEngine.h in Headers */, 50ABBDA11925AB4100A911A9 /* CCGroupCommand.h in Headers */, + 29394CF019B01DBA00D2DE1A /* UIWebView.h in Headers */, 15AE186519AAD31D00C27E9E /* CDOpenALSupport.h in Headers */, 15AE1B5C19AADA9900C27E9E /* UITextAtlas.h in Headers */, 1A5702FC180BCE750088DEC7 /* CCTMXXMLParser.h in Headers */, @@ -5473,7 +5479,6 @@ 15AE1A4019AAD3D500C27E9E /* b2Collision.h in Headers */, 5034CA40191D591100CE6051 /* ccShader_Position_uColor.vert in Headers */, 15AE184719AAD2F700C27E9E /* CCSprite3DMaterial.h in Headers */, - 298D7F6819AC2EFD00FF096D /* WebView.h in Headers */, 15AE1BFC19AAE01E00C27E9E /* CCControlUtils.h in Headers */, 15AE193519AAD35100C27E9E /* CCActionObject.h in Headers */, 15AE1AA919AAD40300C27E9E /* b2TimeStep.h in Headers */, @@ -5571,6 +5576,7 @@ 15AE1AB319AAD40300C27E9E /* b2CircleContact.h in Headers */, 5034CA2E191D591100CE6051 /* ccShader_PositionTextureA8Color.frag in Headers */, 15AE1A4F19AAD3D500C27E9E /* b2Shape.h in Headers */, + 29394CF519B01DBA00D2DE1A /* UIWebViewImpl_iOS.h in Headers */, 50ABBD5B1925AB0000A911A9 /* Vec2.h in Headers */, 50ABBD411925AB0000A911A9 /* CCMath.h in Headers */, 1A5701A0180BCB590088DEC7 /* CCFont.h in Headers */, @@ -5683,6 +5689,7 @@ 15AE1BAC19AADFDF00C27E9E /* UILayout.h in Headers */, 1A570230180BCC1A0088DEC7 /* CCParticleSystemQuad.h in Headers */, 15AE1AF619AAD42500C27E9E /* chipmunk_ffi.h in Headers */, + 29394CF119B01DBA00D2DE1A /* UIWebView.h in Headers */, 15AE18B419AAD33D00C27E9E /* CCBSelectorResolver.h in Headers */, B24AA988195A675C007B4522 /* CCFastTMXLayer.h in Headers */, 15AE1AB919AAD40300C27E9E /* b2EdgeAndCircleContact.h in Headers */, @@ -5848,7 +5855,6 @@ 15AE1B0919AAD42500C27E9E /* cpPolyShape.h in Headers */, 15AE193919AAD35100C27E9E /* CCArmatureAnimation.h in Headers */, 15AE1AA419AAD40300C27E9E /* b2ContactManager.h in Headers */, - 298D7F6A19AC2EFD00FF096D /* WebViewImpl_iOS.h in Headers */, B276EF601988D1D500CD400F /* CCVertexIndexData.h in Headers */, 50ABBD5F1925AB0000A911A9 /* Vec3.h in Headers */, 50ABBE921925AB6F00A911A9 /* CCPlatformMacros.h in Headers */, @@ -6136,6 +6142,7 @@ 15AE187A19AAD33D00C27E9E /* CCBAnimationManager.cpp in Sources */, 15AE1B6D19AADA9900C27E9E /* UIHelper.cpp in Sources */, 15AE1A8A19AAD40300C27E9E /* b2PulleyJoint.cpp in Sources */, + 29394CF219B01DBA00D2DE1A /* UIWebView.mm in Sources */, 15AE1BD419AAE01E00C27E9E /* CCControlSaturationBrightnessPicker.cpp in Sources */, 1A57019D180BCB590088DEC7 /* CCFont.cpp in Sources */, 1A5701A1180BCB590088DEC7 /* CCFontAtlas.cpp in Sources */, @@ -6301,6 +6308,7 @@ 15AE196E19AAD35700C27E9E /* CCActionTimelineCache.cpp in Sources */, 50ABBEB31925AB6F00A911A9 /* CCUserDefault.mm in Sources */, 50ABBEB51925AB6F00A911A9 /* CCUserDefaultAndroid.cpp in Sources */, + 29394CF619B01DBA00D2DE1A /* UIWebViewImpl_iOS.mm in Sources */, 50ABBE831925AB6F00A911A9 /* ccFPSImages.c in Sources */, 15AE1B7019AADA9900C27E9E /* CocosGUI.cpp in Sources */, 15AE19D019AAD3A700C27E9E /* AttachmentLoader.cpp in Sources */, @@ -6644,6 +6652,7 @@ 50ABBD9C1925AB4100A911A9 /* ccGLStateCache.cpp in Sources */, 1A5701E7180BCB8C0088DEC7 /* CCTransition.cpp in Sources */, 15AE1AC019AAD40300C27E9E /* b2MotorJoint.cpp in Sources */, + 29394CF319B01DBA00D2DE1A /* UIWebView.mm in Sources */, 15AE18BD19AAD33D00C27E9E /* CCLabelBMFontLoader.cpp in Sources */, 50ABC01E1926664800A911A9 /* CCThread.cpp in Sources */, 15AE1B3A19AAD43700C27E9E /* cpBB.c in Sources */, @@ -6686,6 +6695,7 @@ 15AE1BFD19AAE01E00C27E9E /* CCInvocation.cpp in Sources */, B24AA98A195A675C007B4522 /* CCFastTMXTiledMap.cpp in Sources */, 15AE18CE19AAD33D00C27E9E /* CCNodeLoader.cpp in Sources */, + 29394CF719B01DBA00D2DE1A /* UIWebViewImpl_iOS.mm in Sources */, 15AE1AC619AAD40300C27E9E /* b2GearJoint.cpp in Sources */, B24AA986195A675C007B4522 /* CCFastTMXLayer.cpp in Sources */, 1A57022E180BCC1A0088DEC7 /* CCParticleSystemQuad.cpp in Sources */, @@ -6720,7 +6730,6 @@ 1A5702C9180BCE370088DEC7 /* CCTextFieldTTF.cpp in Sources */, 15AE1A0519AAD3A700C27E9E /* Bone.cpp in Sources */, 15AE1B3D19AAD43700C27E9E /* cpCollision.c in Sources */, - 298D7F6919AC2EFD00FF096D /* WebView.mm in Sources */, 15AE1C1719AAE2C700C27E9E /* CCPhysicsSprite.cpp in Sources */, 1A5702EB180BCE750088DEC7 /* CCTileMapAtlas.cpp in Sources */, 1A5702EF180BCE750088DEC7 /* CCTMXLayer.cpp in Sources */, @@ -6773,7 +6782,6 @@ 50ABBE5E1925AB6F00A911A9 /* CCEventListener.cpp in Sources */, 15AE1BC719AAE00000C27E9E /* AssetsManager.cpp in Sources */, 50ABBEA81925AB6F00A911A9 /* CCTouch.cpp in Sources */, - 298D7F6B19AC2EFD00FF096D /* WebViewImpl_iOS.mm in Sources */, 15AE1A9619AAD40300C27E9E /* b2Draw.cpp in Sources */, 503DD8E91926736A00CD74DD /* CCES2Renderer.m in Sources */, 5027253D190BF1B900AAF4ED /* cocos2d.cpp in Sources */, diff --git a/cocos/ui/Android.mk b/cocos/ui/Android.mk index 78bd2c463081..221315e57810 100644 --- a/cocos/ui/Android.mk +++ b/cocos/ui/Android.mk @@ -31,6 +31,8 @@ UIRelativeBox.cpp \ UIVideoPlayerAndroid.cpp \ UIDeprecated.cpp \ UIScale9Sprite.cpp \ +UIWebView.cpp \ +UIWebViewImpl_android.cpp \ LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/.. \ $(LOCAL_PATH)/../editor-support diff --git a/cocos/ui/CocosGUI.h b/cocos/ui/CocosGUI.h index b4516a3512db..04ce980bd54a 100644 --- a/cocos/ui/CocosGUI.h +++ b/cocos/ui/CocosGUI.h @@ -47,7 +47,7 @@ THE SOFTWARE. #include "ui/UIRelativeBox.h" #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS) #include "ui/UIVideoPlayer.h" -#include "ui/WebView.h" +#include "ui/UIWebView.h" #endif #include "ui/UIDeprecated.h" #include "ui/GUIExport.h" diff --git a/cocos/ui/Webview-inl.h b/cocos/ui/UIWebView-inl.h similarity index 99% rename from cocos/ui/Webview-inl.h rename to cocos/ui/UIWebView-inl.h index 835d6829bb9b..26e8eb369f69 100644 --- a/cocos/ui/Webview-inl.h +++ b/cocos/ui/UIWebView-inl.h @@ -23,7 +23,7 @@ ****************************************************************************/ -#include "WebView.h" +#include "UIWebView.h" #include "platform/CCGLView.h" #include "base/CCDirector.h" #include "platform/CCFileUtils.h" diff --git a/cocos/ui/WebView.mm b/cocos/ui/UIWebView.cpp similarity index 95% rename from cocos/ui/WebView.mm rename to cocos/ui/UIWebView.cpp index c2a6b0c9da60..07bb327b0fdf 100644 --- a/cocos/ui/WebView.mm +++ b/cocos/ui/UIWebView.cpp @@ -23,8 +23,8 @@ of this software and associated documentation files (the "Software"), to deal ****************************************************************************/ #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS) -#include "WebViewImpl_iOS.h" -#include "WebView-inl.h" +#include "UIWebViewImpl_android.h" +#include "UIWebView-inl.h" #endif diff --git a/cocos/ui/WebView.h b/cocos/ui/UIWebView.h similarity index 100% rename from cocos/ui/WebView.h rename to cocos/ui/UIWebView.h diff --git a/cocos/ui/WebView.cpp b/cocos/ui/UIWebView.mm similarity index 95% rename from cocos/ui/WebView.cpp rename to cocos/ui/UIWebView.mm index c8d4441d7559..455fbb43e5c3 100644 --- a/cocos/ui/WebView.cpp +++ b/cocos/ui/UIWebView.mm @@ -23,8 +23,8 @@ ****************************************************************************/ #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS) -#include "WebViewImpl_android.h" -#include "WebView-inl.h" +#include "UIWebViewImpl_iOS.h" +#include "UIWebView-inl.h" #endif diff --git a/cocos/ui/WebViewImpl_android.cpp b/cocos/ui/UIWebViewImpl_android.cpp similarity index 99% rename from cocos/ui/WebViewImpl_android.cpp rename to cocos/ui/UIWebViewImpl_android.cpp index 514ddaf64813..53abeb2aaadd 100644 --- a/cocos/ui/WebViewImpl_android.cpp +++ b/cocos/ui/UIWebViewImpl_android.cpp @@ -22,7 +22,7 @@ THE SOFTWARE. ****************************************************************************/ -#include "WebViewImpl_android.h" +#include "UIWebViewImpl_android.h" #include #include @@ -30,7 +30,7 @@ #include "jni/JniHelper.h" #include -#include "WebView.h" +#include "UIWebView.h" #include "platform/CCGLView.h" #include "base/CCDirector.h" #include "platform/CCFileUtils.h" diff --git a/cocos/ui/WebViewImpl_android.h b/cocos/ui/UIWebViewImpl_android.h similarity index 100% rename from cocos/ui/WebViewImpl_android.h rename to cocos/ui/UIWebViewImpl_android.h diff --git a/cocos/ui/WebViewImpl_iOS.h b/cocos/ui/UIWebViewImpl_iOS.h similarity index 100% rename from cocos/ui/WebViewImpl_iOS.h rename to cocos/ui/UIWebViewImpl_iOS.h diff --git a/cocos/ui/WebViewImpl_iOS.mm b/cocos/ui/UIWebViewImpl_iOS.mm similarity index 99% rename from cocos/ui/WebViewImpl_iOS.mm rename to cocos/ui/UIWebViewImpl_iOS.mm index 559b3ba1ae07..116b116975f5 100644 --- a/cocos/ui/WebViewImpl_iOS.mm +++ b/cocos/ui/UIWebViewImpl_iOS.mm @@ -24,13 +24,13 @@ of this software and associated documentation files (the "Software"), to deal #if CC_TARGET_PLATFORM == CC_PLATFORM_IOS -#include "WebViewImpl_iOS.h" +#include "UIWebViewImpl_iOS.h" #include "renderer/CCRenderer.h" #include "CCDirector.h" #include "CCGLView.h" #include "CCEAGLView.h" #include "platform/CCFileUtils.h" -#include "ui/WebView.h" +#include "ui/UIWebView.h" @interface UIWebViewWrapper : NSObject @property (nonatomic) std::function shouldStartLoading; From cf0baef7c0ab8089825c425095d950301f8764f3 Mon Sep 17 00:00:00 2001 From: andyque Date: Fri, 29 Aug 2014 11:50:08 +0800 Subject: [PATCH 13/14] add web view javascript support --- .../android/java/src/org/cocos2dx/lib/Cocos2dxWebView.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxWebView.java b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxWebView.java index 4229a21e0bbd..15a55841b7b9 100755 --- a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxWebView.java +++ b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxWebView.java @@ -6,6 +6,7 @@ import android.annotation.SuppressLint; import android.content.Context; import android.util.Log; +import android.webkit.WebChromeClient; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.FrameLayout; @@ -42,6 +43,7 @@ public Cocos2dxWebView(Context context, int viewTag) { } this.setWebViewClient(new Cocos2dxWebViewClient()); + this.setWebChromeClient(new WebChromeClient()); } public void setJavascriptInterfaceScheme(String scheme) { From 4c046336da2a31128718a4bfec612fe05d98e7be Mon Sep 17 00:00:00 2001 From: andyque Date: Fri, 29 Aug 2014 11:53:39 +0800 Subject: [PATCH 14/14] fix linux --- cocos/CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cocos/CMakeLists.txt b/cocos/CMakeLists.txt index 90fc4dea0174..c454b4f63a8a 100644 --- a/cocos/CMakeLists.txt +++ b/cocos/CMakeLists.txt @@ -85,8 +85,8 @@ list(REMOVE_ITEM cocos2d_source_files "${CMAKE_CURRENT_SOURCE_DIR}/base/CCController-android.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/base/CCUserDefaultAndroid.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/ui/UIVideoPlayerAndroid.cpp" - "${CMAKE_CURRENT_SOURCE_DIR}/ui/WebView.cpp" - "${CMAKE_CURRENT_SOURCE_DIR}/ui/WebViewImpl_android.cpp" + "${CMAKE_CURRENT_SOURCE_DIR}/ui/UIWebView.cpp" + "${CMAKE_CURRENT_SOURCE_DIR}/ui/UIWebViewImpl_android.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/storage/local-storage/LocalStorageAndroid.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/base/CCEventController.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/base/CCEventListenerController.cpp"