Gridarta Editor
MapCursorTracker.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 javax.swing.JScrollPane;
24 import javax.swing.JViewport;
25 import javax.swing.ScrollPaneConstants;
34 import org.jetbrains.annotations.NotNull;
35 import org.jetbrains.annotations.Nullable;
36 
43 public class MapCursorTracker<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> {
44 
48  @NotNull
50 
54  @NotNull
56 
60  @NotNull
61  private final JScrollPane scrollPane;
62 
66  @NotNull
68 
69  @Override
70  public void mapCursorChangedPos(@NotNull final Point location) {
72  }
73 
74  @Override
75  public void mapCursorChangedMode() {
76  // Ignore mode change events
77  }
78 
79  @Override
80  public void mapCursorChangedGameObject(@Nullable final MapSquare<G, A, R> mapSquare, @Nullable final G gameObject) {
81  // ignore
82  }
83 
84  @Override
85  public void mapCursorChangedSize() {
86  // ignore
87  }
88 
89  };
90 
101  public MapCursorTracker(final boolean isPickmap, @Nullable final Point viewPosition, final int xScrollDistance, final int yScrollDistance, @NotNull final MapCursor<G, A, R> mapCursor, @NotNull final AbstractMapRenderer<G, A, R> renderer) {
102  this.mapCursor = mapCursor;
103  this.renderer = renderer;
104  this.mapCursor.addMapCursorListener(mapCursorListener);
105  scrollPane = new JScrollPane(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
106  if (isPickmap) {
107  scrollPane.setBackground(CommonConstants.BG_COLOR);
108  }
109 
110  // set the pixel increment scrolling for clicking once on a scroll bar arrow
111  scrollPane.getVerticalScrollBar().setUnitIncrement(yScrollDistance);
112  scrollPane.getHorizontalScrollBar().setUnitIncrement(xScrollDistance);
113  scrollPane.getViewport().setScrollMode(JViewport.SIMPLE_SCROLL_MODE);
114  scrollPane.setViewportView(renderer);
115  if (viewPosition != null) {
116  scrollPane.getViewport().setViewPosition(viewPosition);
117  }
118  scrollPane.setFocusable(true);
120  }
121 
126  public void closeNotify() {
127  scrollPane.setViewportView(null);
128  mapCursor.removeMapCursorListener(mapCursorListener);
129  }
130 
134  private void ensureVisibleMapCursor() {
135  final Point cursorLocation = mapCursor.getLocation();
136  renderer.scrollRectToVisible(renderer.getSquareBounds(cursorLocation));
137  }
138 
143  @NotNull
144  public JScrollPane getScrollPane() {
145  return scrollPane;
146  }
147 
148 }
final MapCursorListener< G, A, R > mapCursorListener
The MapCursorListener attached to mapCursor.
Rectangle getSquareBounds(@NotNull Point p)
Returns coordinates, length and width of map square.
final AbstractMapRenderer< G, A, R > renderer
The AbstractMapRenderer to update.
Graphical User Interface of Gridarta.
final JScrollPane scrollPane
The JScrollPane for this instance.
MapCursor provides methods to move and drag on map.
Definition: MapCursor.java:57
Class with constants used in Gridarta and derivates.
static final Color BG_COLOR
Background Color (for the Panels).
Point getLocation()
Get position of cursor.
Definition: MapCursor.java:226
void closeNotify()
Must be called when this instance is not used anymore.
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
void ensureVisibleMapCursor()
Makes sure the mapCursor is visible.
GameObjects are the objects based on Archetypes found on maps.
MapCursorTracker(final boolean isPickmap, @Nullable final Point viewPosition, final int xScrollDistance, final int yScrollDistance, @NotNull final MapCursor< G, A, R > mapCursor, @NotNull final AbstractMapRenderer< G, A, R > renderer)
Creates a new instance.
JScrollPane getScrollPane()
Returns the JScrollPane of the renderer.
Base classes for rendering maps.
final MapCursor< G, A, R > mapCursor
The MapCursor to track.
Tracks the MapCursor of map and scrolls the AbstractMapRenderer so that the map cursor remains visibl...
Interface for listeners listening to MapCursor related events.
void removeMapCursorListener(@NotNull final MapCursorListener< G, A, R > listener)
Remove a MapCursorListener.
Definition: MapCursor.java:427
void scrollRectToVisible(@NotNull Rectangle aRect)
Ensures that a rectangular area is visible.