From 67e7792be5cb33b2bcabf6174cec4dec2a807d1f Mon Sep 17 00:00:00 2001
From: "jason@long.name" <jason@long.name@d9718cc8-9f43-0410-858b-315f434eb58c>
Date: Thu, 16 May 2013 23:36:27 +0000
Subject: [PATCH] toolstroke: apply at end of mouse click, show drag preview

not completely there yet

git-svn-id: https://micropolis.googlecode.com/svn/trunk/micropolis-java@623 d9718cc8-9f43-0410-858b-315f434eb58c
---
 src/micropolisj/engine/ToolStroke.java | 41 ++++++++++++++++++++++++++
 src/micropolisj/gui/MainWindow.java    | 40 +++++++++++--------------
 2 files changed, 58 insertions(+), 23 deletions(-)

diff --git a/src/micropolisj/engine/ToolStroke.java b/src/micropolisj/engine/ToolStroke.java
index 089be12..e02cc16 100644
--- a/src/micropolisj/engine/ToolStroke.java
+++ b/src/micropolisj/engine/ToolStroke.java
@@ -16,6 +16,8 @@ public class ToolStroke
 	final MicropolisTool tool;
 	int xpos;
 	int ypos;
+	int xdest;
+	int ydest;
 
 	ToolStroke(Micropolis city, MicropolisTool tool, int xpos, int ypos)
 	{
@@ -23,6 +25,8 @@ public class ToolStroke
 		this.tool = tool;
 		this.xpos = xpos;
 		this.ypos = ypos;
+		this.xdest = xpos;
+		this.ydest = ypos;
 	}
 
 	public ToolResult apply()
@@ -80,6 +84,43 @@ public class ToolStroke
 		}
 	}
 
+	public void dragTo(int xdest, int ydest)
+	{
+		this.xdest = xdest;
+		this.ydest = ydest;
+	}
+
+	public java.awt.Rectangle getPreview()
+	{
+		int dx = getPreviewDx();
+		int dy = getPreviewDy();
+
+		int x = Math.min(xpos, xpos + dx);
+		int y = Math.min(ypos, ypos + dy);
+
+		if (tool.getWidth() >= 3) {
+			x--;
+		}
+		if (tool.getHeight() >= 3) {
+			y--;
+		}
+
+		return new java.awt.Rectangle(x, y,
+			Math.abs(dx), Math.abs(dy));
+	}
+
+	int getPreviewDx()
+	{
+		int sgn = xdest > xpos ? 1 : -1;
+		return (sgn + (xdest - xpos) / tool.getWidth()) * tool.getWidth();
+	}
+
+	int getPreviewDy()
+	{
+		int sgn = ydest > ypos ? 1 : -1;
+		return (sgn + (ydest - ypos) / tool.getHeight()) * tool.getWidth();
+	}
+
 	ToolResult apply3x3buildingTool(int xpos, int ypos, char tileBase)
 	{
 		int mapH = xpos - 1;
diff --git a/src/micropolisj/gui/MainWindow.java b/src/micropolisj/gui/MainWindow.java
index ac69843..02d3c65 100644
--- a/src/micropolisj/gui/MainWindow.java
+++ b/src/micropolisj/gui/MainWindow.java
@@ -905,9 +905,8 @@ public class MainWindow extends JFrame
 		notificationPane.showZoneStatus(engine, xpos, ypos, z);
 	}
 
-	// where the tool was first pressed
-	int origX;
-	int origY;
+	// used when a tool is being pressed
+	ToolStroke toolStroke;
 
 	// where the tool was last applied during the current drag
 	int lastX;
@@ -929,22 +928,22 @@ public class MainWindow extends JFrame
 
 		int x = ev.getX() / MicropolisDrawingArea.TILE_WIDTH;
 		int y = ev.getY() / MicropolisDrawingArea.TILE_HEIGHT;
-		lastX = x;
-		lastY = y;
-		origX = x;
-		origY = y;
 
-		applyCurrentTool(x, y, false);
+		this.toolStroke = currentTool.beginStroke(engine, x, y);
+		this.lastX = x;
+		this.lastY = y;
 	}
 
 	private void onToolUp(MouseEvent ev)
 	{
+		if (toolStroke != null) {
+			toolStroke.apply();
+			toolStroke = null;
+		}
 	}
 
 	private void onToolDrag(MouseEvent ev)
 	{
-		onToolHover(ev);
-
 		if (currentTool == null)
 			return;
 		if ((ev.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) == 0)
@@ -955,21 +954,16 @@ public class MainWindow extends JFrame
 		if (x == lastX && y == lastY)
 			return;
 
-		while (lastX != x || lastY != y)
-		{
-			if (Math.abs(lastX-x) >= Math.abs(lastY-y))
-			{
-				lastX += lastX < x ? 1 : -1;
-			}
-			else
-			{
-				lastY += lastY < y ? 1 : -1;
-			}
-			applyCurrentTool(lastX, lastY, true);	
+		if (toolStroke != null) {
+			toolStroke.dragTo(x, y);
+			drawingArea.setToolPreview(
+				toolStroke.getPreview(),
+				parseColor(strings.getString("tool."+currentTool.name()+".border"))
+				);
 		}
 
-		assert lastX == x;
-		assert lastY == y;
+		lastX = x;
+		lastY = y;
 	}
 
 	private void onToolHover(MouseEvent ev)