Search in sources :

Example 16 with Layer

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

the class LayerRangesTableModel method getLayerMap.

public Map<Filter, Layer> getLayerMap() {
    if (!layers.isEmpty()) {
        Map<Filter, Layer> layerMap = new HashMap<>();
        Iterator<Filter> filterIterator = filters.iterator();
        for (Layer layer : layers) {
            Filter filter = filterIterator.next();
            layerMap.put(filter, layer);
        }
        return layerMap;
    } else {
        return null;
    }
}
Also used : HeightFilter(org.pepsoft.worldpainter.themes.HeightFilter) Filter(org.pepsoft.worldpainter.themes.Filter) HashMap(java.util.HashMap) Layer(org.pepsoft.worldpainter.layers.Layer)

Example 17 with Layer

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

the class Tile method releaseEvents.

/**
 * Release an inhibition on firing events. Will fire all appropriate events at this time, if the tile was modified
 * since the first invocation of {@link #inhibitEvents()}, but only if this is the last invocation of
 * <code>releaseEvents()</code> in a nested set.
 */
public synchronized void releaseEvents() {
    if (eventInhibitionCounter > 0) {
        eventInhibitionCounter--;
        if (eventInhibitionCounter == 0) {
            if (heightMapDirty) {
                heightMapChanged();
                heightMapDirty = false;
            }
            if (terrainDirty) {
                terrainChanged();
                terrainDirty = false;
            }
            if (waterLevelDirty) {
                waterLevelChanged();
                waterLevelDirty = false;
            }
            if (bitLayersDirty) {
                allBitLayerDataChanged();
                bitLayersDirty = false;
                for (Iterator<Layer> i = dirtyLayers.iterator(); i.hasNext(); ) {
                    DataSize dataSize = i.next().getDataSize();
                    if ((dataSize == DataSize.BIT) || (dataSize == DataSize.BIT_PER_CHUNK)) {
                        i.remove();
                    }
                }
            }
            if (nonBitLayersDirty) {
                allNonBitLayerDataChanged();
                nonBitLayersDirty = false;
                for (Iterator<Layer> i = dirtyLayers.iterator(); i.hasNext(); ) {
                    DataSize dataSize = i.next().getDataSize();
                    if ((dataSize != DataSize.BIT) && (dataSize != DataSize.BIT_PER_CHUNK)) {
                        i.remove();
                    }
                }
            }
            if (!dirtyLayers.isEmpty()) {
                Set<Layer> changedLayers = Collections.unmodifiableSet(dirtyLayers);
                for (Listener listener : listeners) {
                    listener.layerDataChanged(this, changedLayers);
                }
                dirtyLayers.clear();
            }
            if (seedsDirty) {
                seedsChanged();
                seedsDirty = false;
            }
        }
    } else {
        throw new IllegalStateException("Events not inhibited");
    }
}
Also used : UndoListener(org.pepsoft.util.undo.UndoListener) DataSize(org.pepsoft.worldpainter.layers.Layer.DataSize) Layer(org.pepsoft.worldpainter.layers.Layer)

Example 18 with Layer

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

the class Tile method clearLayerData.

/**
 * Clear all layer data at a particular location (by resetting to the
 * layer's default value), possibly with the exception of certain layers.
 *
 * @param x The X coordinate of the location to clear of layer data.
 * @param y The Y coordinate of the location to clear of layer data.
 * @param excludedLayers The layers to exclude, if any. May be
 *                       <code>null</code>.
 */
public void clearLayerData(int x, int y, Set<Layer> excludedLayers) {
    ensureWriteable(BIT_LAYER_DATA);
    for (Map.Entry<Layer, BitSet> entry : bitLayerData.entrySet()) {
        Layer layer = entry.getKey();
        if ((excludedLayers != null) && excludedLayers.contains(layer)) {
            continue;
        }
        int bitOffset;
        if (layer.getDataSize() == Layer.DataSize.BIT) {
            bitOffset = x | (y << TILE_SIZE_BITS);
        } else {
            bitOffset = (x / 16) + (y / 16) * (TILE_SIZE / 16);
        }
        entry.getValue().set(bitOffset, layer.getDefaultValue() != 0);
        layerDataChanged(layer);
    }
    ensureWriteable(LAYER_DATA);
    for (Map.Entry<Layer, byte[]> entry : layerData.entrySet()) {
        Layer layer = entry.getKey();
        if ((excludedLayers != null) && excludedLayers.contains(layer)) {
            continue;
        }
        byte[] layerValues = entry.getValue();
        switch(layer.getDataSize()) {
            case NIBBLE:
                int byteOffset = x | (y << TILE_SIZE_BITS);
                byte _byte = layerValues[byteOffset / 2];
                if (byteOffset % 2 == 0) {
                    _byte &= 0xF0;
                    _byte |= layer.getDefaultValue();
                } else {
                    _byte &= 0x0F;
                    _byte |= (layer.getDefaultValue() << 4);
                }
                layerValues[byteOffset / 2] = _byte;
                break;
            case BYTE:
                byteOffset = x | (y << TILE_SIZE_BITS);
                layerValues[byteOffset] = (byte) layer.getDefaultValue();
                break;
            default:
                throw new InternalError();
        }
        layerDataChanged(layer);
    }
}
Also used : BitSet(java.util.BitSet) HashMap(java.util.HashMap) Map(java.util.Map) Layer(org.pepsoft.worldpainter.layers.Layer) Point(java.awt.Point)

Example 19 with Layer

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

the class Tile method getLayersAt.

/**
 * Gets all layers that are set at the specified location, along with their
 * intensities. For bit valued layers the intensity is zero for off, one for
 * on.
 *
 * @param x The X location for which to retrieve all layers.
 * @param y The Y location for which to retrieve all layers.
 * @return A map with all layers set at the specified location, mapped to
 *     their intensities at that location. May either be <code>null</code>
 *     or an empty map if no layers are present.
 */
public Map<Layer, Integer> getLayersAt(int x, int y) {
    Map<Layer, Integer> layers = null;
    ensureReadable(LAYER_DATA);
    for (Map.Entry<Layer, byte[]> entry : layerData.entrySet()) {
        Layer layer = entry.getKey();
        byte[] layerValues = entry.getValue();
        int value;
        if (layer.getDataSize() == DataSize.NIBBLE) {
            int byteOffset = x | (y << TILE_SIZE_BITS);
            byte _byte = layerValues[byteOffset / 2];
            if (byteOffset % 2 == 0) {
                value = _byte & 0x0F;
            } else {
                value = (_byte & 0xF0) >> 4;
            }
        } else {
            value = layerValues[x | (y << TILE_SIZE_BITS)] & 0xFF;
        }
        if (value != layer.getDefaultValue()) {
            if (layers == null) {
                layers = new HashMap<>();
            }
            layers.put(layer, value);
        }
    }
    ensureReadable(BIT_LAYER_DATA);
    for (Map.Entry<Layer, BitSet> entry : bitLayerData.entrySet()) {
        Layer layer = entry.getKey();
        BitSet layerValues = entry.getValue();
        int value;
        if (layer.getDataSize() == Layer.DataSize.BIT) {
            value = layerValues.get(x | (y << TILE_SIZE_BITS)) ? 1 : 0;
        } else {
            value = layerValues.get((x >> 4) + (y >> 4) * (TILE_SIZE >> 4)) ? 1 : 0;
        }
        if (value != layer.getDefaultValue()) {
            if (layers == null) {
                layers = new HashMap<>();
            }
            layers.put(layer, value);
        }
    }
    return layers;
}
Also used : BitSet(java.util.BitSet) Layer(org.pepsoft.worldpainter.layers.Layer) HashMap(java.util.HashMap) Map(java.util.Map) Point(java.awt.Point)

Example 20 with Layer

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

the class Tile method transform.

/**
 * Create a new tile based on this one but horizontally transformed according to some transformation.
 *
 * @param transform The transform to apply.
 * @return A new tile with the same contents, except transformed according to the specified transform (including the
 * X and Y coordinates).
 */
public synchronized Tile transform(CoordinateTransform transform) {
    Point transformedCoords = transform.transform(x << TILE_SIZE_BITS, y << TILE_SIZE_BITS);
    Tile transformedTile;
    boolean transformContents = ((transformedCoords.x & TILE_SIZE_MASK) != 0) || ((transformedCoords.y & TILE_SIZE_MASK) != 0);
    if (transformContents) {
        transformedTile = new Tile(transformedCoords.x >> TILE_SIZE_BITS, transformedCoords.y >> TILE_SIZE_BITS, maxHeight);
        for (int x = 0; x < TILE_SIZE; x++) {
            for (int y = 0; y < TILE_SIZE; y++) {
                transformedCoords.x = x;
                transformedCoords.y = y;
                transform.transformInPlace(transformedCoords);
                transformedCoords.x &= TILE_SIZE_MASK;
                transformedCoords.y &= TILE_SIZE_MASK;
                transformedTile.setTerrain(transformedCoords.x, transformedCoords.y, getTerrain(x, y));
                transformedTile.setRawHeight(transformedCoords.x, transformedCoords.y, getRawHeight(x, y));
                transformedTile.setWaterLevel(transformedCoords.x, transformedCoords.y, getWaterLevel(x, y));
            }
        }
        for (Layer layer : getLayers()) {
            if ((layer.getDataSize() == Layer.DataSize.BIT) || (layer.getDataSize() == Layer.DataSize.BIT_PER_CHUNK)) {
                for (int x = 0; x < TILE_SIZE; x++) {
                    for (int y = 0; y < TILE_SIZE; y++) {
                        if (getBitLayerValue(layer, x, y)) {
                            transformedCoords.x = x;
                            transformedCoords.y = y;
                            transform.transformInPlace(transformedCoords);
                            transformedCoords.x &= TILE_SIZE_MASK;
                            transformedCoords.y &= TILE_SIZE_MASK;
                            transformedTile.setBitLayerValue(layer, transformedCoords.x, transformedCoords.y, true);
                        }
                    }
                }
            } else if (layer.getDataSize() != Layer.DataSize.NONE) {
                for (int x = 0; x < TILE_SIZE; x++) {
                    for (int y = 0; y < TILE_SIZE; y++) {
                        int value = getLayerValue(layer, x, y);
                        if (value > 0) {
                            transformedCoords.x = x;
                            transformedCoords.y = y;
                            transform.transformInPlace(transformedCoords);
                            transformedCoords.x &= TILE_SIZE_MASK;
                            transformedCoords.y &= TILE_SIZE_MASK;
                            transformedTile.setLayerValue(layer, transformedCoords.x, transformedCoords.y, value);
                        }
                    }
                }
            }
        }
    } else {
        // The transformation does not affect intra-tile coordinates, so just copy the buffers without transforming them
        transformedTile = new Tile(transformedCoords.x >> TILE_SIZE_BITS, transformedCoords.y >> TILE_SIZE_BITS, maxHeight, false);
        transformedTile.heightMap = copyObject(heightMap);
        transformedTile.tallHeightMap = copyObject(tallHeightMap);
        transformedTile.terrain = terrain.clone();
        transformedTile.waterLevel = copyObject(waterLevel);
        transformedTile.tallWaterLevel = copyObject(tallWaterLevel);
        transformedTile.layerData = copyObject(layerData);
        transformedTile.bitLayerData = copyObject(bitLayerData);
        transformedTile.init();
    }
    if (seeds != null) {
        transformedTile.seeds = new HashSet<>();
        transformedTile.seeds.addAll(seeds);
        for (Seed seed : transformedTile.seeds) {
            seed.transform(transform);
        }
    }
    return transformedTile;
}
Also used : Seed(org.pepsoft.worldpainter.gardenofeden.Seed) Point(java.awt.Point) Layer(org.pepsoft.worldpainter.layers.Layer) Point(java.awt.Point)

Aggregations

Layer (org.pepsoft.worldpainter.layers.Layer)34 CustomLayer (org.pepsoft.worldpainter.layers.CustomLayer)9 Map (java.util.Map)8 ProgressReceiver (org.pepsoft.util.ProgressReceiver)7 HashMap (java.util.HashMap)6 OperationCancelled (org.pepsoft.util.ProgressReceiver.OperationCancelled)5 ExporterSettings (org.pepsoft.worldpainter.layers.exporters.ExporterSettings)5 Point (java.awt.Point)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 SubProgressReceiver (org.pepsoft.util.SubProgressReceiver)4 TILE_SIZE (org.pepsoft.worldpainter.Constants.TILE_SIZE)4 Biome (org.pepsoft.worldpainter.layers.Biome)4 Bo2Layer (org.pepsoft.worldpainter.layers.Bo2Layer)4 CombinedLayer (org.pepsoft.worldpainter.layers.CombinedLayer)4 GroundCoverLayer (org.pepsoft.worldpainter.layers.groundcover.GroundCoverLayer)4 PlantLayer (org.pepsoft.worldpainter.layers.plants.PlantLayer)4 File (java.io.File)3 Arrays (java.util.Arrays)3 BitSet (java.util.BitSet)3