use of mudmap2.backend.Place in project mudmap2 by Neop.
the class CopyPaste method paste.
/**
* Pastes the cut / copied places to layer, if possible
* @param x
* @param y
* @param layer
* @return false on error or user abort
*/
public static boolean paste(int x, int y, Layer layer) {
if (!canPaste(x, y, layer))
return false;
// ask user
String title = (copyMode ? "Copy " : "Paste ") + "place(s)";
String message = title + "? This can not be undone!" + (copyPlaces.iterator().next().getLayer().getWorld() != layer.getWorld() ? " Pasting to another world might cause problems!" : "");
int ret = JOptionPane.showConfirmDialog(null, message, title, JOptionPane.YES_NO_OPTION);
if (ret == JOptionPane.YES_OPTION) {
// map to translate from old to new place
HashMap<Place, Place> place_to_new_place = new HashMap<Place, Place>();
Place[] places;
if (copyMode) {
places = copyPlaces.toArray(new Place[copyPlaces.size()]);
} else {
// getPlace movement direction
final int fact_x = (x <= copydx ? 1 : -1);
final int fact_y = (y <= copydy ? 1 : -1);
// sort places
ArrayList<Place> ordered_places = new ArrayList<>(copyPlaces);
Collections.sort(ordered_places, new Comparator<Place>() {
@Override
public int compare(Place t, Place t1) {
// list will be moved first
if (fact_x * t.getX() > fact_x * t1.getX())
return 1;
else if (t.getX() == t1.getX()) {
if (fact_y * t.getY() > fact_y * t1.getY())
return 1;
else if (t.getY() == t1.getY())
return 0;
}
return -1;
}
});
places = ordered_places.toArray(new Place[ordered_places.size()]);
}
// copy places
for (Place place : places) {
try {
if (place.getLayer().getWorld() != layer.getWorld()) {
if (place.getPlaceGroup() != null && !layer.getWorld().getPlaceGroups().contains(place.getPlaceGroup()))
layer.getWorld().addPlaceGroup(place.getPlaceGroup());
}
if (copyMode) {
// copy places -> duplicate on new layer
Place new_place = place.duplicate();
place_to_new_place.put(place, new_place);
layer.put(new_place, place.getX() - copydx + x, place.getY() - copydy + y);
} else {
// remove place from old layer
Layer layerOld = place.getLayer();
layerOld.remove(place);
// add place to new layer
place.setLayer(layer);
layer.put(place, place.getX() - copydx + x, place.getY() - copydy + y);
}
} catch (Exception ex) {
Logger.getLogger(Mudmap2.class.getName()).log(Level.SEVERE, null, ex);
return false;
}
}
// recreate paths and child connections after copy-paste
if (copyMode) {
for (Place place : copyPlaces) {
Place new_place = place_to_new_place.get(place);
// connect paths
for (Path path : place.getPaths()) {
// only check first place, because the other side will
// check itself
Place path_end_place = path.getPlaces()[0];
// if end place is not this place and is also copied
if (path_end_place != place && copyPlaces.contains(path_end_place)) {
Place other_new_place = place_to_new_place.get(path_end_place);
new_place.connectPath(new Path(other_new_place, path.getExitDirections()[0], new_place, path.getExitDirections()[1]));
}
}
// connect children
for (Place child : place.getChildren()) {
// if child is copied, too
if (copyPlaces.contains(child)) {
Place new_child = place_to_new_place.get(child);
new_place.connectChild(new_child);
}
}
}
// moving places modifies their coordinates so that they cant be pasted again
} else
resetCopy();
}
// don't clean up but change cut to copy
return true;
}
use of mudmap2.backend.Place in project mudmap2 by Neop.
the class EditWorldDialog method create.
@Override
void create() {
setLayout(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
GridBagConstraints constraints_l = new GridBagConstraints();
GridBagConstraints constraints_r = new GridBagConstraints();
constraints.insets = constraints_l.insets = constraints_r.insets = new Insets(2, 2, 2, 2);
constraints_l.fill = GridBagConstraints.HORIZONTAL;
constraints_r.fill = GridBagConstraints.BOTH;
constraints_r.gridx = 1;
constraints_l.weightx = constraints_r.weightx = 1.0;
constraints_l.gridy = ++constraints_r.gridy;
add(new JLabel("World name"), constraints_l);
add(textfield_name = new JTextField(world.getName()), constraints_r);
textfield_name.setColumns(20);
constraints_l.gridy = ++constraints_r.gridy;
constraints.gridy = constraints_l.gridy = ++constraints_r.gridy;
constraints.gridwidth = 2;
constraints.fill = GridBagConstraints.HORIZONTAL;
add(new JSeparator(), constraints);
constraints.gridy = constraints_l.gridy = ++constraints_r.gridy;
add(new JLabel("Risk Levels"), constraints);
risklevel_colors = new HashMap<>();
for (RiskLevel rl : world.getRiskLevels()) {
constraints_l.gridy = ++constraints_r.gridy;
JTextField tf_rl_name = new JTextField(rl.getDescription());
add(tf_rl_name, constraints_l);
ColorChooserButton colorchooser = new ColorChooserButton(getParent(), rl.getColor());
add(colorchooser, constraints_r);
risklevel_colors.put(rl, new Pair<>(tf_rl_name, colorchooser));
}
constraints_l.gridy = ++constraints_r.gridy;
add(risklevel_new_name = new JTextField(), constraints_l);
add(risklevel_new_color = new ColorChooserButton(getParent()), constraints_r);
constraints_l.gridy = ++constraints_r.gridy;
add(new JLabel("Tile center color"), constraints_l);
add(tile_center_color = new ColorChooserButton(getParent(), world.getTileCenterColor()), constraints_r);
constraints.gridy = constraints_l.gridy = ++constraints_r.gridy;
add(new JSeparator(), constraints);
buttongroup_place_id = new ButtonGroup();
radiobutton_place_id_none = new JRadioButton("Don't show place ID on map");
radiobutton_place_id_unique = new JRadioButton("Show place ID if name isn't unique");
radiobutton_place_id_all = new JRadioButton("Always show place ID");
buttongroup_place_id.add(radiobutton_place_id_none);
buttongroup_place_id.add(radiobutton_place_id_unique);
buttongroup_place_id.add(radiobutton_place_id_all);
constraints_l.gridy = ++constraints_r.gridy;
add(radiobutton_place_id_none, constraints_l);
/*constraints_l.gridy = ++constraints_r.gridy; // feature (temporarily?) removed
add(radiobutton_place_id_unique, constraints_l);*/
constraints_l.gridy = ++constraints_r.gridy;
add(radiobutton_place_id_all, constraints_l);
switch(world.getShowPlaceId()) {
case NONE:
buttongroup_place_id.setSelected(radiobutton_place_id_none.getModel(), true);
break;
default:
case UNIQUE:
buttongroup_place_id.setSelected(radiobutton_place_id_unique.getModel(), true);
break;
case ALL:
buttongroup_place_id.setSelected(radiobutton_place_id_all.getModel(), true);
break;
}
constraints_l.insets = constraints_r.insets = new Insets(0, 2, 0, 2);
constraints_l.gridy = ++constraints_r.gridy;
JButton button_cancel = new JButton("Cancel");
add(button_cancel, constraints_l);
button_cancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
dispose();
}
});
JButton button_ok = new JButton("Ok");
add(button_ok, constraints_r);
getRootPane().setDefaultButton(button_ok);
button_ok.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
try {
save();
} catch (Exception ex) {
Logger.getLogger(EditWorldDialog.class.getName()).log(Level.SEVERE, null, ex);
}
dispose();
}
});
pack();
setResizable(false);
setLocation(getParent().getX() + (getParent().getWidth() - getWidth()) / 2, getParent().getY() + (getParent().getHeight() - getHeight()) / 2);
}
use of mudmap2.backend.Place in project mudmap2 by Neop.
the class ExportImageDialog method getSelectionSize.
/**
* Calculates the size needed to draw the selected places
* @param places selected places
* @return width and height
*/
Pair<Integer, Integer> getSelectionSize(HashSet<Place> places) {
Pair<Integer, Integer> imageSize = new Pair<>(0, 0);
if (!places.isEmpty()) {
// get bounds of selected places
int mapXMax, mapXMin, mapYMax, mapYMin;
Place place1 = places.iterator().next();
mapXMax = mapXMin = place1.getX();
mapYMax = mapYMin = place1.getY();
for (Place place : places) {
mapXMax = Math.max(mapXMax, place.getX());
mapXMin = Math.min(mapXMin, place.getX());
mapYMax = Math.max(mapYMax, place.getY());
mapYMin = Math.min(mapYMin, place.getY());
}
// calculate image size from bounds
int tileSize = (int) spTileSize.getValue();
imageSize.first = (mapXMax - mapXMin + 1) * tileSize;
imageSize.second = (mapYMax - mapYMin + 1) * tileSize;
}
return imageSize;
}
use of mudmap2.backend.Place in project mudmap2 by Neop.
the class ExportImageDialog method drawLayer.
/**
* Draws map
* @param center layer and center information
* @return image
*/
BufferedImage drawLayer(WorldCoordinate center) {
BufferedImage image = null;
Integer tileSize = (Integer) spTileSize.getValue();
int width, height;
// get image size and map center
if (rbSelection.isSelected()) {
HashSet<Place> places = worldTab.getWorldPanel().placeGroupGetSelection();
Pair<Integer, Integer> selectionSize = getSelectionSize(places);
width = selectionSize.first;
height = selectionSize.second;
Pair<Double, Double> selectionCenter = getSelectionCenter(places);
center = new WorldCoordinate(center.getLayer(), selectionCenter.first, selectionCenter.second);
} else if (rbCurrentView.isSelected()) {
Dimension size = worldTab.getWorldPanel().getSize();
width = size.width;
height = size.height;
} else {
// whole map
Pair<Integer, Integer> mapSize = getMapSize(worldTab.getWorld().getLayer(center.getLayer()));
width = mapSize.first;
height = mapSize.second;
Layer layer = worldTab.getWorld().getLayer(center.getLayer());
Pair<Double, Double> exactCenter = layer.getExactCenter();
center = new WorldCoordinate(layer.getId(), exactCenter.first, exactCenter.second);
}
if (width != 0 && height != 0) {
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
}
if (image != null) {
Graphics graphics = image.createGraphics();
graphics.setClip(0, 0, width, height);
((Graphics2D) graphics).setBackground(new Color(255, 255, 255, 0));
graphics.clearRect(0, 0, width, height);
graphics.setFont(worldTab.getFont());
MapPainterDefault mappainter = new MapPainterDefault();
mappainter.setGridEnabled(cbBackgroundGrid.isSelected());
mappainter.paint(graphics, tileSize, width, height, worldTab.getWorld().getLayer(center.getLayer()), center);
}
return image;
}
use of mudmap2.backend.Place in project mudmap2 by Neop.
the class ExportImageDialog method getSelectionCenter.
/**
* Calculates the selection center
* @param places selected places
* @return center coordinates
*/
Pair<Double, Double> getSelectionCenter(HashSet<Place> places) {
Pair<Double, Double> selectionCenter = new Pair<>(0.0, 0.0);
if (!places.isEmpty()) {
// get bounds of selected places
int mapXMax, mapXMin, mapYMax, mapYMin;
Place place1 = places.iterator().next();
mapXMax = mapXMin = place1.getX();
mapYMax = mapYMin = place1.getY();
for (Place place : places) {
mapXMax = Math.max(mapXMax, place.getX());
mapXMin = Math.min(mapXMin, place.getX());
mapYMax = Math.max(mapYMax, place.getY());
mapYMin = Math.min(mapYMin, place.getY());
}
// calculate selection center from bounds
selectionCenter.first = 0.5 * (double) (mapXMax - mapXMin + 1) + (double) mapXMin;
selectionCenter.second = 0.5 * (double) (mapYMax - mapYMin + 1) + (double) mapYMin - 1;
}
return selectionCenter;
}
Aggregations