@Override
 public void mousePressed(MouseEvent me) {
   x = me.getX();
   y = me.getY();
   altDown = me.isAltDown() || me.isAltGraphDown();
   undo = (me.getButton() == MouseEvent.BUTTON3) || altDown;
   ctrlDown = me.isControlDown() || me.isMetaDown();
   shiftDown = me.isShiftDown();
   first = true;
   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, 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);
     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);
   }
 }
 @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.drawPoint(dimension, centreX, centreY, dynamicLevel);
   } finally {
     dimension.setEventsInhibited(false);
   }
 }
Beispiel #3
0
 private static World2 createNewWorld() {
   long seed = System.currentTimeMillis();
   TileFactory tileFactory =
       TileFactoryFactory.createNoiseTileFactory(
           seed, Terrain.GRASS, Constants.DEFAULT_MAX_HEIGHT_1, 16, 24, false, true, 20, 1.0);
   World2 world = new World2(seed, tileFactory, World2.DEFAULT_MAX_HEIGHT);
   Dimension dim0 = world.getDimension(0);
   for (int x = -2; x <= 2; x++) {
     for (int y = -2; y <= 2; y++) {
       dim0.addTile(tileFactory.createTile(x, y));
     }
   }
   return world;
 }
 @Override
 public void mouseReleased(MouseEvent me) {
   if (!oneShot) {
     if (timer != null) {
       logOperation(undo ? statisticsKeyUndo : statisticsKey);
       timer.stop();
       timer = null;
     }
     finished();
     Dimension dimension = getDimension();
     if (dimension != null) {
       dimension.armSavePoint();
     }
   }
 }
Beispiel #5
0
 private int getLowestSurroundingDryHeight(Dimension snapshot, int x, int y) {
   int lowestSurroundingDryHeight = Integer.MAX_VALUE;
   for (int dx = -1; dx <= 1; dx++) {
     for (int dy = -1; dy <= 1; dy++) {
       if ((dx != 0) || (dy != 0)) {
         int height = snapshot.getIntHeightAt(x + dx, y + dy);
         if ((height >= snapshot.getWaterLevelAt(x + dx, y + dy))
             && (height < lowestSurroundingDryHeight)) {
           if (height == 0) {
             return 0;
           } else {
             lowestSurroundingDryHeight = height;
           }
         }
       }
     }
   }
   return lowestSurroundingDryHeight;
 }
 @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;
   }
 }
  public ThreeDeeView(
      Dimension dimension,
      ColourScheme colourScheme,
      BiomeScheme biomeScheme,
      CustomBiomeManager customBiomeManager,
      int rotation,
      int zoom) {
    this.dimension = dimension;
    this.colourScheme = colourScheme;
    this.biomeScheme = biomeScheme;
    this.customBiomeManager = customBiomeManager;
    this.rotation = rotation;
    this.zoom = zoom;
    scale = (int) Math.pow(2.0, Math.abs(zoom - 1));
    //        System.out.println("Zoom " + zoom + " -> scale " + scale);
    maxHeight = dimension.getMaxHeight();
    if (dimension.getTileFactory() instanceof HeightMapTileFactory) {
      waterLevel = ((HeightMapTileFactory) dimension.getTileFactory()).getWaterHeight();
    } else {
      waterLevel = maxHeight / 2;
    }
    upsideDown = dimension.getDim() < 0; // Ceiling dimension
    switch (rotation) {
      case 0:
        zSortedTiles =
            new TreeSet<>(
                (t1, t2) -> {
                  if (t1.getY() != t2.getY()) {
                    return t1.getY() - t2.getY();
                  } else {
                    return t1.getX() - t2.getX();
                  }
                });
        break;
      case 1:
        zSortedTiles =
            new TreeSet<>(
                (t1, t2) -> {
                  if (t1.getX() != t2.getX()) {
                    return t1.getX() - t2.getX();
                  } else {
                    return t2.getY() - t1.getY();
                  }
                });
        break;
      case 2:
        zSortedTiles =
            new TreeSet<>(
                (t1, t2) -> {
                  if (t1.getY() != t2.getY()) {
                    return t2.getY() - t1.getY();
                  } else {
                    return t2.getX() - t1.getX();
                  }
                });
        break;
      case 3:
        zSortedTiles =
            new TreeSet<>(
                (t1, t2) -> {
                  if (t1.getX() != t2.getX()) {
                    return t2.getX() - t1.getX();
                  } else {
                    return t1.getY() - t2.getY();
                  }
                });
        break;
      default:
        throw new IllegalArgumentException();
    }
    zSortedTiles.addAll(dimension.getTiles());
    threeDeeRenderManager =
        new ThreeDeeRenderManager(
            dimension, colourScheme, biomeScheme, customBiomeManager, rotation);

    dimension.addDimensionListener(this);
    for (Tile tile : dimension.getTiles()) {
      tile.addListener(this);
    }

    int width = dimension.getWidth() * TILE_SIZE + dimension.getHeight() * TILE_SIZE;
    int height = width / 2 + maxHeight - 1;
    //        maxX = dimension.getHighestX();
    //        maxY = dimension.getHighestY();
    maxX = maxY = 0;
    //        xOffset = 512;
    //        yOffset = 256;
    //        xOffset = yOffset = 0;
    switch (rotation) {
      case 0:
        xOffset = -getTileBounds(dimension.getLowestX(), dimension.getHighestY()).x;
        yOffset = -getTileBounds(dimension.getLowestX(), dimension.getLowestY()).y;
        break;
      case 1:
        xOffset = -getTileBounds(dimension.getHighestX(), dimension.getHighestY()).x;
        yOffset = -getTileBounds(dimension.getLowestX(), dimension.getHighestY()).y;
        break;
      case 2:
        xOffset = -getTileBounds(dimension.getHighestX(), dimension.getLowestY()).x;
        yOffset = -getTileBounds(dimension.getHighestX(), dimension.getHighestY()).y;
        break;
      case 3:
        xOffset = -getTileBounds(dimension.getLowestX(), dimension.getLowestY()).x;
        yOffset = -getTileBounds(dimension.getHighestX(), dimension.getLowestY()).y;
        break;
      default:
        throw new IllegalArgumentException();
    }
    //        System.out.println("xOffset: " + xOffset + ", yOffset: " + yOffset);
    java.awt.Dimension preferredSize = zoom(new java.awt.Dimension(width, height));
    setPreferredSize(preferredSize);
    setMinimumSize(preferredSize);
    setMaximumSize(preferredSize);
    setSize(preferredSize);

    addHierarchyListener(this);
  }