| 1 |
/* |
|---|
| 2 |
* ActionScript Thread Library |
|---|
| 3 |
* |
|---|
| 4 |
* Licensed under the MIT License |
|---|
| 5 |
* |
|---|
| 6 |
* Copyright (c) 2008 BeInteractive! (www.be-interactive.org) and |
|---|
| 7 |
* Spark project (www.libspark.org) |
|---|
| 8 |
* |
|---|
| 9 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|---|
| 10 |
* of this software and associated documentation files (the "Software"), to deal |
|---|
| 11 |
* in the Software without restriction, including without limitation the rights |
|---|
| 12 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|---|
| 13 |
* copies of the Software, and to permit persons to whom the Software is |
|---|
| 14 |
* furnished to do so, subject to the following conditions: |
|---|
| 15 |
* |
|---|
| 16 |
* The above copyright notice and this permission notice shall be included in |
|---|
| 17 |
* all copies or substantial portions of the Software. |
|---|
| 18 |
* |
|---|
| 19 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|---|
| 20 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|---|
| 21 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|---|
| 22 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|---|
| 23 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|---|
| 24 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|---|
| 25 |
* THE SOFTWARE. |
|---|
| 26 |
* |
|---|
| 27 |
*/ |
|---|
| 28 |
package org.libspark.thread.threads.display |
|---|
| 29 |
{ |
|---|
| 30 |
import flash.display.Sprite; |
|---|
| 31 |
import flash.events.MouseEvent; |
|---|
| 32 |
import flash.geom.Point; |
|---|
| 33 |
import org.libspark.thread.Thread; |
|---|
| 34 |
|
|---|
| 35 |
/** |
|---|
| 36 |
* Spriteをドラッグ&ドロップさせるためのスレッドです。 |
|---|
| 37 |
* |
|---|
| 38 |
* <p>このスレッドを start すると、対象の Sprite に対して MOUSE_DOWN, MOUSE_UPイベントを設定し、 |
|---|
| 39 |
* それぞれのハンドラで startDrag, stopDrag を呼び出します。</p> |
|---|
| 40 |
* |
|---|
| 41 |
* <p>イベント発生時の MouseEvent は、それぞれ DownEvent, UpEvent で取得できます。</p> |
|---|
| 42 |
* |
|---|
| 43 |
* @author uwi |
|---|
| 44 |
*/ |
|---|
| 45 |
public class DraggerThread extends Thread |
|---|
| 46 |
{ |
|---|
| 47 |
private var _target : Sprite; |
|---|
| 48 |
private var _downevent : MouseEvent = null; |
|---|
| 49 |
private var _upevent : MouseEvent = null; |
|---|
| 50 |
|
|---|
| 51 |
public function DraggerThread(target : Sprite) { |
|---|
| 52 |
_target = target; |
|---|
| 53 |
} |
|---|
| 54 |
|
|---|
| 55 |
override protected function run() : void |
|---|
| 56 |
{ |
|---|
| 57 |
onMouseUp(null); |
|---|
| 58 |
} |
|---|
| 59 |
|
|---|
| 60 |
protected function onMouseUp(e : MouseEvent) : void |
|---|
| 61 |
{ |
|---|
| 62 |
_upevent = e; |
|---|
| 63 |
_target.stopDrag(); |
|---|
| 64 |
event(_target, MouseEvent.MOUSE_DOWN, onMouseDown); |
|---|
| 65 |
} |
|---|
| 66 |
|
|---|
| 67 |
protected function onMouseDown(e : MouseEvent) : void |
|---|
| 68 |
{ |
|---|
| 69 |
_upevent = null; |
|---|
| 70 |
_downevent = e; |
|---|
| 71 |
_target.startDrag(); |
|---|
| 72 |
event(_target, MouseEvent.MOUSE_UP, onMouseUp); |
|---|
| 73 |
} |
|---|
| 74 |
|
|---|
| 75 |
public function delta() : Point |
|---|
| 76 |
{ |
|---|
| 77 |
if (_downevent != null && _upevent != null) { |
|---|
| 78 |
return new Point(_upevent.stageX - _downevent.stageX, _upevent.stageY - _downevent.stageY); |
|---|
| 79 |
} |
|---|
| 80 |
return null; |
|---|
| 81 |
} |
|---|
| 82 |
|
|---|
| 83 |
public function get Target() : Sprite { return _target; } |
|---|
| 84 |
public function get DownEvent() : MouseEvent { return _downevent; } |
|---|
| 85 |
public function get UpEvent() : MouseEvent { return _upevent; } |
|---|
| 86 |
} |
|---|
| 87 |
|
|---|
| 88 |
} |
|---|