1 /**
  2  * The MIT License (MIT)
  3  *
  4  * Copyright (c) 2016 DeNA Co., Ltd.
  5  *
  6  * Permission is hereby granted, free of charge, to any person obtaining a copy
  7  * of this software and associated documentation files (the "Software"), to deal
  8  * in the Software without restriction, including without limitation the rights
  9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 10  * copies of the Software, and to permit persons to whom the Software is
 11  * furnished to do so, subject to the following conditions:
 12  *
 13  * The above copyright notice and this permission notice shall be included in
 14  * all copies or substantial portions of the Software.
 15  *
 16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 22  * SOFTWARE.
 23  */
 24 
 25 /// <reference path="base.js"/>
 26 /// <reference path="object.js"/>
 27 /// <reference path="event.js"/>
 28 /// <reference path="sprite.js"/>
 29 /// <reference path="movie_clip.js"/>
 30 
 31 /**
 32  * A class that creates an interactive button from a createjs.MovieClip instance
 33  * or a createjs.Sprite instance.
 34  * @param {createjs.Sprite|createjs.MovieClip} target
 35  * @param {string|number=} opt_out
 36  * @param {string|number=} opt_over
 37  * @param {string|number=} opt_down
 38  * @param {boolean=} opt_play
 39  * @param {createjs.DisplayObject=} opt_area
 40  * @param {string=} opt_label
 41  * @extends {createjs.Object}
 42  * @implements {EventListener}
 43  * @constructor
 44  */
 45 createjs.ButtonHelper = function(target,
 46                                  opt_out,
 47                                  opt_over,
 48                                  opt_down,
 49                                  opt_play,
 50                                  opt_area,
 51                                  opt_label) {
 52   /// <signature>
 53   ///   <param type="createjs.Sprite" name="target"/>
 54   ///   <param type="string" optional="true" name="opt_out"/>
 55   ///   <param type="string" optional="true" name="opt_over"/>
 56   ///   <param type="string" optional="true" name="opt_down"/>
 57   ///   <param type="boolean" optional="true" name="opt_play"/>
 58   ///   <param type="createjs.DisplayObject" optional="true" name="opt_area"/>
 59   ///   <param type="string" optional="true" name="opt_label"/>
 60   /// </signature>
 61   /// <signature>
 62   ///   <param type="createjs.MovieClip" name="target"/>
 63   ///   <param type="string" optional="true" name="opt_out"/>
 64   ///   <param type="string" optional="true" name="opt_over"/>
 65   ///   <param type="string" optional="true" name="opt_down"/>
 66   ///   <param type="boolean" optional="true" name="opt_play"/>
 67   ///   <param type="createjs.DisplayObject" optional="true" name="opt_area"/>
 68   ///   <param type="string" optional="true" name="opt_label"/>
 69   /// </signature>
 70   createjs.Object.call(this);
 71 
 72   /**
 73    * The target for this button.
 74    * @type {createjs.MovieClip|createjs.Sprite}
 75    * @private
 76    */
 77   this.target_ = target;
 78 
 79   /**
 80    * The label name or frame number to jump when the target receives a 'pressup'
 81    * event.
 82    * @type {string|number}
 83    * @private
 84    */
 85   this.out_ = (opt_out == null) ? 'out' : opt_out;
 86 
 87   /**
 88    * The label name or frame number to jump when the target receives a
 89    * 'mousedown' event.
 90    * @type {string|number}
 91    * @private
 92    */
 93   this.down_ = (opt_down == null) ? 'down' : opt_down;
 94 
 95   /**
 96    * Whether this button is playing the target clip.
 97    * @type {boolean}
 98    * @private
 99    */
100   this.play_ = !!opt_play;
101 
102   // Listen mouse events for the target clip and start playing it.
103   this.setEnabled(true);
104   this.goto_(this.out_);
105 };
106 createjs.inherits('ButtonHelper', createjs.ButtonHelper, createjs.Object);
107 
108 /**
109  * Whether the object is listening events.
110  * @type {boolean}
111  * @private
112  */
113 createjs.ButtonHelper.prototype.enabled_ = false;
114 
115 /**
116  * Whether the target object has received a mousedown event.
117  * @type {boolean}
118  * @private
119  */
120 createjs.ButtonHelper.prototype.pressed_ = false;
121 
122 /**
123  * Moves the play position of the target clip to the specified label.
124  * @param {string|number} label
125  * @private
126  */
127 createjs.ButtonHelper.prototype.goto_ = function(label) {
128   /// <signature>
129   ///   <param type="string" name="label"/>
130   /// </signature>
131   /// <signature>
132   ///   <param type="number" name="label"/>
133   /// </signature>
134   if (this.play_) {
135     if (this.target_.gotoAndPlay) {
136       this.target_.gotoAndPlay(label);
137     }
138   } else {
139     if (this.target_.gotoAndStop) {
140       this.target_.gotoAndStop(label);
141     }
142   }
143 };
144 
145 /**
146  * Enables or disables this button.
147  * @param {boolean} value
148  * @const
149  */
150 createjs.ButtonHelper.prototype.setEnabled = function(value) {
151   /// <param type="boolean" name="value"/>
152   var enabled = !!value;
153   if (this.enabled_ == enabled) {
154     return;
155   }
156   this.enabled_ = enabled;
157   var target = this.target_;
158   if (enabled) {
159     target.on('mousedown', this);
160     target.on('pressup', this);
161   } else {
162     target.off('mousedown', this);
163     target.off('pressup', this);
164   }
165 };
166 
167 /** @override */
168 createjs.ButtonHelper.prototype.handleEvent = function(event) {
169   /// <param type="Event" name="event"/>
170   var type = event.type;
171   var label = '';
172   if (type == 'mousedown') {
173     this.pressed_ = true;
174     label = this.down_;
175   } else if (type == 'pressup') {
176     this.pressed_ = false;
177     label = this.out_;
178   }
179   this.goto_(label);
180 };
181 
182 // Export the createjs.ButtonHelper object to the global namespace.
183 createjs.exportObject('createjs.ButtonHelper', createjs.ButtonHelper, {
184   // createjs.Stage methods
185   'setEnabled': createjs.ButtonHelper.prototype.setEnabled
186 });
187