root/as3/RepeatButton/src/com/voidelement/display/RepeatButton.as

リビジョン 132, 3.2 kB (コミッタ: yossy, コミット時期: 4 年 前)

Moved.

  • svn:mime-type 属性の設定値: text/plain;charset=utf-8
Line 
1 /**
2  * com.voidelement.display.RepeatButton  Class for ActionScript 3.0
3  * 
4  * @author       Copyright (c) 2007 munegon
5  * @version      1.0
6  * 
7  * @link         http://www.voidelement.com/
8  * @link         http://void.heteml.jp/blog/
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  * 
14  * http://www.apache.org/licenses/LICENSE-2.0
15  * 
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
19  * either express or implied. See the License for the specific language
20  * governing permissions and limitations under the License.
21  */
22
23
24
25 package com.voidelement.display {
26         import flash.display.InteractiveObject;
27         import flash.events.TimerEvent;
28         import flash.events.MouseEvent;
29         import flash.utils.Timer;
30        
31         public class RepeatButton {
32                 private var _target:InteractiveObject;
33                 private var _delay:uint;
34                 private var _interval:uint;
35                
36                 private var _timer:Timer;
37                
38                 private var _pressFunc:Function;
39                 private var _repeatFunc:Function;
40                 private var _releaseFunc:Function;
41                
42                
43                 /**
44                  * ボタン押下中のリピート処理を行うクラス
45                  * @param       target          マウスイベントを登録するための InteractiveObject インスタンス
46                  * @param       delay           リピート処理開始までの遅延時間
47                  * @param       interval        リピート処理間隔
48                  */
49                 public function RepeatButton( target:InteractiveObject, delay:uint = 500, interval:uint = 50 ) {
50                         _target = target;
51                         _delay = delay;
52                         _interval = interval;
53                        
54                         _target.addEventListener( MouseEvent.MOUSE_DOWN, onMouseDown, false, 0, true );
55                 }
56                
57                
58                 public function set onPress( func:Function ):void {
59                         _pressFunc = func;
60                 }
61                
62                 public function set onRepeat( func:Function ):void {
63                         _repeatFunc = func;
64                 }
65                
66                 public function set onRelease( func:Function ):void {
67                         _releaseFunc = func;
68                 }
69                
70                
71                 /**
72                  * マウス押下 - リピート処理登録
73                  */
74                 private function onMouseDown( e:MouseEvent ):void {
75                         if ( _pressFunc != null ){
76                                 _pressFunc();
77                         }
78                        
79                         if ( _repeatFunc != null ){
80                                 _timer = new Timer( _delay, 1 );
81                                 _timer.addEventListener( TimerEvent.TIMER, onMouseDownRepeat );
82                                 _timer.start();
83                         }
84                        
85                         _target.stage.addEventListener( MouseEvent.MOUSE_UP, onMouseUp );
86                 }
87                
88                
89                 /**
90                  * リピート処理
91                  */
92                 private function onMouseDownRepeat( e:TimerEvent ):void {
93                         _timer = new Timer( _interval );
94                         _timer.addEventListener( TimerEvent.TIMER, _repeatFunc );
95                         _timer.start();
96                 }
97                
98                
99                 /**
100                  * マウス押上 - リピート処理解除
101                  */
102                 private function onMouseUp( e:MouseEvent ):void {
103                         if ( _releaseFunc != null ){
104                                 _releaseFunc();
105                         }
106                        
107                         if ( _repeatFunc != null ){
108                                 _timer.stop();
109                                 _timer.removeEventListener( TimerEvent.TIMER, _repeatFunc );
110                                 _timer.removeEventListener( TimerEvent.TIMER, onMouseDownRepeat );
111                         }
112                        
113                         _target.stage.removeEventListener( MouseEvent.MOUSE_UP, onMouseUp );
114                 }
115         }
116 }
117
118
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。