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
This commit is contained in:
parent
c4fe7f0d34
commit
67e7792be5
2 changed files with 58 additions and 23 deletions
|
@ -16,6 +16,8 @@ public class ToolStroke
|
||||||
final MicropolisTool tool;
|
final MicropolisTool tool;
|
||||||
int xpos;
|
int xpos;
|
||||||
int ypos;
|
int ypos;
|
||||||
|
int xdest;
|
||||||
|
int ydest;
|
||||||
|
|
||||||
ToolStroke(Micropolis city, MicropolisTool tool, int xpos, int ypos)
|
ToolStroke(Micropolis city, MicropolisTool tool, int xpos, int ypos)
|
||||||
{
|
{
|
||||||
|
@ -23,6 +25,8 @@ public class ToolStroke
|
||||||
this.tool = tool;
|
this.tool = tool;
|
||||||
this.xpos = xpos;
|
this.xpos = xpos;
|
||||||
this.ypos = ypos;
|
this.ypos = ypos;
|
||||||
|
this.xdest = xpos;
|
||||||
|
this.ydest = ypos;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ToolResult apply()
|
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)
|
ToolResult apply3x3buildingTool(int xpos, int ypos, char tileBase)
|
||||||
{
|
{
|
||||||
int mapH = xpos - 1;
|
int mapH = xpos - 1;
|
||||||
|
|
|
@ -905,9 +905,8 @@ public class MainWindow extends JFrame
|
||||||
notificationPane.showZoneStatus(engine, xpos, ypos, z);
|
notificationPane.showZoneStatus(engine, xpos, ypos, z);
|
||||||
}
|
}
|
||||||
|
|
||||||
// where the tool was first pressed
|
// used when a tool is being pressed
|
||||||
int origX;
|
ToolStroke toolStroke;
|
||||||
int origY;
|
|
||||||
|
|
||||||
// where the tool was last applied during the current drag
|
// where the tool was last applied during the current drag
|
||||||
int lastX;
|
int lastX;
|
||||||
|
@ -929,22 +928,22 @@ public class MainWindow extends JFrame
|
||||||
|
|
||||||
int x = ev.getX() / MicropolisDrawingArea.TILE_WIDTH;
|
int x = ev.getX() / MicropolisDrawingArea.TILE_WIDTH;
|
||||||
int y = ev.getY() / MicropolisDrawingArea.TILE_HEIGHT;
|
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)
|
private void onToolUp(MouseEvent ev)
|
||||||
{
|
{
|
||||||
|
if (toolStroke != null) {
|
||||||
|
toolStroke.apply();
|
||||||
|
toolStroke = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onToolDrag(MouseEvent ev)
|
private void onToolDrag(MouseEvent ev)
|
||||||
{
|
{
|
||||||
onToolHover(ev);
|
|
||||||
|
|
||||||
if (currentTool == null)
|
if (currentTool == null)
|
||||||
return;
|
return;
|
||||||
if ((ev.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) == 0)
|
if ((ev.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) == 0)
|
||||||
|
@ -955,21 +954,16 @@ public class MainWindow extends JFrame
|
||||||
if (x == lastX && y == lastY)
|
if (x == lastX && y == lastY)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
while (lastX != x || lastY != y)
|
if (toolStroke != null) {
|
||||||
{
|
toolStroke.dragTo(x, y);
|
||||||
if (Math.abs(lastX-x) >= Math.abs(lastY-y))
|
drawingArea.setToolPreview(
|
||||||
{
|
toolStroke.getPreview(),
|
||||||
lastX += lastX < x ? 1 : -1;
|
parseColor(strings.getString("tool."+currentTool.name()+".border"))
|
||||||
}
|
);
|
||||||
else
|
|
||||||
{
|
|
||||||
lastY += lastY < y ? 1 : -1;
|
|
||||||
}
|
|
||||||
applyCurrentTool(lastX, lastY, true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
assert lastX == x;
|
lastX = x;
|
||||||
assert lastY == y;
|
lastY = y;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onToolHover(MouseEvent ev)
|
private void onToolHover(MouseEvent ev)
|
||||||
|
|
Reference in a new issue