diff --git a/core/components/Button.cpp b/core/components/Button.cpp index 61e16316..e7d2a316 100644 --- a/core/components/Button.cpp +++ b/core/components/Button.cpp @@ -10,7 +10,7 @@ if (Old) this->RemoveChild(Old); \ if (New) \ { \ - New->SetAnchor(anchor_.x, anchor_.y); \ + New->SetPivot(GetPivotX(), GetPivotY()); \ this->AddChild(New); \ } \ Old = New; \ @@ -135,13 +135,13 @@ void e2d::Button::SetCallbackOnClick(const Function& func) callback_ = func; } -void e2d::Button::SetAnchor(float anchor_x, float anchor_y) +void e2d::Button::SetPivot(float pivot_x, float pivot_y) { - Node::SetAnchor(anchor_x, anchor_y); - SAFE_SET(normal_, SetAnchor, anchor_x, anchor_y); - SAFE_SET(mouseover_, SetAnchor, anchor_x, anchor_y); - SAFE_SET(selected_, SetAnchor, anchor_x, anchor_y); - SAFE_SET(disabled_, SetAnchor, anchor_x, anchor_y); + Node::SetPivot(pivot_x, pivot_y); + SAFE_SET(normal_, SetPivot, pivot_x, pivot_y); + SAFE_SET(mouseover_, SetPivot, pivot_x, pivot_y); + SAFE_SET(selected_, SetPivot, pivot_x, pivot_y); + SAFE_SET(disabled_, SetPivot, pivot_x, pivot_y); } bool e2d::Button::Dispatch(const MouseEvent & e, bool handled) diff --git a/core/e2dcomponent.h b/core/e2dcomponent.h index e78b40ca..646392ea 100644 --- a/core/e2dcomponent.h +++ b/core/e2dcomponent.h @@ -72,9 +72,9 @@ namespace e2d // 设置支点位置 // 默认为 (0, 0), 范围 [0, 1] - virtual void SetAnchor( - float anchor_x, - float anchor_y + virtual void SetPivot( + float pivot_x, + float pivot_y ) override; protected: diff --git a/core/e2dobject.h b/core/e2dobject.h index 17ab12b7..055f9874 100644 --- a/core/e2dobject.h +++ b/core/e2dobject.h @@ -468,10 +468,10 @@ namespace e2d const Size& GetRealSize() const; // 获取节点的支点 - float GetAnchorX() const; + float GetPivotX() const; // 获取节点的支点 - float GetAnchorY() const; + float GetPivotY() const; // 获取节点大小 Size GetSize() const; @@ -612,21 +612,21 @@ namespace e2d // 设置支点的横向位置 // 默认为 0, 范围 [0, 1] - virtual void SetAnchorX( - float anchor_x + virtual void SetPivotX( + float pivot_x ); // 设置支点的纵向位置 // 默认为 0, 范围 [0, 1] - virtual void SetAnchorY( - float anchor_y + virtual void SetPivotY( + float pivot_y ); // 设置支点位置 // 默认为 (0, 0), 范围 [0, 1] - virtual void SetAnchor( - float anchor_x, - float anchor_y + virtual void SetPivot( + float pivot_x, + float pivot_y ); // 修改节点宽度 diff --git a/core/objects/Image.cpp b/core/objects/Image.cpp index 22f081fa..941151d7 100644 --- a/core/objects/Image.cpp +++ b/core/objects/Image.cpp @@ -285,6 +285,10 @@ bool e2d::Image::Preload(const Resource& res) bool e2d::Image::Preload(const String & file_name) { + size_t hash = file_name.GetHash(); + if (bitmap_cache_.find(hash) != bitmap_cache_.end()) + return true; + File image_file; if (!image_file.Open(file_name)) return false; @@ -292,9 +296,6 @@ bool e2d::Image::Preload(const String & file_name) // 用户输入的路径不一定是完整路径,因为用户可能通过 File::AddSearchPath 添加 // 默认搜索路径,所以需要通过 File::GetPath 获取完整路径 String image_file_path = image_file.GetPath(); - size_t hash = image_file_path.GetHash(); - if (bitmap_cache_.find(hash) != bitmap_cache_.end()) - return true; IWICImagingFactory *imaging_factory = Renderer::GetImagingFactory(); ID2D1HwndRenderTarget* render_target = Renderer::GetInstance()->GetRenderTarget(); diff --git a/core/objects/Node.cpp b/core/objects/Node.cpp index 852b6975..8f8e22b0 100644 --- a/core/objects/Node.cpp +++ b/core/objects/Node.cpp @@ -365,12 +365,12 @@ const e2d::Size& e2d::Node::GetRealSize() const return transform_.size; } -float e2d::Node::GetAnchorX() const +float e2d::Node::GetPivotX() const { return transform_.pivot_x; } -float e2d::Node::GetAnchorY() const +float e2d::Node::GetPivotY() const { return transform_.pivot_y; } @@ -536,23 +536,23 @@ void e2d::Node::SetOpacity(float opacity) UpdateOpacity(); } -void e2d::Node::SetAnchorX(float anchor_x) +void e2d::Node::SetPivotX(float pivot_x) { - this->SetAnchor(anchor_x, transform_.pivot_y); + this->SetPivot(pivot_x, transform_.pivot_y); } -void e2d::Node::SetAnchorY(float anchor_y) +void e2d::Node::SetPivotY(float pivot_y) { - this->SetAnchor(transform_.pivot_x, anchor_y); + this->SetPivot(transform_.pivot_x, pivot_y); } -void e2d::Node::SetAnchor(float anchor_x, float anchor_y) +void e2d::Node::SetPivot(float pivot_x, float pivot_y) { - if (transform_.pivot_x == anchor_x && transform_.pivot_y == anchor_y) + if (transform_.pivot_x == pivot_x && transform_.pivot_y == pivot_y) return; - transform_.pivot_x = anchor_x; - transform_.pivot_y = anchor_y; + transform_.pivot_x = pivot_x; + transform_.pivot_y = pivot_y; dirty_transform_ = true; } diff --git a/core/objects/Sprite.cpp b/core/objects/Sprite.cpp index 6a13bb76..12a39f55 100644 --- a/core/objects/Sprite.cpp +++ b/core/objects/Sprite.cpp @@ -93,15 +93,6 @@ bool e2d::Sprite::Open(const String & file_name) return false; } -void e2d::Sprite::Crop(const Rect& crop_rect) -{ - image_->Crop(crop_rect); - Node::SetSize( - std::min(std::max(crop_rect.size.width, 0.f), image_->GetSourceWidth() - image_->GetCropX()), - std::min(std::max(crop_rect.size.height, 0.f), image_->GetSourceHeight() - image_->GetCropY()) - ); -} - e2d::Image * e2d::Sprite::GetImage() const { return image_; @@ -112,19 +103,18 @@ void e2d::Sprite::Draw() const if (image_ && image_->GetBitmap()) { // 获取图片裁剪位置 - float fCropX = image_->GetCropX(); - float fCropY = image_->GetCropY(); + auto crop_pos = image_->GetCropPos(); // 渲染图片 Renderer::GetInstance()->GetRenderTarget()->DrawBitmap( image_->GetBitmap(), - D2D1::RectF(0, 0, size_.width, size_.height), + D2D1::RectF(0, 0, transform_.size.width, transform_.size.height), display_opacity_, D2D1_BITMAP_INTERPOLATION_MODE_LINEAR, D2D1::RectF( - fCropX, - fCropY, - fCropX + size_.width, - fCropY + size_.height + crop_pos.x, + crop_pos.y, + crop_pos.y + transform_.size.width, + crop_pos.y + transform_.size.height ) ); } diff --git a/core/objects/Text.cpp b/core/objects/Text.cpp index bf7e3505..cac7ba48 100644 --- a/core/objects/Text.cpp +++ b/core/objects/Text.cpp @@ -294,7 +294,7 @@ void e2d::Text::Draw() const { auto renderer = Renderer::GetInstance(); // 创建文本区域 - D2D1_RECT_F textLayoutRect = D2D1::RectF(0, 0, size_.width, size_.height); + D2D1_RECT_F textLayoutRect = D2D1::RectF(0, 0, transform_.size.width, transform_.size.height); // 设置画刷颜色和透明度 renderer->GetSolidBrush()->SetOpacity(display_opacity_); // 获取文本渲染器 @@ -429,7 +429,7 @@ void e2d::Text::CreateLayout() (const wchar_t *)text_, length, text_format_, - size_.width, + transform_.size.width, 0, &text_layout_ )