Tutorials, extensions, and source files for ActionScript, Flash, and other Adobe products.

 

senocular.com ActionScript Library

ButtonEvent.as

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
import com.senocular.events.ButtonEventHandler;

/**
 * The ButtonEvent class defines event objects used by ButtonEventHandler.
 * ButtonEvent instances contain properties regarding movie clips handled by 
 * ButtonEventHandler and manages the events they receive. These instances
 * are created by ButtonEventHandler and returned from a call to
 * ButtonEventHandler.initialize.  It is also the object provided
 * to event handlers and provides information regarding the event and the
 * state of the movie clip receiving it. Other objects can be set to listen to 
 * events handled by ButtonEvent instances using addEventListener. 
 * @author Trevor McCauley
 * @link http://www.senocular.com
 * @version 0.9.7
 * @requires com.senocular.events.ButtonEventHandler
 * @usage myButtonEvent:ButtonEvent = ButtonEventHandler.initialize(myMovieClip:Object);
 * @example <pre class="code">
 * var my_mcEvent:ButtonEvent = ButtonEventHandler.initialize(my_mc);
 * my_mcEvent.onPress = function(event:ButtonEvent){
 * 	// my_mc.gotoAndStop("_down");
 * 	event.target.gotoAndStop("_down");
 * 	// optional; prevents onPress from being called for parents
 * 	event.stopPropagation();
 * }
 * </pre>
 * @see ButtonEventHandler class
 */
class com.senocular.events.ButtonEvent {
	/**
	 * A String spcifying the current event. This will be either:
	 * onPress, onRelease, onReleaseOutside, onRollOver, onRollOut, 
	 * onDragOver, onDragOut, or onMouseWithin.
	 * @usage event:String = myButtonEvent.type;
	 */
	public var type:String;
	/**
	 * The movie clip associated with this button event
	 * @usage movie:MovieClip = myButtonEvent.target;
	 * @see currentTarget
	 */
	public var target:Object;
	/**
	 * The first movie clip that initially encountered the current event.
	 * @usage movie:MovieClip = myButtonEvent.currentTarget;
	 * @see target
	 */
	public var currentTarget:Object;
	/**
	 * Specifies whether or not the mouse is within the current target.
	 * @usage movieMouseWithin:Boolean = myButtonEvent.mouseWithin;
	 */
	public var mouseWithin:Boolean = false;
	/**
	 * Specifies whether or not the mouse is down and the current
	 * target has been pressed.
	 * @usage moviePressed:Boolean = myButtonEvent.mouseWithin;
	 */
	public var pressed:Boolean = false;
	/**
	 * Determines whether or not the target will behave as a menu where
	 * it can receive events despite another movie clip having already been
	 * pressed (similar to Button and MovieClip trackAsMenu property).
	 * @usage myButtonEvent.trackAsMenu = value:Boolean;
	 */
	public var trackAsMenu:Boolean = false;
	/**
	 * Determines whether or not overlapping prevents events for objects in
	 * the same timeline (no parent/child relationships)  under the target clip
	 * for this ButtonEvent instance from triggering. If true,
	 * the behavior is like that of normal Flash buttons where buttons
	 * above other buttons, prevent the buttons below from receiving events.
	 * Note that this property only pertains when
	 * ButtonEventHandler.overlapBlocksEvents is false.
	 * @usage myButtonEvent.overlapBlocksEvents = value:Boolean
	 */
	public var overlapBlocksEvents:Boolean = false;

	// public for ButtonEventHandler
	public var depth:Number = 0;
	public var children:Array;
	
	// private
	private static var events:Array = ["onPress","onRelease","onReleaseOutside","onRollOver","onRollOut","onDragOver","onDragOut","onMouseWithin"];


	/**
	 * Used to prevent events from bubbling to parent movie clips. The event stopped
	 * is the current event as specified in this ButtonEvent's type property. This
	 * method would most commonly be used within an event handler for an event
	 * captured by ButtonEventHandler.
	 * @usage myButtonEvent.stopPropagation();
	 */
	public function stopPropagation():Void {
		ButtonEventHandler.stopPropagation(this.type);
	}
	
	// event handling
	/**
	 * Registers a listener object with this ButtonEvent instance that is
	 * broadcasting an event (from the target movie clip). 
	 * When the event occurs, the listener object or function is notified.
	 * @usage myButtonEvent.addEventListener(event:String, listener:Object);
	 * @param event A string that is the name of the event.
	 * @param listener A reference to a listener object or function to
	 * handle the event. 
	 * @return nothing.
	 */
	public function addEventListener(event:String, listener:Object):Void {}
	/**
	 * Removes a listener from receiving events from this ButtonEvent instance.
	 * @usage myButtonEvent.removeEventListener(event:String, listener:Object);
	 * @param event A string that is the name of the event.
	 * @param listener A reference to a listener object or function to
	 * handle the event. 
	 * @return nothing.
	 */
	public function removeEventListener(event:String, listener:Object):Void {}
	
	/**
	 * Define your own handleEvent handler to manage events
	 * sent to the target movie clip. The handleEvent handler can
	 * be defined to a ButtonEvent object (received from calling
	 * ButtonEventHandler.getEventObject(target)) or to the target
	 * movie clip. It will be called every time an event is triggered.
	 * @usage myButtonEvent.handleEvent = function(event:ButtonEvent){ ... };
	 * @param eventObject The current ButtonEvent event object.
	 * @return nothing.
	 */
	public function handleEvent(eventObject:ButtonEvent):Void {}
	private function dispatchEvent(event:ButtonEvent):Void {}

	// EVENTS
	/**
	 * Triggered when the mouse is pressed over a target movie clip.
	 * @usage myButtonEvent.onPress = function(event:ButtonEvent){ ... };
	 * @param event The ButtonEvent object associated with the current target movie clip
	 */
	public function onPress(event:ButtonEvent):Void {}
	/**
	 * Triggered when the mouse is released over a target movie clip that was pressed.
	 * @usage myButtonEvent.onRelease = function(event:ButtonEvent){ ... };
	 * @param event The ButtonEvent object associated with the current target movie clip
	 */
	public function onRelease(event:ButtonEvent):Void {}
	/**
	 * Triggered when the mouse is released over an area not above the target movie clip that was pressed.
	 * @usage myButtonEvent.onRelease = function(event:ButtonEvent){ ... };
	 * @param event The ButtonEvent object associated with the current target movie clip
	 */
	public function onReleaseOutside(event:ButtonEvent):Void {}
	/**
	 * Triggered when the mouse is moves over a target movie clip.
	 * @usage myButtonEvent.onRollOver = function(event:ButtonEvent){ ... };
	 * @param event The ButtonEvent object associated with the current target movie clip
	 */
	public function onRollOver(event:ButtonEvent):Void {}
	/**
	 * Triggered when the mouse is moves off a target movie clip.
	 * @usage myButtonEvent.onRollOut = function(event:ButtonEvent){ ... };
	 * @param event The ButtonEvent object associated with the current target movie clip
	 */
	public function onRollOut(event:ButtonEvent):Void {}
	/**
	 * Triggered when the mouse is moves over a target movie clip that was pressed.
	 * @usage myButtonEvent.onDragOver = function(event:ButtonEvent){ ... };
	 * @param event The ButtonEvent object associated with the current target movie clip
	 */
	public function onDragOver(event:ButtonEvent):Void {}
	/**
	 * Triggered when the mouse is moves off a target movie clip that was pressed.
	 * @usage myButtonEvent.onDragOut = function(event:ButtonEvent){ ... };
	 * @param event The ButtonEvent object associated with the current target movie clip
	 */
	public function onDragOut(event:ButtonEvent):Void {}
	/**
	 * Triggered every frame when the mouse is over a target movie clip.
	 * @usage myButtonEvent.onMouseWithin = function(event:ButtonEvent){ ... };
	 * @param event The ButtonEvent object associated with the current target movie clip
	 */
	public function onMouseWithin(event:ButtonEvent):Void {}


	// public for ButtonEventHandler
	function ButtonEvent(target:Object){
		mx.events.EventDispatcher.initialize(this);
		this.target = target;
		this.children = new Array();
		this.updateDepth();
		for (var event:String in events){
			this.addEventListener(events[event], this);
		}
	}
	public function sendEvent(event:String, currentTarget:Object):Void {
		this.type = event;
		this.currentTarget = currentTarget;
		this.dispatchEvent(this);
	}
	public function updateDepth(Void):Void {
		this.depth = target.getDepth();
	}
	public function mouseHitTest(Void):Boolean {
		if (target.hitArea) return target.hitArea.hitTest(_root._xmouse, _root._ymouse, true);
		return target.hitTest(_root._xmouse, _root._ymouse, true);
	}
	
	// general
	public function toString(Void):String {
		return "ButtonEvent: "+this.target;
	}
	
}