2018-04-21 21:24:46 +08:00
|
|
|
|
#include "..\e2dnode.h"
|
2018-01-30 16:45:38 +08:00
|
|
|
|
|
|
|
|
|
|
|
2018-02-07 16:37:12 +08:00
|
|
|
|
e2d::Sprite::Sprite()
|
2018-05-09 00:34:15 +08:00
|
|
|
|
: _image(nullptr)
|
2018-01-30 16:45:38 +08:00
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-02-07 16:37:12 +08:00
|
|
|
|
e2d::Sprite::Sprite(Image * image)
|
2018-05-09 00:34:15 +08:00
|
|
|
|
: _image(nullptr)
|
2018-01-30 16:45:38 +08:00
|
|
|
|
{
|
2018-03-02 23:49:57 +08:00
|
|
|
|
open(image);
|
2018-01-30 16:45:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-05-07 15:48:06 +08:00
|
|
|
|
e2d::Sprite::Sprite(const String& filePath)
|
2018-05-09 00:34:15 +08:00
|
|
|
|
: _image(nullptr)
|
2018-01-30 16:45:38 +08:00
|
|
|
|
{
|
2018-05-07 15:48:06 +08:00
|
|
|
|
open(filePath);
|
2018-01-30 16:45:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-05-07 15:48:06 +08:00
|
|
|
|
e2d::Sprite::Sprite(int resNameId, const String& resType)
|
2018-05-09 00:34:15 +08:00
|
|
|
|
: _image(nullptr)
|
2018-01-30 16:45:38 +08:00
|
|
|
|
{
|
2018-04-27 00:16:14 +08:00
|
|
|
|
open(resNameId, resType);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-05-22 23:36:46 +08:00
|
|
|
|
e2d::Sprite::Sprite(const String& filePath, const Rect& cropRect)
|
2018-05-09 00:34:15 +08:00
|
|
|
|
: _image(nullptr)
|
2018-04-27 00:16:14 +08:00
|
|
|
|
{
|
2018-05-07 15:48:06 +08:00
|
|
|
|
open(filePath);
|
2018-05-22 23:36:46 +08:00
|
|
|
|
crop(cropRect);
|
2018-04-27 00:16:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-05-22 23:36:46 +08:00
|
|
|
|
e2d::Sprite::Sprite(int resNameId, const String& resType, const Rect& cropRect)
|
2018-05-09 00:34:15 +08:00
|
|
|
|
: _image(nullptr)
|
2018-04-27 00:16:14 +08:00
|
|
|
|
{
|
|
|
|
|
|
open(resNameId, resType);
|
2018-05-22 23:36:46 +08:00
|
|
|
|
crop(cropRect);
|
2018-01-30 16:45:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-02-07 16:37:12 +08:00
|
|
|
|
e2d::Sprite::~Sprite()
|
2018-01-30 16:45:38 +08:00
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-04-27 00:16:14 +08:00
|
|
|
|
bool e2d::Sprite::open(Image * image)
|
2018-01-30 16:45:38 +08:00
|
|
|
|
{
|
|
|
|
|
|
if (image)
|
|
|
|
|
|
{
|
2018-05-19 01:10:37 +08:00
|
|
|
|
GC::release(_image);
|
2018-05-09 00:34:15 +08:00
|
|
|
|
_image = image;
|
|
|
|
|
|
_image->retain();
|
2018-01-30 16:45:38 +08:00
|
|
|
|
|
2018-05-09 00:34:15 +08:00
|
|
|
|
Node::setSize(_image->getWidth(), _image->getHeight());
|
2018-04-27 00:16:14 +08:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-05-07 15:48:06 +08:00
|
|
|
|
bool e2d::Sprite::open(const String& filePath)
|
2018-04-27 00:16:14 +08:00
|
|
|
|
{
|
2018-05-09 00:34:15 +08:00
|
|
|
|
if (!_image)
|
2018-04-27 00:16:14 +08:00
|
|
|
|
{
|
2018-05-19 01:10:37 +08:00
|
|
|
|
_image = GC::create<Image>();
|
2018-05-09 00:34:15 +08:00
|
|
|
|
_image->retain();
|
2018-04-27 00:16:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-05-09 00:34:15 +08:00
|
|
|
|
if (_image->open(filePath))
|
2018-04-27 00:16:14 +08:00
|
|
|
|
{
|
2018-05-09 00:34:15 +08:00
|
|
|
|
Node::setSize(_image->getWidth(), _image->getHeight());
|
2018-04-27 00:16:14 +08:00
|
|
|
|
return true;
|
2018-01-30 16:45:38 +08:00
|
|
|
|
}
|
2018-04-27 00:16:14 +08:00
|
|
|
|
return false;
|
2018-01-30 16:45:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-05-07 15:48:06 +08:00
|
|
|
|
bool e2d::Sprite::open(int resNameId, const String& resType)
|
2018-01-30 16:45:38 +08:00
|
|
|
|
{
|
2018-05-09 00:34:15 +08:00
|
|
|
|
if (!_image)
|
2018-04-27 00:16:14 +08:00
|
|
|
|
{
|
2018-05-19 01:10:37 +08:00
|
|
|
|
_image = GC::create<Image>();
|
2018-05-09 00:34:15 +08:00
|
|
|
|
_image->retain();
|
2018-04-27 00:16:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-05-09 00:34:15 +08:00
|
|
|
|
if (_image->open(resNameId, resType))
|
2018-04-27 00:16:14 +08:00
|
|
|
|
{
|
2018-05-09 00:34:15 +08:00
|
|
|
|
Node::setSize(_image->getWidth(), _image->getHeight());
|
2018-04-27 00:16:14 +08:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
return false;
|
2018-01-30 16:45:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-05-22 23:36:46 +08:00
|
|
|
|
void e2d::Sprite::crop(const Rect& cropRect)
|
2018-01-30 16:45:38 +08:00
|
|
|
|
{
|
2018-05-22 23:36:46 +08:00
|
|
|
|
_image->crop(cropRect);
|
2018-03-01 19:28:22 +08:00
|
|
|
|
Node::setSize(
|
2018-05-22 23:36:46 +08:00
|
|
|
|
min(max(cropRect.size.width, 0), _image->getSourceWidth() - _image->getCropX()),
|
|
|
|
|
|
min(max(cropRect.size.height, 0), _image->getSourceHeight() - _image->getCropY())
|
2018-02-03 22:04:43 +08:00
|
|
|
|
);
|
2018-01-30 16:45:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-02-07 16:37:12 +08:00
|
|
|
|
e2d::Image * e2d::Sprite::getImage() const
|
2018-01-30 16:45:38 +08:00
|
|
|
|
{
|
2018-05-09 00:34:15 +08:00
|
|
|
|
return _image;
|
2018-01-30 16:45:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-02-07 16:37:12 +08:00
|
|
|
|
void e2d::Sprite::onRender()
|
2018-01-30 16:45:38 +08:00
|
|
|
|
{
|
2018-05-09 00:34:15 +08:00
|
|
|
|
if (_image && _image->getBitmap())
|
2018-01-30 16:45:38 +08:00
|
|
|
|
{
|
2018-03-30 01:41:29 +08:00
|
|
|
|
// <20><>ȡͼƬ<CDBC>ü<EFBFBD>λ<EFBFBD><CEBB>
|
2018-05-09 00:34:15 +08:00
|
|
|
|
float fCropX = float(_image->getCropX());
|
|
|
|
|
|
float fCropY = float(_image->getCropY());
|
2018-02-03 22:04:43 +08:00
|
|
|
|
// <20><>ȾͼƬ
|
2018-02-07 16:37:12 +08:00
|
|
|
|
Renderer::getRenderTarget()->DrawBitmap(
|
2018-05-09 00:34:15 +08:00
|
|
|
|
_image->getBitmap(),
|
|
|
|
|
|
D2D1::RectF(0, 0, _width, _height),
|
|
|
|
|
|
_displayOpacity,
|
2018-01-30 16:45:38 +08:00
|
|
|
|
D2D1_BITMAP_INTERPOLATION_MODE_LINEAR,
|
|
|
|
|
|
D2D1::RectF(
|
2018-04-06 11:31:24 +08:00
|
|
|
|
fCropX,
|
|
|
|
|
|
fCropY,
|
2018-05-09 00:34:15 +08:00
|
|
|
|
fCropX + _width,
|
|
|
|
|
|
fCropY + _height
|
2018-01-30 16:45:38 +08:00
|
|
|
|
)
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2018-04-21 18:22:01 +08:00
|
|
|
|
|
2018-05-08 20:03:29 +08:00
|
|
|
|
void e2d::Sprite::onDestroy()
|
2018-04-21 18:22:01 +08:00
|
|
|
|
{
|
2018-05-08 20:03:29 +08:00
|
|
|
|
Node::onDestroy();
|
2018-05-19 01:10:37 +08:00
|
|
|
|
GC::release(_image);
|
2018-04-21 18:22:01 +08:00
|
|
|
|
}
|