toolstroke: fix how tool results are reported

also, fix query tool functionality

git-svn-id: https://micropolis.googlecode.com/svn/trunk/micropolis-java@643 d9718cc8-9f43-0410-858b-315f434eb58c
This commit is contained in:
jason@long.name 2013-05-16 23:45:13 +00:00
parent 1b19af8057
commit 082e05f63c
4 changed files with 65 additions and 74 deletions

View file

@ -60,9 +60,9 @@ class ToolEffect implements ToolEffectIfc
ToolResult apply() ToolResult apply()
{ {
if (originX - preview.offsetX < 0 || if (originX - preview.offsetX < 0 ||
originX - preview.offsetX >= city.getWidth() || originX - preview.offsetX + preview.getWidth() >= city.getWidth() ||
originY - preview.offsetY < 0 || originY - preview.offsetY < 0 ||
originY - preview.offsetY >= city.getHeight()) originY - preview.offsetY + preview.getHeight() >= city.getHeight())
{ {
return ToolResult.UH_OH; return ToolResult.UH_OH;
} }
@ -87,7 +87,7 @@ class ToolEffect implements ToolEffectIfc
return ToolResult.SUCCESS; return ToolResult.SUCCESS;
} }
else { else {
return ToolResult.NONE; return preview.toolResult;
} }
} }
} }

View file

@ -31,6 +31,7 @@ public class ToolPreview implements ToolEffectIfc
this.tiles = new short[1][1]; this.tiles = new short[1][1];
this.tiles[0][0] = CLEAR; this.tiles[0][0] = CLEAR;
this.sounds = new ArrayList<SoundInfo>(); this.sounds = new ArrayList<SoundInfo>();
this.toolResult = ToolResult.NONE;
} }
//implements ToolEffectIfc //implements ToolEffectIfc
@ -44,6 +45,16 @@ public class ToolPreview implements ToolEffectIfc
} }
} }
int getWidth()
{
return tiles[0].length;
}
int getHeight()
{
return tiles.length;
}
boolean inRange(int dx, int dy) boolean inRange(int dx, int dy)
{ {
return offsetY+dy >= 0 && return offsetY+dy >= 0 &&

View file

@ -32,90 +32,59 @@ public class ToolStroke
public ToolResult apply() public ToolResult apply()
{ {
ToolResult tr = ToolResult.NONE;
Rectangle r = getBounds(); Rectangle r = getBounds();
ToolEffect eff = new ToolEffect(city, r.x, r.y); ToolEffect eff = new ToolEffect(city, r.x, r.y);
for (int i = 0; i < r.height; i += tool.getHeight()) { for (int i = 0; i < r.height; i += tool.getHeight()) {
for (int j = 0; j < r.width; j += tool.getWidth()) { for (int j = 0; j < r.width; j += tool.getWidth()) {
ToolResult tmp = apply1( apply1(new TranslatedToolEffect(eff, j, i));
new TranslatedToolEffect(eff, j, i)
);
if (tmp == ToolResult.SUCCESS) {
tr = tmp;
}
else if (tr == ToolResult.NONE) {
tr = tmp;
}
} }
} }
ToolResult tmp = eff.apply(); return eff.apply();
if (tmp != ToolResult.NONE) {
return tmp;
}
else {
return tr;
}
} }
ToolResult apply1(ToolEffectIfc eff) boolean apply1(ToolEffectIfc eff)
{ {
ToolResult tr = ToolResult.SUCCESS;
switch (tool) switch (tool)
{ {
case PARK: case PARK:
tr = applyParkTool(eff); return applyParkTool(eff);
break;
case RESIDENTIAL: case RESIDENTIAL:
tr = applyZone(eff, 3, 3, RESBASE); return applyZone(eff, 3, 3, RESBASE);
break;
case COMMERCIAL: case COMMERCIAL:
tr = applyZone(eff, 3, 3, COMBASE); return applyZone(eff, 3, 3, COMBASE);
break;
case INDUSTRIAL: case INDUSTRIAL:
tr = applyZone(eff, 3, 3, INDBASE); return applyZone(eff, 3, 3, INDBASE);
break;
case FIRE: case FIRE:
tr = applyZone(eff, 3, 3, FIRESTBASE); return applyZone(eff, 3, 3, FIRESTBASE);
break;
case POLICE: case POLICE:
tr = applyZone(eff, 3, 3, POLICESTBASE); return applyZone(eff, 3, 3, POLICESTBASE);
break;
case POWERPLANT: case POWERPLANT:
tr = applyZone(eff, 4, 4, COALBASE); return applyZone(eff, 4, 4, COALBASE);
break;
case STADIUM: case STADIUM:
tr = applyZone(eff, 4, 4, STADIUMBASE); return applyZone(eff, 4, 4, STADIUMBASE);
break;
case SEAPORT: case SEAPORT:
tr = applyZone(eff, 4, 4, PORTBASE); return applyZone(eff, 4, 4, PORTBASE);
break;
case NUCLEAR: case NUCLEAR:
tr = applyZone(eff, 4, 4, NUCLEARBASE); return applyZone(eff, 4, 4, NUCLEARBASE);
break;
case AIRPORT: case AIRPORT:
tr = applyZone(eff, 6, 6, AIRPORTBASE); return applyZone(eff, 6, 6, AIRPORTBASE);
break;
default: default:
// not expected // not expected
throw new Error("unexpected tool: "+tool); throw new Error("unexpected tool: "+tool);
} }
return tr;
} }
public void dragTo(int xdest, int ydest) public void dragTo(int xdest, int ydest)
@ -155,7 +124,12 @@ public class ToolStroke
return r; return r;
} }
ToolResult applyZone(ToolEffectIfc eff, int width, int height, char tileBase) public CityLocation getLocation()
{
return new CityLocation(xpos, ypos);
}
boolean applyZone(ToolEffectIfc eff, int width, int height, char tileBase)
{ {
int cost = tool.getToolCost(); int cost = tool.getToolCost();
boolean canBuild = true; boolean canBuild = true;
@ -176,7 +150,8 @@ public class ToolStroke
} }
} }
if (!canBuild) { if (!canBuild) {
return ToolResult.UH_OH; eff.toolResult(ToolResult.UH_OH);
return false;
} }
eff.spend(cost); eff.spend(cost);
@ -198,7 +173,7 @@ public class ToolStroke
} }
fixBorder(eff, width, height); fixBorder(eff, width, height);
return ToolResult.SUCCESS; return true;
} }
//compatible function //compatible function
@ -223,14 +198,15 @@ public class ToolStroke
} }
} }
ToolResult applyParkTool(ToolEffectIfc eff) boolean applyParkTool(ToolEffectIfc eff)
{ {
int cost = tool.getToolCost(); int cost = tool.getToolCost();
if (eff.getTile(0, 0) != DIRT) { if (eff.getTile(0, 0) != DIRT) {
// some sort of bulldozing is necessary // some sort of bulldozing is necessary
if (!city.autoBulldoze) { if (!city.autoBulldoze) {
return ToolResult.UH_OH; eff.toolResult(ToolResult.UH_OH);
return false;
} }
//FIXME- use a canAutoBulldoze-style function here //FIXME- use a canAutoBulldoze-style function here
@ -240,7 +216,8 @@ public class ToolStroke
} }
else { else {
// cannot be auto-bulldozed // cannot be auto-bulldozed
return ToolResult.UH_OH; eff.toolResult(ToolResult.UH_OH);
return false;
} }
} }
@ -255,7 +232,7 @@ public class ToolStroke
eff.spend(cost); eff.spend(cost);
eff.setTile(0, 0, tile); eff.setTile(0, 0, tile);
return ToolResult.SUCCESS; return true;
} }
// checks whether the tile value represents road with traffic // checks whether the tile value represents road with traffic

View file

@ -929,7 +929,14 @@ 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;
this.toolStroke = currentTool.beginStroke(engine, x, y); if (currentTool == MicropolisTool.QUERY) {
doQueryTool(x, y);
this.toolStroke = null;
}
else {
this.toolStroke = currentTool.beginStroke(engine, x, y);
}
this.lastX = x; this.lastX = x;
this.lastY = y; this.lastY = y;
} }
@ -937,7 +944,9 @@ public class MainWindow extends JFrame
private void onToolUp(MouseEvent ev) private void onToolUp(MouseEvent ev)
{ {
if (toolStroke != null) { if (toolStroke != null) {
toolStroke.apply(); CityLocation loc = toolStroke.getLocation();
ToolResult tr = toolStroke.apply();
showToolResult(loc, tr);
toolStroke = null; toolStroke = null;
} }
} }
@ -961,6 +970,9 @@ public class MainWindow extends JFrame
parseColor(strings.getString("tool."+currentTool.name()+".border")) parseColor(strings.getString("tool."+currentTool.name()+".border"))
); );
} }
else if (currentTool == MicropolisTool.QUERY) {
doQueryTool(x, y);
}
lastX = x; lastX = x;
lastY = y; lastY = y;
@ -995,34 +1007,25 @@ public class MainWindow extends JFrame
drawingArea.setToolPreview(null); drawingArea.setToolPreview(null);
} }
private void applyCurrentTool(int x, int y, boolean drag) private void showToolResult(CityLocation loc, ToolResult result)
{ {
if (currentTool == MicropolisTool.QUERY) {
doQueryTool(x, y);
return;
}
ToolResult result = currentTool.apply(engine, x, y);
switch (result) { switch (result) {
case SUCCESS: case SUCCESS:
citySound(currentTool == MicropolisTool.BULLDOZER ? Sound.BULLDOZE : Sound.BUILD, new CityLocation(x, y)); citySound(currentTool == MicropolisTool.BULLDOZER ? Sound.BULLDOZE : Sound.BUILD, loc);
dirty1 = true; dirty1 = true;
break; break;
case NONE: break; case NONE: break;
case UH_OH: case UH_OH:
if (!drag) { messagesPane.appendCityMessage(MicropolisMessage.BULLDOZE_FIRST);
messagesPane.appendCityMessage(MicropolisMessage.BULLDOZE_FIRST); citySound(Sound.UHUH, loc);
citySound(Sound.UHUH, new CityLocation(x, y));
}
break; break;
case INSUFFICIENT_FUNDS: case INSUFFICIENT_FUNDS:
if (!drag) { messagesPane.appendCityMessage(MicropolisMessage.INSUFFICIENT_FUNDS);
messagesPane.appendCityMessage(MicropolisMessage.INSUFFICIENT_FUNDS); citySound(Sound.SORRY, loc);
citySound(Sound.SORRY, new CityLocation(x, y));
}
break; break;
default: default:
assert false; assert false;
} }