Search in sources :

Example 6 with java.awt.event

use of java.awt.event 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

java.awt.event (java.awt.event)6 javax.swing (javax.swing)5 java.awt (java.awt)4 EmptyBorder (javax.swing.border.EmptyBorder)4 Alarm (com.intellij.util.Alarm)3 java.util (java.util)3 AllIcons (com.intellij.icons.AllIcons)2 MnemonicHelper (com.intellij.openapi.MnemonicHelper)2 ApplicationManager (com.intellij.openapi.application.ApplicationManager)2 UsageInfo (com.intellij.usageView.UsageInfo)2 BufferedImage (java.awt.image.BufferedImage)2 List (java.util.List)2 ImageIO (javax.imageio.ImageIO)2 DocumentEvent (javax.swing.event.DocumentEvent)2 ListSelectionEvent (javax.swing.event.ListSelectionEvent)2 ListSelectionListener (javax.swing.event.ListSelectionListener)2 FileFilter (javax.swing.filechooser.FileFilter)2 NonNls (org.jetbrains.annotations.NonNls)2 Constants (org.pepsoft.minecraft.Constants)2 AutoBiomeScheme (org.pepsoft.worldpainter.biomeschemes.AutoBiomeScheme)2