Touch event core files integration

git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@7548 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
3.0
kim..ng 14 years ago
parent dd100bed74
commit d472a16f7f
  1. 11
      engine/src/core/com/jme3/app/Application.java
  2. 53
      engine/src/core/com/jme3/input/InputManager.java
  3. 4
      engine/src/core/com/jme3/input/RawInputListener.java
  4. 50
      engine/src/core/com/jme3/input/TouchInput.java
  5. 49
      engine/src/core/com/jme3/input/controls/TouchListener.java
  6. 50
      engine/src/core/com/jme3/input/controls/TouchTrigger.java
  7. 231
      engine/src/core/com/jme3/input/event/TouchEvent.java
  8. 6
      engine/src/core/com/jme3/system/JmeContext.java
  9. 6
      engine/src/core/com/jme3/system/NullContext.java

@ -36,6 +36,7 @@ import com.jme3.app.state.AppStateManager;
import com.jme3.input.JoyInput; import com.jme3.input.JoyInput;
import com.jme3.input.KeyInput; import com.jme3.input.KeyInput;
import com.jme3.input.MouseInput; import com.jme3.input.MouseInput;
import com.jme3.input.TouchInput;
import com.jme3.system.*; import com.jme3.system.*;
import com.jme3.math.Vector3f; import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera; import com.jme3.renderer.Camera;
@ -90,6 +91,7 @@ public class Application implements SystemListener {
protected MouseInput mouseInput; protected MouseInput mouseInput;
protected KeyInput keyInput; protected KeyInput keyInput;
protected JoyInput joyInput; protected JoyInput joyInput;
protected TouchInput touchInput;
protected InputManager inputManager; protected InputManager inputManager;
protected AppStateManager stateManager; protected AppStateManager stateManager;
@ -223,13 +225,17 @@ public class Application implements SystemListener {
if (keyInput != null) if (keyInput != null)
keyInput.initialize(); keyInput.initialize();
touchInput = context.getTouchInput();
if (touchInput != null)
touchInput.initialize();
if (!settings.getBoolean("DisableJoysticks")){ if (!settings.getBoolean("DisableJoysticks")){
joyInput = context.getJoyInput(); joyInput = context.getJoyInput();
if (joyInput != null) if (joyInput != null)
joyInput.initialize(); joyInput.initialize();
} }
inputManager = new InputManager(mouseInput, keyInput, joyInput); inputManager = new InputManager(mouseInput, keyInput, joyInput, touchInput);
} }
private void initStateManager(){ private void initStateManager(){
@ -480,6 +486,9 @@ public class Application implements SystemListener {
if (joyInput != null) if (joyInput != null)
joyInput.destroy(); joyInput.destroy();
if (touchInput != null)
touchInput.destroy();
inputManager = null; inputManager = null;
} }

@ -39,6 +39,7 @@ import com.jme3.input.controls.JoyButtonTrigger;
import com.jme3.input.controls.KeyTrigger; import com.jme3.input.controls.KeyTrigger;
import com.jme3.input.controls.MouseAxisTrigger; import com.jme3.input.controls.MouseAxisTrigger;
import com.jme3.input.controls.MouseButtonTrigger; import com.jme3.input.controls.MouseButtonTrigger;
import com.jme3.input.controls.TouchListener;
import com.jme3.input.controls.Trigger; import com.jme3.input.controls.Trigger;
import com.jme3.input.event.InputEvent; import com.jme3.input.event.InputEvent;
import com.jme3.input.event.JoyAxisEvent; import com.jme3.input.event.JoyAxisEvent;
@ -46,6 +47,7 @@ import com.jme3.input.event.JoyButtonEvent;
import com.jme3.input.event.KeyInputEvent; import com.jme3.input.event.KeyInputEvent;
import com.jme3.input.event.MouseButtonEvent; import com.jme3.input.event.MouseButtonEvent;
import com.jme3.input.event.MouseMotionEvent; import com.jme3.input.event.MouseMotionEvent;
import com.jme3.input.event.TouchEvent;
import com.jme3.math.FastMath; import com.jme3.math.FastMath;
import com.jme3.math.Vector2f; import com.jme3.math.Vector2f;
import com.jme3.util.IntMap; import com.jme3.util.IntMap;
@ -70,6 +72,7 @@ public class InputManager implements RawInputListener {
private final KeyInput keys; private final KeyInput keys;
private final MouseInput mouse; private final MouseInput mouse;
private final JoyInput joystick; private final JoyInput joystick;
private final TouchInput touch;
private float frameTPF; private float frameTPF;
private long lastLastUpdateTime = 0; private long lastLastUpdateTime = 0;
private long lastUpdateTime = 0; private long lastUpdateTime = 0;
@ -108,7 +111,7 @@ public class InputManager implements RawInputListener {
* @param joyInput * @param joyInput
* @throws IllegalArgumentException If either mouseInput or keyInput are null. * @throws IllegalArgumentException If either mouseInput or keyInput are null.
*/ */
public InputManager(MouseInput mouse, KeyInput keys, JoyInput joystick) { public InputManager(MouseInput mouse, KeyInput keys, JoyInput joystick, TouchInput touch) {
if (keys == null || mouse == null) { if (keys == null || mouse == null) {
throw new NullPointerException("Mouse or keyboard cannot be null"); throw new NullPointerException("Mouse or keyboard cannot be null");
} }
@ -116,6 +119,7 @@ public class InputManager implements RawInputListener {
this.keys = keys; this.keys = keys;
this.mouse = mouse; this.mouse = mouse;
this.joystick = joystick; this.joystick = joystick;
this.touch = touch;
keys.setInputListener(this); keys.setInputListener(this);
mouse.setInputListener(this); mouse.setInputListener(this);
@ -123,6 +127,9 @@ public class InputManager implements RawInputListener {
joystick.setInputListener(this); joystick.setInputListener(this);
joysticks = joystick.loadJoysticks(this); joysticks = joystick.loadJoysticks(this);
} }
if (touch != null) {
touch.setInputListener(this);
}
firstTime = keys.getInputTimeNanos(); firstTime = keys.getInputTimeNanos();
} }
@ -529,6 +536,22 @@ public class InputManager implements RawInputListener {
return rawListenerArray; return rawListenerArray;
} }
public TouchInput getTouchInput() {
return touch;
}
public void setSimulateMouse(boolean value) {
if (touch != null) {
touch.setSimulateMouse(value);
}
}
public void setSimulateKeyboard(boolean value) {
if (touch != null) {
touch.setSimulateKeyboard(value);
}
}
private void processQueue() { private void processQueue() {
int queueSize = inputQueue.size(); int queueSize = inputQueue.size();
RawInputListener[] array = getRawListenerArray(); RawInputListener[] array = getRawListenerArray();
@ -552,6 +575,8 @@ public class InputManager implements RawInputListener {
listener.onJoyAxisEvent((JoyAxisEvent) event); listener.onJoyAxisEvent((JoyAxisEvent) event);
} else if (event instanceof JoyButtonEvent) { } else if (event instanceof JoyButtonEvent) {
listener.onJoyButtonEvent((JoyButtonEvent) event); listener.onJoyButtonEvent((JoyButtonEvent) event);
} else if (event instanceof TouchEvent) {
listener.onTouchEvent((TouchEvent) event);
} else { } else {
assert false; assert false;
} }
@ -576,6 +601,8 @@ public class InputManager implements RawInputListener {
onJoyAxisEventQueued((JoyAxisEvent) event); onJoyAxisEventQueued((JoyAxisEvent) event);
} else if (event instanceof JoyButtonEvent) { } else if (event instanceof JoyButtonEvent) {
onJoyButtonEventQueued((JoyButtonEvent) event); onJoyButtonEventQueued((JoyButtonEvent) event);
} else if (event instanceof TouchEvent) {
onTouchEventQueued((TouchEvent) event);
} else { } else {
assert false; assert false;
} }
@ -603,6 +630,9 @@ public class InputManager implements RawInputListener {
if (joystick != null) { if (joystick != null) {
joystick.update(); joystick.update();
} }
if (touch != null) {
touch.update();
}
eventsPermitted = false; eventsPermitted = false;
@ -612,4 +642,25 @@ public class InputManager implements RawInputListener {
lastLastUpdateTime = lastUpdateTime; lastLastUpdateTime = lastUpdateTime;
lastUpdateTime = currentTime; lastUpdateTime = currentTime;
} }
/**
* Dispatches touch events to touch listeners
* @param evt
*/
public void onTouchEventQueued(TouchEvent evt) {
for (Mapping mapping : mappings.values()) {
for (InputListener listener : mapping.listeners) {
if (listener instanceof TouchListener) {
((TouchListener) listener).onTouch(mapping.name, evt, frameTPF);
}
}
}
}
@Override
public void onTouchEvent(TouchEvent evt) {
if (!eventsPermitted) {
throw new UnsupportedOperationException("TouchInput has raised an event at an illegal time.");
}
inputQueue.add(evt);
}
} }

@ -37,6 +37,7 @@ import com.jme3.input.event.JoyButtonEvent;
import com.jme3.input.event.KeyInputEvent; import com.jme3.input.event.KeyInputEvent;
import com.jme3.input.event.MouseButtonEvent; import com.jme3.input.event.MouseButtonEvent;
import com.jme3.input.event.MouseMotionEvent; import com.jme3.input.event.MouseMotionEvent;
import com.jme3.input.event.TouchEvent;
/** /**
* An interface used for receiving raw input from devices. * An interface used for receiving raw input from devices.
@ -51,4 +52,7 @@ public interface RawInputListener {
public void onMouseMotionEvent(MouseMotionEvent evt); public void onMouseMotionEvent(MouseMotionEvent evt);
public void onMouseButtonEvent(MouseButtonEvent evt); public void onMouseButtonEvent(MouseButtonEvent evt);
public void onKeyEvent(KeyInputEvent evt); public void onKeyEvent(KeyInputEvent evt);
// Generic Smartphone input event, used by the Android platform currently
public void onTouchEvent(TouchEvent evt);
} }

@ -0,0 +1,50 @@
/*
* Copyright (c) 2009-2010 jMonkeyEngine
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.jme3.input;
/**
* A specific API for interfacing with smartphone touch devices
*/
public interface TouchInput extends Input {
/**
* @param simulate Whether mouse events should be generated
*/
public void setSimulateMouse(boolean simulate);
/**
* @param simulate Whether keyboard events should be generated
*/
public void setSimulateKeyboard(boolean simulate);
}

@ -0,0 +1,49 @@
/*
* Copyright (c) 2009-2010 jMonkeyEngine
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.jme3.input.controls;
import com.jme3.input.event.TouchEvent;
/**
* <code>TouchListener</code> is used to receive events of inputs from smartphone touch devices
*
* @author larynx
*/
public interface TouchListener extends InputListener {
/**
* @param name - the name of the event
* @param event - the touch event itself
* @param tpf - how much time has passed since the last frame
*/
public void onTouch(String name, TouchEvent event, float tpf);
}

@ -0,0 +1,50 @@
/*
* Copyright (c) 2009-2010 jMonkeyEngine
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.jme3.input.controls;
public class TouchTrigger implements Trigger {
public TouchTrigger() {
super();
}
@Override
public int hashCode(){
return 0xfedcba98;
}
@Override
public String getName() {
return "TouchInput";
}
}

@ -0,0 +1,231 @@
/*
* Copyright (c) 2009-2010 jMonkeyEngine
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.jme3.input.event;
import com.jme3.input.event.InputEvent;
import com.jme3.math.Vector2f;
/**
* <code>TouchEvent</code> represents a single event from multi-touch input devices
* @author larynx
*
*/
public class TouchEvent extends InputEvent
{
public static enum Type
{
/**
* Touch down event, fields: posX, posY
*/
DOWN,
/**
* Move/Drag event, fields: posX, posY, deltaX, deltaY
*/
MOVE,
/**
* Touch up event, fields: posX, posY
*/
UP,
/**
* Virtual keyboard or hardware key event down, fields: keyCode, characters
*/
KEY_DOWN,
/**
* Virtual keyboard or hardware key event up, fields: keyCode, characters
*/
KEY_UP,
// Single finger gestures
FLING,
TAP,
DOUBLETAP,
LONGPRESSED,
// Two finger scale events
/**
* Two finger scale event start, fields: posX/posY = getFocusX/Y, scaleFactor, scaleSpan
*/
SCALE_START,
/**
* Two finger scale event, fields: posX/posY = getFocusX/Y, scaleFactor, scaleSpan
*/
SCALE_MOVE,
/**
* Two finger scale event end, fields: posX/posY = getFocusX/Y, scaleFactor, scaleSpan
*/
SCALE_END,
/**
* Scroll event
*/
SCROLL,
/**
* The user has performed a down MotionEvent and not performed a move or up yet. This event is commonly used to provide visual feedback to the user to let them know that their action has been recognized i.e. highlight an element.
*/
SHOWPRESS,
// Others
OUTSIDE,
IDLE}
private Type type = Type.IDLE;
private int pointerId;
private float posX;
private float posY;
private float deltaX;
private float deltaY;
// Used only with KEY* events
private int keyCode;
private String characters;
// Used only with SCALE* events
private float scaleFactor;
private float scaleSpan;
public TouchEvent()
{
set(Type.IDLE, 0f, 0f, 0f, 0f);
}
public TouchEvent(Type type, float x, float y, float deltax, float deltay)
{
set(type, x, y, deltax, deltay);
}
public void set(Type type)
{
this.type = type;
this.posX = 0f;
this.posY = 0f;
this.deltaX = 0f;
this.deltaY = 0f;
}
public void set(Type type, float x, float y, float deltax, float deltay)
{
this.type = type;
this.posX = x;
this.posY = y;
this.deltaX = deltax;
this.deltaY = deltay;
}
public Type getType()
{
return type;
}
public float getX()
{
return posX;
}
public float getY()
{
return posY;
}
public float getDeltaX()
{
return deltaX;
}
public float getDeltaY()
{
return deltaY;
}
public Vector2f getDelta()
{
return new Vector2f(deltaX,deltaY);
}
public int getPointerId()
{
return pointerId;
}
public void setPointerId(int pointerId)
{
this.pointerId = pointerId;
}
public int getKeyCode()
{
return keyCode;
}
public void setKeyCode(int keyCode)
{
this.keyCode = keyCode;
}
public String getCharacters()
{
return characters;
}
public void setCharacters(String characters)
{
this.characters = characters;
}
public float getScaleFactor()
{
return scaleFactor;
}
public void setScaleFactor(float scaleFactor)
{
this.scaleFactor = scaleFactor;
}
public float getScaleSpan()
{
return scaleSpan;
}
public void setScaleSpan(float scaleSpan)
{
this.scaleSpan = scaleSpan;
}
}

@ -35,6 +35,7 @@ package com.jme3.system;
import com.jme3.input.JoyInput; import com.jme3.input.JoyInput;
import com.jme3.input.KeyInput; import com.jme3.input.KeyInput;
import com.jme3.input.MouseInput; import com.jme3.input.MouseInput;
import com.jme3.input.TouchInput;
import com.jme3.renderer.Renderer; import com.jme3.renderer.Renderer;
/** /**
@ -125,6 +126,11 @@ public interface JmeContext {
*/ */
public JoyInput getJoyInput(); public JoyInput getJoyInput();
/**
* @return Touch device input implementation. May be null if not available.
*/
public TouchInput getTouchInput();
/** /**
* @return The timer for this context, or null if not created yet. * @return The timer for this context, or null if not created yet.
*/ */

@ -35,6 +35,7 @@ package com.jme3.system;
import com.jme3.input.JoyInput; import com.jme3.input.JoyInput;
import com.jme3.input.KeyInput; import com.jme3.input.KeyInput;
import com.jme3.input.MouseInput; import com.jme3.input.MouseInput;
import com.jme3.input.TouchInput;
import com.jme3.input.dummy.DummyKeyInput; import com.jme3.input.dummy.DummyKeyInput;
import com.jme3.input.dummy.DummyMouseInput; import com.jme3.input.dummy.DummyMouseInput;
import com.jme3.renderer.Renderer; import com.jme3.renderer.Renderer;
@ -173,6 +174,10 @@ public class NullContext implements JmeContext, Runnable {
return null; return null;
} }
public TouchInput getTouchInput() {
return null;
}
public void setTitle(String title) { public void setTitle(String title) {
} }
@ -222,5 +227,4 @@ public class NullContext implements JmeContext, Runnable {
return true; // Doesn't really matter if true or false. Either way return true; // Doesn't really matter if true or false. Either way
// RenderManager won't render anything. // RenderManager won't render anything.
} }
} }

Loading…
Cancel
Save