Gridarta Editor
ArchTab.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2023 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.gameobjectattributes;
21 
22 import java.awt.Color;
23 import java.awt.Component;
24 import java.awt.Container;
25 import java.awt.GridBagConstraints;
26 import java.awt.GridBagLayout;
27 import java.awt.Insets;
28 import javax.swing.ButtonGroup;
29 import javax.swing.JLabel;
30 import javax.swing.JPanel;
31 import javax.swing.JTextField;
32 import javax.swing.JToggleButton;
33 import javax.swing.text.JTextComponent;
43 import net.sf.japi.swing.action.ActionBuilder;
44 import net.sf.japi.swing.action.ActionBuilderFactory;
45 import net.sf.japi.swing.action.ActionMethod;
46 import org.jetbrains.annotations.NotNull;
47 import org.jetbrains.annotations.Nullable;
48 
55 public class ArchTab<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> extends AbstractGameObjectAttributesTab<G, A, R> {
56 
60  @NotNull
61  private static final ActionBuilder ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder("net.sf.gridarta");
62 
66  @NotNull
68 
72  @NotNull
74 
78  @NotNull
79  private final JTextComponent archNameField = new JTextField(14);
80 
84  @NotNull
85  private final JLabel archFaceText = new JLabel();
86 
90  @NotNull
91  private final JLabel archTypeText = new JLabel();
92 
96  @NotNull
97  private final JLabel archMapPos = new JLabel();
98 
103  @NotNull
104  private final ButtonGroup directionButtonGroup = new ButtonGroup();
105 
110  @NotNull
111  private final JToggleButton @NotNull [] directionButtons = new JToggleButton[9];
112 
116  @NotNull
117  private final JPanel panel = new JPanel();
118 
122  @NotNull
123  private String defaultArchNameFieldValue = "";
124 
132  this.archetypeTypeSet = archetypeTypeSet;
133  this.gameObjectAttributesModel = gameObjectAttributesModel;
134 
135  final GridBagLayout gridBagLayout = new GridBagLayout();
136  final GridBagConstraints gbc = new GridBagConstraints();
137 
138  panel.setLayout(gridBagLayout);
139  gbc.fill = GridBagConstraints.BOTH;
140  gbc.weightx = 1.0;
141  gbc.weighty = 0.0;
142  gbc.anchor = GridBagConstraints.NORTHWEST;
143  gbc.insets = new Insets(4, 4, 0, 0);
144 
145  setArchNameField("", Color.black);
146  gbc.gridx = 0;
147  gbc.gridy = 0;
148  gbc.gridwidth = 1;
149  gbc.gridheight = 1;
150  gridBagLayout.setConstraints(archNameField, gbc);
151  panel.add(archNameField);
152 
153  archFaceText.setText("");
154  gbc.gridx = 0;
155  gbc.gridy = 1;
156  gbc.gridwidth = 1;
157  gbc.gridheight = 1;
158  gridBagLayout.setConstraints(archFaceText, gbc);
159  panel.add(archFaceText);
160 
161  archTypeText.setText("");
162  gbc.gridx = 0;
163  gbc.gridy = 2;
164  gbc.gridwidth = 1;
165  gbc.gridheight = 1;
166  gridBagLayout.setConstraints(archTypeText, gbc);
167  panel.add(archTypeText);
168 
169  archMapPos.setText("");
170  gbc.gridx = 0;
171  gbc.gridy = 3;
172  gbc.gridwidth = 1;
173  gbc.gridheight = 1;
174  gridBagLayout.setConstraints(archMapPos, gbc);
175  panel.add(archMapPos);
176 
177  gbc.gridx = 1;
178  gbc.gridy = 0;
179  gbc.gridwidth = 1;
180  gbc.gridheight = 5;
181  gbc.weightx = 0.0;
182  final Component dirPanel = createDirPanel();
183  panel.add(dirPanel, gbc);
184 
187  }
188 
189  @NotNull
190  @Override
191  public JPanel getPanel() {
192  return panel;
193  }
194 
199  @NotNull
200  private Component createDirPanel() {
201  final Container dirPanel = new JPanel(new GridBagLayout());
202  final GridBagConstraints gbc = new GridBagConstraints();
203  gbc.fill = GridBagConstraints.BOTH;
204  gbc.anchor = GridBagConstraints.CENTER;
205  gbc.insets = new Insets(2, 2, 2, 2);
206 
207  createButton(7, dirPanel, gbc, 0, 0);
208  createButton(8, dirPanel, gbc, 1, 0);
209  createButton(1, dirPanel, gbc, 2, 0);
210  createButton(6, dirPanel, gbc, 0, 1);
211  createButton(0, dirPanel, gbc, 1, 1);
212  createButton(2, dirPanel, gbc, 2, 1);
213  createButton(5, dirPanel, gbc, 0, 2);
214  createButton(4, dirPanel, gbc, 1, 2);
215  createButton(3, dirPanel, gbc, 2, 2);
216 
217  return dirPanel;
218  }
219 
228  private void createButton(final int direction, @NotNull final Container panel, @NotNull final GridBagConstraints gbc, final int x, final int y) {
229  final JToggleButton button = new JToggleButton(ACTION_BUILDER.createAction(false, "direction" + direction, this));
230  directionButtonGroup.add(button);
231  button.setFocusable(false);
232  button.setEnabled(false);
233  gbc.gridx = x;
234  gbc.gridy = y;
235  panel.add(button, gbc);
236  directionButtons[direction] = button;
237  }
238 
239  @Override
240  public boolean canApply() {
241  return !archNameField.getText().equals(defaultArchNameFieldValue);
242  }
243 
244  @Override
245  public void activate() {
246  archNameField.requestFocusInWindow();
247  }
248 
249  @Override
250  protected void apply(@NotNull final G gameObject) {
251  gameObject.setAttributeString(BaseObject.NAME, archNameField.getText());
252  }
253 
254  @Override
255  protected final void refresh(@Nullable final G gameObject) {
256  //noinspection VariableNotUsedInsideIf
257  setTabSeverity(gameObject == null ? Severity.DEFAULT : Severity.MODIFIED);
258 
259  // archNameField
260  if (gameObject == null) {
261  setArchNameField("", Color.black);
262  } else {
263  final String objName = gameObject.getAttributeString(BaseObject.NAME, false);
264  if (objName.isEmpty()) {
265  final String archObjName = gameObject.getArchetype().getObjName();
266  setArchNameField(archObjName.isEmpty() ? gameObject.getArchetype().getArchetypeName() : archObjName, Color.black);
267  } else {
268  setArchNameField(objName, Color.blue);
269  }
270  }
271 
272  // archFaceText
273  if (gameObject == null) {
274  archFaceText.setText("Image:");
275  } else {
276  final StringBuilder faceText = new StringBuilder("Image: ");
277  appendFaceSource(faceText, gameObject);
278  archFaceText.setText(faceText.toString());
279  }
280 
281  // archTypeText
282  if (gameObject == null) {
283  archTypeText.setText("Type:");
284  } else {
285  final StringBuilder typeText = new StringBuilder("Type: ");
286  typeText.append(archetypeTypeSet.getDisplayName(gameObject));
287  typeText.append(" [").append(gameObject.getArchetype().getArchetypeName()).append(']');
288  if (gameObject.isMulti()) {
289  typeText.append(" [").append(gameObject.getSizeX()).append('x').append(gameObject.getSizeY()).append(']');
290  } else {
291  typeText.append(" [single]");
292  }
293  archTypeText.setText(typeText.toString());
294  }
295 
296  // archMapPos
297  if (gameObject == null) {
298  archMapPos.setText("Status: ");
299  } else {
300  final StringBuilder specialText = new StringBuilder("Status:");
301  if (!gameObject.isInContainer()) {
302  specialText.append(" (map: ").append(gameObject.getMapX()).append(", ").append(gameObject.getMapY()).append(')');
303  }
304  if (gameObject.isScripted()) {
305  specialText.append(" (script)");
306  }
307  if (gameObject.getMsgText(true) != null) {
308  specialText.append(" (msg)");
309  }
310  final int invObjects = gameObject.countInvObjects();
311  if (invObjects > 0) {
312  specialText.append(" (inv: ").append(invObjects).append(')');
313  }
314  final BaseObject<G, A, R, ?> cont = gameObject.getContainerGameObject();
315  if (cont != null) {
316  specialText.append(" (env: ").append(cont.getArchetype().getArchetypeName()).append(')');
317  }
318  archMapPos.setText(specialText.toString());
319  }
320 
321  // directions
322  for (final Component button : directionButtons) {
323  button.setBackground(null);
324  }
325  final boolean enableDirButtons = gameObject != null && gameObject.usesDirection();
326  for (final Component button : directionButtons) {
327  button.setEnabled(enableDirButtons);
328  }
329  final int direction = enableDirButtons ? gameObject.getDirection() : 0;
330  directionButtons[direction < directionButtons.length ? direction : 0].setSelected(true);
331  }
332 
339  private static void appendFaceSource(@NotNull final StringBuilder sb, @NotNull final BaseObject<?, ?, ?, ?> gameObject) {
340  final String faceName = gameObject.getFaceObjName();
341  if (faceName == null) {
342  sb.append(">no face<");
343  } else {
344  final FaceSource faceSource = gameObject.getFaceObjSource();
345  sb.append(faceName).append(" (");
346  switch (faceSource) {
347  case FACE_NOT_FOUND:
348  sb.append("face not found");
349  break;
350 
351  case FACE:
352  sb.append("face");
353  break;
354 
355  case ARCHETYPE_FACE:
356  sb.append("archetype face");
357  break;
358 
359  case ANIM:
360  sb.append("anim");
361  break;
362 
363  case ARCHETYPE_ANIM:
364  sb.append("archetype anim");
365  break;
366  }
367  sb.append(')');
368  }
369  }
370 
374  @ActionMethod
375  public void direction0() {
376  direction(0);
377  }
378 
382  @ActionMethod
383  public void direction1() {
384  direction(1);
385  }
386 
390  @ActionMethod
391  public void direction2() {
392  direction(2);
393  }
394 
398  @ActionMethod
399  public void direction3() {
400  direction(3);
401  }
402 
406  @ActionMethod
407  public void direction4() {
408  direction(4);
409  }
410 
414  @ActionMethod
415  public void direction5() {
416  direction(5);
417  }
418 
422  @ActionMethod
423  public void direction6() {
424  direction(6);
425  }
426 
430  @ActionMethod
431  public void direction7() {
432  direction(7);
433  }
434 
438  @ActionMethod
439  public void direction8() {
440  direction(8);
441  }
442 
447  private void direction(final int direction) {
449  if (selectedGameObject == null) {
450  return;
451  }
452 
453  final MapSquare<G, A, R> mapSquare = selectedGameObject.getMapSquareOptional();
454  if (mapSquare == null) {
455  return;
456  }
457 
458  final MapModel<G, A, R> mapModel = mapSquare.getMapModel();
459  mapModel.beginTransaction("Change object attributes");
460  try {
462  } finally {
463  mapModel.endTransaction();
464  }
465  }
466 
472  private void setArchNameField(@NotNull final String objName, @NotNull final Color color) {
473  archNameField.setForeground(color);
474  archNameField.setText(objName);
475  defaultArchNameFieldValue = objName;
476  }
477 
478 }
net.sf.gridarta.gui.panel.gameobjectattributes.ArchTab.defaultArchNameFieldValue
String defaultArchNameFieldValue
The initial value of the archNameField.
Definition: ArchTab.java:123
net.sf.gridarta.model.archetypetype
Defines types of GameObjects with corresponding attributes.
Definition: AbstractArchetypeAttributeInvSpell.java:20
net.sf.gridarta.model.mapmodel.MapModel
A MapModel reflects the data of a map.
Definition: MapModel.java:75
net.sf.gridarta.gui.panel.gameobjectattributes.ArchTab.archNameField
final JTextComponent archNameField
The input field containing the game object's name.
Definition: ArchTab.java:79
net.sf.gridarta.gui.panel.gameobjectattributes.ArchTab
The "Arch" tab in the archetype chooser.
Definition: ArchTab.java:55
net.sf.gridarta.model.gameobject.FaceSource
Enumeration describing the state of the face.
Definition: FaceSource.java:28
net.sf.gridarta.gui.utils.Severity.MODIFIED
MODIFIED
The tab contents are modified from defaults.
Definition: Severity.java:39
net.sf.gridarta.gui.panel.gameobjectattributes.ArchTab.archetypeTypeSet
final ArchetypeTypeSet archetypeTypeSet
The archetype type set.
Definition: ArchTab.java:67
net.sf.gridarta
Base package of all Gridarta classes.
net.sf.gridarta.gui.panel.gameobjectattributes.ArchTab.refresh
final void refresh(@Nullable final G gameObject)
Definition: ArchTab.java:255
net.sf.gridarta.model.mapmodel.MapSquare
A single Map Square.
Definition: MapSquare.java:45
net.sf.gridarta.model.mapmodel.MapModel.endTransaction
void endTransaction()
End a transaction.
net.sf.gridarta.gui.panel.gameobjectattributes.AbstractGameObjectAttributesTab< G, A, R >::selectedGameObject
G selectedGameObject
The currently selected game object.
Definition: AbstractGameObjectAttributesTab.java:55
net.sf.gridarta.model.archetypetype.ArchetypeTypeSet.getDisplayName
String getDisplayName(@NotNull final BaseObject<?, ?, ?, ?> baseObject)
Returns a description of this type.
Definition: ArchetypeTypeSet.java:258
net.sf.gridarta.gui.panel.gameobjectattributes.ArchTab.panel
final JPanel panel
The content panel.
Definition: ArchTab.java:117
net.sf
net.sf.gridarta.model.mapmodel.MapModel.beginTransaction
void beginTransaction(@NotNull String name)
Starts a new transaction.
net.sf.gridarta.gui.panel.gameobjectattributes.ArchTab.getPanel
JPanel getPanel()
Definition: ArchTab.java:191
net.sf.gridarta.model.mapmodel
Definition: AboveFloorInsertionMode.java:20
net.sf.gridarta.gui.panel.gameobjectattributes.ArchTab.gameObjectAttributesModel
final GameObjectAttributesModel< G, A, R > gameObjectAttributesModel
The model to track.
Definition: ArchTab.java:73
net.sf.gridarta.model.archetype
Definition: AbstractArchetype.java:20
net.sf.gridarta.model.gameobject.GameObject
Reflects a game object (object on a map).
Definition: GameObject.java:36
net.sf.gridarta.model.baseobject.BaseObject.NAME
String NAME
The attribute name of the object's name.
Definition: BaseObject.java:60
net.sf.gridarta.gui.panel.gameobjectattributes.ArchTab.direction3
void direction3()
Action method for direction.
Definition: ArchTab.java:399
net.sf.gridarta.gui.panel.gameobjectattributes.ArchTab.createButton
void createButton(final int direction, @NotNull final Container panel, @NotNull final GridBagConstraints gbc, final int x, final int y)
Creates a direction button.
Definition: ArchTab.java:228
net.sf.gridarta.model.archetypetype.ArchetypeTypeSet
Manages ArchetypeType instances, list, and bitmask definitions.
Definition: ArchetypeTypeSet.java:40
net.sf.gridarta.gui.panel.gameobjectattributes.AbstractGameObjectAttributesTab
Base class for GameObjectAttributesTab implementations.
Definition: AbstractGameObjectAttributesTab.java:38
net.sf.gridarta.gui
Graphical User Interface of Gridarta.
net.sf.gridarta.gui.panel.gameobjectattributes.ArchTab.directionButtonGroup
final ButtonGroup directionButtonGroup
The ButtonGroup for the direction buttons.
Definition: ArchTab.java:104
net.sf.gridarta.gui.panel.gameobjectattributes.ArchTab.direction4
void direction4()
Action method for direction.
Definition: ArchTab.java:407
net.sf.gridarta.model.baseobject.BaseObject.getArchetype
R getArchetype()
Returns the Archetype this GameObject is based on.
net.sf.gridarta.gui.panel.gameobjectattributes.ArchTab.canApply
boolean canApply()
Definition: ArchTab.java:240
net.sf.gridarta.gui.utils.Severity
Severity levels for colors of tabs.
Definition: Severity.java:29
net.sf.gridarta.model.gameobject
GameObjects are the objects based on Archetypes found on maps.
Definition: AbstractGameObject.java:20
net.sf.gridarta.gui.panel.gameobjectattributes.ArchTab.direction7
void direction7()
Action method for direction.
Definition: ArchTab.java:431
net
net.sf.gridarta.gui.panel.gameobjectattributes.ArchTab.direction5
void direction5()
Action method for direction.
Definition: ArchTab.java:415
net.sf.gridarta.gui.panel.gameobjectattributes.ArchTab.appendFaceSource
static void appendFaceSource(@NotNull final StringBuilder sb, @NotNull final BaseObject<?, ?, ?, ?> gameObject)
Appends a description of a game object's face to a {}.
Definition: ArchTab.java:339
net.sf.gridarta.model.maparchobject.MapArchObject
Interface for MapArchObjects.
Definition: MapArchObject.java:40
net.sf.gridarta.gui.panel.gameobjectattributes.GameObjectAttributesModel< G, A, R >
net.sf.gridarta.gui.panel.gameobjectattributes.ArchTab.apply
void apply(@NotNull final G gameObject)
Definition: ArchTab.java:250
net.sf.gridarta.gui.panel.gameobjectattributes.ArchTab.archFaceText
final JLabel archFaceText
The label containing the game object's face name.
Definition: ArchTab.java:85
net.sf.gridarta.gui.panel.gameobjectattributes.ArchTab.createDirPanel
Component createDirPanel()
Creates the panel consisting of the direction buttons.
Definition: ArchTab.java:200
net.sf.gridarta.gui.panel.gameobjectattributes.ArchTab.direction
void direction(final int direction)
Action proxy for direction.
Definition: ArchTab.java:447
net.sf.gridarta.gui.panel.gameobjectattributes.ArchTab.direction2
void direction2()
Action method for direction.
Definition: ArchTab.java:391
net.sf.gridarta.gui.panel.gameobjectattributes.ArchTab.directionButtons
final JToggleButton[] directionButtons
The buttons for the direction panel.
Definition: ArchTab.java:111
net.sf.gridarta.model.baseobject.BaseObject
Definition: BaseObject.java:34
net.sf.gridarta.gui.panel.gameobjectattributes.AbstractGameObjectAttributesTab< G, A, R >::addAutoApply
void addAutoApply( @NotNull final Component component)
Registers a component that auto-applies when the focus is lost.
Definition: AbstractGameObjectAttributesTab.java:161
net.sf.gridarta.gui.panel.gameobjectattributes.ArchTab.direction6
void direction6()
Action method for direction.
Definition: ArchTab.java:423
net.sf.gridarta.model.mapmodel.MapSquare.getMapModel
MapModel< G, A, R > getMapModel()
Returns the MapModel this map square is part of.
Definition: MapSquare.java:99
net.sf.gridarta.gui.panel.gameobjectattributes.ArchTab.archMapPos
final JLabel archMapPos
The label containing the location of the game object on the map.
Definition: ArchTab.java:97
net.sf.gridarta.gui.panel.gameobjectattributes.ArchTab.direction8
void direction8()
Action method for direction.
Definition: ArchTab.java:439
net.sf.gridarta.gui.panel.gameobjectattributes.AbstractGameObjectAttributesTab< G, A, R >::setTabSeverity
void setTabSeverity( @NotNull final Severity tabSeverity)
Sets the tab severity.
Definition: AbstractGameObjectAttributesTab.java:102
net.sf.gridarta.model
net.sf.gridarta.model.archetype.Archetype
Reflects an Archetype.
Definition: Archetype.java:41
net.sf.gridarta.model.baseobject
Definition: AbstractBaseObject.java:20
net.sf.gridarta.gui.panel.gameobjectattributes.ArchTab.ArchTab
ArchTab(@NotNull final ArchetypeTypeSet archetypeTypeSet, @NotNull final GameObjectAttributesModel< G, A, R > gameObjectAttributesModel)
Creates a new instance.
Definition: ArchTab.java:130
net.sf.gridarta.gui.panel.gameobjectattributes.ArchTab.archTypeText
final JLabel archTypeText
The label containing information about the game object's type.
Definition: ArchTab.java:91
net.sf.gridarta.model.maparchobject
Definition: AbstractMapArchObject.java:20
net.sf.gridarta.gui.panel.gameobjectattributes.ArchTab.ACTION_BUILDER
static final ActionBuilder ACTION_BUILDER
The action builder.
Definition: ArchTab.java:61
net.sf.gridarta.gui.utils
Definition: AnimationComponent.java:20
net.sf.gridarta.gui.panel.gameobjectattributes.ArchTab.direction0
void direction0()
Action method for direction.
Definition: ArchTab.java:375
net.sf.gridarta.gui.panel.gameobjectattributes.ArchTab.activate
void activate()
Definition: ArchTab.java:245
net.sf.gridarta.gui.panel.gameobjectattributes.GameObjectAttributesModel.getSelectedGameObject
G getSelectedGameObject()
Returns the selected game object.
Definition: GameObjectAttributesModel.java:78
net.sf.gridarta.gui.utils.Severity.DEFAULT
DEFAULT
The tab contents are unchanged from defaults.
Definition: Severity.java:34
net.sf.gridarta.gui.panel.gameobjectattributes.ArchTab.setArchNameField
void setArchNameField(@NotNull final String objName, @NotNull final Color color)
Updates the value of the archNameField.
Definition: ArchTab.java:472
net.sf.gridarta.gui.panel.gameobjectattributes.ArchTab.direction1
void direction1()
Action method for direction.
Definition: ArchTab.java:383
net.sf.gridarta.model.baseobject.BaseObject.DIRECTION
String DIRECTION
The attribute name of the object's direction.
Definition: BaseObject.java:48