main.gd 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. extends Control
  2. const trans_list = ["Linear", "Sine", "Quint", "Quart", "Quad", "Expo", "Elastic", "Cubic", "Circ", "Bounce", "Back"]
  3. const eases_list = ["In", "Out", "InOut", "OutIn"]
  4. const modes_list = ["Move", "Color", "Scale", "Rotate", "Callback", "Follow", "Repeat", "Pause"]
  5. var state = {
  6. trans = Tween.TRANS_LINEAR,
  7. eases = Tween.EASE_IN,
  8. }
  9. onready var trans = $Trans
  10. onready var eases = $Eases
  11. onready var modes = $Modes
  12. onready var tween = $Tween
  13. onready var timeline = $Timeline
  14. onready var color_from_picker = $Colors/ColorFrom/Picker
  15. onready var color_to_picker = $Colors/ColorTo/Picker
  16. onready var sprite = $Tween/Area/Sprite
  17. onready var follow = $Tween/Area/Follow
  18. onready var follow_2 = $Tween/Area/Follow2
  19. onready var size = $Tween/Area.get_size()
  20. func _ready():
  21. for index in range(trans_list.size()):
  22. trans.get_node(trans_list[index]).connect("pressed", self, "on_trans_changed", [trans_list[index], index])
  23. for index in range(eases_list.size()):
  24. eases.get_node(eases_list[index]).connect("pressed", self, "on_eases_changed", [eases_list[index], index])
  25. for index in range(modes_list.size()):
  26. modes.get_node(modes_list[index]).connect("pressed", self, "on_modes_changed", [modes_list[index]])
  27. color_from_picker.set_pick_color(Color.red)
  28. color_from_picker.connect("color_changed", self, "on_color_changed")
  29. color_to_picker.set_pick_color(Color.cyan)
  30. color_to_picker.connect("color_changed", self, "on_color_changed")
  31. $Trans/Linear.set_pressed(true)
  32. $Eases/In.set_pressed(true)
  33. $Modes/Move.set_pressed(true)
  34. $Modes/Repeat.set_pressed(true)
  35. reset_tween()
  36. func on_trans_changed(trans_name, index):
  37. for index in range(trans_list.size()):
  38. var pressed = trans_list[index] == trans_name
  39. var btn = trans.get_node(trans_list[index])
  40. btn.set_pressed(pressed)
  41. set_mouse_filter(Control.MOUSE_FILTER_IGNORE if pressed else Control.MOUSE_FILTER_PASS)
  42. state.trans = index
  43. reset_tween()
  44. func on_eases_changed(ease_name, index):
  45. for index in range(eases_list.size()):
  46. var pressed = eases_list[index] == ease_name
  47. var btn = eases.get_node(eases_list[index])
  48. btn.set_pressed(pressed)
  49. set_mouse_filter(Control.MOUSE_FILTER_IGNORE if pressed else Control.MOUSE_FILTER_PASS)
  50. state.eases = index
  51. reset_tween()
  52. func on_modes_changed(mode_name):
  53. if mode_name == "pause":
  54. if $Modes/Pause.is_pressed():
  55. tween.stop_all()
  56. timeline.set_mouse_filter(Control.MOUSE_FILTER_PASS)
  57. else:
  58. tween.resume_all()
  59. timeline.set_mouse_filter(Control.MOUSE_FILTER_IGNORE)
  60. else:
  61. reset_tween()
  62. func on_color_changed(_color):
  63. reset_tween()
  64. func reset_tween():
  65. var pos = tween.tell()
  66. tween.reset_all()
  67. tween.remove_all()
  68. if $Modes/Move.is_pressed():
  69. tween.interpolate_method(sprite, "set_position", Vector2(0, 0), Vector2(size.x, size.y), 2, state.trans, state.eases)
  70. tween.interpolate_property(sprite, "position", Vector2(size.x, size.y), Vector2(0, 0), 2, state.trans, state.eases, 2)
  71. if $Modes/Color.is_pressed():
  72. tween.interpolate_method(sprite, "set_modulate", color_from_picker.get_pick_color(), color_to_picker.get_pick_color(), 2, state.trans, state.eases)
  73. tween.interpolate_property(sprite, "modulate", color_to_picker.get_pick_color(), color_from_picker.get_pick_color(), 2, state.trans, state.eases, 2)
  74. else:
  75. sprite.set_modulate(Color.white)
  76. if $Modes/Scale.is_pressed():
  77. tween.interpolate_method(sprite, "set_scale", Vector2(0.5, 0.5), Vector2(1.5, 1.5), 2, state.trans, state.eases)
  78. tween.interpolate_property(sprite, "scale", Vector2(1.5, 1.5), Vector2(0.5, 0.5), 2, state.trans, state.eases, 2)
  79. else:
  80. sprite.set_scale(Vector2.ONE)
  81. if $Modes/Rotate.is_pressed():
  82. tween.interpolate_method(sprite, "set_rotation_degrees", 0, 360, 2, state.trans, state.eases)
  83. tween.interpolate_property(sprite, "rotation_degrees", 360, 0, 2, state.trans, state.eases, 2)
  84. if $Modes/Callback.is_pressed():
  85. tween.interpolate_callback(self, 0.5, "on_callback", "0.5 second's after")
  86. tween.interpolate_callback(self, 0.2, "on_callback", "1.2 second's after")
  87. if $Modes/Follow.is_pressed():
  88. follow.show()
  89. follow_2.show()
  90. tween.follow_method(follow, "set_position", Vector2(0, size.y), sprite, "get_position", 2, state.trans, state.eases)
  91. tween.targeting_method(follow, "set_position", sprite, "get_position", Vector2(0, size.y), 2, state.trans, state.eases, 2)
  92. tween.targeting_property(follow_2, "position", sprite, "position", Vector2(size.x, 0), 2, state.trans, state.eases)
  93. tween.follow_property(follow_2, "position", Vector2(size.x, 0), sprite, "position", 2, state.trans, state.eases, 2)
  94. else:
  95. follow.hide()
  96. follow_2.hide()
  97. tween.set_repeat($Modes/Repeat.is_pressed())
  98. tween.start()
  99. tween.seek(pos)
  100. if $Modes/Pause.is_pressed():
  101. tween.stop_all()
  102. #get_node("timeline").set_ignore_mouse(false)
  103. timeline.set_value(0)
  104. else:
  105. tween.resume_all()
  106. #get_node("timeline").set_ignore_mouse(true)
  107. func _on_tween_step(_object, _key, elapsed, _value):
  108. var runtime = tween.get_runtime()
  109. var ratio = 100 * (elapsed / runtime)
  110. timeline.set_value(ratio)
  111. func _on_timeline_value_changed(value):
  112. if !$Modes/Pause.is_pressed():
  113. return
  114. var runtime = tween.get_runtime()
  115. tween.seek(runtime * value / 100)
  116. func on_callback(arg):
  117. $Tween/Area/Label.add_text("on_callback -> " + arg + "\n")