2014-08-26 11:39:56 +08:00
|
|
|
/****************************************************************************
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2022-07-09 22:23:34 +08:00
|
|
|
https://axis-project.github.io/
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2014-08-26 11:39:56 +08:00
|
|
|
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:
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2014-08-26 11:39:56 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2014-08-26 11:39:56 +08:00
|
|
|
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"
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
USING_NS_AX;
|
|
|
|
using namespace axis::ui;
|
2015-04-09 12:23:47 +08:00
|
|
|
|
2015-04-03 14:31:03 +08:00
|
|
|
WebViewTests::WebViewTests()
|
2014-08-26 16:53:37 +08:00
|
|
|
{
|
2015-04-03 14:31:03 +08:00
|
|
|
ADD_TEST_CASE(WebViewTest);
|
2014-08-26 16:53:37 +08:00
|
|
|
}
|
|
|
|
|
2014-08-26 11:39:56 +08:00
|
|
|
bool WebViewTest::init()
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
if (UIScene::init())
|
|
|
|
{
|
2014-08-26 22:03:55 +08:00
|
|
|
Size winSize = Director::getInstance()->getVisibleSize();
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
_webView = axis::ui::WebView::create();
|
2021-12-28 16:06:23 +08:00
|
|
|
_webView->setPosition(winSize / 2);
|
2014-08-26 22:03:55 +08:00
|
|
|
_webView->setContentSize(winSize * 0.5);
|
2016-11-30 17:51:57 +08:00
|
|
|
_webView->loadURL("https://www.baidu.com");
|
2014-08-26 12:08:26 +08:00
|
|
|
_webView->setScalesPageToFit(true);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2014-09-15 18:02:11 +08:00
|
|
|
_webView->setOnShouldStartLoading(CC_CALLBACK_2(WebViewTest::onWebViewShouldStartLoading, this));
|
|
|
|
_webView->setOnDidFinishLoading(CC_CALLBACK_2(WebViewTest::onWebViewDidFinishLoading, this));
|
|
|
|
_webView->setOnDidFailLoading(CC_CALLBACK_2(WebViewTest::onWebViewDidFailLoading, this));
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2014-08-26 11:39:56 +08:00
|
|
|
this->addChild(_webView);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2017-06-07 16:12:32 +08:00
|
|
|
auto spriteHello = Sprite::create("Hello.png");
|
2021-12-28 16:06:23 +08:00
|
|
|
spriteHello->setPosition(winSize / 2);
|
2017-06-07 16:12:32 +08:00
|
|
|
this->addChild(spriteHello);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
|
|
|
TextField* urlTextField = TextField::create("Input a URL here", "Arial", 20);
|
2014-08-26 22:03:55 +08:00
|
|
|
urlTextField->setPlaceHolderColor(Color3B::RED);
|
2021-12-28 16:06:23 +08:00
|
|
|
urlTextField->setPosition(Vec2(winSize / 2) + Vec2(-80, _webView->getContentSize().height / 2 +
|
|
|
|
urlTextField->getContentSize().height / 2 + 10));
|
2014-08-26 22:03:55 +08:00
|
|
|
this->addChild(urlTextField);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
|
|
|
Text* httpLabel = Text::create("https:// ", "Arial", 20);
|
2014-08-26 22:03:55 +08:00
|
|
|
httpLabel->setTextColor(Color4B::GREEN);
|
2021-12-28 16:06:23 +08:00
|
|
|
httpLabel->setAnchorPoint(Vec2(1.0, 0.5));
|
|
|
|
httpLabel->setPosition(urlTextField->getPosition() - Vec2(urlTextField->getContentSize().width / 2, 0));
|
2014-08-26 22:03:55 +08:00
|
|
|
this->addChild(httpLabel);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
|
|
|
Button* resetBtn = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png");
|
2014-08-26 22:03:55 +08:00
|
|
|
resetBtn->setTitleText("Visit URL");
|
2021-12-28 16:06:23 +08:00
|
|
|
resetBtn->setPosition(Vec2(winSize / 2) + Vec2(50, _webView->getContentSize().height / 2 +
|
|
|
|
resetBtn->getContentSize().height / 2 + 10));
|
|
|
|
resetBtn->addClickEventListener([=](Ref*) {
|
2015-01-12 16:16:39 +08:00
|
|
|
if (urlTextField->getString().size() != 0)
|
|
|
|
{
|
2021-12-28 17:20:17 +08:00
|
|
|
_webView->loadURL(std::string("https://").append(urlTextField->getString()));
|
2015-01-12 16:16:39 +08:00
|
|
|
}
|
2014-08-26 22:03:55 +08:00
|
|
|
});
|
|
|
|
this->addChild(resetBtn);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
|
|
|
Button* reloadBtn = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png");
|
2014-08-26 22:03:55 +08:00
|
|
|
reloadBtn->setTitleText("Reload");
|
2021-12-28 16:06:23 +08:00
|
|
|
reloadBtn->setPosition(
|
|
|
|
Vec2(winSize / 2) +
|
|
|
|
Vec2(_webView->getContentSize().width / 2 + reloadBtn->getContentSize().width / 2 + 10, 50));
|
|
|
|
reloadBtn->addClickEventListener([=](Ref*) { _webView->reload(); });
|
2014-08-26 22:03:55 +08:00
|
|
|
this->addChild(reloadBtn);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
|
|
|
Button* forwardBtn = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png");
|
2014-08-26 22:03:55 +08:00
|
|
|
forwardBtn->setTitleText("Forward");
|
2021-12-28 16:06:23 +08:00
|
|
|
forwardBtn->setPosition(
|
|
|
|
Vec2(winSize / 2) +
|
|
|
|
Vec2(_webView->getContentSize().width / 2 + forwardBtn->getContentSize().width / 2 + 10, 0));
|
|
|
|
forwardBtn->addClickEventListener([=](Ref*) { _webView->goForward(); });
|
2014-08-26 22:03:55 +08:00
|
|
|
this->addChild(forwardBtn);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
|
|
|
Button* backBtn = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png");
|
2014-08-26 22:03:55 +08:00
|
|
|
backBtn->setTitleText("Back");
|
2021-12-28 16:06:23 +08:00
|
|
|
backBtn->setPosition(
|
|
|
|
Vec2(winSize / 2) +
|
|
|
|
Vec2(_webView->getContentSize().width / 2 + backBtn->getContentSize().width / 2 + 10, -50));
|
|
|
|
backBtn->addClickEventListener([=](Ref*) { _webView->goBack(); });
|
2014-08-26 22:03:55 +08:00
|
|
|
this->addChild(backBtn);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
|
|
|
Button* loadFileBtn = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png");
|
2014-08-26 22:03:55 +08:00
|
|
|
loadFileBtn->setTitleText("Load FILE");
|
2021-12-28 16:06:23 +08:00
|
|
|
loadFileBtn->setPosition(
|
|
|
|
Vec2(winSize / 2) -
|
|
|
|
Vec2(_webView->getContentSize().width / 2 + loadFileBtn->getContentSize().width / 2 + 10, 50));
|
|
|
|
loadFileBtn->addClickEventListener([=](Ref*) { _webView->loadFile("Test.html"); });
|
2014-08-26 22:03:55 +08:00
|
|
|
this->addChild(loadFileBtn);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
|
|
|
Button* loadHTMLBtn = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png");
|
2015-01-23 18:30:12 +08:00
|
|
|
loadHTMLBtn->setTitleText("Load Data");
|
2021-12-28 16:06:23 +08:00
|
|
|
loadHTMLBtn->setPosition(
|
|
|
|
Vec2(winSize / 2) -
|
|
|
|
Vec2(_webView->getContentSize().width / 2 + loadHTMLBtn->getContentSize().width / 2 + 10, 0));
|
|
|
|
loadHTMLBtn->addClickEventListener([=](Ref*) {
|
|
|
|
_webView->loadHTMLString("<body style=\"font-size:50px;\">Hello World <img src=\"Icon.png\"/> </body>",
|
|
|
|
"Images/");
|
2014-08-26 22:03:55 +08:00
|
|
|
});
|
|
|
|
this->addChild(loadHTMLBtn);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
|
|
|
Button* evalJsBtn = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png");
|
2014-08-26 22:03:55 +08:00
|
|
|
evalJsBtn->setTitleText("Evaluate JS");
|
2021-12-28 16:06:23 +08:00
|
|
|
evalJsBtn->setPosition(
|
|
|
|
Vec2(winSize / 2) -
|
|
|
|
Vec2(_webView->getContentSize().width / 2 + evalJsBtn->getContentSize().width / 2 + 10, -50));
|
|
|
|
evalJsBtn->addClickEventListener([=](Ref*) { _webView->evaluateJS("alert(\"hello\")"); });
|
2015-09-23 11:22:36 +08:00
|
|
|
evalJsBtn->setName("evalJs");
|
2014-08-26 22:03:55 +08:00
|
|
|
this->addChild(evalJsBtn);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
|
|
|
Button* opacityBtn = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png");
|
2017-06-07 16:12:32 +08:00
|
|
|
opacityBtn->setTitleText("Opacity 1.f");
|
2021-12-28 16:06:23 +08:00
|
|
|
opacityBtn->setPosition(
|
|
|
|
Vec2(winSize / 2) -
|
|
|
|
Vec2(_webView->getContentSize().width / 2 + opacityBtn->getContentSize().width / 2 + 10, 100));
|
|
|
|
opacityBtn->addClickEventListener([=](Ref*) {
|
2017-06-07 16:12:32 +08:00
|
|
|
auto currentOpacity = _webView->getOpacityWebView();
|
2021-12-28 16:06:23 +08:00
|
|
|
if (currentOpacity == 1.f)
|
|
|
|
{
|
2017-06-07 16:12:32 +08:00
|
|
|
_webView->setOpacityWebView(.5f);
|
|
|
|
opacityBtn->setTitleText("Opacity .5f");
|
2021-12-28 16:06:23 +08:00
|
|
|
}
|
|
|
|
else if (currentOpacity == .5f)
|
|
|
|
{
|
2017-06-07 16:12:32 +08:00
|
|
|
_webView->setOpacityWebView(0);
|
|
|
|
opacityBtn->setTitleText("Opacity 0.f");
|
2021-12-28 16:06:23 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-06-07 16:12:32 +08:00
|
|
|
_webView->setOpacityWebView(1.f);
|
|
|
|
opacityBtn->setTitleText("Opacity 1.f");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
opacityBtn->setName("Opacity");
|
|
|
|
this->addChild(opacityBtn);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
|
|
|
Button* transparentBgBtn =
|
|
|
|
Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png");
|
2017-06-07 16:12:32 +08:00
|
|
|
transparentBgBtn->setTitleText("Transparent BG");
|
2021-12-28 16:06:23 +08:00
|
|
|
transparentBgBtn->setPosition(
|
|
|
|
Vec2(winSize / 2) +
|
|
|
|
Vec2(_webView->getContentSize().width / 2 + transparentBgBtn->getContentSize().width / 2 + 10, -100));
|
|
|
|
transparentBgBtn->addClickEventListener([=](Ref*) { _webView->setBackgroundTransparent(); });
|
2017-06-07 16:12:32 +08:00
|
|
|
transparentBgBtn->setName("Transparent");
|
|
|
|
this->addChild(transparentBgBtn);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2014-08-26 11:39:56 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2014-08-26 16:53:37 +08:00
|
|
|
}
|
2014-08-26 22:03:55 +08:00
|
|
|
|
2021-12-28 17:20:17 +08:00
|
|
|
bool WebViewTest::onWebViewShouldStartLoading(ui::WebView* sender, std::string_view url)
|
2014-08-26 22:03:55 +08:00
|
|
|
{
|
2021-12-28 17:20:17 +08:00
|
|
|
CCLOG("onWebViewShouldStartLoading, url is %s", url.data());
|
2021-12-28 16:06:23 +08:00
|
|
|
// don't do any OpenGL operation here!! It's forbidden!
|
2014-08-26 22:03:55 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-12-28 17:20:17 +08:00
|
|
|
void WebViewTest::onWebViewDidFinishLoading(ui::WebView* sender, std::string_view url)
|
2014-08-26 22:03:55 +08:00
|
|
|
{
|
2015-09-23 11:22:36 +08:00
|
|
|
auto node = (ui::Button*)this->getChildByName("evalJs");
|
|
|
|
node->setTitleText("start loading...");
|
2021-12-28 17:20:17 +08:00
|
|
|
CCLOG("onWebViewDidFinishLoading, url is %s", url.data());
|
2014-08-26 22:03:55 +08:00
|
|
|
}
|
|
|
|
|
2021-12-28 17:20:17 +08:00
|
|
|
void WebViewTest::onWebViewDidFailLoading(ui::WebView* sender, std::string_view url)
|
2014-08-26 22:03:55 +08:00
|
|
|
{
|
2021-12-28 17:20:17 +08:00
|
|
|
CCLOG("onWebViewDidFailLoading, url is %s", url.data());
|
2014-08-26 22:03:55 +08:00
|
|
|
}
|