libraries/EaselJS_docs/files/easeljs_ui_ButtonHelper.js.html (view raw)
1<!DOCTYPE html>
2<html lang="en">
3<head>
4 <meta charset="utf-8">
5 <link rel="stylesheet" href="../assets/css/yui-cssgrids-min.css">
6 <link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
7 <link rel="stylesheet" href="../assets/css/main.css" id="site_styles">
8 <link rel="shortcut icon" type="image/x-icon" href="../assets/favicon.ico">
9 <script src="../assets/js/yui-source.js"></script>
10 <title>EaselJS v1.0.0 API Documentation : easeljs/ui/ButtonHelper.js</title>
11</head>
12<body class="yui3-skin-sam">
13
14<div id="doc">
15 <div id="hd" class="yui3-g header">
16 <div class="yui3-u-3-4">
17
18 <h1><a href="http://www.createjs.com/easeljs"><img src="../assets/docs-icon-EaselJS.png" title="EaselJS"></a></h1>
19
20 </div>
21 <div class="version">
22 <em>API Documentation for: 1.0.0</em>
23 </div>
24 </div>
25 <div id="bd" class="yui3-g">
26
27 <div class="yui3-u-1-4">
28 <div id="docs-sidebar" class="sidebar apidocs">
29 <div id="api-list">
30 <h2 class="off-left">APIs</h2>
31 <div id="api-tabview" class="tabview">
32 <ul class="tabs">
33 <li><a href="#api-classes">Classes</a></li>
34 </ul>
35
36 <div id="api-tabview-filter">
37 <input type="search" id="api-filter" placeholder="Type to filter APIs">
38 </div>
39
40 <div id="api-tabview-panel">
41 <ul id="api-classes" class="apis classes">
42
43 <li><a href="../classes/AlphaMapFilter.html">AlphaMapFilter</a></li>
44
45 <li><a href="../classes/AlphaMaskFilter.html">AlphaMaskFilter</a></li>
46
47 <li><a href="../classes/Bitmap.html">Bitmap</a></li>
48
49 <li><a href="../classes/BitmapCache.html">BitmapCache</a></li>
50
51 <li><a href="../classes/BitmapText.html">BitmapText</a></li>
52
53 <li><a href="../classes/BlurFilter.html">BlurFilter</a></li>
54
55 <li><a href="../classes/ButtonHelper.html">ButtonHelper</a></li>
56
57 <li><a href="../classes/ColorFilter.html">ColorFilter</a></li>
58
59 <li><a href="../classes/ColorMatrix.html">ColorMatrix</a></li>
60
61 <li><a href="../classes/ColorMatrixFilter.html">ColorMatrixFilter</a></li>
62
63 <li><a href="../classes/Container.html">Container</a></li>
64
65 <li><a href="../classes/DisplayObject.html">DisplayObject</a></li>
66
67 <li><a href="../classes/DisplayProps.html">DisplayProps</a></li>
68
69 <li><a href="../classes/DOMElement.html">DOMElement</a></li>
70
71 <li><a href="../classes/EaselJS.html">EaselJS</a></li>
72
73 <li><a href="../classes/Event.html">Event</a></li>
74
75 <li><a href="../classes/EventDispatcher.html">EventDispatcher</a></li>
76
77 <li><a href="../classes/Filter.html">Filter</a></li>
78
79 <li><a href="../classes/Graphics.html">Graphics</a></li>
80
81 <li><a href="../classes/Graphics.Arc.html">Graphics.Arc</a></li>
82
83 <li><a href="../classes/Graphics.ArcTo.html">Graphics.ArcTo</a></li>
84
85 <li><a href="../classes/Graphics.BeginPath.html">Graphics.BeginPath</a></li>
86
87 <li><a href="../classes/Graphics.BezierCurveTo.html">Graphics.BezierCurveTo</a></li>
88
89 <li><a href="../classes/Graphics.Circle.html">Graphics.Circle</a></li>
90
91 <li><a href="../classes/Graphics.ClosePath.html">Graphics.ClosePath</a></li>
92
93 <li><a href="../classes/Graphics.Ellipse.html">Graphics.Ellipse</a></li>
94
95 <li><a href="../classes/Graphics.Fill.html">Graphics.Fill</a></li>
96
97 <li><a href="../classes/Graphics.LineTo.html">Graphics.LineTo</a></li>
98
99 <li><a href="../classes/Graphics.MoveTo.html">Graphics.MoveTo</a></li>
100
101 <li><a href="../classes/Graphics.PolyStar.html">Graphics.PolyStar</a></li>
102
103 <li><a href="../classes/Graphics.QuadraticCurveTo.html">Graphics.QuadraticCurveTo</a></li>
104
105 <li><a href="../classes/Graphics.Rect.html">Graphics.Rect</a></li>
106
107 <li><a href="../classes/Graphics.RoundRect.html">Graphics.RoundRect</a></li>
108
109 <li><a href="../classes/Graphics.Stroke.html">Graphics.Stroke</a></li>
110
111 <li><a href="../classes/Graphics.StrokeDash.html">Graphics.StrokeDash</a></li>
112
113 <li><a href="../classes/Graphics.StrokeStyle.html">Graphics.StrokeStyle</a></li>
114
115 <li><a href="../classes/Matrix2D.html">Matrix2D</a></li>
116
117 <li><a href="../classes/MouseEvent.html">MouseEvent</a></li>
118
119 <li><a href="../classes/MovieClip.html">MovieClip</a></li>
120
121 <li><a href="../classes/MovieClipPlugin.html">MovieClipPlugin</a></li>
122
123 <li><a href="../classes/Point.html">Point</a></li>
124
125 <li><a href="../classes/Rectangle.html">Rectangle</a></li>
126
127 <li><a href="../classes/Shadow.html">Shadow</a></li>
128
129 <li><a href="../classes/Shape.html">Shape</a></li>
130
131 <li><a href="../classes/Sprite.html">Sprite</a></li>
132
133 <li><a href="../classes/SpriteSheet.html">SpriteSheet</a></li>
134
135 <li><a href="../classes/SpriteSheetBuilder.html">SpriteSheetBuilder</a></li>
136
137 <li><a href="../classes/SpriteSheetUtils.html">SpriteSheetUtils</a></li>
138
139 <li><a href="../classes/Stage.html">Stage</a></li>
140
141 <li><a href="../classes/StageGL.html">StageGL</a></li>
142
143 <li><a href="../classes/Text.html">Text</a></li>
144
145 <li><a href="../classes/Ticker.html">Ticker</a></li>
146
147 <li><a href="../classes/Touch.html">Touch</a></li>
148
149 <li><a href="../classes/UID.html">UID</a></li>
150
151 <li><a href="../classes/Utility Methods.html">Utility Methods</a></li>
152
153 <li><a href="../classes/VideoBuffer.html">VideoBuffer</a></li>
154
155 <li><a href="../classes/WebGLInspector.html">WebGLInspector</a></li>
156
157 </ul>
158
159 <ul id="api-modules" class="apis modules">
160
161 <li><a href="../modules/CreateJS.html">CreateJS</a></li>
162
163 <li><a href="../modules/EaselJS.html">EaselJS</a></li>
164
165 </ul>
166 </div>
167 </div>
168</div>
169
170 </div>
171 </div>
172 <div class="yui3-u-3-4">
173 <div id="api-options">
174 Show:
175 <label for="api-show-inherited">
176 <input type="checkbox" id="api-show-inherited" checked>
177 Inherited
178 </label>
179
180 <label for="api-show-protected">
181 <input type="checkbox" id="api-show-protected">
182 Protected
183 </label>
184
185 <label for="api-show-private">
186 <input type="checkbox" id="api-show-private">
187 Private
188 </label>
189 <label for="api-show-deprecated">
190 <input type="checkbox" id="api-show-deprecated">
191 Deprecated
192 </label>
193
194 </div>
195 <div class="apidocs">
196 <div id="docs-main">
197 <div class="content source-content">
198 <script>
199 var name = easeljs/ui/ButtonHelper.js
200</script>
201<h1 class="file-heading">File:ButtonHelper.js</h1>
202
203<div class="file">
204 <pre class="code prettyprint linenums">
205/*
206* ButtonHelper
207* Visit http://createjs.com/ for documentation, updates and examples.
208*
209* Copyright (c) 2010 gskinner.com, inc.
210*
211* Permission is hereby granted, free of charge, to any person
212* obtaining a copy of this software and associated documentation
213* files (the "Software"), to deal in the Software without
214* restriction, including without limitation the rights to use,
215* copy, modify, merge, publish, distribute, sublicense, and/or sell
216* copies of the Software, and to permit persons to whom the
217* Software is furnished to do so, subject to the following
218* conditions:
219*
220* The above copyright notice and this permission notice shall be
221* included in all copies or substantial portions of the Software.
222*
223* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
224* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
225* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
226* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
227* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
228* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
229* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
230* OTHER DEALINGS IN THE SOFTWARE.
231*/
232
233/**
234 * @module EaselJS
235 */
236
237// namespace:
238this.createjs = this.createjs||{};
239
240(function() {
241 "use strict";
242
243
244// constructor:
245 /**
246 * The ButtonHelper is a helper class to create interactive buttons from {{#crossLink "MovieClip"}}{{/crossLink}} or
247 * {{#crossLink "Sprite"}}{{/crossLink}} instances. This class will intercept mouse events from an object, and
248 * automatically call {{#crossLink "Sprite/gotoAndStop"}}{{/crossLink}} or {{#crossLink "Sprite/gotoAndPlay"}}{{/crossLink}},
249 * to the respective animation labels, add a pointer cursor, and allows the user to define a hit state frame.
250 *
251 * The ButtonHelper instance does not need to be added to the stage, but a reference should be maintained to prevent
252 * garbage collection.
253 *
254 * Note that over states will not work unless you call {{#crossLink "Stage/enableMouseOver"}}{{/crossLink}}.
255 *
256 * <h4>Example</h4>
257 *
258 * var helper = new createjs.ButtonHelper(myInstance, "out", "over", "down", false, myInstance, "hit");
259 * myInstance.addEventListener("click", handleClick);
260 * function handleClick(event) {
261 * // Click Happened.
262 * }
263 *
264 * @class ButtonHelper
265 * @param {Sprite|MovieClip} target The instance to manage.
266 * @param {String} [outLabel="out"] The label or animation to go to when the user rolls out of the button.
267 * @param {String} [overLabel="over"] The label or animation to go to when the user rolls over the button.
268 * @param {String} [downLabel="down"] The label or animation to go to when the user presses the button.
269 * @param {Boolean} [play=false] If the helper should call "gotoAndPlay" or "gotoAndStop" on the button when changing
270 * states.
271 * @param {DisplayObject} [hitArea] An optional item to use as the hit state for the button. If this is not defined,
272 * then the button's visible states will be used instead. Note that the same instance as the "target" argument can be
273 * used for the hitState.
274 * @param {String} [hitLabel] The label or animation on the hitArea instance that defines the hitArea bounds. If this is
275 * null, then the default state of the hitArea will be used. *
276 * @constructor
277 */
278 function ButtonHelper(target, outLabel, overLabel, downLabel, play, hitArea, hitLabel) {
279 if (!target.addEventListener) { return; }
280
281
282 // public properties:
283 /**
284 * The target for this button helper.
285 * @property target
286 * @type MovieClip | Sprite
287 * @readonly
288 **/
289 this.target = target;
290
291 /**
292 * The label name or frame number to display when the user mouses out of the target. Defaults to "over".
293 * @property overLabel
294 * @type String | Number
295 **/
296 this.overLabel = overLabel == null ? "over" : overLabel;
297
298 /**
299 * The label name or frame number to display when the user mouses over the target. Defaults to "out".
300 * @property outLabel
301 * @type String | Number
302 **/
303 this.outLabel = outLabel == null ? "out" : outLabel;
304
305 /**
306 * The label name or frame number to display when the user presses on the target. Defaults to "down".
307 * @property downLabel
308 * @type String | Number
309 **/
310 this.downLabel = downLabel == null ? "down" : downLabel;
311
312 /**
313 * If true, then ButtonHelper will call gotoAndPlay, if false, it will use gotoAndStop. Default is false.
314 * @property play
315 * @default false
316 * @type Boolean
317 **/
318 this.play = play;
319
320
321 // private properties
322 /**
323 * @property _isPressed
324 * @type Boolean
325 * @protected
326 **/
327 this._isPressed = false;
328
329 /**
330 * @property _isOver
331 * @type Boolean
332 * @protected
333 **/
334 this._isOver = false;
335
336 /**
337 * @property _enabled
338 * @type Boolean
339 * @protected
340 **/
341 this._enabled = false;
342
343 // setup:
344 target.mouseChildren = false; // prevents issues when children are removed from the display list when state changes.
345 this.enabled = true;
346 this.handleEvent({});
347 if (hitArea) {
348 if (hitLabel) {
349 hitArea.actionsEnabled = false;
350 hitArea.gotoAndStop&&hitArea.gotoAndStop(hitLabel);
351 }
352 target.hitArea = hitArea;
353 }
354 }
355 var p = ButtonHelper.prototype;
356
357// getter / setters:
358 /**
359 * Use the {{#crossLink "ButtonHelper/enabled:property"}}{{/crossLink}} property instead.
360 * @method setEnabled
361 * @param {Boolean} value The enabled property to set the instance to.
362 * @[rptected
363 * @protected
364 **/
365 p._setEnabled = function(value) {
366 if (value == this._enabled) { return; }
367 var o = this.target;
368 this._enabled = value;
369 if (value) {
370 o.cursor = "pointer";
371 o.addEventListener("rollover", this);
372 o.addEventListener("rollout", this);
373 o.addEventListener("mousedown", this);
374 o.addEventListener("pressup", this);
375 if (o._reset) { o.__reset = o._reset; o._reset = this._reset;}
376 } else {
377 o.cursor = null;
378 o.removeEventListener("rollover", this);
379 o.removeEventListener("rollout", this);
380 o.removeEventListener("mousedown", this);
381 o.removeEventListener("pressup", this);
382 if (o.__reset) { o._reset = o.__reset; delete(o.__reset); }
383 }
384 };
385 // ButtonHelper.setEnabled is @deprecated. Remove for 1.1+
386 p.setEnabled = createjs.deprecate(p._setEnabled, "ButtonHelper.setEnabled");
387
388 /**
389 * Use the {{#crossLink "ButtonHelper/enabled:property"}}{{/crossLink}} property instead.
390 * @method getEnabled
391 * @protected
392 * @return {Boolean}
393 **/
394 p._getEnabled = function() {
395 return this._enabled;
396 };
397 // ButtonHelper.getEnabled is @deprecated. Remove for 1.1+
398 p.getEnabled = createjs.deprecate(p._getEnabled, "ButtonHelper.getEnabled");
399
400 /**
401 * Enables or disables the button functionality on the target.
402 * @property enabled
403 * @type {Boolean}
404 **/
405 try {
406 Object.defineProperties(p, {
407 enabled: { get: p._getEnabled, set: p._setEnabled }
408 });
409 } catch (e) {} // TODO: use Log
410
411
412// public methods:
413 /**
414 * Returns a string representation of this object.
415 * @method toString
416 * @return {String} a string representation of the instance.
417 **/
418 p.toString = function() {
419 return "[ButtonHelper]";
420 };
421
422
423// private methods:
424 /**
425 * @method handleEvent
426 * @param {Object} evt The mouse event to handle.
427 * @protected
428 **/
429 p.handleEvent = function(evt) {
430 var label, t = this.target, type = evt.type;
431 if (type == "mousedown") {
432 this._isPressed = true;
433 label = this.downLabel;
434 } else if (type == "pressup") {
435 this._isPressed = false;
436 label = this._isOver ? this.overLabel : this.outLabel;
437 } else if (type == "rollover") {
438 this._isOver = true;
439 label = this._isPressed ? this.downLabel : this.overLabel;
440 } else { // rollout and default
441 this._isOver = false;
442 label = this._isPressed ? this.overLabel : this.outLabel;
443 }
444 if (this.play) {
445 t.gotoAndPlay&&t.gotoAndPlay(label);
446 } else {
447 t.gotoAndStop&&t.gotoAndStop(label);
448 }
449 };
450
451 /**
452 * Injected into target. Preserves the paused state through a reset.
453 * @method _reset
454 * @protected
455 **/
456 p._reset = function() {
457 // TODO: explore better ways to handle this issue. This is hacky & disrupts object signatures.
458 var p = this.paused;
459 this.__reset();
460 this.paused = p;
461 };
462
463
464 createjs.ButtonHelper = ButtonHelper;
465}());
466
467 </pre>
468</div>
469
470 </div>
471 </div>
472 </div>
473 </div>
474 </div>
475</div>
476<script src="../assets/vendor/prettify/prettify-min.js"></script>
477<script>prettyPrint();</script>
478<script src="../assets/js/yui-prettify.js"></script>
479<script src="../assets/../api.js"></script>
480<script src="../assets/js/api-filter.js"></script>
481<script src="../assets/js/api-list.js"></script>
482<script src="../assets/js/api-search.js"></script>
483<script src="../assets/js/apidocs.js"></script>
484</body>
485</html>