From f91973255d624eb2f1da2dff5414abb661e4a8d6 Mon Sep 17 00:00:00 2001 From: Nomango <569629550@qq.com> Date: Tue, 22 May 2018 23:36:46 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86=E5=9B=BE=E7=89=87=E8=A3=81=E5=89=AA?= =?UTF-8?q?=E7=9A=84=E5=8F=82=E6=95=B0=E6=9B=BF=E6=8D=A2=E4=B8=BARect?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/Common/Image.cpp | 45 ++++++++++++++++++++-------------------- core/Common/Point.cpp | 6 ++++++ core/Common/Size.cpp | 6 ++++++ core/Node/Sprite.cpp | 16 +++++++-------- core/e2dcommon.h | 48 ++++++++++++------------------------------- core/e2dnode.h | 15 +++----------- 6 files changed, 59 insertions(+), 77 deletions(-) diff --git a/core/Common/Image.cpp b/core/Common/Image.cpp index 6827178a..0c101835 100644 --- a/core/Common/Image.cpp +++ b/core/Common/Image.cpp @@ -9,37 +9,38 @@ static std::set 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; } } diff --git a/core/Common/Point.cpp b/core/Common/Point.cpp index 1a05f43b..c22cd854 100644 --- a/core/Common/Point.cpp +++ b/core/Common/Point.cpp @@ -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); diff --git a/core/Common/Size.cpp b/core/Common/Size.cpp index c7ae6e69..cacb84cc 100644 --- a/core/Common/Size.cpp +++ b/core/Common/Size.cpp @@ -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); diff --git a/core/Node/Sprite.cpp b/core/Node/Sprite.cpp index 0cc29279..8db6dd8b 100644 --- a/core/Node/Sprite.cpp +++ b/core/Node/Sprite.cpp @@ -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()) ); } diff --git a/core/e2dcommon.h b/core/e2dcommon.h index de7de2c1..81b67cef 100644 --- a/core/e2dcommon.h +++ b/core/e2dcommon.h @@ -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; }; diff --git a/core/e2dnode.h b/core/e2dnode.h index 40088992..25c166a2 100644 --- a/core/e2dnode.h +++ b/core/e2dnode.h @@ -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 对象