<commit_before>﻿package com.haxepunk.tweens.sound;

import com.haxepunk.Sfx;
import com.haxepunk.Tween;
import com.haxepunk.utils.Ease;

/**
 * Sound effect fader.
 */
class SfxFader extends Tween
{
	/**
	 * Constructor.
	 * @param	sfx			The Sfx object to alter.
	 * @param	complete	Optional completion callback.
	 * @param	type		Tween type.
	 */
	public function new(sfx:Sfx, ?complete:Dynamic -> Void, ?type:TweenType)
	{
		super(0, type, complete);
		_sfx = sfx;
	}

	/**
	 * Fades the Sfx to the target volume.
	 * @param	volume		The volume to fade to.
	 * @param	duration	Duration of the fade.
	 * @param	ease		Optional easer function.
	 */
	public function fadeTo(volume:Float, duration:Float, ease:Float -> Float = null)
	{
		if (volume < 0) volume = 0;
		_start = _sfx.volume;
		_range = volume - _start;
		_target = duration;
		_ease = ease;
		start();
	}

	/**
	 * Fades out the Sfx, while also playing and fading in a replacement Sfx.
	 * @param	play		The Sfx to play and fade in.
	 * @param	loop		If the new Sfx should loop.
	 * @param	duration	Duration of the crossfade.
	 * @param	volume		The volume to fade in the new Sfx to.
	 * @param	ease		Optional easer function.
	 */
	public function crossFade(play:Sfx, loop:Bool, duration:Float, volume:Float = 1, ease:Float -> Float = null)
	{
		_crossSfx = play;
		_crossRange = volume;
		_start = _sfx.volume;
		_range = -_start;
		_target = duration;
		_ease = ease;
		if (loop) _crossSfx.loop(0);
		else _crossSfx.play(0);
		start();
	}

	/** @private Updates the Tween. */
	@:dox(hide)
	override public function update()
	{
		super.update();
		if (_sfx != null) _sfx.volume = _start + _range * _t;
		if (_crossSfx != null) _crossSfx.volume = _crossRange * _t;
	}

	/** @private When the tween completes. */
	override private function finish()
	{
		super.finish();
		if (_crossSfx != null)
		{
			if (_sfx != null) _sfx.stop();
			_sfx = _crossSfx;
			_crossSfx = null;
		}
		dispatchEvent(new TweenEvent(TweenEvent.FINISH));
	}

	/**
	 * The current Sfx this object is effecting.
	 */
	public var sfx(get, null):Sfx;
	private function get_sfx():Sfx { return _sfx; }

	// Fader information.
	private var _sfx:Sfx;
	private var _start:Float;
	private var _range:Float;
	private var _crossSfx:Sfx;
	private var _crossRange:Float;
	private var _complete:Dynamic -> Void;
}<commit_msg>Remove redundant dispatchEvent from SfxFader.finish.<commit_after>﻿package com.haxepunk.tweens.sound;

import com.haxepunk.Sfx;
import com.haxepunk.Tween;
import com.haxepunk.utils.Ease;

/**
 * Sound effect fader.
 */
class SfxFader extends Tween
{
	/**
	 * Constructor.
	 * @param	sfx			The Sfx object to alter.
	 * @param	complete	Optional completion callback.
	 * @param	type		Tween type.
	 */
	public function new(sfx:Sfx, ?complete:Dynamic -> Void, ?type:TweenType)
	{
		super(0, type, complete);
		_sfx = sfx;
	}

	/**
	 * Fades the Sfx to the target volume.
	 * @param	volume		The volume to fade to.
	 * @param	duration	Duration of the fade.
	 * @param	ease		Optional easer function.
	 */
	public function fadeTo(volume:Float, duration:Float, ease:Float -> Float = null)
	{
		if (volume < 0) volume = 0;
		_start = _sfx.volume;
		_range = volume - _start;
		_target = duration;
		_ease = ease;
		start();
	}

	/**
	 * Fades out the Sfx, while also playing and fading in a replacement Sfx.
	 * @param	play		The Sfx to play and fade in.
	 * @param	loop		If the new Sfx should loop.
	 * @param	duration	Duration of the crossfade.
	 * @param	volume		The volume to fade in the new Sfx to.
	 * @param	ease		Optional easer function.
	 */
	public function crossFade(play:Sfx, loop:Bool, duration:Float, volume:Float = 1, ease:Float -> Float = null)
	{
		_crossSfx = play;
		_crossRange = volume;
		_start = _sfx.volume;
		_range = -_start;
		_target = duration;
		_ease = ease;
		if (loop) _crossSfx.loop(0);
		else _crossSfx.play(0);
		start();
	}

	/** @private Updates the Tween. */
	@:dox(hide)
	override public function update()
	{
		super.update();
		if (_sfx != null) _sfx.volume = _start + _range * _t;
		if (_crossSfx != null) _crossSfx.volume = _crossRange * _t;
	}

	/** @private When the tween completes. */
	override private function finish()
	{
		super.finish();
		if (_crossSfx != null)
		{
			if (_sfx != null) _sfx.stop();
			_sfx = _crossSfx;
			_crossSfx = null;
		}
	}

	/**
	 * The current Sfx this object is effecting.
	 */
	public var sfx(get, null):Sfx;
	private function get_sfx():Sfx { return _sfx; }

	// Fader information.
	private var _sfx:Sfx;
	private var _start:Float;
	private var _range:Float;
	private var _crossSfx:Sfx;
	private var _crossRange:Float;
	private var _complete:Dynamic -> Void;
}
{'fraction_non_alphanumeric': 0.12160137015628344, 'fraction_numerical': 0.002569043031470777, 'mean_word_length': 3.325925925925926, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4316285', 'n_tokens_mistral': 1841, 'n_tokens_neox': 1708, 'n_words': 508}