PuzzleSDK
UIWebViewTest.cpp
浏览该文件的文档.
1/****************************************************************************
2 Copyright (c) 2013-2016 Chukong Technologies Inc.
3 Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
4
5 http://www.cocos2d-x.org
6
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13
14 The above copyright notice and this permission notice shall be included in
15 all copies or substantial portions of the Software.
16
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 THE SOFTWARE.
24 ****************************************************************************/
25
26#include "UIWebViewTest.h"
27
29using namespace cocos2d::ui;
30
31WebViewTests::WebViewTests()
32{
34}
35
37{
38 if (UIScene::init()) {
39 Size winSize = Director::getInstance()->getVisibleSize();
40
41
42 _webView = cocos2d::ui::WebView::create();
43 _webView->setPosition(winSize/2);
44 _webView->setContentSize(winSize * 0.5);
45 _webView->loadURL("https://www.baidu.com");
46 _webView->setScalesPageToFit(true);
47
48 _webView->setOnShouldStartLoading(CC_CALLBACK_2(WebViewTest::onWebViewShouldStartLoading, this));
49 _webView->setOnDidFinishLoading(CC_CALLBACK_2(WebViewTest::onWebViewDidFinishLoading, this));
50 _webView->setOnDidFailLoading(CC_CALLBACK_2(WebViewTest::onWebViewDidFailLoading, this));
51
52 this->addChild(_webView);
53
54 auto spriteHello = Sprite::create("Hello.png");
55 spriteHello->setPosition(winSize/2);
56 this->addChild(spriteHello);
57
58 TextField *urlTextField = TextField::create("Input a URL here", "Arial", 20);
59 urlTextField->setPlaceHolderColor(Color3B::RED);
60 urlTextField->setPosition(Vec2(winSize/2) + Vec2(-80, _webView->getContentSize().height/2 +
61 urlTextField->getContentSize().height/2 + 10));
62 this->addChild(urlTextField);
63
64 Text *httpLabel = Text::create("https:// ", "Arial", 20);
65 httpLabel->setTextColor(Color4B::GREEN);
66 httpLabel->setAnchorPoint(Vec2(1.0,0.5));
67 httpLabel->setPosition(urlTextField->getPosition() - Vec2(urlTextField->getContentSize().width/2,0));
68 this->addChild(httpLabel);
69
70
71 Button *resetBtn = Button::create("cocosui/animationbuttonnormal.png",
72 "cocosui/animationbuttonpressed.png");
73 resetBtn->setTitleText("Visit URL");
74 resetBtn->setPosition(Vec2(winSize/2) + Vec2(50, _webView->getContentSize().height/2 +
75 resetBtn->getContentSize().height/2 + 10));
76 resetBtn->addClickEventListener([=](Ref*){
77 if (urlTextField->getString().size() != 0)
78 {
79 _webView->loadURL(std::string("https://") + urlTextField->getString());
80 }
81 });
82 this->addChild(resetBtn);
83
84
85 Button *reloadBtn = Button::create("cocosui/animationbuttonnormal.png",
86 "cocosui/animationbuttonpressed.png");
87 reloadBtn->setTitleText("Reload");
88 reloadBtn->setPosition(Vec2(winSize/2) + Vec2( _webView->getContentSize().width/2 +
89 reloadBtn->getContentSize().width/2 + 10,50 ));
90 reloadBtn->addClickEventListener([=](Ref*){
91 _webView->reload();
92 });
93 this->addChild(reloadBtn);
94
95 Button *forwardBtn = Button::create("cocosui/animationbuttonnormal.png",
96 "cocosui/animationbuttonpressed.png");
97 forwardBtn->setTitleText("Forward");
98 forwardBtn->setPosition(Vec2(winSize/2) + Vec2( _webView->getContentSize().width/2 +
99 forwardBtn->getContentSize().width/2 + 10,0 ));
100 forwardBtn->addClickEventListener([=](Ref*){
101 _webView->goForward();
102 });
103 this->addChild(forwardBtn);
104
105
106
107 Button *backBtn = Button::create("cocosui/animationbuttonnormal.png",
108 "cocosui/animationbuttonpressed.png");
109 backBtn->setTitleText("Back");
110 backBtn->setPosition(Vec2(winSize/2) + Vec2( _webView->getContentSize().width/2 +
111 backBtn->getContentSize().width/2 + 10,-50 ));
112 backBtn->addClickEventListener([=](Ref*){
113 _webView->goBack();
114 });
115 this->addChild(backBtn);
116
117
118 Button *loadFileBtn = Button::create("cocosui/animationbuttonnormal.png",
119 "cocosui/animationbuttonpressed.png");
120 loadFileBtn->setTitleText("Load FILE");
121 loadFileBtn->setPosition(Vec2(winSize/2) - Vec2( _webView->getContentSize().width/2 +
122 loadFileBtn->getContentSize().width/2 + 10,50 ));
123 loadFileBtn->addClickEventListener([=](Ref*){
124 _webView->loadFile("Test.html");
125 });
126 this->addChild(loadFileBtn);
127
128 Button *loadHTMLBtn = Button::create("cocosui/animationbuttonnormal.png",
129 "cocosui/animationbuttonpressed.png");
130 loadHTMLBtn->setTitleText("Load Data");
131 loadHTMLBtn->setPosition(Vec2(winSize/2) - Vec2( _webView->getContentSize().width/2 +
132 loadHTMLBtn->getContentSize().width/2 + 10,0 ));
133 loadHTMLBtn->addClickEventListener([=](Ref*){
134 _webView->loadHTMLString("<body style=\"font-size:50px;\">Hello World <img src=\"Icon.png\"/> </body>","Images/");
135 });
136 this->addChild(loadHTMLBtn);
137
138
139
140
141 Button *evalJsBtn = Button::create("cocosui/animationbuttonnormal.png",
142 "cocosui/animationbuttonpressed.png");
143 evalJsBtn->setTitleText("Evaluate JS");
144 evalJsBtn->setPosition(Vec2(winSize/2) - Vec2( _webView->getContentSize().width/2 +
145 evalJsBtn->getContentSize().width/2 + 10,-50 ));
146 evalJsBtn->addClickEventListener([=](Ref*){
147 _webView->evaluateJS("alert(\"hello\")");
148 });
149 evalJsBtn->setName("evalJs");
150 this->addChild(evalJsBtn);
151
152 Button *opacityBtn = Button::create("cocosui/animationbuttonnormal.png",
153 "cocosui/animationbuttonpressed.png");
154 opacityBtn->setTitleText("Opacity 1.f");
155 opacityBtn->setPosition(Vec2(winSize/2) - Vec2( _webView->getContentSize().width/2 +
156 opacityBtn->getContentSize().width/2 + 10, 100 ));
157 opacityBtn->addClickEventListener([=](Ref*){
158 auto currentOpacity = _webView->getOpacityWebView();
159 if(currentOpacity ==1.f){
160 _webView->setOpacityWebView(.5f);
161 opacityBtn->setTitleText("Opacity .5f");
162 }else if(currentOpacity == .5f){
163 _webView->setOpacityWebView(0);
164 opacityBtn->setTitleText("Opacity 0.f");
165 }else{
166 _webView->setOpacityWebView(1.f);
167 opacityBtn->setTitleText("Opacity 1.f");
168 }
169
170 });
171 opacityBtn->setName("Opacity");
172 this->addChild(opacityBtn);
173
174
175 Button *transparentBgBtn = Button::create("cocosui/animationbuttonnormal.png",
176 "cocosui/animationbuttonpressed.png");
177 transparentBgBtn->setTitleText("Transparent BG");
178 transparentBgBtn->setPosition(Vec2(winSize/2) + Vec2( _webView->getContentSize().width/2 +
179 transparentBgBtn->getContentSize().width/2 + 10,-100 ));
180 transparentBgBtn->addClickEventListener([=](Ref*){
181 _webView->setBackgroundTransparent();
182 });
183 transparentBgBtn->setName("Transparent");
184 this->addChild(transparentBgBtn);
185
186 return true;
187 }
188 return false;
189}
190
191bool WebViewTest::onWebViewShouldStartLoading(ui::WebView *sender, const std::string &url)
192{
193 CCLOG("onWebViewShouldStartLoading, url is %s", url.c_str());
194 //don't do any OpenGL operation here!! It's forbidden!
195 return true;
196}
197
198void WebViewTest::onWebViewDidFinishLoading(ui::WebView *sender, const std::string &url)
199{
200 auto node = (ui::Button*)this->getChildByName("evalJs");
201 node->setTitleText("start loading...");
202 CCLOG("onWebViewDidFinishLoading, url is %s", url.c_str());
203}
204
205void WebViewTest::onWebViewDidFailLoading(ui::WebView *sender, const std::string &url)
206{
207 CCLOG("onWebViewDidFailLoading, url is %s", url.c_str());
208}
209
#define ADD_TEST_CASE(__className__)
Definition: BaseTest.h:211
USING_NS_CC
virtual bool init() override
Definition: UIScene.cpp:46
bool onWebViewShouldStartLoading(cocos2d::ui::WebView *sender, const std::string &url)
virtual bool init() override
cocos2d::ui::WebView * _webView
Definition: UIWebViewTest.h:45
void onWebViewDidFailLoading(cocos2d::ui::WebView *sender, const std::string &url)
void onWebViewDidFinishLoading(cocos2d::ui::WebView *sender, const std::string &url)