diff --git a/doc/classes/AnimatedSprite.xml b/doc/classes/AnimatedSprite.xml index e2837386..d92e261b 100644 --- a/doc/classes/AnimatedSprite.xml +++ b/doc/classes/AnimatedSprite.xml @@ -31,6 +31,12 @@ The current animation from the [member frames] resource. If this value changes, the [code]frame[/code] counter is reset. + + If [code]true[/code], this sprite will snap to pixel increments. + + + If [code]true[/code], texture will be aligned to the bottom. + If [code]true[/code], texture will be centered. diff --git a/doc/classes/SpriteBase3D.xml b/doc/classes/SpriteBase3D.xml index 0ace81c2..7b69fc6b 100644 --- a/doc/classes/SpriteBase3D.xml +++ b/doc/classes/SpriteBase3D.xml @@ -44,6 +44,12 @@ + + If [code]true[/code], this sprite will snap to pixel increments. + + + If [code]true[/code], texture will be aligned to the bottom. + If [code]true[/code], texture will be centered. diff --git a/scene/2d/animated_sprite.cpp b/scene/2d/animated_sprite.cpp index 9a7ac70f..187ff256 100644 --- a/scene/2d/animated_sprite.cpp +++ b/scene/2d/animated_sprite.cpp @@ -498,7 +498,7 @@ void AnimatedSprite::_notification(int p_what) { } } - if (Engine::get_singleton()->get_use_gpu_pixel_snap()) { + if (Engine::get_singleton()->get_use_gpu_pixel_snap() || force_pixel_snapping) { ofs = ofs.floor(); } Rect2 dst_rect(ofs, s); @@ -573,6 +573,14 @@ int AnimatedSprite::get_frame() const { return frame; } +void AnimatedSprite::set_force_pixel_snapping(bool p_snapping) { + force_pixel_snapping = p_snapping; +} + +bool AnimatedSprite::get_force_pixel_snapping() const { + return force_pixel_snapping; +} + void AnimatedSprite::set_speed_scale(float p_speed_scale) { float elapsed = _get_frame_duration() - timeout; @@ -776,6 +784,9 @@ void AnimatedSprite::_bind_methods() { ClassDB::bind_method(D_METHOD("play", "anim", "backwards"), &AnimatedSprite::play, DEFVAL(StringName()), DEFVAL(false)); ClassDB::bind_method(D_METHOD("stop"), &AnimatedSprite::stop); + ClassDB::bind_method(D_METHOD("set_force_pixel_snapping", "snap"), &AnimatedSprite::set_force_pixel_snapping); + ClassDB::bind_method(D_METHOD("get_force_pixel_snapping"), &AnimatedSprite::get_force_pixel_snapping); + ClassDB::bind_method(D_METHOD("set_centered", "centered"), &AnimatedSprite::set_centered); ClassDB::bind_method(D_METHOD("is_centered"), &AnimatedSprite::is_centered); @@ -809,6 +820,7 @@ void AnimatedSprite::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::BOOL, "playing"), "set_playing", "is_playing"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "centered"), "set_centered", "is_centered"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "basealigned"), "set_basealigned", "is_basealigned"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "force_pixel_snapping"), "set_force_pixel_snapping", "get_force_pixel_snapping"); ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "offset"), "set_offset", "get_offset"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_h"), "set_flip_h", "is_flipped_h"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_v"), "set_flip_v", "is_flipped_v"); @@ -822,6 +834,7 @@ AnimatedSprite::AnimatedSprite() { basealigned = false; hflip = false; vflip = false; + force_pixel_snapping = false; frame = 0; speed_scale = 1.0f; diff --git a/scene/2d/animated_sprite.h b/scene/2d/animated_sprite.h index 2117786d..db4d74ab 100644 --- a/scene/2d/animated_sprite.h +++ b/scene/2d/animated_sprite.h @@ -153,6 +153,7 @@ class AnimatedSprite : public Node2D { int frame; float speed_scale; + bool force_pixel_snapping; bool centered; bool basealigned; Point2 offset; @@ -208,6 +209,9 @@ class AnimatedSprite : public Node2D { void set_speed_scale(float p_speed_scale); float get_speed_scale() const; + void set_force_pixel_snapping(bool p_snapping); + bool get_force_pixel_snapping() const; + void set_centered(bool p_center); bool is_centered() const; diff --git a/scene/2d/sprite.cpp b/scene/2d/sprite.cpp index 3cc31aa7..4d57d101 100644 --- a/scene/2d/sprite.cpp +++ b/scene/2d/sprite.cpp @@ -108,7 +108,7 @@ void Sprite::_get_rects(Rect2 &r_src_rect, Rect2 &r_dst_rect, bool &r_filter_cli } - if (Engine::get_singleton()->get_use_gpu_pixel_snap()) { + if (Engine::get_singleton()->get_use_gpu_pixel_snap() || force_pixel_snapping) { dest_offset = dest_offset.floor(); } @@ -179,6 +179,14 @@ Ref Sprite::get_texture() const { return texture; } +void Sprite::set_force_pixel_snapping(bool p_snapping) { + force_pixel_snapping = p_snapping; +} + +bool Sprite::get_force_pixel_snapping() const { + return force_pixel_snapping; +} + void Sprite::set_centered(bool p_center) { centered = p_center; update(); @@ -397,7 +405,7 @@ Rect2 Sprite::get_rect() const { } } - if (Engine::get_singleton()->get_use_gpu_pixel_snap()) { + if (Engine::get_singleton()->get_use_gpu_pixel_snap() || force_pixel_snapping) { ofs = ofs.floor(); } @@ -435,6 +443,9 @@ void Sprite::_bind_methods() { ClassDB::bind_method(D_METHOD("set_normal_map", "normal_map"), &Sprite::set_normal_map); ClassDB::bind_method(D_METHOD("get_normal_map"), &Sprite::get_normal_map); + ClassDB::bind_method(D_METHOD("set_force_pixel_snapping", "snap"), &Sprite::set_force_pixel_snapping); + ClassDB::bind_method(D_METHOD("get_force_pixel_snapping"), &Sprite::get_force_pixel_snapping); + ClassDB::bind_method(D_METHOD("set_centered", "centered"), &Sprite::set_centered); ClassDB::bind_method(D_METHOD("is_centered"), &Sprite::is_centered); @@ -485,6 +496,7 @@ void Sprite::_bind_methods() { ADD_GROUP("Offset", ""); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "centered"), "set_centered", "is_centered"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "basealigned"), "set_basealigned", "is_basealigned"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "force_pixel_snapping"), "set_force_pixel_snapping", "get_force_pixel_snapping"); ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "offset"), "set_offset", "get_offset"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_h"), "set_flip_h", "is_flipped_h"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_v"), "set_flip_v", "is_flipped_v"); @@ -507,6 +519,7 @@ Sprite::Sprite() { vflip = false; region = false; region_filter_clip = false; + force_pixel_snapping = false; frame = 0; diff --git a/scene/2d/sprite.h b/scene/2d/sprite.h index 2e338456..0f8a3005 100644 --- a/scene/2d/sprite.h +++ b/scene/2d/sprite.h @@ -40,8 +40,9 @@ class Sprite : public Node2D { Ref texture; Ref normal_map; - bool basealigned; + bool force_pixel_snapping; bool centered; + bool basealigned; Point2 offset; bool hflip; @@ -88,6 +89,9 @@ class Sprite : public Node2D { void set_normal_map(const Ref &p_texture); Ref get_normal_map() const; + void set_force_pixel_snapping(bool p_snapping); + bool get_force_pixel_snapping() const; + void set_centered(bool p_center); bool is_centered() const;