00001 package edu.stanford.hci.r3.events.handlers;
00002
00003 import edu.stanford.hci.r3.events.EventHandler;
00004 import edu.stanford.hci.r3.events.PenEvent;
00005 import edu.stanford.hci.r3.units.Units;
00006 import edu.stanford.hci.r3.units.coordinates.PercentageCoordinates;
00007
00019 public abstract class GestureHandler extends EventHandler {
00020
00026 public static enum GestureDirection {
00027 E,
00028 N,
00029 NE,
00030 NW,
00031 S,
00032 SE,
00033 SW,
00034 W
00035
00036 }
00037
00038 private double dx;
00039
00040 private double dy;
00041
00042 private Units firstPercentageX;
00043
00044 private Units firstPercentageY;
00045
00050 private int gestureThreshold = 4;
00051
00052 private Units lastPercentageX;
00053
00054 private Units lastPercentageY;
00055
00056 private int numSamples;
00057
00061 public void handleEvent(PenEvent event) {
00062 PercentageCoordinates percentageLocation = event.getPercentageLocation();
00063 if (event.isTypePenDown()) {
00064 firstPercentageX = percentageLocation.getActualValueInXDirection();
00065 firstPercentageY = percentageLocation.getActualValueInYDirection();
00066 numSamples = 0;
00067 } else if (event.isTypePenUp()) {
00068
00069
00070 if (numSamples > gestureThreshold) {
00071 dx = lastPercentageX.getValue() - firstPercentageX.getValue();
00072 dy = lastPercentageY.getValue() - firstPercentageY.getValue();
00073
00074
00075 double theta = Math.atan2(dy, dx);
00076
00077
00078
00079
00080 double testTheta = -theta + Math.PI / 8;
00081
00082 final double PI_4 = Math.PI / 4;
00083 final double PI_2 = Math.PI / 2;
00084 final double THREE_PI_4 = 3 * Math.PI / 4;
00085
00086
00087 if (testTheta > 0) {
00088 if (testTheta < PI_2) {
00089 if (testTheta < PI_4) {
00090 handleMark(event, GestureDirection.E);
00091 } else {
00092 handleMark(event, GestureDirection.NE);
00093 }
00094 } else {
00095 if (testTheta < THREE_PI_4) {
00096 handleMark(event, GestureDirection.N);
00097 } else if (testTheta < Math.PI) {
00098 handleMark(event, GestureDirection.NW);
00099 } else {
00100 handleMark(event, GestureDirection.W);
00101 }
00102 }
00103 } else {
00104 if (testTheta > -PI_2) {
00105 if (testTheta > -PI_4) {
00106 handleMark(event, GestureDirection.SE);
00107 } else {
00108 handleMark(event, GestureDirection.S);
00109 }
00110 } else {
00111 if (testTheta > -THREE_PI_4) {
00112 handleMark(event, GestureDirection.SW);
00113 } else {
00114 handleMark(event, GestureDirection.W);
00115 }
00116 }
00117
00118 }
00119
00120 }
00121 } else {
00122 numSamples++;
00123 lastPercentageX = percentageLocation.getActualValueInXDirection();
00124 lastPercentageY = percentageLocation.getActualValueInYDirection();
00125 }
00126 }
00127
00132 public abstract void handleMark(PenEvent e, GestureDirection dir);
00133
00134
00135
00136
00137
00138
00139 public String toString() {
00140 return "GestureHandler";
00141 }
00142 }