Gridarta Editor
ErroneousMapSquares.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.mapview;
21 
22 import java.util.HashMap;
23 import java.util.Map;
24 import java.util.Set;
37 import net.sf.gridarta.utils.Size2D;
38 import org.jetbrains.annotations.NotNull;
39 import org.jetbrains.annotations.Nullable;
40 
47 public class ErroneousMapSquares<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> {
48 
52  @NotNull
53  private final MapModel<G, A, R> mapModel;
54 
58  @NotNull
59  private final MapGrid mapGrid;
60 
64  @NotNull
66 
70  @NotNull
71  private final Map<MapSquare<G, A, R>, ValidationError<G, A, R>> erroneousMapSquares = new HashMap<>();
72 
76  @NotNull
78 
79  @Override
80  public void mapSizeChanged(@NotNull final Size2D newSize) {
81  mapGrid.resize(newSize);
82  }
83 
84  @Override
85  public void mapSquaresChanged(@NotNull final Set<MapSquare<G, A, R>> mapSquares) {
86  // ignore
87  }
88 
89  @Override
90  public void mapObjectsChanged(@NotNull final Set<G> gameObjects, @NotNull final Set<G> transientGameObjects) {
91  // ignore
92  }
93 
94  @Override
95  public void errorsChanged(@NotNull final ErrorCollector<G, A, R> errors) {
97  }
98 
99  @Override
100  public void mapFileChanged(@Nullable final MapFile oldMapFile) {
101  // ignore
102  }
103 
104  @Override
105  public void modifiedChanged() {
106  // ignore
107  }
108 
109  };
110 
117  public ErroneousMapSquares(@NotNull final MapModel<G, A, R> mapModel, @NotNull final MapGrid mapGrid, @NotNull final AbstractMapRenderer<G, A, R> renderer) {
118  this.mapModel = mapModel;
119  this.mapGrid = mapGrid;
120  this.renderer = renderer;
121  this.renderer.setErroneousMapSquares(erroneousMapSquares);
122  this.mapModel.addMapModelListener(mapModelListener);
123  }
124 
129  public void closeNotify() {
130  mapModel.removeMapModelListener(mapModelListener);
131  }
132 
137  public void errorsChanged(@NotNull final ErrorCollector<G, A, R> errors) {
138  erroneousMapSquares.clear();
139  mapGrid.beginTransaction();
140  try {
141  mapGrid.clearErrors();
142  for (final ValidationError<G, A, R> validationError : errors.getErrors()) {
143  for (final MapSquare<G, A, R> mapSquare : validationError.getMapSquares()) {
144  erroneousMapSquares.put(mapSquare, validationError);
145  mapGrid.setError(mapSquare.getMapX(), mapSquare.getMapY());
146  }
147  for (final G gameObject : validationError.getGameObjects()) {
148  final BaseObject<G, A, R, ?> topContainer = gameObject.getTopContainer();
149  mapGrid.setError(topContainer.getMapX(), topContainer.getMapY());
150  }
151  }
152  } finally {
153  mapGrid.endTransaction();
154  }
155  renderer.setErroneousMapSquares(erroneousMapSquares);
156  }
157 
158 }
Tracks a MapModel for changed erroneous map squares and updates a MapGrid and an AbstractMapRenderer ...
void setErroneousMapSquares(@NotNull final Map< MapSquare< G, A, R >, ValidationError< G, A, R >> erroneousMapSquares)
Sets the MapSquares that are known to contain errors.
void resize(@NotNull final Size2D newSize)
Resizes the MapGrid.
Definition: MapGrid.java:215
A MapModel reflects the data of a map.
Definition: MapModel.java:75
Graphical User Interface of Gridarta.
This package contains the framework for validating maps.
Interface for listeners listening on MapModel events.
void errorsChanged(@NotNull final ErrorCollector< G, A, R > errors)
Updates the erroneous map squares.
final Map< MapSquare< G, A, R >, ValidationError< G, A, R > > erroneousMapSquares
The erroneous MapSquares.
final MapModel< G, A, R > mapModel
The MapModel to track.
void setError(final int x, final int y)
Sets the error flag at given coordinates.
Definition: MapGrid.java:619
Super class of all errors that could occur during map validation.
int getMapX()
Returns the X coordinate of this GameObject on its map.
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
int getMapY()
Returns the Y coordinate of this GameObject on its map.
final MapModelListener< G, A, R > mapModelListener
The MapModelListener attached to mapModel.
GameObjects are the objects based on Archetypes found on maps.
2D-Grid containing flags for selection, pre-selection, cursor, warnings and errors.
Definition: MapGrid.java:45
Base classes for rendering maps.
void beginTransaction()
Starts a new transaction.
Definition: MapGrid.java:756
void removeMapModelListener(@NotNull MapModelListener< G, A, R > listener)
Unregister a map listener.
void clearErrors()
Clears all error flags.
Definition: MapGrid.java:631
final AbstractMapRenderer< G, A, R > renderer
The AbstractMapRenderer to notify.
void closeNotify()
Must be called when this instance is not used anymore.
void endTransaction()
Ends a transaction.
Definition: MapGrid.java:776
An interface for classes that collect errors.
ErroneousMapSquares(@NotNull final MapModel< G, A, R > mapModel, @NotNull final MapGrid mapGrid, @NotNull final AbstractMapRenderer< G, A, R > renderer)
Creates a new instance.
The location of a map file with a map directory.
Definition: MapFile.java:31
The class Size2D represents a 2d rectangular area.
Definition: Size2D.java:30