Add tween seek/repeat support

Add tween demo
This commit is contained in:
sanikoyes 2014-08-20 16:39:28 +08:00
parent b51da466e9
commit 87faf1c046
5 changed files with 525 additions and 4 deletions

View File

@ -0,0 +1,10 @@
[application]
name="Tween Demo"
main_scene="res://main.xml"
icon="icon.png"
[display]
stretch_mode="2d"
stretch_aspect="keep_width"

127
demos/misc/tween/main.gd Normal file
View File

@ -0,0 +1,127 @@
extends Control
# member variables here, example:
# var a=2
# var b="textvar"
var trans = ["linear", "sine", "quint", "quart", "quad", "expo", "elastic", "cubic", "circ", "bounce", "back"]
var eases = ["in", "out", "in_out", "out_in"]
var modes = ["move", "color", "scale", "rotate", "repeat", "pause"]
var state = {
trans = Tween.TRANS_LINEAR,
eases = Tween.EASE_IN,
}
func _ready():
for index in range(trans.size()):
var name = trans[index]
get_node("trans/" + name).connect("pressed", self, "on_trans_changed", [name, index])
for index in range(eases.size()):
var name = eases[index]
get_node("eases/" + name).connect("pressed", self, "on_eases_changed", [name, index])
for index in range(modes.size()):
var name = modes[index]
get_node("modes/" + name).connect("pressed", self, "on_modes_changed", [name])
get_node("color/color_from").set_color(Color(1, 0, 0, 1))
get_node("color/color_from").connect("color_changed", self, "on_color_changed")
get_node("color/color_to").set_color(Color(0, 1, 1, 1))
get_node("color/color_to").connect("color_changed", self, "on_color_changed")
get_node("trans/linear").set_pressed(true)
get_node("eases/in").set_pressed(true)
get_node("modes/move").set_pressed(true)
get_node("modes/repeat").set_pressed(true)
reset_tween()
# Initalization here
pass
func on_trans_changed(name, index):
for index in range(trans.size()):
var pressed = trans[index] == name
var btn = get_node("trans/" + trans[index])
btn.set_pressed(pressed)
btn.set_ignore_mouse(pressed)
state.trans = index
reset_tween()
func on_eases_changed(name, index):
for index in range(eases.size()):
var pressed = eases[index] == name
var btn = get_node("eases/" + eases[index])
btn.set_pressed(pressed)
btn.set_ignore_mouse(pressed)
state.eases = index
reset_tween()
func on_modes_changed(name):
var tween = get_node("tween")
if name == "pause":
if get_node("modes/pause").is_pressed():
tween.stop_all()
get_node("timeline").show()
else:
tween.resume_all()
get_node("timeline").hide()
else:
reset_tween()
func on_color_changed(color):
reset_tween()
func reset_tween():
var tween = get_node("tween")
tween.reset_all()
tween.remove_all()
var sprite = get_node("tween/area/sprite")
if get_node("modes/move").is_pressed():
tween.interpolate_method(sprite, "set_pos", Vector2(0,0), Vector2(736, 184), 2, state.trans, state.eases)
if get_node("modes/color").is_pressed():
tween.interpolate_method(sprite, "set_modulate", get_node("color/color_from").get_color(), get_node("color/color_to").get_color(), 2, state.trans, state.eases)
else:
sprite.set_modulate(Color(1, 1, 1, 1))
if get_node("modes/scale").is_pressed():
tween.interpolate_method(sprite, "set_scale", Vector2(0.5,0.5), Vector2(1.5, 1.5), 2, state.trans, state.eases)
if get_node("modes/rotate").is_pressed():
tween.interpolate_method(sprite, "set_rot", 0.0, 6.28, 2, state.trans, state.eases)
tween.set_repeat(get_node("modes/repeat").is_pressed())
tween.start()
if get_node("modes/pause").is_pressed():
tween.stop_all()
get_node("timeline").show()
get_node("timeline").set_value(0)
else:
tween.resume_all()
get_node("timeline").hide()
func _on_tween_step( object, key, elapsed, value ):
var timeline = get_node("timeline")
var ratio = 100 * (elapsed / 2)
timeline.set_value(ratio)
func _on_timeline_value_changed( value ):
if !get_node("modes/pause").is_pressed():
return
var tween = get_node("tween")
tween.seek(2.0 * value / 100)

332
demos/misc/tween/main.xml Normal file

File diff suppressed because one or more lines are too long

View File

@ -37,6 +37,10 @@ bool Tween::_set(const StringName& p_name, const Variant& p_value) {
} else if (name=="playback/active") {
set_active(p_value);
} else if (name=="playback/repeat") {
set_repeat(p_value);
}
return true;
}
@ -51,13 +55,18 @@ bool Tween::_get(const StringName& p_name,Variant &r_ret) const {
} else if (name=="playback/active") {
r_ret=is_active();
} else if(name=="playback/repeat") {
r_ret=is_repeat();
}
return true;
}
void Tween::_get_property_list(List<PropertyInfo> *p_list) const {
p_list->push_back( PropertyInfo( Variant::BOOL, "playback/active", PROPERTY_HINT_NONE,"" ) );
p_list->push_back( PropertyInfo( Variant::BOOL, "playback/repeat", PROPERTY_HINT_NONE,"" ) );
p_list->push_back( PropertyInfo( Variant::REAL, "playback/speed", PROPERTY_HINT_RANGE, "-64,64,0.01") );
}
@ -104,6 +113,9 @@ 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("is_repeat"),&Tween::is_repeat );
ObjectTypeDB::bind_method(_MD("set_repeat","repeat"),&Tween::set_repeat );
ObjectTypeDB::bind_method(_MD("set_speed","speed"),&Tween::set_speed);
ObjectTypeDB::bind_method(_MD("get_speed"),&Tween::get_speed);
@ -119,13 +131,14 @@ void Tween::_bind_methods() {
ObjectTypeDB::bind_method(_MD("resume_all"),&Tween::resume_all );
ObjectTypeDB::bind_method(_MD("remove"),&Tween::remove );
ObjectTypeDB::bind_method(_MD("remove_all"),&Tween::remove_all );
ObjectTypeDB::bind_method(_MD("seek"),&Tween::seek );
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_SIGNAL( MethodInfo("tween_step", PropertyInfo( Variant::OBJECT,"object"), PropertyInfo( Variant::STRING,"key"), PropertyInfo( Variant::REAL,"elapsed"), PropertyInfo( Variant::OBJECT,"value")) );
ADD_SIGNAL( MethodInfo("tween_complete", PropertyInfo( Variant::OBJECT,"object"), PropertyInfo( Variant::STRING,"key")) );
//ADD_PROPERTY( PropertyInfo( Variant::BOOL, "activate"), _SCS("set_active"), _SCS("is_active"));
@ -334,8 +347,14 @@ void Tween::_tween_process(float p_delta) {
for(List<InterpolateData>::Element *E=interpolates.front();E;E=E->next()) {
InterpolateData& data = E->get();
if(!data.active || data.elapsed == data.times_in_sec)
if(!data.active)
continue;
if(data.elapsed == data.times_in_sec) {
if(!repeat)
continue;
data.elapsed = 0;
}
if(data.elapsed == 0)
emit_signal("tween_start",data.object,data.key);
@ -345,7 +364,7 @@ void Tween::_tween_process(float p_delta) {
data.elapsed = data.times_in_sec;
Variant result = _run_equation(data);
emit_signal("tween_step",data.object,data.key,result);
emit_signal("tween_step",data.object,data.key,data.elapsed,result);
_apply_tween_value(data, result);
@ -400,6 +419,16 @@ void Tween::set_active(bool p_active) {
_set_process(processing,true);
}
bool Tween::is_repeat() const {
return repeat;
}
void Tween::set_repeat(bool p_repeat) {
repeat = p_repeat;
}
void Tween::set_speed(float p_speed) {
speed_scale=p_speed;
@ -511,6 +540,22 @@ bool Tween::remove_all() {
return true;
}
bool Tween::seek(real_t p_time) {
for(List<InterpolateData>::Element *E=interpolates.front();E;E=E->next()) {
InterpolateData& data = E->get();
data.elapsed = p_time;
if(data.elapsed > data.times_in_sec)
data.elapsed = data.times_in_sec;
Variant result = _run_equation(data);
_apply_tween_value(data, result);
}
return true;
}
bool Tween::_calc_delta_val(InterpolateData& p_data) {
@ -703,6 +748,7 @@ Tween::Tween() {
tween_process_mode=TWEEN_PROCESS_IDLE;
processing=false;
active=false;
repeat=false;
speed_scale=1;
}

View File

@ -88,6 +88,7 @@ private:
TweenProcessMode tween_process_mode;
bool processing;
bool active;
bool repeat;
float speed_scale;
List<InterpolateData> interpolates;
@ -117,6 +118,9 @@ public:
bool is_active() const;
void set_active(bool p_active);
bool is_repeat() const;
void set_repeat(bool p_repeat);
void set_tween_process_mode(TweenProcessMode p_mode);
TweenProcessMode get_tween_process_mode() const;
@ -133,6 +137,8 @@ public:
bool remove(Variant p_object, String p_key);
bool remove_all();
bool seek(real_t p_time);
bool interpolate_property(Variant p_object
, String p_property
, Variant p_initial_val