Gridarta Editor
SimpleFlatMapRenderer.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.var.crossfire.gui.map.renderer;
21 
22 import java.awt.Color;
23 import java.awt.Graphics;
24 import java.awt.Point;
25 import java.awt.Rectangle;
26 import java.awt.image.BufferedImage;
27 import javax.swing.ImageIcon;
32 import net.sf.gridarta.utils.Size2D;
37 import org.jetbrains.annotations.NotNull;
38 
43 public class SimpleFlatMapRenderer extends AbstractMapRenderer<GameObject, MapArchObject, Archetype> {
44 
48  private static final long serialVersionUID = 1L;
49 
54  @NotNull
56 
60  @NotNull
62 
67  @NotNull
68  private final Point offset = new Point();
69 
73  @NotNull
75 
82  public SimpleFlatMapRenderer(@NotNull final MapModel<GameObject, MapArchObject, Archetype> mapModel, @NotNull final ResourceIcons resourceIcons, @NotNull final SmoothingRenderer smoothingRenderer) {
83  super(mapModel, null);
84  this.mapModel = mapModel;
85  this.resourceIcons = resourceIcons;
86  this.smoothingRenderer = smoothingRenderer;
87  }
88 
89  @Override
90  public boolean getSquareLocationAt(@NotNull final Point point, @NotNull final Point retPoint) {
91  throw new IllegalStateException();
92  }
93 
94  @NotNull
95  @Override
96  public BufferedImage getFullImage() {
97  final Size2D mapSize = mapModel.getMapArchObject().getMapSize();
98  final int viewWidth = mapSize.getWidth() * IGUIConstants.SQUARE_WIDTH;
99  final int viewHeight = mapSize.getHeight() * IGUIConstants.SQUARE_HEIGHT;
100  final BufferedImage image = new BufferedImage(viewWidth, viewHeight, BufferedImage.TYPE_INT_ARGB);
101  final Graphics g = image.getGraphics();
102  try {
103  paintComponent(g);
104  } finally {
105  g.dispose();
106  }
107  return image;
108  }
109 
110  @Override
111  public void forceRepaint() {
112  throw new IllegalStateException();
113  }
114 
115  @Override
116  protected void paintComponent(@NotNull final Graphics g) {
117  final Size2D mapSize = mapModel.getMapArchObject().getMapSize();
118  g.setColor(Color.white);
119  g.fillRect(0, 0, mapSize.getWidth() * IGUIConstants.SQUARE_WIDTH, mapSize.getHeight() * IGUIConstants.SQUARE_HEIGHT);
120  final Point point = new Point();
121  for (point.y = 0; point.y < mapSize.getHeight(); point.y++) {
122  for (point.x = 0; point.x < mapSize.getWidth(); point.x++) {
123  paintSquare(g, point);
124  }
125  }
126  }
127 
133  private void paintSquare(@NotNull final Graphics graphics, @NotNull final Point point) {
134  if (mapModel.getMapSquare(point).isEmpty()) {
135  resourceIcons.getResourceIcon(ResourceIcons.SQUARE_EMPTY).paintIcon(this, graphics, point.x * IGUIConstants.SQUARE_WIDTH, point.y * IGUIConstants.SQUARE_HEIGHT);
136  return;
137  }
138 
139  int layer = -1;
140  for (final BaseObject<?, ?, ?, ?> node : mapModel.getMapSquare(point)) {
141  if (node.getAttributeInt(GameObject.INVISIBLE, true) == 0) {
142  layer++;
143  }
144  final ImageIcon img = node.getNormalImage();
145  if (!node.isMulti() || (img.getIconWidth() == IGUIConstants.SQUARE_WIDTH && img.getIconHeight() == IGUIConstants.SQUARE_HEIGHT)) {
146  offset.x = 0;
147  offset.y = 0;
148  } else {
149  // this is an oversized image, so it must be shifted
150  offset.x = IGUIConstants.SQUARE_WIDTH * (node.getArchetype().getMultiX() - node.getMinX());
151  offset.y = IGUIConstants.SQUARE_HEIGHT * (node.getArchetype().getMultiY() - node.getMinY());
152  }
153  graphics.drawImage(img.getImage(), point.x * IGUIConstants.SQUARE_WIDTH, point.y * IGUIConstants.SQUARE_HEIGHT, point.x * IGUIConstants.SQUARE_WIDTH + IGUIConstants.SQUARE_WIDTH, point.y * IGUIConstants.SQUARE_HEIGHT + IGUIConstants.SQUARE_HEIGHT, offset.x, offset.y, offset.x + IGUIConstants.SQUARE_WIDTH, offset.y + IGUIConstants.SQUARE_HEIGHT, this);
154  if (node.getAttributeInt(GameObject.SMOOTHLEVEL, true) > 0) {
155  smoothingRenderer.paintSmooth(graphics, point, node.getAttributeInt(GameObject.SMOOTHLEVEL, true), layer, false, 0, 0);
156  }
157  }
158  if (layer > -1) {
159  smoothingRenderer.paintSmooth(graphics, point, 1, layer + 1, true, 0, 0);
160  }
161  }
162 
163  @Override
164  public void closeNotify() {
165  }
166 
167  @NotNull
168  @Override
169  public Rectangle getSquareBounds(@NotNull final Point p) {
170  throw new IllegalStateException();
171  }
172 
173 }
A MapModel reflects the data of a map.
Definition: MapModel.java:75
Graphical User Interface of Gridarta.
final MapModel< GameObject, MapArchObject, Archetype > mapModel
The map model to render.
static final String SMOOTHLEVEL
The name of the "smoothlevel" attribute.
Definition: GameObject.java:70
int SQUARE_HEIGHT
The height of a square in pixels.
MapArchObject contains the specific meta data about a map that is stored in the map-arch, at the very beginning of the map file.
Handles the Crossfire variants of GameObjects and Archetypes.
Renderer for smoothed faces as used by Crossfire.
void forceRepaint()
Repaint the view because some view parameters may have changed.
Base package of all Gridarta classes.
Abstract base class for classes implementing MapRenderer.
SimpleFlatMapRenderer(@NotNull final MapModel< GameObject, MapArchObject, Archetype > mapModel, @NotNull final ResourceIcons resourceIcons, @NotNull final SmoothingRenderer smoothingRenderer)
Creates a new instance.
MapSquare< G, A, R > getMapSquare(@NotNull Point pos)
Get the square at a specified location.
int getWidth()
Returns the width of the area.
Definition: Size2D.java:96
Defines common UI constants used in different dialogs and all used icon files.
Rectangle getSquareBounds(@NotNull final Point p)
Returns coordinates, length and width of map square.
Base classes for rendering maps.
Main package of Gridarta4Crossfire, contains all classes specific to the Crossfire version of the Gri...
static final String INVISIBLE
The name of the "invisible" attribute.
Definition: GameObject.java:64
BufferedImage getFullImage()
Returns an image of the entire map view.
void paintSquare(@NotNull final Graphics graphics, @NotNull final Point point)
Paint one square.
A getMapArchObject()
Returns the Map Arch Object with the meta information about the map.
Creates ImageIcon instances from resources.
ImageIcon getResourceIcon(@NotNull final String iconName)
Returns the image icon for the given icon name.
int getHeight()
Returns the height of the area.
Definition: Size2D.java:104
boolean getSquareLocationAt(@NotNull final Point point, @NotNull final Point retPoint)
Returns the map location at the given point.
int SQUARE_WIDTH
The width of a square in pixels.
void paintSmooth(@NotNull final Graphics graphics, @NotNull final Point pos, final int level, final int firstLayer, final boolean allLayers, final int borderOffsetX, final int borderOffsetY)
Draw the smoothing information at given position of map, for a given limit smoothlevel, at a given layer.
The class Size2D represents a 2d rectangular area.
Definition: Size2D.java:30
final ResourceIcons resourceIcons
The ResourceIcons for creating icons.
final SmoothingRenderer smoothingRenderer
The SmoothingRenderer for rendering smoothed faces.