extends GdUnitTestSuite ## ButtonBounce: buttons grow under the mouse, shrink while held down, pop when asked to, ## and always around their middle. var _button: Button func before_test() -> void: _button = auto_free(Button.new()) _button.size = Vector2(40, 40) add_child(_button) ButtonBounce.add_to(_button) func _settle() -> void: await await_millis(int(maxf(ButtonBounce.DURATION, ButtonBounce.POP_DURATION) * 1000) + 80) func test_starts_at_its_normal_size_scaled_around_its_middle() -> void: assert_vector(_button.scale).is_equal(Vector2.ONE) assert_vector(_button.pivot_offset).is_equal(Vector2(20, 20)) func test_pivot_follows_the_size() -> void: _button.size = Vector2(60, 30) assert_vector(_button.pivot_offset).is_equal(Vector2(30, 15)) func test_grows_when_the_mouse_enters_and_shrinks_back_when_it_leaves() -> void: _button.mouse_entered.emit() await _settle() assert_vector(_button.scale).is_equal_approx(ButtonBounce.HOVER_SCALE, Vector2(0.01, 0.01)) _button.mouse_exited.emit() await _settle() assert_vector(_button.scale).is_equal_approx(Vector2.ONE, Vector2(0.01, 0.01)) func test_shrinks_while_held_down() -> void: _button.button_down.emit() await _settle() assert_vector(_button.scale).is_equal_approx(ButtonBounce.PRESSED_SCALE, Vector2(0.01, 0.01)) func test_goes_back_to_normal_when_let_go_away_from_the_mouse() -> void: _button.button_down.emit() await _settle() _button.button_up.emit() await _settle() assert_vector(_button.scale).is_equal_approx(Vector2.ONE, Vector2(0.01, 0.01)) func test_disabled_button_does_not_grow() -> void: _button.disabled = true _button.mouse_entered.emit() await _settle() assert_vector(_button.scale).is_equal(Vector2.ONE) func test_pop_starts_small_and_springs_back() -> void: ButtonBounce.pop(_button) assert_vector(_button.scale).is_equal(ButtonBounce.POP_SCALE) await _settle() assert_vector(_button.scale).is_equal_approx(Vector2.ONE, Vector2(0.01, 0.01)) func test_a_new_animation_stops_the_one_before() -> void: _button.mouse_entered.emit() _button.button_down.emit() await _settle() # Only the last one ran to the end assert_vector(_button.scale).is_equal_approx(ButtonBounce.PRESSED_SCALE, Vector2(0.01, 0.01))