1/*
2 * Copyright (c) 2009-2010 jMonkeyEngine
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 *   notice, this list of conditions and the following disclaimer.
11 *
12 * * Redistributions in binary form must reproduce the above copyright
13 *   notice, this list of conditions and the following disclaimer in the
14 *   documentation and/or other materials provided with the distribution.
15 *
16 * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
17 *   may be used to endorse or promote products derived from this software
18 *   without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32package com.jme3.input.event;
33
34/**
35 * <code>TouchEvent</code> represents a single event from multi-touch input devices
36 * @author larynx
37 */
38public class TouchEvent extends InputEvent {
39
40    public enum Type {
41
42        /**
43         * Touch down event, fields: posX, posY, pressure
44         */
45        DOWN,
46        /**
47         * Move/Drag event, fields: posX, posY, deltaX, deltaY, pressure
48         */
49        MOVE,
50        /**
51         * Touch up event, fields: posX, posY, pressure
52         */
53        UP,
54        /**
55         * Virtual keyboard or hardware key event down, fields: keyCode, characters
56         */
57        KEY_DOWN,
58        /**
59         * Virtual keyboard or hardware key event up, fields: keyCode, characters
60         */
61        KEY_UP,
62        // Single finger gestures
63        FLING,
64        TAP,
65        DOUBLETAP,
66        LONGPRESSED,
67        // Two finger scale events
68        /**
69         * Two finger scale event start, fields: posX/posY = getFocusX/Y, scaleFactor, scaleSpan
70         */
71        SCALE_START,
72        /**
73         * Two finger scale event, fields: posX/posY = getFocusX/Y, scaleFactor, scaleSpan
74         */
75        SCALE_MOVE,
76        /**
77         * Two finger scale event end, fields: posX/posY = getFocusX/Y, scaleFactor, scaleSpan
78         */
79        SCALE_END,
80        /**
81         *  Scroll event
82         */
83        SCROLL,
84        /**
85         * 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.
86         */
87        SHOWPRESS,
88        // Others
89        OUTSIDE,
90        IDLE
91    }
92    private Type type = Type.IDLE;
93    private int pointerId;
94    private float posX;
95    private float posY;
96    private float deltaX;
97    private float deltaY;
98    private float pressure;
99
100    // Used only with KEY* events
101    private int keyCode;
102    private String characters;
103    // Used only with SCALE* events
104    private float scaleFactor;
105    private float scaleSpan;
106
107    public TouchEvent() {
108        set(Type.IDLE, 0f, 0f, 0f, 0f);
109    }
110
111    public TouchEvent(Type type, float x, float y, float deltax, float deltay) {
112        set(type, x, y, deltax, deltay);
113    }
114
115    public void set(Type type) {
116        set(type, 0f, 0f, 0f, 0f);
117    }
118
119    public void set(Type type, float x, float y, float deltax, float deltay) {
120        this.type = type;
121        this.posX = x;
122        this.posY = y;
123        this.deltaX = deltax;
124        this.deltaY = deltay;
125        pointerId = 0;
126        pressure = 0;
127        keyCode = 0;
128        scaleFactor = 0;
129        scaleSpan = 0;
130        characters = "";
131        consumed = false;
132    }
133
134    /**
135     * Returns the type of touch event.
136     *
137     * @return the type of touch event.
138     */
139    public Type getType() {
140        return type;
141    }
142
143    public float getX() {
144        return posX;
145    }
146
147    public float getY() {
148        return posY;
149    }
150
151    public float getDeltaX() {
152        return deltaX;
153    }
154
155    public float getDeltaY() {
156        return deltaY;
157    }
158
159    public float getPressure()
160    {
161        return pressure;
162    }
163
164    public void setPressure(float pressure)
165    {
166        this.pressure = pressure;
167    }
168
169    public int getPointerId()
170    {
171        return pointerId;
172    }
173
174    public void setPointerId(int pointerId) {
175        this.pointerId = pointerId;
176    }
177
178    public int getKeyCode() {
179        return keyCode;
180    }
181
182    public void setKeyCode(int keyCode) {
183        this.keyCode = keyCode;
184    }
185
186    public String getCharacters() {
187        return characters;
188    }
189
190    public void setCharacters(String characters) {
191        this.characters = characters;
192    }
193
194    public float getScaleFactor() {
195        return scaleFactor;
196    }
197
198    public void setScaleFactor(float scaleFactor) {
199        this.scaleFactor = scaleFactor;
200    }
201
202    public float getScaleSpan() {
203        return scaleSpan;
204    }
205
206    public void setScaleSpan(float scaleSpan) {
207        this.scaleSpan = scaleSpan;
208    }
209}
210