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);
if (! _videoURL.empty())
if (!visible || isRunning())
{
JniHelper::callStaticVoidMethod(videoHelperClassName, "setVideoVisible", _videoPlayerIndex, visible);
}
}
}
void VideoPlayer::onEnter()
{
Widget::onEnter();
this->setVisible(true);
if (isVisible() && !_videoURL.empty())
{
JniHelper::callStaticVoidMethod(videoHelperClassName, "setVideoVisible", _videoPlayerIndex, true);
}
}
void VideoPlayer::onExit()
{
Widget::onExit();
this->setVisible(false);
JniHelper::callStaticVoidMethod(videoHelperClassName, "setVideoVisible", _videoPlayerIndex, false);
}
void VideoPlayer::addEventListener(const VideoPlayer::ccVideoPlayerCallback& callback)

View File

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

View File

@ -1,18 +1,18 @@
/****************************************************************************
Copyright (c) 2014-2015 Chukong Technologies Inc.
http://www.cocos2d-x.org
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:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
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
@ -33,7 +33,7 @@
NS_CC_BEGIN
namespace experimental{
namespace ui{
WebView::WebView()
: _impl(new WebViewImpl(this)),
_onJSCallback(nullptr),
@ -42,12 +42,12 @@ namespace experimental{
_onDidFailLoading(nullptr)
{
}
WebView::~WebView()
{
CC_SAFE_DELETE(_impl);
}
WebView *WebView::create()
{
auto webView = new(std::nothrow) WebView();
@ -59,12 +59,12 @@ namespace experimental{
CC_SAFE_DELETE(webView);
return nullptr;
}
void WebView::setJavascriptInterfaceScheme(const std::string &scheme)
{
_impl->setJavascriptInterfaceScheme(scheme);
}
void WebView::loadData(const cocos2d::Data &data,
const std::string &MIMEType,
const std::string &encoding,
@ -72,74 +72,92 @@ namespace experimental{
{
_impl->loadData(data, MIMEType, encoding, baseURL);
}
void WebView::loadHTMLString(const std::string &string, const std::string &baseURL)
{
_impl->loadHTMLString(string, baseURL);
}
void WebView::loadURL(const std::string &url)
{
_impl->loadURL(url);
}
void WebView::loadFile(const std::string &fileName)
{
_impl->loadFile(fileName);
}
void WebView::stopLoading()
{
_impl->stopLoading();
}
void WebView::reload()
{
_impl->reload();
}
bool WebView::canGoBack()
{
return _impl->canGoBack();
}
bool WebView::canGoForward()
{
return _impl->canGoForward();
}
void WebView::goBack()
{
_impl->goBack();
}
void WebView::goForward()
{
_impl->goForward();
}
void WebView::evaluateJS(const std::string &js)
{
_impl->evaluateJS(js);
}
void WebView::setScalesPageToFit(bool const scalesPageToFit)
{
_impl->setScalesPageToFit(scalesPageToFit);
}
void WebView::draw(cocos2d::Renderer *renderer, cocos2d::Mat4 const &transform, uint32_t flags)
{
cocos2d::ui::Widget::draw(renderer, transform, flags);
_impl->draw(renderer, transform, flags);
}
void WebView::setVisible(bool 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()
{
return WebView::create();
@ -157,59 +175,47 @@ namespace experimental{
this->_onJSCallback = webView->_onJSCallback;
}
}
void WebView::setOnDidFailLoading(const ccWebViewCallback &callback)
{
_onDidFailLoading = callback;
}
void WebView::setOnDidFinishLoading(const ccWebViewCallback &callback)
{
_onDidFinishLoading = callback;
}
void WebView::setOnShouldStartLoading(const std::function<bool(WebView *sender, const std::string &url)> &callback)
{
_onShouldStartLoading = callback;
}
void WebView::setOnJSCallback(const ccWebViewCallback &callback)
{
_onJSCallback = callback;
}
std::function<bool(WebView *sender, const std::string &url)> WebView::getOnShouldStartLoading()const
{
return _onShouldStartLoading;
}
WebView::ccWebViewCallback WebView::getOnDidFailLoading()const
{
return _onDidFailLoading;
}
WebView::ccWebViewCallback WebView::getOnDidFinishLoading()const
{
return _onDidFinishLoading;
}
WebView::ccWebViewCallback WebView::getOnJSCallback()const
{
return _onJSCallback;
}
void WebView::onEnter()
{
Widget::onEnter();
_impl->setVisible(true);
}
void WebView::onExit()
{
Widget::onExit();
_impl->setVisible(false);
}
} // namespace ui
} // namespace experimental
} //namespace cocos2d

View File

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