setCurrentTile(newTile);
} else if (currentPointerState == PS_PAINT) {
// In case we are dragging to create a custom brush, let
// the user know where we are creating it from
if (marqueeSelection == null) {
marqueeSelection = new SelectionLayer(
currentMap.getWidth(), currentMap.getHeight());
currentMap.addLayerSpecial(marqueeSelection);
}
Point limp = mouseInitialPressLocation;
Rectangle oldArea =
marqueeSelection.getSelectedAreaBounds();
int minx = Math.min(limp.x, tile.x);
int miny = Math.min(limp.y, tile.y);
Rectangle selRect = new Rectangle(
minx, miny,
(Math.max(limp.x, tile.x) - minx)+1,
(Math.max(limp.y, tile.y) - miny)+1);
marqueeSelection.selectRegion(selRect);
if (oldArea != null) {
oldArea.add(marqueeSelection.getSelectedAreaBounds());
mapView.repaintRegion(oldArea);
}
}
} else if (layer instanceof UnitGroup && !bMouseIsDragging) {
// Get the unit on this location and display the relative options dialog
UnitGroup group = (UnitGroup) layer;
Point pos = mapView.screenToPixelCoords(
event.getX(), event.getY());
MapUnit unit = group.getUnitAt(pos.x, pos.y);
if (unit != null) {
UnitDialog od = new UnitDialog(appFrame, unit, undoSupport);
od.getProps();
//TODO change to only updating the unit if that isn't done automatically yet
mapChanged(new MapChangedEvent(currentMap));
}
} else if (layer instanceof ZoneGroup && !bMouseIsDragging) {
// Get the zone on this location and display the relative options dialog
ZoneGroup group = (ZoneGroup) layer;
Point pos = mapView.screenToPixelCoords(
event.getX(), event.getY());
MapZone zone = group.getZoneAt(pos.x, pos.y);
if (zone != null) {
ZoneDialog od = new ZoneDialog(appFrame, zone, undoSupport);
od.updateInfo();
od.setVisible(true);
//TODO change to only updating the zone, if that isn't done automatically yet
mapChanged(new MapChangedEvent(currentMap));
}
}
} else if (mouseButton == MouseEvent.BUTTON2 ||
(mouseButton == MouseEvent.BUTTON1 &&
(event.getModifiersEx() & MouseEvent.ALT_DOWN_MASK ) != 0)) {
// Scroll with middle mouse button
int dx = event.getX() - mouseInitialScreenLocation.x;
int dy = event.getY() - mouseInitialScreenLocation.y;
JViewport mapViewPort = mapScrollPane.getViewport();
Point currentPosition = mapViewPort.getViewPosition();
mouseInitialScreenLocation = new Point(
event.getX() - dx,
event.getY() - dy);
Point newPosition = new Point(
currentPosition.x - dx,
currentPosition.y - dy);
// Take into account map boundaries in order to prevent
// scrolling past them
int maxX = mapView.getWidth() - mapViewPort.getWidth();
int maxY = mapView.getHeight() - mapViewPort.getHeight();
newPosition.x = Math.min(maxX, Math.max(0, newPosition.x));
newPosition.y = Math.min(maxY, Math.max(0, newPosition.y));
mapViewPort.setViewPosition(newPosition);
} else if (mouseButton == MouseEvent.BUTTON1) {
switch (currentPointerState) {
case PS_PAINT:
paintEdit.setPresentationName(TOOL_PAINT);
if (layer instanceof TileLayer) {
try {
mapView.repaintRegion(
currentBrush.doPaint(tile.x, tile.y));
if(currentTile instanceof AnimatedTile) {
animatedTiles.add(new AnimTile(tile.x, tile.y, (AnimatedTile) currentTile, layer));
}
} catch (Exception e) {
e.printStackTrace();
}
}
break;
case PS_ERASE:
paintEdit.setPresentationName(TOOL_ERASE);
if (layer instanceof TileLayer) {
if(((TileLayer) layer).getTileAt(tile.x, tile.y) instanceof AnimatedTile) {
removeAnimTileAt(tile.x, tile.y);
}
((TileLayer) layer).setTileAt(tile.x, tile.y, null);
mapView.repaintRegion(new Rectangle(
tile.x, tile.y, 1, 1));
}
break;
case PS_POUR:
//TODO add animated tiles here, is a real bitch due to undo/redo
paintEdit = null;
if (layer instanceof TileLayer) {
if(!(currentTile instanceof AnimatedTile)) {
TileLayer tileLayer = (TileLayer) layer;
Tile oldTile = tileLayer.getTileAt(tile.x, tile.y);
pour(tileLayer, tile.x, tile.y, currentTile, oldTile);
mapView.repaint();
}
}
break;
case PS_EYED:
if (layer instanceof TileLayer) {
TileLayer tileLayer = (TileLayer) layer;
Tile newTile = tileLayer.getTileAt(tile.x, tile.y);
setCurrentTile(newTile);
}
break;
case PS_MOVE:
if (layer instanceof TileLayer) {
Point translation = new Point(
tile.x - mousePressLocation.x,
tile.y - mousePressLocation.y);
translateAnimTiles(translation, layer);
layer.translate(translation.x, translation.y);
moveDist.translate(translation.x, translation.y);
mapView.repaint();
}
break;
case PS_MARQUEE:
if (!(layer instanceof TileLayer)) {
break;
}
if (marqueeSelection != null) {
Point limp = mouseInitialPressLocation;
Rectangle oldArea =
marqueeSelection.getSelectedAreaBounds();
int minx = Math.min(limp.x, tile.x);
int miny = Math.min(limp.y, tile.y);
Rectangle selRect = new Rectangle(
minx, miny,
(Math.max(limp.x, tile.x) - minx)+1,
(Math.max(limp.y, tile.y) - miny)+1);
if (event.isShiftDown()) {
marqueeSelection.add(new Area(selRect));
} else if (event.isControlDown()) {
marqueeSelection.subtract(new Area(selRect));
} else {
marqueeSelection.selectRegion(selRect);
}
if (oldArea != null) {
oldArea.add(
marqueeSelection.getSelectedAreaBounds());
mapView.repaintRegion(oldArea);
}
}
break;
case PS_ADDUNIT:
if (layer instanceof UnitGroup) {
if (marqueeSelection == null) {
marqueeSelection = new SelectionLayer(
currentMap.getWidth(),
currentMap.getHeight());
currentMap.addLayerSpecial(marqueeSelection);
}
Point limp = mouseInitialPressLocation;
Rectangle oldArea =
marqueeSelection.getSelectedAreaBounds();
int minx = Math.min(limp.x, tile.x);
int miny = Math.min(limp.y, tile.y);
Rectangle selRect = new Rectangle(
minx, miny,
(Math.max(limp.x, tile.x) - minx) + 1,
(Math.max(limp.y, tile.y) - miny) + 1);
marqueeSelection.selectRegion(selRect);
if (oldArea != null) {
oldArea.add(marqueeSelection.getSelectedAreaBounds());
mapView.repaintRegion(oldArea);
}
}
break;
case PS_REMOVEUNIT:
if (layer instanceof UnitGroup) {
UnitGroup group = (UnitGroup) layer;
Point pos = mapView.screenToPixelCoords(
event.getX(), event.getY());
MapUnit unit = group.getUnitAt(pos.x, pos.y);
if (unit != null) {
undoSupport.postEdit(new RemoveUnitEdit(group, unit));
group.removeUnit(unit);
// TODO: repaint only affected area
mapView.repaint();
}
} else if (layer instanceof ZoneGroup) {
ZoneGroup group = (ZoneGroup) layer;
Point pos = mapView.screenToPixelCoords(
event.getX(), event.getY());
MapZone zone = group.getZoneAt(pos.x, pos.y);
if (zone != null) {
undoSupport.postEdit(new RemoveZoneEdit(group, zone));
group.removeZone(zone);
// TODO: repaint only affected area
mapView.repaint();
}
}
break;
case PS_MOVEUNIT:
if (layer instanceof UnitGroup) {
Point pos = mapView.screenToPixelCoords(
event.getX(), event.getY());
if (currentUnit == null) {
UnitGroup group = (UnitGroup) layer;
currentUnit = group.getUnitAt(pos.x, pos.y);
if (currentUnit == null) { // No unit to move
break;
}
mouseLastPixelLocation = pos;
moveDist = new Point(0, 0);
break;
}
Point translation = new Point(
pos.x - mouseLastPixelLocation.x,
pos.y - mouseLastPixelLocation.y);
currentUnit.translate(translation.x, translation.y);
moveDist.translate(translation.x, translation.y);
mouseLastPixelLocation = pos;
mapView.repaint();
} else if (layer instanceof ZoneGroup) {
Point pos = mapView.screenToPixelCoords(
event.getX(), event.getY());
if (currentZone == null) {
ZoneGroup group = (ZoneGroup) layer;
currentZone = group.getZoneAt(pos.x, pos.y);
if (currentZone == null) { // No unit to move
break;
}
mouseLastPixelLocation = pos;
moveDist = new Point(0, 0);
break;
}
Point translation = new Point(
pos.x - mouseLastPixelLocation.x,
pos.y - mouseLastPixelLocation.y);
currentZone.translate(translation.x, translation.y);
moveDist.translate(translation.x, translation.y);
mouseLastPixelLocation = pos;
mapView.repaint();
}
break;
case PS_ADDZON:
if (layer instanceof ZoneGroup) {
if (marqueeSelection == null) {
marqueeSelection = new SelectionLayer(
currentMap.getWidth(),
currentMap.getHeight());
currentMap.addLayerSpecial(marqueeSelection);
}