Gridarta Editor
IsoMapRenderer.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2015 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.gui.map.renderer;
21 
22 import java.awt.Color;
23 import java.awt.Graphics;
24 import java.awt.Graphics2D;
25 import java.awt.Point;
26 import javax.swing.Icon;
43 import org.jetbrains.annotations.NotNull;
44 
49 public class IsoMapRenderer<G extends DefaultIsoGameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> extends AbstractIsoMapRenderer<G, A, R> {
50 
54  private static final long serialVersionUID = 1L;
55 
56  @NotNull
58 
59  @NotNull
60  private final Color[] highLightMask = { new Color(1.0f, 0.0f, 0.0f, 0.33f), new Color(0.0f, 1.0f, 0.0f, 0.33f), new Color(0.0f, 1.0f, 1.0f, 0.33f), };
61 
62  @NotNull
63  private final Icon emptySquareIcon;
64 
68  @NotNull
70 
74  @NotNull
76 
80  @NotNull
81  private final FilterState filterState = new FilterState();
82 
86  @NotNull
87  private final int[] xPoints = new int[4];
88 
92  @NotNull
93  private final int[] yPoints = new int[4];
94 
98  @NotNull
99  private final Point point = new Point();
100 
105  @NotNull
107 
108  @Override
109  public void configChanged(@NotNull final FilterConfigChangeType filterConfigChangeType, @NotNull final FilterConfig<?, ?> filterConfig) {
110  forceRepaint();
111  }
112 
113  };
114 
130  public IsoMapRenderer(final int spawnPointTypeNo, @NotNull final MapViewSettings mapViewSettings, @NotNull final FilterControl<G, A, R> filterControl, @NotNull final MapModel<G, A, R> mapModel, @NotNull final MapGrid mapGrid, @NotNull final MultiPositionData multiPositionData, @NotNull final IsoMapSquareInfo isoMapSquareInfo, @NotNull final GridMapSquarePainter gridMapSquarePainter, @NotNull final GameObjectParser<G, A, R> gameObjectParser, @NotNull final ResourceIcons resourceIcons) {
131  super(spawnPointTypeNo, mapViewSettings, mapModel, mapGrid, isoMapSquareInfo.getXLen(), 2 * isoMapSquareInfo.getYLen(), multiPositionData, isoMapSquareInfo, gridMapSquarePainter, gameObjectParser, resourceIcons.getResourceIcon(ResourceIcons.SQUARE_UNKNOWN));
132  this.filterControl = filterControl;
133  emptySquareIcon = resourceIcons.getResourceIcon(ResourceIcons.SQUARE_EMPTY);
134  this.mapViewSettings = mapViewSettings;
135  this.isoMapSquareInfo = isoMapSquareInfo;
136  this.filterControl.addConfigListener(filterConfigListener);
137  }
138 
139  @Override
140  public void closeNotify() {
141  super.closeNotify();
142  filterControl.removeConfigListener(filterConfigListener);
143  }
144 
145  @Override
146  protected void clearBackground(@NotNull final Graphics g) {
147  }
148 
149  @Override
150  protected boolean isGameObjectVisible(@NotNull final G gameObject) {
151  return mapViewSettings.isEditType(gameObject);
152  }
153 
154  @Override
155  protected void paintSquare(@NotNull final Graphics2D g, final int x, final int y, @NotNull final MapSquare<G, A, R> square) {
156  point.setLocation(square.getMapX(), square.getMapY());
157  final int[] yOffsets = tileStretchingOffsets(point, foundSubLayers);
158  int highestSubLayer = 0;
159  for (int subLayer = 0; subLayer < yOffsets.length; subLayer++) {
160  if (yOffsets[subLayer] < yOffsets[highestSubLayer]) {
161  highestSubLayer = subLayer;
162  }
163  }
164  filterControl.newSquare(filterState);
165  if (square.isEmpty()) {
166  emptySquareIcon.paintIcon(this, g, x, y + yOffsets[0]);
167  } else {
168  for (final G node : square) {
169  filterControl.objectInSquare(filterState, node);
170  if (filterControl.canShow(node)) {
171  final G head = node.getHead();
172  final int subLayer;
173  if (head.getAttributeInt(DefaultIsoGameObject.LAYER) == 0) {
174  subLayer = highestSubLayer;
175  } else {
176  subLayer = head.getAttributeInt(DefaultIsoGameObject.SUB_LAYER);
177  }
178  paintGameObjectIfVisible(g, x, y + yOffsets[subLayer], node);
179  }
180  }
181  }
182  for (int subLayer = 0; subLayer < yOffsets.length; subLayer++) {
183  if (!foundSubLayers[subLayer]) {
184  continue;
185  }
186 
187  for (int i = 0; i < FilterControl.MAX_HIGHLIGHT; i++) {
188  if (filterControl.isHighlightedSquare(filterState, i)) {
189  final Color color = g.getColor();
190  g.setColor(highLightMask[i]);
191  xPoints[0] = x + isoMapSquareInfo.getXLen() / 2;
192  xPoints[1] = x + isoMapSquareInfo.getXLen() - 1;
193  xPoints[2] = xPoints[0];
194  xPoints[3] = x;
195  yPoints[0] = y + yOffsets[subLayer];
196  yPoints[1] = y + yOffsets[subLayer] + isoMapSquareInfo.getYLen() / 2;
197  yPoints[2] = y + yOffsets[subLayer] + isoMapSquareInfo.getYLen() - 1;
198  yPoints[3] = yPoints[1];
199  g.fillPolygon(xPoints, yPoints, 4);
200  g.setColor(color);
201  }
202  }
203  }
204  }
205 
206 }
IsoMapRenderer(final int spawnPointTypeNo, @NotNull final MapViewSettings mapViewSettings, @NotNull final FilterControl< G, A, R > filterControl, @NotNull final MapModel< G, A, R > mapModel, @NotNull final MapGrid mapGrid, @NotNull final MultiPositionData multiPositionData, @NotNull final IsoMapSquareInfo isoMapSquareInfo, @NotNull final GridMapSquarePainter gridMapSquarePainter, @NotNull final GameObjectParser< G, A, R > gameObjectParser, @NotNull final ResourceIcons resourceIcons)
Creates a new instance.
int [] tileStretchingOffsets( @NotNull final Point point, final boolean[] foundSubLayers)
Calculates the tile stretching Y offset.
final MultiPositionData multiPositionData
The MultiPositionData instance to query for multi-part objects.
final FilterState filterState
The filter state instance for this map renderer.
A MapRenderer that renders isometric squares.
final IsoMapSquareInfo isoMapSquareInfo
The IsoMapSquareInfo to use.
final int [] xPoints
The x-coordinates for painting highlighted squares.
A MapModel reflects the data of a map.
Definition: MapModel.java:75
Graphical User Interface of Gridarta.
Reading and writing of maps, handling of paths.
void newSquare(@NotNull FilterState filterState)
static final String LAYER
The name of the "layer" attribute.
final MapViewSettings mapViewSettings
The map view settings instance.
Interface for classes that read or write GameObject instances.
A AbstractIsoMapRenderer to render map files.
boolean isEditType(int editType)
Get information on the current state of edit type.
int getYLen()
Returns the vertical size of a square.
final int spawnPointTypeNo
The game object type number of spawn points.
Base package of all Gridarta classes.
boolean isGameObjectVisible(@NotNull final G gameObject)
static final String SUB_LAYER
The name of the "sub_layer" attribute.
static final long serialVersionUID
Serial Version UID.
void clearBackground(@NotNull final Graphics g)
Container for settings that affect the rendering of maps.
GameObjects are the objects based on Archetypes found on maps.
boolean canShow(@NotNull G gameObject)
void paintSquare(@NotNull final Graphics2D g, final int x, final int y, @NotNull final MapSquare< G, A, R > square)
boolean isHighlightedSquare(@NotNull FilterState filterState, int path)
final FilterConfigListener filterConfigListener
The FilterConfigListener attached to filterControl to repaint all after config changes.
void paintGameObjectIfVisible( @NotNull final Graphics2D g, final int xStart, final int yStart, @NotNull final G gameObject)
Paints a single game object if it is visible according to current editor settings.
The MultiPositionData class stores an array of numbers which is required in order to calculate displa...
2D-Grid containing flags for selection, pre-selection, cursor, warnings and errors.
Definition: MapGrid.java:45
The filter package contains the classes for Filters.
Definition: BtnPopup.java:20
final Point point
Used for calculations in paintSquare.
final FilterControl< G, A, R > filterControl
Provides information about isometric map squares.
void objectInSquare(@NotNull FilterState filterState, @NotNull G gameObject)
final boolean [] foundSubLayers
Boolean array for tileStretchingOffsets to avoid allocating lots of arrays.
Creates ImageIcon instances from resources.
void removeConfigListener(@NotNull FilterConfigListener listener)
Removes a FilterConfigListener to be notified about changes.
final int [] yPoints
The y-coordinates for painting highlighted squares.
Default implementation for GameObject implementing classes.
Interface for filter configurations.
final GridMapSquarePainter gridMapSquarePainter
The GridMapSquarePainter to use.
int getXLen()
Returns the horizontal size of a square.
Interface for listeners interested in FilterConfig related changes.
The highlighted state while using a FilterControl instance.