/** * com.voidelement.display.RepeatButton Class for ActionScript 3.0 * * @author Copyright (c) 2007 munegon * @version 1.0 * * @link http://www.voidelement.com/ * @link http://void.heteml.jp/blog/ * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, * either express or implied. See the License for the specific language * governing permissions and limitations under the License. */ package com.voidelement.display { import flash.display.InteractiveObject; import flash.events.TimerEvent; import flash.events.MouseEvent; import flash.utils.Timer; public class RepeatButton { private var _target:InteractiveObject; private var _delay:uint; private var _interval:uint; private var _timer:Timer; private var _pressFunc:Function; private var _repeatFunc:Function; private var _releaseFunc:Function; /** * ボタン押下中のリピート処理を行うクラス * @param target マウスイベントを登録するための InteractiveObject インスタンス * @param delay リピート処理開始までの遅延時間 * @param interval リピート処理間隔 */ public function RepeatButton( target:InteractiveObject, delay:uint = 500, interval:uint = 50 ) { _target = target; _delay = delay; _interval = interval; _target.addEventListener( MouseEvent.MOUSE_DOWN, onMouseDown, false, 0, true ); } public function set onPress( func:Function ):void { _pressFunc = func; } public function set onRepeat( func:Function ):void { _repeatFunc = func; } public function set onRelease( func:Function ):void { _releaseFunc = func; } /** * マウス押下 - リピート処理登録 */ private function onMouseDown( e:MouseEvent ):void { if ( _pressFunc != null ){ _pressFunc(); } if ( _repeatFunc != null ){ _timer = new Timer( _delay, 1 ); _timer.addEventListener( TimerEvent.TIMER, onMouseDownRepeat ); _timer.start(); } _target.stage.addEventListener( MouseEvent.MOUSE_UP, onMouseUp ); } /** * リピート処理 */ private function onMouseDownRepeat( e:TimerEvent ):void { _timer = new Timer( _interval ); _timer.addEventListener( TimerEvent.TIMER, _repeatFunc ); _timer.start(); } /** * マウス押上 - リピート処理解除 */ private function onMouseUp( e:MouseEvent ):void { if ( _releaseFunc != null ){ _releaseFunc(); } if ( _repeatFunc != null ){ _timer.stop(); _timer.removeEventListener( TimerEvent.TIMER, _repeatFunc ); _timer.removeEventListener( TimerEvent.TIMER, onMouseDownRepeat ); } _target.stage.removeEventListener( MouseEvent.MOUSE_UP, onMouseUp ); } } }