From f75b8a81d2f356627731633471b2fe992daa1edf Mon Sep 17 00:00:00 2001 From: sanikoyes Date: Wed, 20 Aug 2014 12:01:41 +0800 Subject: [PATCH] Add tween support for godot --- scene/animation/tween.cpp | 684 ++++++++++++++++++++++++ scene/animation/tween.h | 161 ++++++ scene/animation/tween_interpolaters.cpp | 407 ++++++++++++++ scene/register_scene_types.cpp | 2 + 4 files changed, 1254 insertions(+) create mode 100644 scene/animation/tween.cpp create mode 100644 scene/animation/tween.h create mode 100644 scene/animation/tween_interpolaters.cpp diff --git a/scene/animation/tween.cpp b/scene/animation/tween.cpp new file mode 100644 index 00000000000..e7876e3bfa4 --- /dev/null +++ b/scene/animation/tween.cpp @@ -0,0 +1,684 @@ +/*************************************************************************/ +/* tween.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* http://www.godotengine.org */ +/*************************************************************************/ +/* 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. */ +/*************************************************************************/ +#include "tween.h" + +bool Tween::_set(const StringName& p_name, const Variant& p_value) { + + String name=p_name; + + if (name=="playback/speed" || name=="speed") { //bw compatibility + set_speed(p_value); + + } else if (name=="playback/active") { + set_active(p_value); + } + return true; +} + +bool Tween::_get(const StringName& p_name,Variant &r_ret) const { + + String name=p_name; + + if (name=="playback/speed") { //bw compatibility + + r_ret=speed_scale; + } else if (name=="playback/active") { + + r_ret=is_active(); + } + return true; +} + +void Tween::_get_property_list(List *p_list) const { + + p_list->push_back( PropertyInfo( Variant::BOOL, "playback/active", PROPERTY_HINT_NONE,"" ) ); + p_list->push_back( PropertyInfo( Variant::REAL, "playback/speed", PROPERTY_HINT_RANGE, "-64,64,0.01") ); +} + +void Tween::_notification(int p_what) { + + switch(p_what) { + + case NOTIFICATION_ENTER_SCENE: { + + if (!processing) { + //make sure that a previous process state was not saved + //only process if "processing" is set + set_fixed_process(false); + set_process(false); + } + } break; + case NOTIFICATION_READY: { + + } break; + case NOTIFICATION_PROCESS: { + if (tween_process_mode==TWEEN_PROCESS_FIXED) + break; + + if (processing) + _tween_process( get_process_delta_time() ); + } break; + case NOTIFICATION_FIXED_PROCESS: { + + if (tween_process_mode==TWEEN_PROCESS_IDLE) + break; + + if (processing) + _tween_process( get_fixed_process_delta_time() ); + } break; + case NOTIFICATION_EXIT_SCENE: { + + stop_all(); + } break; + } +} + +void Tween::_bind_methods() { + + ObjectTypeDB::bind_method(_MD("is_active"),&Tween::is_active ); + ObjectTypeDB::bind_method(_MD("set_active","active"),&Tween::set_active ); + + ObjectTypeDB::bind_method(_MD("set_speed","speed"),&Tween::set_speed); + ObjectTypeDB::bind_method(_MD("get_speed"),&Tween::get_speed); + + ObjectTypeDB::bind_method(_MD("set_tween_process_mode","mode"),&Tween::set_tween_process_mode); + ObjectTypeDB::bind_method(_MD("get_tween_process_mode"),&Tween::get_tween_process_mode); + + ObjectTypeDB::bind_method(_MD("start"),&Tween::start ); + ObjectTypeDB::bind_method(_MD("reset","object,key"),&Tween::reset ); + ObjectTypeDB::bind_method(_MD("reset_all"),&Tween::reset_all ); + ObjectTypeDB::bind_method(_MD("stop","object,key"),&Tween::stop ); + ObjectTypeDB::bind_method(_MD("stop_all"),&Tween::stop_all ); + + ObjectTypeDB::bind_method(_MD("interpolate_property","object","property","initial_val","final_val","times_in_sec","trans_type","ease_type"),&Tween::interpolate_property ); + ObjectTypeDB::bind_method(_MD("interpolate_method","object","method","initial_val","final_val","times_in_sec","trans_type","ease_type"),&Tween::interpolate_method ); + + ADD_SIGNAL( MethodInfo("tween_start", PropertyInfo( Variant::OBJECT,"object"), PropertyInfo( Variant::STRING,"key")) ); + ADD_SIGNAL( MethodInfo("tween_step", PropertyInfo( Variant::OBJECT,"object"), PropertyInfo( Variant::STRING,"key"), PropertyInfo( Variant::OBJECT,"value")) ); + ADD_SIGNAL( MethodInfo("tween_complete", PropertyInfo( Variant::INT,"id")) ); + + //ADD_PROPERTY( PropertyInfo( Variant::BOOL, "activate"), _SCS("set_active"), _SCS("is_active")); + + BIND_CONSTANT(TRANS_LINEAR); + BIND_CONSTANT(TRANS_SINE); + BIND_CONSTANT(TRANS_QUINT); + BIND_CONSTANT(TRANS_QUART); + BIND_CONSTANT(TRANS_QUAD); + BIND_CONSTANT(TRANS_EXPO); + BIND_CONSTANT(TRANS_ELASTIC); + BIND_CONSTANT(TRANS_CUBIC); + BIND_CONSTANT(TRANS_CIRC); + BIND_CONSTANT(TRANS_BOUNCE); + BIND_CONSTANT(TRANS_BACK); + + BIND_CONSTANT(EASE_IN); + BIND_CONSTANT(EASE_OUT); + BIND_CONSTANT(EASE_IN_OUT); + BIND_CONSTANT(EASE_OUT_IN); +} + +Variant Tween::_run_equation(InterpolateData& p_data) { + + Variant& initial_val = p_data.initial_val; + Variant& delta_val = p_data.delta_val; + Variant result; + +#define APPLY_EQUATION(element)\ + r.element = _run_equation(p_data.trans_type, p_data.ease_type, p_data.elapsed, i.element, d.element, p_data.times_in_sec); + + switch(initial_val.get_type()) + { + case Variant::INT: + result = (int) _run_equation(p_data.trans_type, p_data.ease_type, p_data.elapsed, (int) initial_val, (int) delta_val, p_data.times_in_sec); + break; + + case Variant::REAL: + result = _run_equation(p_data.trans_type, p_data.ease_type, p_data.elapsed, (real_t) initial_val, (real_t) delta_val, p_data.times_in_sec); + break; + + case Variant::VECTOR2: + { + Vector2 i = initial_val; + Vector2 d = delta_val; + Vector2 r; + + APPLY_EQUATION(x); + APPLY_EQUATION(y); + + result = r; + } + break; + + case Variant::VECTOR3: + { + Vector3 i = initial_val; + Vector3 d = delta_val; + Vector3 r; + + APPLY_EQUATION(x); + APPLY_EQUATION(y); + APPLY_EQUATION(z); + + result = r; + } + break; + + case Variant::MATRIX3: + { + Matrix3 i = initial_val; + Matrix3 d = delta_val; + Matrix3 r; + + APPLY_EQUATION(elements[0][0]); + APPLY_EQUATION(elements[0][1]); + APPLY_EQUATION(elements[0][2]); + APPLY_EQUATION(elements[1][0]); + APPLY_EQUATION(elements[1][1]); + APPLY_EQUATION(elements[1][2]); + APPLY_EQUATION(elements[2][0]); + APPLY_EQUATION(elements[2][1]); + APPLY_EQUATION(elements[2][2]); + + result = r; + } + break; + + case Variant::MATRIX32: + { + Matrix3 i = initial_val; + Matrix3 d = delta_val; + Matrix3 r; + + APPLY_EQUATION(elements[0][0]); + APPLY_EQUATION(elements[0][1]); + APPLY_EQUATION(elements[1][0]); + APPLY_EQUATION(elements[1][1]); + APPLY_EQUATION(elements[2][0]); + APPLY_EQUATION(elements[2][1]); + + result = r; + } + break; + case Variant::QUAT: + { + Quat i = initial_val; + Quat d = delta_val; + Quat r; + + APPLY_EQUATION(x); + APPLY_EQUATION(y); + APPLY_EQUATION(z); + APPLY_EQUATION(w); + + result = r; + } + break; + case Variant::_AABB: + { + AABB i = initial_val; + AABB d = delta_val; + AABB r; + + APPLY_EQUATION(pos.x); + APPLY_EQUATION(pos.y); + APPLY_EQUATION(pos.z); + APPLY_EQUATION(size.x); + APPLY_EQUATION(size.y); + APPLY_EQUATION(size.z); + + result = r; + } + break; + case Variant::TRANSFORM: + { + Transform i = initial_val; + Transform d = delta_val; + Transform r; + + APPLY_EQUATION(basis.elements[0][0]); + APPLY_EQUATION(basis.elements[0][1]); + APPLY_EQUATION(basis.elements[0][2]); + APPLY_EQUATION(basis.elements[1][0]); + APPLY_EQUATION(basis.elements[1][1]); + APPLY_EQUATION(basis.elements[1][2]); + APPLY_EQUATION(basis.elements[2][0]); + APPLY_EQUATION(basis.elements[2][1]); + APPLY_EQUATION(basis.elements[2][2]); + APPLY_EQUATION(origin.x); + APPLY_EQUATION(origin.y); + APPLY_EQUATION(origin.z); + + result = r; + } + break; + case Variant::COLOR: + { + Color i = initial_val; + Color d = delta_val; + Color r; + + APPLY_EQUATION(r); + APPLY_EQUATION(g); + APPLY_EQUATION(b); + APPLY_EQUATION(a); + + result = r; + } + break; + }; +#undef APPLY_EQUATION + + return result; +} + +bool Tween::_apply_tween_value(InterpolateData& p_data, Variant& value) { + + Variant& object = p_data.object; + + if(p_data.is_method) { + + Variant *arg[1] = { &value }; + + Variant::CallError error; + object.call(p_data.key, (const Variant **) arg, 1, error); + if(error.error == Variant::CallError::CALL_OK) + return true; + + return false; + + } else { + + bool valid = false; + object.set(p_data.key,value, &valid); + return valid; + } + return true; +} + +void Tween::_tween_process(float p_delta) { + + if (speed_scale == 0) + return; + p_delta *= speed_scale; + + for(List::Element *E=interpolates.front();E;E=E->next()) { + + InterpolateData& data = E->get(); + if(!data.active || data.elapsed == data.times_in_sec) + continue; + + if(data.elapsed == 0) + emit_signal("tween_start",data.object,data.key); + + data.elapsed += p_delta; + if(data.elapsed > data.times_in_sec) + data.elapsed = data.times_in_sec; + + Variant result = _run_equation(data); + emit_signal("tween_step",data.object,data.key,result); + + _apply_tween_value(data, result); + + if(data.elapsed == data.times_in_sec) + emit_signal("tween_complete",data.object,data.key); + } +} + +void Tween::set_tween_process_mode(TweenProcessMode p_mode) { + + if (tween_process_mode==p_mode) + return; + + bool pr = processing; + if (pr) + _set_process(false); + tween_process_mode=p_mode; + if (pr) + _set_process(true); +} + +Tween::TweenProcessMode Tween::get_tween_process_mode() const { + + return tween_process_mode; +} + +void Tween::_set_process(bool p_process,bool p_force) { + + if (processing==p_process && !p_force) + return; + + switch(tween_process_mode) { + + case TWEEN_PROCESS_FIXED: set_fixed_process(p_process && active); break; + case TWEEN_PROCESS_IDLE: set_process(p_process && active); break; + } + + processing=p_process; +} + +bool Tween::is_active() const { + + return active; +} + +void Tween::set_active(bool p_active) { + + if (active==p_active) + return; + + active=p_active; + _set_process(processing,true); +} + +void Tween::set_speed(float p_speed) { + + speed_scale=p_speed; +} + +float Tween::get_speed() const { + + return speed_scale; +} + +bool Tween::start() { + + set_active(true); + _set_process(true); + return true; +} + +bool Tween::reset(Variant p_object, String p_key) { + + for(List::Element *E=interpolates.front();E;E=E->next()) { + + InterpolateData& data = E->get(); + if(data.object == p_object && data.key == p_key) + _apply_tween_value(data, data.initial_val); + } + return true; +} + +bool Tween::reset_all() { + + for(List::Element *E=interpolates.front();E;E=E->next()) { + + InterpolateData& data = E->get(); + _apply_tween_value(data, data.initial_val); + } + return true; +} + +bool Tween::stop(Variant p_object, String p_key) { + + for(List::Element *E=interpolates.front();E;E=E->next()) { + + InterpolateData& data = E->get(); + if(data.object == p_object && data.key == p_key) + data.active = false; + } + return true; +} + +bool Tween::stop_all() { + + set_active(false); + _set_process(false); + + for(List::Element *E=interpolates.front();E;E=E->next()) { + + InterpolateData& data = E->get(); + data.active = false; + } + return true; +} + +bool Tween::resume(Variant p_object, String p_key) { + + set_active(true); + _set_process(true); + + for(List::Element *E=interpolates.front();E;E=E->next()) { + + InterpolateData& data = E->get(); + if(data.object == p_object && data.key == p_key) + data.active = true; + } + return true; +} + +bool Tween::resume_all() { + + set_active(true); + _set_process(true); + + for(List::Element *E=interpolates.front();E;E=E->next()) { + + InterpolateData& data = E->get(); + data.active = true; + } + return true; +} + +bool Tween::_calc_delta_val(InterpolateData& p_data) { + + Variant& initial_val = p_data.initial_val; + Variant& delta_val = p_data.delta_val; + Variant& final_val = p_data.final_val; + + switch(initial_val.get_type()) { + case Variant::INT: + delta_val = (int) final_val - (int) initial_val; + break; + + case Variant::REAL: + delta_val = (real_t) final_val - (real_t) initial_val; + break; + + case Variant::VECTOR2: + delta_val = final_val.operator Vector2() - initial_val.operator Vector2(); + break; + + case Variant::VECTOR3: + delta_val = final_val.operator Vector3() - initial_val.operator Vector3(); + break; + + case Variant::MATRIX3: + { + Matrix3 i = initial_val; + Matrix3 f = final_val; + delta_val = Matrix3(f.elements[0][0] - i.elements[0][0], + f.elements[0][1] - i.elements[0][1], + f.elements[0][2] - i.elements[0][2], + f.elements[1][0] - i.elements[1][0], + f.elements[1][1] - i.elements[1][1], + f.elements[1][2] - i.elements[1][2], + f.elements[2][0] - i.elements[2][0], + f.elements[2][1] - i.elements[2][1], + f.elements[2][2] - i.elements[2][2] + ); + } + break; + + case Variant::MATRIX32: + { + Matrix32 i = initial_val; + Matrix32 f = final_val; + Matrix32 d = Matrix32(); + d[0][0] = f.elements[0][0] - i.elements[0][0]; + d[0][1] = f.elements[0][1] - i.elements[0][1]; + d[1][0] = f.elements[1][0] - i.elements[1][0]; + d[1][1] = f.elements[1][1] - i.elements[1][1]; + d[2][0] = f.elements[2][0] - i.elements[2][0]; + d[2][1] = f.elements[2][1] - i.elements[2][1]; + delta_val = d; + } + break; + case Variant::QUAT: + delta_val = final_val.operator Quat() - initial_val.operator Quat(); + break; + case Variant::_AABB: + { + AABB i = initial_val; + AABB f = final_val; + delta_val = AABB(f.pos - i.pos, f.size - i.size); + } + break; + case Variant::TRANSFORM: + { + Transform i = initial_val; + Transform f = final_val; + Transform d; + d.set(f.basis.elements[0][0] - i.basis.elements[0][0], + f.basis.elements[0][1] - i.basis.elements[0][1], + f.basis.elements[0][2] - i.basis.elements[0][2], + f.basis.elements[1][0] - i.basis.elements[1][0], + f.basis.elements[1][1] - i.basis.elements[1][1], + f.basis.elements[1][2] - i.basis.elements[1][2], + f.basis.elements[2][0] - i.basis.elements[2][0], + f.basis.elements[2][1] - i.basis.elements[2][1], + f.basis.elements[2][2] - i.basis.elements[2][2], + f.origin.x - i.origin.x, + f.origin.y - i.origin.y, + f.origin.z - i.origin.z + ); + + delta_val = d; + } + break; + case Variant::COLOR: + { + Color i = initial_val; + Color f = final_val; + delta_val = Color(f.r - i.r, f.g - i.g, f.b - i.b, f.a - i.a); + } + break; + + default: + ERR_PRINT("Invalid param type, except(int/real/vector2/vector/matrix/matrix32/quat/aabb/transform/color)"); + return false; + }; + return true; +} + +bool Tween::interpolate_property(Variant p_object + , String p_property + , Variant p_initial_val + , Variant p_final_val + , real_t p_times_in_sec + , TransitionType p_trans_type + , EaseType p_ease_type +) { + + ERR_FAIL_COND_V(p_object.get_type() != Variant::OBJECT, false); + ERR_FAIL_COND_V(p_initial_val.get_type() != p_final_val.get_type(), false); + ERR_FAIL_COND_V(p_times_in_sec <= 0, false); + + bool prop_found = false; + Object *obj = (Object *) p_object; + List props; + obj->get_property_list(&props); + for(List::Element *E=props.front();E;E=E->next()) { + + PropertyInfo& prop=E->get(); + if(prop.name==p_property) + { + prop_found = true; + break; + } + } + ERR_FAIL_COND_V(!prop_found, false); + + InterpolateData data; + data.active = true; + data.is_method = false; + data.elapsed = 0; + + data.object = p_object; + data.key = p_property; + data.initial_val = p_initial_val; + data.final_val = p_final_val; + data.times_in_sec = p_times_in_sec; + data.trans_type = p_trans_type; + data.ease_type = p_ease_type; + + if(!_calc_delta_val(data)) + return false; + + interpolates.push_back(data); + return true; +} + +bool Tween::interpolate_method(Variant p_object + , String p_method + , Variant p_initial_val + , Variant p_final_val + , real_t p_times_in_sec + , TransitionType p_trans_type + , EaseType p_ease_type +) { + + ERR_FAIL_COND_V(p_object.get_type() != Variant::OBJECT, false); + ERR_FAIL_COND_V(p_initial_val.get_type() != p_final_val.get_type(), false); + ERR_FAIL_COND_V(p_times_in_sec <= 0, false); + + Object *obj = (Object *) p_object; + ERR_FAIL_COND_V(!obj->has_method(p_method), false); + + InterpolateData data; + data.active = true; + data.is_method = true; + data.elapsed = 0; + + data.object = p_object; + data.key = p_method; + data.initial_val = p_initial_val; + data.final_val = p_final_val; + data.times_in_sec = p_times_in_sec; + data.trans_type = p_trans_type; + data.ease_type = p_ease_type; + + if(!_calc_delta_val(data)) + return false; + + interpolates.push_back(data); + return true; +} + +Tween::Tween() { + + //String autoplay; + tween_process_mode=TWEEN_PROCESS_IDLE; + processing=false; + active=false; + speed_scale=1; +} + +Tween::~Tween() { + +} diff --git a/scene/animation/tween.h b/scene/animation/tween.h new file mode 100644 index 00000000000..9d087f93caf --- /dev/null +++ b/scene/animation/tween.h @@ -0,0 +1,161 @@ +/*************************************************************************/ +/* tween.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* http://www.godotengine.org */ +/*************************************************************************/ +/* 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. */ +/*************************************************************************/ +#ifndef TWEEN_H +#define TWEEN_H + +#include "scene/main/node.h" + + +class Tween : public Node { + + OBJ_TYPE( Tween, Node ); + +public: + enum TweenProcessMode { + TWEEN_PROCESS_FIXED, + TWEEN_PROCESS_IDLE, + }; + + enum TransitionType { + TRANS_LINEAR, + TRANS_SINE, + TRANS_QUINT, + TRANS_QUART, + TRANS_QUAD, + TRANS_EXPO, + TRANS_ELASTIC, + TRANS_CUBIC, + TRANS_CIRC, + TRANS_BOUNCE, + TRANS_BACK, + + TRANS_COUNT, + }; + + enum EaseType { + EASE_IN, + EASE_OUT, + EASE_IN_OUT, + EASE_OUT_IN, + + EASE_COUNT, + }; + +private: + + struct InterpolateData { + bool active; + bool is_method; + real_t elapsed; + + Variant object; + String key; + Variant initial_val; + Variant delta_val; + Variant final_val; + real_t times_in_sec; + TransitionType trans_type; + EaseType ease_type; + }; + + String autoplay; + TweenProcessMode tween_process_mode; + bool processing; + bool active; + float speed_scale; + + List interpolates; + + typedef real_t (*interpolater)(real_t t, real_t b, real_t c, real_t d); + static interpolater interpolaters[TRANS_COUNT][EASE_COUNT]; + + real_t _run_equation(TransitionType p_trans_type, EaseType p_ease_type, real_t t, real_t b, real_t c, real_t d); + Variant _run_equation(InterpolateData& p_data); + bool _calc_delta_val(InterpolateData& p_data); + bool _apply_tween_value(InterpolateData& p_data, Variant& value); + + void _tween_process(float p_delta); + void _set_process(bool p_process,bool p_force=false); + +protected: + + bool _set(const StringName& p_name, const Variant& p_value); + bool _get(const StringName& p_name,Variant &r_ret) const; + void _get_property_list(List *p_list) const; + void _notification(int p_what); + + static void _bind_methods(); + +public: + + bool is_active() const; + void set_active(bool p_active); + + void set_tween_process_mode(TweenProcessMode p_mode); + TweenProcessMode get_tween_process_mode() const; + + void set_speed(float p_speed); + float get_speed() const; + + bool start(); + bool reset(Variant p_object, String p_key); + bool reset_all(); + bool stop(Variant p_object, String p_key); + bool stop_all(); + bool resume(Variant p_object, String p_key); + bool resume_all(); + + bool interpolate_property(Variant p_object + , String p_property + , Variant p_initial_val + , Variant p_final_val + , real_t p_times_in_sec + , TransitionType p_trans_type + , EaseType p_ease_type + ); + + bool interpolate_method(Variant p_object + , String p_method + , Variant p_initial_val + , Variant p_final_val + , real_t p_times_in_sec + , TransitionType p_trans_type + , EaseType p_ease_type + ); + + Tween(); + ~Tween(); +}; + +VARIANT_ENUM_CAST( Tween::TweenProcessMode ); +VARIANT_ENUM_CAST( Tween::TransitionType ); +VARIANT_ENUM_CAST( Tween::EaseType ); + +#endif + diff --git a/scene/animation/tween_interpolaters.cpp b/scene/animation/tween_interpolaters.cpp new file mode 100644 index 00000000000..7d0f2cd4e0a --- /dev/null +++ b/scene/animation/tween_interpolaters.cpp @@ -0,0 +1,407 @@ +/*************************************************************************/ +/* tween.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* http://www.godotengine.org */ +/*************************************************************************/ +/* 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. */ +/*************************************************************************/ +#include "tween.h" + +const real_t pi = 3.1415926535898; + +/////////////////////////////////////////////////////////////////////////// +// linear +/////////////////////////////////////////////////////////////////////////// +namespace linear { + static real_t in(real_t t, real_t b, real_t c, real_t d) + { + return c * t / d + b; + } + + static real_t out(real_t t, real_t b, real_t c, real_t d) + { + return c * t / d + b; + } + + static real_t in_out(real_t t, real_t b, real_t c, real_t d) + { + return c * t / d + b; + } + + static real_t out_in(real_t t, real_t b, real_t c, real_t d) + { + return c * t / d + b; + } +}; +/////////////////////////////////////////////////////////////////////////// +// sine +/////////////////////////////////////////////////////////////////////////// +namespace sine { + static real_t in(real_t t, real_t b, real_t c, real_t d) + { + return -c * cos(t / d * (pi / 2)) + c + b; + } + + static real_t out(real_t t, real_t b, real_t c, real_t d) + { + return c * sin(t / d * (pi / 2)) + b; + } + + static real_t in_out(real_t t, real_t b, real_t c, real_t d) + { + return -c / 2 * (cos(pi * t / d) - 1) + b; + } + + static real_t out_in(real_t t, real_t b, real_t c, real_t d) + { + return (t < d / 2) + ? out(t * 2, b, c / 2, d) + : in((t * 2) - d, b + c / 2, c / 2, d) + ; + } +}; +/////////////////////////////////////////////////////////////////////////// +// quint +/////////////////////////////////////////////////////////////////////////// +namespace quint { + static real_t in(real_t t, real_t b, real_t c, real_t d) + { + return c * pow(t / d, 5) + b; + } + + static real_t out(real_t t, real_t b, real_t c, real_t d) + { + return c * (pow(t / d - 1, 5) + 1) + b; + } + + static real_t in_out(real_t t, real_t b, real_t c, real_t d) + { + t = t / d * 2; + if (t < 1) return c / 2 * pow(t, 5) + b; + return c / 2 * (pow(t - 2, 5) + 2) + b; + } + + static real_t out_in(real_t t, real_t b, real_t c, real_t d) + { + return (t < d / 2) + ? out(t * 2, b, c / 2, d) + : in((t * 2) - d, b + c / 2, c / 2, d) + ; + } +}; +/////////////////////////////////////////////////////////////////////////// +// quart +/////////////////////////////////////////////////////////////////////////// +namespace quart { + static real_t in(real_t t, real_t b, real_t c, real_t d) + { + return c * pow(t / d, 4) + b; + } + + static real_t out(real_t t, real_t b, real_t c, real_t d) + { + return -c * (pow(t / d - 1, 4) - 1) + b; + } + + static real_t in_out(real_t t, real_t b, real_t c, real_t d) + { + t = t / d * 2; + if (t < 1) return c / 2 * pow(t, 4) + b; + return -c / 2 * (pow(t - 2, 4) - 2) + b; + } + + static real_t out_in(real_t t, real_t b, real_t c, real_t d) + { + return (t < d / 2) + ? out(t * 2, b, c / 2, d) + : in((t * 2) - d, b + c / 2, c / 2, d) + ; + } +}; +/////////////////////////////////////////////////////////////////////////// +// quad +/////////////////////////////////////////////////////////////////////////// +namespace quad { + static real_t in(real_t t, real_t b, real_t c, real_t d) + { + return c * pow(t / d, 2) + b; + } + + static real_t out(real_t t, real_t b, real_t c, real_t d) + { + t = t / d; + return -c * t * (t - 2) + b; + } + + static real_t in_out(real_t t, real_t b, real_t c, real_t d) + { + t = t / d * 2; + if (t < 1) return c / 2 * pow(t, 2) + b; + return -c / 2 * ((t - 1) * (t - 3) - 1) + b; + } + + static real_t out_in(real_t t, real_t b, real_t c, real_t d) + { + return (t < d / 2) + ? out(t * 2, b, c / 2, d) + : in((t * 2) - d, b + c / 2, c / 2, d) + ; + } +}; +/////////////////////////////////////////////////////////////////////////// +// expo +/////////////////////////////////////////////////////////////////////////// +namespace expo { + static real_t in(real_t t, real_t b, real_t c, real_t d) + { + if (t == 0) return b; + return c * pow(2, 10 * (t / d - 1)) + b - c * 0.001; + } + + static real_t out(real_t t, real_t b, real_t c, real_t d) + { + if (t == d) return b + c; + return c * 1.001 * (-pow(2, -10 * t / d) + 1) + b; + } + + static real_t in_out(real_t t, real_t b, real_t c, real_t d) + { + if (t == 0) return b; + if (t == d) return b + c; + t = t / d * 2; + if (t < 1) return c / 2 * pow(2, 10 * (t - 1)) + b - c * 0.0005; + return c / 2 * 1.0005 * (-pow(2, -10 * (t - 1)) + 2) + b; + } + + static real_t out_in(real_t t, real_t b, real_t c, real_t d) + { + return (t < d / 2) + ? out(t * 2, b, c / 2, d) + : in((t * 2) - d, b + c / 2, c / 2, d) + ; + } +}; +/////////////////////////////////////////////////////////////////////////// +// elastic +/////////////////////////////////////////////////////////////////////////// +namespace elastic { + static real_t in(real_t t, real_t b, real_t c, real_t d) + { + if (t == 0) return b; + if ((t /= d) == 1) return b + c; + float p = d * 0.3f; + float a = c; + float s = p / 4; + float postFix = a * pow(2,10 * (t -= 1)); // this is a fix, again, with post-increment operators + return -(postFix * sin((t * d - s) * (2 * pi) / p )) + b; + } + + static real_t out(real_t t, real_t b, real_t c, real_t d) + { + if (t == 0) return b; + if ((t /= d) == 1) return b + c; + float p = d * 0.3f; + float a = c; + float s = p / 4; + return (a * pow(2, -10 * t) * sin((t * d - s) * (2 * pi) / p ) + c + b); + } + + static real_t in_out(real_t t, real_t b, real_t c, real_t d) + { + if (t == 0) return b; + if ((t /= d / 2) == 2) return b + c; + float p = d * (0.3f * 1.5f); + float a = c; + float s = p / 4; + + if (t < 1) { + float postFix = a * pow(2, 10 * (t -= 1)); // postIncrement is evil + return -0.5f * (postFix * sin((t * d - s) * (2 * pi) / p)) + b; + } + float postFix = a * pow(2, -10 * (t -= 1)); // postIncrement is evil + return postFix * sin((t * d - s) * (2 * pi) / p ) * 0.5f + c + b; + } + + static real_t out_in(real_t t, real_t b, real_t c, real_t d) + { + return (t < d / 2) + ? out(t * 2, b, c / 2, d) + : in((t * 2) - d, b + c / 2, c / 2, d) + ; + } +}; +/////////////////////////////////////////////////////////////////////////// +// cubic +/////////////////////////////////////////////////////////////////////////// +namespace cubic { + static real_t in(real_t t, real_t b, real_t c, real_t d) + { + return c * (t /= d) * t * t + b; + } + + static real_t out(real_t t, real_t b, real_t c, real_t d) + { + return c * ((t = t / d - 1) * t * t + 1) + b; + } + + static real_t in_out(real_t t, real_t b, real_t c, real_t d) + { + if ((t /= d / 2) < 1) return c / 2 * t * t * t + b; + return c / 2 * ((t -= 2) * t * t + 2) + b; + } + + static real_t out_in(real_t t, real_t b, real_t c, real_t d) + { + return (t < d / 2) + ? out(t * 2, b, c / 2, d) + : in((t * 2) - d, b + c / 2, c / 2, d) + ; + } +}; +/////////////////////////////////////////////////////////////////////////// +// circ +/////////////////////////////////////////////////////////////////////////// +namespace circ { + static real_t in(real_t t, real_t b, real_t c, real_t d) + { + return -c * (sqrt(1 - (t /= d) * t) - 1) + b; + } + + static real_t out(real_t t, real_t b, real_t c, real_t d) + { + return c * sqrt(1 - (t = t / d - 1) * t) + b; + } + + static real_t in_out(real_t t, real_t b, real_t c, real_t d) + { + if ((t /= d / 2) < 1) return -c / 2 * (sqrt(1 - t * t) - 1) + b; + return c / 2 * (sqrt(1 - t * (t -= 2)) + 1) + b; + } + + static real_t out_in(real_t t, real_t b, real_t c, real_t d) + { + return (t < d / 2) + ? out(t * 2, b, c / 2, d) + : in((t * 2) - d, b + c / 2, c / 2, d) + ; + } +}; +/////////////////////////////////////////////////////////////////////////// +// bounce +/////////////////////////////////////////////////////////////////////////// +namespace bounce { + static real_t out(real_t t, real_t b, real_t c, real_t d); + + static real_t in(real_t t, real_t b, real_t c, real_t d) + { + return c - out(d - t, 0, c, d) + b; + } + + static real_t out(real_t t, real_t b, real_t c, real_t d) + { + if ((t /= d) < (1 / 2.75f)) { + return c*(7.5625f*t*t) + b; + } else if (t < (2/2.75f)) { + float postFix = t-=(1.5f/2.75f); + return c*(7.5625f*(postFix)*t + .75f) + b; + } else if (t < (2.5/2.75)) { + float postFix = t-=(2.25f/2.75f); + return c*(7.5625f*(postFix)*t + .9375f) + b; + } else { + float postFix = t-=(2.625f/2.75f); + return c*(7.5625f*(postFix)*t + .984375f) + b; + } + } + + static real_t in_out(real_t t, real_t b, real_t c, real_t d) + { + return (t < d / 2) + ? in(t * 2, b, c / 2, d) + : out((t * 2) - d, b + c / 2, c / 2, d) + ; + } + + static real_t out_in(real_t t, real_t b, real_t c, real_t d) + { + return (t < d / 2) + ? out(t * 2, b, c / 2, d) + : in((t * 2) - d, b + c / 2, c / 2, d) + ; + } +}; +/////////////////////////////////////////////////////////////////////////// +// back +/////////////////////////////////////////////////////////////////////////// +namespace back { + static real_t in(real_t t, real_t b, real_t c, real_t d) + { + float s = 1.70158f; + float postFix = t /= d; + return c * (postFix) * t * ((s + 1) * t - s) + b; + } + + static real_t out(real_t t, real_t b, real_t c, real_t d) + { + float s = 1.70158f; + return c * ((t = t / d- 1) * t * ((s + 1) * t + s) + 1) + b; + } + + static real_t in_out(real_t t, real_t b, real_t c, real_t d) + { + float s = 1.70158f; + if ((t /= d / 2) < 1) return c / 2 * (t * t * (((s *= (1.525f)) + 1) * t - s)) + b; + float postFix = t -= 2; + return c / 2 * ((postFix) * t * (((s *= (1.525f)) + 1) * t + s) + 2) + b; + } + + static real_t out_in(real_t t, real_t b, real_t c, real_t d) + { + return (t < d / 2) + ? out(t * 2, b, c / 2, d) + : in((t * 2) - d, b + c / 2, c / 2, d) + ; + } +}; + +Tween::interpolater Tween::interpolaters[Tween::TRANS_COUNT][Tween::EASE_COUNT] = { + { &linear::in, &linear::out, &linear::in_out, &linear::out_in }, + { &sine::in, &sine::out, &sine::in_out, &sine::out_in }, + { &quint::in, &quint::out, &quint::in_out, &quint::out_in }, + { &quart::in, &quart::out, &quart::in_out, &quart::out_in }, + { &quad::in, &quad::out, &quad::in_out, &quad::out_in }, + { &expo::in, &expo::out, &expo::in_out, &expo::out_in }, + { &elastic::in, &elastic::out, &elastic::in_out, &elastic::out_in }, + { &cubic::in, &cubic::out, &cubic::in_out, &cubic::out_in }, + { &circ::in, &circ::out, &circ::in_out, &circ::out_in }, + { &bounce::in, &bounce::out, &bounce::in_out, &bounce::out_in }, + { &back::in, &back::out, &back::in_out, &back::out_in }, +}; + +real_t Tween::_run_equation(TransitionType p_trans_type, EaseType p_ease_type, real_t t, real_t b, real_t c, real_t d) { + + interpolater cb = interpolaters[p_trans_type][p_ease_type]; + ERR_FAIL_COND_V(cb == NULL, b); + return cb(t, b, c, d); +} + diff --git a/scene/register_scene_types.cpp b/scene/register_scene_types.cpp index f7d6a246e66..9811de3e4a1 100644 --- a/scene/register_scene_types.cpp +++ b/scene/register_scene_types.cpp @@ -108,6 +108,7 @@ #include "scene/animation/animation_player.h" #include "scene/animation/animation_tree_player.h" +#include "scene/animation/tween.h" #include "scene/main/scene_main_loop.h" #include "scene/main/resource_preloader.h" #include "scene/resources/packed_scene.h" @@ -369,6 +370,7 @@ void register_scene_types() { ObjectTypeDB::register_type(); ObjectTypeDB::register_type(); ObjectTypeDB::register_type(); + ObjectTypeDB::register_type(); OS::get_singleton()->yield(); //may take time to init