| 1 |
/* |
|---|
| 2 |
* Licensed under the MIT License |
|---|
| 3 |
* |
|---|
| 4 |
* Copyright (c) 2008 BeInteractive!, Spark project |
|---|
| 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 |
|---|
| 22 |
* THE SOFTWARE. |
|---|
| 23 |
* |
|---|
| 24 |
*/ |
|---|
| 25 |
|
|---|
| 26 |
package org.libspark.utils.display |
|---|
| 27 |
{ |
|---|
| 28 |
import flash.display.InteractiveObject; |
|---|
| 29 |
import flash.display.Stage; |
|---|
| 30 |
import flash.events.Event; |
|---|
| 31 |
import flash.events.EventDispatcher; |
|---|
| 32 |
import flash.events.MouseEvent; |
|---|
| 33 |
|
|---|
| 34 |
/** |
|---|
| 35 |
* ターゲットの InteractiveObject が押されたときに MouseEvent.CLICK を配信します。 |
|---|
| 36 |
* 更に、指定された delay フレーム以上押し続けた場合に、interval の間隔で MouseEvent.CLICK をリピートします。 |
|---|
| 37 |
* |
|---|
| 38 |
* Dispatch and repeat MouseEvent.CLICK event if target InteractiveObject was pressed. |
|---|
| 39 |
* |
|---|
| 40 |
* Usage: |
|---|
| 41 |
* |
|---|
| 42 |
* 1. Initialize this class with stage. |
|---|
| 43 |
* |
|---|
| 44 |
* RepeatedClickDispatcher.initialize(stage); |
|---|
| 45 |
* |
|---|
| 46 |
* 2. Create a instance of this class with target InteractiveObject, repeat delay and repeat interval. |
|---|
| 47 |
* |
|---|
| 48 |
* var rcd:RepeatedClickDispatcher = new RepeatedClickDispatcher(target, 12, 1); |
|---|
| 49 |
* |
|---|
| 50 |
* 3. Hook the MouseEvent.CLICK event listener. |
|---|
| 51 |
* |
|---|
| 52 |
* rcd.addEventListener(MouseEvent.CLICK, clickHandler); |
|---|
| 53 |
* |
|---|
| 54 |
* 4. If you need finalize the instance, call removeEventListener and finalize method (for each instance). |
|---|
| 55 |
* |
|---|
| 56 |
* rcd.removeEventListener(MouseEvent.CLICK, clickHandler); |
|---|
| 57 |
* rcd.finalize(); |
|---|
| 58 |
* |
|---|
| 59 |
* @see http://www.libspark.org/wiki/Utils/RepeatedClickDispatcher |
|---|
| 60 |
*/ |
|---|
| 61 |
public class RepeatedClickDispatcher extends EventDispatcher |
|---|
| 62 |
{ |
|---|
| 63 |
private static var _stage:Stage; |
|---|
| 64 |
|
|---|
| 65 |
/** |
|---|
| 66 |
* 初期化 |
|---|
| 67 |
* |
|---|
| 68 |
* Initialize |
|---|
| 69 |
* |
|---|
| 70 |
* @param stage ステージ |
|---|
| 71 |
*/ |
|---|
| 72 |
public static function initialize(stage:Stage):void |
|---|
| 73 |
{ |
|---|
| 74 |
_stage = stage; |
|---|
| 75 |
} |
|---|
| 76 |
|
|---|
| 77 |
/** |
|---|
| 78 |
* @param target ターゲット |
|---|
| 79 |
* @param delay リピート開始までのフレーム数 |
|---|
| 80 |
* @param interval 何フレームごとにリピートするか |
|---|
| 81 |
*/ |
|---|
| 82 |
public function RepeatedClickDispatcher(target:InteractiveObject, delay:uint = 0, interval:uint = 0) |
|---|
| 83 |
{ |
|---|
| 84 |
_target = target; |
|---|
| 85 |
_delay = delay; |
|---|
| 86 |
_interval = interval == 0 ? 1 : interval; |
|---|
| 87 |
|
|---|
| 88 |
_target.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler); |
|---|
| 89 |
} |
|---|
| 90 |
|
|---|
| 91 |
private var _target:InteractiveObject; |
|---|
| 92 |
private var _counter:uint; |
|---|
| 93 |
private var _delay:uint; |
|---|
| 94 |
private var _interval:uint; |
|---|
| 95 |
|
|---|
| 96 |
private function mouseDownHandler(e:MouseEvent):void |
|---|
| 97 |
{ |
|---|
| 98 |
dispatchClickEvent(); |
|---|
| 99 |
|
|---|
| 100 |
_counter = 0; |
|---|
| 101 |
|
|---|
| 102 |
registerStageListener(); |
|---|
| 103 |
} |
|---|
| 104 |
|
|---|
| 105 |
private function mouseUpHandler(e:MouseEvent):void |
|---|
| 106 |
{ |
|---|
| 107 |
unregisterStageListener(); |
|---|
| 108 |
} |
|---|
| 109 |
|
|---|
| 110 |
private function registerStageListener():void |
|---|
| 111 |
{ |
|---|
| 112 |
_stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler); |
|---|
| 113 |
_stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler); |
|---|
| 114 |
} |
|---|
| 115 |
|
|---|
| 116 |
private function unregisterStageListener():void |
|---|
| 117 |
{ |
|---|
| 118 |
_stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler); |
|---|
| 119 |
_stage.removeEventListener(Event.ENTER_FRAME, enterFrameHandler); |
|---|
| 120 |
} |
|---|
| 121 |
|
|---|
| 122 |
private function enterFrameHandler(e:Event):void |
|---|
| 123 |
{ |
|---|
| 124 |
if (++_counter > _delay) { |
|---|
| 125 |
if ((_counter - _delay) % _interval == 0) { |
|---|
| 126 |
dispatchClickEvent(); |
|---|
| 127 |
} |
|---|
| 128 |
} |
|---|
| 129 |
} |
|---|
| 130 |
|
|---|
| 131 |
private function dispatchClickEvent():void |
|---|
| 132 |
{ |
|---|
| 133 |
dispatchEvent(new MouseEvent(MouseEvent.CLICK)); |
|---|
| 134 |
} |
|---|
| 135 |
|
|---|
| 136 |
/** |
|---|
| 137 |
* 破棄 |
|---|
| 138 |
*/ |
|---|
| 139 |
public function finalize():void |
|---|
| 140 |
{ |
|---|
| 141 |
_target.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler); |
|---|
| 142 |
unregisterStageListener(); |
|---|
| 143 |
} |
|---|
| 144 |
} |
|---|
| 145 |
} |
|---|