Gridarta Editor
MapKeyListener.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.mapuserlistener;
21 
22 import java.awt.event.InputEvent;
23 import java.awt.event.KeyEvent;
24 import java.awt.event.KeyListener;
25 import java.awt.event.MouseEvent;
26 import java.awt.event.MouseListener;
36 import org.jetbrains.annotations.NotNull;
37 import org.jetbrains.annotations.Nullable;
38 
43 public class MapKeyListener<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> {
44 
48  @Nullable
50 
54  private boolean inside;
55 
59  private boolean lightVisible;
60 
64  @NotNull
65  private final KeyListener keyListener = new KeyListener() {
66 
67  @Override
68  public void keyTyped(@NotNull final KeyEvent e) {
69  // ignore
70  }
71 
72  @Override
73  public void keyPressed(@NotNull final KeyEvent e) {
74  setModifiers(e.getModifiers());
75  }
76 
77  @Override
78  public void keyReleased(@NotNull final KeyEvent e) {
79  setModifiers(e.getModifiers());
80  }
81 
82  };
83 
88  @NotNull
89  private final MouseListener mouseListener = new MouseListener() {
90 
91  @Override
92  public void mouseClicked(@NotNull final MouseEvent e) {
93  // ignore
94  }
95 
96  @Override
97  public void mousePressed(@NotNull final MouseEvent e) {
98  // ignore
99  }
100 
101  @Override
102  public void mouseReleased(@NotNull final MouseEvent e) {
103  // ignore
104  }
105 
106  @Override
107  public void mouseEntered(@NotNull final MouseEvent e) {
108  setInside(true);
109  setModifiers(e.getModifiers());
110  }
111 
112  @Override
113  public void mouseExited(@NotNull final MouseEvent e) {
114  setInside(false);
115  }
116 
117  };
118 
123  @NotNull
125 
126  @Override
127  public void mapViewCreated(@NotNull final MapView<G, A, R> mapView) {
128  // ignore
129  }
130 
131  @Override
132  public void mapViewClosing(@NotNull final MapView<G, A, R> mapView) {
133  // ignore
134  }
135 
136  @Override
137  public void activeMapViewChanged(@Nullable final MapView<G, A, R> mapView) {
138  setMapView(mapView);
139  }
140 
141  };
142 
148  @NotNull
150 
151  @Override
152  public void currentMapChanged(@Nullable final MapControl<G, A, R> mapControl) {
153  if (mapView != null) {
154  // Hack: mapViewManagerListener.activeMapViewChanged() has been called too early
155  setInside(mapView.getInternalFrame().getMousePosition() != null);
156  }
157  }
158 
159  @Override
160  public void mapCreated(@NotNull final MapControl<G, A, R> mapControl, final boolean interactive) {
161  // ignore
162  }
163 
164  @Override
165  public void mapClosing(@NotNull final MapControl<G, A, R> mapControl) {
166  // ignore
167  }
168 
169  @Override
170  public void mapClosed(@NotNull final MapControl<G, A, R> mapControl) {
171  // ignore
172  }
173 
174  };
175 
181  public MapKeyListener(@NotNull final MapViewManager<G, A, R> mapViewManager, @NotNull final MapManager<G, A, R> mapManager) {
182  mapViewManager.addMapViewManagerListener(mapViewManagerListener);
183  mapManager.addMapManagerListener(mapManagerListener);
184  setMapView(mapViewManager.getActiveMapView());
185  }
186 
191  private void setMapView(@Nullable final MapView<G, A, R> mapView) {
192  final MapView<G, A, R> prevMapView = this.mapView;
193  if (prevMapView != null) {
194  prevMapView.getInternalFrame().removeKeyListener(keyListener);
195  prevMapView.getRenderer().removeMouseListener(mouseListener);
196  prevMapView.getRenderer().setLightVisible(false);
197  }
198  this.mapView = mapView;
199  if (mapView != null) {
200  mapView.getInternalFrame().addKeyListener(keyListener);
201  mapView.getRenderer().addMouseListener(mouseListener);
202  setModifiers(0);
203  setInside(mapView.getInternalFrame().getMousePosition() != null);
204  } else {
205  setModifiers(0);
206  setInside(false);
207  }
208  }
209 
214  private void setModifiers(final int modifiers) {
215  final boolean lightVisible = (modifiers & (InputEvent.CTRL_MASK | InputEvent.SHIFT_MASK)) == (InputEvent.CTRL_MASK | InputEvent.SHIFT_MASK);
216  if (this.lightVisible != lightVisible) {
217  this.lightVisible = lightVisible;
218  updateRenderer();
219  }
220  }
221 
226  private void setInside(final boolean inside) {
227  if (this.inside != inside) {
228  this.inside = inside;
229  updateRenderer();
230  }
231  }
232 
237  private void updateRenderer() {
238  if (mapView != null) {
239  mapView.getRenderer().setLightVisible(inside && lightVisible);
240  }
241  }
242 
243 }
final KeyListener keyListener
The KeyListener attached to mapView.
void updateRenderer()
Updates the current map view&#39;s renderer settings.
void setInside(final boolean inside)
Updates the "mouse is inside the window" flag for the current map view.
A MapManager manages all opened maps.
Definition: MapManager.java:37
Graphical User Interface of Gridarta.
MapView< G, A, R > mapView
The MapView being tracked for key actions.
final MapViewManagerListener< G, A, R > mapViewManagerListener
The listener used to detect map view changes.
Base package of all Gridarta classes.
Reflects a game object (object on a map).
Definition: GameObject.java:36
Interface for listeners listening to MapManager changes.
MapKeyListener(@NotNull final MapViewManager< G, A, R > mapViewManager, @NotNull final MapManager< G, A, R > mapManager)
Creates a new instance.
GameObjects are the objects based on Archetypes found on maps.
MapRenderer getRenderer()
Returns the MapRenderer for this view.
Interface for listeners interested in events related to MapViewManager instances. ...
void setModifiers(final int modifiers)
Updates the active modifiers for the current map view.
Base classes for rendering maps.
Currently nothing more than a marker interface for unification.
Definition: MapControl.java:35
A map view consists of a map grid and a map cursor, and is attached to a map control.
Definition: MapView.java:43
final MouseListener mouseListener
The MouseListener attached to mapView.
final MapManagerListener< G, A, R > mapManagerListener
The MapManagerListener for tracking MapControl instances.
Tracks key presses and maps them to actions.
boolean lightVisible
Whether the CTRL+SHIFT modifiers are pressed.
void setMapView(@Nullable final MapView< G, A, R > mapView)
Sets the current map view.
boolean inside
Whether the mouse is inside the window.
JInternalFrame getInternalFrame()
Returns the JInternalFrame instance for this map view.