Search in sources :

Example 1 with MouseAdapter

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);
    });
}
Also used : TreeExpansionListener(javax.swing.event.TreeExpansionListener) MouseEvent(java.awt.event.MouseEvent) Configuration(org.pepsoft.worldpainter.Configuration) WPTrustManager(org.pepsoft.worldpainter.browser.WPTrustManager) MouseAdapter(org.pepsoft.worldpainter.MouseAdapter) SecureRandom(java.security.SecureRandom) CertificateException(java.security.cert.CertificateException) SSLContext(javax.net.ssl.SSLContext) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate) KeyManagementException(java.security.KeyManagementException) TreePath(javax.swing.tree.TreePath) File(java.io.File) Main(org.pepsoft.worldpainter.Main) TreeExpansionEvent(javax.swing.event.TreeExpansionEvent)

Example 2 with MouseAdapter

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);
            }
        }
    });
}
Also used : java.util(java.util) AutoBiomeScheme(org.pepsoft.worldpainter.biomeschemes.AutoBiomeScheme) Biome(org.pepsoft.worldpainter.layers.Biome) LoggerFactory(org.slf4j.LoggerFactory) org.pepsoft.worldpainter.heightMaps(org.pepsoft.worldpainter.heightMaps) ImageIO(javax.imageio.ImageIO) HeightMapTileProvider(org.pepsoft.worldpainter.heightMaps.gui.HeightMapTileProvider) HeightMapPropertiesPanel(org.pepsoft.worldpainter.heightMaps.gui.HeightMapPropertiesPanel) org.pepsoft.worldpainter(org.pepsoft.worldpainter) MouseAdapter(org.pepsoft.worldpainter.MouseAdapter) SimpleTheme(org.pepsoft.worldpainter.themes.SimpleTheme) Logger(org.slf4j.Logger) DEFAULT_MAX_HEIGHT_2(org.pepsoft.minecraft.Constants.DEFAULT_MAX_HEIGHT_2) TreePath(javax.swing.tree.TreePath) BufferedImage(java.awt.image.BufferedImage) DynMapColourScheme(org.pepsoft.worldpainter.colourschemes.DynMapColourScheme) IOException(java.io.IOException) FileFilter(javax.swing.filechooser.FileFilter) GRASS(org.pepsoft.worldpainter.Terrain.GRASS) HeightMapTreeModel(org.pepsoft.worldpainter.heightMaps.gui.HeightMapTreeModel) File(java.io.File) Layer(org.pepsoft.worldpainter.layers.Layer) java.awt(java.awt) Constants(org.pepsoft.minecraft.Constants) TileFactoryFactory(org.pepsoft.worldpainter.TileFactoryFactory) java.awt.event(java.awt.event) FileUtils(org.pepsoft.util.FileUtils) HeightMapTreeCellRenderer(org.pepsoft.worldpainter.heightMaps.gui.HeightMapTreeCellRenderer) javax.swing(javax.swing) BufferedImage(java.awt.image.BufferedImage) FileFilter(javax.swing.filechooser.FileFilter) MouseAdapter(org.pepsoft.worldpainter.MouseAdapter) IOException(java.io.IOException) TreePath(javax.swing.tree.TreePath) File(java.io.File)

Aggregations

File (java.io.File)2 TreePath (javax.swing.tree.TreePath)2 MouseAdapter (org.pepsoft.worldpainter.MouseAdapter)2 java.awt (java.awt)1 java.awt.event (java.awt.event)1 MouseEvent (java.awt.event.MouseEvent)1 BufferedImage (java.awt.image.BufferedImage)1 IOException (java.io.IOException)1 KeyManagementException (java.security.KeyManagementException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 SecureRandom (java.security.SecureRandom)1 CertificateException (java.security.cert.CertificateException)1 CertificateFactory (java.security.cert.CertificateFactory)1 X509Certificate (java.security.cert.X509Certificate)1 java.util (java.util)1 ImageIO (javax.imageio.ImageIO)1 SSLContext (javax.net.ssl.SSLContext)1 javax.swing (javax.swing)1 TreeExpansionEvent (javax.swing.event.TreeExpansionEvent)1 TreeExpansionListener (javax.swing.event.TreeExpansionListener)1