1 16 package com.buchuki.ensmer.input.event; 17 18 import java.awt.*; 19 import java.awt.event.*; 20 import com.buchuki.ensmer.*; 21 import com.buchuki.ensmer.input.*; 22 23 38 public class TranslateMouseMoveEvent implements InputProcessor { 39 40 48 public TranslateMouseMoveEvent(InputProcessor processor) { 49 this.processor = processor; 50 try { 51 mousePositioner = new Robot(); 52 } catch (AWTException e) { 53 e.printStackTrace(); 54 System.err.println("No way to reposition the pointer. Navigation" + 55 "will not work"); 56 } 57 } 58 59 65 public void processInput(EnsmerInputEvent event) { 66 if (event instanceof MouseMoveEvent) { 67 Point newPoint = ((MouseEvent) event.getInputEvent()).getPoint(); 68 if (lastInputRobotic) { 69 lastInputRobotic = false; 70 lastPoint = getCanvasCenter(); 71 return; 72 } 73 if (lastPoint != null) { 74 int dx = lastPoint.x - newPoint.x; 75 int dy = lastPoint.y - newPoint.y; 76 int adx = Math.abs(dx); 77 int ady = Math.abs(dy); 78 if (adx < 10 && ady < 10) { 79 return; 80 } 81 if (adx > ady) { processor.processInput(new HorizontalMouseMoveEvent(dx)); 83 } 84 else { processor.processInput(new VerticalMouseMoveEvent(dy)); 86 } 87 centerMouse(); 88 } 89 lastPoint = newPoint; 90 } 91 else { 92 processor.processInput(event); 93 } 94 } 95 96 100 public void resetProcessor() { 101 lastPoint = null; 102 } 103 104 108 private void centerMouse() { 109 Component component 110 = EnsmerManager.instance().getInterfaceManager().getCanvas(); 111 Point componentLoc = component.getLocationOnScreen(); 112 Point centre = getCanvasCenter(); 113 lastInputRobotic = true; 114 mousePositioner.mouseMove( 115 componentLoc.x + centre.x, componentLoc.y + centre.y); 116 } 117 118 124 private Point getCanvasCenter() { 125 Component component 126 = EnsmerManager.instance().getInterfaceManager().getCanvas(); 127 128 return new Point( 129 component.getSize().width / 2, 130 component.getSize().height / 2); 131 } 132 133 136 private InputProcessor processor; 137 138 141 private Robot mousePositioner; 142 143 146 private Point lastPoint; 147 148 151 private boolean lastInputRobotic = false; 152 } 153 154 | Popular Tags |