39 Size winSize = Director::getInstance()->getVisibleSize();
42 _webView = cocos2d::ui::WebView::create();
44 _webView->setContentSize(winSize * 0.5);
45 _webView->loadURL(
"https://www.baidu.com");
54 auto spriteHello = Sprite::create(
"Hello.png");
55 spriteHello->setPosition(winSize/2);
56 this->addChild(spriteHello);
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);
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);
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)
79 _webView->loadURL(std::string(
"https://") + urlTextField->getString());
82 this->addChild(resetBtn);
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*){
93 this->addChild(reloadBtn);
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*){
103 this->addChild(forwardBtn);
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*){
115 this->addChild(backBtn);
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*){
126 this->addChild(loadFileBtn);
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/");
136 this->addChild(loadHTMLBtn);
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\")");
149 evalJsBtn->setName(
"evalJs");
150 this->addChild(evalJsBtn);
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){
161 opacityBtn->setTitleText(
"Opacity .5f");
162 }
else if(currentOpacity == .5f){
164 opacityBtn->setTitleText(
"Opacity 0.f");
167 opacityBtn->setTitleText(
"Opacity 1.f");
171 opacityBtn->setName(
"Opacity");
172 this->addChild(opacityBtn);
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();
183 transparentBgBtn->setName(
"Transparent");
184 this->addChild(transparentBgBtn);
virtual bool init() override
bool onWebViewShouldStartLoading(cocos2d::ui::WebView *sender, const std::string &url)
cocos2d::ui::WebView * _webView
void onWebViewDidFailLoading(cocos2d::ui::WebView *sender, const std::string &url)
void onWebViewDidFinishLoading(cocos2d::ui::WebView *sender, const std::string &url)