Abedron
3/4/2014 - 8:33 AM

Add button features to DisplayObject

Add button features to DisplayObject

/**
 * Created by cf232110 on 4.3.14.
 */
package utils {
import com.greensock.TweenMax;

import decko.sound.SoundMix;

import flash.display.DisplayObject;
import flash.events.MouseEvent;
import flash.utils.Dictionary;

public class ButtonInteract{
    public static var mute:Boolean;
    private static var dict:Dictionary = new Dictionary();

    public static function add(button:DisplayObject, over:DisplayObject = null, sound:Object = null):void {
        button.addEventListener(MouseEvent.MOUSE_OVER, overHandler);
        button.addEventListener(MouseEvent.MOUSE_OUT, outHandler);
        dict[button] = [over, sound];
    }

    public static function remove(button:DisplayObject):Boolean {
        var exist:Boolean;

        if(dict[button]){
            exist = true;
            button.removeEventListener(MouseEvent.MOUSE_OVER, overHandler);
            button.removeEventListener(MouseEvent.MOUSE_OUT, outHandler);
            delete dict[button];
        }

        return exist;
    }

    private static function overHandler(e:MouseEvent):void {
        var over:Object = dict[e.currentTarget][0];
        var sound:Object = dict[e.currentTarget][1];

        if(over){
            TweenMax.to(over, .2, {autoAlpha: 1});
        }

        if (sound) {
            if (!mute) {
                SoundMix.play(sound);
            }
        }
    }

    private static function outHandler(e:MouseEvent):void {
        var over:Object = dict[e.currentTarget][0];
        var sound:Object = dict[e.currentTarget][1];

        if(over){
            TweenMax.to(over, .2, {autoAlpha: 0});
        }

        if (sound) {
            if (!mute || SoundMix.isPlaying(sound)) {
                SoundMix.stop(sound);
            }
        }
    }
}
}