annotate java/src/uk/ac/qmul/eecs/ccmi/haptics/MouseHaptics.java @ 1:e3935c01cde2 tip

moved license of PdPersistenceManager to the beginning of the file
author Fiore Martin <f.martin@qmul.ac.uk>
date Tue, 08 Jul 2014 19:52:03 +0100
parents 78b7fc5391a2
children
rev   line source
f@0 1 /*
f@0 2 CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool
f@0 3
f@0 4 Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/)
f@0 5
f@0 6 This program is free software: you can redistribute it and/or modify
f@0 7 it under the terms of the GNU General Public License as published by
f@0 8 the Free Software Foundation, either version 3 of the License, or
f@0 9 (at your option) any later version.
f@0 10
f@0 11 This program is distributed in the hope that it will be useful,
f@0 12 but WITHOUT ANY WARRANTY; without even the implied warranty of
f@0 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f@0 14 GNU General Public License for more details.
f@0 15
f@0 16 You should have received a copy of the GNU General Public License
f@0 17 along with this program. If not, see <http://www.gnu.org/licenses/>.
f@0 18 */
f@0 19
f@0 20 package uk.ac.qmul.eecs.ccmi.haptics;
f@0 21
f@0 22 import java.awt.AWTException;
f@0 23 import java.awt.BasicStroke;
f@0 24 import java.awt.BorderLayout;
f@0 25 import java.awt.Color;
f@0 26 import java.awt.Graphics;
f@0 27 import java.awt.Graphics2D;
f@0 28 import java.awt.Robot;
f@0 29 import java.awt.Stroke;
f@0 30 import java.awt.event.InputEvent;
f@0 31 import java.awt.event.KeyEvent;
f@0 32 import java.awt.event.KeyListener;
f@0 33 import java.awt.event.MouseEvent;
f@0 34 import java.awt.event.MouseListener;
f@0 35 import java.awt.event.MouseMotionListener;
f@0 36 import java.awt.event.WindowEvent;
f@0 37 import java.awt.event.WindowFocusListener;
f@0 38 import java.awt.geom.Line2D;
f@0 39 import java.awt.geom.Point2D;
f@0 40 import java.io.IOException;
f@0 41 import java.util.BitSet;
f@0 42
f@0 43 import javax.swing.JFrame;
f@0 44 import javax.swing.JPanel;
f@0 45
f@0 46 import uk.ac.qmul.eecs.ccmi.gui.DiagramPanel;
f@0 47 import uk.ac.qmul.eecs.ccmi.main.DiagramEditorApp;
f@0 48 import uk.ac.qmul.eecs.ccmi.speech.NarratorFactory;
f@0 49
f@0 50 @SuppressWarnings("serial")
f@0 51 class MouseHaptics extends JPanel implements Haptics, KeyListener, MouseListener, MouseMotionListener {
f@0 52 static Haptics createInstance(HapticListener listener){
f@0 53 MouseHaptics instance = new MouseHaptics(listener);
f@0 54 try{
f@0 55 instance.initMouseHaptics();
f@0 56 return instance;
f@0 57 }catch(IOException ioe){
f@0 58 return null;
f@0 59 }
f@0 60 }
f@0 61
f@0 62 private MouseHaptics(HapticListener listener){
f@0 63 this.listener = listener;
f@0 64 solidStroke = new BasicStroke(EDGE_THICKNESS);
f@0 65 dottedStroke = new BasicStroke(EDGE_THICKNESS,
f@0 66 BasicStroke.CAP_ROUND,
f@0 67 BasicStroke.JOIN_ROUND,
f@0 68 0.0f,
f@0 69 new float[]{1.0f,30.0f},
f@0 70 0.0f);
f@0 71 dashedStroke = new BasicStroke(EDGE_THICKNESS,
f@0 72 BasicStroke.CAP_ROUND,
f@0 73 BasicStroke.JOIN_ROUND,
f@0 74 0.0f,
f@0 75 new float[]{80.0f,50.0f},
f@0 76 0.0f);
f@0 77 }
f@0 78
f@0 79 @Override
f@0 80 public void paintComponent(Graphics g){
f@0 81 super.paintComponent(g);
f@0 82
f@0 83 Graphics2D g2 = (Graphics2D)g;
f@0 84
f@0 85 Stroke oldStroke = g2.getStroke();
f@0 86 // draw edges
f@0 87 g2.setColor(EDGE_COLOR);
f@0 88 for(Edge e : listSupport.getCurrentEdges()){
f@0 89 switch(e.stipplePattern){
f@0 90 case Edge.SOLID_LINE :
f@0 91 g2.setStroke(solidStroke);break;
f@0 92 case Edge.DOTTED_LINE :
f@0 93 g2.setStroke(dottedStroke);break;
f@0 94 case Edge.DASHED_LINE :
f@0 95 g2.setStroke(dashedStroke);break;
f@0 96 }
f@0 97 for(int i=0; i< e.adjMatrix.length; i++){
f@0 98 BitSet adj = e.adjMatrix[i];
f@0 99 for (int j = adj.nextSetBit(0); j >= 0; j = adj.nextSetBit(j+1)) {
f@0 100 g2.drawLine((int)e.xs[i], (int)e.ys[i], (int)e.xs[j], (int)e.ys[j]);
f@0 101 }
f@0 102 }
f@0 103 }
f@0 104
f@0 105 g2.setStroke(oldStroke);
f@0 106 // draw nodes
f@0 107 g2.setColor(NODE_COLOR);
f@0 108 for(Node n : listSupport.getCurrentNodes()){
f@0 109 g2.fillOval((int)(n.x - NODE_DIAMETER/2), (int)(n.y - NODE_DIAMETER/2), NODE_DIAMETER, NODE_DIAMETER);
f@0 110 }
f@0 111 }
f@0 112
f@0 113 @Override
f@0 114 public void run() {}
f@0 115
f@0 116 void initMouseHaptics() throws IOException {
f@0 117 hapticFrame = new JFrame("Haptic Frame");
f@0 118 try {
f@0 119 robot = new Robot();
f@0 120 } catch (AWTException e) {
f@0 121 throw new IOException(e);
f@0 122 }
f@0 123 /* this is necessary to make the window screen size. it doens't work with the default layout */
f@0 124 setLayout(new BorderLayout());
f@0 125
f@0 126 setBackground(Color.black);
f@0 127
f@0 128 listSupport = new HapticListSupport();
f@0 129
f@0 130 hapticFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
f@0 131 hapticFrame.addWindowFocusListener(new WindowFocusListener(){
f@0 132 @Override
f@0 133 public void windowGainedFocus(WindowEvent arg0) {
f@0 134 NarratorFactory.getInstance().speak("Haptic window Focused");
f@0 135 }
f@0 136
f@0 137 @Override
f@0 138 public void windowLostFocus(WindowEvent arg0) {}
f@0 139 });
f@0 140 hapticFrame.setContentPane(this);
f@0 141 hapticFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
f@0 142 hapticFrame.setUndecorated(true);
f@0 143 hapticFrame.addKeyListener(this);
f@0 144 addMouseMotionListener(this);
f@0 145 addMouseListener(this);
f@0 146 hapticFrame.pack();
f@0 147 }
f@0 148
f@0 149 @Override
f@0 150 public void addNewDiagram(String diagramName) {
f@0 151 listSupport.addNewDiagram(diagramName);
f@0 152 repaint();
f@0 153 }
f@0 154
f@0 155 @Override
f@0 156 public void switchDiagram(String diagramName) {
f@0 157 listSupport.switchDiagram(diagramName);
f@0 158 repaint();
f@0 159 }
f@0 160
f@0 161 @Override
f@0 162 public void removeDiagram(String diagramNameToRemove,
f@0 163 String diagramNameOfNext) {
f@0 164 listSupport.removeDiagram(diagramNameToRemove, diagramNameOfNext);
f@0 165 repaint();
f@0 166 }
f@0 167
f@0 168 @Override
f@0 169 public void addNode(double x, double y, int nodeHashCode, String diagramName) {
f@0 170 Node n = new Node(x,y,nodeHashCode, 0);
f@0 171 listSupport.addNode(n,diagramName);
f@0 172 repaint();
f@0 173 }
f@0 174
f@0 175 @Override
f@0 176 public void removeNode(int nodeHashCode, String diagramName) {
f@0 177 listSupport.removeNode(nodeHashCode, diagramName);
f@0 178 repaint();
f@0 179 }
f@0 180
f@0 181 @Override
f@0 182 public void moveNode(double x, double y, int nodeHashCode,
f@0 183 String diagramName) {
f@0 184 Node n = listSupport.getNode(nodeHashCode, diagramName);
f@0 185 n.x = x;
f@0 186 n.y = y;
f@0 187 repaint();
f@0 188 }
f@0 189
f@0 190 @Override
f@0 191 public void addEdge(int edgeHashCode, double[] xs, double[] ys,
f@0 192 BitSet[] adjMatrix, int nodeStart, int stipplePattern,
f@0 193 Line2D attractLine, String diagramName) {
f@0 194 // find the mid point of the line of attraction
f@0 195 double pX = Math.min(attractLine.getX1(), attractLine.getX2());
f@0 196 double pY = Math.min(attractLine.getY1(), attractLine.getY2());
f@0 197 pX += Math.abs(attractLine.getX1() - attractLine.getX2())/2;
f@0 198 pY += Math.abs(attractLine.getY1() - attractLine.getY2())/2;
f@0 199 Edge e = new Edge(edgeHashCode,0, xs, ys,
f@0 200 adjMatrix, nodeStart, stipplePattern,
f@0 201 pX, pY);
f@0 202 listSupport.addEdge(e, diagramName);
f@0 203 repaint();
f@0 204 }
f@0 205
f@0 206 @Override
f@0 207 public void updateEdge(int edgeHashCode, double[] xs, double[] ys,
f@0 208 BitSet[] adjMatrix, int nodeStart, Line2D attractLine,
f@0 209 String diagramName) {
f@0 210 Edge e = listSupport.getEdge(edgeHashCode, diagramName);
f@0 211 e.xs = xs;
f@0 212 e.ys = ys;
f@0 213 e.adjMatrix = adjMatrix;
f@0 214 e.nodeStart = nodeStart;
f@0 215 repaint();
f@0 216 }
f@0 217
f@0 218 @Override
f@0 219 public void removeEdge(int edgeHashCode, String diagramName) {
f@0 220 listSupport.removeEdge(edgeHashCode, diagramName);
f@0 221 repaint();
f@0 222 }
f@0 223
f@0 224 @Override
f@0 225 public void attractTo(int elementHashCode) {
f@0 226
f@0 227 }
f@0 228
f@0 229 @Override
f@0 230 public void pickUp(int elementHashCode) {
f@0 231 // needed to change the status of the haptic device. not needed here
f@0 232 }
f@0 233
f@0 234 @Override
f@0 235 public boolean isAlive() {
f@0 236 return false;
f@0 237 }
f@0 238
f@0 239 @Override
f@0 240 public void setVisible(boolean visible){
f@0 241 hapticFrame.setVisible(visible);
f@0 242 }
f@0 243
f@0 244 @Override
f@0 245 public void dispose() {
f@0 246 // no resources to free up
f@0 247 }
f@0 248
f@0 249 @Override
f@0 250 public void keyPressed(KeyEvent evt) {
f@0 251 DiagramPanel panel = DiagramEditorApp.getFrame().getActiveTab();
f@0 252 if(panel == null)
f@0 253 DiagramEditorApp.getFrame().editorTabbedPane.dispatchEvent(evt);
f@0 254 else
f@0 255 panel.getTree().dispatchEvent(evt);
f@0 256 }
f@0 257
f@0 258 @Override
f@0 259 public void keyReleased(KeyEvent evt) {
f@0 260 keyPressed(evt);
f@0 261 }
f@0 262
f@0 263 @Override
f@0 264 public void keyTyped(KeyEvent evt) {
f@0 265 keyPressed(evt);
f@0 266 }
f@0 267
f@0 268 @Override
f@0 269 public void mouseDragged(MouseEvent evt) {
f@0 270 /* priority to nodes: check if the mouse pointer is inside a circle centred on *
f@0 271 * the node and with radius equal to NODE_RADIUS. If the mouse pointer is within *
f@0 272 * the radius of two or more nodes then the closest is picked up. The command is *
f@0 273 * executed once when the node is touched. In order have it executed again the *
f@0 274 * used must get away from it and hover above it again */
f@0 275 Point2D p = evt.getPoint();
f@0 276 if(elementPickedUp){
f@0 277 draggedDistance += evt.getPoint().distance(lastDragPoint);
f@0 278 if( draggedDistance > CHAIN_SOUND_INTERVAL){
f@0 279 listener.executeCommand(HapticListenerCommand.PLAY_SOUND, 3, 0,0,0,0);
f@0 280 draggedDistance = 0;
f@0 281 }
f@0 282 lastDragPoint = evt.getPoint();
f@0 283 }
f@0 284
f@0 285 Node hoveredNode = null;
f@0 286 for(Node n : listSupport.getCurrentNodes()){
f@0 287 double distance = p.distance(n.x, n.y);
f@0 288 if( distance < NODE_HOVER_DIST){
f@0 289 if(hoveredNode == null || distance < p.distance(hoveredNode.x,hoveredNode.y) ){
f@0 290 hoveredNode = n;
f@0 291 }
f@0 292 }
f@0 293 }
f@0 294 if(hoveredNode != null && hoveredNode != lastTouchedNode){
f@0 295 listener.executeCommand(HapticListenerCommand.PLAY_ELEMENT_SPEECH, hoveredNode.diagramId, 0, 0, 0, 0);
f@0 296 lastTouchedNode = hoveredNode;
f@0 297 lastTouchedEdge = null;
f@0 298 return;
f@0 299 }
f@0 300 lastTouchedNode = hoveredNode;
f@0 301 /* if hovering inside a node neither send the command nor take edges into account */
f@0 302 if(hoveredNode != null)
f@0 303 return;
f@0 304
f@0 305 /* if no node is being touched, check the edges out. */
f@0 306 Edge hoveredEdge = null;
f@0 307 Line2D line = new Line2D.Double();
f@0 308 /* look at all edges */
f@0 309 for(Edge e : listSupport.getCurrentEdges()){
f@0 310 /* look at all edge's lines */
f@0 311 for(int i=0; i< e.adjMatrix.length; i++){
f@0 312 BitSet adj = e.adjMatrix[i];
f@0 313 for (int j = adj.nextSetBit(0); j >= 0; j = adj.nextSetBit(j+1)) {
f@0 314 line.setLine(e.xs[i], e.ys[i], e.xs[j], e.ys[j]);
f@0 315 if(lastTouchedEdge != e && line.ptSegDist(p)<EDGE_HOVER_DIST){
f@0 316 hoveredEdge = e;
f@0 317 listener.executeCommand(HapticListenerCommand.PLAY_ELEMENT_SPEECH, hoveredEdge.diagramId, 0, 0, 0, 0);
f@0 318 lastTouchedEdge = hoveredEdge;
f@0 319 return;
f@0 320 }
f@0 321 }
f@0 322 }
f@0 323 }
f@0 324 lastTouchedEdge = hoveredEdge;
f@0 325 }
f@0 326
f@0 327 @Override
f@0 328 public void mouseMoved(MouseEvent evt) {
f@0 329 /* right click on a graphic tablet used as mouse has the effect of nullifying the *
f@0 330 * dragging. That is there is no right click but rather it's like if you untouch *
f@0 331 * the tablet. In order to address this a robot is used in order to re-leftclick each time *
f@0 332 * the right click is pressed. In this way we assure that the left click is always held *
f@0 333 * and therefore the mouse is always dragging rather than moving */
f@0 334 if(mustReclick){
f@0 335 mustReclick = false;
f@0 336 reclickedAfterMove = true; // this is to make this.mousePressed() have no effect, when it's the robot clicking
f@0 337 robot.mousePress(InputEvent.BUTTON1_MASK);
f@0 338 }
f@0 339 }
f@0 340
f@0 341 @Override
f@0 342 public void mousePressed(MouseEvent evt) {
f@0 343 /* by clicking on the object, its name is stated by the TTS. *
f@0 344 * Much as what happens when hovering on it with the button pressed */
f@0 345 if(evt.getButton() == MouseEvent.BUTTON1){
f@0 346 if(reclickedAfterMove){
f@0 347 reclickedAfterMove = false;
f@0 348 return;
f@0 349 }
f@0 350 lastTouchedEdge = null; // these two fields are used with dragging to avoid repeating
f@0 351 lastTouchedNode = null;
f@0 352
f@0 353 mouseDragged(evt);//left clicking on an object is like to drag on it
f@0 354 return;
f@0 355 }
f@0 356 /* button 3 (right click) is for moving the objects (picking up and dropping) */
f@0 357 if(evt.getButton() != MouseEvent.BUTTON3)
f@0 358 return;
f@0 359 if(!secondClick){ // clicked for the first time: pick up the node or edge
f@0 360 Point2D p = evt.getPoint();
f@0 361 Node hoveredNode = null;
f@0 362 for(Node n : listSupport.getCurrentNodes()){
f@0 363 double distance = p.distance(n.x, n.y);
f@0 364 if( distance < NODE_HOVER_DIST){
f@0 365 if(hoveredNode == null || distance < p.distance(hoveredNode.x,hoveredNode.y) ){
f@0 366 hoveredNode = n;
f@0 367 }
f@0 368 }
f@0 369 }
f@0 370 if(hoveredNode != null){ // clicked on a node
f@0 371 listener.executeCommand(HapticListenerCommand.PICK_UP, hoveredNode.diagramId, 0, 0, 0, 0);
f@0 372 secondClick = true;
f@0 373 startX = evt.getX();
f@0 374 startY = evt.getY();
f@0 375 pickedUpElementId = hoveredNode.diagramId;
f@0 376 mustReclick = true;
f@0 377 /* sets the variables for the chain sound when dragging the element around */
f@0 378 elementPickedUp = true;
f@0 379 lastDragPoint = evt.getPoint();
f@0 380 return;
f@0 381 }
f@0 382 /* if no node is being touched, check the edges out. */
f@0 383 Line2D line = new Line2D.Double();
f@0 384 /* look at all edges */
f@0 385 for(Edge e : listSupport.getCurrentEdges()){
f@0 386 /* look at all edge's lines */
f@0 387 for(int i=0; i< e.adjMatrix.length; i++){
f@0 388 BitSet adj = e.adjMatrix[i];
f@0 389 for (int j = adj.nextSetBit(0); j >= 0; j = adj.nextSetBit(j+1)) {
f@0 390 line.setLine(e.xs[i], e.ys[i], e.xs[j], e.ys[j]);
f@0 391 if(/*lastTouchedEdge != e && */line.ptSegDist(p)<EDGE_HOVER_DIST){
f@0 392 listener.executeCommand(HapticListenerCommand.PICK_UP, e.diagramId, 0, 0, 0, 0);
f@0 393 secondClick = true;
f@0 394 startX = evt.getX();
f@0 395 startY = evt.getY();
f@0 396 pickedUpElementId = e.diagramId;
f@0 397 mustReclick = true;
f@0 398 /* sets the variables for the chain sound when dragging the element around */
f@0 399 elementPickedUp = true;
f@0 400 lastDragPoint = evt.getPoint();
f@0 401 return;
f@0 402 }
f@0 403 }
f@0 404 }
f@0 405 }
f@0 406 }else{
f@0 407 listener.executeCommand(HapticListenerCommand.MOVE, pickedUpElementId, evt.getX(), evt.getY(), startX, startY);
f@0 408 elementPickedUp = false;
f@0 409 secondClick = false;
f@0 410 }
f@0 411 mustReclick = true;
f@0 412 }
f@0 413
f@0 414 @Override
f@0 415 public void mouseEntered(MouseEvent evt) {}
f@0 416
f@0 417 @Override
f@0 418 public void mouseExited(MouseEvent evt) {}
f@0 419
f@0 420 @Override
f@0 421 public void mouseClicked(MouseEvent evt) {}
f@0 422
f@0 423 @Override
f@0 424 public void mouseReleased(MouseEvent evt) {}
f@0 425
f@0 426 private HapticListener listener;
f@0 427 private JFrame hapticFrame;
f@0 428 private HapticListSupport listSupport;
f@0 429 private Stroke solidStroke;
f@0 430 private Stroke dashedStroke;
f@0 431 private Stroke dottedStroke;
f@0 432 private Node lastTouchedNode;
f@0 433 private Edge lastTouchedEdge;
f@0 434 private Robot robot;
f@0 435 private boolean mustReclick;
f@0 436 private boolean reclickedAfterMove;
f@0 437 private boolean secondClick;
f@0 438 private boolean elementPickedUp;
f@0 439 private Point2D lastDragPoint;
f@0 440 private double draggedDistance;
f@0 441 private int startX;
f@0 442 private int startY;
f@0 443 private int pickedUpElementId;
f@0 444 private static final Color EDGE_COLOR = Color.RED;
f@0 445 private static final Color NODE_COLOR = Color.WHITE;
f@0 446 private static final int EDGE_THICKNESS = 26;//2;
f@0 447 private static final int NODE_DIAMETER = 50;//10;
f@0 448 private static final int NODE_HOVER_DIST = 25;
f@0 449 private static final int EDGE_HOVER_DIST = 13;
f@0 450 private static final double CHAIN_SOUND_INTERVAL = 150;
f@0 451
f@0 452 }