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-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.awt.Point;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.List;
34 import net.sf.gridarta.utils.Size2D;
35 import org.jetbrains.annotations.NotNull;
36 import org.jetbrains.annotations.Nullable;
37 
42 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> {
43 
47  @NotNull
48  private final MapModel<G, A, R> mapModel;
49 
53  @NotNull
54  private final MapGrid mapGrid;
55 
59  @NotNull
61 
68  protected AbstractMapView(@NotNull final MapModel<G, A, R> mapModel, @NotNull final MapGrid mapGrid, @NotNull final MapCursor<G, A, R> mapCursor) {
69  this.mapModel = mapModel;
70  this.mapGrid = mapGrid;
71  this.mapCursor = mapCursor;
72  }
73 
74  @NotNull
75  @Override
76  public List<MapSquare<G, A, R>> getSelectedSquares() {
77  final Point[] selectedMapSquares = mapGrid.getSelection();
78  final List<MapSquare<G, A, R>> selection = new ArrayList<>();
79  for (final Point pos : selectedMapSquares) {
80  selection.add(mapModel.getMapSquare(pos));
81  }
82  return selection;
83  }
84 
85  @NotNull
86  @Override
87  public List<G> getSelectedGameObjects() {
88  final Collection<MapSquare<G, A, R>> selectedMapSquares = getSelectedSquares();
89  final List<G> objects = new ArrayList<>();
90  for (final Iterable<G> mapSquare : selectedMapSquares) {
91  for (final GameObject<G, A, R> gameObject : mapSquare) {
92  objects.add(gameObject.getHead());
93  }
94  }
95 
96  if (objects.isEmpty()) {
97  return mapModel.getAllGameObjects();
98  }
99  return objects;
100  }
101 
102  @Nullable
103  @Override
105  final List<G> objects = getSelectedGameObjects();
106  final int objectSize = objects.size();
107  if (objectSize == 0) {
108  return null;
109  }
110  if (objectSize == 1) {
111  return objects.get(0);
112  }
113  return objects.get(RandomUtils.RND.nextInt(objects.size()));
114  }
115 
116  @NotNull
117  @Override
118  public MapGrid getMapGrid() {
119  return mapGrid;
120  }
121 
122  @NotNull
123  @Override
125  return mapCursor;
126  }
127 
128  @Override
129  public void setCursorLocation(@NotNull final Point point) {
130  final Size2D mapSize = getMapControl().getMapModel().getMapArchObject().getMapSize();
131  if (point.x >= mapSize.getWidth()) {
132  point.x = mapSize.getWidth() - 1;
133  } else if (point.x < 0) {
134  point.x = 0;
135  }
136  if (point.y >= mapSize.getHeight()) {
137  point.y = mapSize.getHeight() - 1;
138  } else if (point.y < 0) {
139  point.y = 0;
140  }
141  mapCursor.setLocation(point);
142  }
143 
144 }
A MapModel reflects the data of a map.
Definition: MapModel.java:75
Random number utilities.
MapCursor provides methods to move and drag on map.
Definition: MapCursor.java:57
MapControl< G, A, R > getMapControl()
Return the controller of this view.
Point [] getSelection()
Returns the selection.
Definition: MapGrid.java:842
final MapCursor< G, A, R > mapCursor
The MapCursor of this map view.
MapModel< G, A, R > getMapModel()
Returns the map model.
void setLocation(@NotNull final Point p)
Move cursor to a new location.
Definition: MapCursor.java:235
Base package of all Gridarta classes.
Reflects a game object (object on a map).
Definition: GameObject.java:36
AbstractMapView(@NotNull final MapModel< G, A, R > mapModel, @NotNull final MapGrid mapGrid, @NotNull final MapCursor< G, A, R > mapCursor)
Creates a new instance.
MapSquare< G, A, R > getMapSquare(@NotNull Point pos)
Get the square at a specified location.
GameObjects are the objects based on Archetypes found on maps.
int getWidth()
Returns the width of the area.
Definition: Size2D.java:96
final MapModel< G, A, R > mapModel
The MapModel of this map view.
static final Random RND
Global random number generator.
2D-Grid containing flags for selection, pre-selection, cursor, warnings and errors.
Definition: MapGrid.java:45
A getMapArchObject()
Returns the Map Arch Object with the meta information about the map.
final MapGrid mapGrid
The MapGrid of this map view.
Abstract base class for MapView implementations.
A map view consists of a map grid and a map cursor, and is attached to a map control.
Definition: MapView.java:43
void setCursorLocation(@NotNull final Point point)
List< MapSquare< G, A, R > > getSelectedSquares()
List< G > getAllGameObjects()
Returns all game objects.
int getHeight()
Returns the height of the area.
Definition: Size2D.java:104
The class Size2D represents a 2d rectangular area.
Definition: Size2D.java:30