From 11cb4eb3638b0f97e1efde1ac54a3c7a94ea9d1b Mon Sep 17 00:00:00 2001 From: groud Date: Fri, 25 Jan 2019 19:21:23 +0100 Subject: [PATCH] Add frame_cords accessors to Sprite and Sprite3D --- doc/classes/Sprite.xml | 3 +++ doc/classes/Sprite3D.xml | 3 +++ scene/2d/sprite.cpp | 16 ++++++++++++++++ scene/2d/sprite.h | 3 +++ scene/3d/sprite_3d.cpp | 18 ++++++++++++++++++ scene/3d/sprite_3d.h | 3 +++ 6 files changed, 46 insertions(+) diff --git a/doc/classes/Sprite.xml b/doc/classes/Sprite.xml index e5aea961f41..35db0b37628 100644 --- a/doc/classes/Sprite.xml +++ b/doc/classes/Sprite.xml @@ -46,6 +46,9 @@ Current frame to display from sprite sheet. [member vframes] or [member hframes] must be greater than 1. + + Coordinates of the frame to display from sprite sheet. This is as an alias for the [member frame] property. [member vframes] or [member hframes] must be greater than 1. + The number of columns in the sprite sheet. diff --git a/doc/classes/Sprite3D.xml b/doc/classes/Sprite3D.xml index 9a51302bf1d..be15b066aac 100644 --- a/doc/classes/Sprite3D.xml +++ b/doc/classes/Sprite3D.xml @@ -14,6 +14,9 @@ Current frame to display from sprite sheet. [member vframes] or [member hframes] must be greater than 1. + + Coordinates of the frame to display from sprite sheet. This is as an alias for the [member frame] property. [member vframes] or [member hframes] must be greater than 1. + The number of columns in the sprite sheet. diff --git a/scene/2d/sprite.cpp b/scene/2d/sprite.cpp index dbe47e89ece..d7a8005187b 100644 --- a/scene/2d/sprite.cpp +++ b/scene/2d/sprite.cpp @@ -259,6 +259,7 @@ void Sprite::set_frame(int p_frame) { frame = p_frame; _change_notify("frame"); + _change_notify("frame_coords"); emit_signal(SceneStringNames::get_singleton()->frame_changed); } @@ -267,6 +268,17 @@ int Sprite::get_frame() const { return frame; } +void Sprite::set_frame_coords(const Vector2 &p_coord) { + ERR_FAIL_INDEX(int(p_coord.x), vframes); + ERR_FAIL_INDEX(int(p_coord.y), hframes); + + set_frame(int(p_coord.y) * hframes + int(p_coord.x)); +} + +Vector2 Sprite::get_frame_coords() const { + return Vector2(frame % hframes, frame / hframes); +} + void Sprite::set_vframes(int p_amount) { ERR_FAIL_COND(p_amount < 1); @@ -420,6 +432,9 @@ void Sprite::_bind_methods() { ClassDB::bind_method(D_METHOD("set_frame", "frame"), &Sprite::set_frame); ClassDB::bind_method(D_METHOD("get_frame"), &Sprite::get_frame); + ClassDB::bind_method(D_METHOD("set_frame_coords", "coords"), &Sprite::set_frame_coords); + ClassDB::bind_method(D_METHOD("get_frame_coords"), &Sprite::get_frame_coords); + ClassDB::bind_method(D_METHOD("set_vframes", "vframes"), &Sprite::set_vframes); ClassDB::bind_method(D_METHOD("get_vframes"), &Sprite::get_vframes); @@ -442,6 +457,7 @@ void Sprite::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::INT, "vframes", PROPERTY_HINT_RANGE, "1,16384,1"), "set_vframes", "get_vframes"); ADD_PROPERTY(PropertyInfo(Variant::INT, "hframes", PROPERTY_HINT_RANGE, "1,16384,1"), "set_hframes", "get_hframes"); ADD_PROPERTY(PropertyInfo(Variant::INT, "frame"), "set_frame", "get_frame"); + ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "frame_coords", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR), "set_frame_coords", "get_frame_coords"); ADD_GROUP("Region", "region_"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "region_enabled"), "set_region", "is_region"); diff --git a/scene/2d/sprite.h b/scene/2d/sprite.h index e38db3a2998..490db31cdb7 100644 --- a/scene/2d/sprite.h +++ b/scene/2d/sprite.h @@ -110,6 +110,9 @@ public: void set_frame(int p_frame); int get_frame() const; + void set_frame_coords(const Vector2 &p_row); + Vector2 get_frame_coords() const; + void set_vframes(int p_amount); int get_vframes() const; diff --git a/scene/3d/sprite_3d.cpp b/scene/3d/sprite_3d.cpp index b39762fde8e..fc5523633df 100644 --- a/scene/3d/sprite_3d.cpp +++ b/scene/3d/sprite_3d.cpp @@ -564,6 +564,9 @@ void Sprite3D::set_frame(int p_frame) { frame = p_frame; _queue_update(); + + _change_notify("frame"); + _change_notify("frame_coords"); emit_signal(SceneStringNames::get_singleton()->frame_changed); } @@ -572,6 +575,17 @@ int Sprite3D::get_frame() const { return frame; } +void Sprite3D::set_frame_coords(const Vector2 &p_coord) { + ERR_FAIL_INDEX(int(p_coord.x), vframes); + ERR_FAIL_INDEX(int(p_coord.y), hframes); + + set_frame(int(p_coord.y) * hframes + int(p_coord.x)); +} + +Vector2 Sprite3D::get_frame_coords() const { + return Vector2(frame % hframes, frame / hframes); +} + void Sprite3D::set_vframes(int p_amount) { ERR_FAIL_COND(p_amount < 1); @@ -648,6 +662,9 @@ void Sprite3D::_bind_methods() { ClassDB::bind_method(D_METHOD("set_frame", "frame"), &Sprite3D::set_frame); ClassDB::bind_method(D_METHOD("get_frame"), &Sprite3D::get_frame); + ClassDB::bind_method(D_METHOD("set_frame_coords", "coords"), &Sprite3D::set_frame_coords); + ClassDB::bind_method(D_METHOD("get_frame_coords"), &Sprite3D::get_frame_coords); + ClassDB::bind_method(D_METHOD("set_vframes", "vframes"), &Sprite3D::set_vframes); ClassDB::bind_method(D_METHOD("get_vframes"), &Sprite3D::get_vframes); @@ -659,6 +676,7 @@ void Sprite3D::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::INT, "vframes", PROPERTY_HINT_RANGE, "1,16384,1"), "set_vframes", "get_vframes"); ADD_PROPERTY(PropertyInfo(Variant::INT, "hframes", PROPERTY_HINT_RANGE, "1,16384,1"), "set_hframes", "get_hframes"); ADD_PROPERTY(PropertyInfo(Variant::INT, "frame"), "set_frame", "get_frame"); + ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "frame_coords", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR), "set_frame_coords", "get_frame_coords"); ADD_GROUP("Region", "region_"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "region_enabled"), "set_region", "is_region"); ADD_PROPERTY(PropertyInfo(Variant::RECT2, "region_rect"), "set_region_rect", "get_region_rect"); diff --git a/scene/3d/sprite_3d.h b/scene/3d/sprite_3d.h index 0e086ef31e5..5ae9b153f95 100644 --- a/scene/3d/sprite_3d.h +++ b/scene/3d/sprite_3d.h @@ -173,6 +173,9 @@ public: void set_frame(int p_frame); int get_frame() const; + void set_frame_coords(const Vector2 &p_row); + Vector2 get_frame_coords() const; + void set_vframes(int p_amount); int get_vframes() const;