Skip to content

Commit

Permalink
implement force pixel snapping for sprites
Browse files Browse the repository at this point in the history
  • Loading branch information
Valla-Chan committed Jan 19, 2024
1 parent df56c63 commit 49b5dc9
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 4 deletions.
6 changes: 6 additions & 0 deletions doc/classes/AnimatedSprite.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@
<member name="animation" type="String" setter="set_animation" getter="get_animation" default="&quot;default&quot;">
The current animation from the [member frames] resource. If this value changes, the [code]frame[/code] counter is reset.
</member>
<member name="force_pixel_snapping" type="bool" setter="set_force_pixel_snapping" getter="get_force_pixel_snapping" default="false">
If [code]true[/code], this sprite will snap to pixel increments.
</member>
<member name="basealigned" type="bool" setter="set_basealigned" getter="is_basealigned" default="false">
If [code]true[/code], texture will be aligned to the bottom.
</member>
<member name="centered" type="bool" setter="set_centered" getter="is_centered" default="true">
If [code]true[/code], texture will be centered.
</member>
Expand Down
6 changes: 6 additions & 0 deletions doc/classes/SpriteBase3D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@
</member>
<member name="billboard" type="int" setter="set_billboard_mode" getter="get_billboard_mode" enum="SpatialMaterial.BillboardMode" default="0">
</member>
<member name="force_pixel_snapping" type="bool" setter="set_force_pixel_snapping" getter="get_force_pixel_snapping" default="false">
If [code]true[/code], this sprite will snap to pixel increments.
</member>
<member name="basealigned" type="bool" setter="set_basealigned" getter="is_basealigned" default="false">
If [code]true[/code], texture will be aligned to the bottom.
</member>
<member name="centered" type="bool" setter="set_centered" getter="is_centered" default="true">
If [code]true[/code], texture will be centered.
</member>
Expand Down
15 changes: 14 additions & 1 deletion scene/2d/animated_sprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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");
Expand All @@ -822,6 +834,7 @@ AnimatedSprite::AnimatedSprite() {
basealigned = false;
hflip = false;
vflip = false;
force_pixel_snapping = false;

frame = 0;
speed_scale = 1.0f;
Expand Down
4 changes: 4 additions & 0 deletions scene/2d/animated_sprite.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ class AnimatedSprite : public Node2D {
int frame;
float speed_scale;

bool force_pixel_snapping;
bool centered;
bool basealigned;
Point2 offset;
Expand Down Expand Up @@ -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;

Expand Down
17 changes: 15 additions & 2 deletions scene/2d/sprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -179,6 +179,14 @@ Ref<Texture> 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();
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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");
Expand All @@ -507,6 +519,7 @@ Sprite::Sprite() {
vflip = false;
region = false;
region_filter_clip = false;
force_pixel_snapping = false;

frame = 0;

Expand Down
6 changes: 5 additions & 1 deletion scene/2d/sprite.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ class Sprite : public Node2D {
Ref<Texture> texture;
Ref<Texture> normal_map;

bool basealigned;
bool force_pixel_snapping;
bool centered;
bool basealigned;
Point2 offset;

bool hflip;
Expand Down Expand Up @@ -88,6 +89,9 @@ class Sprite : public Node2D {
void set_normal_map(const Ref<Texture> &p_texture);
Ref<Texture> 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;

Expand Down

0 comments on commit 49b5dc9

Please sign in to comment.