/src/org/mt4j/input/inputSources/KeyboardInputSource.java

http://mt4j.googlecode.com/ · Java · 271 lines · 107 code · 51 blank · 113 comment · 24 complexity · a1acb71eeeb88d8fb68385acb3f774cf MD5 · raw file

  1. /***********************************************************************
  2. * mt4j Copyright (c) 2008 - 2009, C.Ruff, Fraunhofer-Gesellschaft All rights reserved.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. ***********************************************************************/
  18. package org.mt4j.input.inputSources;
  19. import java.awt.event.KeyEvent;
  20. import org.mt4j.MTApplication;
  21. import org.mt4j.input.inputData.ActiveCursorPool;
  22. import org.mt4j.input.inputData.InputCursor;
  23. import org.mt4j.input.inputData.MTFingerInputEvt;
  24. import processing.core.PApplet;
  25. /**
  26. * The Class KeyboardInputSource.
  27. * @author Christopher Ruff
  28. */
  29. public class KeyboardInputSource extends AbstractInputSource {
  30. /** The last used keyb id. */
  31. private long lastUsedKeybID;
  32. /** The location x. */
  33. private int locationX;
  34. /** The location y. */
  35. private int locationY;
  36. /** The space has been pressed. */
  37. private boolean spaceHasBeenPressed = false;
  38. /** The applet. */
  39. private PApplet applet;
  40. private int newFingerLocationKCode;
  41. private int moveUpKeyCode;
  42. private int moveLeftKeyCode;
  43. private int moveDownKeyCode;
  44. private int moveRightKeyCode;
  45. private int fingerDownKeyCode;
  46. /**
  47. * Instantiates a new keyboard input source.
  48. *
  49. * @param pa the pa
  50. */
  51. public KeyboardInputSource(MTApplication pa){
  52. super(pa);
  53. this.applet = pa;
  54. applet.registerKeyEvent(this);
  55. // applet.registerDraw(this);
  56. this.locationX = 0;
  57. this.locationY = 0;
  58. this.moveUpKeyCode = KeyEvent.VK_W;
  59. this.moveLeftKeyCode = KeyEvent.VK_A;
  60. this.moveDownKeyCode = KeyEvent.VK_S;
  61. this.moveRightKeyCode = KeyEvent.VK_D;
  62. this.newFingerLocationKCode = KeyEvent.VK_N;
  63. this.fingerDownKeyCode = KeyEvent.VK_SHIFT;
  64. }
  65. //401 = pressed //402 = released
  66. /**
  67. * Key event.
  68. *
  69. * @param e the e
  70. */
  71. public void keyEvent(KeyEvent e){
  72. // System.out.println(e.getID());
  73. // System.out.println(e.getKeyCode());
  74. int evtID = e.getID();
  75. if (evtID == KeyEvent.KEY_PRESSED ){
  76. if (e.isControlDown() && e.getKeyCode() == this.newFingerLocationKCode){
  77. locationX = applet.mouseX;
  78. locationY = applet.mouseY;
  79. }else if (e.getKeyCode() == this.moveUpKeyCode){
  80. locationY-=5;
  81. if (e.isShiftDown()){
  82. fingerDown(e);
  83. }
  84. }else if (e.getKeyCode() == this.moveLeftKeyCode){
  85. locationX-=5;
  86. if (e.isShiftDown()){
  87. fingerDown(e);
  88. }
  89. }else if (e.getKeyCode() == this.moveDownKeyCode){
  90. locationY+=5;
  91. if (e.isShiftDown()){
  92. fingerDown(e);
  93. }
  94. }else if (e.getKeyCode() == this.moveRightKeyCode){
  95. locationX+=5;
  96. if (e.isShiftDown()){
  97. fingerDown(e);
  98. }
  99. }else if (e.getKeyCode() == this.fingerDownKeyCode){
  100. fingerDown(e);
  101. }
  102. }else if (evtID == KeyEvent.KEY_RELEASED){
  103. if (e.getKeyCode() == this.fingerDownKeyCode){
  104. fingerUp(e);
  105. }
  106. }
  107. /*
  108. switch (e.getKeyCode()){
  109. case KeyEvent.VK_W:
  110. if (evtID == KeyEvent.KEY_PRESSED ){
  111. locationY-=5;
  112. if (e.isShiftDown()){
  113. shiftPressed(e);
  114. }
  115. }
  116. break;
  117. case KeyEvent.VK_A:
  118. if (evtID == KeyEvent.KEY_PRESSED){
  119. locationX-=5;
  120. if (e.isShiftDown()){
  121. shiftPressed(e);
  122. }
  123. }
  124. break;
  125. case KeyEvent.VK_S:
  126. if (evtID == KeyEvent.KEY_PRESSED){
  127. locationY+=5;
  128. if (e.isShiftDown()){
  129. shiftPressed(e);
  130. }
  131. }
  132. break;
  133. case KeyEvent.VK_D:
  134. if (evtID == KeyEvent.KEY_PRESSED){
  135. locationX+=5;
  136. if (e.isShiftDown()){
  137. shiftPressed(e);
  138. }
  139. }
  140. break;
  141. case KeyEvent.VK_SHIFT:
  142. if (evtID == KeyEvent.KEY_PRESSED){
  143. shiftPressed(e);
  144. }
  145. else if (evtID == KeyEvent.KEY_RELEASED){
  146. shiftReleased(e);
  147. }
  148. break;
  149. case KeyEvent.VK_N: //set the location to the mouseposition
  150. if (evtID == KeyEvent.KEY_PRESSED){
  151. locationX = applet.mouseX;
  152. locationY = applet.mouseY;
  153. }
  154. break;
  155. default:
  156. break;
  157. }
  158. */
  159. }
  160. public void setNewFingerLocationKeyCode(int keyCode){
  161. this.newFingerLocationKCode = keyCode;
  162. }
  163. public void setFingerDownKeyCode(int keyCode){
  164. this.fingerDownKeyCode = keyCode;
  165. }
  166. public void setMoveUpKeyCode(int keyCode){
  167. this.moveUpKeyCode = keyCode;
  168. }
  169. public void setMoveLeftKeyCode(int keyCode){
  170. this.moveLeftKeyCode = keyCode;
  171. }
  172. public void setmoveDownKeyCode(int keyCode){
  173. this.moveDownKeyCode = keyCode;
  174. }
  175. public void setMoveRightKeyCode(int keyCode){
  176. this.moveRightKeyCode = keyCode;
  177. }
  178. /**
  179. *
  180. * @param e the e
  181. */
  182. private void fingerDown(KeyEvent e){
  183. if (!spaceHasBeenPressed){
  184. InputCursor m = new InputCursor();
  185. MTFingerInputEvt touchEvt = new MTFingerInputEvt(this, locationX, locationY, MTFingerInputEvt.INPUT_STARTED, m);
  186. // m.addEvent(touchEvt);
  187. lastUsedKeybID = m.getId();
  188. ActiveCursorPool.getInstance().putActiveCursor(lastUsedKeybID, m);
  189. //FIRE
  190. this.enqueueInputEvent(touchEvt);
  191. spaceHasBeenPressed = true;
  192. }else{
  193. InputCursor m = ActiveCursorPool.getInstance().getActiveCursorByID(lastUsedKeybID);
  194. // if (m.getLastEvent().getPositionX() != locationX || m.getLastEvent().getPositionY() != locationY){
  195. MTFingerInputEvt te = new MTFingerInputEvt(this, locationX, locationY, MTFingerInputEvt.INPUT_UPDATED, m);
  196. // m.addEvent(new MTFingerInputEvt2(this, e.getX(), e.getY(), MTFingerInputEvt.FINGER_UPDATE, m));
  197. //FIRE
  198. this.enqueueInputEvent(te);
  199. // }
  200. }
  201. }
  202. /**
  203. *
  204. * @param e the e
  205. */
  206. private void fingerUp(KeyEvent e) {
  207. InputCursor m = ActiveCursorPool.getInstance().getActiveCursorByID(lastUsedKeybID);
  208. MTFingerInputEvt te = new MTFingerInputEvt(this, locationX, locationY, MTFingerInputEvt.INPUT_ENDED, m);
  209. // m.addEvent(te);
  210. this.enqueueInputEvent(te);
  211. ActiveCursorPool.getInstance().removeCursor((lastUsedKeybID));
  212. spaceHasBeenPressed = false;
  213. // MTFingerInputEvt te = MTFingerInputEvtPool.getInstance().getEventByID(lastUsedKeybID);
  214. // //FIXME warum gibts manchmal NUllpointer weil kein TE vorhanden? auch bei mouse..
  215. }
  216. // @Override
  217. // public boolean firesEventType(Class<? extends MTInputEvent> evtClass){
  218. // return (evtClass == MTFingerInputEvt.class);
  219. // }
  220. }