use of org.pepsoft.worldpainter.MouseAdapter in project WorldPainter by Captain-Chaos.
the class MapExplorer method main.
public static void main(String[] args) throws IOException, ClassNotFoundException {
// Load or initialise configuration
File configDir = Configuration.getConfigDir();
if (!configDir.isDirectory()) {
configDir.mkdirs();
}
// This will migrate the configuration directory if necessary
Configuration config = Configuration.load();
if (config == null) {
if (!logger.isDebugEnabled()) {
// If debug logging is on, the Configuration constructor will
// already log this
logger.info("Creating new configuration");
}
config = new Configuration();
}
Configuration.setInstance(config);
logger.info("Installation ID: " + config.getUuid());
// Load and install trusted WorldPainter root certificate
X509Certificate trustedCert = null;
try {
CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
trustedCert = (X509Certificate) certificateFactory.generateCertificate(Main.class.getResourceAsStream("/wproot.pem"));
WPTrustManager trustManager = new WPTrustManager(trustedCert);
SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, new TrustManager[] { trustManager }, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
} catch (CertificateException e) {
logger.error("Certificate exception while loading trusted root certificate", e);
} catch (NoSuchAlgorithmException e) {
logger.error("No such algorithm exception while loading trusted root certificate", e);
} catch (KeyManagementException e) {
logger.error("Key management exception while loading trusted root certificate", e);
}
// Load the plugins
if (trustedCert != null) {
PluginManager.loadPlugins(new File(configDir, "plugins"), trustedCert.getPublicKey());
} else {
logger.error("Trusted root certificate not available; not loading plugins");
}
WPPluginManager.initialise(config.getUuid());
SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame("Minecraft Map Explorer");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
MapTreeModel treeModel = new MapTreeModel();
// File minecraftDir = MinecraftUtil.findMinecraftDir();
// File defaultDir;
// if (minecraftDir != null) {
// defaultDir = new File(minecraftDir, "saves");
// } else {
// defaultDir = new File(System.getProperty("user.home"));
// }
JTree tree = new JTree(treeModel);
tree.setRootVisible(false);
tree.setShowsRootHandles(true);
tree.setCellRenderer(new MapTreeCellRenderer());
JScrollPane scrollPane = new JScrollPane(tree);
// tree.expandPath(treeModel.getPath(defaultDir));
// tree.scrollPathToVisible(treeModel.getPath(defaultDir));
// Automatically expand any nodes if they only have one child
tree.addTreeExpansionListener(new TreeExpansionListener() {
@Override
public void treeExpanded(TreeExpansionEvent event) {
Object node = event.getPath().getLastPathComponent();
if ((!treeModel.isLeaf(node)) && (treeModel.getChildCount(node) == 1)) {
tree.expandPath(event.getPath().pathByAddingChild(treeModel.getChild(node, 0)));
}
}
@Override
public void treeCollapsed(TreeExpansionEvent event) {
}
});
tree.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
TreePath path = tree.getPathForLocation(e.getX(), e.getY());
if (path != null) {
((Node) path.getLastPathComponent()).doubleClicked();
}
}
}
});
frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
frame.setSize(1024, 768);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
}
use of org.pepsoft.worldpainter.MouseAdapter in project WorldPainter by Captain-Chaos.
the class HeightMapEditor method createHeightMap.
private void createHeightMap() throws IOException {
rootHeightMap = TileFactoryFactory.createFancyTileFactory(new Random().nextLong(), GRASS, DEFAULT_MAX_HEIGHT_2, 62, 58, false, 20f, 1.0).getHeightMap();
// File bitmapFile = new File("/home/pepijn/Pictures/WorldPainter/test-image-8-bit-grayscale.png");
// BufferedImage bitmap = ImageIO.read(bitmapFile);
// BitmapHeightMap bitmapHeightMap = BitmapHeightMap.build().withImage(bitmap).withSmoothScaling(false).withRepeat(true).now();
// TransformingHeightMap scaledHeightMap = TransformingHeightMap.build().withHeightMap(bitmapHeightMap).withScale(300).now();
// NoiseHeightMap angleMap = new NoiseHeightMap("Angle", (float) (Math.PI * 2), 2.5f, 1);
// NoiseHeightMap distanceMap = new NoiseHeightMap("Distance", 25f, 2.5f, 1);
// heightMap = new DisplacementHeightMap(scaledHeightMap, angleMap, distanceMap);
focusOn(rootHeightMap);
installHeightMap(true);
jTree1.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent event) {
if (event.isPopupTrigger()) {
showPopupMenu(event);
}
}
@Override
public void mouseReleased(MouseEvent event) {
if (event.isPopupTrigger()) {
showPopupMenu(event);
}
}
private void showPopupMenu(MouseEvent event) {
TreePath path = jTree1.getPathForLocation(event.getX(), event.getY());
if (path == null) {
return;
}
jTree1.setSelectionPath(path);
HeightMap heightMap = (HeightMap) path.getLastPathComponent();
TreePath parentPath = path.getParentPath();
DelegatingHeightMap parent;
if (parentPath != null) {
parent = (DelegatingHeightMap) parentPath.getLastPathComponent();
} else {
parent = null;
}
JPopupMenu menu = new JPopupMenu();
JMenuItem menuItem = new JMenuItem("Focus Here");
menuItem.addActionListener(actionEvent -> {
focusOn(heightMap);
installHeightMap(false);
});
menu.add(menuItem);
JMenu insertMenu = new JMenu("Insert");
menuItem = new JMenuItem("Product");
menuItem.addActionListener(actionEvent -> {
ProductHeightMap productHeightMap = new ProductHeightMap(heightMap, new ConstantHeightMap(1.0f));
replace(parent, heightMap, productHeightMap);
});
insertMenu.add(menuItem);
menuItem = new JMenuItem("Sum");
menuItem.addActionListener(actionEvent -> {
SumHeightMap sumHeightMap = new SumHeightMap(heightMap, new ConstantHeightMap(0.0f));
replace(parent, heightMap, sumHeightMap);
});
insertMenu.add(menuItem);
menuItem = new JMenuItem("Maximum");
menuItem.addActionListener(actionEvent -> {
MaximisingHeightMap maximisingHeightMap = new MaximisingHeightMap(heightMap, new ConstantHeightMap(0.0f));
replace(parent, heightMap, maximisingHeightMap);
});
insertMenu.add(menuItem);
menuItem = new JMenuItem("Slope");
menuItem.addActionListener(actionEvent -> {
SlopeHeightMap slopeHeightMap = new SlopeHeightMap(heightMap);
replace(parent, heightMap, slopeHeightMap);
});
insertMenu.add(menuItem);
menuItem = new JMenuItem("Displacement");
menuItem.addActionListener(actionEvent -> {
DisplacementHeightMap displacementHeightMap = new DisplacementHeightMap(heightMap, new ConstantHeightMap(0.0f), new ConstantHeightMap(0.0f));
replace(parent, heightMap, displacementHeightMap);
});
insertMenu.add(menuItem);
menuItem = new JMenuItem("Transformation");
menuItem.addActionListener(actionEvent -> {
TransformingHeightMap transformingHeightMap = new TransformingHeightMap(heightMap.getName(), heightMap, 100, 100, 0, 0, 0);
replace(parent, heightMap, transformingHeightMap);
});
insertMenu.add(menuItem);
menuItem = new JMenuItem("Shelves");
menuItem.addActionListener(actionEvent -> {
ShelvingHeightMap shelvingHeightMap = new ShelvingHeightMap(heightMap);
replace(parent, heightMap, shelvingHeightMap);
});
insertMenu.add(menuItem);
menu.add(insertMenu);
JMenu replaceMenu = new JMenu("Replace");
menuItem = new JMenuItem("Mandelbrot");
menuItem.addActionListener(actionEvent -> {
MandelbrotHeightMap mandelbrotHeightMap = new MandelbrotHeightMap();
replace(parent, heightMap, mandelbrotHeightMap);
});
replaceMenu.add(menuItem);
menuItem = new JMenuItem("Nine Patch");
menuItem.addActionListener(actionEvent -> {
NinePatchHeightMap ninePatchHeightMap = new NinePatchHeightMap(100, 25, 1.0f);
replace(parent, heightMap, ninePatchHeightMap);
});
replaceMenu.add(menuItem);
menuItem = new JMenuItem("Constant");
menuItem.addActionListener(actionEvent -> {
ConstantHeightMap constantHeightMap = new ConstantHeightMap(1.0f);
replace(parent, heightMap, constantHeightMap);
});
replaceMenu.add(menuItem);
menuItem = new JMenuItem("Bitmap");
menuItem.addActionListener(actionEvent -> {
File myHeightMapDir = Configuration.getInstance().getHeightMapsDirectory();
final Set<String> extensions = new HashSet<>(Arrays.asList(ImageIO.getReaderFileSuffixes()));
StringBuilder sb = new StringBuilder("Supported image formats (");
boolean first = true;
for (String extension : extensions) {
if (first) {
first = false;
} else {
sb.append(", ");
}
sb.append("*.");
sb.append(extension);
}
sb.append(')');
final String description = sb.toString();
File file = FileUtils.selectFileForOpen(HeightMapEditor.this, "Select a height map image file", myHeightMapDir, new FileFilter() {
@Override
public boolean accept(File f) {
if (f.isDirectory()) {
return true;
}
String filename = f.getName();
int p = filename.lastIndexOf('.');
if (p != -1) {
String extension = filename.substring(p + 1).toLowerCase();
return extensions.contains(extension);
} else {
return false;
}
}
@Override
public String getDescription() {
return description;
}
});
if (file != null) {
try {
BufferedImage image = ImageIO.read(file);
BitmapHeightMap bitmapHeightMap = new BitmapHeightMap(file.getName(), image, 0, file, false, false);
replace(parent, heightMap, bitmapHeightMap);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
});
replaceMenu.add(menuItem);
menuItem = new JMenuItem("Noise");
menuItem.addActionListener(actionEvent -> {
NoiseHeightMap noiseHeightMap = new NoiseHeightMap(1f, 1.0, 1);
replace(parent, heightMap, noiseHeightMap);
});
replaceMenu.add(menuItem);
menuItem = new JMenuItem("Bands");
menuItem.addActionListener(actionEvent -> {
BandedHeightMap bandedHeightMap = new BandedHeightMap();
replace(parent, heightMap, bandedHeightMap);
});
replaceMenu.add(menuItem);
replaceMenu.addSeparator();
menuItem = new JMenuItem("Product");
menuItem.addActionListener(actionEvent -> {
ProductHeightMap productHeightMap = new ProductHeightMap(new ConstantHeightMap(1.0f), new ConstantHeightMap(1.0f));
replace(parent, heightMap, productHeightMap);
});
replaceMenu.add(menuItem);
menuItem = new JMenuItem("Sum");
menuItem.addActionListener(actionEvent -> {
SumHeightMap sumHeightMap = new SumHeightMap(new ConstantHeightMap(0.5f), new ConstantHeightMap(0.5f));
replace(parent, heightMap, sumHeightMap);
});
replaceMenu.add(menuItem);
menuItem = new JMenuItem("Maximum");
menuItem.addActionListener(actionEvent -> {
MaximisingHeightMap maximisingHeightMap = new MaximisingHeightMap(new ConstantHeightMap(1.0f), new ConstantHeightMap(1.0f));
replace(parent, heightMap, maximisingHeightMap);
});
replaceMenu.add(menuItem);
menuItem = new JMenuItem("Slope");
menuItem.addActionListener(actionEvent -> {
SlopeHeightMap slopeHeightMap = new SlopeHeightMap(new ConstantHeightMap(1.0f));
replace(parent, heightMap, slopeHeightMap);
});
replaceMenu.add(menuItem);
menuItem = new JMenuItem("Displacement");
menuItem.addActionListener(actionEvent -> {
DisplacementHeightMap displacementHeightMap = new DisplacementHeightMap(new ConstantHeightMap(1.0f), new ConstantHeightMap(0.0f), new ConstantHeightMap(0.0f));
replace(parent, heightMap, displacementHeightMap);
});
replaceMenu.add(menuItem);
menuItem = new JMenuItem("Transformation");
menuItem.addActionListener(actionEvent -> {
TransformingHeightMap transformingHeightMap = new TransformingHeightMap(null, new ConstantHeightMap(1.0f), 100, 100, 0, 0, 0);
replace(parent, heightMap, transformingHeightMap);
});
replaceMenu.add(menuItem);
menuItem = new JMenuItem("Shelves");
menuItem.addActionListener(actionEvent -> {
ShelvingHeightMap shelvingHeightMap = new ShelvingHeightMap(new ConstantHeightMap(1.0f));
replace(parent, heightMap, shelvingHeightMap);
});
replaceMenu.add(menuItem);
menu.add(replaceMenu);
if (heightMap instanceof DelegatingHeightMap) {
menuItem = new JMenuItem("Delete");
menuItem.addActionListener(e -> {
replace(parent, heightMap, ((DelegatingHeightMap) heightMap).getHeightMap(0));
treeModel.notifyListeners();
});
menu.add(menuItem);
}
menu.show(jTree1, event.getX(), event.getY());
}
private void replace(DelegatingHeightMap parent, HeightMap oldHeightMap, HeightMap newHeightMap) {
if (parent == null) {
HeightMapEditor.this.rootHeightMap = newHeightMap;
focusOn(newHeightMap);
installHeightMap(true);
} else {
parent.replace(oldHeightMap, newHeightMap);
if (oldHeightMap == HeightMapEditor.this.focusHeightMap) {
focusOn(newHeightMap);
}
treeModel.notifyListeners();
synchronized (tileCache) {
tileCache.clear();
tileCache.notifyAll();
}
tiledImageViewer1.refresh(true);
}
}
});
}
Aggregations