From 72ad341d2474fa584227ae1bf878a9122443ca4c Mon Sep 17 00:00:00 2001 From: Undume Date: Wed, 7 Jun 2017 10:12:32 +0200 Subject: [PATCH] Webview opacity and background transparency (#17831) * Added three new methods for UiWebView. SetOpacityWebView(float), getOpacityWebView(), setBackgroundTransparent() in IOS * Implemented methods for UiWebView. SetOpacityWebView(float), getOpacityWebView(). They only work using android sdk +11. * New methods not implemented for Tizen. * Added new button for testing the opacity in the cocos2d-x test application. * Added setBackgroundTransparent to android, and his UI-Test. * Added setBackgroundTransparent to android, and his UI-Test. * Added keyword const to the getters. * Android sdk reflection. +11sdk for chaging the opacity. * Reflection used for the webview.setLayerType, webview.setAlpha, webview.getAlpha * Unified exceptions. --- .../cocos2dx/lib/Cocos2dxWebViewHelper.java | 76 +++++++++++++++++++ cocos/ui/UIWebView-inl.h | 13 ++++ cocos/ui/UIWebView.h | 14 ++++ cocos/ui/UIWebViewImpl-android.cpp | 13 ++++ cocos/ui/UIWebViewImpl-android.h | 6 ++ cocos/ui/UIWebViewImpl-ios.h | 6 ++ cocos/ui/UIWebViewImpl-ios.mm | 34 +++++++++ cocos/ui/UIWebViewImpl-tizen.cpp | 12 +++ cocos/ui/UIWebViewImpl-tizen.h | 6 ++ .../UIWebViewTest/UIWebViewTest.cpp | 38 ++++++++++ 10 files changed, 218 insertions(+) diff --git a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxWebViewHelper.java b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxWebViewHelper.java index fc6beb355c..9e93149b6d 100755 --- a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxWebViewHelper.java +++ b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxWebViewHelper.java @@ -1,12 +1,19 @@ package org.cocos2dx.lib; +import android.annotation.TargetApi; +import android.graphics.Color; +import android.graphics.Paint; import android.os.Handler; import android.os.Looper; +import android.util.Log; import android.util.SparseArray; import android.view.View; +import android.webkit.WebView; import android.widget.FrameLayout; import android.webkit.WebSettings; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.FutureTask; @@ -95,6 +102,75 @@ public class Cocos2dxWebViewHelper { }); } + public static void setBackgroundTransparent(final int index) { + if(android.os.Build.VERSION.SDK_INT >10) { + sCocos2dxActivity.runOnUiThread(new Runnable() { + @Override + public void run() { + Cocos2dxWebView webView = webViews.get(index); + if (webView != null) { + webView.setBackgroundColor(Color.TRANSPARENT); + try { + Method method = webView.getClass().getMethod("setLayerType",int.class,Paint.class); + method.invoke(webView,WebView.LAYER_TYPE_SOFTWARE,null); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + }); + } + } + + public static void setOpacityWebView(final int index, final float opacity) { + if(android.os.Build.VERSION.SDK_INT >10){ + sCocos2dxActivity.runOnUiThread(new Runnable() { + @Override + public void run() { + Cocos2dxWebView webView = webViews.get(index); + if (webView != null) { + try { + Method method = webView.getClass().getMethod("setAlpha",float.class); + method.invoke(webView,opacity); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + }); + } + } + + + public static float getOpacityWebView(final int index) { + if(android.os.Build.VERSION.SDK_INT >10){ + FutureTask futureResult = new FutureTask(new Callable() { + @Override + public Float call() throws Exception { + float opacity=0.f; + Cocos2dxWebView webView = webViews.get(index); + Object valueToReturn=null; + if (webView != null) { + try { + Method method = webView.getClass().getMethod("getAlpha"); + valueToReturn = method.invoke(webView); + } catch (Exception e) { + e.printStackTrace(); + } + } + return (Float) valueToReturn; + } + }); + sCocos2dxActivity.runOnUiThread(futureResult); + try { + return futureResult.get(); + } catch (Exception e) { + e.printStackTrace(); + } + } + return 1; + } + public static void setWebViewRect(final int index, final int left, final int top, final int maxWidth, final int maxHeight) { sCocos2dxActivity.runOnUiThread(new Runnable() { @Override diff --git a/cocos/ui/UIWebView-inl.h b/cocos/ui/UIWebView-inl.h index 93c7c842b8..18e086ec3c 100644 --- a/cocos/ui/UIWebView-inl.h +++ b/cocos/ui/UIWebView-inl.h @@ -147,6 +147,19 @@ namespace experimental{ _impl->setVisible(visible); } } + + void WebView::setOpacityWebView(float opacity){ + _impl->setOpacityWebView(opacity); + } + + float WebView::getOpacityWebView() const{ + return _impl->getOpacityWebView(); + } + + void WebView::setBackgroundTransparent() + { + _impl->setBackgroundTransparent(); + }; void WebView::onEnter() { diff --git a/cocos/ui/UIWebView.h b/cocos/ui/UIWebView.h index 448a166e02..13ebc41103 100644 --- a/cocos/ui/UIWebView.h +++ b/cocos/ui/UIWebView.h @@ -216,6 +216,20 @@ public: * Toggle visibility of WebView. */ virtual void setVisible(bool visible) override; + /** + * SetOpacity of webview. + */ + virtual void setOpacityWebView(float opacity); + + /** + * getOpacity of webview. + */ + virtual float getOpacityWebView() const; + + /** + * set the background transparent + */ + virtual void setBackgroundTransparent(); virtual void onEnter() override; virtual void onExit() override; diff --git a/cocos/ui/UIWebViewImpl-android.cpp b/cocos/ui/UIWebViewImpl-android.cpp index 5f52d624ae..5fa3fc3376 100644 --- a/cocos/ui/UIWebViewImpl-android.cpp +++ b/cocos/ui/UIWebViewImpl-android.cpp @@ -285,6 +285,19 @@ namespace cocos2d { void WebViewImpl::setVisible(bool visible) { JniHelper::callStaticVoidMethod(className, "setVisible", _viewTag, visible); } + + void WebViewImpl::setOpacityWebView(const float opacity){ + JniHelper::callStaticVoidMethod(className, "setOpacityWebView", _viewTag, opacity); + }; + + + float WebViewImpl::getOpacityWebView()const{ + return JniHelper::callStaticFloatMethod(className, "getOpacityWebView", _viewTag); + }; + + void WebViewImpl::setBackgroundTransparent(){ + JniHelper::callStaticVoidMethod(className, "setBackgroundTransparent", _viewTag); + }; void WebViewImpl::setBounces(bool bounces) { // empty function as this was mainly a fix for iOS diff --git a/cocos/ui/UIWebViewImpl-android.h b/cocos/ui/UIWebViewImpl-android.h index 5440305514..39cca8307a 100644 --- a/cocos/ui/UIWebViewImpl-android.h +++ b/cocos/ui/UIWebViewImpl-android.h @@ -85,6 +85,12 @@ namespace cocos2d { virtual void setVisible(bool visible); void setBounces(bool bounces); + + void setOpacityWebView(float opacity); + + float getOpacityWebView()const; + + void setBackgroundTransparent(); static bool shouldStartLoading(const int viewTag, const std::string &url); static void didFinishLoading(const int viewTag, const std::string &url); diff --git a/cocos/ui/UIWebViewImpl-ios.h b/cocos/ui/UIWebViewImpl-ios.h index 33e09ed62f..a4c3c10ede 100644 --- a/cocos/ui/UIWebViewImpl-ios.h +++ b/cocos/ui/UIWebViewImpl-ios.h @@ -83,6 +83,12 @@ public: virtual void setVisible(bool visible); void setBounces(bool bounces); + + virtual void setOpacityWebView(float opacity); + + virtual float getOpacityWebView() const; + + virtual void setBackgroundTransparent(); private: UIWebViewWrapper *_uiWebViewWrapper; diff --git a/cocos/ui/UIWebViewImpl-ios.mm b/cocos/ui/UIWebViewImpl-ios.mm index 1d7024b445..67f7087035 100644 --- a/cocos/ui/UIWebViewImpl-ios.mm +++ b/cocos/ui/UIWebViewImpl-ios.mm @@ -74,6 +74,12 @@ static std::string getFixedBaseUrl(const std::string& baseUrl) - (void)setBounces:(bool)bounces; +- (void)setOpacityWebView:(float)opacity; + +- (float)getOpacityWebView; + +- (void)setBackgroundTransparent; + - (void)setFrameWithX:(float)x y:(float)y width:(float)width height:(float)height; - (void)setJavascriptInterfaceScheme:(const std::string &)scheme; @@ -152,6 +158,19 @@ static std::string getFixedBaseUrl(const std::string& baseUrl) self.uiWebView.scrollView.bounces = bounces; } +- (void)setOpacityWebView:(float)opacity { + self.uiWebView.alpha=opacity; + [self.uiWebView setOpaque:NO]; +} + +-(float) getOpacityWebView{ + return self.uiWebView.alpha; +} + +-(void) setBackgroundTransparent{ + [self.uiWebView setBackgroundColor:[UIColor clearColor]]; +} + - (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); @@ -232,6 +251,8 @@ static std::string getFixedBaseUrl(const std::string& baseUrl) self.uiWebView.scalesPageToFit = scalesPageToFit; } + + #pragma mark - UIWebViewDelegate - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { NSString *url = [[request URL] absoluteString]; @@ -398,6 +419,19 @@ void WebViewImpl::setVisible(bool visible){ [_uiWebViewWrapper setVisible:visible]; } +void WebViewImpl::setOpacityWebView(float opacity){ + [_uiWebViewWrapper setOpacityWebView: opacity]; +} + +float WebViewImpl::getOpacityWebView() const{ + return [_uiWebViewWrapper getOpacityWebView]; +} + +void WebViewImpl::setBackgroundTransparent(){ + [_uiWebViewWrapper setBackgroundTransparent]; +} + + } // namespace ui } // namespace experimental } //namespace cocos2d diff --git a/cocos/ui/UIWebViewImpl-tizen.cpp b/cocos/ui/UIWebViewImpl-tizen.cpp index dfa2e83066..5967dfa5eb 100644 --- a/cocos/ui/UIWebViewImpl-tizen.cpp +++ b/cocos/ui/UIWebViewImpl-tizen.cpp @@ -163,6 +163,18 @@ namespace cocos2d { void WebViewImpl::setBounces(bool bounces) { // empty function as this was mainly a fix for iOS } + + void WebViewImpl::setOpacityWebView(float opacity){ + //TODO + } + + float WebViewImpl::getOpacityWebView() const{ + //TODO + } + + void WebViewImpl::setBackgroundTransparent(){ + //TODO + } } // namespace ui } // namespace experimental } //namespace cocos2d diff --git a/cocos/ui/UIWebViewImpl-tizen.h b/cocos/ui/UIWebViewImpl-tizen.h index b63eee00d9..969f98ae54 100644 --- a/cocos/ui/UIWebViewImpl-tizen.h +++ b/cocos/ui/UIWebViewImpl-tizen.h @@ -87,6 +87,12 @@ namespace cocos2d { void setBounces(bool bounces); + void setOpacityWebView(float opacity); + + float getOpacityWebView() const; + + void setBackgroundTransparent; + Evas_Object* _ewkWin; Evas_Object* _ewkView; private: diff --git a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIWebViewTest/UIWebViewTest.cpp b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIWebViewTest/UIWebViewTest.cpp index b28513eed7..ab81ffc8a3 100644 --- a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIWebViewTest/UIWebViewTest.cpp +++ b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIWebViewTest/UIWebViewTest.cpp @@ -50,6 +50,10 @@ bool WebViewTest::init() this->addChild(_webView); + auto spriteHello = Sprite::create("Hello.png"); + spriteHello->setPosition(winSize/2); + this->addChild(spriteHello); + 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 + @@ -144,6 +148,40 @@ bool WebViewTest::init() evalJsBtn->setName("evalJs"); this->addChild(evalJsBtn); + Button *opacityBtn = Button::create("cocosui/animationbuttonnormal.png", + "cocosui/animationbuttonpressed.png"); + opacityBtn->setTitleText("Opacity 1.f"); + opacityBtn->setPosition(Vec2(winSize/2) - Vec2( _webView->getContentSize().width/2 + + opacityBtn->getContentSize().width/2 + 10, 100 )); + opacityBtn->addClickEventListener([=](Ref*){ + auto currentOpacity = _webView->getOpacityWebView(); + if(currentOpacity ==1.f){ + _webView->setOpacityWebView(.5f); + opacityBtn->setTitleText("Opacity .5f"); + }else if(currentOpacity == .5f){ + _webView->setOpacityWebView(0); + opacityBtn->setTitleText("Opacity 0.f"); + }else{ + _webView->setOpacityWebView(1.f); + opacityBtn->setTitleText("Opacity 1.f"); + } + + }); + opacityBtn->setName("Opacity"); + this->addChild(opacityBtn); + + + Button *transparentBgBtn = Button::create("cocosui/animationbuttonnormal.png", + "cocosui/animationbuttonpressed.png"); + transparentBgBtn->setTitleText("Transparent BG"); + transparentBgBtn->setPosition(Vec2(winSize/2) + Vec2( _webView->getContentSize().width/2 + + transparentBgBtn->getContentSize().width/2 + 10,-100 )); + transparentBgBtn->addClickEventListener([=](Ref*){ + _webView->setBackgroundTransparent(); + }); + transparentBgBtn->setName("Transparent"); + this->addChild(transparentBgBtn); + return true; } return false;