package com.servegame.abendstern.tunnelblick.backend;

import java.awt.event.*;

/**
 * Input driver which translates mouse motion into pointer and/or body
 * control.
 *
 * The driver is modal in that, at any given time, it is controlling EITHER
 * pointer zero or body 0. The mouse whell is used to select mode; up shifts to
 * pointer mode, down to body mode.
 */
public final class ModalMouseMotionInputDriver
extends AWTInputDriver
implements MouseMotionListener, MouseWheelListener {
  private boolean pointerMode = true;
  private GameManager manager = null;

  /**
   * Sets the mode to use. True is pointer mode, false is body mode.
   */
  public void setPointerMode(boolean pointerMode) {
    this.pointerMode = pointerMode;
  }

  public void installInto(GameManager man) {
    manager = man;
    man.getCanvas().addMouseMotionListener(this);
    man.getFrame ().addMouseWheelListener (this);
  }

  public void mouseMoved(MouseEvent evt) {
    java.awt.Component canvas = manager.getCanvas();
    float x = evt.getX() / (float)canvas.getWidth();
    float y = evt.getY() / (float)canvas.getHeight() * manager.vheight();
    //OpenGL's Y axis is upside-down
    y = manager.vheight() - y;

    InputStatus is = manager.getSharedInputStatus();

    if (pointerMode) {
      is.pointers[0][0] = x;
      is.pointers[0][1] = y;
      enqueue(new InputEvent(InputEvent.TYPE_POINTER_MOVEMENT, 0, x, y));
    } else {
      is.bodies[0] = x;
      enqueue(new InputEvent(InputEvent.TYPE_BODY_MOVEMENT, 0, x, y));
    }
  }

  public void mouseDragged(MouseEvent evt) {
    mouseMoved(evt);
  }

  public void mouseWheelMoved(MouseWheelEvent evt) {
    pointerMode = (evt.getWheelRotation() < 0);
  }
}

@@ -1249,16 +1249,47 @@
 tus();%0A%0A
+    synchronized (manager) %7B%0A  
     if (
@@ -1299,24 +1299,26 @@
 nterMode) %7B%0A
+  
       is.poi
@@ -1336,24 +1336,26 @@
  = x;%0A      
+  
 is.pointers%5B
@@ -1365,16 +1365,18 @@
 1%5D = y;%0A
+  
       en
@@ -1437,24 +1437,26 @@
  0, x, y));%0A
+  
     %7D else %7B
@@ -1462,16 +1462,18 @@
 %7B%0A      
+  
 is.bodie
@@ -1478,24 +1478,26 @@
 ies%5B0%5D = x;%0A
+  
       enqueu
@@ -1551,24 +1551,32 @@
  0, x, y));%0A
+      %7D%0A
     %7D%0A  %7D%0A%0A 

Fix ModalMouseMotionInputDriver not synchronising on the manager when mutating state.