| 
									
										
										
										
											2023-01-05 13:25:55 +01:00
										 |  |  | /**************************************************************************/ | 
					
						
							|  |  |  | /*  animated_sprite_2d.cpp                                                */ | 
					
						
							|  |  |  | /**************************************************************************/ | 
					
						
							|  |  |  | /*                         This file is part of:                          */ | 
					
						
							|  |  |  | /*                             GODOT ENGINE                               */ | 
					
						
							|  |  |  | /*                        https://godotengine.org                         */ | 
					
						
							|  |  |  | /**************************************************************************/ | 
					
						
							|  |  |  | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ | 
					
						
							|  |  |  | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur.                  */ | 
					
						
							|  |  |  | /*                                                                        */ | 
					
						
							|  |  |  | /* Permission is hereby granted, free of charge, to any person obtaining  */ | 
					
						
							|  |  |  | /* a copy of this software and associated documentation files (the        */ | 
					
						
							|  |  |  | /* "Software"), to deal in the Software without restriction, including    */ | 
					
						
							|  |  |  | /* without limitation the rights to use, copy, modify, merge, publish,    */ | 
					
						
							|  |  |  | /* distribute, sublicense, and/or sell copies of the Software, and to     */ | 
					
						
							|  |  |  | /* permit persons to whom the Software is furnished to do so, subject to  */ | 
					
						
							|  |  |  | /* the following conditions:                                              */ | 
					
						
							|  |  |  | /*                                                                        */ | 
					
						
							|  |  |  | /* The above copyright notice and this permission notice shall be         */ | 
					
						
							|  |  |  | /* included in all copies or substantial portions of the Software.        */ | 
					
						
							|  |  |  | /*                                                                        */ | 
					
						
							|  |  |  | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,        */ | 
					
						
							|  |  |  | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF     */ | 
					
						
							|  |  |  | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ | 
					
						
							|  |  |  | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY   */ | 
					
						
							|  |  |  | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,   */ | 
					
						
							|  |  |  | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE      */ | 
					
						
							|  |  |  | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                 */ | 
					
						
							|  |  |  | /**************************************************************************/ | 
					
						
							| 
									
										
										
										
											2018-01-05 00:50:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | #include "animated_sprite_2d.h"
 | 
					
						
							| 
									
										
										
										
											2019-07-25 09:11:41 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-29 18:09:16 -03:00
										 |  |  | #include "scene/main/viewport.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-21 23:37:07 +02:00
										 |  |  | #ifdef TOOLS_ENABLED
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | Dictionary AnimatedSprite2D::_edit_get_state() const { | 
					
						
							| 
									
										
										
										
											2017-11-07 08:58:35 +01:00
										 |  |  | 	Dictionary state = Node2D::_edit_get_state(); | 
					
						
							|  |  |  | 	state["offset"] = offset; | 
					
						
							|  |  |  | 	return state; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | void AnimatedSprite2D::_edit_set_state(const Dictionary &p_state) { | 
					
						
							| 
									
										
										
										
											2017-11-07 08:58:35 +01:00
										 |  |  | 	Node2D::_edit_set_state(p_state); | 
					
						
							|  |  |  | 	set_offset(p_state["offset"]); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | void AnimatedSprite2D::_edit_set_pivot(const Point2 &p_pivot) { | 
					
						
							| 
									
										
										
										
											2017-11-07 08:58:35 +01:00
										 |  |  | 	set_offset(get_offset() - p_pivot); | 
					
						
							|  |  |  | 	set_position(get_transform().xform(p_pivot)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | Point2 AnimatedSprite2D::_edit_get_pivot() const { | 
					
						
							| 
									
										
										
										
											2017-11-07 08:58:35 +01:00
										 |  |  | 	return Vector2(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | bool AnimatedSprite2D::_edit_use_pivot() const { | 
					
						
							| 
									
										
										
										
											2017-11-07 08:58:35 +01:00
										 |  |  | 	return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | Rect2 AnimatedSprite2D::_edit_get_rect() const { | 
					
						
							| 
									
										
										
										
											2018-05-05 16:59:00 +02:00
										 |  |  | 	return _get_rect(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | bool AnimatedSprite2D::_edit_use_rect() const { | 
					
						
							| 
									
										
										
										
											2022-08-31 16:15:24 +02:00
										 |  |  | 	if (frames.is_null() || !frames->has_animation(animation)) { | 
					
						
							| 
									
										
										
										
											2018-05-05 16:59:00 +02:00
										 |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-08-31 16:15:24 +02:00
										 |  |  | 	if (frame < 0 || frame >= frames->get_frame_count(animation)) { | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-11 15:43:37 -03:00
										 |  |  | 	Ref<Texture2D> t; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	if (animation) { | 
					
						
							| 
									
										
										
										
											2022-11-26 17:00:38 +03:00
										 |  |  | 		t = frames->get_frame_texture(animation, frame); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-06-26 15:08:25 +02:00
										 |  |  | 	return t.is_valid(); | 
					
						
							| 
									
										
										
										
											2018-05-05 16:59:00 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-10-21 23:37:07 +02:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2018-05-05 16:59:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | Rect2 AnimatedSprite2D::get_anchorable_rect() const { | 
					
						
							| 
									
										
										
										
											2018-05-05 16:59:00 +02:00
										 |  |  | 	return _get_rect(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | Rect2 AnimatedSprite2D::_get_rect() const { | 
					
						
							| 
									
										
										
										
											2022-08-31 16:15:24 +02:00
										 |  |  | 	if (frames.is_null() || !frames->has_animation(animation)) { | 
					
						
							|  |  |  | 		return Rect2(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (frame < 0 || frame >= frames->get_frame_count(animation)) { | 
					
						
							| 
									
										
										
										
											2018-05-05 16:59:00 +02:00
										 |  |  | 		return Rect2(); | 
					
						
							| 
									
										
										
										
											2017-11-07 08:58:35 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-11 15:43:37 -03:00
										 |  |  | 	Ref<Texture2D> t; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	if (animation) { | 
					
						
							| 
									
										
										
										
											2022-11-26 17:00:38 +03:00
										 |  |  | 		t = frames->get_frame_texture(animation, frame); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if (t.is_null()) { | 
					
						
							| 
									
										
										
										
											2018-05-05 16:59:00 +02:00
										 |  |  | 		return Rect2(); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-11-07 08:58:35 +01:00
										 |  |  | 	Size2 s = t->get_size(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Point2 ofs = offset; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	if (centered) { | 
					
						
							| 
									
										
										
										
											2021-09-25 12:18:18 +02:00
										 |  |  | 		ofs -= s / 2; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-11-07 08:58:35 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	if (s == Size2(0, 0)) { | 
					
						
							| 
									
										
										
										
											2017-11-07 08:58:35 +01:00
										 |  |  | 		s = Size2(1, 1); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-11-07 08:58:35 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return Rect2(ofs, s); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-05-14 23:48:23 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-12 23:57:11 +03:00
										 |  |  | void AnimatedSprite2D::_validate_property(PropertyInfo &p_property) const { | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	if (!frames.is_valid()) { | 
					
						
							| 
									
										
										
										
											2016-05-14 23:48:23 -03:00
										 |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-09-12 22:59:35 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-12 23:57:11 +03:00
										 |  |  | 	if (p_property.name == "animation") { | 
					
						
							| 
									
										
										
										
											2016-05-14 23:48:23 -03:00
										 |  |  | 		List<StringName> names; | 
					
						
							|  |  |  | 		frames->get_animation_list(&names); | 
					
						
							|  |  |  | 		names.sort_custom<StringName::AlphCompare>(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		bool current_found = false; | 
					
						
							| 
									
										
										
										
											2021-11-05 21:51:42 +01:00
										 |  |  | 		bool is_first_element = true; | 
					
						
							| 
									
										
										
										
											2016-05-14 23:48:23 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-05 21:51:42 +01:00
										 |  |  | 		for (const StringName &E : names) { | 
					
						
							|  |  |  | 			if (!is_first_element) { | 
					
						
							| 
									
										
										
										
											2022-08-12 23:57:11 +03:00
										 |  |  | 				p_property.hint_string += ","; | 
					
						
							| 
									
										
										
										
											2021-11-05 21:51:42 +01:00
										 |  |  | 			} else { | 
					
						
							|  |  |  | 				is_first_element = false; | 
					
						
							| 
									
										
										
										
											2016-05-14 23:48:23 -03:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-05 21:51:42 +01:00
										 |  |  | 			p_property.hint_string += String(E); | 
					
						
							|  |  |  | 			if (animation == E) { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 				current_found = true; | 
					
						
							| 
									
										
										
										
											2016-05-14 23:48:23 -03:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (!current_found) { | 
					
						
							| 
									
										
										
										
											2022-08-12 23:57:11 +03:00
										 |  |  | 			if (p_property.hint_string.is_empty()) { | 
					
						
							|  |  |  | 				p_property.hint_string = String(animation); | 
					
						
							| 
									
										
										
										
											2016-05-14 23:48:23 -03:00
										 |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2022-08-12 23:57:11 +03:00
										 |  |  | 				p_property.hint_string = String(animation) + "," + p_property.hint_string; | 
					
						
							| 
									
										
										
										
											2016-05-14 23:48:23 -03:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2022-09-12 22:59:35 +02:00
										 |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2016-05-14 23:48:23 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-12 23:57:11 +03:00
										 |  |  | 	if (p_property.name == "frame") { | 
					
						
							| 
									
										
										
										
											2022-09-12 22:59:35 +02:00
										 |  |  | 		if (playing) { | 
					
						
							|  |  |  | 			p_property.usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_READ_ONLY; | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-12 23:57:11 +03:00
										 |  |  | 		p_property.hint = PROPERTY_HINT_RANGE; | 
					
						
							| 
									
										
										
										
											2022-07-03 15:31:43 +02:00
										 |  |  | 		if (frames->has_animation(animation) && frames->get_frame_count(animation) > 0) { | 
					
						
							| 
									
										
										
										
											2022-08-12 23:57:11 +03:00
										 |  |  | 			p_property.hint_string = "0," + itos(frames->get_frame_count(animation) - 1) + ",1"; | 
					
						
							| 
									
										
										
										
											2022-07-03 15:31:43 +02:00
										 |  |  | 		} else { | 
					
						
							|  |  |  | 			// Avoid an error, `hint_string` is required for `PROPERTY_HINT_RANGE`.
 | 
					
						
							| 
									
										
										
										
											2022-08-12 23:57:11 +03:00
										 |  |  | 			p_property.hint_string = "0,0,1"; | 
					
						
							| 
									
										
										
										
											2016-05-14 23:48:23 -03:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2022-08-12 23:57:11 +03:00
										 |  |  | 		p_property.usage |= PROPERTY_USAGE_KEYING_INCREMENTS; | 
					
						
							| 
									
										
										
										
											2016-05-14 23:48:23 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | void AnimatedSprite2D::_notification(int p_what) { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	switch (p_what) { | 
					
						
							| 
									
										
										
										
											2023-01-21 14:51:03 +09:00
										 |  |  | 		case NOTIFICATION_READY: { | 
					
						
							|  |  |  | 			if (!Engine::get_singleton()->is_editor_hint() && !frames.is_null() && frames->has_animation(autoplay)) { | 
					
						
							|  |  |  | 				play(autoplay); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-10 18:02:19 -03:00
										 |  |  | 		case NOTIFICATION_INTERNAL_PROCESS: { | 
					
						
							| 
									
										
										
										
											2022-08-31 16:15:24 +02:00
										 |  |  | 			if (frames.is_null() || !frames->has_animation(animation)) { | 
					
						
							| 
									
										
										
										
											2016-05-14 23:48:23 -03:00
										 |  |  | 				return; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2022-08-31 16:15:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-01 21:16:37 -05:00
										 |  |  | 			double remaining = get_process_delta_time(); | 
					
						
							| 
									
										
										
										
											2022-11-26 17:00:38 +03:00
										 |  |  | 			int i = 0; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 			while (remaining) { | 
					
						
							| 
									
										
										
										
											2022-11-26 17:00:38 +03:00
										 |  |  | 				// Animation speed may be changed by animation_finished or frame_changed signals.
 | 
					
						
							| 
									
										
										
										
											2023-01-21 14:51:03 +09:00
										 |  |  | 				double speed = frames->get_animation_speed(animation) * speed_scale * custom_speed_scale * frame_speed_scale; | 
					
						
							|  |  |  | 				double abs_speed = Math::abs(speed); | 
					
						
							| 
									
										
										
										
											2016-05-14 23:48:23 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-26 17:00:38 +03:00
										 |  |  | 				if (speed == 0) { | 
					
						
							|  |  |  | 					return; // Do nothing.
 | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				// Frame count may be changed by animation_finished or frame_changed signals.
 | 
					
						
							|  |  |  | 				int fc = frames->get_frame_count(animation); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-21 14:51:03 +09:00
										 |  |  | 				int last_frame = fc - 1; | 
					
						
							|  |  |  | 				if (!signbit(speed)) { | 
					
						
							|  |  |  | 					// Forwards.
 | 
					
						
							|  |  |  | 					if (frame_progress >= 1.0) { | 
					
						
							| 
									
										
										
										
											2022-08-09 12:57:18 +02:00
										 |  |  | 						if (frame >= last_frame) { | 
					
						
							|  |  |  | 							if (frames->get_animation_loop(animation)) { | 
					
						
							| 
									
										
										
										
											2019-03-17 04:03:23 -03:00
										 |  |  | 								frame = 0; | 
					
						
							| 
									
										
										
										
											2023-01-21 14:51:03 +09:00
										 |  |  | 								emit_signal("animation_looped"); | 
					
						
							| 
									
										
										
										
											2022-08-09 12:57:18 +02:00
										 |  |  | 							} else { | 
					
						
							|  |  |  | 								frame = last_frame; | 
					
						
							| 
									
										
										
										
											2023-01-21 14:51:03 +09:00
										 |  |  | 								pause(); | 
					
						
							| 
									
										
										
										
											2023-09-04 17:01:33 +02:00
										 |  |  | 								emit_signal(SceneStringName(animation_finished)); | 
					
						
							| 
									
										
										
										
											2023-01-21 14:51:03 +09:00
										 |  |  | 								return; | 
					
						
							| 
									
										
										
										
											2018-10-04 12:51:45 +02:00
										 |  |  | 							} | 
					
						
							| 
									
										
										
										
											2022-08-09 12:57:18 +02:00
										 |  |  | 						} else { | 
					
						
							|  |  |  | 							frame++; | 
					
						
							| 
									
										
										
										
											2016-05-14 23:48:23 -03:00
										 |  |  | 						} | 
					
						
							| 
									
										
										
										
											2023-01-21 14:51:03 +09:00
										 |  |  | 						_calc_frame_speed_scale(); | 
					
						
							|  |  |  | 						frame_progress = 0.0; | 
					
						
							|  |  |  | 						queue_redraw(); | 
					
						
							| 
									
										
										
										
											2023-09-04 17:01:33 +02:00
										 |  |  | 						emit_signal(SceneStringName(frame_changed)); | 
					
						
							| 
									
										
										
										
											2023-01-21 14:51:03 +09:00
										 |  |  | 					} | 
					
						
							|  |  |  | 					double to_process = MIN((1.0 - frame_progress) / abs_speed, remaining); | 
					
						
							|  |  |  | 					frame_progress += to_process * abs_speed; | 
					
						
							|  |  |  | 					remaining -= to_process; | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					// Backwards.
 | 
					
						
							|  |  |  | 					if (frame_progress <= 0) { | 
					
						
							| 
									
										
										
										
											2022-08-09 12:57:18 +02:00
										 |  |  | 						if (frame <= 0) { | 
					
						
							|  |  |  | 							if (frames->get_animation_loop(animation)) { | 
					
						
							|  |  |  | 								frame = last_frame; | 
					
						
							| 
									
										
										
										
											2023-01-21 14:51:03 +09:00
										 |  |  | 								emit_signal("animation_looped"); | 
					
						
							| 
									
										
										
										
											2022-08-09 12:57:18 +02:00
										 |  |  | 							} else { | 
					
						
							|  |  |  | 								frame = 0; | 
					
						
							| 
									
										
										
										
											2023-01-21 14:51:03 +09:00
										 |  |  | 								pause(); | 
					
						
							| 
									
										
										
										
											2023-09-04 17:01:33 +02:00
										 |  |  | 								emit_signal(SceneStringName(animation_finished)); | 
					
						
							| 
									
										
										
										
											2023-01-21 14:51:03 +09:00
										 |  |  | 								return; | 
					
						
							| 
									
										
										
										
											2022-08-09 12:57:18 +02:00
										 |  |  | 							} | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 						} else { | 
					
						
							| 
									
										
										
										
											2022-08-09 12:57:18 +02:00
										 |  |  | 							frame--; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 						} | 
					
						
							| 
									
										
										
										
											2023-01-21 14:51:03 +09:00
										 |  |  | 						_calc_frame_speed_scale(); | 
					
						
							|  |  |  | 						frame_progress = 1.0; | 
					
						
							|  |  |  | 						queue_redraw(); | 
					
						
							| 
									
										
										
										
											2023-09-04 17:01:33 +02:00
										 |  |  | 						emit_signal(SceneStringName(frame_changed)); | 
					
						
							| 
									
										
										
										
											2016-05-14 23:48:23 -03:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2023-01-21 14:51:03 +09:00
										 |  |  | 					double to_process = MIN(frame_progress / abs_speed, remaining); | 
					
						
							|  |  |  | 					frame_progress -= to_process * abs_speed; | 
					
						
							|  |  |  | 					remaining -= to_process; | 
					
						
							| 
									
										
										
										
											2016-05-14 23:48:23 -03:00
										 |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-26 17:00:38 +03:00
										 |  |  | 				i++; | 
					
						
							|  |  |  | 				if (i > fc) { | 
					
						
							|  |  |  | 					return; // Prevents freezing if to_process is each time much less than remaining.
 | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2016-05-14 23:48:23 -03:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} break; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		case NOTIFICATION_DRAW: { | 
					
						
							| 
									
										
										
										
											2022-08-31 16:15:24 +02:00
										 |  |  | 			if (frames.is_null() || !frames->has_animation(animation)) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 				return; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-26 17:00:38 +03:00
										 |  |  | 			Ref<Texture2D> texture = frames->get_frame_texture(animation, frame); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 			if (texture.is_null()) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 				return; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			RID ci = get_canvas_item(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-25 12:18:18 +02:00
										 |  |  | 			Size2 s = texture->get_size(); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 			Point2 ofs = offset; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 			if (centered) { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 				ofs -= s / 2; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-29 18:09:16 -03:00
										 |  |  | 			if (get_viewport() && get_viewport()->is_snap_2d_transforms_to_pixel_enabled()) { | 
					
						
							| 
									
										
										
										
											2024-01-15 16:23:35 -05:00
										 |  |  | 				ofs = ofs.round(); | 
					
						
							| 
									
										
										
										
											2015-10-13 15:53:34 -03:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2024-01-15 16:23:35 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 			Rect2 dst_rect(ofs, s); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 			if (hflip) { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 				dst_rect.size.x = -dst_rect.size.x; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 			} | 
					
						
							|  |  |  | 			if (vflip) { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 				dst_rect.size.y = -dst_rect.size.y; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-24 12:15:43 -03:00
										 |  |  | 			texture->draw_rect_region(ci, dst_rect, Rect2(Vector2(), texture->get_size()), Color(1, 1, 1), false); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		} break; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | void AnimatedSprite2D::set_sprite_frames(const Ref<SpriteFrames> &p_frames) { | 
					
						
							| 
									
										
										
										
											2023-01-21 14:51:03 +09:00
										 |  |  | 	if (frames == p_frames) { | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	if (frames.is_valid()) { | 
					
						
							| 
									
										
										
										
											2024-05-13 16:56:03 +02:00
										 |  |  | 		frames->disconnect(CoreStringName(changed), callable_mp(this, &AnimatedSprite2D::_res_changed)); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-01-21 14:51:03 +09:00
										 |  |  | 	stop(); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	frames = p_frames; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	if (frames.is_valid()) { | 
					
						
							| 
									
										
										
										
											2024-05-13 16:56:03 +02:00
										 |  |  | 		frames->connect(CoreStringName(changed), callable_mp(this, &AnimatedSprite2D::_res_changed)); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-21 14:51:03 +09:00
										 |  |  | 		List<StringName> al; | 
					
						
							|  |  |  | 		frames->get_animation_list(&al); | 
					
						
							|  |  |  | 		if (al.size() == 0) { | 
					
						
							|  |  |  | 			set_animation(StringName()); | 
					
						
							| 
									
										
										
										
											2023-03-23 14:31:46 +01:00
										 |  |  | 			autoplay = String(); | 
					
						
							| 
									
										
										
										
											2023-01-21 14:51:03 +09:00
										 |  |  | 		} else { | 
					
						
							|  |  |  | 			if (!frames->has_animation(animation)) { | 
					
						
							| 
									
										
										
										
											2024-04-15 15:18:34 +02:00
										 |  |  | 				set_animation(al.front()->get()); | 
					
						
							| 
									
										
										
										
											2023-01-21 14:51:03 +09:00
										 |  |  | 			} | 
					
						
							|  |  |  | 			if (!frames->has_animation(autoplay)) { | 
					
						
							| 
									
										
										
										
											2023-03-23 14:31:46 +01:00
										 |  |  | 				autoplay = String(); | 
					
						
							| 
									
										
										
										
											2023-01-21 14:51:03 +09:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-05-14 23:48:23 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-10 17:18:45 -03:00
										 |  |  | 	notify_property_list_changed(); | 
					
						
							| 
									
										
										
										
											2022-08-13 23:21:24 +02:00
										 |  |  | 	queue_redraw(); | 
					
						
							| 
									
										
										
										
											2020-10-29 05:01:28 -05:00
										 |  |  | 	update_configuration_warnings(); | 
					
						
							| 
									
										
										
										
											2023-01-21 14:51:03 +09:00
										 |  |  | 	emit_signal("sprite_frames_changed"); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | Ref<SpriteFrames> AnimatedSprite2D::get_sprite_frames() const { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	return frames; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | void AnimatedSprite2D::set_frame(int p_frame) { | 
					
						
							| 
									
										
										
										
											2023-01-21 14:51:03 +09:00
										 |  |  | 	set_frame_and_progress(p_frame, signbit(get_playing_speed()) ? 1.0 : 0.0); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int AnimatedSprite2D::get_frame() const { | 
					
						
							|  |  |  | 	return frame; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void AnimatedSprite2D::set_frame_progress(real_t p_progress) { | 
					
						
							|  |  |  | 	frame_progress = p_progress; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | real_t AnimatedSprite2D::get_frame_progress() const { | 
					
						
							|  |  |  | 	return frame_progress; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void AnimatedSprite2D::set_frame_and_progress(int p_frame, real_t p_progress) { | 
					
						
							| 
									
										
										
										
											2022-08-09 12:57:18 +02:00
										 |  |  | 	if (frames.is_null()) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-05-14 23:48:23 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-21 14:51:03 +09:00
										 |  |  | 	bool has_animation = frames->has_animation(animation); | 
					
						
							|  |  |  | 	int end_frame = has_animation ? MAX(0, frames->get_frame_count(animation) - 1) : 0; | 
					
						
							|  |  |  | 	bool is_changed = frame != p_frame; | 
					
						
							| 
									
										
										
										
											2016-05-14 23:48:23 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	if (p_frame < 0) { | 
					
						
							| 
									
										
										
										
											2023-01-21 14:51:03 +09:00
										 |  |  | 		frame = 0; | 
					
						
							|  |  |  | 	} else if (has_animation && p_frame > end_frame) { | 
					
						
							|  |  |  | 		frame = end_frame; | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		frame = p_frame; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-21 14:51:03 +09:00
										 |  |  | 	_calc_frame_speed_scale(); | 
					
						
							|  |  |  | 	frame_progress = p_progress; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-21 14:51:03 +09:00
										 |  |  | 	if (!is_changed) { | 
					
						
							|  |  |  | 		return; // No change, don't redraw.
 | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-08-13 23:21:24 +02:00
										 |  |  | 	queue_redraw(); | 
					
						
							| 
									
										
										
										
											2023-09-04 17:01:33 +02:00
										 |  |  | 	emit_signal(SceneStringName(frame_changed)); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-05-14 14:29:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-26 17:00:38 +03:00
										 |  |  | void AnimatedSprite2D::set_speed_scale(float p_speed_scale) { | 
					
						
							| 
									
										
										
										
											2022-08-31 16:15:24 +02:00
										 |  |  | 	speed_scale = p_speed_scale; | 
					
						
							| 
									
										
										
										
											2018-04-26 21:08:21 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-26 17:00:38 +03:00
										 |  |  | float AnimatedSprite2D::get_speed_scale() const { | 
					
						
							| 
									
										
										
										
											2018-04-26 21:08:21 +02:00
										 |  |  | 	return speed_scale; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-21 14:51:03 +09:00
										 |  |  | float AnimatedSprite2D::get_playing_speed() const { | 
					
						
							|  |  |  | 	if (!playing) { | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return speed_scale * custom_speed_scale; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | void AnimatedSprite2D::set_centered(bool p_center) { | 
					
						
							| 
									
										
										
										
											2023-11-24 16:33:23 +02:00
										 |  |  | 	if (centered == p_center) { | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	centered = p_center; | 
					
						
							| 
									
										
										
										
											2022-08-13 23:21:24 +02:00
										 |  |  | 	queue_redraw(); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	item_rect_changed(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | bool AnimatedSprite2D::is_centered() const { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	return centered; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | void AnimatedSprite2D::set_offset(const Point2 &p_offset) { | 
					
						
							| 
									
										
										
										
											2023-11-24 16:33:23 +02:00
										 |  |  | 	if (offset == p_offset) { | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	offset = p_offset; | 
					
						
							| 
									
										
										
										
											2022-08-13 23:21:24 +02:00
										 |  |  | 	queue_redraw(); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	item_rect_changed(); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-05-14 14:29:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | Point2 AnimatedSprite2D::get_offset() const { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	return offset; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | void AnimatedSprite2D::set_flip_h(bool p_flip) { | 
					
						
							| 
									
										
										
										
											2023-11-24 16:33:23 +02:00
										 |  |  | 	if (hflip == p_flip) { | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	hflip = p_flip; | 
					
						
							| 
									
										
										
										
											2022-08-13 23:21:24 +02:00
										 |  |  | 	queue_redraw(); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-05-14 14:29:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | bool AnimatedSprite2D::is_flipped_h() const { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	return hflip; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | void AnimatedSprite2D::set_flip_v(bool p_flip) { | 
					
						
							| 
									
										
										
										
											2023-11-24 16:33:23 +02:00
										 |  |  | 	if (vflip == p_flip) { | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	vflip = p_flip; | 
					
						
							| 
									
										
										
										
											2022-08-13 23:21:24 +02:00
										 |  |  | 	queue_redraw(); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-05-14 14:29:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | bool AnimatedSprite2D::is_flipped_v() const { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	return vflip; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | void AnimatedSprite2D::_res_changed() { | 
					
						
							| 
									
										
										
										
											2023-01-21 14:51:03 +09:00
										 |  |  | 	set_frame_and_progress(frame, frame_progress); | 
					
						
							| 
									
										
										
										
											2022-08-13 23:21:24 +02:00
										 |  |  | 	queue_redraw(); | 
					
						
							| 
									
										
										
										
											2022-11-26 17:00:38 +03:00
										 |  |  | 	notify_property_list_changed(); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-21 14:51:03 +09:00
										 |  |  | bool AnimatedSprite2D::is_playing() const { | 
					
						
							|  |  |  | 	return playing; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void AnimatedSprite2D::set_autoplay(const String &p_name) { | 
					
						
							|  |  |  | 	if (is_inside_tree() && !Engine::get_singleton()->is_editor_hint()) { | 
					
						
							|  |  |  | 		WARN_PRINT("Setting autoplay after the node has been added to the scene has no effect."); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-01-21 14:51:03 +09:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	autoplay = p_name; | 
					
						
							| 
									
										
										
										
											2016-05-14 23:48:23 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-21 14:51:03 +09:00
										 |  |  | String AnimatedSprite2D::get_autoplay() const { | 
					
						
							|  |  |  | 	return autoplay; | 
					
						
							| 
									
										
										
										
											2016-05-14 23:48:23 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-21 14:51:03 +09:00
										 |  |  | void AnimatedSprite2D::play(const StringName &p_name, float p_custom_scale, bool p_from_end) { | 
					
						
							|  |  |  | 	StringName name = p_name; | 
					
						
							| 
									
										
										
										
											2019-09-03 15:59:54 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-21 14:51:03 +09:00
										 |  |  | 	if (name == StringName()) { | 
					
						
							|  |  |  | 		name = animation; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-09 17:52:40 +02:00
										 |  |  | 	ERR_FAIL_NULL_MSG(frames, vformat("There is no animation with name '%s'.", name)); | 
					
						
							| 
									
										
										
										
											2023-01-21 14:51:03 +09:00
										 |  |  | 	ERR_FAIL_COND_MSG(!frames->get_animation_names().has(name), vformat("There is no animation with name '%s'.", name)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (frames->get_frame_count(name) == 0) { | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	playing = true; | 
					
						
							|  |  |  | 	custom_speed_scale = p_custom_scale; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	int end_frame = MAX(0, frames->get_frame_count(animation) - 1); | 
					
						
							|  |  |  | 	if (name != animation) { | 
					
						
							|  |  |  | 		animation = name; | 
					
						
							|  |  |  | 		if (p_from_end) { | 
					
						
							|  |  |  | 			set_frame_and_progress(end_frame, 1.0); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			set_frame_and_progress(0, 0.0); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2024-05-13 16:56:03 +02:00
										 |  |  | 		emit_signal(SceneStringName(animation_changed)); | 
					
						
							| 
									
										
										
										
											2023-01-21 14:51:03 +09:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		bool is_backward = signbit(speed_scale * custom_speed_scale); | 
					
						
							|  |  |  | 		if (p_from_end && is_backward && frame == 0 && frame_progress <= 0.0) { | 
					
						
							|  |  |  | 			set_frame_and_progress(end_frame, 1.0); | 
					
						
							|  |  |  | 		} else if (!p_from_end && !is_backward && frame == end_frame && frame_progress >= 1.0) { | 
					
						
							|  |  |  | 			set_frame_and_progress(0, 0.0); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	set_process_internal(true); | 
					
						
							| 
									
										
										
										
											2023-01-29 00:24:52 +09:00
										 |  |  | 	notify_property_list_changed(); | 
					
						
							|  |  |  | 	queue_redraw(); | 
					
						
							| 
									
										
										
										
											2023-01-21 14:51:03 +09:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void AnimatedSprite2D::play_backwards(const StringName &p_name) { | 
					
						
							|  |  |  | 	play(p_name, -1, true); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void AnimatedSprite2D::_stop_internal(bool p_reset) { | 
					
						
							|  |  |  | 	playing = false; | 
					
						
							|  |  |  | 	if (p_reset) { | 
					
						
							|  |  |  | 		custom_speed_scale = 1.0; | 
					
						
							|  |  |  | 		set_frame_and_progress(0, 0.0); | 
					
						
							| 
									
										
										
										
											2019-09-03 15:59:54 -07:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-01-21 14:51:03 +09:00
										 |  |  | 	notify_property_list_changed(); | 
					
						
							|  |  |  | 	set_process_internal(false); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-03-17 04:03:23 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-21 14:51:03 +09:00
										 |  |  | void AnimatedSprite2D::pause() { | 
					
						
							|  |  |  | 	_stop_internal(false); | 
					
						
							| 
									
										
										
										
											2016-05-14 23:48:23 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | void AnimatedSprite2D::stop() { | 
					
						
							| 
									
										
										
										
											2023-01-21 14:51:03 +09:00
										 |  |  | 	_stop_internal(true); | 
					
						
							| 
									
										
										
										
											2016-05-14 23:48:23 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-01 21:16:37 -05:00
										 |  |  | double AnimatedSprite2D::_get_frame_duration() { | 
					
						
							| 
									
										
										
										
											2016-05-14 23:48:23 -03:00
										 |  |  | 	if (frames.is_valid() && frames->has_animation(animation)) { | 
					
						
							| 
									
										
										
										
											2022-11-26 17:00:38 +03:00
										 |  |  | 		return frames->get_frame_duration(animation, frame); | 
					
						
							| 
									
										
										
										
											2016-05-14 23:48:23 -03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-01-21 14:51:03 +09:00
										 |  |  | 	return 1.0; | 
					
						
							| 
									
										
										
										
											2018-05-13 23:29:06 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-21 14:51:03 +09:00
										 |  |  | void AnimatedSprite2D::_calc_frame_speed_scale() { | 
					
						
							|  |  |  | 	frame_speed_scale = 1.0 / _get_frame_duration(); | 
					
						
							| 
									
										
										
										
											2016-05-14 23:48:23 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-21 14:51:03 +09:00
										 |  |  | void AnimatedSprite2D::set_animation(const StringName &p_name) { | 
					
						
							|  |  |  | 	if (animation == p_name) { | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	animation = p_name; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-13 16:56:03 +02:00
										 |  |  | 	emit_signal(SceneStringName(animation_changed)); | 
					
						
							| 
									
										
										
										
											2018-12-19 20:38:19 -02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-21 14:51:03 +09:00
										 |  |  | 	if (frames == nullptr) { | 
					
						
							|  |  |  | 		animation = StringName(); | 
					
						
							|  |  |  | 		stop(); | 
					
						
							|  |  |  | 		ERR_FAIL_MSG(vformat("There is no animation with name '%s'.", p_name)); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	int frame_count = frames->get_frame_count(animation); | 
					
						
							|  |  |  | 	if (animation == StringName() || frame_count == 0) { | 
					
						
							|  |  |  | 		stop(); | 
					
						
							| 
									
										
										
										
											2016-05-14 23:48:23 -03:00
										 |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2023-01-21 14:51:03 +09:00
										 |  |  | 	} else if (!frames->get_animation_names().has(animation)) { | 
					
						
							|  |  |  | 		animation = StringName(); | 
					
						
							|  |  |  | 		stop(); | 
					
						
							|  |  |  | 		ERR_FAIL_MSG(vformat("There is no animation with name '%s'.", p_name)); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (signbit(get_playing_speed())) { | 
					
						
							|  |  |  | 		set_frame_and_progress(frame_count - 1, 1.0); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		set_frame_and_progress(0, 0.0); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-05-14 23:48:23 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-10 17:18:45 -03:00
										 |  |  | 	notify_property_list_changed(); | 
					
						
							| 
									
										
										
										
											2022-08-13 23:21:24 +02:00
										 |  |  | 	queue_redraw(); | 
					
						
							| 
									
										
										
										
											2016-05-14 23:48:23 -03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-05-14 14:29:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | StringName AnimatedSprite2D::get_animation() const { | 
					
						
							| 
									
										
										
										
											2016-05-14 23:48:23 -03:00
										 |  |  | 	return animation; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-17 19:03:21 +01:00
										 |  |  | PackedStringArray AnimatedSprite2D::get_configuration_warnings() const { | 
					
						
							|  |  |  | 	PackedStringArray warnings = Node2D::get_configuration_warnings(); | 
					
						
							| 
									
										
										
										
											2016-05-17 18:27:15 -03:00
										 |  |  | 	if (frames.is_null()) { | 
					
						
							| 
									
										
										
										
											2022-09-18 02:52:20 +02:00
										 |  |  | 		warnings.push_back(RTR("A SpriteFrames resource must be created or set in the \"Frames\" property in order for AnimatedSprite2D to display frames.")); | 
					
						
							| 
									
										
										
										
											2016-05-17 18:27:15 -03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-10-29 05:01:28 -05:00
										 |  |  | 	return warnings; | 
					
						
							| 
									
										
										
										
											2016-05-17 18:27:15 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-03 12:10:11 +01:00
										 |  |  | #ifdef TOOLS_ENABLED
 | 
					
						
							| 
									
										
										
										
											2022-05-04 20:56:20 +08:00
										 |  |  | void AnimatedSprite2D::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const { | 
					
						
							| 
									
										
										
										
											2024-01-03 12:10:11 +01:00
										 |  |  | 	const String pf = p_function; | 
					
						
							| 
									
										
										
										
											2024-01-03 01:33:08 +01:00
										 |  |  | 	if (p_idx == 0 && frames.is_valid()) { | 
					
						
							| 
									
										
										
										
											2024-01-03 12:10:11 +01:00
										 |  |  | 		if (pf == "play" || pf == "play_backwards" || pf == "set_animation" || pf == "set_autoplay") { | 
					
						
							| 
									
										
										
										
											2024-01-03 01:33:08 +01:00
										 |  |  | 			List<StringName> al; | 
					
						
							|  |  |  | 			frames->get_animation_list(&al); | 
					
						
							|  |  |  | 			for (const StringName &name : al) { | 
					
						
							|  |  |  | 				r_options->push_back(String(name).quote()); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2022-05-04 20:56:20 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-01-03 00:13:04 +01:00
										 |  |  | 	Node2D::get_argument_options(p_function, p_idx, r_options); | 
					
						
							| 
									
										
										
										
											2022-05-04 20:56:20 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2024-01-03 12:10:11 +01:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2022-05-04 20:56:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-21 14:51:03 +09:00
										 |  |  | #ifndef DISABLE_DEPRECATED
 | 
					
						
							|  |  |  | bool AnimatedSprite2D::_set(const StringName &p_name, const Variant &p_value) { | 
					
						
							|  |  |  | 	if ((p_name == SNAME("frames"))) { | 
					
						
							|  |  |  | 		set_sprite_frames(p_value); | 
					
						
							|  |  |  | 		return true; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | void AnimatedSprite2D::_bind_methods() { | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("set_sprite_frames", "sprite_frames"), &AnimatedSprite2D::set_sprite_frames); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("get_sprite_frames"), &AnimatedSprite2D::get_sprite_frames); | 
					
						
							| 
									
										
										
										
											2016-05-14 23:48:23 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-21 14:51:03 +09:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("set_animation", "name"), &AnimatedSprite2D::set_animation); | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("get_animation"), &AnimatedSprite2D::get_animation); | 
					
						
							| 
									
										
										
										
											2016-05-14 23:48:23 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-21 14:51:03 +09:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("set_autoplay", "name"), &AnimatedSprite2D::set_autoplay); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("get_autoplay"), &AnimatedSprite2D::get_autoplay); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-04 15:31:40 +05:45
										 |  |  | 	ClassDB::bind_method(D_METHOD("is_playing"), &AnimatedSprite2D::is_playing); | 
					
						
							| 
									
										
										
										
											2016-05-14 23:48:23 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-21 14:51:03 +09:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("play", "name", "custom_speed", "from_end"), &AnimatedSprite2D::play, DEFVAL(StringName()), DEFVAL(1.0), DEFVAL(false)); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("play_backwards", "name"), &AnimatedSprite2D::play_backwards, DEFVAL(StringName())); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("pause"), &AnimatedSprite2D::pause); | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("stop"), &AnimatedSprite2D::stop); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("set_centered", "centered"), &AnimatedSprite2D::set_centered); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("is_centered"), &AnimatedSprite2D::is_centered); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("set_offset", "offset"), &AnimatedSprite2D::set_offset); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("get_offset"), &AnimatedSprite2D::get_offset); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("set_flip_h", "flip_h"), &AnimatedSprite2D::set_flip_h); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("is_flipped_h"), &AnimatedSprite2D::is_flipped_h); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("set_flip_v", "flip_v"), &AnimatedSprite2D::set_flip_v); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("is_flipped_v"), &AnimatedSprite2D::is_flipped_v); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("set_frame", "frame"), &AnimatedSprite2D::set_frame); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("get_frame"), &AnimatedSprite2D::get_frame); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-21 14:51:03 +09:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("set_frame_progress", "progress"), &AnimatedSprite2D::set_frame_progress); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("get_frame_progress"), &AnimatedSprite2D::get_frame_progress); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("set_frame_and_progress", "frame", "progress"), &AnimatedSprite2D::set_frame_and_progress); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("set_speed_scale", "speed_scale"), &AnimatedSprite2D::set_speed_scale); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("get_speed_scale"), &AnimatedSprite2D::get_speed_scale); | 
					
						
							| 
									
										
										
										
											2023-01-21 14:51:03 +09:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("get_playing_speed"), &AnimatedSprite2D::get_playing_speed); | 
					
						
							| 
									
										
										
										
											2018-04-26 21:08:21 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-21 14:51:03 +09:00
										 |  |  | 	ADD_SIGNAL(MethodInfo("sprite_frames_changed")); | 
					
						
							|  |  |  | 	ADD_SIGNAL(MethodInfo("animation_changed")); | 
					
						
							| 
									
										
										
										
											2014-12-07 02:04:20 -03:00
										 |  |  | 	ADD_SIGNAL(MethodInfo("frame_changed")); | 
					
						
							| 
									
										
										
										
											2023-01-21 14:51:03 +09:00
										 |  |  | 	ADD_SIGNAL(MethodInfo("animation_looped")); | 
					
						
							| 
									
										
										
										
											2017-01-08 17:35:11 -03:00
										 |  |  | 	ADD_SIGNAL(MethodInfo("animation_finished")); | 
					
						
							| 
									
										
										
										
											2014-12-07 02:04:20 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-04 22:54:32 -03:00
										 |  |  | 	ADD_GROUP("Animation", ""); | 
					
						
							| 
									
										
										
										
											2023-01-21 14:51:03 +09:00
										 |  |  | 	ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "sprite_frames", PROPERTY_HINT_RESOURCE_TYPE, "SpriteFrames"), "set_sprite_frames", "get_sprite_frames"); | 
					
						
							|  |  |  | 	ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "animation", PROPERTY_HINT_ENUM, ""), "set_animation", "get_animation"); | 
					
						
							|  |  |  | 	ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "autoplay", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_autoplay", "get_autoplay"); | 
					
						
							| 
									
										
										
										
											2019-07-25 09:11:41 +02:00
										 |  |  | 	ADD_PROPERTY(PropertyInfo(Variant::INT, "frame"), "set_frame", "get_frame"); | 
					
						
							| 
									
										
										
										
											2023-01-21 14:51:03 +09:00
										 |  |  | 	ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "frame_progress", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_frame_progress", "get_frame_progress"); | 
					
						
							| 
									
										
										
											
												Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
											
										 
											2020-02-24 15:20:53 -03:00
										 |  |  | 	ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "speed_scale"), "set_speed_scale", "get_speed_scale"); | 
					
						
							| 
									
										
										
										
											2019-07-04 22:54:32 -03:00
										 |  |  | 	ADD_GROUP("Offset", ""); | 
					
						
							| 
									
										
										
										
											2018-11-08 11:30:02 -03:00
										 |  |  | 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "centered"), "set_centered", "is_centered"); | 
					
						
							| 
									
										
										
										
											2021-12-02 18:09:19 -06:00
										 |  |  | 	ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "offset", PROPERTY_HINT_NONE, "suffix:px"), "set_offset", "get_offset"); | 
					
						
							| 
									
										
										
										
											2018-11-08 11:30:02 -03:00
										 |  |  | 	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"); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | AnimatedSprite2D::AnimatedSprite2D() { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } |