1 11 12 package org.eclipse.ui.internal.gestures; 13 14 import org.eclipse.swt.graphics.Rectangle; 15 import org.eclipse.swt.widgets.Display; 16 import org.eclipse.swt.widgets.Shell; 17 import org.eclipse.ui.keys.SWTKeySupport; 18 19 public final class GestureSupport { 20 21 public static void main(String [] args) { 22 final int HEIGHT = 300; 23 final int WIDTH = 400; 24 Display display = new Display(); 25 Rectangle bounds = display.getBounds(); 26 Shell shell = new Shell(display); 27 28 if (bounds.height >= HEIGHT && bounds.width >= WIDTH) 29 shell.setBounds( 30 (bounds.x + bounds.width - WIDTH) / 2, 31 (bounds.y + bounds.height - HEIGHT) / 2, 32 WIDTH, 33 HEIGHT); 34 35 shell.setText(GestureSupport.class.getName()); 36 shell.open(); 37 Capture capture = new Capture(); 38 39 capture.addCaptureListener(new ICaptureListener() { 40 public void capture(CaptureEvent captureEvent) { 41 System.out.println("Pen: " + captureEvent.getPen() + " Key Stroke: " + SWTKeySupport.convertAcceleratorToKeyStroke(captureEvent.getData()) + " Points: " + captureEvent.getPoints().length + " Gesture: " + recognize(captureEvent.getPoints(), 20)); } 46 }); 47 48 capture.setControl(shell); 49 50 while (!shell.isDisposed()) 51 if (!display.readAndDispatch()) 52 display.sleep(); 53 54 display.dispose(); 55 } 56 57 public static String recognize(Point[] points, int sensitivity) { 58 char stroke = '\0'; 59 StringBuffer sequence = new StringBuffer (); 60 int x0 = 0; 61 int y0 = 0; 62 63 for (int i = 0; i < points.length; i++) { 64 Point point = points[i]; 65 66 if (i == 0) { 67 x0 = point.getX(); 68 y0 = point.getY(); 69 continue; 70 } 71 72 int x1 = point.getX(); 73 int y1 = point.getY(); 74 int dx = (x1 - x0) / sensitivity; 75 int dy = (y1 - y0) / sensitivity; 76 77 if (dx != 0 || dy != 0) { 78 if (dx > 0 && stroke != 'E') 79 sequence.append(stroke = 'E'); 80 else if (dx < 0 && stroke != 'W') 81 sequence.append(stroke = 'W'); 82 else if (dy > 0 && stroke != 'S') 83 sequence.append(stroke = 'S'); 84 else if (dy < 0 && stroke != 'N') 85 sequence.append(stroke = 'N'); 86 87 x0 = x1; 88 y0 = y1; 89 } 90 } 91 92 return sequence.toString(); 93 } 94 95 private GestureSupport() { 96 } 97 } | Popular Tags |