Gridarta Editor
AbstractMapRenderer.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.Point;
23 import java.awt.event.MouseEvent;
24 import java.io.File;
25 import java.io.IOException;
26 import java.util.Collections;
27 import java.util.HashMap;
28 import java.util.Map;
29 import javax.imageio.ImageIO;
30 import javax.swing.JComponent;
38 import org.jetbrains.annotations.NotNull;
39 import org.jetbrains.annotations.Nullable;
40 
45 public abstract class AbstractMapRenderer<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> extends JComponent implements MapRenderer {
46 
50  private static final long serialVersionUID = 1L;
51 
55  @NotNull
56  private Map<MapSquare<G, A, R>, ValidationError<G, A, R>> erroneousMapSquares = Collections.emptyMap();
57 
61  @NotNull
62  private final Point tmpPoint = new Point();
63 
67  @NotNull
68  private final MapModel<G, A, R> mapModel;
69 
74  @Nullable
76 
80  private boolean lightVisible;
81 
88  protected AbstractMapRenderer(@NotNull final MapModel<G, A, R> mapModel, @Nullable final GameObjectParser<G, A, R> gameObjectParser) {
89  this.mapModel = mapModel;
90  this.gameObjectParser = gameObjectParser;
91  }
92 
93  @Override
94  public void printFullImage(@NotNull final File file) throws IOException {
95  ImageIO.write(getFullImage(), "png", file);
96  }
97 
103  public void setErroneousMapSquares(@NotNull final Map<MapSquare<G, A, R>, ValidationError<G, A, R>> erroneousMapSquares) {
104  this.erroneousMapSquares = new HashMap<>(erroneousMapSquares);
105  }
106 
110  public abstract void closeNotify();
111 
115  @Nullable
116  @Override
117  public String getToolTipText(@NotNull final MouseEvent event) {
118  if (!getSquareLocationAt(event.getPoint(), tmpPoint)) {
119  return null;
120  }
121 
122  final MapSquare<G, A, R> mapSquare = mapModel.getMapSquare(tmpPoint);
123 
124  final ToolTipAppender<G, A, R> toolTipAppender = new ToolTipAppender<>(gameObjectParser);
125  for (final G gameObject : mapSquare.reverse()) {
126  toolTipAppender.appendGameObject(gameObject, false, "");
127  }
128 
129  final ValidationError<G, A, R> error = erroneousMapSquares.get(mapSquare);
130  if (error != null) {
131  toolTipAppender.appendValidationError(error);
132  }
133  return toolTipAppender.finish();
134  }
135 
136  @Override
137  public void setLightVisible(final boolean lightVisible) {
138  if (this.lightVisible == lightVisible) {
139  return;
140  }
141 
142  this.lightVisible = lightVisible;
143  forceRepaint();
144  }
145 
150  protected boolean isLightVisible() {
151  return lightVisible;
152  }
153 
154 }
void setErroneousMapSquares(@NotNull final Map< MapSquare< G, A, R >, ValidationError< G, A, R >> erroneousMapSquares)
Sets the MapSquares that are known to contain errors.
Iterable< G > reverse()
Return an object that is the reverse representation.
Map< MapSquare< G, A, R >, ValidationError< G, A, R > > erroneousMapSquares
The MapSquares that are known to contain errors.
A MapModel reflects the data of a map.
Definition: MapModel.java:75
void setLightVisible(final boolean lightVisible)
If set, inverts the setting of net.sf.gridarta.model.mapviewsettings.MapViewSettings#isLightVisible()...
Reading and writing of maps, handling of paths.
BufferedImage getFullImage()
Returns an image of the entire map view.
void printFullImage(@NotNull final File file)
Saves an image of the entire view to a file.
This package contains the framework for validating maps.
final MapModel< G, A, R > mapModel
The rendered MapModel.
boolean lightVisible
Whether the setting for lighted map squares is inverted.
Interface for classes that read or write GameObject instances.
Super class of all errors that could occur during map validation.
Base package of all Gridarta classes.
Abstract base class for classes implementing MapRenderer.
Reflects a game object (object on a map).
Definition: GameObject.java:36
AbstractMapRenderer(@NotNull final MapModel< G, A, R > mapModel, @Nullable final GameObjectParser< G, A, R > gameObjectParser)
Creates a new instance.
void appendValidationError(@NotNull final ValidationError< G, ?, ?> error)
final Point tmpPoint
Used to avoid creation millions of points.
abstract void closeNotify()
Must be called when this renderer is not used anymore.
MapSquare< G, A, R > getMapSquare(@NotNull Point pos)
Get the square at a specified location.
void appendGameObject(@NotNull final G gameObject, final boolean alwaysInclude, @NotNull final String prefix)
GameObjects are the objects based on Archetypes found on maps.
String getToolTipText(@NotNull final MouseEvent event)
RefusedBequest
final GameObjectParser< G, A, R > gameObjectParser
The GameObjectParser for creating tooltip information or.
boolean getSquareLocationAt(@NotNull Point point, @NotNull Point retPoint)
Returns the map location at the given point.
boolean isLightVisible()
Returns whether the setting for lighted map squares should be inverted.
Common interface for renderers of map control instances.
void forceRepaint()
Repaint the view because some view parameters may have changed.
static final long serialVersionUID
The serial version UID.