2013-09-13 22:20:20 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013 cocos2d-x.org
|
|
|
|
|
|
|
|
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-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-10-15 18:00:03 +08:00
|
|
|
namespace gui {
|
2013-09-13 22:20:20 +08:00
|
|
|
|
2013-11-06 16:04:06 +08:00
|
|
|
|
2013-09-16 20:54:13 +08:00
|
|
|
#define DYNAMIC_CAST_CCSPRITE dynamic_cast<cocos2d::Sprite*>(_imageRenderer)
|
|
|
|
#define DYNAMIC_CAST_SCALE9SPRITE dynamic_cast<cocos2d::extension::Scale9Sprite*>(_imageRenderer)
|
2013-09-13 22:20:20 +08:00
|
|
|
|
|
|
|
UIImageView::UIImageView():
|
2013-09-16 20:54:13 +08:00
|
|
|
_clickCount(0),
|
|
|
|
_clickTimeInterval(0.0),
|
|
|
|
_startCheckDoubleClick(false),
|
|
|
|
_touchRelease(false),
|
|
|
|
_doubleClickEnabled(false),
|
|
|
|
_scale9Enabled(false),
|
|
|
|
_prevIgnoreSize(true),
|
2013-11-06 16:04:06 +08:00
|
|
|
_capInsets(cocos2d::Rect::ZERO),
|
2013-09-16 20:54:13 +08:00
|
|
|
_imageRenderer(NULL),
|
|
|
|
_textureFile(""),
|
|
|
|
_imageTexType(UI_TEX_TYPE_LOCAL),
|
|
|
|
_imageTextureSize(_size)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
UIImageView::~UIImageView()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
UIImageView* UIImageView::create()
|
|
|
|
{
|
|
|
|
UIImageView* widget = new UIImageView();
|
|
|
|
if (widget && widget->init())
|
|
|
|
{
|
|
|
|
widget->autorelease();
|
|
|
|
return widget;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(widget);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UIImageView::initRenderer()
|
|
|
|
{
|
|
|
|
UIWidget::initRenderer();
|
2013-11-06 16:04:06 +08:00
|
|
|
_imageRenderer = cocos2d::Sprite::create();
|
2013-09-16 20:54:13 +08:00
|
|
|
_renderer->addChild(_imageRenderer);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UIImageView::loadTexture(const char *fileName, TextureResType texType)
|
|
|
|
{
|
|
|
|
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
|
|
|
{
|
|
|
|
DYNAMIC_CAST_SCALE9SPRITE->initWithFile(fileName);
|
|
|
|
DYNAMIC_CAST_SCALE9SPRITE->setColor(getColor());
|
|
|
|
DYNAMIC_CAST_SCALE9SPRITE->setOpacity(getOpacity());
|
2013-11-06 16:04:06 +08:00
|
|
|
DYNAMIC_CAST_SCALE9SPRITE->setCapInsets(_capInsets);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-11-14 07:55:36 +08:00
|
|
|
DYNAMIC_CAST_CCSPRITE->setTexture(fileName);
|
2013-09-13 22:20:20 +08:00
|
|
|
DYNAMIC_CAST_CCSPRITE->setColor(getColor());
|
|
|
|
DYNAMIC_CAST_CCSPRITE->setOpacity(getOpacity());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case UI_TEX_TYPE_PLIST:
|
2013-09-16 20:54:13 +08:00
|
|
|
if (_scale9Enabled)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
DYNAMIC_CAST_SCALE9SPRITE->initWithSpriteFrameName(fileName);
|
|
|
|
DYNAMIC_CAST_SCALE9SPRITE->setColor(getColor());
|
|
|
|
DYNAMIC_CAST_SCALE9SPRITE->setOpacity(getOpacity());
|
2013-11-06 16:04:06 +08:00
|
|
|
DYNAMIC_CAST_SCALE9SPRITE->setCapInsets(_capInsets);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-11-14 07:55:36 +08:00
|
|
|
DYNAMIC_CAST_CCSPRITE->setSpriteFrame(fileName);
|
2013-09-13 22:20:20 +08:00
|
|
|
DYNAMIC_CAST_CCSPRITE->setColor(getColor());
|
|
|
|
DYNAMIC_CAST_CCSPRITE->setOpacity(getOpacity());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2013-09-16 20:54:13 +08:00
|
|
|
_imageTextureSize = _imageRenderer->getContentSize();
|
2013-09-13 22:20:20 +08:00
|
|
|
updateAnchorPoint();
|
|
|
|
imageTextureScaleChangedWithSize();
|
|
|
|
}
|
|
|
|
|
2013-11-06 16:04:06 +08:00
|
|
|
void UIImageView::setTextureRect(const cocos2d::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
|
|
|
|
{
|
|
|
|
DYNAMIC_CAST_CCSPRITE->setTextureRect(rect);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-06 16:04:06 +08:00
|
|
|
bool UIImageView::onTouchBegan(const cocos2d::Point &touchPoint)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
setFocused(true);
|
2013-09-16 15:32:52 +08:00
|
|
|
_touchStartPos.x = touchPoint.x;
|
|
|
|
_touchStartPos.y = touchPoint.y;
|
|
|
|
_widgetParent->checkChildInfo(0,this,touchPoint);
|
2013-09-13 22:20:20 +08:00
|
|
|
pushDownEvent();
|
|
|
|
|
2013-09-16 20:54:13 +08:00
|
|
|
if (_doubleClickEnabled)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_clickTimeInterval = 0;
|
|
|
|
_startCheckDoubleClick = true;
|
|
|
|
_clickCount++;
|
|
|
|
_touchRelease = false;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
2013-09-16 15:32:52 +08:00
|
|
|
return _touchPassedEnabled;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-11-06 16:04:06 +08:00
|
|
|
void UIImageView::onTouchEnded(const cocos2d::Point &touchPoint)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
if (_doubleClickEnabled)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
if (_clickCount >= 2)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
doubleClickEvent();
|
2013-09-16 20:54:13 +08:00
|
|
|
_clickCount = 0;
|
|
|
|
_startCheckDoubleClick = false;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_touchRelease = true;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
UIWidget::onTouchEnded(touchPoint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UIImageView::doubleClickEvent()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void UIImageView::checkDoubleClick(float dt)
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
if (_startCheckDoubleClick)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_clickTimeInterval += dt;
|
|
|
|
if (_clickTimeInterval >= 200 && _clickCount > 0)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_clickTimeInterval = 0;
|
|
|
|
_clickCount--;
|
|
|
|
_startCheckDoubleClick = false;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
if (_clickCount <= 1)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
if (_touchRelease)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
releaseUpEvent();
|
2013-09-16 20:54:13 +08:00
|
|
|
_clickTimeInterval = 0;
|
|
|
|
_clickCount = 0;
|
|
|
|
_touchRelease = false;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UIImageView::setDoubleClickEnabled(bool able)
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
if (able == _doubleClickEnabled)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-09-16 20:54:13 +08:00
|
|
|
_doubleClickEnabled = able;
|
2013-09-13 22:20:20 +08:00
|
|
|
if (able)
|
|
|
|
{
|
|
|
|
// COCOUISYSTEM->getUIInputManager()->addCheckedDoubleClickWidget(this);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UIImageView::setFlipX(bool flipX)
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
if (_scale9Enabled)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DYNAMIC_CAST_CCSPRITE->setFlippedX(flipX);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UIImageView::setFlipY(bool flipY)
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
if (_scale9Enabled)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DYNAMIC_CAST_CCSPRITE->setFlippedY(flipY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool UIImageView::isFlipX()
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
if (_scale9Enabled)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return DYNAMIC_CAST_CCSPRITE->isFlippedX();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool UIImageView::isFlipY()
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
if (_scale9Enabled)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return DYNAMIC_CAST_CCSPRITE->isFlippedY();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UIImageView::setScale9Enabled(bool able)
|
|
|
|
{
|
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;
|
|
|
|
_renderer->removeChild(_imageRenderer, true);
|
|
|
|
_imageRenderer = NULL;
|
|
|
|
if (_scale9Enabled)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-11-06 16:04:06 +08:00
|
|
|
_imageRenderer = cocos2d::extension::Scale9Sprite::create();
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-11-06 16:04:06 +08:00
|
|
|
_imageRenderer = cocos2d::Sprite::create();
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
2013-09-16 20:54:13 +08:00
|
|
|
loadTexture(_textureFile.c_str(),_imageTexType);
|
|
|
|
_renderer->addChild(_imageRenderer);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void UIImageView::ignoreContentAdaptWithSize(bool ignore)
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
if (!_scale9Enabled || (_scale9Enabled && !ignore))
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
UIWidget::ignoreContentAdaptWithSize(ignore);
|
2013-09-16 20:54:13 +08:00
|
|
|
_prevIgnoreSize = ignore;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-06 16:04:06 +08:00
|
|
|
void UIImageView::setCapInsets(const cocos2d::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;
|
|
|
|
}
|
|
|
|
DYNAMIC_CAST_SCALE9SPRITE->setCapInsets(capInsets);
|
|
|
|
}
|
|
|
|
|
2013-11-06 16:04:06 +08:00
|
|
|
void UIImageView::setAnchorPoint(const cocos2d::Point &pt)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
UIWidget::setAnchorPoint(pt);
|
2013-09-16 20:54:13 +08:00
|
|
|
_imageRenderer->setAnchorPoint(pt);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UIImageView::onSizeChanged()
|
|
|
|
{
|
|
|
|
imageTextureScaleChangedWithSize();
|
|
|
|
}
|
|
|
|
|
2013-11-06 16:04:06 +08:00
|
|
|
const cocos2d::Size& UIImageView::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-11-06 16:04:06 +08:00
|
|
|
cocos2d::Node* UIImageView::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
|
|
|
}
|
|
|
|
|
|
|
|
void UIImageView::imageTextureScaleChangedWithSize()
|
|
|
|
{
|
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-11-06 16:04:06 +08:00
|
|
|
dynamic_cast<cocos2d::extension::Scale9Sprite*>(_imageRenderer)->setPreferredSize(_size);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-11-06 16:04:06 +08:00
|
|
|
cocos2d::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-09-17 17:59:20 +08:00
|
|
|
const char* UIImageView::getDescription() const
|
|
|
|
{
|
|
|
|
return "ImageView";
|
|
|
|
}
|
|
|
|
|
2013-11-06 16:04:06 +08:00
|
|
|
UIWidget* UIImageView::createCloneInstance()
|
|
|
|
{
|
|
|
|
return UIImageView::create();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UIImageView::copySpecialProperties(UIWidget *widget)
|
|
|
|
{
|
|
|
|
UIImageView* imageView = dynamic_cast<UIImageView*>(widget);
|
|
|
|
if (imageView)
|
|
|
|
{
|
|
|
|
_prevIgnoreSize = imageView->_prevIgnoreSize;
|
|
|
|
setScale9Enabled(imageView->_scale9Enabled);
|
|
|
|
loadTexture(imageView->_textureFile.c_str(), imageView->_imageTexType);
|
|
|
|
setCapInsets(imageView->_capInsets);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-15 18:00:03 +08:00
|
|
|
}
|