use of org.pepsoft.worldpainter.Dimension in project WorldPainter by Captain-Chaos.
the class Fill method tick.
@Override
protected void tick(int centreX, int centreY, boolean inverse, boolean first, float dynamicLevel) {
painter.setUndo(inverse);
Dimension dimension = getDimension();
dimension.setEventsInhibited(true);
try {
painter.fill(dimension, centreX, centreY, SwingUtilities.getWindowAncestor(getView()));
} catch (IndexOutOfBoundsException e) {
// This most likely indicates that the area being flooded was too
// large
dimension.undoChanges();
JOptionPane.showMessageDialog(getView(), "The area to be flooded is too large; please retry with a smaller area", "Area Too Large", JOptionPane.ERROR_MESSAGE);
} finally {
dimension.setEventsInhibited(false);
}
}
use of org.pepsoft.worldpainter.Dimension in project WorldPainter by Captain-Chaos.
the class Flood method tick.
@Override
protected void tick(int centreX, int centreY, boolean inverse, boolean first, float dynamicLevel) {
Dimension dimension = getDimension();
int terrainHeight = dimension.getIntHeightAt(centreX, centreY);
if (terrainHeight == -1) {
// Not on a tile
return;
}
int waterLevel = dimension.getWaterLevelAt(centreX, centreY);
boolean fluidPresent = waterLevel > terrainHeight;
if (inverse && (!fluidPresent)) {
// No point lowering the water level if there is no water...
return;
}
int height = Math.max(terrainHeight, waterLevel);
int floodToHeight;
if (fluidPresent && (floodWithLava != dimension.getBitLayerValueAt(FloodWithLava.INSTANCE, centreX, centreY))) {
// There is fluid present of a different type; don't change the
// height, just change the type
floodToHeight = height;
inverse = false;
} else {
if (inverse ? (height <= 0) : (height >= (dimension.getMaxHeight() - 1))) {
// Already at the lowest or highest possible point
return;
}
floodToHeight = inverse ? height : (height + 1);
}
synchronized (dimension) {
dimension.setEventsInhibited(true);
}
try {
synchronized (dimension) {
dimension.rememberChanges();
}
QueueLinearFloodFiller flooder = new QueueLinearFloodFiller(dimension, floodToHeight, floodWithLava, inverse);
if (!flooder.floodFill(centreX, centreY, SwingUtilities.getWindowAncestor(getView()))) {
// Cancelled by user
synchronized (dimension) {
if (dimension.undoChanges()) {
dimension.clearRedo();
dimension.armSavePoint();
}
}
}
} finally {
synchronized (dimension) {
dimension.setEventsInhibited(false);
}
}
}
use of org.pepsoft.worldpainter.Dimension in project WorldPainter by Captain-Chaos.
the class Height method tick.
// @Override
// protected void altPressed() {
// dimensionSnapshot = getDimension().getSnapshot();
// }
//
// @Override
// protected void altReleased() {
// dimensionSnapshot = null;
// }
@Override
protected void tick(int centreX, int centreY, boolean inverse, boolean first, float dynamicLevel) {
float adjustment = (float) Math.pow(dynamicLevel * getLevel() * 2, 2.0);
Dimension dimension = getDimension();
int minHeight, maxHeight;
if (getFilter() instanceof DefaultFilter) {
DefaultFilter filter = (DefaultFilter) getFilter();
if (filter.getAboveLevel() != -1) {
minHeight = filter.getAboveLevel();
} else {
minHeight = Integer.MIN_VALUE;
}
if (filter.getBelowLevel() != -1) {
maxHeight = filter.getBelowLevel();
} else {
maxHeight = Integer.MAX_VALUE;
}
} else {
minHeight = Integer.MIN_VALUE;
maxHeight = Integer.MAX_VALUE;
}
boolean applyTheme = options.isApplyTheme();
dimension.setEventsInhibited(true);
try {
int radius = getEffectiveRadius();
float maxZ = dimension.getMaxHeight() - 1;
for (int x = centreX - radius; x <= centreX + radius; x++) {
for (int y = centreY - radius; y <= centreY + radius; y++) {
float currentHeight = dimension.getHeightAt(x, y);
float targetHeight = inverse ? Math.max(currentHeight - adjustment, minHeight) : Math.min(currentHeight + adjustment, maxHeight);
if (targetHeight < 0.0f) {
targetHeight = 0.0f;
} else if (targetHeight > maxZ) {
targetHeight = maxZ;
}
float strength = getFullStrength(centreX, centreY, x, y);
if (strength > 0.0f) {
float newHeight = strength * targetHeight + (1 - strength) * currentHeight;
if (inverse ? (newHeight < currentHeight) : (newHeight > currentHeight)) {
dimension.setHeightAt(x, y, newHeight);
if (applyTheme) {
dimension.applyTheme(x, y);
}
}
}
}
}
} finally {
dimension.setEventsInhibited(false);
}
}
use of org.pepsoft.worldpainter.Dimension in project WorldPainter by Captain-Chaos.
the class Erode method tick.
@Override
protected void tick(int centreX, int centreY, boolean inverse, boolean first, float dynamicLevel) {
Dimension dimension = getDimension();
dimension.setEventsInhibited(true);
try {
int radius = getEffectiveRadius();
for (int i = 0; i < ROUNDS; i++) {
for (int x = centreX - radius; x <= centreX + radius; x++) {
for (int y = centreY - radius; y <= centreY + radius; y++) {
float strength = getStrength(centreX, centreY, x, y);
if ((strength == 1.0f) || (random.nextFloat() < strength)) {
for (int dx = -1; dx <= 1; dx++) {
for (int dy = -1; dy <= 1; dy++) {
heightBuffer[dx + 1][dy + 1] = dimension.getRawHeightAt(x + dx, y + dy);
}
}
int lowestDx = 0, lowestDy = 0, lowestRawHeight = Integer.MAX_VALUE;
boolean reverseX = random.nextBoolean(), reverseY = random.nextBoolean();
if (reverseX) {
for (int dx = 2; dx >= 0; dx--) {
if (reverseY) {
for (int dy = 2; dy >= 0; dy--) {
if (heightBuffer[dx][dy] < lowestRawHeight) {
lowestRawHeight = heightBuffer[dx][dy];
lowestDx = dx;
lowestDy = dy;
}
}
} else {
for (int dy = 0; dy < 3; dy++) {
if (heightBuffer[dx][dy] < lowestRawHeight) {
lowestRawHeight = heightBuffer[dx][dy];
lowestDx = dx;
lowestDy = dy;
}
}
}
}
} else {
for (int dx = 0; dx < 3; dx++) {
if (reverseY) {
for (int dy = 2; dy >= 0; dy--) {
if (heightBuffer[dx][dy] < lowestRawHeight) {
lowestRawHeight = heightBuffer[dx][dy];
lowestDx = dx;
lowestDy = dy;
}
}
} else {
for (int dy = 0; dy < 3; dy++) {
if (heightBuffer[dx][dy] < lowestRawHeight) {
lowestRawHeight = heightBuffer[dx][dy];
lowestDx = dx;
lowestDy = dy;
}
}
}
}
}
if ((lowestDx != 1) || (lowestDy != 1)) {
int difference = heightBuffer[1][1] - heightBuffer[lowestDx][lowestDy];
int amount = Math.min((int) (difference / 2 / (((lowestDx != 1) && (lowestDy != 1)) ? ROOT_OF_TWO : 1)), ERODE_AMOUNT);
amount = (int) ((amount / 64f) * (amount / 64f) * 64);
if (amount > 0) {
dimension.setRawHeightAt(x, y, heightBuffer[1][1] - amount);
dimension.setRawHeightAt(x + lowestDx - 1, y + lowestDy - 1, heightBuffer[lowestDx][lowestDy] + amount);
}
}
}
}
}
}
} finally {
dimension.setEventsInhibited(false);
}
}
use of org.pepsoft.worldpainter.Dimension in project WorldPainter by Captain-Chaos.
the class MouseOrTabletOperation method penButtonEvent.
@Override
public void penButtonEvent(PButtonEvent pbe) {
PKind.Type penKindType = pbe.pen.getKind().getType();
final boolean stylus = penKindType == PKind.Type.STYLUS;
final boolean eraser = penKindType == PKind.Type.ERASER;
if ((!stylus) && (!eraser) && (penKindType != PKind.Type.CURSOR)) {
// We don't want events from keyboards, etc.
return;
}
final PButton.Type buttonType = pbe.button.getType();
switch(buttonType) {
case ALT:
altDown = pbe.button.value;
break;
case CONTROL:
ctrlDown = pbe.button.value;
break;
case SHIFT:
shiftDown = pbe.button.value;
break;
case LEFT:
case RIGHT:
if (pbe.button.value) {
// Button pressed
first = true;
undo = eraser || (buttonType == PButton.Type.RIGHT) || altDown;
if (!oneShot) {
if (timer == null) {
timer = new Timer(delay, e -> {
Point worldCoords = view.viewToWorld((int) x, (int) y);
tick(worldCoords.x, worldCoords.y, undo, first, (stylus || eraser) ? dynamicLevel : 1.0f);
view.updateStatusBar(worldCoords.x, worldCoords.y);
first = false;
});
timer.setInitialDelay(0);
timer.start();
// start = System.currentTimeMillis();
}
} else {
Point worldCoords = view.viewToWorld((int) x, (int) y);
SwingUtilities.invokeLater(() -> {
tick(worldCoords.x, worldCoords.y, undo, true, 1.0f);
view.updateStatusBar(worldCoords.x, worldCoords.y);
Dimension dimension = getDimension();
if (dimension != null) {
dimension.armSavePoint();
}
logOperation(undo ? statisticsKeyUndo : statisticsKey);
});
}
} else {
// Button released
if (!oneShot) {
SwingUtilities.invokeLater(() -> {
if (timer != null) {
logOperation(undo ? statisticsKeyUndo : statisticsKey);
timer.stop();
timer = null;
}
finished();
Dimension dimension = getDimension();
if (dimension != null) {
dimension.armSavePoint();
}
});
}
}
break;
}
}
Aggregations