Gridarta Editor
InsertionTool.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.panel.tools;
21 
22 import java.awt.Component;
23 import java.awt.Container;
24 import java.awt.GridBagConstraints;
25 import java.awt.GridBagLayout;
26 import java.awt.Point;
27 import javax.swing.JComboBox;
28 import javax.swing.JPanel;
45 import net.sf.japi.swing.action.ActionBuilder;
46 import net.sf.japi.swing.action.ActionBuilderFactory;
47 import org.jetbrains.annotations.NotNull;
48 import org.jetbrains.annotations.Nullable;
49 
54 public class InsertionTool<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> extends BasicAbstractTool<G, A, R> {
55 
59  private static final int MODE_AUTO = 0;
60 
64  private static final int MODE_TOPMOST = 1;
65 
69  private static final int MODE_ABOVE_FLOOR = 2;
70 
74  private static final int MODE_BELOW_FLOOR = 3;
75 
79  private static final int MODE_BOTTOMMOST = 4;
80 
84  @NotNull
85  private static final ActionBuilder ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder("net.sf.gridarta.gui.panel.tools");
86 
90  @NotNull
91  private final JComboBox<?> modeComboBox = createModeComboBox();
92 
96  @NotNull
98 
102  @NotNull
104 
108  @NotNull
110 
114  @NotNull
116 
120  @NotNull
122 
131  public InsertionTool(@NotNull final SelectedSquareModel<G, A, R> selectedSquareModel, @NotNull final ObjectChooser<G, A, R> objectChooser, @NotNull final PickmapSettings pickmapSettings, @NotNull final InsertionModeSet<G, A, R> insertionModeSet, @NotNull final MapViewSettings mapViewSettings) {
132  super("insertion");
133  this.selectedSquareModel = selectedSquareModel;
134  this.objectChooser = objectChooser;
135  this.pickmapSettings = pickmapSettings;
136  this.insertionModeSet = insertionModeSet;
137  this.mapViewSettings = mapViewSettings;
138  }
139 
140  @Override
141  public void pressed(@NotNull final MouseOpEvent<G, A, R> e) {
142  doInsert(e, true);
143  }
144 
145  @Override
146  public void released(@NotNull final MouseOpEvent<G, A, R> e) {
147 
148  }
149 
150  @Override
151  public void clicked(@NotNull final MouseOpEvent<G, A, R> e) {
152  }
153 
154  @Override
155  public void dragged(@NotNull final MouseOpEvent<G, A, R> e) {
156  doInsert(e, false);
157  }
158 
159  @Override
160  public void moved(@NotNull final MouseOpEvent<G, A, R> e) {
161  }
162 
169  private void doInsert(@NotNull final MouseOpEvent<G, A, R> e, final boolean isPressed) {
170  final MapControl<G, A, R> mapControl = e.getMapControl();
171  if (mapControl.isPickmap() && pickmapSettings.isLocked()) {
172  return;
173  }
174 
175  final Point p = e.getMapLocation();
176  final MapCursor<G, A, R> mapCursor = e.getMapCursor();
177 
178  if (isPressed) {
179  if (p == null) {
180  return;
181  }
182  if (!mapControl.isPickmap()) {
183  mapCursor.setLocation(p);
184  }
185  } else {
186  if (mapControl.isPickmap()) {
187  return;
188  }
189  if (!mapCursor.setLocationSafe(p)) {
190  return;
191  }
192  }
193 
194  final MapModel<G, A, R> mapModel = mapControl.getMapModel();
195  final MapArchObject<A> mapArchObject = mapModel.getMapArchObject();
196  mapModel.beginTransaction("Insert Object");
197  try {
198  @Nullable final G insertedObject;
199  if (mapControl.isPickmap()) {
200  final GameObject<G, A, R> selectedGameObject = selectedSquareModel.getSelectedGameObject();
201  if (selectedGameObject != null) {
202  final G head = selectedGameObject.getHead();
203 
204  // check if all spaces are free that the multi will occupy
205  final Point point = new Point();
206  for (R part = head.getArchetype(); part != null; part = part.getMultiNext()) {
207  point.x = p.x + part.getMultiX();
208  point.y = p.y + part.getMultiY();
209  if (!mapArchObject.isPointValid(point) || !mapModel.getMapSquare(point).isEmpty()) {
210  return;
211  }
212  }
213 
214  mapModel.insertBaseObject(head, p, true, false, insertionModeSet.getTopmostInsertionMode());
215  }
216  insertedObject = null;
217  } else {
218  final int modeIndex = modeComboBox.getSelectedIndex();
219  final InsertionMode<G, A, R> insertionMode;
220  switch (modeIndex) {
221  case MODE_AUTO:
222  insertionMode = insertionModeSet.getAutoInsertionMode();
223  break;
224 
225  case MODE_TOPMOST:
226  insertionMode = insertionModeSet.getTopmostInsertionMode();
227  break;
228 
229  case MODE_ABOVE_FLOOR:
230  insertionMode = insertionModeSet.getAboveFloorInsertionMode();
231  break;
232 
233  case MODE_BELOW_FLOOR:
234  insertionMode = insertionModeSet.getBelowFloorInsertionMode();
235  break;
236 
237  case MODE_BOTTOMMOST:
238  insertionMode = insertionModeSet.getBottommostInsertionMode();
239  break;
240 
241  default:
242  throw new AssertionError();
243  }
244  final BaseObject<G, A, R, ?> selectedArchetype = objectChooser.getSelection();
245  insertedObject = selectedArchetype == null ? null : mapModel.insertBaseObject(selectedArchetype, p, isPressed || modeIndex == MODE_AUTO, mapViewSettings.isAutojoin(), insertionMode);
246  }
247  selectedSquareModel.setSelectedGameObject(insertedObject);
248  } finally {
249  mapModel.endTransaction();
250  }
251  }
252 
253  @Nullable
254  @Override
255  public Component createOptionsView() {
256  final Container panel = new JPanel();
257  panel.setLayout(new GridBagLayout());
258 
259  final GridBagConstraints gbcLabel = new GridBagConstraints();
260  gbcLabel.anchor = GridBagConstraints.EAST;
261 
262  final GridBagConstraints gbcComboBox = new GridBagConstraints();
263  gbcComboBox.fill = GridBagConstraints.HORIZONTAL;
264  gbcComboBox.weightx = 1.0;
265  gbcComboBox.gridwidth = GridBagConstraints.REMAINDER;
266 
267  final GridBagConstraints gbcCheckBox = new GridBagConstraints();
268  gbcCheckBox.fill = GridBagConstraints.HORIZONTAL;
269  gbcCheckBox.gridwidth = GridBagConstraints.REMAINDER;
270 
271  panel.add(SwingUtils.createLabel("insertionTool.mode", modeComboBox), gbcLabel);
272  panel.add(modeComboBox, gbcComboBox);
273  return panel;
274  }
275 
280  private static JComboBox<?> createModeComboBox() {
281  final String[] options = { ActionBuilderUtils.getString(ACTION_BUILDER, "insertionTool.mode.auto"), ActionBuilderUtils.getString(ACTION_BUILDER, "insertionTool.mode.topmost"), ActionBuilderUtils.getString(ACTION_BUILDER, "insertionTool.mode.aboveFloor"), ActionBuilderUtils.getString(ACTION_BUILDER, "insertionTool.mode.belowFloor"), ActionBuilderUtils.getString(ACTION_BUILDER, "insertionTool.mode.bottommost"), };
282  final JComboBox<?> comboBox = new JComboBox<>(options);
283  comboBox.setToolTipText(ActionBuilderUtils.getString(ACTION_BUILDER, "insertionTool.mode.shortdescription"));
284  return comboBox;
285  }
286 
287 }
T getHead()
Return the head part of a multi-part object.
Base class for the default provided tools.
static final int MODE_TOPMOST
Index into modeComboBox: insert topmost.
G getSelectedGameObject()
Returns the currently selected GameObject within this list (currently selected MapSquare).
static final int MODE_ABOVE_FLOOR
Index into modeComboBox: insert above floor.
InsertionMode< G, A, R > getAutoInsertionMode()
Returns the "auto" insertion mode.
boolean isPointValid(@Nullable Point pos)
Checks whether the given coordinate is within map bounds.
A MapModel reflects the data of a map.
Definition: MapModel.java:75
boolean isAutojoin()
Returns whether "autojoin" is enabled.
final SelectedSquareModel< G, A, R > selectedSquareModel
The SelectedSquareModel.
Graphical User Interface of Gridarta.
static final int MODE_BELOW_FLOOR
Index into modeComboBox: insert below floor.
static final int MODE_AUTO
Index into modeComboBox: guess insertion location.
void released(@NotNull final MouseOpEvent< G, A, R > e)
void endTransaction()
End a transaction.
BaseObject< G, A, R, ?> getSelection()
Returns the active arch in the left-side panel.
MapCursor provides methods to move and drag on map.
Definition: MapCursor.java:57
static final int MODE_BOTTOMMOST
Index into modeComboBox: insert bottommost.
Utility class for Swing related functions.
Definition: SwingUtils.java:37
boolean setLocationSafe(@Nullable final Point p)
Move cursor to a new location.
Definition: MapCursor.java:254
MapModel< G, A, R > getMapModel()
Returns the map model.
static JComboBox<?> createModeComboBox()
Create a JComboBox for selecting the scope to delete.
final JComboBox<?> modeComboBox
The JComboBox for selecting the insertion mode.
static String getString(@NotNull final ActionBuilder actionBuilder, @NotNull final String key, @NotNull final String defaultValue)
Returns the value of a key.
G insertBaseObject(@NotNull BaseObject< G, A, R, ?> baseObject, @NotNull Point pos, boolean allowMany, boolean join, @NotNull InsertionMode< G, A, R > insertionMode)
Inserts a BaseObject to a map.
void setLocation(@NotNull final Point p)
Move cursor to a new location.
Definition: MapCursor.java:235
Base package of all Gridarta classes.
InsertionMode< G, A, R > getAboveFloorInsertionMode()
Returns the "above floor" insertion mode.
Reflects a game object (object on a map).
Definition: GameObject.java:36
final ObjectChooser< G, A, R > objectChooser
The ObjectChooser to use.
static final ActionBuilder ACTION_BUILDER
Action Builder.
boolean isPickmap()
Return flag that indicates whether this is a pickmap or not.
InsertionMode< G, A, R > getBottommostInsertionMode()
Returns the "bottommost" insertion mode.
void moved(@NotNull final MouseOpEvent< G, A, R > e)
Container for settings that affect the rendering of maps.
MapSquare< G, A, R > getMapSquare(@NotNull Point pos)
Get the square at a specified location.
InsertionMode< G, A, R > getTopmostInsertionMode()
Returns the "topmost" insertion mode.
GameObjects are the objects based on Archetypes found on maps.
void pressed(@NotNull final MouseOpEvent< G, A, R > e)
final MapViewSettings mapViewSettings
The MapViewSettings to use.
Displays the contents of the currently selected map square.
void doInsert(@NotNull final MouseOpEvent< G, A, R > e, final boolean isPressed)
Inserts an game object.
Base classes for rendering maps.
Utility class for ActionBuilder related functions.
A getMapArchObject()
Returns the Map Arch Object with the meta information about the map.
void dragged(@NotNull final MouseOpEvent< G, A, R > e)
Currently nothing more than a marker interface for unification.
Definition: MapControl.java:35
A MouseOpEvent is an event triggered for a MouseOpListener.
static Component createLabel(@NotNull final String key, @Nullable final Component component)
Create a javax.swing.JLabel instance.
Definition: SwingUtils.java:58
boolean isLocked()
Returns whether pickmaps are immutable.
void setSelectedGameObject(@Nullable final G gameObject)
Sets the currently selected GameObject.
Common base interface for ObjectChoosers.
void beginTransaction(@NotNull String name)
Starts a new transaction.
The model component of the selected square control.
InsertionTool(@NotNull final SelectedSquareModel< G, A, R > selectedSquareModel, @NotNull final ObjectChooser< G, A, R > objectChooser, @NotNull final PickmapSettings pickmapSettings, @NotNull final InsertionModeSet< G, A, R > insertionModeSet, @NotNull final MapViewSettings mapViewSettings)
Create a BasicAbstractTool.
void clicked(@NotNull final MouseOpEvent< G, A, R > e)
final PickmapSettings pickmapSettings
The PickmapSettings to use.
InsertionMode< G, A, R > getBelowFloorInsertionMode()
Returns the "below floor" insertion mode.
Container for settings that affect pickmaps.
final InsertionModeSet< G, A, R > insertionModeSet
The InsertionModeSet to use.