将图片裁剪的参数替换为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()
: _bitmap(nullptr)
, _cropX(0)
, _cropY(0)
, _cropWidth(0)
, _cropHeight(0)
, _cropRect()
{
}
e2d::Image::Image(const String& filePath)
: _bitmap(nullptr)
, _cropRect()
{
this->open(filePath);
}
e2d::Image::Image(int resNameId, const String& resType)
: _bitmap(nullptr)
, _cropRect()
{
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)
, _cropRect()
{
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)
, _cropRect()
{
this->open(resNameId, resType);
this->crop(cropX, cropY, cropWidth, cropHeight);
this->crop(cropRect);
}
e2d::Image::~Image()
@ -75,30 +76,30 @@ bool e2d::Image::open(int resNameId, const String& resType)
return true;
}
void e2d::Image::crop(double x, double y, double width, double height)
void e2d::Image::crop(const Rect& cropRect)
{
if (_bitmap)
{
_cropX = min(max(x, 0), this->getSourceWidth());
_cropY = min(max(y, 0), this->getSourceHeight());
_cropWidth = min(max(width, 0), this->getSourceWidth() - _cropX);
_cropHeight = min(max(height, 0), this->getSourceHeight() - _cropY);
_cropRect.origin.x = min(max(cropRect.origin.x, 0), this->getSourceWidth());
_cropRect.origin.y = min(max(cropRect.origin.y, 0), this->getSourceHeight());
_cropRect.size.width = min(max(cropRect.size.width, 0), this->getSourceWidth() - cropRect.origin.x);
_cropRect.size.height = min(max(cropRect.size.height, 0), this->getSourceHeight() - cropRect.origin.y);
}
}
double e2d::Image::getWidth() const
{
return _cropWidth;
return _cropRect.size.width;
}
double e2d::Image::getHeight() const
{
return _cropHeight;
return _cropRect.size.height;
}
e2d::Size e2d::Image::getSize() const
{
return Size(_cropWidth, _cropHeight);
return _cropRect.size;
}
double e2d::Image::getSourceWidth() const
@ -139,17 +140,17 @@ e2d::Size e2d::Image::getSourceSize() const
double e2d::Image::getCropX() const
{
return _cropX;
return _cropRect.origin.x;
}
double e2d::Image::getCropY() const
{
return _cropY;
return _cropRect.origin.y;
}
e2d::Point e2d::Image::getCropPos() const
{
return Point(_cropX, _cropY);
return _cropRect.origin;
}
bool e2d::Image::preload(const String& filePath)
@ -384,9 +385,9 @@ void e2d::Image::_setBitmap(ID2D1Bitmap * bitmap)
if (bitmap)
{
_bitmap = bitmap;
_cropX = _cropY = 0;
_cropWidth = _bitmap->GetSize().width;
_cropHeight = _bitmap->GetSize().height;
_cropRect.origin.x = _cropRect.origin.y = 0;
_cropRect.size.width = _bitmap->GetSize().width;
_cropRect.size.height = _bitmap->GetSize().height;
}
}

View File

@ -13,6 +13,12 @@ e2d::Point::Point(double x, double 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
{
return Point(x + p.x, y + p.y);

View File

@ -12,6 +12,12 @@ e2d::Size::Size(double width, double 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
{
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);
}
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)
{
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)
{
open(resNameId, resType);
crop(x, y, width, height);
crop(cropRect);
}
e2d::Sprite::~Sprite()
@ -88,12 +88,12 @@ bool e2d::Sprite::open(int resNameId, const String& resType)
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(
min(max(width, 0), _image->getSourceWidth() - _image->getCropX()),
min(max(height, 0), _image->getSourceHeight() - _image->getCropY())
min(max(cropRect.size.width, 0), _image->getSourceWidth() - _image->getCropX()),
min(max(cropRect.size.height, 0), _image->getSourceHeight() - _image->getCropY())
);
}

View File

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

View File

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