将图片裁剪的参数替换为Rect

This commit is contained in:
Nomango 2018-05-22 23:36:46 +08:00
parent 0d066e131a
commit f91973255d
6 changed files with 59 additions and 77 deletions

View File

@ -9,37 +9,38 @@ static std::set<ID2D1Bitmap*> s_vBitmaps;
e2d::Image::Image() e2d::Image::Image()
: _bitmap(nullptr) : _bitmap(nullptr)
, _cropX(0) , _cropRect()
, _cropY(0)
, _cropWidth(0)
, _cropHeight(0)
{ {
} }
e2d::Image::Image(const String& filePath) e2d::Image::Image(const String& filePath)
: _bitmap(nullptr) : _bitmap(nullptr)
, _cropRect()
{ {
this->open(filePath); this->open(filePath);
} }
e2d::Image::Image(int resNameId, const String& resType) e2d::Image::Image(int resNameId, const String& resType)
: _bitmap(nullptr) : _bitmap(nullptr)
, _cropRect()
{ {
this->open(resNameId, resType); this->open(resNameId, resType);
} }
e2d::Image::Image(const String& filePath, double cropX, double cropY, double cropWidth, double cropHeight) e2d::Image::Image(const String& filePath, const Rect& cropRect)
: _bitmap(nullptr) : _bitmap(nullptr)
, _cropRect()
{ {
this->open(filePath); this->open(filePath);
this->crop(cropX, cropY, cropWidth, cropHeight); this->crop(cropRect);
} }
e2d::Image::Image(int resNameId, const String& resType, double cropX, double cropY, double cropWidth, double cropHeight) e2d::Image::Image(int resNameId, const String& resType, const Rect& cropRect)
: _bitmap(nullptr) : _bitmap(nullptr)
, _cropRect()
{ {
this->open(resNameId, resType); this->open(resNameId, resType);
this->crop(cropX, cropY, cropWidth, cropHeight); this->crop(cropRect);
} }
e2d::Image::~Image() e2d::Image::~Image()
@ -75,30 +76,30 @@ bool e2d::Image::open(int resNameId, const String& resType)
return true; return true;
} }
void e2d::Image::crop(double x, double y, double width, double height) void e2d::Image::crop(const Rect& cropRect)
{ {
if (_bitmap) if (_bitmap)
{ {
_cropX = min(max(x, 0), this->getSourceWidth()); _cropRect.origin.x = min(max(cropRect.origin.x, 0), this->getSourceWidth());
_cropY = min(max(y, 0), this->getSourceHeight()); _cropRect.origin.y = min(max(cropRect.origin.y, 0), this->getSourceHeight());
_cropWidth = min(max(width, 0), this->getSourceWidth() - _cropX); _cropRect.size.width = min(max(cropRect.size.width, 0), this->getSourceWidth() - cropRect.origin.x);
_cropHeight = min(max(height, 0), this->getSourceHeight() - _cropY); _cropRect.size.height = min(max(cropRect.size.height, 0), this->getSourceHeight() - cropRect.origin.y);
} }
} }
double e2d::Image::getWidth() const double e2d::Image::getWidth() const
{ {
return _cropWidth; return _cropRect.size.width;
} }
double e2d::Image::getHeight() const double e2d::Image::getHeight() const
{ {
return _cropHeight; return _cropRect.size.height;
} }
e2d::Size e2d::Image::getSize() const e2d::Size e2d::Image::getSize() const
{ {
return Size(_cropWidth, _cropHeight); return _cropRect.size;
} }
double e2d::Image::getSourceWidth() const double e2d::Image::getSourceWidth() const
@ -139,17 +140,17 @@ e2d::Size e2d::Image::getSourceSize() const
double e2d::Image::getCropX() const double e2d::Image::getCropX() const
{ {
return _cropX; return _cropRect.origin.x;
} }
double e2d::Image::getCropY() const double e2d::Image::getCropY() const
{ {
return _cropY; return _cropRect.origin.y;
} }
e2d::Point e2d::Image::getCropPos() const e2d::Point e2d::Image::getCropPos() const
{ {
return Point(_cropX, _cropY); return _cropRect.origin;
} }
bool e2d::Image::preload(const String& filePath) bool e2d::Image::preload(const String& filePath)
@ -384,9 +385,9 @@ void e2d::Image::_setBitmap(ID2D1Bitmap * bitmap)
if (bitmap) if (bitmap)
{ {
_bitmap = bitmap; _bitmap = bitmap;
_cropX = _cropY = 0; _cropRect.origin.x = _cropRect.origin.y = 0;
_cropWidth = _bitmap->GetSize().width; _cropRect.size.width = _bitmap->GetSize().width;
_cropHeight = _bitmap->GetSize().height; _cropRect.size.height = _bitmap->GetSize().height;
} }
} }

View File

@ -13,6 +13,12 @@ e2d::Point::Point(double x, double y)
this->y = y; this->y = y;
} }
e2d::Point::Point(const Point & other)
{
x = other.x;
y = other.y;
}
e2d::Point e2d::Point::operator+(Point const & p) const e2d::Point e2d::Point::operator+(Point const & p) const
{ {
return Point(x + p.x, y + p.y); return Point(x + p.x, y + p.y);

View File

@ -12,6 +12,12 @@ e2d::Size::Size(double width, double height)
this->height = height; this->height = height;
} }
e2d::Size::Size(const Size & other)
{
width = other.width;
height = other.height;
}
e2d::Size e2d::Size::operator+(Size const & size) const e2d::Size e2d::Size::operator+(Size const & size) const
{ {
return Size(width + size.width, height + size.height); return Size(width + size.width, height + size.height);

View File

@ -24,18 +24,18 @@ e2d::Sprite::Sprite(int resNameId, const String& resType)
open(resNameId, resType); open(resNameId, resType);
} }
e2d::Sprite::Sprite(const String& filePath, double x, double y, double width, double height) e2d::Sprite::Sprite(const String& filePath, const Rect& cropRect)
: _image(nullptr) : _image(nullptr)
{ {
open(filePath); open(filePath);
crop(x, y, width, height); crop(cropRect);
} }
e2d::Sprite::Sprite(int resNameId, const String& resType, double x, double y, double width, double height) e2d::Sprite::Sprite(int resNameId, const String& resType, const Rect& cropRect)
: _image(nullptr) : _image(nullptr)
{ {
open(resNameId, resType); open(resNameId, resType);
crop(x, y, width, height); crop(cropRect);
} }
e2d::Sprite::~Sprite() e2d::Sprite::~Sprite()
@ -88,12 +88,12 @@ bool e2d::Sprite::open(int resNameId, const String& resType)
return false; return false;
} }
void e2d::Sprite::crop(double x, double y, double width, double height) void e2d::Sprite::crop(const Rect& cropRect)
{ {
_image->crop(x, y, width, height); _image->crop(cropRect);
Node::setSize( Node::setSize(
min(max(width, 0), _image->getSourceWidth() - _image->getCropX()), min(max(cropRect.size.width, 0), _image->getSourceWidth() - _image->getCropX()),
min(max(height, 0), _image->getSourceHeight() - _image->getCropY()) min(max(cropRect.size.height, 0), _image->getSourceHeight() - _image->getCropY())
); );
} }

View File

@ -38,6 +38,8 @@ public:
Point(double x, double y); Point(double x, double y);
Point(const Point& other);
Point operator + (Point const & point) const; Point operator + (Point const & point) const;
Point operator - (Point const & point) const; Point operator - (Point const & point) const;
Point operator * (double const & point) const; Point operator * (double const & point) const;
@ -65,6 +67,8 @@ public:
Size(double width, double height); Size(double width, double height);
Size(const Size& other);
Size operator + (Size const & size) const; Size operator + (Size const & size) const;
Size operator - (Size const & size) const; Size operator - (Size const & size) const;
Size operator * (double const & size) const; Size operator * (double const & size) const;
@ -86,29 +90,15 @@ public:
public: public:
Rect(); Rect();
Rect( Rect(double x, double y, double width, double height);
double x,
double y,
double width,
double height
);
Rect( Rect(const Point& pos, const Size& size);
const Point& pos,
const Size& size
);
Rect( Rect(const Rect& other);
const Rect& other
);
Rect& operator= ( Rect& operator= (const Rect& other);
const Rect& other
);
bool operator== ( bool operator== (const Rect& rect) const;
const Rect& rect
) const;
// 设置矩形 // 设置矩形
void setRect( void setRect(
@ -490,19 +480,13 @@ public:
Image( Image(
const String& filePath, /* 图片文件路径 */ const String& filePath, /* 图片文件路径 */
double cropX, /* 裁剪位置 X 坐标 */ const Rect& cropRect /* ²Ã¼ô¾ØÐÎ */
double cropY, /* 裁剪位置 Y 坐标 */
double cropWidth, /* 裁剪宽度 */
double cropHeight /* 裁剪高度 */
); );
Image( Image(
int resNameId, /* 图片资源名称 */ int resNameId, /* 图片资源名称 */
const String& resType, /* 图片资源类型 */ const String& resType, /* 图片资源类型 */
double cropX, /* 裁剪位置 X 坐标 */ const Rect& cropRect /* ²Ã¼ô¾ØÐÎ */
double cropY, /* 裁剪位置 Y 坐标 */
double cropWidth, /* 裁剪宽度 */
double cropHeight /* 裁剪高度 */
); );
virtual ~Image(); virtual ~Image();
@ -520,10 +504,7 @@ public:
// 将图片裁剪为矩形 // 将图片裁剪为矩形
void crop( void crop(
double cropX, /* 裁剪位置 X 坐标 */ const Rect& cropRect /* ²Ã¼ô¾ØÐÎ */
double cropY, /* 裁剪位置 Y 坐标 */
double cropWidth, /* 裁剪宽度 */
double cropHeight /* 裁剪高度 */
); );
// 获取宽度 // 获取宽度
@ -577,10 +558,7 @@ protected:
); );
protected: protected:
double _cropX; Rect _cropRect;
double _cropY;
double _cropWidth;
double _cropHeight;
ID2D1Bitmap * _bitmap; ID2D1Bitmap * _bitmap;
}; };

View File

@ -483,19 +483,13 @@ public:
Sprite( Sprite(
const String& filePath, /* 图片文件路径 */ const String& filePath, /* 图片文件路径 */
double x, const Rect& cropRect /* ²Ã¼ô¾ØÐÎ */
double y,
double width,
double height
); );
Sprite( Sprite(
int resNameId, /* 图片资源名称 */ int resNameId, /* 图片资源名称 */
const String& resType, /* 图片资源类型 */ const String& resType, /* 图片资源类型 */
double x, const Rect& cropRect /* ²Ã¼ô¾ØÐÎ */
double y,
double width,
double height
); );
virtual ~Sprite(); virtual ~Sprite();
@ -518,10 +512,7 @@ public:
// 将图片裁剪为矩形 // 将图片裁剪为矩形
void crop( void crop(
double x, const Rect& cropRect /* ²Ã¼ô¾ØÐÎ */
double y,
double width,
double height
); );
// 获取 Image 对象 // 获取 Image 对象