/src/flash/com/hexagonstar/io/key/KeyManager.as

http://hexagonlib.googlecode.com/ · ActionScript · 237 lines · 120 code · 33 blank · 84 comment · 14 complexity · 82d9d2d938ff93bf69a94d731ccd40f3 MD5 · raw file

  1. /*
  2. * hexagonlib - Multi-Purpose ActionScript 3 Library.
  3. * __ __
  4. * __/ \__/ \__ __
  5. * / \__/HEXAGON \__/ \
  6. * \__/ \__/ LIBRARY _/
  7. * \__/ \__/
  8. *
  9. * Licensed under the MIT License
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  12. * this software and associated documentation files (the "Software"), to deal in
  13. * the Software without restriction, including without limitation the rights to
  14. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  15. * the Software, and to permit persons to whom the Software is furnished to do so,
  16. * subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included in all
  19. * copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  23. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  24. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  25. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  26. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. */
  28. package com.hexagonstar.io.key
  29. {
  30. import com.hexagonstar.core.BasicClass;
  31. import com.hexagonstar.event.KeyCombinationEvent;
  32. import com.hexagonstar.exception.SingletonException;
  33. import flash.events.KeyboardEvent;
  34. import flash.utils.Dictionary;
  35. /**
  36. * Manages Keyboard Input.
  37. */
  38. public class KeyManager extends BasicClass
  39. {
  40. ////////////////////////////////////////////////////////////////////////////////////////
  41. // Properties //
  42. ////////////////////////////////////////////////////////////////////////////////////////
  43. /** @private */
  44. private static var _instance:KeyManager;
  45. /** @private */
  46. private static var _singletonLock:Boolean = false;
  47. /** @private */
  48. private var _key:Key;
  49. /** @private */
  50. private var _assignmentsDown:Dictionary;
  51. /** @private */
  52. private var _assignmentsRelease:Dictionary;
  53. ////////////////////////////////////////////////////////////////////////////////////////
  54. // Public Methods //
  55. ////////////////////////////////////////////////////////////////////////////////////////
  56. /**
  57. * Creates a new instance of the class.
  58. */
  59. public function KeyManager()
  60. {
  61. // TODO There is still a bug with KeyCombination when more than 3 keys are used!
  62. if (!_singletonLock) throw new SingletonException(this);
  63. _assignmentsDown = new Dictionary();
  64. _assignmentsRelease = new Dictionary();
  65. _key = Key.instance;
  66. _key.addEventListener(KeyCombinationEvent.DOWN, onCombinationDown);
  67. _key.addEventListener(KeyCombinationEvent.RELEASE, onCombinationRelease);
  68. _key.addEventListener(KeyCombinationEvent.SEQUENCE, onCombinationTyped);
  69. _key.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
  70. _key.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
  71. }
  72. /**
  73. * Assigns a new Key Combination to the KeyManager.
  74. *
  75. * @param keyCodes key codes that the key combination consists of.
  76. * @param callback the function that should be called when the combination is entered.
  77. * @param isRelease true if key comb is triggered when pressed keys are released.
  78. */
  79. public function assignKeyCombination(keyCodes:Array,
  80. callback:Function,
  81. isRelease:Boolean = false):void
  82. {
  83. var c:KeyCombination = new KeyCombination(keyCodes);
  84. if (isRelease)
  85. {
  86. _assignmentsRelease[c] = callback;
  87. }
  88. else
  89. {
  90. _assignmentsDown[c] = callback;
  91. }
  92. _key.addKeyCombination(c);
  93. }
  94. /**
  95. * Removes a Key Combination from the KeyManager.
  96. *
  97. * @param keyCodes key codes that the key combination consists of.
  98. */
  99. public function removeKeyCombination(keyCodes:Array):void
  100. {
  101. var c:KeyCombination = new KeyCombination(keyCodes);
  102. _assignmentsRelease[c] = null;
  103. _assignmentsDown[c] = null;
  104. delete _assignmentsRelease[c];
  105. delete _assignmentsDown[c];
  106. _key.removeKeyCombination(c);
  107. }
  108. /**
  109. * Clears all key assignments from the Key manager.
  110. */
  111. public function clearAssignments():void
  112. {
  113. for (var c1:Object in _assignmentsDown)
  114. {
  115. if (c1 is KeyCombination)
  116. {
  117. _key.removeKeyCombination(KeyCombination(c1));
  118. }
  119. }
  120. _assignmentsDown = new Dictionary();
  121. for (var c2:Object in _assignmentsRelease)
  122. {
  123. if (c2 is KeyCombination)
  124. {
  125. _key.removeKeyCombination(KeyCombination(c2));
  126. }
  127. }
  128. _assignmentsRelease = new Dictionary();
  129. }
  130. ////////////////////////////////////////////////////////////////////////////////////////
  131. // Getters & Setters //
  132. ////////////////////////////////////////////////////////////////////////////////////////
  133. /**
  134. * Returns the singleton instance of KeyManager.
  135. */
  136. public static function get instance():KeyManager
  137. {
  138. if (_instance == null)
  139. {
  140. _singletonLock = true;
  141. _instance = new KeyManager();
  142. _singletonLock = false;
  143. }
  144. return _instance;
  145. }
  146. ////////////////////////////////////////////////////////////////////////////////////////
  147. // Event Handlers //
  148. ////////////////////////////////////////////////////////////////////////////////////////
  149. /**
  150. * @private
  151. */
  152. private function onCombinationDown(e:KeyCombinationEvent):void
  153. {
  154. for (var c:Object in _assignmentsDown)
  155. {
  156. if (c is KeyCombination)
  157. {
  158. if (e.keyCombination.equals(KeyCombination(c)))
  159. {
  160. var callback:Function = _assignmentsDown[c];
  161. callback.apply(null);
  162. break;
  163. }
  164. }
  165. }
  166. }
  167. /**
  168. * @private
  169. */
  170. private function onCombinationRelease(e:KeyCombinationEvent):void
  171. {
  172. for (var c:Object in _assignmentsRelease)
  173. {
  174. if (c is KeyCombination)
  175. {
  176. if (e.keyCombination.equals(KeyCombination(c)))
  177. {
  178. var callback:Function = _assignmentsRelease[c];
  179. callback.apply(null);
  180. break;
  181. }
  182. }
  183. }
  184. }
  185. /**
  186. * @private
  187. */
  188. private function onCombinationTyped(e:KeyCombinationEvent):void
  189. {
  190. }
  191. /**
  192. * @private
  193. */
  194. private function onKeyDown(e:KeyboardEvent):void
  195. {
  196. }
  197. /**
  198. * @private
  199. */
  200. private function onKeyUp(e:KeyboardEvent):void
  201. {
  202. }
  203. }
  204. }