use of mudmap2.backend.World in project mudmap2 by Neop.
the class WorldFileJSON method backup.
/**
* Create a copy of the referenced file.
* The new filename will be originalfilename + .bak
* existing files will be overwritten
* @throws FileNotFoundException
*/
@Override
public void backup() throws FileNotFoundException {
try {
File fileold = new File(filename);
File filenew = new File(filename + ".bak");
if (fileold.canRead()) {
if (filenew.exists())
filenew.delete();
Files.copy(fileold.toPath(), filenew.toPath());
}
} catch (IOException ex) {
Logger.getLogger(WorldFileJSON.class.getName()).log(Level.SEVERE, null, ex);
JOptionPane.showMessageDialog(null, "Could not create world backup file", "World backup", JOptionPane.INFORMATION_MESSAGE);
}
}
use of mudmap2.backend.World 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.World 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.World in project mudmap2 by Neop.
the class OpenWorldDialog method create.
private void create() {
filechooser = new JFileChooser();
filechooser.setAcceptAllFileFilterUsed(false);
filechooser.setDialogTitle("Open world");
filechooser.setMultiSelectionEnabled(false);
filechooser.addChoosableFileFilter(new FileFilter() {
@Override
public boolean accept(File file) {
if (file == null)
return false;
if (file.isDirectory())
return true;
String worldname = null;
try {
worldname = (new WorldFileDefault(file.getPath()).readWorldName());
} catch (Exception ex) {
Logger.getLogger(OpenWorldDialog.class.getName()).log(Level.SEVERE, null, ex);
}
return worldname != null && !worldname.equals("");
}
@Override
public String getDescription() {
return "MUD Map world files";
}
});
int ret = filechooser.showOpenDialog(parent);
if (ret == JFileChooser.APPROVE_OPTION) {
String file = filechooser.getSelectedFile().toString();
try {
World world = WorldManager.getWorld(file);
if (null != world) {
// create world tab
parent.createTab(world, file);
}
} catch (Exception ex) {
Logger.getLogger(OpenWorldDialog.class.getName()).log(Level.WARNING, null, ex);
JOptionPane.showMessageDialog(parent, "Could not open world file '" + file + "'", "Open world", JOptionPane.ERROR_MESSAGE);
}
}
}
use of mudmap2.backend.World in project mudmap2 by Neop.
the class WorldFileDefaultTest method testCanRead.
/**
* Test of canRead method, of class WorldFileDefault.
*/
@Test
public void testCanRead() {
System.out.println("canRead");
World world = new World("foobar");
String wfjFile = folder.getRoot() + "/wfj";
try {
WorldFileJSON wfj = new WorldFileJSON(wfjFile);
wfj.writeFile(world);
} catch (IOException ex) {
Logger.getLogger(WorldFileJSONTest.class.getName()).log(Level.SEVERE, null, ex);
fail("Could not create files for test");
}
WorldFileDefault instancewfj = new WorldFileDefault(wfjFile);
Boolean result = instancewfj.canRead();
assertTrue(result);
}
Aggregations