Crossfire JXClient, Trunk
GUIItemShortcut.java
Go to the documentation of this file.
1 /*
2  * This file is part of JXClient, the Fullscreen Java Crossfire Client.
3  *
4  * JXClient is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * JXClient is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with JXClient; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  * Copyright (C) 2005-2008 Yann Chachkoff
19  * Copyright (C) 2006-2017,2019-2023 Andreas Kirschbaum
20  * Copyright (C) 2010-2012,2014-2018,2020-2023 Nicolas Weeger
21  */
22 
23 package com.realtime.crossfire.jxclient.gui.item;
24 
39 import java.awt.Color;
40 import java.awt.Dimension;
41 import java.awt.Font;
42 import java.awt.Graphics;
43 import java.awt.Image;
44 import org.jetbrains.annotations.NotNull;
45 import org.jetbrains.annotations.Nullable;
46 
51 public class GUIItemShortcut extends GUIItem {
52 
56  private static final long serialVersionUID = 1;
57 
61  @NotNull
62  private static final Color BACKGROUND_COLOR = new Color(0, 0, 0, 0.0f);
63 
67  @NotNull
68  private static final String DEFAULT_TOOLTIP_TEXT = "(empty)";
69 
74  @NotNull
75  private final Shortcuts shortcuts;
76 
80  @NotNull
81  private final FacesManager facesManager;
82 
86  @NotNull
88 
92  @Nullable
93  private final Color activateColor;
94 
98  @Nullable
99  private final Image activateImage;
100 
104  @Nullable
105  private final Color immediateColor;
106 
110  @Nullable
111  private final Image immediateImage;
112 
116  @NotNull
117  private final Font font;
118 
122  @NotNull
123  private final ShortcutSlot shortcutSlot;
124 
128  @NotNull
129  private final Object sync = new Object();
130 
135  @Nullable
137 
141  @NotNull
143 
144  @Override
145  public void shortcutAdded(@NotNull final ShortcutSlot shortcutSlot, @NotNull final Shortcut shortcut) {
148  }
149  }
150 
151  @Override
152  public void shortcutRemoved(@NotNull final ShortcutSlot shortcutSlot, @NotNull final Shortcut shortcut) {
154  setShortcut(null);
155  }
156  }
157  };
158 
162  @NotNull
163  private final ShortcutListener shortcutListener = () -> {
164  setChanged();
165  tooltipChanged();
166  };
167 
171  @NotNull
172  private final FacesManagerListener facesManagerListener = face -> {
173  final Shortcut shortcut = getShortcut();
174  if (shortcut != null && shortcut.displaysFace(face)) {
175  setChanged();
176  }
177  };
178 
199  public GUIItemShortcut(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, @Nullable final Color activateColor, @Nullable final Image activateImage, @Nullable final Color immediateColor, @Nullable final Image immediateImage, @NotNull final ShortcutSlot shortcutSlot, @NotNull final FacesManager facesManager, @NotNull final CommandExecutor commandExecutor, @NotNull final Shortcuts shortcuts, @NotNull final Font font, @NotNull final GuiFactory guiFactory) {
201  this.shortcuts = shortcuts;
202  this.facesManager = facesManager;
203  this.commandExecutor = commandExecutor;
204  this.activateColor = activateColor;
205  this.activateImage = activateImage;
206  this.immediateColor = immediateColor;
207  this.immediateImage = immediateImage;
208  this.font = font;
209  this.shortcutSlot = shortcutSlot;
210  this.shortcuts.addShortcutsListener(shortcutsListener);
211  this.facesManager.addFacesManagerListener(facesManagerListener);
212  }
213 
214  @Override
215  public void dispose() {
216  super.dispose();
219  setShortcut(null);
220  }
221 
226  @Nullable
227  private Shortcut getShortcut() {
228  synchronized (sync) {
229  return shortcut;
230  }
231  }
232 
233  @Nullable
234  @Override
236  final Shortcut shortcut = getShortcut();
238  }
239 
240  @Override
241  public void notifyOpen() {
242  }
243 
249  private void setShortcut(@Nullable final Shortcut shortcut) {
250  synchronized (sync) {
251  if (this.shortcut == shortcut) {
252  return;
253  }
254 
255  if (this.shortcut != null) {
256  this.shortcut.removeShortcutListener(shortcutListener);
257  }
258  this.shortcut = shortcut;
259  if (this.shortcut != null) {
260  this.shortcut.addShortcutListener(shortcutListener);
261  }
262  }
263  setChanged();
264  tooltipChanged();
265  }
266 
267  @Override
268  public void button1Clicked(final int modifiers) {
269  final Shortcut shortcut = getShortcut();
270  switch (modifiers&Modifiers.MASK) {
271  case Modifiers.NONE:
272  if (shortcut != null) {
274  }
275  break;
276 
277  case Modifiers.CTRL:
278  if (shortcut != null) {
280  }
281  break;
282  }
283  }
284 
285  @Override
286  public void button2Clicked(final int modifiers) {
287  //noinspection SwitchStatementWithTooFewBranches
288  switch (modifiers&Modifiers.MASK) {
289  case Modifiers.NONE:
290  final Shortcut shortcut = getShortcut();
291  if (shortcut != null) {
293  }
294  break;
295  }
296  }
297 
298  @Override
299  public void button3Clicked(final int modifiers) {
300  }
301 
302  @Override
303  public void paintComponent(@NotNull final Graphics g) {
304  super.paintComponent(g);
305 
306  g.setColor(BACKGROUND_COLOR);
307  g.fillRect(0, 0, getWidth(), getHeight());
308 
309  final Shortcut shortcut = getShortcut();
310  if (shortcut != null) {
311  final Spell spell = shortcut.getSpell();
312  if (spell != null) {
313  final Color color = shortcut.isImmediate() ? immediateColor : activateColor;
314  if (color != null) {
315  g.setColor(color);
316  g.fillRect(0, 0, getWidth(), getHeight());
317  }
318  g.drawImage(facesManager.getOriginalImageIcon(spell.getFaceNum(), null).getImage(), 0, 0, null);
319  final Image image = shortcut.isImmediate() ? immediateImage : activateImage;
320  if (image != null) {
321  g.drawImage(image, 0, 0, null);
322  }
323  }
324  }
325 
326  g.setFont(font);
327  //noinspection VariableNotUsedInsideIf
328  g.setColor(shortcut == null ? Color.LIGHT_GRAY : Color.DARK_GRAY); // XXX: define in skin
329  g.drawString(shortcutSlot.toString(), 1, 1+font.getSize()); // XXX: define in skin
330  }
331 
332  @NotNull
333  @Override
334  @SuppressWarnings("MethodDoesntCallSuperMethod")
335  public Dimension getPreferredSize() {
336  return getMinimumSizeInt();
337  }
338 
339  @NotNull
340  @Override
341  @SuppressWarnings("MethodDoesntCallSuperMethod")
342  public Dimension getMinimumSize() {
343  return getMinimumSizeInt();
344  }
345 
350  @NotNull
351  private static Dimension getMinimumSizeInt() {
352  return new Dimension(32, 32);
353  }
354 
355  @Override
356  public boolean canScroll(final int distance) {
357  return false;
358  }
359 
360  @Override
361  public void scroll(final int distance) {
362  }
363 
364  @Override
365  public void resetScroll() {
366  }
367 
368 }
com.realtime.crossfire.jxclient
com.realtime.crossfire.jxclient.skin.skin
Definition: DefaultJXCSkin.java:23
com.realtime.crossfire.jxclient.gui.item.GUIItemShortcut.setShortcut
void setShortcut(@Nullable final Shortcut shortcut)
Updates shortcut and registers/de-registers shortcutListener.
Definition: GUIItemShortcut.java:249
com.realtime.crossfire.jxclient.faces.FacesManager
Maintains a mapping of face numbers to face data.
Definition: FacesManager.java:40
com.realtime.crossfire.jxclient.shortcuts.Shortcut.getCommand
String getCommand()
Returns the command to execute.
Definition: Shortcut.java:76
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement.tooltipChanged
void tooltipChanged()
Must be called whenever the tooltip may have changed.
Definition: AbstractGUIElement.java:264
com.realtime.crossfire.jxclient.gui.item.GUIItemShortcut.serialVersionUID
static final long serialVersionUID
The serial version UID.
Definition: GUIItemShortcut.java:56
com.realtime.crossfire.jxclient.gui.item.GUIItemShortcut.canScroll
boolean canScroll(final int distance)
Returns whether scrolling is possible.
Definition: GUIItemShortcut.java:356
com.realtime.crossfire.jxclient.faces.FacesManager.removeFacesManagerListener
void removeFacesManagerListener(@NotNull FacesManagerListener facesManagerListener)
Removes a FacesManagerListener to be notified about updated faces.
com.realtime.crossfire.jxclient.shortcuts.ShortcutSlot
A slot in the shortcuts bar.
Definition: ShortcutSlot.java:8
com.realtime.crossfire.jxclient.gui.item.GUIItemShortcut.getShortcut
Shortcut getShortcut()
Returns shortcut.
Definition: GUIItemShortcut.java:227
com.realtime.crossfire.jxclient.gui.item.GUIItemShortcut.GUIItemShortcut
GUIItemShortcut(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, @Nullable final Color activateColor, @Nullable final Image activateImage, @Nullable final Color immediateColor, @Nullable final Image immediateImage, @NotNull final ShortcutSlot shortcutSlot, @NotNull final FacesManager facesManager, @NotNull final CommandExecutor commandExecutor, @NotNull final Shortcuts shortcuts, @NotNull final Font font, @NotNull final GuiFactory guiFactory)
Creates a new instance.
Definition: GUIItemShortcut.java:199
com.realtime.crossfire.jxclient.gui.item.GUIItemShortcut.shortcut
Shortcut shortcut
The currently monitored Shortcut instance.
Definition: GUIItemShortcut.java:136
com.realtime.crossfire.jxclient.spells.Spell.getFaceNum
int getFaceNum()
Returns the face number.
Definition: Spell.java:215
com.realtime.crossfire.jxclient.skin
com.realtime.crossfire.jxclient.faces.FacesManager.getOriginalImageIcon
ImageIcon getOriginalImageIcon(int faceNum, @Nullable AtomicBoolean returnIsUnknownImage)
Returns the "original" face for a face ID.
com.realtime.crossfire.jxclient.gui.item.GUIItemShortcut.commandExecutor
final CommandExecutor commandExecutor
The CommandExecutor for executing commands.
Definition: GUIItemShortcut.java:87
com.realtime.crossfire.jxclient.shortcuts.ShortcutListener
Interface for listeners for Shortcut changes.
Definition: ShortcutListener.java:31
com.realtime.crossfire.jxclient.gui.misc.Modifiers.MASK
static final int MASK
The mask for all used modifiers.
Definition: Modifiers.java:56
com.realtime.crossfire.jxclient.gui.item.GUIItemShortcut.getMinimumSizeInt
static Dimension getMinimumSizeInt()
Returns the minimal size to display this component.
Definition: GUIItemShortcut.java:351
com.realtime.crossfire.jxclient.shortcuts.Shortcut.toggleImmediate
void toggleImmediate()
Toggles between immediate execution and activation only.
Definition: Shortcut.java:158
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement.setChanged
void setChanged()
Records that the contents have changed and must be repainted.
Definition: AbstractGUIElement.java:223
com.realtime.crossfire.jxclient.gui.item.GUIItemShortcut.dispose
void dispose()
Releases all allocated resources.
Definition: GUIItemShortcut.java:215
com.realtime.crossfire.jxclient.gui.textinput
Definition: ActivateCommandInputCommand.java:23
com.realtime.crossfire.jxclient.faces.FacesManagerListener
Interface for listeners interested in FacesManager events.
Definition: FacesManagerListener.java:32
com.realtime.crossfire.jxclient.shortcuts.ShortcutSlot.toString
String toString()
Definition: ShortcutSlot.java:94
com.realtime.crossfire.jxclient.gui.item.GUIItemShortcut.shortcuts
final Shortcuts shortcuts
The Shortcuts instance for looking up Shortcuts.
Definition: GUIItemShortcut.java:75
com.realtime.crossfire.jxclient.gui.item.GUIItem
A GUIElement representing an in-game object.
Definition: GUIItem.java:39
com.realtime.crossfire.jxclient.faces
Manages image information ("faces") needed to display the map view, items, and spell icons.
Definition: AbstractFaceQueue.java:23
com.realtime.crossfire.jxclient.gui.item.GUIItemShortcut.button1Clicked
void button1Clicked(final int modifiers)
Called when the left mouse button was pressed.
Definition: GUIItemShortcut.java:268
com.realtime.crossfire.jxclient.gui.item.GUIItemShortcut.activateImage
final Image activateImage
The overlay image for shortcuts that activate the command.
Definition: GUIItemShortcut.java:99
com.realtime.crossfire.jxclient.gui.item.GUIItemShortcut.notifyOpen
void notifyOpen()
Called each time the enclosing dialog is opened (or raised).
Definition: GUIItemShortcut.java:241
com.realtime.crossfire.jxclient.gui.item.GUIItemShortcut.getMinimumSize
Dimension getMinimumSize()
Definition: GUIItemShortcut.java:342
com.realtime.crossfire.jxclient.shortcuts.Shortcuts.removeShortcutsListener
void removeShortcutsListener(@NotNull final ShortcutsListener listener)
Removes a ShortcutsListener.
Definition: Shortcuts.java:188
com.realtime.crossfire.jxclient.spells.Spell
Describes a Crossfire spell.
Definition: Spell.java:37
com.realtime.crossfire.jxclient.gui.item.GUIItemShortcut.shortcutSlot
final ShortcutSlot shortcutSlot
The shortcut slot.
Definition: GUIItemShortcut.java:123
com.realtime.crossfire.jxclient.shortcuts.Shortcut.getTooltipText
String getTooltipText()
Returns the current tooltip text.
Definition: Shortcut.java:129
com.realtime.crossfire.jxclient.gui.item.GUIItemShortcut.button2Clicked
void button2Clicked(final int modifiers)
Called when the middle mouse button was pressed.
Definition: GUIItemShortcut.java:286
com.realtime.crossfire.jxclient.gui.misc.Modifiers
Helper functions for keyboard modifiers.
Definition: Modifiers.java:31
com.realtime.crossfire.jxclient.gui.item.GUIItemShortcut.button3Clicked
void button3Clicked(final int modifiers)
Called when the right mouse button was pressed.
Definition: GUIItemShortcut.java:299
com.realtime.crossfire.jxclient.gui.item.GUIItemShortcut.scroll
void scroll(final int distance)
Scrolls the element.
Definition: GUIItemShortcut.java:361
com.realtime.crossfire.jxclient.gui.item.GUIItemShortcut.immediateColor
final Color immediateColor
The background color for shortcuts that immediately execute the command.
Definition: GUIItemShortcut.java:105
com.realtime.crossfire.jxclient.shortcuts.Shortcut.addShortcutListener
void addShortcutListener(@NotNull final ShortcutListener listener)
Registers a shortcut listener.
Definition: Shortcut.java:103
com.realtime.crossfire.jxclient.gui.item.GUIItemShortcut.resetScroll
void resetScroll()
Resets the scroll index to the default value.
Definition: GUIItemShortcut.java:365
com.realtime.crossfire.jxclient.gui.item.GUIItemShortcut.immediateImage
final Image immediateImage
The overlay image for shortcuts that immediately execute the command.
Definition: GUIItemShortcut.java:111
com.realtime.crossfire.jxclient.gui
com.realtime.crossfire.jxclient.shortcuts.Shortcut.getSpell
Spell getSpell()
Returns the associated spell to cast.
Definition: Shortcut.java:86
com.realtime.crossfire.jxclient.gui.item.GUIItemShortcut.DEFAULT_TOOLTIP_TEXT
static final String DEFAULT_TOOLTIP_TEXT
The default tooltip text for empty slots.
Definition: GUIItemShortcut.java:68
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement.guiFactory
final GuiFactory guiFactory
The global GuiFactory instance.
Definition: AbstractGUIElement.java:48
com.realtime.crossfire.jxclient.shortcuts.Shortcut
Abstract base class for shortcut commands.
Definition: Shortcut.java:35
com.realtime.crossfire.jxclient.gui.item.GUIItemShortcut.activateColor
final Color activateColor
The background color for shortcuts that activate the command.
Definition: GUIItemShortcut.java:93
com.realtime.crossfire.jxclient.gui.textinput.CommandExecutor
Executes commands.
Definition: CommandExecutor.java:31
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement.newTooltipText
TooltipText newTooltipText(@Nullable final String tooltipText)
Creates a TooltipText instance relative to this instance.
Definition: AbstractGUIElement.java:247
com.realtime.crossfire.jxclient.gui.gui.ActivatableGUIElement.elementListener
final GUIElementListener elementListener
The GUIElementListener to notify.
Definition: ActivatableGUIElement.java:44
com.realtime.crossfire.jxclient.gui.item.GUIItemShortcut.paintComponent
void paintComponent(@NotNull final Graphics g)
Definition: GUIItemShortcut.java:303
com.realtime.crossfire.jxclient.gui.item.GUIItemShortcut.facesManagerListener
final FacesManagerListener facesManagerListener
The FacesManagerListener registered to detect updated faces.
Definition: GUIItemShortcut.java:172
com.realtime.crossfire.jxclient.shortcuts.ShortcutsListener
Interface for listeners for Shortcut changes.
Definition: ShortcutsListener.java:32
com.realtime.crossfire.jxclient.gui.misc.Modifiers.CTRL
static final int CTRL
The mask for "ctrl".
Definition: Modifiers.java:46
com.realtime.crossfire.jxclient.gui.textinput.CommandExecutor.executeCommand
void executeCommand(@NotNull final CharSequence commandLine)
Executes a command or a list of commands.
com.realtime.crossfire.jxclient.gui.gui.TooltipText
Information for displaying tooltips.
Definition: TooltipText.java:31
com.realtime.crossfire.jxclient.shortcuts
Definition: Shortcut.java:23
com.realtime.crossfire.jxclient.gui.item.GUIItemShortcut.getTooltip
TooltipText getTooltip()
Returns the current tooltip text.
Definition: GUIItemShortcut.java:235
com.realtime.crossfire.jxclient.skin.skin.GuiFactory
Factory for creating Gui instances.
Definition: GuiFactory.java:41
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement.name
final String name
The name of this element.
Definition: AbstractGUIElement.java:77
com.realtime.crossfire.jxclient.gui.misc.Modifiers.NONE
static final int NONE
The mask for "no modifier".
Definition: Modifiers.java:36
com.realtime.crossfire.jxclient.gui.gui
Definition: AbstractGUIElement.java:23
com.realtime.crossfire.jxclient.gui.item.GUIItemShortcut.facesManager
final FacesManager facesManager
The FacesManager instance for looking up faces.
Definition: GUIItemShortcut.java:81
com.realtime.crossfire
com.realtime.crossfire.jxclient.gui.gui.TooltipManager
Manages the tooltip display.
Definition: TooltipManager.java:33
com.realtime.crossfire.jxclient.gui.item.GUIItemShortcut.font
final Font font
The Font for displaying the key that activates the shortcut.
Definition: GUIItemShortcut.java:117
com.realtime
com
com.realtime.crossfire.jxclient.gui.item.GUIItemShortcut.getPreferredSize
Dimension getPreferredSize()
Definition: GUIItemShortcut.java:335
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement.tooltipManager
final TooltipManager tooltipManager
The TooltipManager to update.
Definition: AbstractGUIElement.java:83
com.realtime.crossfire.jxclient.gui.item.GUIItemShortcut.BACKGROUND_COLOR
static final Color BACKGROUND_COLOR
The background color of this item.
Definition: GUIItemShortcut.java:62
com.realtime.crossfire.jxclient.gui.item.GUIItemShortcut.shortcutsListener
final ShortcutsListener shortcutsListener
The ShortcutsListener attached to shortcuts.
Definition: GUIItemShortcut.java:142
com.realtime.crossfire.jxclient.shortcuts.Shortcuts
Manages a list of Shortcuts.
Definition: Shortcuts.java:43
com.realtime.crossfire.jxclient.shortcuts.Shortcut.displaysFace
boolean displaysFace(@NotNull final Face face)
Returns whether this shortcut displays the given face.
Definition: Shortcut.java:141
com.realtime.crossfire.jxclient.gui.misc
Definition: GUICheckBox.java:23
com.realtime.crossfire.jxclient.shortcuts.Shortcut.isImmediate
boolean isImmediate()
Returns whether the command should be executed immediately.
Definition: Shortcut.java:150
com.realtime.crossfire.jxclient.gui.item.GUIItemShortcut.sync
final Object sync
The synchronization object for accesses to shortcut.
Definition: GUIItemShortcut.java:129
com.realtime.crossfire.jxclient.gui.item.GUIItemShortcut
Displays a shortcut command.
Definition: GUIItemShortcut.java:51
com.realtime.crossfire.jxclient.gui.item.GUIItemShortcut.shortcutListener
final ShortcutListener shortcutListener
The ShortcutListener attached to shortcut.
Definition: GUIItemShortcut.java:163
com.realtime.crossfire.jxclient.gui.gui.GUIElementListener
Listener for GUIElement related events.
Definition: GUIElementListener.java:32
com.realtime.crossfire.jxclient.spells
Definition: Spell.java:23