Gridarta Editor
ViewActions.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.viewaction;
21 
22 import java.awt.Container;
23 import java.util.Arrays;
24 import java.util.Comparator;
25 import java.util.LinkedList;
26 import java.util.List;
27 import javax.swing.Action;
38 import net.sf.japi.swing.action.ActionMethod;
39 import org.jetbrains.annotations.NotNull;
40 import org.jetbrains.annotations.Nullable;
41 
46 public class ViewActions<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> implements EditorAction {
47 
51  @NotNull
53 
57  @NotNull
58  private final List<ViewAction> viewActions = new LinkedList<>();
59 
64  @NotNull
66 
67  @Override
68  public void currentMapChanged(@Nullable final MapControl<G, A, R> mapControl) {
69  updateActions();
70  }
71 
72  @Override
73  public void mapCreated(@NotNull final MapControl<G, A, R> mapControl, final boolean interactive) {
74  // ignore
75  }
76 
77  @Override
78  public void mapClosing(@NotNull final MapControl<G, A, R> mapControl) {
79  // ignore
80  }
81 
82  @Override
83  public void mapClosed(@NotNull final MapControl<G, A, R> mapControl) {
84  // ignore
85  }
86 
87  };
88 
93  @NotNull
95 
96  @Override
97  public void gridVisibleChanged(final boolean gridVisible) {
98  // ignore
99  }
100 
101  @Override
102  public void lightVisibleChanged(final boolean lightVisible) {
103  // ignore
104  }
105 
106  @Override
107  public void smoothingChanged(final boolean smoothing) {
108  // ignore
109  }
110 
111  @Override
112  public void tileStretchingChanged(final boolean tileStretching) {
113  // ignore
114  }
115 
116  @Override
117  public void doubleFacesChanged(final boolean doubleFaces) {
118  // ignore
119  }
120 
121  @Override
122  public void alphaTypeChanged(final int alphaType) {
123  // ignore
124  }
125 
126  @Override
127  public void editTypeChanged(final int editType) {
128  updateActions();
129  }
130 
131  @Override
132  public void autojoinChanged(final boolean autojoin) {
133  // ignore
134  }
135 
136  };
137 
143  public ViewActions(@NotNull final MapViewSettings mapViewSettings, @NotNull final MapManager<G, A, R> mapManager) {
144  this.mapViewSettings = mapViewSettings;
145  mapManager.addMapManagerListener(mapManagerListener);
146  mapViewSettings.addMapViewSettingsListener(mapViewSettingsListener);
147  }
148 
152  @NotNull
153  private static final Comparator<ViewAction> ACTION_NAME_COMPARATOR = new Comparator<ViewAction>() {
154 
155  @Override
156  public int compare(final ViewAction o1, final ViewAction o2) {
157  return String.CASE_INSENSITIVE_ORDER.compare(o1.getName(), o2.getName());
158  }
159 
160  };
161 
166  public void init(@NotNull final Iterable<NamedGameObjectMatcher> gameObjectMatchers) {
167  for (final NamedGameObjectMatcher matcher : gameObjectMatchers) {
168  viewActions.add(new ViewAction(mapViewSettings, matcher));
169  }
170  }
171 
176  public void setMenu(@Nullable final Container viewActionsMenu) {
177  if (viewActionsMenu == null) {
178  return;
179  }
180 
181  final ViewAction[] actions = viewActions.toArray(new ViewAction[viewActions.size()]);
182  Arrays.sort(actions, ACTION_NAME_COMPARATOR);
183  int index = 0;
184  for (final ViewAction viewAction : actions) {
185  viewActionsMenu.add(viewAction.getCheckBoxMenuItem(), index++);
186  }
187  }
188 
192  @ActionMethod
193  public void resetView() {
194  // set edit type to zero (-> show all)
195  mapViewSettings.unsetEditType(~0);
196  }
197 
201  private void updateActions() {
202  for (final ViewAction viewAction : viewActions) {
203  viewAction.updateAction();
204  }
205  }
206 
207  @Override
208  public void setAction(@NotNull final Action action, @NotNull final String name) {
209  }
210 
211 }
A MapManager manages all opened maps.
Definition: MapManager.java:37
This package contains classes related to matching GameObjects, so called GameObjectMatchers.
final MapViewSettingsListener mapViewSettingsListener
The MapViewSettingsListener attached to mapViewSettings to detect edit types changes.
void resetView()
Action method for "reset view".
Action to manage one edit type represented by one NamedGameObjectMatcher.
Definition: ViewAction.java:38
Decorates an arbitrary GameObjectMatcher with a localized name that is suitable for the user interfac...
void addMapViewSettingsListener(@NotNull MapViewSettingsListener listener)
Register a MapViewSettingsListener.
Base package of all Gridarta classes.
Reflects a game object (object on a map).
Definition: GameObject.java:36
String getName()
Returns the name for sorting the menu entries.
Definition: ViewAction.java:92
A global editor action.
Interface for listeners listening to MapManager changes.
Interface for event listeners that are interested in changes on MapViewSettings.
Container for settings that affect the rendering of maps.
GameObjects are the objects based on Archetypes found on maps.
final List< ViewAction > viewActions
The view actions.
void setAction(@NotNull final Action action, @NotNull final String name)
Sets the Action instance for this editor action.
void init(@NotNull final Iterable< NamedGameObjectMatcher > gameObjectMatchers)
Creates the view actions.
void updateActions()
Update the actions&#39; state to the current edit mode.
ViewActions(@NotNull final MapViewSettings mapViewSettings, @NotNull final MapManager< G, A, R > mapManager)
Create a new instance.
Currently nothing more than a marker interface for unification.
Definition: MapControl.java:35
final MapViewSettings mapViewSettings
The map view settings instance.
static final Comparator< ViewAction > ACTION_NAME_COMPARATOR
The comparator for sorting menu entries.
void unsetEditType(int editType)
Set the map view to hide squares of the given type.
final MapManagerListener< G, A, R > mapManagerListener
The map manager listener which is attached to the current map if the current map is tracked...
void setMenu(@Nullable final Container viewActionsMenu)
Sets the menu to add the actions to.
Manages edit type actions in "view" menu.