| 1 |
/* |
|---|
| 2 |
* Copyright(c) 2007 Yuki KODAMA [endflow.net] |
|---|
| 3 |
* |
|---|
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
|---|
| 5 |
* you may not use this file except in compliance with the License. |
|---|
| 6 |
* You may obtain a copy of the License at |
|---|
| 7 |
* |
|---|
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
|---|
| 9 |
* |
|---|
| 10 |
* Unless required by applicable law or agreed to in writing, software |
|---|
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
|---|
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, |
|---|
| 13 |
* either express or implied. See the License for the specific language |
|---|
| 14 |
* governing permissions and limitations under the License. |
|---|
| 15 |
*/ |
|---|
| 16 |
|
|---|
| 17 |
/** |
|---|
| 18 |
* KeyIfクラス |
|---|
| 19 |
* |
|---|
| 20 |
* @description キーイベントハンドラを生成してくれるクラス。 |
|---|
| 21 |
* @author Yuki KODAMA [endflow.net] |
|---|
| 22 |
* @since Flash Player 8 (ActionScript 2.0) |
|---|
| 23 |
* @version 0.2.0 |
|---|
| 24 |
* @link http://snippets.libspark.org/trac/wiki/kuy/KeyIf |
|---|
| 25 |
* @history [0.0.0] 200?-??-??: 初版 |
|---|
| 26 |
* [0.1.0] 2007-05-27: 改良。Snippets Projectに公開。 |
|---|
| 27 |
* [0.1.1] 2007-09-14: ちょっとリファクタリング&コメント追加。 |
|---|
| 28 |
* [0.2.0] 2007-09-15: Keyクラスで定義されていないキーもキーコードで指定可能にした。 |
|---|
| 29 |
*/ |
|---|
| 30 |
class net.endflow.util.KeyIf { |
|---|
| 31 |
/** |
|---|
| 32 |
* インスタンス化することはできません。 |
|---|
| 33 |
*/ |
|---|
| 34 |
private function KeyIf() {} |
|---|
| 35 |
|
|---|
| 36 |
/** |
|---|
| 37 |
* @param info : Object - キーイベントハンドラを生成するためのObject |
|---|
| 38 |
* @return Function - 生成されたキーイベントハンドラ |
|---|
| 39 |
* @usage <code> |
|---|
| 40 |
* Key.addListener(this); |
|---|
| 41 |
* this.onKeyDown = KeyIf.f({ |
|---|
| 42 |
* HOME: function(){trace('HOME')}, |
|---|
| 43 |
* UP: {PGUP: function(){trace('UP/PGUP')}}, |
|---|
| 44 |
* DOWN: {PGDN: function(){trace('DOWN/PGDN')}}, |
|---|
| 45 |
* LEFT: [function(){trace('LEFT#1')}, function(){trace('LEFT#2')}], |
|---|
| 46 |
* RIGHT: [function(){trace('RIGHT#1')}, function(){trace('RIGHT#2')}, function(){trace('RIGHT#3')}] |
|---|
| 47 |
* SPACE: {CONTROL: function(){trace('SPACE/CTRL')}}, |
|---|
| 48 |
* _90: function(){trace('Z')} |
|---|
| 49 |
* }); |
|---|
| 50 |
* </code> |
|---|
| 51 |
*/ |
|---|
| 52 |
public static function f(info:Object):Function { |
|---|
| 53 |
// 複数割り当て指定のオブジェクトを展開 |
|---|
| 54 |
for(var k in info) { |
|---|
| 55 |
if((typeof(info[k]) == 'object') && !(info[k] instanceof Array)) { |
|---|
| 56 |
// 再帰(一番奥の関数リテラルまで) |
|---|
| 57 |
info[k] = (function(root, info){ |
|---|
| 58 |
for(var k in info) { |
|---|
| 59 |
if(typeof(info[k]) == 'function') { |
|---|
| 60 |
return (root[k] = info[k]); |
|---|
| 61 |
} else if(typeof(info[k]) == 'object') { |
|---|
| 62 |
return (root[k] = arguments.callee(root, info[k])); |
|---|
| 63 |
} |
|---|
| 64 |
} |
|---|
| 65 |
}).call(null, info, info[k]); |
|---|
| 66 |
} |
|---|
| 67 |
} |
|---|
| 68 |
// 並列実行指定の配列を単一関数に置き換え |
|---|
| 69 |
for(var k in info) { |
|---|
| 70 |
if(info[k] instanceof Array) { |
|---|
| 71 |
info[k] = function(){ |
|---|
| 72 |
for(var i = 0; i < info[k].length; i++) { |
|---|
| 73 |
info[k][i](); |
|---|
| 74 |
} |
|---|
| 75 |
} |
|---|
| 76 |
}; |
|---|
| 77 |
} |
|---|
| 78 |
// ハンドラ関数の生成 |
|---|
| 79 |
return function() { |
|---|
| 80 |
for(var k in info) { |
|---|
| 81 |
if((k.charAt(0) == "_" && parseInt(k.substr(1)) == Key.getCode()) |
|---|
| 82 |
|| (Key[k] == Key.getCode())) { |
|---|
| 83 |
info[k](); |
|---|
| 84 |
} |
|---|
| 85 |
} |
|---|
| 86 |
}; |
|---|
| 87 |
} |
|---|
| 88 |
} |
|---|