Gridarta Editor
AbstractMapView.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2023 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.awt.Dimension;
23 import java.awt.Point;
24 import java.awt.Rectangle;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.List;
28 import java.util.Set;
29 import javax.swing.JScrollPane;
41 import net.sf.gridarta.utils.Size2D;
42 import org.jetbrains.annotations.NotNull;
43 import org.jetbrains.annotations.Nullable;
44 
49 public abstract class AbstractMapView<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> implements MapView<G, A, R> {
50 
54  @NotNull
55  private final MapModel<G, A, R> mapModel;
56 
60  @NotNull
61  private final MapGrid mapGrid;
62 
66  @NotNull
68 
73  @NotNull
75 
76  @Override
77  public void mapSizeChanged(@NotNull final Size2D newSize) {
78  mapGrid.resize(newSize);
79  }
80 
81  @Override
82  public void mapSquaresChanged(@NotNull final Set<MapSquare<G, A, R>> mapSquares) {
83  // ignore
84  }
85 
86  @Override
87  public void mapObjectsChanged(@NotNull final Set<G> gameObjects, @NotNull final Set<G> transientGameObjects) {
88  // ignore
89  }
90 
91  @Override
92  public void errorsChanged(@NotNull final ErrorCollector<G, A, R> errors) {
93  // ignore
94  }
95 
96  @Override
97  public void mapFileChanged(@Nullable final MapFile oldMapFile) {
98  // ignore
99  }
100 
101  @Override
102  public void modifiedChanged() {
104  }
105 
106  };
107 
114  protected AbstractMapView(@NotNull final MapModel<G, A, R> mapModel, @NotNull final MapGrid mapGrid, @NotNull final MapCursor<G, A, R> mapCursor) {
115  this.mapModel = mapModel;
116  this.mapGrid = mapGrid;
117  this.mapCursor = mapCursor;
118 
120  }
121 
122  @Override
123  public void closeNotify() {
125  }
126 
127  @NotNull
128  @Override
129  public List<MapSquare<G, A, R>> getSelectedSquares() {
130  final Point[] selectedMapSquares = mapGrid.getSelection();
131  final List<MapSquare<G, A, R>> selection = new ArrayList<>();
132  for (final Point pos : selectedMapSquares) {
133  selection.add(mapModel.getMapSquare(pos));
134  }
135  return selection;
136  }
137 
138  @NotNull
139  @Override
140  public List<G> getSelectedGameObjects() {
141  final Collection<MapSquare<G, A, R>> selectedMapSquares = getSelectedSquares();
142  final List<G> objects = new ArrayList<>();
143  for (final Iterable<G> mapSquare : selectedMapSquares) {
144  for (final GameObject<G, A, R> gameObject : mapSquare) {
145  objects.add(gameObject.getHead());
146  }
147  }
148 
149  return objects.isEmpty() ? mapModel.getAllGameObjects() : objects;
150  }
151 
152  @Nullable
153  @Override
155  final List<G> objects = getSelectedGameObjects();
156  final int objectSize = objects.size();
157  return objectSize == 0 ? null : objects.get(objectSize == 1 ? 0 : RandomUtils.RND.nextInt(objects.size()));
158  }
159 
160  @NotNull
161  @Override
162  public MapGrid getMapGrid() {
163  return mapGrid;
164  }
165 
166  @NotNull
167  @Override
169  return mapCursor;
170  }
171 
172  @Override
173  public void setCursorLocation(@NotNull final Point point) {
174  final Size2D mapSize = getMapControl().getMapModel().getMapArchObject().getMapSize();
175  if (point.x >= mapSize.getWidth()) {
176  point.x = mapSize.getWidth() - 1;
177  } else if (point.x < 0) {
178  point.x = 0;
179  }
180  if (point.y >= mapSize.getHeight()) {
181  point.y = mapSize.getHeight() - 1;
182  } else if (point.y < 0) {
183  point.y = 0;
184  }
185  mapCursor.setLocation(point);
186  }
187 
188  @Override
189  public void centerMapView(@NotNull final Point centerSquare) {
190  final Rectangle squareBounds = getRenderer().getSquareBounds(centerSquare);
191  final JScrollPane scrollPane = getScrollPane();
192  final Dimension extentSize = scrollPane.getViewport().getExtentSize();
193  final Point centerPoint = new Point(Math.max(0, squareBounds.x + squareBounds.width / 2 - extentSize.width / 2), Math.max(0, squareBounds.y + squareBounds.height / 2 - extentSize.height / 2));
194  scrollPane.getViewport().setViewPosition(centerPoint);
195  }
196 
197 }
net.sf.gridarta.utils.RandomUtils
Random number utilities.
Definition: RandomUtils.java:28
net.sf.gridarta.utils.Size2D.getWidth
int getWidth()
Returns the width of the area.
Definition: Size2D.java:96
net.sf.gridarta.gui.map.mapview.AbstractMapView.mapGrid
final MapGrid mapGrid
The MapGrid of this map view.
Definition: AbstractMapView.java:61
net.sf.gridarta.model.mapmodel.MapModel
A MapModel reflects the data of a map.
Definition: MapModel.java:75
net.sf.gridarta.model.mapmodel.MapModel.getMapArchObject
A getMapArchObject()
Returns the Map Arch Object with the meta information about the map.
net.sf.gridarta.gui.map.mapview.AbstractMapView.mapModel
final MapModel< G, A, R > mapModel
The MapModel of this map view.
Definition: AbstractMapView.java:55
net.sf.gridarta.gui.map.mapview.MapView< G, A, R >::getMapControl
MapControl< G, A, R > getMapControl()
Return the controller of this view.
net.sf.gridarta
Base package of all Gridarta classes.
net.sf.gridarta.gui.map.mapview.AbstractMapView.getMapGrid
MapGrid getMapGrid()
Definition: AbstractMapView.java:162
net.sf.gridarta.model.mapmodel.MapSquare
A single Map Square.
Definition: MapSquare.java:45
net.sf
net.sf.gridarta.gui.map.mapview.AbstractMapView.closeNotify
void closeNotify()
Definition: AbstractMapView.java:123
net.sf.gridarta.model.mapmodel
Definition: AboveFloorInsertionMode.java:20
net.sf.gridarta.gui.map.mapview.AbstractMapView.getSelectedSquares
List< MapSquare< G, A, R > > getSelectedSquares()
Definition: AbstractMapView.java:129
net.sf.gridarta.model.archetype
Definition: AbstractArchetype.java:20
net.sf.gridarta.model.gameobject.GameObject
Reflects a game object (object on a map).
Definition: GameObject.java:36
net.sf.gridarta.gui.map.mapview.MapView< G, A, R >::getRenderer
MapRenderer getRenderer()
Returns the MapRenderer for this view.
net.sf.gridarta.gui.map.mapview.AbstractMapView.getMapCursor
MapCursor< G, A, R > getMapCursor()
Definition: AbstractMapView.java:168
net.sf.gridarta.gui.map.mapview.AbstractMapView.centerMapView
void centerMapView(@NotNull final Point centerSquare)
Definition: AbstractMapView.java:189
net.sf.gridarta.model.gameobject
GameObjects are the objects based on Archetypes found on maps.
Definition: AbstractGameObject.java:20
net
net.sf.gridarta.utils.Size2D.getHeight
int getHeight()
Returns the height of the area.
Definition: Size2D.java:104
net.sf.gridarta.gui.map.mapview.AbstractMapView.getSelectedGameObject
G getSelectedGameObject()
Definition: AbstractMapView.java:154
net.sf.gridarta.utils.RandomUtils.RND
static final Random RND
Global random number generator.
Definition: RandomUtils.java:33
errors
errors
Definition: ArchetypeTypeSetParserTest-ignoreDefaultAttribute1-result.txt:1
net.sf.gridarta.model.mapmodel.MapModel.removeMapModelListener
void removeMapModelListener(@NotNull MapModelListener< G, A, R > listener)
Unregister a map listener.
net.sf.gridarta.model.maparchobject.MapArchObject
Interface for MapArchObjects.
Definition: MapArchObject.java:40
net.sf.gridarta.model.validation.ErrorCollector
An interface for classes that collect errors.
Definition: ErrorCollector.java:33
net.sf.gridarta.model.mapcursor.MapCursor.getLocation
Point getLocation()
Get position of cursor.
Definition: MapCursor.java:227
net.sf.gridarta.gui.map.mapview.MapView
A map view consists of a map grid and a map cursor, and is attached to a map control.
Definition: MapView.java:43
net.sf.gridarta.model.validation
This package contains the framework for validating maps.
Definition: AbstractValidator.java:20
net.sf.gridarta.model.mapcursor.MapCursor
MapCursor provides methods to move and drag on map.
Definition: MapCursor.java:58
net.sf.gridarta.gui.map.mapview.MapView< G, A, R >::getScrollPane
JScrollPane getScrollPane()
Returns the JScrollPane of this map view.
net.sf.gridarta.model.mapcursor.MapCursor.setLocation
void setLocation(@NotNull final Point p)
Move cursor to a new location.
Definition: MapCursor.java:236
net.sf.gridarta.model.mapgrid.MapGrid.getSelection
Point[] getSelection()
Returns the selection.
Definition: MapGrid.java:842
net.sf.gridarta.model.mapmodel.MapFile
The location of a map file with a map directory.
Definition: MapFile.java:31
net.sf.gridarta.model.mapmodel.MapModelListener
Interface for listeners listening on MapModel events.
Definition: MapModelListener.java:36
net.sf.gridarta.model.mapgrid.MapGrid
2D-Grid containing flags for selection, pre-selection, cursor, warnings and errors.
Definition: MapGrid.java:46
net.sf.gridarta.model.mapmodel.MapModel.getMapSquare
MapSquare< G, A, R > getMapSquare(@NotNull Point pos)
Get the square at a specified location.
net.sf.gridarta.gui.map.renderer.MapRenderer.getSquareBounds
Rectangle getSquareBounds(@NotNull Point p)
Returns coordinates, length and width of map square.
net.sf.gridarta.model.mapgrid
Definition: MapGrid.java:20
net.sf.gridarta.gui.map.mapview.AbstractMapView.mapModelListener
final MapModelListener< G, A, R > mapModelListener
The MapModelListener used to detect changes in the map model that should be reflected in the window t...
Definition: AbstractMapView.java:74
net.sf.gridarta.model
net.sf.gridarta.model.archetype.Archetype
Reflects an Archetype.
Definition: Archetype.java:41
net.sf.gridarta.model.mapgrid.MapGrid.resize
void resize(@NotNull final Size2D newSize)
Resizes the MapGrid.
Definition: MapGrid.java:215
net.sf.gridarta.model.mapmodel.MapModel.prependMapModelListener
void prependMapModelListener(@NotNull MapModelListener< G, A, R > listener)
Register a map listener and call it before all other listeners.
net.sf.gridarta.gui.map.mapview.AbstractMapView
Abstract base class for MapView implementations.
Definition: AbstractMapView.java:49
net.sf.gridarta.gui.map.mapview.AbstractMapView.AbstractMapView
AbstractMapView(@NotNull final MapModel< G, A, R > mapModel, @NotNull final MapGrid mapGrid, @NotNull final MapCursor< G, A, R > mapCursor)
Creates a new instance.
Definition: AbstractMapView.java:114
net.sf.gridarta.model.mapcontrol.MapControl.getMapModel
MapModel< G, A, R > getMapModel()
Returns the map model.
net.sf.gridarta.model.maparchobject
Definition: AbstractMapArchObject.java:20
net.sf.gridarta.gui.map.mapview.AbstractMapView.getSelectedGameObjects
List< G > getSelectedGameObjects()
Definition: AbstractMapView.java:140
net.sf.gridarta.model.mapcursor
Definition: MapCursor.java:20
net.sf.gridarta.utils.Size2D
The class Size2D represents a 2d rectangular area.
Definition: Size2D.java:30
net.sf.gridarta.model.mapmodel.MapModel.getAllGameObjects
List< G > getAllGameObjects()
Returns all game objects.
net.sf.gridarta.utils
Definition: ActionBuilderUtils.java:20
net.sf.gridarta.gui.map.mapview.AbstractMapView.mapCursor
final MapCursor< G, A, R > mapCursor
The MapCursor of this map view.
Definition: AbstractMapView.java:67
net.sf.gridarta.gui.map.mapview.AbstractMapView.setCursorLocation
void setCursorLocation(@NotNull final Point point)
Definition: AbstractMapView.java:173