2013-09-13 22:20:20 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:47:11 +08:00
|
|
|
Copyright (c) 2013-2014 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
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
2013-09-13 22:20:20 +08:00
|
|
|
|
2013-10-16 16:48:39 +08:00
|
|
|
#include "gui/UIImageView.h"
|
2013-10-16 11:19:01 +08:00
|
|
|
#include "extensions/GUI/CCControlExtension/CCScale9Sprite.h"
|
2013-09-13 22:20:20 +08:00
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2014-02-24 18:56:45 +08:00
|
|
|
namespace ui {
|
2013-09-13 22:20:20 +08:00
|
|
|
|
2013-11-06 16:04:06 +08:00
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
#define STATIC_CAST_CCSPRITE static_cast<Sprite*>(_imageRenderer)
|
|
|
|
#define STATIC_CAST_SCALE9SPRITE static_cast<extension::Scale9Sprite*>(_imageRenderer)
|
2013-12-24 20:22:14 +08:00
|
|
|
|
2014-01-03 15:46:42 +08:00
|
|
|
static const int IMAGE_RENDERER_Z = (-1);
|
2013-09-13 22:20:20 +08:00
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
ImageView::ImageView():
|
2013-09-16 20:54:13 +08:00
|
|
|
_scale9Enabled(false),
|
|
|
|
_prevIgnoreSize(true),
|
2013-12-23 15:02:52 +08:00
|
|
|
_capInsets(Rect::ZERO),
|
2013-11-14 11:37:46 +08:00
|
|
|
_imageRenderer(nullptr),
|
2013-09-16 20:54:13 +08:00
|
|
|
_textureFile(""),
|
|
|
|
_imageTexType(UI_TEX_TYPE_LOCAL),
|
|
|
|
_imageTextureSize(_size)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
ImageView::~ImageView()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
ImageView* ImageView::create()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
ImageView* widget = new ImageView();
|
2013-09-13 22:20:20 +08:00
|
|
|
if (widget && widget->init())
|
|
|
|
{
|
|
|
|
widget->autorelease();
|
|
|
|
return widget;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(widget);
|
2013-11-14 11:37:46 +08:00
|
|
|
return nullptr;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void ImageView::initRenderer()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
_imageRenderer = Sprite::create();
|
2014-01-03 15:46:42 +08:00
|
|
|
Node::addChild(_imageRenderer, IMAGE_RENDERER_Z, -1);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void ImageView::loadTexture(const char *fileName, TextureResType texType)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
if (!fileName || strcmp(fileName, "") == 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-09-16 20:54:13 +08:00
|
|
|
_textureFile = fileName;
|
|
|
|
_imageTexType = texType;
|
|
|
|
switch (_imageTexType)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
case UI_TEX_TYPE_LOCAL:
|
2013-09-16 20:54:13 +08:00
|
|
|
if (_scale9Enabled)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
extension::Scale9Sprite* imageRendererScale9 = STATIC_CAST_SCALE9SPRITE;
|
|
|
|
imageRendererScale9->initWithFile(fileName);
|
|
|
|
imageRendererScale9->setCapInsets(_capInsets);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
Sprite* imageRenderer = STATIC_CAST_CCSPRITE;
|
|
|
|
imageRenderer->setTexture(fileName);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case UI_TEX_TYPE_PLIST:
|
2013-09-16 20:54:13 +08:00
|
|
|
if (_scale9Enabled)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
extension::Scale9Sprite* imageRendererScale9 = STATIC_CAST_SCALE9SPRITE;
|
|
|
|
imageRendererScale9->initWithSpriteFrameName(fileName);
|
|
|
|
imageRendererScale9->setCapInsets(_capInsets);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
Sprite* imageRenderer = STATIC_CAST_CCSPRITE;
|
|
|
|
imageRenderer->setSpriteFrame(fileName);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2013-09-16 20:54:13 +08:00
|
|
|
_imageTextureSize = _imageRenderer->getContentSize();
|
2013-09-13 22:20:20 +08:00
|
|
|
updateAnchorPoint();
|
2014-03-03 23:24:57 +08:00
|
|
|
updateRGBAToRenderer(_imageRenderer);
|
2013-09-13 22:20:20 +08:00
|
|
|
imageTextureScaleChangedWithSize();
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void ImageView::setTextureRect(const Rect &rect)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
if (_scale9Enabled)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
STATIC_CAST_CCSPRITE->setTextureRect(rect);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void ImageView::setFlipX(bool flipX)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
if (_scale9Enabled)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
STATIC_CAST_CCSPRITE->setFlippedX(flipX);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void ImageView::setFlipY(bool flipY)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
if (_scale9Enabled)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
STATIC_CAST_CCSPRITE->setFlippedY(flipY);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
bool ImageView::isFlipX()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
if (_scale9Enabled)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
return STATIC_CAST_CCSPRITE->isFlippedX();
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
bool ImageView::isFlipY()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
if (_scale9Enabled)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
return STATIC_CAST_CCSPRITE->isFlippedY();
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void ImageView::setScale9Enabled(bool able)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
if (_scale9Enabled == able)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-16 20:54:13 +08:00
|
|
|
_scale9Enabled = able;
|
2013-12-24 20:22:14 +08:00
|
|
|
Node::removeChild(_imageRenderer);
|
2013-11-14 11:37:46 +08:00
|
|
|
_imageRenderer = nullptr;
|
2013-09-16 20:54:13 +08:00
|
|
|
if (_scale9Enabled)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
_imageRenderer = extension::Scale9Sprite::create();
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
_imageRenderer = Sprite::create();
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
2013-09-16 20:54:13 +08:00
|
|
|
loadTexture(_textureFile.c_str(),_imageTexType);
|
2014-01-03 15:46:42 +08:00
|
|
|
Node::addChild(_imageRenderer, IMAGE_RENDERER_Z, -1);
|
2013-09-16 20:54:13 +08:00
|
|
|
if (_scale9Enabled)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 15:32:52 +08:00
|
|
|
bool ignoreBefore = _ignoreSize;
|
2013-09-13 22:20:20 +08:00
|
|
|
ignoreContentAdaptWithSize(false);
|
2013-09-16 20:54:13 +08:00
|
|
|
_prevIgnoreSize = ignoreBefore;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
ignoreContentAdaptWithSize(_prevIgnoreSize);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
2013-09-16 20:54:13 +08:00
|
|
|
setCapInsets(_capInsets);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
2014-03-01 22:34:25 +08:00
|
|
|
|
|
|
|
bool ImageView::isScale9Enabled()
|
|
|
|
{
|
|
|
|
return _scale9Enabled;
|
|
|
|
}
|
2013-09-13 22:20:20 +08:00
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void ImageView::ignoreContentAdaptWithSize(bool ignore)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
if (!_scale9Enabled || (_scale9Enabled && !ignore))
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
Widget::ignoreContentAdaptWithSize(ignore);
|
2013-09-16 20:54:13 +08:00
|
|
|
_prevIgnoreSize = ignore;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void ImageView::setCapInsets(const Rect &capInsets)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_capInsets = capInsets;
|
|
|
|
if (!_scale9Enabled)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-12-23 15:02:52 +08:00
|
|
|
STATIC_CAST_SCALE9SPRITE->setCapInsets(capInsets);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2014-03-01 22:34:25 +08:00
|
|
|
const Rect& ImageView::getCapInsets()
|
|
|
|
{
|
|
|
|
return _capInsets;
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void ImageView::setAnchorPoint(const Point &pt)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
Widget::setAnchorPoint(pt);
|
2013-09-16 20:54:13 +08:00
|
|
|
_imageRenderer->setAnchorPoint(pt);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void ImageView::onSizeChanged()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
Widget::onSizeChanged();
|
2013-09-13 22:20:20 +08:00
|
|
|
imageTextureScaleChangedWithSize();
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
const Size& ImageView::getContentSize() const
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
return _imageTextureSize;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
Node* ImageView::getVirtualRenderer()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
return _imageRenderer;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void ImageView::imageTextureScaleChangedWithSize()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 15:32:52 +08:00
|
|
|
if (_ignoreSize)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
if (!_scale9Enabled)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_imageRenderer->setScale(1.0f);
|
|
|
|
_size = _imageTextureSize;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
if (_scale9Enabled)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
static_cast<extension::Scale9Sprite*>(_imageRenderer)->setPreferredSize(_size);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
Size textureSize = _imageRenderer->getContentSize();
|
2013-09-13 22:20:20 +08:00
|
|
|
if (textureSize.width <= 0.0f || textureSize.height <= 0.0f)
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_imageRenderer->setScale(1.0f);
|
2013-09-13 22:20:20 +08:00
|
|
|
return;
|
|
|
|
}
|
2013-09-16 15:32:52 +08:00
|
|
|
float scaleX = _size.width / textureSize.width;
|
|
|
|
float scaleY = _size.height / textureSize.height;
|
2013-09-16 20:54:13 +08:00
|
|
|
_imageRenderer->setScaleX(scaleX);
|
|
|
|
_imageRenderer->setScaleY(scaleY);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
std::string ImageView::getDescription() const
|
2013-09-17 17:59:20 +08:00
|
|
|
{
|
|
|
|
return "ImageView";
|
|
|
|
}
|
2014-03-03 23:24:57 +08:00
|
|
|
|
|
|
|
void ImageView::updateTextureColor()
|
|
|
|
{
|
|
|
|
updateColorToRenderer(_imageRenderer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImageView::updateTextureOpacity()
|
|
|
|
{
|
|
|
|
updateOpacityToRenderer(_imageRenderer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImageView::updateTextureRGBA()
|
|
|
|
{
|
|
|
|
updateRGBAToRenderer(_imageRenderer);
|
|
|
|
}
|
2013-09-17 17:59:20 +08:00
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
Widget* ImageView::createCloneInstance()
|
2013-11-06 16:04:06 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
return ImageView::create();
|
2013-11-06 16:04:06 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void ImageView::copySpecialProperties(Widget *widget)
|
2013-11-06 16:04:06 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
ImageView* imageView = dynamic_cast<ImageView*>(widget);
|
2013-11-06 16:04:06 +08:00
|
|
|
if (imageView)
|
|
|
|
{
|
|
|
|
_prevIgnoreSize = imageView->_prevIgnoreSize;
|
|
|
|
setScale9Enabled(imageView->_scale9Enabled);
|
|
|
|
loadTexture(imageView->_textureFile.c_str(), imageView->_imageTexType);
|
|
|
|
setCapInsets(imageView->_capInsets);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
}
|
|
|
|
|
2014-02-24 18:56:45 +08:00
|
|
|
NS_CC_END
|