package cz.pohlreichlukas.ludumdare30.input;

import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseEvent;
import java.awt.Robot;
import java.awt.AWTException;
import java.awt.Point;

import cz.pohlreichlukas.ludumdare30.GamePane;

public class InputListener implements MouseListener, MouseMotionListener {

    private boolean leftIsDown;
    private boolean rightIsDown;
    private int lastMouseX;
    private int lastMouseY;
    private int mouseX;
    private int mouseY;
    private GamePane pane;

    public InputListener( GamePane pane ) {
        this.leftIsDown = false;
        this.rightIsDown = false;
        this.lastMouseX = this.lastMouseY = 0;
        this.mouseX = this.mouseY = 0;
        this.pane = pane;
    }

    public void mousePressed( MouseEvent e ) {
        if ( e.getButton() == MouseEvent.BUTTON1 ) {
            this.leftIsDown = true;
        }
        if ( e.getButton() == MouseEvent.BUTTON2 ) {
            this.rightIsDown = true;
        }
    }

    public void mouseReleased( MouseEvent e ) { 
        if ( e.getButton() == MouseEvent.BUTTON1 ) {
            this.leftIsDown = false;
        }
        if ( e.getButton() == MouseEvent.BUTTON2 ) {
            this.rightIsDown = false;
        }
    }

    public void mouseMoved( MouseEvent e ) {
        this.lastMouseX = this.mouseX;
        this.lastMouseY = this.mouseY;
        this.mouseX = e.getX();
        this.mouseY = e.getY();
    }

    public void mouseDragged( MouseEvent e ) {
        this.mouseMoved( e );
    }

    public void mouseExited( MouseEvent e ) {
        try {
            Robot r = new Robot();
            Point p = this.pane.getLocationOnScreen();
            int panePosX = (int) p.getX();
            int panePosY = (int) p.getY();
            r.mouseMove( this.lastMouseX + panePosX, this.lastMouseY + panePosY );
        } catch ( AWTException a ) {
            a.printStackTrace();
        }
    }

    public boolean isLeftDown() {
        return this.leftIsDown;
    }

    public boolean isRightDown() {
        return this.rightIsDown;
    }

    public int getMouseX() {
        return this.mouseX;
    }

    public int getMouseY() {
        return this.mouseY;
    }

    public int getLastMouseX() {
        return this.lastMouseX;
    }

    public int getLastMouseY() {
        return this.lastMouseY;
    }

    public void mouseClicked( MouseEvent e ) {}
    public void mouseEntered( MouseEvent e ) {}
}

@@ -549,16 +549,41 @@
 ne pane;
+%0A    private Robot robot;
 %0A%0A    pu
@@ -795,24 +795,156 @@
 ane = pane;%0A
+        try %7B%0A            this.robot = new Robot();%0A        %7D catch ( AWTException e ) %7B%0A            e.printStackTrace();%0A        %7D%0A
     %7D%0A%0A    p
@@ -1776,61 +1776,8 @@
 ) %7B%0A
-        try %7B%0A            Robot r = new Robot();%0A    
     
@@ -1823,36 +1823,32 @@
 reen();%0A        
-    
 int panePosX = (
@@ -1866,28 +1866,24 @@
 ();%0A        
- 
-   
 int panePosY
@@ -1909,21 +1909,26 @@
         
-    r
+this.robot
 .mouseMo
@@ -1993,88 +1993,8 @@
  );%0A
-        %7D catch ( AWTException a ) %7B%0A            a.printStackTrace();%0A        %7D%0A
     

Create robot in constructor