Merge pull request #25327 from groud/frame_row_column

Add frame_coords accessor to Sprite
This commit is contained in:
Rémi Verschelde 2019-07-27 14:09:45 +02:00 committed by GitHub
commit 6c9ccf18f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 46 additions and 0 deletions

View File

@ -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>

View File

@ -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>

View File

@ -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");

View File

@ -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;

View File

@ -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");

View File

@ -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;