2014-03-11 17:13:54 +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.
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
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 "ui/UIImageView.h"
|
2014-07-29 14:57:45 +08:00
|
|
|
#include "ui/UIScale9Sprite.h"
|
2014-12-17 16:46:48 +08:00
|
|
|
#include "ui/UIHelper.h"
|
2014-06-04 14:26:21 +08:00
|
|
|
#include "2d/CCSprite.h"
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
namespace ui
|
|
|
|
{
|
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
static const int IMAGE_RENDERER_Z = (-1);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
IMPLEMENT_CLASS_GUI_INFO(ImageView)
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
ImageView::ImageView()
|
|
|
|
: _scale9Enabled(false)
|
|
|
|
, _prevIgnoreSize(true)
|
|
|
|
, _capInsets(Rect::ZERO)
|
|
|
|
, _imageRenderer(nullptr)
|
|
|
|
, _imageTexType(TextureResType::LOCAL)
|
|
|
|
, _imageTextureSize(_contentSize)
|
|
|
|
, _imageRendererAdaptDirty(true)
|
|
|
|
, _textureFile("")
|
|
|
|
{}
|
2014-03-11 17:13:54 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
ImageView::~ImageView() {}
|
2014-03-11 17:13:54 +08:00
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
ImageView* ImageView::create(std::string_view imageFileName, TextureResType texType)
|
2014-04-02 16:37:09 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
ImageView* widget = new ImageView();
|
2021-12-08 00:11:53 +08:00
|
|
|
if (widget->init(imageFileName, texType))
|
2014-12-16 16:44:04 +08:00
|
|
|
{
|
2014-04-02 16:37:09 +08:00
|
|
|
widget->autorelease();
|
|
|
|
return widget;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(widget);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
ImageView* ImageView::create()
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
ImageView* widget = new ImageView();
|
|
|
|
if (widget->init())
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
widget->autorelease();
|
|
|
|
return widget;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(widget);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-04-02 16:37:09 +08:00
|
|
|
bool ImageView::init()
|
|
|
|
{
|
2014-04-04 18:26:53 +08:00
|
|
|
bool ret = true;
|
2014-12-16 16:44:04 +08:00
|
|
|
do
|
|
|
|
{
|
|
|
|
if (!Widget::init())
|
|
|
|
{
|
2014-04-04 18:26:53 +08:00
|
|
|
ret = false;
|
2014-04-02 16:37:09 +08:00
|
|
|
break;
|
|
|
|
}
|
2014-05-09 14:56:05 +08:00
|
|
|
_imageTexType = TextureResType::LOCAL;
|
2014-04-02 16:37:09 +08:00
|
|
|
} while (0);
|
2014-04-04 18:26:53 +08:00
|
|
|
return ret;
|
2014-04-02 16:37:09 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
bool ImageView::init(std::string_view imageFileName, TextureResType texType)
|
2014-04-02 16:37:09 +08:00
|
|
|
{
|
|
|
|
bool bRet = true;
|
2014-12-16 16:44:04 +08:00
|
|
|
do
|
|
|
|
{
|
|
|
|
if (!Widget::init())
|
|
|
|
{
|
2014-04-02 16:37:09 +08:00
|
|
|
bRet = false;
|
|
|
|
break;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-04-02 16:37:09 +08:00
|
|
|
this->loadTexture(imageFileName, texType);
|
|
|
|
} while (0);
|
|
|
|
return bRet;
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
void ImageView::initRenderer()
|
|
|
|
{
|
2014-07-30 15:27:18 +08:00
|
|
|
_imageRenderer = Scale9Sprite::create();
|
2015-12-09 14:26:12 +08:00
|
|
|
_imageRenderer->setRenderingType(Scale9Sprite::RenderingType::SIMPLE);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-03-24 15:25:44 +08:00
|
|
|
addProtectedChild(_imageRenderer, IMAGE_RENDERER_Z, -1);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
void ImageView::loadTexture(std::string_view fileName, TextureResType texType)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2015-06-05 14:19:05 +08:00
|
|
|
if (fileName.empty())
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
_textureFile = fileName;
|
2014-03-11 17:13:54 +08:00
|
|
|
_imageTexType = texType;
|
|
|
|
switch (_imageTexType)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
case TextureResType::LOCAL:
|
|
|
|
_imageRenderer->initWithFile(fileName);
|
|
|
|
break;
|
|
|
|
case TextureResType::PLIST:
|
|
|
|
_imageRenderer->initWithSpriteFrameName(fileName);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
// FIXME: https://github.com/cocos2d/cocos2d-x/issues/12249
|
|
|
|
if (!_ignoreSize && _customSize.equals(Vec2::ZERO))
|
|
|
|
{
|
2015-12-09 17:15:09 +08:00
|
|
|
_customSize = _imageRenderer->getContentSize();
|
|
|
|
}
|
2015-06-05 14:19:05 +08:00
|
|
|
this->setupTexture();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImageView::loadTexture(SpriteFrame* spriteframe)
|
|
|
|
{
|
|
|
|
_imageRenderer->initWithSpriteFrame(spriteframe);
|
|
|
|
this->setupTexture();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImageView::setupTexture()
|
|
|
|
{
|
2014-03-11 17:13:54 +08:00
|
|
|
_imageTextureSize = _imageRenderer->getContentSize();
|
2015-06-05 14:19:05 +08:00
|
|
|
|
2014-08-11 15:04:00 +08:00
|
|
|
this->updateChildrenDisplayedRGBA();
|
|
|
|
|
2014-04-17 14:08:25 +08:00
|
|
|
updateContentSizeWithTextureSize(_imageTextureSize);
|
|
|
|
_imageRendererAdaptDirty = true;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void ImageView::setTextureRect(const Rect& rect)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// This API should be refactor
|
2014-03-11 17:13:54 +08:00
|
|
|
if (_scale9Enabled)
|
2021-12-25 10:04:45 +08:00
|
|
|
{}
|
2014-03-11 17:13:54 +08:00
|
|
|
else
|
|
|
|
{
|
2014-07-30 17:29:22 +08:00
|
|
|
auto sprite = _imageRenderer->getSprite();
|
|
|
|
if (sprite)
|
|
|
|
{
|
|
|
|
sprite->setTextureRect(rect);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCLOG("Warning!! you should load texture before set the texture's rect!");
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
void ImageView::setScale9Enabled(bool able)
|
|
|
|
{
|
|
|
|
if (_scale9Enabled == able)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
_scale9Enabled = able;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_scale9Enabled)
|
|
|
|
{
|
2015-12-09 14:26:12 +08:00
|
|
|
_imageRenderer->setRenderingType(Scale9Sprite::RenderingType::SLICE);
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-12-09 14:26:12 +08:00
|
|
|
_imageRenderer->setRenderingType(Scale9Sprite::RenderingType::SIMPLE);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
if (_scale9Enabled)
|
|
|
|
{
|
|
|
|
bool ignoreBefore = _ignoreSize;
|
|
|
|
ignoreContentAdaptWithSize(false);
|
|
|
|
_prevIgnoreSize = ignoreBefore;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ignoreContentAdaptWithSize(_prevIgnoreSize);
|
|
|
|
}
|
|
|
|
setCapInsets(_capInsets);
|
2015-01-20 17:24:04 +08:00
|
|
|
_imageRendererAdaptDirty = true;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
bool ImageView::isScale9Enabled() const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return _scale9Enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImageView::ignoreContentAdaptWithSize(bool ignore)
|
|
|
|
{
|
|
|
|
if (!_scale9Enabled || (_scale9Enabled && !ignore))
|
|
|
|
{
|
|
|
|
Widget::ignoreContentAdaptWithSize(ignore);
|
|
|
|
_prevIgnoreSize = ignore;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void ImageView::setCapInsets(const Rect& capInsets)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-12-17 16:46:48 +08:00
|
|
|
_capInsets = ui::Helper::restrictCapInsetRect(capInsets, _imageTextureSize);
|
2014-03-11 17:13:54 +08:00
|
|
|
if (!_scale9Enabled)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-12-17 16:46:48 +08:00
|
|
|
_imageRenderer->setCapInsets(_capInsets);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
const Rect& ImageView::getCapInsets() const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return _capInsets;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImageView::onSizeChanged()
|
|
|
|
{
|
|
|
|
Widget::onSizeChanged();
|
2014-04-17 14:08:25 +08:00
|
|
|
_imageRendererAdaptDirty = true;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-04-17 14:08:25 +08:00
|
|
|
void ImageView::adaptRenderers()
|
|
|
|
{
|
|
|
|
if (_imageRendererAdaptDirty)
|
|
|
|
{
|
|
|
|
imageTextureScaleChangedWithSize();
|
|
|
|
_imageRendererAdaptDirty = false;
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2021-10-23 23:27:14 +08:00
|
|
|
Vec2 ImageView::getVirtualRendererSize() const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return _imageTextureSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node* ImageView::getVirtualRenderer()
|
|
|
|
{
|
|
|
|
return _imageRenderer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImageView::imageTextureScaleChangedWithSize()
|
|
|
|
{
|
2015-12-09 14:26:12 +08:00
|
|
|
_imageRenderer->setPreferredSize(_contentSize);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-04-17 14:08:25 +08:00
|
|
|
_imageRenderer->setPosition(_contentSize.width / 2.0f, _contentSize.height / 2.0f);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string ImageView::getDescription() const
|
|
|
|
{
|
|
|
|
return "ImageView";
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget* ImageView::createCloneInstance()
|
|
|
|
{
|
|
|
|
return ImageView::create();
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void ImageView::copySpecialProperties(Widget* widget)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
ImageView* imageView = dynamic_cast<ImageView*>(widget);
|
|
|
|
if (imageView)
|
|
|
|
{
|
|
|
|
_prevIgnoreSize = imageView->_prevIgnoreSize;
|
|
|
|
setScale9Enabled(imageView->_scale9Enabled);
|
2015-06-05 14:19:05 +08:00
|
|
|
auto imageSprite = imageView->_imageRenderer->getSprite();
|
2021-12-25 10:04:45 +08:00
|
|
|
if (nullptr != imageSprite)
|
2015-06-05 14:19:05 +08:00
|
|
|
{
|
|
|
|
loadTexture(imageSprite->getSpriteFrame());
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
setCapInsets(imageView->_capInsets);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-05 00:26:07 +08:00
|
|
|
ResourceData ImageView::getRenderFile()
|
2015-11-18 13:38:29 +08:00
|
|
|
{
|
2015-12-05 00:26:07 +08:00
|
|
|
ResourceData rData;
|
2015-11-18 13:38:29 +08:00
|
|
|
rData.type = (int)_imageTexType;
|
|
|
|
rData.file = _textureFile;
|
|
|
|
return rData;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void ImageView::setBlendFunc(const BlendFunc& blendFunc)
|
2017-08-22 09:40:37 +08:00
|
|
|
{
|
|
|
|
_imageRenderer->setBlendFunc(blendFunc);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2017-08-22 09:40:37 +08:00
|
|
|
const BlendFunc& ImageView::getBlendFunc() const
|
|
|
|
{
|
|
|
|
return _imageRenderer->getBlendFunc();
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
} // namespace ui
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
NS_CC_END
|