Gridarta Editor
MapMouseListener.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.Point;
23 import java.awt.event.MouseEvent;
24 import java.awt.event.MouseListener;
25 import java.awt.event.MouseMotionListener;
34 import org.jetbrains.annotations.NotNull;
35 import org.jetbrains.annotations.Nullable;
36 
43 public class MapMouseListener<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> {
44 
49  @NotNull
51 
55  @NotNull
56  private final MapRenderer renderer;
57 
61  @NotNull
62  private final Point tmpPoint = new Point();
63 
68  @NotNull
70 
74  @NotNull
75  private final MouseListener mouseListener = new MouseListener() {
76 
77  @Override
78  public void mouseClicked(@NotNull final MouseEvent e) {
79  final MouseOpListener<G, A, R> mouseOpListener = getMouseOperation(e);
80  if (mouseOpListener != null) {
81  mouseOpListener.clicked(mouseOpEvent);
82  }
83  }
84 
85  @Override
86  public void mousePressed(@NotNull final MouseEvent e) {
87  final MouseOpListener<G, A, R> mouseOpListener = getMouseOperation(e);
88  if (mouseOpListener != null) {
89  mouseOpListener.pressed(mouseOpEvent);
90  }
91  }
92 
93  @Override
94  public void mouseReleased(@NotNull final MouseEvent e) {
95  final MouseOpListener<G, A, R> mouseOpListener = getMouseOperation(e);
96  if (mouseOpListener != null) {
97  mouseOpListener.released(mouseOpEvent);
98  }
99  }
100 
101  @Override
102  public void mouseEntered(@NotNull final MouseEvent e) {
103  // ignore
104  }
105 
106  @Override
107  public void mouseExited(@NotNull final MouseEvent e) {
108  // ignore
109  }
110 
111  };
112 
116  @NotNull
117  private final MouseMotionListener mouseMotionListener = new MouseMotionListener() {
118 
119  @Override
120  public void mouseDragged(@NotNull final MouseEvent e) {
121  final MouseOpListener<G, A, R> mouseOpListener = getMouseOperation(e);
122  if (mouseOpListener != null) {
123  mouseOpListener.dragged(mouseOpEvent);
124  }
125  }
126 
127  @Override
128  public void mouseMoved(@NotNull final MouseEvent e) {
129  final MouseOpListener<G, A, R> mouseOpListener = getMouseOperation(e);
130  if (mouseOpListener != null) {
131  mouseOpListener.moved(mouseOpEvent);
132  }
133  }
134 
135  };
136 
143  public MapMouseListener(@NotNull final MapRenderer renderer, @NotNull final ToolPalette<G, A, R> toolPalette, @NotNull final MapView<G, A, R> mapView) {
144  this.toolPalette = toolPalette;
145  this.renderer = renderer;
146  renderer.addMouseListener(mouseListener);
147  renderer.addMouseMotionListener(mouseMotionListener);
148  mouseOpEvent = new MouseOpEvent<>(mapView);
149  }
150 
154  public void closeNotify() {
155  renderer.removeMouseListener(mouseListener);
156  renderer.removeMouseMotionListener(mouseMotionListener);
157  }
158 
163  private void initEvent(@NotNull final MouseEvent event) {
164  mouseOpEvent.setButton(event.getButton());
165  mouseOpEvent.setId(event.getID());
166  mouseOpEvent.setMapLocation(getMapLocation(event));
167  mouseOpEvent.setModifiers(event.getModifiersEx());
168  mouseOpEvent.setClickCount(event.getClickCount());
169  event.consume();
170  }
171 
177  @Nullable
178  private MouseOpListener<G, A, R> getMouseOperation(@NotNull final MouseEvent event) {
179  initEvent(event);
180  return toolPalette.getTool(mouseOpEvent);
181  }
182 
188  @Nullable
189  private Point getMapLocation(@NotNull final MouseEvent event) {
190  return renderer.getSquareLocationAt(event.getPoint(), tmpPoint) ? tmpPoint : null;
191  }
192 
193 }
void setButton(final int button)
Sets the mouse button that changed.
Graphical User Interface of Gridarta.
void clicked(@NotNull MouseOpEvent< G, A, R > e)
Mouse was clicked.
void setClickCount(final int clickCount)
final ToolPalette< G, A, R > toolPalette
The ToolPalette for mapping mouse events to actions.
This package handles tools for manipulating maps.
Point getMapLocation(@NotNull final MouseEvent event)
Get the map location for a MouseEvent.
final MouseOpEvent< G, A, R > mouseOpEvent
The parameters for the currently processed event.
void closeNotify()
Must be called when this object is freed.
final MouseMotionListener mouseMotionListener
The MouseMotionListener attached to renderer.
void pressed(@NotNull MouseOpEvent< G, A, R > e)
Mouse was pressed.
Base package of all Gridarta classes.
void removeMouseMotionListener(@NotNull MouseMotionListener mouseMotionListener)
Removes a MouseMotionListener to be notified about mouse events.
final MapRenderer renderer
The MapRenderer being tracked for mouse actions.
Reflects a game object (object on a map).
Definition: GameObject.java:36
void moved(@NotNull MouseOpEvent< G, A, R > e)
Mouse was moved.
void removeMouseListener(@NotNull MouseListener l)
void dragged(@NotNull MouseOpEvent< G, A, R > e)
Mouse was dragged.
void setMapLocation(final Point mapLocation)
GameObjects are the objects based on Archetypes found on maps.
void addMouseListener(@NotNull MouseListener l)
Tracks mouse actions and calls the appropriate MouseOpListeners.
void released(@NotNull MouseOpEvent< G, A, R > e)
Mouse was released.
void setId(final int id)
Sets the event type.
Base classes for rendering maps.
MapMouseListener(@NotNull final MapRenderer renderer, @NotNull final ToolPalette< G, A, R > toolPalette, @NotNull final MapView< G, A, R > mapView)
Creates a new instance.
void initEvent(@NotNull final MouseEvent event)
Initializes mouseOpEvent from a MouseEvent.
boolean getSquareLocationAt(@NotNull Point point, @NotNull Point retPoint)
Returns the map location at the given point.
MouseOpListener< G, A, R > getMouseOperation(@NotNull final MouseEvent event)
Get the mouse operation for a MouseEvent.
Pane for controlling which mouse to select and configure tools for.
A MouseOpEvent is an event triggered for a MouseOpListener.
A map view consists of a map grid and a map cursor, and is attached to a map control.
Definition: MapView.java:43
Common interface for renderers of map control instances.
MouseOpListener< G, A, R > getTool(final MouseOpEvent< G, A, R > event)
Returns a MouseOpListener depending on the.
final MouseListener mouseListener
The MouseListener attached to renderer.