diff options
author | Juan Linietsky <reduzio@gmail.com> | 2014-09-17 10:00:24 -0300 |
---|---|---|
committer | Juan Linietsky <reduzio@gmail.com> | 2014-09-17 10:00:24 -0300 |
commit | 574a84b40ceb0d52d4bbee5390c18af08d5d754a (patch) | |
tree | 5020920dd352fd8eb2340ac79c6cf1c3a6be140a | |
parent | 337fe6770fbb24948160b900a111d9cb811647ff (diff) | |
parent | 5bddfa201e4982a27dfa2e6d70e2433e7861889c (diff) |
Merge pull request #628 from sanikoyes/PR-tween-support
thanks sanikoyes this is great!
-rw-r--r-- | demos/misc/tween/engine.cfg | 11 | ||||
-rw-r--r-- | demos/misc/tween/main.gd | 164 | ||||
-rw-r--r-- | demos/misc/tween/main.xml | 367 | ||||
-rw-r--r-- | scene/animation/tween.cpp | 1219 | ||||
-rw-r--r-- | scene/animation/tween.h | 239 | ||||
-rw-r--r-- | scene/animation/tween_interpolaters.cpp | 407 | ||||
-rw-r--r-- | scene/register_scene_types.cpp | 2 |
7 files changed, 2409 insertions, 0 deletions
diff --git a/demos/misc/tween/engine.cfg b/demos/misc/tween/engine.cfg new file mode 100644 index 0000000000..f97e540dbd --- /dev/null +++ b/demos/misc/tween/engine.cfg @@ -0,0 +1,11 @@ +[application] + +name="Tween Demo" +main_scene="res://main.xml" +icon="icon.png" +target_fps=60 + +[display] + +stretch_mode="2d" +stretch_aspect="keep_width" diff --git a/demos/misc/tween/main.gd b/demos/misc/tween/main.gd new file mode 100644 index 0000000000..364651c827 --- /dev/null +++ b/demos/misc/tween/main.gd @@ -0,0 +1,164 @@ + +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", "callback", "follow", "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").set_ignore_mouse(false) + else: + tween.resume_all() + get_node("timeline").set_ignore_mouse(true) + else: + reset_tween() + +func on_color_changed(color): + reset_tween() + +func reset_tween(): + var tween = get_node("tween") + var pos = tween.tell() + tween.reset_all() + tween.remove_all() + + var sprite = get_node("tween/area/sprite") + var follow = get_node("tween/area/follow") + var follow_2 = get_node("tween/area/follow_2") + var size = get_node("tween/area").get_size() + + if get_node("modes/move").is_pressed(): + tween.interpolate_method(sprite, "set_pos", Vector2(0,0), Vector2(size.width, size.height), 2, state.trans, state.eases) + tween.interpolate_property(sprite, "transform/pos", Vector2(size.width,size.height), Vector2(0, 0), 2, state.trans, state.eases, 2) + + 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) + tween.interpolate_property(sprite, "modulate", get_node("color/color_to").get_color(), get_node("color/color_from").get_color(), 2, state.trans, state.eases, 2) + 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) + tween.interpolate_property(sprite, "transform/scale", Vector2(1.5,1.5), Vector2(0.5, 0.5), 2, state.trans, state.eases, 2) + else: + sprite.set_scale(Vector2(1, 1)) + + if get_node("modes/rotate").is_pressed(): + tween.interpolate_method(sprite, "_set_rotd", 0, 360, 2, state.trans, state.eases) + tween.interpolate_property(sprite, "transform/rot", 360, 0, 2, state.trans, state.eases, 2) + + if get_node("modes/callback").is_pressed(): + tween.interpolate_callback(self, "on_callback", 0.5, "0.5 second's after") + tween.interpolate_callback(self, "on_callback", 1.2, "1.2 second's after") + + if get_node("modes/follow").is_pressed(): + follow.show() + follow_2.show() + + tween.follow_method(follow, "set_pos", Vector2(0, size.height), sprite, "get_pos", 2, state.trans, state.eases) + tween.targeting_method(follow, "set_pos", sprite, "get_pos", Vector2(0, size.height), 2, state.trans, state.eases, 2) + + tween.targeting_property(follow_2, "transform/pos", sprite, "transform/pos", Vector2(size.width, 0), 2, state.trans, state.eases) + tween.follow_property(follow_2, "transform/pos", Vector2(size.width, 0), sprite, "transform/pos", 2, state.trans, state.eases, 2) + else: + follow.hide() + follow_2.hide() + + tween.set_repeat(get_node("modes/repeat").is_pressed()) + tween.start() + tween.seek(pos) + + if get_node("modes/pause").is_pressed(): + tween.stop_all() + get_node("timeline").set_ignore_mouse(false) + get_node("timeline").set_value(0) + else: + tween.resume_all() + get_node("timeline").set_ignore_mouse(true) + +func _on_tween_step( object, key, elapsed, value ): + + var timeline = get_node("timeline") + + var tween = get_node("tween") + var runtime = tween.get_runtime() + + var ratio = 100 * (elapsed / runtime) + timeline.set_value(ratio) + + +func _on_timeline_value_changed( value ): + if !get_node("modes/pause").is_pressed(): + return + + var tween = get_node("tween") + var runtime = tween.get_runtime() + tween.seek(runtime * value / 100) + +func on_callback(arg): + var label = get_node("tween/area/label") + label.add_text("on_callback -> " + arg + "\n") diff --git a/demos/misc/tween/main.xml b/demos/misc/tween/main.xml new file mode 100644 index 0000000000..6580ba04da --- /dev/null +++ b/demos/misc/tween/main.xml @@ -0,0 +1,367 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<resource_file type="PackedScene" subresource_count="3" version="1.0" version_name="Godot Engine v1.0.3917-beta1"> + <ext_resource path="res://icon.png" type="Texture"></ext_resource> + <ext_resource path="res://main.gd" type="Script"></ext_resource> + <main_resource> + <dictionary name="_bundled" shared="false"> + <string> "names" </string> + <string_array len="115"> + <string> "main" </string> + <string> "Control" </string> + <string> "_import_path" </string> + <string> "visibility/visible" </string> + <string> "visibility/opacity" </string> + <string> "visibility/self_opacity" </string> + <string> "visibility/behind_parent" </string> + <string> "margin/right" </string> + <string> "margin/bottom" </string> + <string> "transform/rot" </string> + <string> "transform/scale" </string> + <string> "focus_neighbour/left" </string> + <string> "focus_neighbour/top" </string> + <string> "focus_neighbour/right" </string> + <string> "focus_neighbour/bottom" </string> + <string> "focus/ignore_mouse" </string> + <string> "focus/stop_mouse" </string> + <string> "size_flags/horizontal" </string> + <string> "size_flags/vertical" </string> + <string> "size_flags/stretch_ratio" </string> + <string> "script/script" </string> + <string> "__meta__" </string> + <string> "trans" </string> + <string> "VBoxContainer" </string> + <string> "margin/left" </string> + <string> "margin/top" </string> + <string> "linear" </string> + <string> "Button" </string> + <string> "disabled" </string> + <string> "pressed" </string> + <string> "toggle_mode" </string> + <string> "click_on_press" </string> + <string> "text" </string> + <string> "icon" </string> + <string> "flat" </string> + <string> "clip_text" </string> + <string> "align" </string> + <string> "sine" </string> + <string> "quint" </string> + <string> "quart" </string> + <string> "quad" </string> + <string> "expo" </string> + <string> "elastic" </string> + <string> "cubic" </string> + <string> "circ" </string> + <string> "bounce" </string> + <string> "back" </string> + <string> "eases" </string> + <string> "in" </string> + <string> "out" </string> + <string> "in_out" </string> + <string> "out_in" </string> + <string> "modes" </string> + <string> "move" </string> + <string> "color" </string> + <string> "scale" </string> + <string> "rotate" </string> + <string> "callback" </string> + <string> "follow" </string> + <string> "repeat" </string> + <string> "pause" </string> + <string> "label_1" </string> + <string> "Label" </string> + <string> "range/min" </string> + <string> "range/max" </string> + <string> "range/step" </string> + <string> "range/page" </string> + <string> "range/value" </string> + <string> "range/exp_edit" </string> + <string> "rounded_values" </string> + <string> "valign" </string> + <string> "autowrap" </string> + <string> "uppercase" </string> + <string> "percent_visible" </string> + <string> "color_from" </string> + <string> "ColorPicker" </string> + <string> "label_2" </string> + <string> "color_to" </string> + <string> "tween" </string> + <string> "Tween" </string> + <string> "playback/process_mode" </string> + <string> "playback/active" </string> + <string> "playback/repeat" </string> + <string> "playback/speed" </string> + <string> "area" </string> + <string> "Panel" </string> + <string> "label" </string> + <string> "RichTextLabel" </string> + <string> "scroll_active" </string> + <string> "scroll_follow" </string> + <string> "tab_size" </string> + <string> "selection_enabled" </string> + <string> "sprite" </string> + <string> "Sprite" </string> + <string> "transform/pos" </string> + <string> "texture" </string> + <string> "centered" </string> + <string> "offset" </string> + <string> "flip_h" </string> + <string> "flip_v" </string> + <string> "vframes" </string> + <string> "hframes" </string> + <string> "frame" </string> + <string> "modulate" </string> + <string> "region" </string> + <string> "region_rect" </string> + <string> "follow_2" </string> + <string> "timeline" </string> + <string> "HSlider" </string> + <string> "tick_count" </string> + <string> "ticks_on_borders" </string> + <string> "_on_tween_step" </string> + <string> "tween_step" </string> + <string> "_on_timeline_value_changed" </string> + <string> "value_changed" </string> + </string_array> + <string> "version" </string> + <int> 1 </int> + <string> "conn_count" </string> + <int> 2 </int> + <string> "node_count" </string> + <int> 39 </int> + <string> "variants" </string> + <array len="104" shared="false"> + <node_path> "" </node_path> + <bool> True </bool> + <real> 1 </real> + <bool> False </bool> + <real> 800 </real> + <real> 600 </real> + <real> 0 </real> + <vector2> 1, 1 </vector2> + <int> 2 </int> + <resource resource_type="Script" path="res://main.gd"> </resource> + <dictionary shared="false"> + <string> "__editor_plugin_states__" </string> + <dictionary shared="false"> + <string> "Script" </string> + <dictionary shared="false"> + <string> "current" </string> + <int> 0 </int> + <string> "sources" </string> + <array len="1" shared="false"> + <string> "res://main.gd" </string> + </array> + </dictionary> + <string> "2D" </string> + <dictionary shared="false"> + <string> "pixel_snap" </string> + <bool> False </bool> + <string> "zoom" </string> + <real> 1.360374 </real> + <string> "use_snap" </string> + <bool> True </bool> + <string> "ofs" </string> + <vector2> -215.073, -20.8125 </vector2> + <string> "snap" </string> + <int> 8 </int> + </dictionary> + <string> "3D" </string> + <dictionary shared="false"> + <string> "zfar" </string> + <real> 500 </real> + <string> "fov" </string> + <real> 45 </real> + <string> "viewports" </string> + <array len="4" shared="false"> + <dictionary shared="false"> + <string> "distance" </string> + <real> 4 </real> + <string> "x_rot" </string> + <real> 0 </real> + <string> "y_rot" </string> + <real> 0 </real> + <string> "use_orthogonal" </string> + <bool> False </bool> + <string> "use_environment" </string> + <bool> False </bool> + <string> "pos" </string> + <vector3> 0, 0, 0 </vector3> + </dictionary> + <dictionary shared="false"> + <string> "distance" </string> + <real> 4 </real> + <string> "x_rot" </string> + <real> 0 </real> + <string> "y_rot" </string> + <real> 0 </real> + <string> "use_orthogonal" </string> + <bool> False </bool> + <string> "use_environment" </string> + <bool> False </bool> + <string> "pos" </string> + <vector3> 0, 0, 0 </vector3> + </dictionary> + <dictionary shared="false"> + <string> "distance" </string> + <real> 4 </real> + <string> "x_rot" </string> + <real> 0 </real> + <string> "y_rot" </string> + <real> 0 </real> + <string> "use_orthogonal" </string> + <bool> False </bool> + <string> "use_environment" </string> + <bool> False </bool> + <string> "pos" </string> + <vector3> 0, 0, 0 </vector3> + </dictionary> + <dictionary shared="false"> + <string> "distance" </string> + <real> 4 </real> + <string> "x_rot" </string> + <real> 0 </real> + <string> "y_rot" </string> + <real> 0 </real> + <string> "use_orthogonal" </string> + <bool> False </bool> + <string> "use_environment" </string> + <bool> False </bool> + <string> "pos" </string> + <vector3> 0, 0, 0 </vector3> + </dictionary> + </array> + <string> "viewport_mode" </string> + <int> 1 </int> + <string> "default_light" </string> + <bool> True </bool> + <string> "show_grid" </string> + <bool> True </bool> + <string> "show_origin" </string> + <bool> True </bool> + <string> "znear" </string> + <real> 0.1 </real> + </dictionary> + </dictionary> + <string> "__editor_run_settings__" </string> + <dictionary shared="false"> + <string> "custom_args" </string> + <string> "-l $scene" </string> + <string> "run_mode" </string> + <int> 0 </int> + </dictionary> + <string> "__editor_plugin_screen__" </string> + <string> "Script" </string> + </dictionary> + <real> 56 </real> + <real> 256 </real> + <real> 129 </real> + <real> 582 </real> + <dictionary shared="false"> + <string> "_editor_collapsed" </string> + <bool> True </bool> + </dictionary> + <real> 73 </real> + <real> 26 </real> + <string> "linear" </string> + <resource name=""></resource> <int> 1 </int> + <real> 30 </real> + <string> "sine" </string> + <real> 60 </real> + <real> 86 </real> + <string> "quint" </string> + <real> 90 </real> + <real> 116 </real> + <string> "quart" </string> + <real> 120 </real> + <real> 146 </real> + <string> "quad" </string> + <real> 150 </real> + <real> 176 </real> + <string> "expo" </string> + <real> 180 </real> + <real> 206 </real> + <string> "elastic" </string> + <real> 210 </real> + <real> 236 </real> + <string> "cubic" </string> + <real> 240 </real> + <real> 266 </real> + <string> "circ" </string> + <real> 270 </real> + <real> 296 </real> + <string> "bounce" </string> + <real> 300 </real> + <real> 326 </real> + <string> "back" </string> + <real> 152 </real> + <real> 215 </real> + <real> 372 </real> + <dictionary shared="false"> + <string> "_editor_collapsed" </string> + <bool> True </bool> + </dictionary> + <real> 63 </real> + <string> "in" </string> + <string> "out" </string> + <string> "in_out" </string> + <string> "out_in" </string> + <real> 317 </real> + <real> 492 </real> + <dictionary shared="false"> + <string> "_editor_collapsed" </string> + <bool> True </bool> + </dictionary> + <real> 77 </real> + <string> "move" </string> + <string> "color" </string> + <string> "scale" </string> + <string> "rotate" </string> + <string> "callback" </string> + <string> "follow" </string> + <string> "repeat" </string> + <string> "pause" </string> + <real> 384 </real> + <real> 760 </real> + <real> 592 </real> + <dictionary shared="false"> + <string> "_editor_collapsed" </string> + <bool> True </bool> + </dictionary> + <real> 376 </real> + <real> 19 </real> + <string> "Color From:" </string> + <int> 0 </int> + <real> -1 </real> + <real> 23 </real> + <real> 174 </real> + <real> 178 </real> + <real> 197 </real> + <string> "Color To:" </string> + <real> 201 </real> + <real> 352 </real> + <real> 32 </real> + <real> 768 </real> + <real> 216 </real> + <real> 24 </real> + <real> 552 </real> + <real> 160 </real> + <string> "" </string> + <int> 4 </int> + <vector2> 0, 0 </vector2> + <resource resource_type="Texture" path="res://icon.png"> </resource> + <color> 1, 1, 1, 1 </color> + <rect2> 0, 0, 0, 0 </rect2> + <vector2> 0, 184 </vector2> + <vector2> 736, 0 </vector2> + <real> 40 </real> + <real> 224 </real> + <real> 100 </real> + </array> + <string> "nodes" </string> + <int_array len="2229"> -1, -1, 1, 0, -1, 20, 2, 0, 3, 1, 4, 2, 5, 2, 6, 3, 7, 4, 8, 5, 9, 6, 10, 7, 11, 0, 12, 0, 13, 0, 14, 0, 15, 3, 16, 1, 17, 8, 18, 8, 19, 2, 20, 9, 21, 10, 0, 0, 0, 23, 22, -1, 21, 2, 0, 3, 1, 4, 2, 5, 2, 6, 3, 24, 11, 25, 12, 7, 13, 8, 14, 9, 6, 10, 7, 11, 0, 12, 0, 13, 0, 14, 0, 15, 3, 16, 3, 17, 8, 18, 8, 19, 2, 21, 15, 0, 1, 0, 27, 26, -1, 27, 2, 0, 3, 1, 4, 2, 5, 2, 6, 3, 7, 16, 8, 17, 9, 6, 10, 7, 11, 0, 12, 0, 13, 0, 14, 0, 15, 3, 16, 1, 17, 8, 18, 8, 19, 2, 28, 3, 29, 3, 30, 1, 31, 3, 32, 18, 33, 19, 34, 3, 35, 3, 36, 20, 0, 1, 0, 27, 37, -1, 28, 2, 0, 3, 1, 4, 2, 5, 2, 6, 3, 25, 21, 7, 16, 8, 11, 9, 6, 10, 7, 11, 0, 12, 0, 13, 0, 14, 0, 15, 3, 16, 1, 17, 8, 18, 8, 19, 2, 28, 3, 29, 3, 30, 1, 31, 3, 32, 22, 33, 19, 34, 3, 35, 3, 36, 20, 0, 1, 0, 27, 38, -1, 28, 2, 0, 3, 1, 4, 2, 5, 2, 6, 3, 25, 23, 7, 16, 8, 24, 9, 6, 10, 7, 11, 0, 12, 0, 13, 0, 14, 0, 15, 3, 16, 1, 17, 8, 18, 8, 19, 2, 28, 3, 29, 3, 30, 1, 31, 3, 32, 25, 33, 19, 34, 3, 35, 3, 36, 20, 0, 1, 0, 27, 39, -1, 28, 2, 0, 3, 1, 4, 2, 5, 2, 6, 3, 25, 26, 7, 16, 8, 27, 9, 6, 10, 7, 11, 0, 12, 0, 13, 0, 14, 0, 15, 3, 16, 1, 17, 8, 18, 8, 19, 2, 28, 3, 29, 3, 30, 1, 31, 3, 32, 28, 33, 19, 34, 3, 35, 3, 36, 20, 0, 1, 0, 27, 40, -1, 28, 2, 0, 3, 1, 4, 2, 5, 2, 6, 3, 25, 29, 7, 16, 8, 30, 9, 6, 10, 7, 11, 0, 12, 0, 13, 0, 14, 0, 15, 3, 16, 1, 17, 8, 18, 8, 19, 2, 28, 3, 29, 3, 30, 1, 31, 3, 32, 31, 33, 19, 34, 3, 35, 3, 36, 20, 0, 1, 0, 27, 41, -1, 28, 2, 0, 3, 1, 4, 2, 5, 2, 6, 3, 25, 32, 7, 16, 8, 33, 9, 6, 10, 7, 11, 0, 12, 0, 13, 0, 14, 0, 15, 3, 16, 1, 17, 8, 18, 8, 19, 2, 28, 3, 29, 3, 30, 1, 31, 3, 32, 34, 33, 19, 34, 3, 35, 3, 36, 20, 0, 1, 0, 27, 42, -1, 28, 2, 0, 3, 1, 4, 2, 5, 2, 6, 3, 25, 35, 7, 16, 8, 36, 9, 6, 10, 7, 11, 0, 12, 0, 13, 0, 14, 0, 15, 3, 16, 1, 17, 8, 18, 8, 19, 2, 28, 3, 29, 3, 30, 1, 31, 3, 32, 37, 33, 19, 34, 3, 35, 3, 36, 20, 0, 1, 0, 27, 43, -1, 28, 2, 0, 3, 1, 4, 2, 5, 2, 6, 3, 25, 38, 7, 16, 8, 39, 9, 6, 10, 7, 11, 0, 12, 0, 13, 0, 14, 0, 15, 3, 16, 1, 17, 8, 18, 8, 19, 2, 28, 3, 29, 3, 30, 1, 31, 3, 32, 40, 33, 19, 34, 3, 35, 3, 36, 20, 0, 1, 0, 27, 44, -1, 28, 2, 0, 3, 1, 4, 2, 5, 2, 6, 3, 25, 41, 7, 16, 8, 42, 9, 6, 10, 7, 11, 0, 12, 0, 13, 0, 14, 0, 15, 3, 16, 1, 17, 8, 18, 8, 19, 2, 28, 3, 29, 3, 30, 1, 31, 3, 32, 43, 33, 19, 34, 3, 35, 3, 36, 20, 0, 1, 0, 27, 45, -1, 28, 2, 0, 3, 1, 4, 2, 5, 2, 6, 3, 25, 44, 7, 16, 8, 45, 9, 6, 10, 7, 11, 0, 12, 0, 13, 0, 14, 0, 15, 3, 16, 1, 17, 8, 18, 8, 19, 2, 28, 3, 29, 3, 30, 1, 31, 3, 32, 46, 33, 19, 34, 3, 35, 3, 36, 20, 0, 1, 0, 27, 46, -1, 28, 2, 0, 3, 1, 4, 2, 5, 2, 6, 3, 25, 47, 7, 16, 8, 48, 9, 6, 10, 7, 11, 0, 12, 0, 13, 0, 14, 0, 15, 3, 16, 1, 17, 8, 18, 8, 19, 2, 28, 3, 29, 3, 30, 1, 31, 3, 32, 49, 33, 19, 34, 3, 35, 3, 36, 20, 0, 0, 0, 23, 47, -1, 21, 2, 0, 3, 1, 4, 2, 5, 2, 6, 3, 24, 50, 25, 12, 7, 51, 8, 52, 9, 6, 10, 7, 11, 0, 12, 0, 13, 0, 14, 0, 15, 3, 16, 3, 17, 8, 18, 8, 19, 2, 21, 53, 0, 13, 0, 27, 48, -1, 27, 2, 0, 3, 1, 4, 2, 5, 2, 6, 3, 7, 54, 8, 17, 9, 6, 10, 7, 11, 0, 12, 0, 13, 0, 14, 0, 15, 3, 16, 1, 17, 8, 18, 8, 19, 2, 28, 3, 29, 3, 30, 1, 31, 3, 32, 55, 33, 19, 34, 3, 35, 3, 36, 20, 0, 13, 0, 27, 49, -1, 28, 2, 0, 3, 1, 4, 2, 5, 2, 6, 3, 25, 21, 7, 54, 8, 11, 9, 6, 10, 7, 11, 0, 12, 0, 13, 0, 14, 0, 15, 3, 16, 1, 17, 8, 18, 8, 19, 2, 28, 3, 29, 3, 30, 1, 31, 3, 32, 56, 33, 19, 34, 3, 35, 3, 36, 20, 0, 13, 0, 27, 50, -1, 28, 2, 0, 3, 1, 4, 2, 5, 2, 6, 3, 25, 23, 7, 54, 8, 24, 9, 6, 10, 7, 11, 0, 12, 0, 13, 0, 14, 0, 15, 3, 16, 1, 17, 8, 18, 8, 19, 2, 28, 3, 29, 3, 30, 1, 31, 3, 32, 57, 33, 19, 34, 3, 35, 3, 36, 20, 0, 13, 0, 27, 51, -1, 28, 2, 0, 3, 1, 4, 2, 5, 2, 6, 3, 25, 26, 7, 54, 8, 27, 9, 6, 10, 7, 11, 0, 12, 0, 13, 0, 14, 0, 15, 3, 16, 1, 17, 8, 18, 8, 19, 2, 28, 3, 29, 3, 30, 1, 31, 3, 32, 58, 33, 19, 34, 3, 35, 3, 36, 20, 0, 0, 0, 23, 52, -1, 21, 2, 0, 3, 1, 4, 2, 5, 2, 6, 3, 24, 41, 25, 12, 7, 59, 8, 60, 9, 6, 10, 7, 11, 0, 12, 0, 13, 0, 14, 0, 15, 3, 16, 3, 17, 8, 18, 8, 19, 2, 21, 61, 0, 18, 0, 27, 53, -1, 27, 2, 0, 3, 1, 4, 2, 5, 2, 6, 3, 7, 62, 8, 17, 9, 6, 10, 7, 11, 0, 12, 0, 13, 0, 14, 0, 15, 3, 16, 1, 17, 8, 18, 8, 19, 2, 28, 3, 29, 3, 30, 1, 31, 3, 32, 63, 33, 19, 34, 3, 35, 3, 36, 20, 0, 18, 0, 27, 54, -1, 28, 2, 0, 3, 1, 4, 2, 5, 2, 6, 3, 25, 21, 7, 62, 8, 11, 9, 6, 10, 7, 11, 0, 12, 0, 13, 0, 14, 0, 15, 3, 16, 1, 17, 8, 18, 8, 19, 2, 28, 3, 29, 3, 30, 1, 31, 3, 32, 64, 33, 19, 34, 3, 35, 3, 36, 20, 0, 18, 0, 27, 55, -1, 28, 2, 0, 3, 1, 4, 2, 5, 2, 6, 3, 25, 23, 7, 62, 8, 24, 9, 6, 10, 7, 11, 0, 12, 0, 13, 0, 14, 0, 15, 3, 16, 1, 17, 8, 18, 8, 19, 2, 28, 3, 29, 3, 30, 1, 31, 3, 32, 65, 33, 19, 34, 3, 35, 3, 36, 20, 0, 18, 0, 27, 56, -1, 28, 2, 0, 3, 1, 4, 2, 5, 2, 6, 3, 25, 26, 7, 62, 8, 27, 9, 6, 10, 7, 11, 0, 12, 0, 13, 0, 14, 0, 15, 3, 16, 1, 17, 8, 18, 8, 19, 2, 28, 3, 29, 3, 30, 1, 31, 3, 32, 66, 33, 19, 34, 3, 35, 3, 36, 20, 0, 18, 0, 27, 57, -1, 28, 2, 0, 3, 1, 4, 2, 5, 2, 6, 3, 25, 29, 7, 62, 8, 30, 9, 6, 10, 7, 11, 0, 12, 0, 13, 0, 14, 0, 15, 3, 16, 1, 17, 8, 18, 8, 19, 2, 28, 3, 29, 3, 30, 1, 31, 3, 32, 67, 33, 19, 34, 3, 35, 3, 36, 20, 0, 18, 0, 27, 58, -1, 28, 2, 0, 3, 1, 4, 2, 5, 2, 6, 3, 25, 32, 7, 62, 8, 33, 9, 6, 10, 7, 11, 0, 12, 0, 13, 0, 14, 0, 15, 3, 16, 1, 17, 8, 18, 8, 19, 2, 28, 3, 29, 3, 30, 1, 31, 3, 32, 68, 33, 19, 34, 3, 35, 3, 36, 20, 0, 18, 0, 27, 59, -1, 28, 2, 0, 3, 1, 4, 2, 5, 2, 6, 3, 25, 35, 7, 62, 8, 36, 9, 6, 10, 7, 11, 0, 12, 0, 13, 0, 14, 0, 15, 3, 16, 1, 17, 8, 18, 8, 19, 2, 28, 3, 29, 3, 30, 1, 31, 3, 32, 69, 33, 19, 34, 3, 35, 3, 36, 20, 0, 18, 0, 27, 60, -1, 28, 2, 0, 3, 1, 4, 2, 5, 2, 6, 3, 25, 38, 7, 62, 8, 39, 9, 6, 10, 7, 11, 0, 12, 0, 13, 0, 14, 0, 15, 3, 16, 1, 17, 8, 18, 8, 19, 2, 28, 3, 29, 3, 30, 1, 31, 3, 32, 70, 33, 19, 34, 3, 35, 3, 36, 20, 0, 0, 0, 23, 54, -1, 21, 2, 0, 3, 1, 4, 2, 5, 2, 6, 3, 24, 71, 25, 41, 7, 72, 8, 73, 9, 6, 10, 7, 11, 0, 12, 0, 13, 0, 14, 0, 15, 3, 16, 3, 17, 8, 18, 8, 19, 2, 21, 74, 0, 27, 0, 62, 61, -1, 30, 2, 0, 3, 1, 4, 2, 5, 2, 6, 3, 7, 75, 8, 76, 9, 6, 10, 7, 11, 0, 12, 0, 13, 0, 14, 0, 15, 1, 16, 1, 17, 8, 19, 2, 63, 6, 64, 2, 65, 2, 66, 2, 67, 6, 68, 3, 69, 3, 32, 77, 36, 78, 70, 78, 71, 3, 72, 3, 73, 79, 0, 27, 0, 75, 74, -1, 19, 2, 0, 3, 1, 4, 2, 5, 2, 6, 3, 25, 80, 7, 75, 8, 81, 9, 6, 10, 7, 11, 0, 12, 0, 13, 0, 14, 0, 15, 3, 16, 3, 17, 8, 18, 8, 19, 2, 0, 27, 0, 62, 76, -1, 31, 2, 0, 3, 1, 4, 2, 5, 2, 6, 3, 25, 82, 7, 75, 8, 83, 9, 6, 10, 7, 11, 0, 12, 0, 13, 0, 14, 0, 15, 1, 16, 1, 17, 8, 19, 2, 63, 6, 64, 2, 65, 2, 66, 2, 67, 6, 68, 3, 69, 3, 32, 84, 36, 78, 70, 78, 71, 3, 72, 3, 73, 79, 0, 27, 0, 75, 77, -1, 19, 2, 0, 3, 1, 4, 2, 5, 2, 6, 3, 25, 85, 7, 75, 8, 86, 9, 6, 10, 7, 11, 0, 12, 0, 13, 0, 14, 0, 15, 3, 16, 3, 17, 8, 18, 8, 19, 2, 0, 0, 0, 79, 78, -1, 5, 2, 0, 80, 20, 81, 1, 82, 1, 83, 2, 0, 32, 0, 85, 84, -1, 20, 2, 0, 3, 1, 4, 2, 5, 2, 6, 3, 24, 87, 25, 87, 7, 88, 8, 89, 9, 6, 10, 7, 11, 0, 12, 0, 13, 0, 14, 0, 15, 3, 16, 1, 17, 8, 18, 8, 19, 2, 0, 33, 0, 87, 86, -1, 24, 2, 0, 3, 1, 4, 2, 5, 2, 6, 3, 24, 33, 25, 90, 7, 91, 8, 92, 9, 6, 10, 7, 11, 0, 12, 0, 13, 0, 14, 0, 15, 3, 16, 1, 17, 8, 19, 2, 32, 93, 88, 1, 89, 1, 90, 94, 91, 3, 0, 33, 0, 93, 92, -1, 19, 2, 0, 3, 1, 4, 2, 5, 2, 6, 3, 94, 95, 9, 6, 10, 7, 95, 96, 96, 1, 97, 95, 98, 3, 99, 3, 100, 20, 101, 20, 102, 78, 103, 97, 104, 3, 105, 98, 0, 33, 0, 93, 58, -1, 19, 2, 0, 3, 1, 4, 2, 5, 2, 6, 3, 94, 99, 9, 6, 10, 7, 95, 96, 96, 1, 97, 95, 98, 3, 99, 3, 100, 20, 101, 20, 102, 78, 103, 97, 104, 3, 105, 98, 0, 33, 0, 93, 106, -1, 19, 2, 0, 3, 1, 4, 2, 5, 2, 6, 3, 94, 100, 9, 6, 10, 7, 95, 96, 96, 1, 97, 95, 98, 3, 99, 3, 100, 20, 101, 20, 102, 78, 103, 97, 104, 3, 105, 98, 0, 0, 0, 108, 107, -1, 28, 2, 0, 3, 1, 4, 2, 5, 2, 6, 3, 24, 101, 25, 102, 7, 72, 8, 41, 9, 6, 10, 7, 11, 0, 12, 0, 13, 0, 14, 0, 15, 3, 16, 1, 17, 8, 19, 2, 63, 6, 64, 103, 65, 2, 66, 6, 67, 2, 68, 3, 69, 3, 109, 78, 110, 3, 0 </int_array> + <string> "conns" </string> + <int_array len="12"> 32, 0, 112, 111, 2, 0, 38, 0, 114, 113, 2, 0 </int_array> + </dictionary> + + </main_resource> +</resource_file> diff --git a/scene/animation/tween.cpp b/scene/animation/tween.cpp new file mode 100644 index 0000000000..f2df6d47c9 --- /dev/null +++ b/scene/animation/tween.cpp @@ -0,0 +1,1219 @@ +/*************************************************************************/ +/* 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); + + } else if (name=="playback/repeat") { + set_repeat(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(); + } 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") ); +} + +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("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); + + 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","node","key"),&Tween::reset ); + ObjectTypeDB::bind_method(_MD("reset_all"),&Tween::reset_all ); + ObjectTypeDB::bind_method(_MD("stop","node","key"),&Tween::stop ); + ObjectTypeDB::bind_method(_MD("stop_all"),&Tween::stop_all ); + ObjectTypeDB::bind_method(_MD("resume","node","key"),&Tween::resume ); + ObjectTypeDB::bind_method(_MD("resume_all"),&Tween::resume_all ); + ObjectTypeDB::bind_method(_MD("remove","node","key"),&Tween::remove ); + ObjectTypeDB::bind_method(_MD("remove_all"),&Tween::remove_all ); + ObjectTypeDB::bind_method(_MD("seek","time"),&Tween::seek ); + ObjectTypeDB::bind_method(_MD("tell"),&Tween::tell ); + ObjectTypeDB::bind_method(_MD("get_runtime"),&Tween::get_runtime ); + + ObjectTypeDB::bind_method(_MD("interpolate_property","node","property","initial_val","final_val","times_in_sec","trans_type","ease_type","delay"),&Tween::interpolate_property, DEFVAL(0) ); + ObjectTypeDB::bind_method(_MD("interpolate_method","node","method","initial_val","final_val","times_in_sec","trans_type","ease_type","delay"),&Tween::interpolate_method, DEFVAL(0) ); + ObjectTypeDB::bind_method(_MD("interpolate_callback","node","callback","times_in_sec","args"),&Tween::interpolate_callback, DEFVAL(Variant()) ); + ObjectTypeDB::bind_method(_MD("follow_property","node","property","initial_val","target","target_property","times_in_sec","trans_type","ease_type","delay"),&Tween::follow_property, DEFVAL(0) ); + ObjectTypeDB::bind_method(_MD("follow_method","node","method","initial_val","target","target_method","times_in_sec","trans_type","ease_type","delay"),&Tween::follow_method, DEFVAL(0) ); + ObjectTypeDB::bind_method(_MD("targeting_property","node","property","initial","initial_val","final_val","times_in_sec","trans_type","ease_type","delay"),&Tween::targeting_property, DEFVAL(0) ); + ObjectTypeDB::bind_method(_MD("targeting_method","node","method","initial","initial_method","final_val","times_in_sec","trans_type","ease_type","delay"),&Tween::targeting_method, DEFVAL(0) ); + + ADD_SIGNAL( MethodInfo("tween_start", PropertyInfo( Variant::OBJECT,"node"), PropertyInfo( Variant::STRING,"key")) ); + ADD_SIGNAL( MethodInfo("tween_step", PropertyInfo( Variant::OBJECT,"node"), PropertyInfo( Variant::STRING,"key"), PropertyInfo( Variant::REAL,"elapsed"), PropertyInfo( Variant::OBJECT,"value")) ); + ADD_SIGNAL( MethodInfo("tween_complete", PropertyInfo( Variant::OBJECT,"node"), PropertyInfo( Variant::STRING,"key")) ); + + ADD_PROPERTY( PropertyInfo( Variant::INT, "playback/process_mode", PROPERTY_HINT_ENUM, "Fixed,Idle"), _SCS("set_tween_process_mode"), _SCS("get_tween_process_mode")); + //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::_get_initial_val(InterpolateData& p_data) { + + switch(p_data.type) { + case INTER_PROPERTY: + case INTER_METHOD: + case FOLLOW_PROPERTY: + case FOLLOW_METHOD: + return p_data.initial_val; + + case TARGETING_PROPERTY: + case TARGETING_METHOD: { + + Node *node = get_node(p_data.target); + ERR_FAIL_COND_V(node == NULL,p_data.initial_val); + + static Variant initial_val; + if(p_data.type == TARGETING_PROPERTY) { + + bool valid = false; + initial_val = node->get(p_data.target_key, &valid); + ERR_FAIL_COND_V(!valid,p_data.initial_val); + } else { + + Variant::CallError error; + initial_val = node->call(p_data.target_key, NULL, 0, error); + ERR_FAIL_COND_V(error.error != Variant::CallError::CALL_OK,p_data.initial_val); + } + return initial_val; + } + break; + } + return p_data.delta_val; +} + +Variant& Tween::_get_delta_val(InterpolateData& p_data) { + + switch(p_data.type) { + case INTER_PROPERTY: + case INTER_METHOD: + return p_data.delta_val; + + case FOLLOW_PROPERTY: + case FOLLOW_METHOD: { + + Node *target = get_node(p_data.target); + ERR_FAIL_COND_V(target == NULL,p_data.initial_val); + + Variant final_val; + + if(p_data.type == FOLLOW_PROPERTY) { + + bool valid = false; + final_val = target->get(p_data.target_key, &valid); + ERR_FAIL_COND_V(!valid,p_data.initial_val); + } else { + + Variant::CallError error; + final_val = target->call(p_data.target_key, NULL, 0, error); + ERR_FAIL_COND_V(error.error != Variant::CallError::CALL_OK,p_data.initial_val); + } + + // convert INT to REAL is better for interpolaters + if(final_val.get_type() == Variant::INT) final_val = final_val.operator real_t(); + _calc_delta_val(p_data.initial_val, final_val, p_data.delta_val); + return p_data.delta_val; + } + break; + + case TARGETING_PROPERTY: + case TARGETING_METHOD: { + + Variant initial_val = _get_initial_val(p_data); + // convert INT to REAL is better for interpolaters + if(initial_val.get_type() == Variant::INT) initial_val = initial_val.operator real_t(); + + //_calc_delta_val(p_data.initial_val, p_data.final_val, p_data.delta_val); + _calc_delta_val(initial_val, p_data.final_val, p_data.delta_val); + return p_data.delta_val; + } + break; + } + return p_data.initial_val; +} + +Variant Tween::_run_equation(InterpolateData& p_data) { + + Variant& initial_val = _get_initial_val(p_data); + Variant& delta_val = _get_delta_val(p_data); + Variant result; + +#define APPLY_EQUATION(element)\ + r.element = _run_equation(p_data.trans_type, p_data.ease_type, p_data.elapsed - p_data.delay, 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 - p_data.delay, (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 - p_data.delay, (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) { + + Object *object = get_node(p_data.path); + ERR_FAIL_COND_V(object == NULL, false); + + switch(p_data.type) { + + case INTER_PROPERTY: + case FOLLOW_PROPERTY: + case TARGETING_PROPERTY: + { + bool valid = false; + object->set(p_data.key,value, &valid); + return valid; + } + + case INTER_METHOD: + case FOLLOW_METHOD: + case TARGETING_METHOD: + { + Variant::CallError error; + if (value.get_type() != Variant::NIL) { + Variant *arg[1] = { &value }; + object->call(p_data.key, (const Variant **) arg, 1, error); + } else { + object->call(p_data.key, NULL, 0, error); + } + + if(error.error == Variant::CallError::CALL_OK) + return true; + return false; + } + + case INTER_CALLBACK: + break; + }; + return true; +} + +void Tween::_tween_process(float p_delta) { + + if (speed_scale == 0) + return; + p_delta *= speed_scale; + + // if repeat and all interpolates was finished then reset all interpolates + if(repeat) { + bool all_finished = true; + + for(List<InterpolateData>::Element *E=interpolates.front();E;E=E->next()) { + + InterpolateData& data = E->get(); + + if(!data.finish) { + all_finished = false; + break; + } + } + + if(all_finished) + reset_all(); + } + + for(List<InterpolateData>::Element *E=interpolates.front();E;E=E->next()) { + + InterpolateData& data = E->get(); + if(!data.active || data.finish) + continue; + + Object *object = get_node(data.path); + if(object == NULL) + continue; + + bool prev_delaying = data.elapsed <= data.delay; + data.elapsed += p_delta; + if(data.elapsed < data.delay) + continue; + else if(prev_delaying) { + + emit_signal("tween_start",object,data.key); + _apply_tween_value(data, data.initial_val); + } + + if(data.elapsed > (data.delay + data.times_in_sec)) { + + data.elapsed = data.delay + data.times_in_sec; + data.finish = true; + } + + switch(data.type) + { + case INTER_PROPERTY: + case INTER_METHOD: + break; + case INTER_CALLBACK: + if(data.finish) { + + Variant::CallError error; + if (data.arg.get_type() != Variant::NIL) { + Variant *arg[1] = { &data.arg }; + object->call(data.key, (const Variant **) arg, 1, error); + } else { + object->call(data.key, NULL, 0, error); + } + } + continue; + } + + Variant result = _run_equation(data); + emit_signal("tween_step",object,data.key,data.elapsed,result); + + _apply_tween_value(data, result); + + if(data.finish) + emit_signal("tween_complete",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); +} + +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; +} + +float Tween::get_speed() const { + + return speed_scale; +} + +bool Tween::start() { + + set_active(true); + _set_process(true); + return true; +} + +bool Tween::reset(Node *p_node, String p_key) { + + for(List<InterpolateData>::Element *E=interpolates.front();E;E=E->next()) { + + InterpolateData& data = E->get(); + Node *node = get_node(data.path); + if(node == NULL) + continue; + + if(node == p_node && data.key == p_key) { + + data.elapsed = 0; + data.finish = false; + if(data.delay == 0) + _apply_tween_value(data, data.initial_val); + } + } + return true; +} + +bool Tween::reset_all() { + + for(List<InterpolateData>::Element *E=interpolates.front();E;E=E->next()) { + + InterpolateData& data = E->get(); + data.elapsed = 0; + data.finish = false; + if(data.delay == 0) + _apply_tween_value(data, data.initial_val); + } + return true; +} + +bool Tween::stop(Node *p_node, String p_key) { + + for(List<InterpolateData>::Element *E=interpolates.front();E;E=E->next()) { + + InterpolateData& data = E->get(); + Node *node = get_node(data.path); + if(node == NULL) + continue; + if(node == p_node && data.key == p_key) + data.active = false; + } + return true; +} + +bool Tween::stop_all() { + + set_active(false); + _set_process(false); + + for(List<InterpolateData>::Element *E=interpolates.front();E;E=E->next()) { + + InterpolateData& data = E->get(); + data.active = false; + } + return true; +} + +bool Tween::resume(Node *p_node, String p_key) { + + set_active(true); + _set_process(true); + + for(List<InterpolateData>::Element *E=interpolates.front();E;E=E->next()) { + + InterpolateData& data = E->get(); + Node *node = get_node(data.path); + if(node == NULL) + continue; + if(node == p_node && data.key == p_key) + data.active = true; + } + return true; +} + +bool Tween::resume_all() { + + set_active(true); + _set_process(true); + + for(List<InterpolateData>::Element *E=interpolates.front();E;E=E->next()) { + + InterpolateData& data = E->get(); + data.active = true; + } + return true; +} + +bool Tween::remove(Node *p_node, String p_key) { + + for(List<InterpolateData>::Element *E=interpolates.front();E;E=E->next()) { + + InterpolateData& data = E->get(); + Node *node = get_node(data.path); + if(node == NULL) + continue; + if(node == p_node && data.key == p_key) { + interpolates.erase(E); + return true; + } + } + return true; +} + +bool Tween::remove_all() { + + set_active(false); + _set_process(false); + interpolates.clear(); + 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.delay) { + + data.finish = false; + continue; + } + else if(data.elapsed >= (data.delay + data.times_in_sec)) { + + data.finish = true; + data.elapsed = (data.delay + data.times_in_sec); + } else + data.finish = false; + + switch(data.type) + { + case INTER_PROPERTY: + case INTER_METHOD: + break; + case INTER_CALLBACK: + continue; + } + + Variant result = _run_equation(data); + + _apply_tween_value(data, result); + } + return true; +} + +real_t Tween::tell() const { + + real_t pos = 0; + for(const List<InterpolateData>::Element *E=interpolates.front();E;E=E->next()) { + + const InterpolateData& data = E->get(); + if(data.elapsed > pos) + pos = data.elapsed; + } + return pos; +} + +real_t Tween::get_runtime() const { + + real_t runtime = 0; + for(const List<InterpolateData>::Element *E=interpolates.front();E;E=E->next()) { + + const InterpolateData& data = E->get(); + real_t t = data.delay + data.times_in_sec; + if(t > runtime) + runtime = t; + } + return runtime; +} + +bool Tween::_calc_delta_val(const Variant& p_initial_val, const Variant& p_final_val, Variant& p_delta_val) { + + const Variant& initial_val = p_initial_val; + const Variant& final_val = p_final_val; + Variant& delta_val = p_delta_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(Node *p_node + , 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 + , real_t p_delay +) { + // convert INT to REAL is better for interpolaters + if(p_initial_val.get_type() == Variant::INT) p_initial_val = p_initial_val.operator real_t(); + if(p_final_val.get_type() == Variant::INT) p_final_val = p_final_val.operator real_t(); + + ERR_FAIL_COND_V(p_node == NULL, 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); + ERR_FAIL_COND_V(p_trans_type < 0 || p_trans_type >= TRANS_COUNT, false); + ERR_FAIL_COND_V(p_ease_type < 0 || p_ease_type >= EASE_COUNT, false); + ERR_FAIL_COND_V(p_delay < 0, false); + + bool prop_valid = false; + p_node->get(p_property,&prop_valid); + ERR_FAIL_COND_V(!prop_valid, false); + + InterpolateData data; + data.active = true; + data.type = INTER_PROPERTY; + data.finish = false; + data.elapsed = 0; + + data.path = p_node->get_path(); + 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; + data.delay = p_delay; + + if(!_calc_delta_val(data.initial_val, data.final_val, data.delta_val)) + return false; + + interpolates.push_back(data); + return true; +} + +bool Tween::interpolate_method(Node *p_node + , 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 + , real_t p_delay +) { + // convert INT to REAL is better for interpolaters + if(p_initial_val.get_type() == Variant::INT) p_initial_val = p_initial_val.operator real_t(); + if(p_final_val.get_type() == Variant::INT) p_final_val = p_final_val.operator real_t(); + + ERR_FAIL_COND_V(p_node == NULL, 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); + ERR_FAIL_COND_V(p_trans_type < 0 || p_trans_type >= TRANS_COUNT, false); + ERR_FAIL_COND_V(p_ease_type < 0 || p_ease_type >= EASE_COUNT, false); + ERR_FAIL_COND_V(p_delay < 0, false); + + ERR_FAIL_COND_V(!p_node->has_method(p_method), false); + + InterpolateData data; + data.active = true; + data.type = INTER_METHOD; + data.finish = false; + data.elapsed = 0; + + data.path = p_node->get_path(); + 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; + data.delay = p_delay; + + if(!_calc_delta_val(data.initial_val, data.final_val, data.delta_val)) + return false; + + interpolates.push_back(data); + return true; +} + +bool Tween::interpolate_callback(Node *p_node + , String p_callback + , real_t p_times_in_sec + , Variant p_arg +) { + + ERR_FAIL_COND_V(p_node == NULL, false); + ERR_FAIL_COND_V(p_times_in_sec < 0, false); + + ERR_FAIL_COND_V(!p_node->has_method(p_callback), false); + + InterpolateData data; + data.active = true; + data.type = INTER_CALLBACK; + data.finish = false; + data.elapsed = 0; + + data.path = p_node->get_path(); + data.key = p_callback; + data.times_in_sec = p_times_in_sec; + data.delay = 0; + data.arg = p_arg; + + interpolates.push_back(data); + return true; +} + +bool Tween::follow_property(Node *p_node + , String p_property + , Variant p_initial_val + , Node *p_target + , String p_target_property + , real_t p_times_in_sec + , TransitionType p_trans_type + , EaseType p_ease_type + , real_t p_delay +) { + // convert INT to REAL is better for interpolaters + if(p_initial_val.get_type() == Variant::INT) p_initial_val = p_initial_val.operator real_t(); + + ERR_FAIL_COND_V(p_node == NULL, false); + ERR_FAIL_COND_V(p_target == NULL, false); + ERR_FAIL_COND_V(p_times_in_sec <= 0, false); + ERR_FAIL_COND_V(p_trans_type < 0 || p_trans_type >= TRANS_COUNT, false); + ERR_FAIL_COND_V(p_ease_type < 0 || p_ease_type >= EASE_COUNT, false); + ERR_FAIL_COND_V(p_delay < 0, false); + + bool prop_valid = false; + p_node->get(p_property,&prop_valid); + ERR_FAIL_COND_V(!prop_valid, false); + + bool target_prop_valid = false; + Variant target_val = p_target->get(p_target_property,&target_prop_valid); + ERR_FAIL_COND_V(!target_prop_valid, false); + + // convert INT to REAL is better for interpolaters + if(target_val.get_type() == Variant::INT) target_val = target_val.operator real_t(); + ERR_FAIL_COND_V(target_val.get_type() != p_initial_val.get_type(), false); + + InterpolateData data; + data.active = true; + data.type = FOLLOW_PROPERTY; + data.finish = false; + data.elapsed = 0; + + data.path = p_node->get_path(); + data.key = p_property; + data.initial_val = p_initial_val; + data.target = p_target->get_path(); + data.target_key = p_target_property; + data.times_in_sec = p_times_in_sec; + data.trans_type = p_trans_type; + data.ease_type = p_ease_type; + data.delay = p_delay; + + interpolates.push_back(data); + return true; +} + +bool Tween::follow_method(Node *p_node + , String p_method + , Variant p_initial_val + , Node *p_target + , String p_target_method + , real_t p_times_in_sec + , TransitionType p_trans_type + , EaseType p_ease_type + , real_t p_delay +) { + // convert INT to REAL is better for interpolaters + if(p_initial_val.get_type() == Variant::INT) p_initial_val = p_initial_val.operator real_t(); + + ERR_FAIL_COND_V(p_node == NULL, false); + ERR_FAIL_COND_V(p_target == NULL, false); + ERR_FAIL_COND_V(p_times_in_sec <= 0, false); + ERR_FAIL_COND_V(p_trans_type < 0 || p_trans_type >= TRANS_COUNT, false); + ERR_FAIL_COND_V(p_ease_type < 0 || p_ease_type >= EASE_COUNT, false); + ERR_FAIL_COND_V(p_delay < 0, false); + + ERR_FAIL_COND_V(!p_node->has_method(p_method), false); + ERR_FAIL_COND_V(!p_target->has_method(p_target_method), false); + + Variant::CallError error; + Variant target_val = p_target->call(p_target_method, NULL, 0, error); + ERR_FAIL_COND_V(error.error != Variant::CallError::CALL_OK, false); + + // convert INT to REAL is better for interpolaters + if(target_val.get_type() == Variant::INT) target_val = target_val.operator real_t(); + ERR_FAIL_COND_V(target_val.get_type() != p_initial_val.get_type(), false); + + InterpolateData data; + data.active = true; + data.type = FOLLOW_METHOD; + data.finish = false; + data.elapsed = 0; + + data.path = p_node->get_path(); + data.key = p_method; + data.initial_val = p_initial_val; + data.target = p_target->get_path(); + data.target_key = p_target_method; + data.times_in_sec = p_times_in_sec; + data.trans_type = p_trans_type; + data.ease_type = p_ease_type; + data.delay = p_delay; + + interpolates.push_back(data); + return true; +} + +bool Tween::targeting_property(Node *p_node + , String p_property + , Node *p_initial + , String p_initial_property + , Variant p_final_val + , real_t p_times_in_sec + , TransitionType p_trans_type + , EaseType p_ease_type + , real_t p_delay +) { + // convert INT to REAL is better for interpolaters + if(p_final_val.get_type() == Variant::INT) p_final_val = p_final_val.operator real_t(); + + ERR_FAIL_COND_V(p_node == NULL, false); + ERR_FAIL_COND_V(p_initial == NULL, false); + ERR_FAIL_COND_V(p_times_in_sec <= 0, false); + ERR_FAIL_COND_V(p_trans_type < 0 || p_trans_type >= TRANS_COUNT, false); + ERR_FAIL_COND_V(p_ease_type < 0 || p_ease_type >= EASE_COUNT, false); + ERR_FAIL_COND_V(p_delay < 0, false); + + bool prop_valid = false; + p_node->get(p_property,&prop_valid); + ERR_FAIL_COND_V(!prop_valid, false); + + bool initial_prop_valid = false; + Variant initial_val = p_initial->get(p_initial_property,&initial_prop_valid); + ERR_FAIL_COND_V(!initial_prop_valid, false); + + // convert INT to REAL is better for interpolaters + if(initial_val.get_type() == Variant::INT) initial_val = initial_val.operator real_t(); + ERR_FAIL_COND_V(initial_val.get_type() != p_final_val.get_type(), false); + + InterpolateData data; + data.active = true; + data.type = TARGETING_PROPERTY; + data.finish = false; + data.elapsed = 0; + + data.path = p_node->get_path(); + data.key = p_property; + data.target = p_initial->get_path(); + data.target_key = p_initial_property; + data.initial_val = 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; + data.delay = p_delay; + + if(!_calc_delta_val(data.initial_val, data.final_val, data.delta_val)) + return false; + + interpolates.push_back(data); + return true; +} + + +bool Tween::targeting_method(Node *p_node + , String p_method + , Node *p_initial + , String p_initial_method + , Variant p_final_val + , real_t p_times_in_sec + , TransitionType p_trans_type + , EaseType p_ease_type + , real_t p_delay +) { + // convert INT to REAL is better for interpolaters + if(p_final_val.get_type() == Variant::INT) p_final_val = p_final_val.operator real_t(); + + ERR_FAIL_COND_V(p_node == NULL, false); + ERR_FAIL_COND_V(p_initial == NULL, false); + ERR_FAIL_COND_V(p_times_in_sec <= 0, false); + ERR_FAIL_COND_V(p_trans_type < 0 || p_trans_type >= TRANS_COUNT, false); + ERR_FAIL_COND_V(p_ease_type < 0 || p_ease_type >= EASE_COUNT, false); + ERR_FAIL_COND_V(p_delay < 0, false); + + ERR_FAIL_COND_V(!p_node->has_method(p_method), false); + ERR_FAIL_COND_V(!p_initial->has_method(p_initial_method), false); + + Variant::CallError error; + Variant initial_val = p_initial->call(p_initial_method, NULL, 0, error); + ERR_FAIL_COND_V(error.error != Variant::CallError::CALL_OK, false); + + // convert INT to REAL is better for interpolaters + if(initial_val.get_type() == Variant::INT) initial_val = initial_val.operator real_t(); + ERR_FAIL_COND_V(initial_val.get_type() != p_final_val.get_type(), false); + + InterpolateData data; + data.active = true; + data.type = TARGETING_METHOD; + data.finish = false; + data.elapsed = 0; + + data.path = p_node->get_path(); + data.key = p_method; + data.target = p_initial->get_path(); + data.target_key = p_initial_method; + data.initial_val = 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; + data.delay = p_delay; + + if(!_calc_delta_val(data.initial_val, data.final_val, data.delta_val)) + return false; + + interpolates.push_back(data); + return true; +} + +Tween::Tween() { + + //String autoplay; + tween_process_mode=TWEEN_PROCESS_IDLE; + processing=false; + active=false; + repeat=false; + speed_scale=1; +} + +Tween::~Tween() { + +} diff --git a/scene/animation/tween.h b/scene/animation/tween.h new file mode 100644 index 0000000000..51d5fc9132 --- /dev/null +++ b/scene/animation/tween.h @@ -0,0 +1,239 @@ +/*************************************************************************/ +/* 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: + enum InterpolateType { + + INTER_PROPERTY, + INTER_METHOD, + FOLLOW_PROPERTY, + FOLLOW_METHOD, + TARGETING_PROPERTY, + TARGETING_METHOD, + INTER_CALLBACK, + }; + + struct InterpolateData { + bool active; + InterpolateType type; + bool finish; + real_t elapsed; + NodePath path; + StringName key; + Variant initial_val; + Variant delta_val; + Variant final_val; + NodePath target; + StringName target_key; + real_t times_in_sec; + TransitionType trans_type; + EaseType ease_type; + real_t delay; + Variant arg; + }; + + String autoplay; + TweenProcessMode tween_process_mode; + bool processing; + bool active; + bool repeat; + float speed_scale; + + List<InterpolateData> 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& _get_delta_val(InterpolateData& p_data); + Variant& _get_initial_val(InterpolateData& p_data); + Variant _run_equation(InterpolateData& p_data); + bool _calc_delta_val(const Variant& p_initial_val, const Variant& p_final_val, Variant& p_delta_val); + 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<PropertyInfo> *p_list) const; + void _notification(int p_what); + + static void _bind_methods(); + +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; + + void set_speed(float p_speed); + float get_speed() const; + + bool start(); + bool reset(Node *p_node, String p_key); + bool reset_all(); + bool stop(Node *p_node, String p_key); + bool stop_all(); + bool resume(Node *p_node, String p_key); + bool resume_all(); + bool remove(Node *p_node, String p_key); + bool remove_all(); + + bool seek(real_t p_time); + real_t tell() const; + real_t get_runtime() const; + + bool interpolate_property(Node *p_node + , 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 + , real_t p_delay = 0 + ); + + bool interpolate_method(Node *p_node + , 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 + , real_t p_delay = 0 + ); + + bool interpolate_callback(Node *p_node + , String p_callback + , real_t p_times_in_sec + , Variant p_arg = Variant() + ); + + bool follow_property(Node *p_node + , String p_property + , Variant p_initial_val + , Node *p_target + , String p_target_property + , real_t p_times_in_sec + , TransitionType p_trans_type + , EaseType p_ease_type + , real_t p_delay = 0 + ); + + bool follow_method(Node *p_node + , String p_method + , Variant p_initial_val + , Node *p_target + , String p_target_method + , real_t p_times_in_sec + , TransitionType p_trans_type + , EaseType p_ease_type + , real_t p_delay = 0 + ); + + bool targeting_property(Node *p_node + , String p_property + , Node *p_initial + , String p_initial_property + , Variant p_final_val + , real_t p_times_in_sec + , TransitionType p_trans_type + , EaseType p_ease_type + , real_t p_delay = 0 + ); + + bool targeting_method(Node *p_node + , String p_method + , Node *p_initial + , String p_initial_method + , Variant p_final_val + , real_t p_times_in_sec + , TransitionType p_trans_type + , EaseType p_ease_type + , real_t p_delay = 0 + ); + + 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 0000000000..7d0f2cd4e0 --- /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 08e6ff2e54..2a1cca6a3a 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<Spatial>(); ObjectTypeDB::register_type<Skeleton>(); ObjectTypeDB::register_type<AnimationPlayer>(); + ObjectTypeDB::register_type<Tween>(); OS::get_singleton()->yield(); //may take time to init |