diff options
author | RĂ©mi Verschelde <rverschelde@gmail.com> | 2019-07-27 14:09:45 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-07-27 14:09:45 +0200 |
commit | 6c9ccf18f87a662e1994c870088a6beaaeeb0384 (patch) | |
tree | 1ec767bcbfeb83670b0ddd08e6b2c06935a6f3a4 | |
parent | 94aabf56a0a22df689bc57c71981c0008eb40464 (diff) | |
parent | 11cb4eb3638b0f97e1efde1ac54a3c7a94ea9d1b (diff) |
Merge pull request #25327 from groud/frame_row_column
Add frame_coords accessor to Sprite
-rw-r--r-- | doc/classes/Sprite.xml | 3 | ||||
-rw-r--r-- | doc/classes/Sprite3D.xml | 3 | ||||
-rw-r--r-- | scene/2d/sprite.cpp | 16 | ||||
-rw-r--r-- | scene/2d/sprite.h | 3 | ||||
-rw-r--r-- | scene/3d/sprite_3d.cpp | 18 | ||||
-rw-r--r-- | scene/3d/sprite_3d.h | 3 |
6 files changed, 46 insertions, 0 deletions
diff --git a/doc/classes/Sprite.xml b/doc/classes/Sprite.xml index e5aea961f4..35db0b3762 100644 --- a/doc/classes/Sprite.xml +++ b/doc/classes/Sprite.xml @@ -46,6 +46,9 @@ <member name="frame" type="int" setter="set_frame" getter="get_frame" default="0"> Current frame to display from sprite sheet. [member vframes] or [member hframes] must be greater than 1. </member> + <member name="frame_coords" type="Vector2" setter="set_frame_coords" getter="get_frame_coords"> + 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. + </member> <member name="hframes" type="int" setter="set_hframes" getter="get_hframes" default="1"> The number of columns in the sprite sheet. </member> diff --git a/doc/classes/Sprite3D.xml b/doc/classes/Sprite3D.xml index 9a51302bf1..be15b066aa 100644 --- a/doc/classes/Sprite3D.xml +++ b/doc/classes/Sprite3D.xml @@ -14,6 +14,9 @@ <member name="frame" type="int" setter="set_frame" getter="get_frame" default="0"> Current frame to display from sprite sheet. [member vframes] or [member hframes] must be greater than 1. </member> + <member name="frame_coords" type="Vector2" setter="set_frame_coords" getter="get_frame_coords"> + 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. + </member> <member name="hframes" type="int" setter="set_hframes" getter="get_hframes" default="1"> The number of columns in the sprite sheet. </member> diff --git a/scene/2d/sprite.cpp b/scene/2d/sprite.cpp index dbe47e89ec..d7a8005187 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 e38db3a299..490db31cdb 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 b39762fde8..fc5523633d 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 0e086ef31e..5ae9b153f9 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; |