Search in sources :

Example 1 with Terrain

use of org.pepsoft.worldpainter.Terrain in project WorldPainter by Captain-Chaos.

the class App method importLayers.

private void importLayers(String paletteName) {
    Configuration config = Configuration.getInstance();
    File layerDirectory = config.getLayerDirectory();
    if ((layerDirectory == null) || (!layerDirectory.isDirectory())) {
        layerDirectory = DesktopUtils.getDocumentsFolder();
    }
    File[] selectedFiles = FileUtils.selectFilesForOpen(this, "Select WorldPainter layer file(s)", layerDirectory, new FileFilter() {

        @Override
        public boolean accept(File f) {
            return f.isDirectory() || f.getName().toLowerCase().endsWith(".layer");
        }

        @Override
        public String getDescription() {
            return "WorldPainter Custom Layers (*.layer)";
        }
    });
    if (selectedFiles != null) {
        boolean updateCustomTerrainButtons = false;
        for (File selectedFile : selectedFiles) {
            try {
                try (ObjectInputStream in = new ObjectInputStream(new GZIPInputStream(new BufferedInputStream(new FileInputStream(selectedFile))))) {
                    CustomLayer layer = (CustomLayer) in.readObject();
                    for (Layer existingLayer : getCustomLayers()) {
                        if (layer.equals(existingLayer)) {
                            showMessageDialog(this, "That layer is already present in the dimension.\nThe layer has not been added.", "Layer Already Present", WARNING_MESSAGE);
                            return;
                        }
                    }
                    if (paletteName != null) {
                        layer.setPalette(paletteName);
                    }
                    registerCustomLayer(layer, true);
                    if (layer instanceof CombinedLayer) {
                        CombinedLayer combinedLayer = (CombinedLayer) layer;
                        addLayersFromCombinedLayer(combinedLayer);
                        if (!combinedLayer.restoreCustomTerrain()) {
                            showMessageDialog(this, "The layer contained a Custom Terrain which could not be restored. The terrain has been reset.", "Custom Terrain Not Restored", ERROR_MESSAGE);
                        } else {
                            // Check for a custom terrain type and if necessary make
                            // sure it has a button
                            Terrain terrain = combinedLayer.getTerrain();
                            if ((terrain != null) && terrain.isCustom()) {
                                updateCustomTerrainButtons = true;
                                if (customMaterialButtons[terrain.getCustomTerrainIndex()] == null) {
                                    addButtonForNewCustomTerrain(terrain.getCustomTerrainIndex(), Terrain.getCustomMaterial(terrain.getCustomTerrainIndex()), false);
                                }
                            }
                        }
                    }
                }
            } catch (FileNotFoundException e) {
                logger.error("File not found while loading file " + selectedFile, e);
                showMessageDialog(this, "The specified path does not exist or is not a file", "Nonexistent File", ERROR_MESSAGE);
                return;
            } catch (IOException e) {
                logger.error("I/O error while loading file " + selectedFile, e);
                showMessageDialog(this, "I/O error occurred while reading the specified file,\nor is not a (valid) WorldPainter layer file", "I/O Error Or Invalid File", ERROR_MESSAGE);
                return;
            } catch (ClassNotFoundException e) {
                logger.error("Class not found exception while loading file " + selectedFile, e);
                showMessageDialog(this, "The specified file is not a (valid) WorldPainter layer file", "Invalid File", ERROR_MESSAGE);
                return;
            } catch (ClassCastException e) {
                logger.error("Class cast exception while loading file " + selectedFile, e);
                showMessageDialog(this, "The specified file is not a (valid) WorldPainter layer file", "Invalid File", ERROR_MESSAGE);
                return;
            }
        }
        if (updateCustomTerrainButtons) {
            updateCustomTerrainButtons();
        }
    }
}
Also used : TunnelLayer(org.pepsoft.worldpainter.layers.tunnel.TunnelLayer) UndergroundPocketsLayer(org.pepsoft.worldpainter.layers.pockets.UndergroundPocketsLayer) GroundCoverLayer(org.pepsoft.worldpainter.layers.groundcover.GroundCoverLayer) PlantLayer(org.pepsoft.worldpainter.layers.plants.PlantLayer) GZIPInputStream(java.util.zip.GZIPInputStream) Terrain(org.pepsoft.worldpainter.Terrain) FileFilter(javax.swing.filechooser.FileFilter)

Example 2 with Terrain

use of org.pepsoft.worldpainter.Terrain in project WorldPainter by Captain-Chaos.

the class App method setDimension.

public void setDimension(final Dimension dimension) {
    Configuration config = Configuration.getInstance();
    if (this.dimension != null) {
        this.dimension.removePropertyChangeListener(this);
        Point viewPosition = view.getViewCentreInWorldCoords();
        if (viewPosition != null) {
            this.dimension.setLastViewPosition(viewPosition);
            // in sync
            if (world != null) {
                Dimension oppositeDimension = null;
                switch(this.dimension.getDim()) {
                    case DIM_NORMAL:
                        oppositeDimension = world.getDimension(DIM_NORMAL_CEILING);
                        break;
                    case DIM_NORMAL_CEILING:
                        oppositeDimension = world.getDimension(DIM_NORMAL);
                        break;
                    case DIM_NETHER:
                        oppositeDimension = world.getDimension(DIM_NETHER_CEILING);
                        break;
                    case DIM_NETHER_CEILING:
                        oppositeDimension = world.getDimension(DIM_NETHER);
                        break;
                    case DIM_END:
                        oppositeDimension = world.getDimension(DIM_END_CEILING);
                        break;
                    case DIM_END_CEILING:
                        oppositeDimension = world.getDimension(DIM_END);
                        break;
                }
                if (oppositeDimension != null) {
                    oppositeDimension.setLastViewPosition(viewPosition);
                }
            }
        }
        this.dimension.unregister();
        currentUndoManager.unregisterActions();
        currentUndoManager = null;
        // currently in use
        if (!paletteManager.isEmpty()) {
            List<CustomLayer> customLayers = new ArrayList<>();
            boolean visibleLayersChanged = false;
            for (Palette palette : paletteManager.clear()) {
                List<CustomLayer> paletteLayers = palette.getLayers();
                customLayers.addAll(paletteLayers);
                for (Layer layer : paletteLayers) {
                    if (hiddenLayers.contains(layer)) {
                        hiddenLayers.remove(layer);
                        visibleLayersChanged = true;
                    }
                    if (layer.equals(soloLayer)) {
                        soloLayer = null;
                        visibleLayersChanged = true;
                    }
                }
                dockingManager.removeFrame(palette.getDockableFrame().getKey());
            }
            if (visibleLayersChanged) {
                updateLayerVisibility();
            }
            layerSoloCheckBoxes.clear();
            this.dimension.setCustomLayers(customLayers);
        } else {
            this.dimension.setCustomLayers(Collections.EMPTY_LIST);
        }
        layersWithNoButton.clear();
        saveCustomBiomes();
    }
    this.dimension = dimension;
    if (dimension != null) {
        // NOI18N
        setTitle("WorldPainter - " + world.getName() + " - " + dimension.getName());
        viewSurfaceMenuItem.setSelected(dimension.getDim() == DIM_NORMAL);
        viewSurfaceCeilingMenuItem.setSelected(dimension.getDim() == DIM_NORMAL_CEILING);
        viewNetherMenuItem.setSelected(dimension.getDim() == DIM_NETHER);
        viewNetherCeilingMenuItem.setSelected(dimension.getDim() == DIM_NETHER_CEILING);
        viewEndMenuItem.setSelected(dimension.getDim() == DIM_END);
        viewEndCeilingMenuItem.setSelected(dimension.getDim() == DIM_END_CEILING);
        // neglected to do it automatically)
        if (dimension.isFixOverlayCoords()) {
            Toolkit.getDefaultToolkit().beep();
            if (showConfirmDialog(this, "This world was created in an older version of WorldPainter\n" + "in which the overlay offsets were not stored correctly.\n" + "Do you want WorldPainter to fix the offsets now?\n" + "\n" + "If you already manually fixed the offsets using version 1.9.0\n" + "or 1.9.1 of WorldPainter, say no. If you're unsure, say yes.", "Fix Overlay Offset?", YES_NO_OPTION, QUESTION_MESSAGE) != NO_OPTION) {
                dimension.setOverlayOffsetX(dimension.getOverlayOffsetX() + dimension.getLowestX() << TILE_SIZE_BITS);
                dimension.setOverlayOffsetY(dimension.getOverlayOffsetY() + dimension.getLowestY() << TILE_SIZE_BITS);
            }
            dimension.setFixOverlayCoords(false);
        }
        view.setDimension(dimension);
        view.moveTo(dimension.getLastViewPosition());
        setDimensionControlStates(world.getPlatform());
        if ((!"true".equals(System.getProperty("org.pepsoft.worldpainter.disableUndo"))) && config.isUndoEnabled()) {
            currentUndoManager = undoManagers.get(dimension.getDim());
            if (currentUndoManager == null) {
                currentUndoManager = new UndoManager(ACTION_UNDO, ACTION_REDO, Math.max(config.getUndoLevels() + 1, 2));
                undoManagers.put(dimension.getDim(), currentUndoManager);
                currentUndoManager.setStopAtClasses(PropertyChangeListener.class, Tile.Listener.class, Biome.class, BetterAction.class);
                dimension.register(currentUndoManager);
            } else {
                currentUndoManager.registerActions(ACTION_UNDO, ACTION_REDO);
            }
            dimension.armSavePoint();
        } else {
            // Still install an undo manager, because some operations depend
            // on one level of undo being available
            currentUndoManager = new UndoManager(2);
            currentUndoManager.setStopAtClasses(PropertyChangeListener.class, Tile.Listener.class, Biome.class, BetterAction.class);
            dimension.register(currentUndoManager);
            ACTION_UNDO.setEnabled(false);
            ACTION_REDO.setEnabled(false);
        }
        if (threeDeeFrame != null) {
            threeDeeFrame.setDimension(dimension);
        }
        // Add the custom object layers from the world
        StringBuilder warnings = new StringBuilder();
        for (CustomLayer customLayer : dimension.getCustomLayers()) {
            if (customLayer.isHide()) {
                layersWithNoButton.add(customLayer);
            } else {
                registerCustomLayer(customLayer, false);
            }
            if (customLayer instanceof CombinedLayer) {
                if (!((CombinedLayer) customLayer).restoreCustomTerrain()) {
                    if (warnings.length() == 0) {
                        warnings.append("The Custom Terrain for one or more Combined Layer could not be restored:\n\n");
                    }
                    warnings.append(customLayer.getName()).append('\n');
                } else {
                    // Check for a custom terrain type and if necessary make
                    // sure it has a button
                    Terrain terrain = ((CombinedLayer) customLayer).getTerrain();
                    if ((terrain != null) && terrain.isCustom() && (customMaterialButtons[terrain.getCustomTerrainIndex()] == null)) {
                        addButtonForNewCustomTerrain(terrain.getCustomTerrainIndex(), Terrain.getCustomMaterial(terrain.getCustomTerrainIndex()), false);
                    }
                }
            }
        }
        if (warnings.length() > 0) {
            warnings.append("\nThe Custom Terrain has been removed from the layer(s).");
            showMessageDialog(this, warnings.toString(), "Custom Terrain(s) Not Restored", ERROR_MESSAGE);
        }
        // Set action states
        ACTION_GRID.setSelected(view.isPaintGrid());
        ACTION_CONTOURS.setSelected(view.isDrawContours());
        ACTION_OVERLAY.setSelected(view.isDrawOverlay());
        // TODO: make this work correctly with undo/redo, and make "inside selection" ineffective when there is no selection, to avoid confusion
        // Set operation states
        // if (dimension.containsOneOf(SelectionChunk.INSTANCE, SelectionBlock.INSTANCE)) {
        // selectionState.setValue(true);
        // } else {
        // if (activeOperation instanceof CopySelectionOperation) {
        // deselectTool();
        // }
        // selectionState.setValue(false);
        // }
        // Load custom biomes. But first remove any that are now regular
        // biomes
        List<CustomBiome> customBiomes = dimension.getCustomBiomes();
        if (customBiomes != null) {
            for (Iterator<CustomBiome> i = customBiomes.iterator(); i.hasNext(); ) {
                CustomBiome customBiome = i.next();
                if (autoBiomeScheme.isBiomePresent(customBiome.getId())) {
                    i.remove();
                }
            }
            if (customBiomes.isEmpty()) {
                customBiomes = null;
            }
        }
        programmaticChange = true;
        try {
            customBiomeManager.setCustomBiomes(customBiomes);
        } finally {
            programmaticChange = false;
        }
        dimension.addPropertyChangeListener(this);
    } else {
        view.setDimension(null);
        // NOI18N
        setTitle("WorldPainter");
        // Clear action states
        ACTION_GRID.setSelected(false);
        ACTION_CONTOURS.setSelected(false);
        ACTION_OVERLAY.setSelected(false);
        // Close the 3D view
        if (threeDeeFrame != null) {
            threeDeeFrame.dispose();
            threeDeeFrame = null;
        }
        // Clear status bar
        locationLabel.setText(strings.getString("location-"));
        heightLabel.setText(" ");
        waterLabel.setText(" ");
        biomeLabel.setText(" ");
        materialLabel.setText(" ");
        // Deselect any current operation
        if (activeOperation != null) {
            deselectTool();
        }
        // TODO: make this work correctly with undo/redo, and make "inside selection" ineffective when there is no selection, to avoid confusion
        // Disable copy selection operation
        // selectionState.setValue(false);
        programmaticChange = true;
        try {
            customBiomeManager.setCustomBiomes(null);
        } finally {
            programmaticChange = false;
        }
    }
}
Also used : TunnelLayer(org.pepsoft.worldpainter.layers.tunnel.TunnelLayer) UndergroundPocketsLayer(org.pepsoft.worldpainter.layers.pockets.UndergroundPocketsLayer) GroundCoverLayer(org.pepsoft.worldpainter.layers.groundcover.GroundCoverLayer) PlantLayer(org.pepsoft.worldpainter.layers.plants.PlantLayer) UndoManager(org.pepsoft.util.undo.UndoManager) Terrain(org.pepsoft.worldpainter.Terrain) CustomBiome(org.pepsoft.worldpainter.biomeschemes.CustomBiome)

Example 3 with Terrain

use of org.pepsoft.worldpainter.Terrain in project WorldPainter by Captain-Chaos.

the class NewWorldDialog method createTileFactory.

private TileFactory createTileFactory(long seed) {
    Terrain terrain = (Terrain) comboBoxSurfaceMaterial.getSelectedItem();
    int baseHeight = (Integer) spinnerTerrainLevel.getValue();
    int waterHeight = (Integer) spinnerWaterLevel.getValue();
    float range = ((Number) spinnerRange.getValue()).floatValue();
    double scale = ((Integer) spinnerScale.getValue()) / 100.0;
    boolean floodWithLava = checkBoxLava.isSelected();
    boolean beaches = checkBoxBeaches.isSelected();
    int maxHeight = (Integer) comboBoxMaxHeight.getSelectedItem();
    HeightMapTileFactory tileFactory;
    if ("true".equals(System.getProperty("org.pepsoft.worldpainter.fancyworlds"))) {
        tileFactory = TileFactoryFactory.createFancyTileFactory(seed, terrain, maxHeight, baseHeight, waterHeight, floodWithLava, range, scale);
    } else {
        // HeightMapTileFactory tileFactory = new ExperimentalTileFactory(maxHeight);
        if (radioButtonHilly.isSelected()) {
            tileFactory = TileFactoryFactory.createNoiseTileFactory(seed, terrain, maxHeight, baseHeight, waterHeight, floodWithLava, beaches, range, scale);
        } else {
            tileFactory = TileFactoryFactory.createFlatTileFactory(seed, terrain, maxHeight, baseHeight, waterHeight, floodWithLava, beaches);
        }
        if (radioButtonAdvancedTerrain.isSelected()) {
            theme.setWaterHeight((Integer) spinnerWaterLevel.getValue());
            theme.setBeaches(checkBoxBeaches.isSelected());
            tileFactory.setTheme(theme);
        }
        Configuration config = Configuration.getInstance();
        Dimension defaults = config.getDefaultTerrainAndLayerSettings();
        if ((dim == DIM_NORMAL) && (defaults.getTileFactory() instanceof HeightMapTileFactory) && (((HeightMapTileFactory) defaults.getTileFactory()).getTheme() instanceof SimpleTheme) && (((SimpleTheme) ((HeightMapTileFactory) defaults.getTileFactory()).getTheme()).getTerrainRanges() != null)) {
            HeightMapTileFactory defaultTileFactory = (HeightMapTileFactory) defaults.getTileFactory();
            SimpleTheme defaultTheme = (SimpleTheme) defaultTileFactory.getTheme();
            if (radioButtonSimpleTerrain.isSelected()) {
                SortedMap<Integer, Terrain> terrainRanges = new TreeMap<>(defaultTheme.getTerrainRanges());
                int surfaceLevel = terrainRanges.headMap(waterHeight + 3).lastKey();
                terrainRanges.put(surfaceLevel, terrain);
                SimpleTheme theme = (SimpleTheme) tileFactory.getTheme();
                theme.setTerrainRanges(terrainRanges);
                theme.setRandomise(defaultTheme.isRandomise());
            }
        } else if ((dim != DIM_NORMAL) && radioButtonSimpleTerrain.isSelected()) {
            // Override the default terrain map:
            SortedMap<Integer, Terrain> terrainMap = new TreeMap<>();
            terrainMap.put(-1, terrain);
            SimpleTheme theme = (SimpleTheme) tileFactory.getTheme();
            theme.setTerrainRanges(terrainMap);
        }
    }
    return tileFactory;
}
Also used : SimpleTheme(org.pepsoft.worldpainter.themes.SimpleTheme) Terrain(org.pepsoft.worldpainter.Terrain)

Example 4 with Terrain

use of org.pepsoft.worldpainter.Terrain in project WorldPainter by Captain-Chaos.

the class UndergroundPocketsDialog method updateNameAndColour.

private void updateNameAndColour() {
    if (fieldName.isEnabled()) {
        if (radioButtonCustomMaterial.isSelected()) {
            MixedMaterial material = mixedMaterialChooser.getMaterial();
            if (material != null) {
                fieldName.setText(material.toString());
                if (material.getColour() != null) {
                    selectedColour = material.getColour();
                    setLabelColour();
                }
            }
        } else {
            Terrain terrain = (Terrain) comboBoxTerrain.getSelectedItem();
            if (terrain != null) {
                fieldName.setText(terrain.getName());
            }
        }
    }
}
Also used : Terrain(org.pepsoft.worldpainter.Terrain) MixedMaterial(org.pepsoft.worldpainter.MixedMaterial)

Example 5 with Terrain

use of org.pepsoft.worldpainter.Terrain in project WorldPainter by Captain-Chaos.

the class UndergroundPocketsDialog method updatePreview.

private void updatePreview() {
    if (previewUpdateTimer != null) {
        // Superfluous?
        previewUpdateTimer.stop();
        previewUpdateTimer = null;
    }
    MixedMaterial material = radioButtonCustomMaterial.isSelected() ? mixedMaterialChooser.getMaterial() : null;
    Terrain terrain = radioButtonTerrain.isSelected() ? (Terrain) comboBoxTerrain.getSelectedItem() : null;
    if ((material == null) && (terrain == null)) {
        return;
    }
    int occurrence = (Integer) spinnerOccurrence.getValue();
    int scale = (Integer) spinnerScale.getValue();
    int minLevel = (Integer) spinnerMinLevel.getValue();
    int maxLevel = (Integer) spinnerMaxLevel.getValue();
    UndergroundPocketsLayer tmpLayer = new UndergroundPocketsLayer("tmp", material, terrain, occurrence, minLevel, maxLevel, scale, 0);
    labelPreview.setIcon(new ImageIcon(((UndergroundPocketsLayerExporter) tmpLayer.getExporter()).createPreview(labelPreview.getWidth(), labelPreview.getHeight())));
}
Also used : Terrain(org.pepsoft.worldpainter.Terrain) MixedMaterial(org.pepsoft.worldpainter.MixedMaterial)

Aggregations

Terrain (org.pepsoft.worldpainter.Terrain)16 Tile (org.pepsoft.worldpainter.Tile)4 MixedMaterial (org.pepsoft.worldpainter.MixedMaterial)3 GroundCoverLayer (org.pepsoft.worldpainter.layers.groundcover.GroundCoverLayer)3 PlantLayer (org.pepsoft.worldpainter.layers.plants.PlantLayer)3 UndergroundPocketsLayer (org.pepsoft.worldpainter.layers.pockets.UndergroundPocketsLayer)3 TunnelLayer (org.pepsoft.worldpainter.layers.tunnel.TunnelLayer)3 Map (java.util.Map)2 SortedMap (java.util.SortedMap)2 TreeMap (java.util.TreeMap)2 TableModelEvent (javax.swing.event.TableModelEvent)2 TableModelListener (javax.swing.event.TableModelListener)2 CombinedLayer (org.pepsoft.worldpainter.layers.CombinedLayer)2 Layer (org.pepsoft.worldpainter.layers.Layer)2 Random (java.util.Random)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 FileFilter (javax.swing.filechooser.FileFilter)1 ChunkFactory (org.pepsoft.minecraft.ChunkFactory)1 PerlinNoise (org.pepsoft.util.PerlinNoise)1 UndoManager (org.pepsoft.util.undo.UndoManager)1