toolstroke: uniform mechanism for keeping tool cursor updated

git-svn-id: https://micropolis.googlecode.com/svn/trunk/micropolis-java@644 d9718cc8-9f43-0410-858b-315f434eb58c
This commit is contained in:
jason@long.name 2013-05-16 23:45:25 +00:00
parent 082e05f63c
commit d0a6bb2303
2 changed files with 42 additions and 36 deletions

View file

@ -23,7 +23,6 @@ import javax.swing.Timer;
import javax.swing.filechooser.FileNameExtensionFilter; import javax.swing.filechooser.FileNameExtensionFilter;
import micropolisj.engine.*; import micropolisj.engine.*;
import static micropolisj.gui.ColorParser.parseColor;
public class MainWindow extends JFrame public class MainWindow extends JFrame
implements Micropolis.Listener, EarthquakeListener implements Micropolis.Listener, EarthquakeListener
@ -949,6 +948,8 @@ public class MainWindow extends JFrame
showToolResult(loc, tr); showToolResult(loc, tr);
toolStroke = null; toolStroke = null;
} }
onToolHover(ev);
} }
private void onToolDrag(MouseEvent ev) private void onToolDrag(MouseEvent ev)
@ -965,9 +966,9 @@ public class MainWindow extends JFrame
if (toolStroke != null) { if (toolStroke != null) {
toolStroke.dragTo(x, y); toolStroke.dragTo(x, y);
drawingArea.setToolPreview( drawingArea.setToolCursor(
toolStroke.getBounds(), toolStroke.getBounds(),
parseColor(strings.getString("tool."+currentTool.name()+".border")) currentTool
); );
} }
else if (currentTool == MicropolisTool.QUERY) { else if (currentTool == MicropolisTool.QUERY) {
@ -982,7 +983,7 @@ public class MainWindow extends JFrame
{ {
if (currentTool == null || currentTool == MicropolisTool.QUERY) if (currentTool == null || currentTool == MicropolisTool.QUERY)
{ {
drawingArea.setToolPreview(null); drawingArea.setToolCursor(null);
return; return;
} }
@ -996,15 +997,12 @@ public class MainWindow extends JFrame
if (h >= 3) if (h >= 3)
y--; y--;
drawingArea.setToolPreview(new Rectangle(x,y,w,h), drawingArea.setToolCursor(new Rectangle(x,y,w,h), currentTool);
parseColor(strings.getString("tool."+currentTool.name()+".border"))
);
drawingArea.toolPreview.fillColor = parseColor(strings.getString("tool."+currentTool.name()+".bgcolor"));
} }
private void onToolExited(MouseEvent ev) private void onToolExited(MouseEvent ev)
{ {
drawingArea.setToolPreview(null); drawingArea.setToolCursor(null);
} }
private void showToolResult(CityLocation loc, ToolResult result) private void showToolResult(CityLocation loc, ToolResult result)

View file

@ -19,6 +19,7 @@ import javax.swing.Timer;
import micropolisj.engine.*; import micropolisj.engine.*;
import static micropolisj.engine.TileConstants.*; import static micropolisj.engine.TileConstants.*;
import static micropolisj.gui.ColorParser.parseColor;
public class MicropolisDrawingArea extends JComponent public class MicropolisDrawingArea extends JComponent
implements Scrollable, MapListener implements Scrollable, MapListener
@ -28,10 +29,12 @@ public class MicropolisDrawingArea extends JComponent
HashSet<Point> unpoweredZones = new HashSet<Point>(); HashSet<Point> unpoweredZones = new HashSet<Point>();
boolean blink; boolean blink;
Timer blinkTimer; Timer blinkTimer;
ToolCursor toolCursor;
ToolPreview toolPreview; ToolPreview toolPreview;
int shakeStep; int shakeStep;
static final Dimension PREFERRED_VIEWPORT_SIZE = new Dimension(640,640); static final Dimension PREFERRED_VIEWPORT_SIZE = new Dimension(640,640);
static final ResourceBundle strings = MainWindow.strings;
public MicropolisDrawingArea(Micropolis engine) public MicropolisDrawingArea(Micropolis engine)
{ {
@ -185,12 +188,12 @@ public class MicropolisDrawingArea extends JComponent
} }
} }
if (toolPreview != null) if (toolCursor != null)
{ {
int x0 = toolPreview.rect.x * TILE_WIDTH; int x0 = toolCursor.rect.x * TILE_WIDTH;
int x1 = (toolPreview.rect.x + toolPreview.rect.width) * TILE_WIDTH; int x1 = (toolCursor.rect.x + toolCursor.rect.width) * TILE_WIDTH;
int y0 = toolPreview.rect.y * TILE_HEIGHT; int y0 = toolCursor.rect.y * TILE_HEIGHT;
int y1 = (toolPreview.rect.y + toolPreview.rect.height) * TILE_HEIGHT; int y1 = (toolCursor.rect.y + toolCursor.rect.height) * TILE_HEIGHT;
gr.setColor(Color.BLACK); gr.setColor(Color.BLACK);
gr.drawLine(x0-1,y0-1,x0-1,y1-1); gr.drawLine(x0-1,y0-1,x0-1,y1-1);
@ -204,56 +207,61 @@ public class MicropolisDrawingArea extends JComponent
gr.drawLine(x1, y0-1,x1, y1 ); gr.drawLine(x1, y0-1,x1, y1 );
gr.drawLine(x0-1,y1, x1, y1 ); gr.drawLine(x0-1,y1, x1, y1 );
gr.setColor(toolPreview.borderColor); gr.setColor(toolCursor.borderColor);
gr.drawRect(x0-3,y0-3,x1-x0+5,y1-y0+5); gr.drawRect(x0-3,y0-3,x1-x0+5,y1-y0+5);
gr.drawRect(x0-2,y0-2,x1-x0+3,y1-y0+3); gr.drawRect(x0-2,y0-2,x1-x0+3,y1-y0+3);
if (toolPreview.fillColor != null) { if (toolCursor.fillColor != null) {
gr.setColor(toolPreview.fillColor); gr.setColor(toolCursor.fillColor);
gr.fillRect(x0,y0,x1-x0,y1-y0); gr.fillRect(x0,y0,x1-x0,y1-y0);
} }
} }
} }
static class ToolPreview static class ToolCursor
{ {
Rectangle rect; Rectangle rect;
Color borderColor; Color borderColor;
Color fillColor; Color fillColor;
} }
public void setToolPreview(Rectangle newRect, Color toolColor) public void setToolCursor(Rectangle newRect, MicropolisTool tool)
{ {
ToolPreview tp = new ToolPreview(); ToolCursor tp = new ToolCursor();
tp.rect = newRect; tp.rect = newRect;
tp.borderColor = toolColor; tp.borderColor = parseColor(
setToolPreview(tp); strings.getString("tool."+tool.name()+".border")
);
tp.fillColor = parseColor(
strings.getString("tool."+tool.name()+".bgcolor")
);
setToolCursor(tp);
} }
public void setToolPreview(ToolPreview newPreview) public void setToolCursor(ToolCursor newCursor)
{ {
if (toolPreview == newPreview) if (toolCursor == newCursor)
return; return;
if (toolPreview != null && toolPreview.equals(newPreview)) if (toolCursor != null && toolCursor.equals(newCursor))
return; return;
if (toolPreview != null) if (toolCursor != null)
{ {
repaint(new Rectangle( repaint(new Rectangle(
toolPreview.rect.x*TILE_WIDTH - 4, toolCursor.rect.x*TILE_WIDTH - 4,
toolPreview.rect.y*TILE_HEIGHT - 4, toolCursor.rect.y*TILE_HEIGHT - 4,
toolPreview.rect.width*TILE_WIDTH + 8, toolCursor.rect.width*TILE_WIDTH + 8,
toolPreview.rect.height*TILE_HEIGHT + 8 toolCursor.rect.height*TILE_HEIGHT + 8
)); ));
} }
toolPreview = newPreview; toolCursor = newCursor;
if (toolPreview != null) if (toolCursor != null)
{ {
repaint(new Rectangle( repaint(new Rectangle(
toolPreview.rect.x*TILE_WIDTH - 4, toolCursor.rect.x*TILE_WIDTH - 4,
toolPreview.rect.y*TILE_HEIGHT - 4, toolCursor.rect.y*TILE_HEIGHT - 4,
toolPreview.rect.width*TILE_WIDTH + 8, toolCursor.rect.width*TILE_WIDTH + 8,
toolPreview.rect.height*TILE_HEIGHT + 8 toolCursor.rect.height*TILE_HEIGHT + 8
)); ));
} }
} }