use of org.pepsoft.worldpainter.operations.Filter in project WorldPainter by Captain-Chaos.
the class FillDialog method clearLayer.
private void clearLayer(ProgressReceiver progressReceiver) throws OperationCancelled {
Layer layer = (Layer) comboBoxClearLayer.getSelectedItem();
if (filter == null) {
dimension.clearLayerData(layer);
} else {
if (layer.getDataSize() == Layer.DataSize.NIBBLE) {
dimension.visitTiles().forFilter(filter).andDo(tile -> {
final int worldTileX = tile.getX() << TILE_SIZE_BITS;
final int worldTileY = tile.getY() << TILE_SIZE_BITS;
for (int x = 0; x < TILE_SIZE; x++) {
for (int y = 0; y < TILE_SIZE; y++) {
int oldLayervalue = tile.getLayerValue(layer, x, y);
int layerValue;
if (filter == null) {
layerValue = 0;
} else {
layerValue = Math.min(oldLayervalue, 15 - (int) (filter.modifyStrength(worldTileX | x, worldTileY | y, 1.0f) * 15));
}
if (oldLayervalue != layerValue) {
tile.setLayerValue(layer, x, y, layerValue);
}
}
}
}, progressReceiver);
} else if (layer.getDataSize() == Layer.DataSize.BIT) {
dimension.visitTiles().forFilter(filter).andDo(tile -> {
final int worldTileX = tile.getX() << TILE_SIZE_BITS;
final int worldTileY = tile.getY() << TILE_SIZE_BITS;
for (int x = 0; x < TILE_SIZE; x++) {
for (int y = 0; y < TILE_SIZE; y++) {
boolean set;
if (filter == null) {
set = true;
} else {
float strength = filter.modifyStrength(worldTileX | x, worldTileY | y, 1.0f);
set = (strength > 0.95f) || (Math.random() < strength);
}
if (set && tile.getBitLayerValue(layer, x, y)) {
tile.setBitLayerValue(layer, x, y, false);
}
}
}
}, progressReceiver);
} else if (layer.getDataSize() == Layer.DataSize.BIT_PER_CHUNK) {
dimension.visitTiles().forFilter(filter).andDo(tile -> {
final int worldTileX = tile.getX() << TILE_SIZE_BITS;
final int worldTileY = tile.getY() << TILE_SIZE_BITS;
for (int x = 0; x < TILE_SIZE; x += 16) {
for (int y = 0; y < TILE_SIZE; y += 16) {
boolean set;
if (filter == null) {
set = true;
} else {
float strength = filter.modifyStrength(worldTileX | x, worldTileY | y, 1.0f);
set = (strength > 0.95f) || (Math.random() < strength);
}
if (set && tile.getBitLayerValue(layer, x, y)) {
tile.setBitLayerValue(layer, x, y, false);
}
}
}
}, progressReceiver);
} else {
throw new UnsupportedOperationException();
}
}
}
use of org.pepsoft.worldpainter.operations.Filter in project WorldPainter by Captain-Chaos.
the class BrushOptions method filterChanged.
private void filterChanged() {
if (listener != null) {
Filter filter = getFilter();
if (logger.isTraceEnabled()) {
logger.trace("Reporting new filter " + filter + " to listener " + listener);
}
listener.filterChanged(filter);
}
}
use of org.pepsoft.worldpainter.operations.Filter in project WorldPainter by Captain-Chaos.
the class Dimension method init.
private void init() {
listeners = new ArrayList<>();
dirtyTiles = new HashSet<>();
addedTiles = new HashSet<>();
removedTiles = new HashSet<>();
propertyChangeSupport = new PropertyChangeSupport(this);
garden = new WPGarden();
topLayerDepthNoise = new PerlinNoise(seed + TOP_LAYER_DEPTH_SEED_OFFSET);
for (Tile tile : tiles.values()) {
tile.addListener(this);
Set<Seed> seeds = tile.getSeeds();
if (seeds != null) {
for (Seed gardenSeed : seeds) {
gardenSeed.garden = garden;
}
}
}
// Legacy support
if (wpVersion < 1) {
if (borderSize == 0) {
borderSize = 2;
}
if (overlayScale == 0.0f) {
overlayScale = 1.0f;
}
if (overlayTransparency == 0.0f) {
overlayTransparency = 0.5f;
}
if (gridSize == 0) {
gridSize = 128;
}
if (!biomesConverted) {
// Convert the nibble sized biomes data from a legacy map (by
// deleting it so that it will be recalculated
tiles.values().forEach(org.pepsoft.worldpainter.Tile::convertBiomeData);
biomesConverted = true;
}
if (maxHeight == 0) {
maxHeight = 128;
}
if (subsurfaceMaterial == Terrain.RESOURCES) {
subsurfaceMaterial = Terrain.STONE;
// Load legacy settings
ResourcesExporterSettings settings = new ResourcesExporterSettings(maxHeight);
settings.setChance(BLK_GOLD_ORE, 1);
settings.setChance(BLK_IRON_ORE, 5);
settings.setChance(BLK_COAL, 9);
settings.setChance(BLK_LAPIS_LAZULI_ORE, 1);
settings.setChance(BLK_DIAMOND_ORE, 1);
settings.setChance(BLK_REDSTONE_ORE, 6);
settings.setChance(BLK_WATER, 1);
settings.setChance(BLK_LAVA, 1);
settings.setChance(BLK_DIRT, 9);
settings.setChance(BLK_GRAVEL, 9);
settings.setMaxLevel(BLK_GOLD_ORE, Terrain.GOLD_LEVEL);
settings.setMaxLevel(BLK_IRON_ORE, Terrain.IRON_LEVEL);
settings.setMaxLevel(BLK_COAL, Terrain.COAL_LEVEL);
settings.setMaxLevel(BLK_LAPIS_LAZULI_ORE, Terrain.LAPIS_LAZULI_LEVEL);
settings.setMaxLevel(BLK_DIAMOND_ORE, Terrain.DIAMOND_LEVEL);
settings.setMaxLevel(BLK_REDSTONE_ORE, Terrain.REDSTONE_LEVEL);
settings.setMaxLevel(BLK_WATER, Terrain.WATER_LEVEL);
settings.setMaxLevel(BLK_LAVA, Terrain.LAVA_LEVEL);
settings.setMaxLevel(BLK_DIRT, Terrain.DIRT_LEVEL);
settings.setMaxLevel(BLK_GRAVEL, Terrain.GRAVEL_LEVEL);
layerSettings.put(Resources.INSTANCE, settings);
}
if (contourSeparation == 0) {
contourSeparation = 10;
}
if (topLayerMinDepth == 0) {
topLayerMinDepth = 3;
topLayerVariation = 4;
}
if (lastViewPosition == null) {
lastViewPosition = new Point();
}
if ((customLayers == null) || customLayers.isEmpty()) {
// The customLayers.isEmpty() is to fix a bug which escaped in a beta
customLayers = new ArrayList<>();
customLayers.addAll(getAllLayers(false).stream().filter(layer -> layer instanceof CustomLayer).map(layer -> (CustomLayer) layer).collect(Collectors.toList()));
}
}
if (wpVersion < 2) {
if (overlay != null) {
fixOverlayCoords = true;
}
}
if (wpVersion < 3) {
ceilingHeight = maxHeight;
}
wpVersion = CURRENT_WP_VERSION;
// Make sure that any custom layers which somehow ended up in the world
// are on the custom layer list so they will be added to a palette in
// the GUI. TODO: fix this properly
// Make sure customLayers isn't some weird read-only list
getAllLayers(false).stream().filter(layer -> (layer instanceof CustomLayer) && (!customLayers.contains(layer))).forEach(layer -> {
if ((!(customLayers instanceof ArrayList)) && (!(customLayers instanceof LinkedList))) {
// Make sure customLayers isn't some weird read-only list
customLayers = new ArrayList<>(customLayers);
}
customLayers.add((CustomLayer) layer);
});
}
use of org.pepsoft.worldpainter.operations.Filter in project WorldPainter by Captain-Chaos.
the class BrushOptions method createObjectSelectionMenu.
private JPopupMenu createObjectSelectionMenu(final ObjectSelectionListener listener) {
JMenuItem waterItem = new JMenuItem("Water");
waterItem.addActionListener(e -> listener.objectSelected(DefaultFilter.WATER, "Water", null));
JMenu popupMenu = new JMenu();
popupMenu.add(waterItem);
JMenuItem lavaItem = new JMenuItem("Lava");
lavaItem.addActionListener(e -> listener.objectSelected(DefaultFilter.LAVA, "Lava", null));
popupMenu.add(lavaItem);
JMenuItem landItem = new JMenuItem("Land");
landItem.addActionListener(e -> listener.objectSelected(DefaultFilter.LAND, "Land", null));
popupMenu.add(landItem);
JMenu terrainMenu = new JMenu("Terrain");
JMenu customTerrainMenu = new JMenu("Custom");
JMenu stainedClayTerrainMenu = new JMenu("Stained Clay");
App app = App.getInstance();
ColourScheme colourScheme = app.getColourScheme();
for (Terrain terrain : Terrain.getConfiguredValues()) {
final Terrain selectedTerrain = terrain;
final String name = terrain.getName();
final Icon icon = new ImageIcon(terrain.getIcon(colourScheme));
JMenuItem menuItem = new JMenuItem(name, icon);
menuItem.addActionListener(e -> listener.objectSelected(selectedTerrain, name, icon));
if (terrain.isCustom()) {
customTerrainMenu.add(menuItem);
} else if (terrain.getName().endsWith(" Clay") && (terrain != Terrain.HARDENED_CLAY)) {
stainedClayTerrainMenu.add(menuItem);
} else {
terrainMenu.add(menuItem);
}
}
terrainMenu.add(stainedClayTerrainMenu);
if (customTerrainMenu.getMenuComponentCount() > 0) {
terrainMenu.add(customTerrainMenu);
}
popupMenu.add(terrainMenu);
JMenu layerMenu = new JMenu("Layer");
LayerManager.getInstance().getLayers().stream().filter(l -> !l.equals(Biome.INSTANCE)).forEach(l -> {
JMenuItem menuItem = new JMenuItem(l.getName(), new ImageIcon(l.getIcon()));
menuItem.addActionListener(e -> listener.objectSelected(l, l.getName(), new ImageIcon(l.getIcon())));
layerMenu.add(menuItem);
});
Set<CustomLayer> customLayers = app.getCustomLayers();
if (customLayers.size() > 15) {
// If there are fifteen or more custom layers, split them by palette
// and move them to separate submenus to try and conserve screen
// space
app.getCustomLayersByPalette().entrySet().stream().map((entry) -> {
String palette = entry.getKey();
JMenu paletteMenu = new JMenu(palette != null ? palette : "Hidden Layers");
entry.getValue().forEach(l -> {
JMenuItem menuItem = new JMenuItem(l.getName(), new ImageIcon(l.getIcon()));
menuItem.addActionListener(e -> listener.objectSelected(l, l.getName(), new ImageIcon(l.getIcon())));
paletteMenu.add(menuItem);
});
return paletteMenu;
}).forEach(layerMenu::add);
} else {
customLayers.forEach(l -> {
JMenuItem menuItem = new JMenuItem(l.getName(), new ImageIcon(l.getIcon()));
menuItem.addActionListener(e -> listener.objectSelected(l, l.getName(), new ImageIcon(l.getIcon())));
layerMenu.add(menuItem);
});
}
popupMenu.add(layerMenu);
final JMenu biomeMenu = new JMenu("Biome");
final CustomBiomeManager customBiomeManager = app.getCustomBiomeManager();
final BiomeHelper biomeHelper = new BiomeHelper(autoBiomeScheme, colourScheme, customBiomeManager);
List<CustomBiome> customBiomes = customBiomeManager.getCustomBiomes();
if ((customBiomes != null) && (!customBiomes.isEmpty())) {
JMenu customBiomeMenu = new JMenu("Custom");
for (CustomBiome customBiome : customBiomes) {
final int selectedBiome = customBiome.getId();
final String name = biomeHelper.getBiomeName(selectedBiome) + " (" + selectedBiome + ")";
final Icon icon = biomeHelper.getBiomeIcon(selectedBiome);
final JMenuItem menuItem = new JMenuItem(name, icon);
menuItem.addActionListener(e -> listener.objectSelected(new DefaultFilter.LayerValue(Biome.INSTANCE, selectedBiome), name, icon));
customBiomeMenu.add(menuItem);
}
biomeMenu.add(customBiomeMenu);
}
for (int i = 0; i < autoBiomeScheme.getBiomeCount(); i++) {
if (autoBiomeScheme.isBiomePresent(i)) {
final int selectedBiome = i;
final String name = biomeHelper.getBiomeName(i) + " (" + selectedBiome + ")";
final Icon icon = biomeHelper.getBiomeIcon(i);
final JMenuItem menuItem = new JMenuItem(name, icon);
menuItem.addActionListener(e -> listener.objectSelected(new DefaultFilter.LayerValue(Biome.INSTANCE, selectedBiome), name, icon));
biomeMenu.add(menuItem);
}
}
JMenu autoBiomeSubMenu = new JMenu("Auto Biomes");
JMenuItem autoBiomesMenuItem = new JMenuItem("All Auto Biomes");
autoBiomesMenuItem.addActionListener(e -> listener.objectSelected(DefaultFilter.AUTO_BIOMES, "All Auto Biomes", null));
autoBiomeSubMenu.add(autoBiomesMenuItem);
for (int autoBiome : Dimension.POSSIBLE_AUTO_BIOMES) {
final int selectedBiome = -autoBiome;
final String name = biomeHelper.getBiomeName(autoBiome);
final Icon icon = biomeHelper.getBiomeIcon(autoBiome);
final JMenuItem menuItem = new JMenuItem(name, icon);
menuItem.addActionListener(e -> listener.objectSelected(new DefaultFilter.LayerValue(Biome.INSTANCE, selectedBiome), name, icon));
autoBiomeSubMenu.add(menuItem);
}
biomeMenu.add(autoBiomeSubMenu);
popupMenu.add(biomeMenu);
JMenu annotationsMenu = new JMenu("Annotations");
JMenuItem menuItem = new JMenuItem("All Annotations");
menuItem.addActionListener(e -> listener.objectSelected(new DefaultFilter.LayerValue(Annotations.INSTANCE), "All Annotations", null));
annotationsMenu.add(menuItem);
for (int i = 1; i < 16; i++) {
final int selectedColour = i, dataValue = selectedColour - ((selectedColour < 8) ? 1 : 0);
final Icon icon = IconUtils.createScaledColourIcon(colourScheme.getColour(Constants.BLK_WOOL, dataValue));
menuItem = new JMenuItem(Constants.COLOUR_NAMES[dataValue], icon);
menuItem.addActionListener(e -> listener.objectSelected(new DefaultFilter.LayerValue(Annotations.INSTANCE, selectedColour), Constants.COLOUR_NAMES[dataValue] + " Annotations", icon));
annotationsMenu.add(menuItem);
}
popupMenu.add(annotationsMenu);
popupMenu = breakUpLongMenus(popupMenu, 25);
JPopupMenu result = new JPopupMenu();
Arrays.stream(popupMenu.getMenuComponents()).forEach(result::add);
return result;
}
use of org.pepsoft.worldpainter.operations.Filter in project WorldPainter by Captain-Chaos.
the class FillDialog method fillWithLayer.
private void fillWithLayer(ProgressReceiver progressReceiver) throws UnsupportedOperationException, OperationCancelled {
Layer layer = (Layer) comboBoxSetLayer.getSelectedItem();
if (layer.getDataSize() == Layer.DataSize.NIBBLE) {
int baseLayerValue = Math.round((sliderLayerValue.getValue() + 2) / 6.667f);
dimension.visitTiles().forFilter(filter).andDo(tile -> {
final int worldTileX = tile.getX() << TILE_SIZE_BITS;
final int worldTileY = tile.getY() << TILE_SIZE_BITS;
for (int x = 0; x < TILE_SIZE; x++) {
for (int y = 0; y < TILE_SIZE; y++) {
int layerValue;
if (filter == null) {
layerValue = baseLayerValue;
} else {
layerValue = (int) (filter.modifyStrength(worldTileX | x, worldTileY | y, 1.0f) * baseLayerValue);
}
if (tile.getLayerValue(layer, x, y) < layerValue) {
tile.setLayerValue(layer, x, y, layerValue);
}
}
}
}, progressReceiver);
} else if (layer.getDataSize() == Layer.DataSize.BIT) {
dimension.visitTiles().forFilter(filter).andDo(tile -> {
final int worldTileX = tile.getX() << TILE_SIZE_BITS;
final int worldTileY = tile.getY() << TILE_SIZE_BITS;
for (int x = 0; x < TILE_SIZE; x++) {
for (int y = 0; y < TILE_SIZE; y++) {
boolean set;
if (filter == null) {
set = true;
} else {
float strength = filter.modifyStrength(worldTileX | x, worldTileY | y, 1.0f);
set = (strength > 0.95f) || (Math.random() < strength);
}
if (set && (!tile.getBitLayerValue(layer, x, y))) {
tile.setBitLayerValue(layer, x, y, true);
}
}
}
}, progressReceiver);
} else if (layer.getDataSize() == Layer.DataSize.BIT_PER_CHUNK) {
dimension.visitTiles().forFilter(filter).andDo(tile -> {
final int worldTileX = tile.getX() << TILE_SIZE_BITS;
final int worldTileY = tile.getY() << TILE_SIZE_BITS;
for (int x = 0; x < TILE_SIZE; x += 16) {
for (int y = 0; y < TILE_SIZE; y += 16) {
boolean set;
if (filter == null) {
set = true;
} else {
float strength = filter.modifyStrength(worldTileX | x, worldTileY | y, 1.0f);
set = (strength > 0.95f) || (Math.random() < strength);
}
if (set && (!tile.getBitLayerValue(layer, x, y))) {
tile.setBitLayerValue(layer, x, y, true);
}
}
}
}, progressReceiver);
} else {
throw new UnsupportedOperationException();
}
}
Aggregations