diff options
Diffstat (limited to 'scene/2d/sprite_2d.cpp')
-rw-r--r-- | scene/2d/sprite_2d.cpp | 57 |
1 files changed, 38 insertions, 19 deletions
diff --git a/scene/2d/sprite_2d.cpp b/scene/2d/sprite_2d.cpp index ce350f167e..7e07019578 100644 --- a/scene/2d/sprite_2d.cpp +++ b/scene/2d/sprite_2d.cpp @@ -96,25 +96,29 @@ void Sprite2D::_get_rects(Rect2 &r_src_rect, Rect2 &r_dst_rect, bool &r_filter_c r_src_rect.position = base_rect.position + frame_offset; Point2 dest_offset = offset; - if (centered) + if (centered) { dest_offset -= frame_size / 2; + } if (Engine::get_singleton()->get_use_pixel_snap()) { dest_offset = dest_offset.floor(); } r_dst_rect = Rect2(dest_offset, frame_size); - if (hflip) + if (hflip) { r_dst_rect.size.x = -r_dst_rect.size.x; - if (vflip) + } + if (vflip) { r_dst_rect.size.y = -r_dst_rect.size.y; + } } void Sprite2D::_notification(int p_what) { switch (p_what) { case NOTIFICATION_DRAW: { - if (texture.is_null()) + if (texture.is_null()) { return; + } RID ci = get_canvas_item(); @@ -133,16 +137,19 @@ void Sprite2D::_notification(int p_what) { } void Sprite2D::set_texture(const Ref<Texture2D> &p_texture) { - if (p_texture == texture) + if (p_texture == texture) { return; + } - if (texture.is_valid()) + if (texture.is_valid()) { texture->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(this, &Sprite2D::_texture_changed)); + } texture = p_texture; - if (texture.is_valid()) + if (texture.is_valid()) { texture->connect(CoreStringNames::get_singleton()->changed, callable_mp(this, &Sprite2D::_texture_changed)); + } update(); emit_signal("texture_changed"); @@ -230,8 +237,9 @@ bool Sprite2D::is_flipped_v() const { } void Sprite2D::set_region(bool p_region) { - if (p_region == region) + if (p_region == region) { return; + } region = p_region; update(); @@ -242,13 +250,15 @@ bool Sprite2D::is_region() const { } void Sprite2D::set_region_rect(const Rect2 &p_region_rect) { - if (region_rect == p_region_rect) + if (region_rect == p_region_rect) { return; + } region_rect = p_region_rect; - if (region) + if (region) { item_rect_changed(); + } _change_notify("region_rect"); } @@ -269,8 +279,9 @@ bool Sprite2D::is_region_filter_clip_enabled() const { void Sprite2D::set_frame(int p_frame) { ERR_FAIL_INDEX(p_frame, vframes * hframes); - if (frame != p_frame) + if (frame != p_frame) { item_rect_changed(); + } frame = p_frame; @@ -319,25 +330,30 @@ int Sprite2D::get_hframes() const { } bool Sprite2D::is_pixel_opaque(const Point2 &p_point) const { - if (texture.is_null()) + if (texture.is_null()) { return false; + } - if (texture->get_size().width == 0 || texture->get_size().height == 0) + if (texture->get_size().width == 0 || texture->get_size().height == 0) { return false; + } Rect2 src_rect, dst_rect; bool filter_clip; _get_rects(src_rect, dst_rect, filter_clip); dst_rect.size = dst_rect.size.abs(); - if (!dst_rect.has_point(p_point)) + if (!dst_rect.has_point(p_point)) { return false; + } Vector2 q = (p_point - dst_rect.position) / dst_rect.size; - if (hflip) + if (hflip) { q.x = 1.0f - q.x; - if (vflip) + } + if (vflip) { q.y = 1.0f - q.y; + } q = q * src_rect.size + src_rect.position; #ifndef _MSC_VER #warning this need to be obtained from CanvasItem new repeat mode (but it needs to guess it from hierarchy, need to add a function for that) @@ -368,8 +384,9 @@ bool Sprite2D::is_pixel_opaque(const Point2 &p_point) const { } Rect2 Sprite2D::get_rect() const { - if (texture.is_null()) + if (texture.is_null()) { return Rect2(0, 0, 1, 1); + } Size2i s; @@ -382,11 +399,13 @@ Rect2 Sprite2D::get_rect() const { s = s / Point2(hframes, vframes); Point2 ofs = offset; - if (centered) + if (centered) { ofs -= Size2(s) / 2; + } - if (s == Size2(0, 0)) + if (s == Size2(0, 0)) { s = Size2(1, 1); + } return Rect2(ofs, s); } |