1 | package org.expeditee.gio.gesture;
|
---|
2 |
|
---|
3 | import java.util.HashMap;
|
---|
4 | import java.util.LinkedList;
|
---|
5 | import java.util.List;
|
---|
6 |
|
---|
7 | import org.expeditee.gio.gesture.data.GestureData;
|
---|
8 |
|
---|
9 | /**
|
---|
10 | * Class representing the abstract notion of input to Expeditee. Classes can implement
|
---|
11 | * the <code>GestureListener</code> interface and register themselves with the <code>InputManager</code>
|
---|
12 | * to receive notification of gestures, which are translated from the raw input (mouse/keyboard/touch/etc.).
|
---|
13 | *
|
---|
14 | * @author cts16
|
---|
15 | */
|
---|
16 | public class Gesture {
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * Class representing a type of gesture. Identified by a string name. Each name corresponds
|
---|
20 | * to a single GestureType object throughout the entire program, so type matching can be
|
---|
21 | * performed with the == operator. The GestureType object also is initialised with a
|
---|
22 | * GestureData type, which is the type of data that should come with gestures
|
---|
23 | * of that type.
|
---|
24 | *
|
---|
25 | * @author cts16
|
---|
26 | */
|
---|
27 | public static class GestureType
|
---|
28 | {
|
---|
29 | /** Static mapping retains reference to the single GestureType object per string. */
|
---|
30 | private static HashMap<String, GestureType> _registeredTypes = new HashMap<String, GestureType>();
|
---|
31 |
|
---|
32 | /** The name of this gesture type. */
|
---|
33 | private String _name;
|
---|
34 | /** The type of data that goes with gestures of this type. */
|
---|
35 | private Class<? extends GestureData> _dataClass;
|
---|
36 |
|
---|
37 | public static GestureType register(String name)
|
---|
38 | {
|
---|
39 | if (name == null || isRegistered(name)) return null;
|
---|
40 |
|
---|
41 | _registeredTypes.put(name, new GestureType(name, null));
|
---|
42 |
|
---|
43 | return get(name);
|
---|
44 | }
|
---|
45 |
|
---|
46 | public static boolean isRegistered(String name)
|
---|
47 | {
|
---|
48 | return (name != null) && (_registeredTypes.containsKey(name));
|
---|
49 | }
|
---|
50 |
|
---|
51 | public static GestureType get(String name)
|
---|
52 | {
|
---|
53 | if (name == null || !isRegistered(name)) return null;
|
---|
54 |
|
---|
55 | return _registeredTypes.get(name);
|
---|
56 | }
|
---|
57 |
|
---|
58 | private GestureType(String name, Class<? extends GestureData> dataClass)
|
---|
59 | {
|
---|
60 | _name = name;
|
---|
61 | _dataClass = dataClass;
|
---|
62 | }
|
---|
63 |
|
---|
64 | public String name()
|
---|
65 | {
|
---|
66 | return _name;
|
---|
67 | }
|
---|
68 |
|
---|
69 | public Class<? extends GestureData> dataClass()
|
---|
70 | {
|
---|
71 | return _dataClass;
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | /** The type of gesture that was performed. */
|
---|
76 | private GestureType _type;
|
---|
77 |
|
---|
78 | /** The specifics of the gesture (differs by gesture type). */
|
---|
79 | private GestureData _data;
|
---|
80 |
|
---|
81 | /** Whether or not the gesture was created by the program instead of an actual input event. */
|
---|
82 | private boolean _isRobotic = false;
|
---|
83 |
|
---|
84 | public Gesture(GestureType type, GestureData data)
|
---|
85 | {
|
---|
86 | assert(type != null);
|
---|
87 |
|
---|
88 | _type = type;
|
---|
89 | _data = data;
|
---|
90 | }
|
---|
91 |
|
---|
92 | public Gesture(Gesture other)
|
---|
93 | {
|
---|
94 | this(other._type, other._data);
|
---|
95 | setRobotic(other._isRobotic);
|
---|
96 | }
|
---|
97 |
|
---|
98 | public GestureType getType()
|
---|
99 | {
|
---|
100 | return _type;
|
---|
101 | }
|
---|
102 |
|
---|
103 | public GestureData getData()
|
---|
104 | {
|
---|
105 | return _data;
|
---|
106 | }
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * @return
|
---|
110 | * True if the gesture was generated by software, false if it came from
|
---|
111 | * an actual input event.
|
---|
112 | */
|
---|
113 | public boolean isRobotic()
|
---|
114 | {
|
---|
115 | return _isRobotic;
|
---|
116 | }
|
---|
117 |
|
---|
118 | public void setRobotic(boolean isRobotic)
|
---|
119 | {
|
---|
120 | _isRobotic = isRobotic;
|
---|
121 | }
|
---|
122 |
|
---|
123 | public Gesture clone()
|
---|
124 | {
|
---|
125 | return new Gesture(this);
|
---|
126 | }
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * Helper-method to create a list of gestures with a single gesture in it.
|
---|
130 | */
|
---|
131 | public static List<Gesture> single(GestureType type, GestureData data)
|
---|
132 | {
|
---|
133 | return single(new Gesture(type, data));
|
---|
134 | }
|
---|
135 |
|
---|
136 | /**
|
---|
137 | * Helper-method to create a list of gestures with a single gesture in it.
|
---|
138 | */
|
---|
139 | public static List<Gesture> single(Gesture gesture)
|
---|
140 | {
|
---|
141 | List<Gesture> gestures = new LinkedList<Gesture>();
|
---|
142 | gestures.add(gesture);
|
---|
143 | return gestures;
|
---|
144 | }
|
---|
145 | }
|
---|