1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
|
package com.button.ui;
// Classes Macromedia
import flash.display.MovieClip;
import flash.geom.Point;
import flash.display.DisplayObject;
import flash.geom.Rectangle;
import flash.filters.BevelFilter;
import flash.filters.DropShadowFilter;
import flash.filters.ColorMatrixFilter;
import flash.events.MouseEvent;
/**
* Class Button : create a label with some most actions.
*
* Button Object diffuse some events RELEASE_BTN, ROLLOVER_BTN, ROLLOUT_BTN, RELEASEOUTSIDE_BTN to Listeners.
*
*/
class Button extends Label
{
/* options du bouton */
private var _sTxtToolTip : String; // Le texte de l'infos bulle
/* getter/setter */
public var enabled(null, setEnabled) : Bool;
/* events */
public static var RELEASE_BTN : String = "_onReleaseBtn";
public static var ROLLOVER_BTN : String = "_onRollOverBtn";
public static var ROLLOUT_BTN : String = "_onRollOutBtn";
public static var RELEASEOUTSIDE_BTN : String = "_onReleaseOutsideBtn";
/* state */
public static var STATE_PRESS : String = "press";
public static var STATE_ROLLOVER : String = "rollover";
public static var STATE_ROLLOUT : String = "rollout";
public static var STATE_NORMAL : String = "normal";
/**
* CONSTRUCTOR
*
*/
public function new(?x:Float, ?y:Float, ?pad:Int, ?txt:String,
?urlIcon:String, ?posIconeLeft:Bool, ?txtTooltip:String, ?oAbsForm:AbstractForm,
?w:Float, ?h:Float)
{
// création du Label
super(x, y, pad, txt, urlIcon, posIconeLeft, oAbsForm, w, h);
/* initialisation du Bouton */
_sTxtToolTip = txtTooltip;
}
/* ***************************************************************************
* PUBLIC FUNCTIONS
******************************************************************************/
public function draw (mc:MovieClip, ?depth : Int):Void
{
super.draw(mc, depth);
this.hide();
/* Action à effectuer pour chaque type d'évenements sur le bouton */
_mc.addEventListener(MouseEvent.CLICK, _onRelease);
_mc.addEventListener(MouseEvent.MOUSE_DOWN, _onReleaseOutside);
_mc.addEventListener(MouseEvent.MOUSE_OUT, _onRollOut);
_mc.addEventListener(MouseEvent.MOUSE_OVER, _onRollOver);
this._mc.buttonMode = true;
}
/**
* Update the ToolTip.
*/
public function setTooltip(msg:String)
{
this._sTxtToolTip = msg;
}
/**
* Apply property enabled to the button.
*
*/
private function setEnabled(bValue:Bool)
{
this._mc.enabled = bValue;
this._mc.buttonMode = bValue;
return bValue;
}
/**
* Chose the state of the button ( rollout, rollout...).
*
*/
public function setState( sFrameName:String)
{
this.goToFrame(sFrameName);
}
/* ***************************************************************************
* PRIVATE FUNCTIONS
******************************************************************************/
/**
* Appelé lorsqu'un utilisateur relache le bouton de la souris sur le bouton
* Ferme le tooltip
*
* @return
*/
private function _onRelease(?event:MouseEvent)
{
if(this._mc.enabled == false) return;
ToolTip.close();
this.goToFrame(STATE_PRESS);
this._oEventManager.broadcastEvent(RELEASE_BTN, this);
}
/**
* Les comportements au passage de la souris sont géré en interne.
* Crée un effet visuel sur le bouton.
*
* @return
*/
private function _onRollOver(?event:MouseEvent)
{
if(this._mc.enabled == false) return;
this.goToFrame(STATE_ROLLOVER);
// si il y a un texte à afficher dans le tooltip
if( _sTxtToolTip!=null )
{
// On récupère les coordonnées du bouton
// Puis on converti ces coordonnées dans le référentiel de la scène
var oCoord:Point = new Point(_nPadding, _nPadding);
var oCoord:Point = _mc.localToGlobal(oCoord);
var oRect:Rectangle = new Rectangle(oCoord.x, oCoord.y, this._nWidth-_nPadding, this._nHeight-_nPadding);
ToolTip.open(_sTxtToolTip, oRect);
}
this._oEventManager.broadcastEvent(ROLLOVER_BTN, this);
}
/**
* Supprime l'effet visuel lorsque la souris n'est plus au dessus du Button.
* Ferme le tooltip.
*
* @return
*/
private function _onRollOut(?event:MouseEvent)
{
if(this._mc.enabled == false) return;
ToolTip.close();
this.goToFrame(STATE_ROLLOUT);
this._oEventManager.broadcastEvent(ROLLOUT_BTN, this);
}
/**
* On reçoit ici l'évènement lors d'un click à l'exterieur pour remettre le bouton
* dans son état normal.
*
* @return
*/
private function _onReleaseOutside(?event:MouseEvent)
{
if(this._mc.enabled == false) return;
ToolTip.close();
this.goToFrame(STATE_NORMAL);
this._oEventManager.broadcastEvent(RELEASEOUTSIDE_BTN, this);
}
/**
* movieclip go to the wished frame.
*
*/
private function goToFrame(sName:String)
{
if(this._mc.enabled == false) return;
if( _oForm!=null)
_oForm.getChild().gotoAndStop(sName);
}
} |
Partager