20 package net.sf.gridarta.gui.map.renderer;
22 import java.awt.AlphaComposite;
23 import java.awt.Color;
24 import java.awt.Composite;
25 import java.awt.Dimension;
26 import java.awt.Graphics;
27 import java.awt.Graphics2D;
28 import java.awt.Point;
29 import java.awt.Rectangle;
30 import java.awt.geom.AffineTransform;
31 import java.awt.image.BufferedImage;
32 import java.util.Arrays;
34 import javax.swing.Icon;
56 import org.jetbrains.annotations.NotNull;
57 import org.jetbrains.annotations.Nullable;
95 private final Point
origin =
new Point();
130 private final Rectangle
tmpRec =
new Rectangle();
180 public void gridVisibleChanged(
final boolean gridVisible) {
185 public void lightVisibleChanged(
final boolean lightVisible) {
190 public void smoothingChanged(
final boolean smoothing) {
195 public void tileStretchingChanged(
final boolean tileStretching) {
201 public void doubleFacesChanged(
final boolean doubleFaces) {
206 public void alphaTypeChanged(
final int alphaType) {
211 public void editTypeChanged(
final int editType) {
216 public void autojoinChanged(
final boolean autojoin) {
229 public void mapSizeChanged(@NotNull
final Size2D newSize) {
239 public void mapObjectsChanged(@NotNull
final Set<G> gameObjects, @NotNull
final Set<G> transientGameObjects) {
241 boolean resize =
false;
242 for (
final G gameObject : gameObjects) {
243 if (!gameObject.isStretched(
true)) {
256 if (mapSquare ==
null) {
264 if (direction.getDz() != 0) {
272 }
catch (
final IndexOutOfBoundsException ignored) {
275 for (
final G gameObjectAdjacent : mapSquareAdjacent) {
276 if (!gameObjectAdjacent.isStretched(
true)) {
284 gameObjectAdjacent.refreshStretchFactor();
288 gameObject.refreshStretchFactor();
305 public void mapFileChanged(@Nullable
final MapFile oldMapFile) {
310 public void modifiedChanged() {
323 public void mapGridChanged(@NotNull
final MapGridEvent e) {
328 public void mapGridResized(@NotNull
final MapGridEvent e) {
329 mapSize = e.getSource().getSize();
353 protected AbstractIsoMapRenderer(
final int spawnPointTypeNo, @NotNull
final MapViewSettings mapViewSettings, @NotNull
final MapModel<G, A, R> mapModel, @NotNull
final MapGrid mapGrid,
final int borderOffsetX,
final int borderOffsetY, @NotNull
final MultiPositionData multiPositionData, @NotNull
final IsoMapSquareInfo isoMapSquareInfo, @NotNull
final GridMapSquarePainter gridMapSquarePainter, @NotNull
final GameObjectParser<G, A, R> gameObjectParser, @NotNull
final Icon
unknownSquareIcon) {
362 mapSize = this.mapModel.getMapArchObject().getMapSize();
368 setToolTipText(
"dummy");
407 throw new IllegalStateException(
"operation not supported");
416 final int sum = mapWidth + mapHeight;
421 final BufferedImage image =
new BufferedImage(viewWidth, viewHeight, BufferedImage.TYPE_INT_ARGB);
422 final Graphics2D graphics = image.createGraphics();
424 graphics.fillRect(0, 0, viewWidth, viewHeight);
431 final Point pos =
new Point();
432 for (pos.y = 0; pos.y < mapHeight; pos.y++) {
435 for (pos.x = 0; pos.x < mapWidth; pos.x++) {
491 final Point pos =
new Point();
492 final Point endPos = rec.getLocation();
493 endPos.translate(rec.width, rec.height);
494 for (pos.y = rec.y; pos.y < endPos.y; pos.y++) {
497 for (pos.x = rec.x; pos.x < endPos.x; pos.x++) {
515 private Rectangle
getRepaintRec(@NotNull
final Rectangle visibleRectangle) {
517 final Rectangle rec =
new Rectangle();
519 final Point posUL = visibleRectangle.getLocation();
521 final Dimension visDim = visibleRectangle.getSize();
523 final Point posUR =
new Point(posUL.x + visDim.width, posUL.y);
524 final Point posDL =
new Point(posUL.x, posUL.y + visDim.height);
525 final Point posDR =
new Point(posUR.x, posDL.y);
550 protected void paintGameObjectIfVisible(@NotNull
final Graphics2D g,
final int xStart,
final int yStart, @NotNull
final G gameObject) {
551 final G head = gameObject.getHead();
565 private void paintGameObject(@NotNull
final Graphics2D g,
final int xStart,
final int yStart, @NotNull
final G gameObject,
final boolean inSpawnPoint) {
566 final G head = gameObject.getHead();
568 if (
"trans.101".equals(head.getFaceObjName())) {
574 icon = drawDouble ? head.getTransparentDoubleImage() : head.getTransparentImage();
577 final long stretchFactor = head.getStretchFactor();
578 if (stretchFactor != 0) {
579 icon = head.getStretchedImage(stretchFactor);
580 }
else if (drawDouble) {
581 icon = head.getDoubleImage();
583 icon = head.getNormalImage();
585 }
else if (drawDouble) {
586 icon = head.getDoubleImage();
588 icon = head.getNormalImage();
593 final int yOffset = head.getYOffset();
597 final int tmpIconWidth;
598 final int tmpIconHeight;
599 if (zoom == 0 || zoom == 100) {
600 tmpIconWidth = icon.getIconWidth();
601 tmpIconHeight = icon.getIconHeight();
603 tmpIconWidth = (icon.getIconWidth() * zoom + 50) / 100;
604 tmpIconHeight = (icon.getIconHeight() * zoom + 50) / 100;
607 final int iconHeight;
608 if (rotate < 0.001) {
609 iconWidth = tmpIconWidth;
610 iconHeight = tmpIconHeight;
612 iconWidth = (int) (Math.abs(Math.cos(rotate) * tmpIconWidth) + 0.5) + (int) (Math.abs(Math.sin(rotate) * tmpIconHeight) + 0.5);
613 iconHeight = (int) (Math.abs(Math.sin(rotate) * tmpIconWidth) + 0.5) + (int) (Math.abs(Math.cos(rotate) * tmpIconHeight) + 0.5);
615 if (head.getMultiRefCount() > 0) {
616 final R archetype = gameObject.getArchetype();
617 if (inSpawnPoint || archetype.isLowestPart()) {
618 final int headMultiShapeID = head.getArchetype().getMultiShapeID();
619 final int multiPartNr = archetype.getMultiPartNr();
622 paintScaledIcon(g, icon, x + xOffset, y - yOffset, zoom, alpha, rotate, tmpIconWidth, tmpIconHeight, iconWidth, iconHeight);
627 paintScaledIcon(g, icon, x + xOffset, y - yOffset, zoom, alpha, rotate, tmpIconWidth, tmpIconHeight, iconWidth, iconHeight);
632 final G mob = head.getFirst();
646 return (rotate < 0 ? rotate + 360 : rotate) * 2 * Math.PI / 360;
663 private void paintScaledIcon(@NotNull
final Graphics2D g, @NotNull
final Icon icon,
final int x,
final int y,
final int zoom,
final int alpha,
final double rotate,
final int oldIconWidth,
final int oldIconHeight,
final int newIconWidth,
final int newIconHeight) {
664 if (zoom <= 0 || zoom == 100) {
665 paintAlphaIcon(g, icon, x, y, alpha, rotate, oldIconWidth, oldIconHeight, newIconWidth, newIconHeight);
667 final AffineTransform savedTransform = g.getTransform();
670 g.scale(zoom / 100.0, zoom / 100.0);
671 paintAlphaIcon(g, icon, 0, 0, alpha, rotate, oldIconWidth, oldIconHeight, newIconWidth, newIconHeight);
673 g.setTransform(savedTransform);
691 private void paintAlphaIcon(@NotNull
final Graphics2D g, @NotNull
final Icon icon,
final int x,
final int y,
final int alpha,
final double rotate,
final int oldIconWidth,
final int oldIconHeight,
final int newIconWidth,
final int newIconHeight) {
692 if (alpha <= 0 || alpha >= 255) {
693 paintRotatedIcon(g, icon, x, y, rotate, oldIconWidth, oldIconHeight, newIconWidth, newIconHeight);
695 final Composite savedComposite = g.getComposite();
697 g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha / 255.0F));
698 paintRotatedIcon(g, icon, x, y, rotate, oldIconWidth, oldIconHeight, newIconWidth, newIconHeight);
700 g.setComposite(savedComposite);
717 private void paintRotatedIcon(@NotNull
final Graphics2D g, @NotNull
final Icon icon,
final int x,
final int y,
final double rotate,
final int oldIconWidth,
final int oldIconHeight,
final int newIconWidth,
final int newIconHeight) {
718 if (rotate < 0.001) {
726 final AffineTransform savedTransform = g.getTransform();
728 g.translate(x + newIconWidth / 2, y + newIconHeight / 2);
730 g.translate(-oldIconWidth / 2, -oldIconHeight / 2);
733 g.setTransform(savedTransform);
743 protected void paintIcon(@NotNull
final Graphics2D g, @NotNull
final Icon icon) {
744 icon.paintIcon(
this, g, 0, 0);
764 final Point point =
new Point();
773 for (
int subLayer = 0; subLayer < offsets.length; subLayer++) {
777 final int yStart2 = yStart + offsets[subLayer];
803 g.setColor(Color.black);
807 for (
int x = 0; x <= mapWidth; x++) {
810 for (
int y = 0; y <= mapHeight; y++) {
827 boolean foundAny =
false;
830 final G head = gameObject.getHead();
834 if (yOffset > offsets[subLayer]) {
835 offsets[subLayer] = yOffset;
847 for (
int subLayer = 0; subLayer < offsets.length; subLayer++) {
848 offsets[subLayer] =
maxYOffset - offsets[subLayer];
857 final int x0 = point.x -
origin.x;
858 final int y0 = point.y -
origin.y;
859 final int yt = (2 * y0 - x0) / 2;
860 final int xt = yt + x0;
867 retPoint.setLocation(xm, ym);
873 boolean retval =
false;
881 for (
int subLayer = 0; subLayer < offsets.length; subLayer++) {
885 final int yPos = yStart + offsets[subLayer];
889 final G head = gameObject.getHead();
891 stretch = head.getStretchFactor();
916 final Point pos =
new Point();
922 for (pos.y = 0; pos.y < mapHeight; pos.y++) {
923 for (pos.x = 0; pos.x < mapWidth; pos.x++) {
925 final G head = gameObject.getHead();
940 final int sum = mapWidth + mapHeight;
942 setPreferredSize(forcedSize);
943 setMinimumSize(forcedSize);