Merge pull request #15651 from zilongshanren/fixNativeCotnrolIssuesV2

fix UI native control onEnter issue
This commit is contained in:
zilongshanren 2016-05-16 16:58:32 +08:00
commit 9203630583
4 changed files with 105 additions and 90 deletions

View File

@ -234,22 +234,25 @@ void VideoPlayer::setVisible(bool visible)
{ {
cocos2d::ui::Widget::setVisible(visible); cocos2d::ui::Widget::setVisible(visible);
if (! _videoURL.empty()) if (!visible || isRunning())
{ {
JniHelper::callStaticVoidMethod(videoHelperClassName, "setVideoVisible", _videoPlayerIndex, visible); JniHelper::callStaticVoidMethod(videoHelperClassName, "setVideoVisible", _videoPlayerIndex, visible);
} }
} }
void VideoPlayer::onEnter() void VideoPlayer::onEnter()
{ {
Widget::onEnter(); Widget::onEnter();
this->setVisible(true); if (isVisible() && !_videoURL.empty())
{
JniHelper::callStaticVoidMethod(videoHelperClassName, "setVideoVisible", _videoPlayerIndex, true);
}
} }
void VideoPlayer::onExit() void VideoPlayer::onExit()
{ {
Widget::onExit(); Widget::onExit();
this->setVisible(false); JniHelper::callStaticVoidMethod(videoHelperClassName, "setVideoVisible", _videoPlayerIndex, false);
} }
void VideoPlayer::addEventListener(const VideoPlayer::ccVideoPlayerCallback& callback) void VideoPlayer::addEventListener(const VideoPlayer::ccVideoPlayerCallback& callback)

View File

@ -46,10 +46,10 @@ using namespace cocos2d::experimental::ui;
- (void) resume; - (void) resume;
- (void) stop; - (void) stop;
- (void) seekTo:(float) sec; - (void) seekTo:(float) sec;
- (void) setVisible:(bool) visible; - (void) setVisible:(BOOL) visible;
- (void) setKeepRatioEnabled:(bool) enabled; - (void) setKeepRatioEnabled:(BOOL) enabled;
- (void) setFullScreenEnabled:(bool) enabled; - (void) setFullScreenEnabled:(BOOL) enabled;
- (bool) isFullScreenEnabled; - (BOOL) isFullScreenEnabled;
-(id) init:(void*) videoPlayer; -(id) init:(void*) videoPlayer;
@ -66,7 +66,7 @@ using namespace cocos2d::experimental::ui;
int _width; int _width;
int _height; int _height;
bool _keepRatioEnabled; bool _keepRatioEnabled;
VideoPlayer* _videoPlayer; VideoPlayer* _videoPlayer;
} }
@ -77,7 +77,7 @@ using namespace cocos2d::experimental::ui;
_videoPlayer = (VideoPlayer*)videoPlayer; _videoPlayer = (VideoPlayer*)videoPlayer;
_keepRatioEnabled = false; _keepRatioEnabled = false;
} }
return self; return self;
} }
@ -86,7 +86,7 @@ using namespace cocos2d::experimental::ui;
if (self.moviePlayer != nullptr) { if (self.moviePlayer != nullptr) {
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer]; [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackStateDidChangeNotification object:self.moviePlayer]; [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackStateDidChangeNotification object:self.moviePlayer];
[self.moviePlayer stop]; [self.moviePlayer stop];
[self.moviePlayer.view removeFromSuperview]; [self.moviePlayer.view removeFromSuperview];
self.moviePlayer = nullptr; self.moviePlayer = nullptr;
@ -106,19 +106,19 @@ using namespace cocos2d::experimental::ui;
} }
} }
-(void) setFullScreenEnabled:(bool) enabled -(void) setFullScreenEnabled:(BOOL) enabled
{ {
if (self.moviePlayer != nullptr) { if (self.moviePlayer != nullptr) {
[self.moviePlayer setFullscreen:enabled animated:(true)]; [self.moviePlayer setFullscreen:enabled animated:(true)];
} }
} }
-(bool) isFullScreenEnabled -(BOOL) isFullScreenEnabled
{ {
if (self.moviePlayer != nullptr) { if (self.moviePlayer != nullptr) {
return [self.moviePlayer isFullscreen]; return [self.moviePlayer isFullscreen];
} }
return false; return false;
} }
@ -127,12 +127,12 @@ using namespace cocos2d::experimental::ui;
if (self.moviePlayer != nullptr) { if (self.moviePlayer != nullptr) {
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer]; [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackStateDidChangeNotification object:self.moviePlayer]; [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackStateDidChangeNotification object:self.moviePlayer];
[self.moviePlayer stop]; [self.moviePlayer stop];
[self.moviePlayer.view removeFromSuperview]; [self.moviePlayer.view removeFromSuperview];
self.moviePlayer = nullptr; self.moviePlayer = nullptr;
} }
if (videoSource == 1) { if (videoSource == 1) {
self.moviePlayer = [[[MPMoviePlayerController alloc] init] autorelease]; self.moviePlayer = [[[MPMoviePlayerController alloc] init] autorelease];
self.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming; self.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
@ -144,24 +144,24 @@ using namespace cocos2d::experimental::ui;
self.moviePlayer.allowsAirPlay = false; self.moviePlayer.allowsAirPlay = false;
self.moviePlayer.controlStyle = MPMovieControlStyleEmbedded; self.moviePlayer.controlStyle = MPMovieControlStyleEmbedded;
self.moviePlayer.view.userInteractionEnabled = true; self.moviePlayer.view.userInteractionEnabled = true;
auto clearColor = [UIColor clearColor]; auto clearColor = [UIColor clearColor];
self.moviePlayer.backgroundView.backgroundColor = clearColor; self.moviePlayer.backgroundView.backgroundColor = clearColor;
self.moviePlayer.view.backgroundColor = clearColor; self.moviePlayer.view.backgroundColor = clearColor;
for (UIView * subView in self.moviePlayer.view.subviews) { for (UIView * subView in self.moviePlayer.view.subviews) {
subView.backgroundColor = clearColor; subView.backgroundColor = clearColor;
} }
if (_keepRatioEnabled) { if (_keepRatioEnabled) {
self.moviePlayer.scalingMode = MPMovieScalingModeAspectFit; self.moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
} else { } else {
self.moviePlayer.scalingMode = MPMovieScalingModeFill; self.moviePlayer.scalingMode = MPMovieScalingModeFill;
} }
auto view = cocos2d::Director::getInstance()->getOpenGLView(); auto view = cocos2d::Director::getInstance()->getOpenGLView();
auto eaglview = (CCEAGLView *) view->getEAGLView(); auto eaglview = (CCEAGLView *) view->getEAGLView();
[eaglview addSubview:self.moviePlayer.view]; [eaglview addSubview:self.moviePlayer.view];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playStateChange) name:MPMoviePlayerPlaybackStateDidChangeNotification object:self.moviePlayer]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playStateChange) name:MPMoviePlayerPlaybackStateDidChangeNotification object:self.moviePlayer];
} }
@ -208,14 +208,14 @@ using namespace cocos2d::experimental::ui;
} }
} }
-(void) setVisible:(bool)visible -(void) setVisible:(BOOL)visible
{ {
if (self.moviePlayer != NULL) { if (self.moviePlayer != NULL) {
[self.moviePlayer.view setHidden:!visible]; [self.moviePlayer.view setHidden:!visible];
} }
} }
-(void) setKeepRatioEnabled:(bool)enabled -(void) setKeepRatioEnabled:(BOOL)enabled
{ {
_keepRatioEnabled = enabled; _keepRatioEnabled = enabled;
if (self.moviePlayer != NULL) { if (self.moviePlayer != NULL) {
@ -303,27 +303,27 @@ void VideoPlayer::setURL(const std::string& videoUrl)
void VideoPlayer::draw(Renderer* renderer, const Mat4 &transform, uint32_t flags) void VideoPlayer::draw(Renderer* renderer, const Mat4 &transform, uint32_t flags)
{ {
cocos2d::ui::Widget::draw(renderer,transform,flags); cocos2d::ui::Widget::draw(renderer,transform,flags);
if (flags & FLAGS_TRANSFORM_DIRTY) if (flags & FLAGS_TRANSFORM_DIRTY)
{ {
auto directorInstance = Director::getInstance(); auto directorInstance = Director::getInstance();
auto glView = directorInstance->getOpenGLView(); auto glView = directorInstance->getOpenGLView();
auto frameSize = glView->getFrameSize(); auto frameSize = glView->getFrameSize();
auto scaleFactor = [static_cast<CCEAGLView *>(glView->getEAGLView()) contentScaleFactor]; auto scaleFactor = [static_cast<CCEAGLView *>(glView->getEAGLView()) contentScaleFactor];
auto winSize = directorInstance->getWinSize(); auto winSize = directorInstance->getWinSize();
auto leftBottom = convertToWorldSpace(Vec2::ZERO); auto leftBottom = convertToWorldSpace(Vec2::ZERO);
auto rightTop = convertToWorldSpace(Vec2(_contentSize.width,_contentSize.height)); auto rightTop = convertToWorldSpace(Vec2(_contentSize.width,_contentSize.height));
auto uiLeft = (frameSize.width / 2 + (leftBottom.x - winSize.width / 2 ) * glView->getScaleX()) / scaleFactor; auto uiLeft = (frameSize.width / 2 + (leftBottom.x - winSize.width / 2 ) * glView->getScaleX()) / scaleFactor;
auto uiTop = (frameSize.height /2 - (rightTop.y - winSize.height / 2) * glView->getScaleY()) / scaleFactor; auto uiTop = (frameSize.height /2 - (rightTop.y - winSize.height / 2) * glView->getScaleY()) / scaleFactor;
[((UIVideoViewWrapperIos*)_videoView) setFrame :uiLeft :uiTop [((UIVideoViewWrapperIos*)_videoView) setFrame :uiLeft :uiTop
:(rightTop.x - leftBottom.x) * glView->getScaleX() / scaleFactor :(rightTop.x - leftBottom.x) * glView->getScaleX() / scaleFactor
:( (rightTop.y - leftBottom.y) * glView->getScaleY()/scaleFactor)]; :( (rightTop.y - leftBottom.y) * glView->getScaleY()/scaleFactor)];
} }
#if CC_VIDEOPLAYER_DEBUG_DRAW #if CC_VIDEOPLAYER_DEBUG_DRAW
_debugDrawNode->clear(); _debugDrawNode->clear();
auto size = getContentSize(); auto size = getContentSize();
@ -343,18 +343,6 @@ bool VideoPlayer::isFullScreenEnabled()const
return [((UIVideoViewWrapperIos*)_videoView) isFullScreenEnabled]; return [((UIVideoViewWrapperIos*)_videoView) isFullScreenEnabled];
} }
void VideoPlayer::onEnter()
{
Widget::onEnter();
[((UIVideoViewWrapperIos*)_videoView) setVisible:YES];
}
void VideoPlayer::onExit()
{
Widget::onExit();
[((UIVideoViewWrapperIos*)_videoView) setVisible:NO];
}
void VideoPlayer::setFullScreenEnabled(bool enabled) void VideoPlayer::setFullScreenEnabled(bool enabled)
{ {
[((UIVideoViewWrapperIos*)_videoView) setFullScreenEnabled:enabled]; [((UIVideoViewWrapperIos*)_videoView) setFullScreenEnabled:enabled];
@ -417,11 +405,30 @@ bool VideoPlayer::isPlaying() const
void VideoPlayer::setVisible(bool visible) void VideoPlayer::setVisible(bool visible)
{ {
cocos2d::ui::Widget::setVisible(visible); cocos2d::ui::Widget::setVisible(visible);
if (! _videoURL.empty()) if (!visible)
{ {
[((UIVideoViewWrapperIos*)_videoView) setVisible:visible]; [((UIVideoViewWrapperIos*)_videoView) setVisible:NO];
} }
else if(isRunning())
{
[((UIVideoViewWrapperIos*)_videoView) setVisible:YES];
}
}
void VideoPlayer::onEnter()
{
Widget::onEnter();
if (isVisible())
{
[((UIVideoViewWrapperIos*)_videoView) setVisible: YES];
}
}
void VideoPlayer::onExit()
{
Widget::onExit();
[((UIVideoViewWrapperIos*)_videoView) setVisible: NO];
} }
void VideoPlayer::addEventListener(const VideoPlayer::ccVideoPlayerCallback& callback) void VideoPlayer::addEventListener(const VideoPlayer::ccVideoPlayerCallback& callback)
@ -436,7 +443,7 @@ void VideoPlayer::onPlayEvent(int event)
} else { } else {
_isPlaying = false; _isPlaying = false;
} }
if (_eventCallback) if (_eventCallback)
{ {
_eventCallback(this, (VideoPlayer::EventType)event); _eventCallback(this, (VideoPlayer::EventType)event);

View File

@ -1,18 +1,18 @@
/**************************************************************************** /****************************************************************************
Copyright (c) 2014-2015 Chukong Technologies Inc. Copyright (c) 2014-2015 Chukong Technologies Inc.
http://www.cocos2d-x.org http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions: furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software. all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@ -33,7 +33,7 @@
NS_CC_BEGIN NS_CC_BEGIN
namespace experimental{ namespace experimental{
namespace ui{ namespace ui{
WebView::WebView() WebView::WebView()
: _impl(new WebViewImpl(this)), : _impl(new WebViewImpl(this)),
_onJSCallback(nullptr), _onJSCallback(nullptr),
@ -42,12 +42,12 @@ namespace experimental{
_onDidFailLoading(nullptr) _onDidFailLoading(nullptr)
{ {
} }
WebView::~WebView() WebView::~WebView()
{ {
CC_SAFE_DELETE(_impl); CC_SAFE_DELETE(_impl);
} }
WebView *WebView::create() WebView *WebView::create()
{ {
auto webView = new(std::nothrow) WebView(); auto webView = new(std::nothrow) WebView();
@ -59,12 +59,12 @@ namespace experimental{
CC_SAFE_DELETE(webView); CC_SAFE_DELETE(webView);
return nullptr; return nullptr;
} }
void WebView::setJavascriptInterfaceScheme(const std::string &scheme) void WebView::setJavascriptInterfaceScheme(const std::string &scheme)
{ {
_impl->setJavascriptInterfaceScheme(scheme); _impl->setJavascriptInterfaceScheme(scheme);
} }
void WebView::loadData(const cocos2d::Data &data, void WebView::loadData(const cocos2d::Data &data,
const std::string &MIMEType, const std::string &MIMEType,
const std::string &encoding, const std::string &encoding,
@ -72,74 +72,92 @@ namespace experimental{
{ {
_impl->loadData(data, MIMEType, encoding, 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); _impl->loadHTMLString(string, baseURL);
} }
void WebView::loadURL(const std::string &url) void WebView::loadURL(const std::string &url)
{ {
_impl->loadURL(url); _impl->loadURL(url);
} }
void WebView::loadFile(const std::string &fileName) void WebView::loadFile(const std::string &fileName)
{ {
_impl->loadFile(fileName); _impl->loadFile(fileName);
} }
void WebView::stopLoading() void WebView::stopLoading()
{ {
_impl->stopLoading(); _impl->stopLoading();
} }
void WebView::reload() void WebView::reload()
{ {
_impl->reload(); _impl->reload();
} }
bool WebView::canGoBack() bool WebView::canGoBack()
{ {
return _impl->canGoBack(); return _impl->canGoBack();
} }
bool WebView::canGoForward() bool WebView::canGoForward()
{ {
return _impl->canGoForward(); return _impl->canGoForward();
} }
void WebView::goBack() void WebView::goBack()
{ {
_impl->goBack(); _impl->goBack();
} }
void WebView::goForward() void WebView::goForward()
{ {
_impl->goForward(); _impl->goForward();
} }
void WebView::evaluateJS(const std::string &js) void WebView::evaluateJS(const std::string &js)
{ {
_impl->evaluateJS(js); _impl->evaluateJS(js);
} }
void WebView::setScalesPageToFit(bool const scalesPageToFit) void WebView::setScalesPageToFit(bool const scalesPageToFit)
{ {
_impl->setScalesPageToFit(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); cocos2d::ui::Widget::draw(renderer, transform, flags);
_impl->draw(renderer, transform, flags); _impl->draw(renderer, transform, flags);
} }
void WebView::setVisible(bool visible) void WebView::setVisible(bool visible)
{ {
Node::setVisible(visible); Node::setVisible(visible);
_impl->setVisible(visible); if (!visible || isRunning())
{
_impl->setVisible(visible);
}
} }
void WebView::onEnter()
{
Widget::onEnter();
if(isVisible())
{
_impl->setVisible(true);
}
}
void WebView::onExit()
{
Widget::onExit();
_impl->setVisible(false);
}
cocos2d::ui::Widget* WebView::createCloneInstance() cocos2d::ui::Widget* WebView::createCloneInstance()
{ {
return WebView::create(); return WebView::create();
@ -157,59 +175,47 @@ namespace experimental{
this->_onJSCallback = webView->_onJSCallback; this->_onJSCallback = webView->_onJSCallback;
} }
} }
void WebView::setOnDidFailLoading(const ccWebViewCallback &callback) void WebView::setOnDidFailLoading(const ccWebViewCallback &callback)
{ {
_onDidFailLoading = callback; _onDidFailLoading = callback;
} }
void WebView::setOnDidFinishLoading(const ccWebViewCallback &callback) void WebView::setOnDidFinishLoading(const ccWebViewCallback &callback)
{ {
_onDidFinishLoading = callback; _onDidFinishLoading = callback;
} }
void WebView::setOnShouldStartLoading(const std::function<bool(WebView *sender, const std::string &url)> &callback) void WebView::setOnShouldStartLoading(const std::function<bool(WebView *sender, const std::string &url)> &callback)
{ {
_onShouldStartLoading = callback; _onShouldStartLoading = callback;
} }
void WebView::setOnJSCallback(const ccWebViewCallback &callback) void WebView::setOnJSCallback(const ccWebViewCallback &callback)
{ {
_onJSCallback = callback; _onJSCallback = callback;
} }
std::function<bool(WebView *sender, const std::string &url)> WebView::getOnShouldStartLoading()const std::function<bool(WebView *sender, const std::string &url)> WebView::getOnShouldStartLoading()const
{ {
return _onShouldStartLoading; return _onShouldStartLoading;
} }
WebView::ccWebViewCallback WebView::getOnDidFailLoading()const WebView::ccWebViewCallback WebView::getOnDidFailLoading()const
{ {
return _onDidFailLoading; return _onDidFailLoading;
} }
WebView::ccWebViewCallback WebView::getOnDidFinishLoading()const WebView::ccWebViewCallback WebView::getOnDidFinishLoading()const
{ {
return _onDidFinishLoading; return _onDidFinishLoading;
} }
WebView::ccWebViewCallback WebView::getOnJSCallback()const WebView::ccWebViewCallback WebView::getOnJSCallback()const
{ {
return _onJSCallback; return _onJSCallback;
} }
void WebView::onEnter()
{
Widget::onEnter();
_impl->setVisible(true);
}
void WebView::onExit()
{
Widget::onExit();
_impl->setVisible(false);
}
} // namespace ui } // namespace ui
} // namespace experimental } // namespace experimental
} //namespace cocos2d } //namespace cocos2d

View File

@ -203,7 +203,6 @@ public:
* Toggle visibility of WebView. * Toggle visibility of WebView.
*/ */
virtual void setVisible(bool visible) override; virtual void setVisible(bool visible) override;
virtual void onEnter() override; virtual void onEnter() override;
virtual void onExit() override; virtual void onExit() override;