/src/org/mt4j/input/inputProcessors/globalProcessors/InputRetargeter.java

http://mt4j.googlecode.com/ · Java · 114 lines · 60 code · 12 blank · 42 comment · 8 complexity · d4898557f880d33980c2143784c7db47 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.inputProcessors.globalProcessors;
  19. import java.util.HashMap;
  20. import java.util.Map;
  21. import org.mt4j.components.interfaces.IMTComponent3D;
  22. import org.mt4j.input.IHitTestInfoProvider;
  23. import org.mt4j.input.inputData.AbstractCursorInputEvt;
  24. import org.mt4j.input.inputData.InputCursor;
  25. import org.mt4j.input.inputData.MTInputEvent;
  26. /**
  27. * The Class InputRetargeter. This global input analyzer is automatically created with each new scene and listens
  28. * to all InputSources for AbstractCursorInputEvt events (Input events with a discrete position attached).
  29. * <br>This global input analyzer uses MTPositionEvents to check which object in the current scene was hit.
  30. * Then the targetComponent is added to the event and the event is delivered to the scenes canvas where the
  31. * targeted event is delivered to the targetComponent. No new event is created for performance reasons.
  32. * So the event is merely retargeted and redirected.
  33. *
  34. * @author Christopher Ruff
  35. */
  36. public class InputRetargeter extends AbstractGlobalInputProcessor {
  37. private Map<InputCursor, IMTComponent3D> cursorToObjectMap;
  38. /** The app info provider. */
  39. private IHitTestInfoProvider appInfoProvider;
  40. public InputRetargeter(IHitTestInfoProvider appInfoProvider) {
  41. super();
  42. this.appInfoProvider = appInfoProvider;
  43. this.cursorToObjectMap = new HashMap<InputCursor, IMTComponent3D>();
  44. }
  45. public void processInputEvtImpl(MTInputEvent inputEvent) {
  46. if (inputEvent instanceof AbstractCursorInputEvt) {
  47. AbstractCursorInputEvt posEvt = (AbstractCursorInputEvt) inputEvent;
  48. InputCursor m = posEvt.getCursor();
  49. switch (posEvt.getId()) {
  50. case AbstractCursorInputEvt.INPUT_STARTED:{
  51. // logger.debug("Finger DOWN-> " + " ID:" + posEvt.getId() + "; X:" + posEvt.getPosX() + " Y:" + posEvt.getPosY() + "; Source: " + posEvt.getSource());
  52. // System.out.println("Finger DOWN-> " + " ID:" + posEvt.getId() + "; X:" + posEvt.getPosX() + " Y:" + posEvt.getPosY() + "; Source: " + posEvt.getSource()+ " CursorID: " + m.getId() + " appInfoProv: " + appInfoProvider);
  53. //Check if there is an object under the cursor and save it to a hashtable with the event if so
  54. IMTComponent3D obj = appInfoProvider.getComponentAt(posEvt.getX(), posEvt.getY());
  55. if (obj != null){
  56. cursorToObjectMap.put(m, obj);
  57. posEvt.setTarget(obj);
  58. // posEvt.setCurrentTarget(obj.getRoot()); //Enable this if using event CAPTURING PHASE
  59. posEvt.setCurrentTarget(obj);
  60. posEvt.setEventPhase(MTInputEvent.CAPTURING_PHASE);
  61. this.fireInputEvent(posEvt);
  62. }
  63. }
  64. break;
  65. case AbstractCursorInputEvt.INPUT_UPDATED:{
  66. // logger.debug("Finger UPDATE-> " + " ID:" + posEvt.getId() + "; X:" + posEvt.getPositionX() + " Y:" + posEvt.getPositionY() + "; Source: " + posEvt.getSource());
  67. IMTComponent3D associatedObj = cursorToObjectMap.get(m);
  68. if (associatedObj != null){
  69. posEvt.setTarget(associatedObj);
  70. // posEvt.setCurrentTarget(associatedObj.getRoot());//Enable this if using event CAPTURING PHASE
  71. posEvt.setCurrentTarget(associatedObj);
  72. posEvt.setEventPhase(MTInputEvent.CAPTURING_PHASE);
  73. this.fireInputEvent(posEvt);
  74. }
  75. }
  76. break;
  77. case AbstractCursorInputEvt.INPUT_ENDED:{
  78. // logger.debug("Finger UP-> " + " ID:" + posEvt.getId() + "; X:" + posEvt.getPositionX() + " Y:" + posEvt.getPositionY() + "; Source: " + posEvt.getSource());
  79. // IMTComponent3D associatedObj = motionToObjectMap.get(m);
  80. IMTComponent3D associatedObj = cursorToObjectMap.remove(m);
  81. if (associatedObj != null){
  82. posEvt.setTarget(associatedObj);
  83. // posEvt.setCurrentTarget(associatedObj.getRoot());//Enable this if using event CAPTURING PHASE
  84. posEvt.setCurrentTarget(associatedObj);
  85. posEvt.setEventPhase(MTInputEvent.CAPTURING_PHASE);
  86. this.fireInputEvent(posEvt);
  87. // motionToObjectMap.remove(m);
  88. }
  89. }
  90. break;
  91. default:
  92. break;
  93. }
  94. }else{
  95. //Other event type, evtl ohne absolute x,y coordianten (z.b. joystick)
  96. //einfach an mtcanvas weiterleiten?
  97. // logger.error("Warning in " + this + " Dont know how to handle evt: " + inputEvent );
  98. //Just fire other input events to the current canvas by default
  99. this.fireInputEvent(inputEvent);
  100. }
  101. }
  102. }