Crossfire JXClient, Trunk  R20561
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-2011 Andreas Kirschbaum.
20  */
21 
22 package com.realtime.crossfire.jxclient.gui.item;
23 
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  @Nullable
87  private final Color castColor;
88 
92  @Nullable
93  private final Image castImage;
94 
98  @Nullable
99  private final Color invokeColor;
100 
104  @Nullable
105  private final Image invokeImage;
106 
110  @NotNull
111  private final Font font;
112 
116  private final int index;
117 
121  @NotNull
123 
128  @Nullable
130 
134  @NotNull
136 
137  @Override
138  public void shortcutAdded(final int index, @NotNull final Shortcut shortcut) {
139  if (index == GUIItemShortcut.this.index) {
140  setShortcut(shortcut);
141  }
142  }
143 
144  @Override
145  public void shortcutRemoved(final int index, @NotNull final Shortcut shortcut) {
146  if (index == GUIItemShortcut.this.index) {
147  setShortcut(null);
148  }
149  }
150  };
151 
155  @NotNull
156  private final ShortcutListener shortcutListener = () -> {
157  setChanged();
159  };
160 
164  @NotNull
166 
167  @Override
168  public void faceUpdated(@NotNull final Face face) {
169  if (shortcut != null && shortcut.displaysFace(face)) {
170  setChanged();
171  }
172  }
173  };
174 
192  public GUIItemShortcut(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, @Nullable final Color castColor, @Nullable final Image castImage, @Nullable final Color invokeColor, @Nullable final Image invokeImage, final int index, @NotNull final FacesManager facesManager, @NotNull final Shortcuts shortcuts, @NotNull final Font font, @NotNull final CurrentSpellManager currentSpellManager) {
193  super(tooltipManager, elementListener, name);
194  this.shortcuts = shortcuts;
195  this.facesManager = facesManager;
196  this.castColor = castColor;
197  this.castImage = castImage;
198  this.invokeColor = invokeColor;
199  this.invokeImage = invokeImage;
200  this.font = font;
201  this.index = index;
202  this.currentSpellManager = currentSpellManager;
203  this.shortcuts.addShortcutsListener(shortcutsListener);
204  this.facesManager.addFacesManagerListener(facesManagerListener);
206  }
207 
211  @Override
212  public void dispose() {
213  super.dispose();
214  facesManager.removeFacesManagerListener(facesManagerListener);
215  shortcuts.removeShortcutsListener(shortcutsListener);
216  setShortcut(null);
217  }
218 
224  private void setShortcut(@Nullable final Shortcut shortcut) {
225  if (this.shortcut == shortcut) {
226  return;
227  }
228 
229  if (this.shortcut != null) {
230  this.shortcut.removeShortcutListener(shortcutListener);
231  }
232  this.shortcut = shortcut;
233  if (this.shortcut != null) {
234  this.shortcut.addShortcutListener(shortcutListener);
235  }
236  setChanged();
237 
239  }
240 
244  @Override
245  public void button1Clicked(final int modifiers) {
246  switch (modifiers&Modifiers.MASK) {
247  case Modifiers.NONE:
248  if (shortcut != null) {
249  shortcut.execute();
250  }
251  break;
252 
253  case Modifiers.CTRL:
254  if (shortcut instanceof ShortcutSpell) {
255  final ShortcutSpell shortcutSpell = (ShortcutSpell)shortcut;
256  shortcutSpell.setCast(!shortcutSpell.isCast());
257  }
258  break;
259  }
260  }
261 
265  @Override
266  public void button2Clicked(final int modifiers) {
267  switch (modifiers&Modifiers.MASK) {
268  case Modifiers.NONE:
269  if (shortcut instanceof ShortcutSpell) {
270  final ShortcutSpell shortcutSpell = (ShortcutSpell)shortcut;
271  shortcutSpell.setCast(!shortcutSpell.isCast());
272  }
273  break;
274  }
275  }
276 
280  @Override
281  public void button3Clicked(final int modifiers) {
282  switch (modifiers&Modifiers.MASK) {
283  case Modifiers.NONE:
284  final Spell spell = currentSpellManager.getCurrentSpell();
285  if (spell == null) {
286  return;
287  }
288 
289  shortcuts.setSpellShortcut(index, spell, true);
290  break;
291  }
292  }
293 
297  @Override
298  public void paintComponent(@NotNull final Graphics g) {
299  super.paintComponent(g);
300 
301  g.setColor(BACKGROUND_COLOR);
302  g.fillRect(0, 0, getWidth(), getHeight());
303 
304  final Shortcut tmpShortcut = shortcut;
305  if (tmpShortcut == null) {
306  return;
307  }
308 
309  final ShortcutVisitor visitor = new ShortcutVisitor() {
310 
311  @Override
312  public void visit(@NotNull final ShortcutCommand shortcutCommand) {
313  // XXX: todo
314  }
315 
316  @Override
317  public void visit(@NotNull final ShortcutSpell shortcutSpell) {
318  final Color color = shortcutSpell.isCast() ? castColor : invokeColor;
319  if (color != null) {
320  g.setColor(color);
321  g.fillRect(0, 0, getWidth(), getHeight());
322  }
323  g.drawImage(facesManager.getOriginalImageIcon(shortcutSpell.getSpell().getFaceNum(), null).getImage(), 0, 0, null);
324  final Image image = shortcutSpell.isCast() ? castImage : invokeImage;
325  if (image != null) {
326  g.drawImage(image, 0, 0, null);
327  }
328  }
329 
330  };
331  tmpShortcut.visit(visitor);
332  g.setFont(font);
333  g.setColor(Color.YELLOW);
334  g.drawString("F"+(index+1), 1, 1+font.getSize()); // XXX: define in skin
335  }
336 
340  @NotNull
341  @Override
342  public Dimension getPreferredSize() {
343  return getMinimumSizeInt();
344  }
345 
349  @NotNull
350  @Override
351  public Dimension getMinimumSize() {
352  return getMinimumSizeInt();
353  }
354 
359  @NotNull
360  private static Dimension getMinimumSizeInt() {
361  return new Dimension(32, 32);
362  }
363 
367  @Override
368  public boolean canScroll(final int distance) {
369  return false;
370  }
371 
375  @Override
376  public void scroll(final int distance) {
377  }
378 
382  @Override
383  public void resetScroll() {
384  }
385 
389  private void updateTooltipText() {
390  setTooltipText(shortcut == null ? DEFAULT_TOOLTIP_TEXT : shortcut.getTooltipText());
391  }
392 
393 }
ImageIcon getOriginalImageIcon(int faceNum, @Nullable boolean[] isUnknownImage)
Returns the "original" face for a face ID.
boolean canScroll(final int distance)
Returns whether scrolling is possible.the distance to scroll whether scrolling is possible ...
final TooltipManager tooltipManager
The TooltipManager to update.
final ShortcutsListener shortcutsListener
The ShortcutsListener attached to shortcuts.
void setCast(final boolean cast)
Sets whether the spell should be "cast" or "invoked".
final GUIElementListener elementListener
The GUIElementListener to notify.
void setChanged()
Records that the contents have changed and must be repainted.
final Font font
The Font for displaying the key that activates the shortcut.
abstract void visit(@NotNull final ShortcutVisitor visitor)
Calls a ShortcutVisitor's.
final CurrentSpellManager currentSpellManager
The CurrentSpellManager for tracking the active spell.
final Image invokeImage
The overlay image for shortcuts that /invoke a spell.
Spell getCurrentSpell()
Returns the currently selected spell object.
GUIItemShortcut(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, @Nullable final Color castColor, @Nullable final Image castImage, @Nullable final Color invokeColor, @Nullable final Image invokeImage, final int index, @NotNull final FacesManager facesManager, @NotNull final Shortcuts shortcuts, @NotNull final Font font, @NotNull final CurrentSpellManager currentSpellManager)
Creates a new instance.
A GUIElement representing an in-game object.
Definition: GUIItem.java:38
final Color invokeColor
The background color for shortcuts that /invoke a spell.
abstract void execute()
Executes the shortcut.
Manages image information ("faces") needed to display the map view, items, and spell icons...
static final Color BACKGROUND_COLOR
The background color of this item.
final FacesManager facesManager
The FacesManager instance for looking up faces.
void setSpellShortcut(final int index, @NotNull final String spellName, final boolean cast)
Sets a Shortcut to a spell.
Definition: Shortcuts.java:164
void dispose()
Releases all allocated resources.
final FacesManagerListener facesManagerListener
The FacesManagerListener registered to detect updated faces.
abstract boolean displaysFace(final Face face)
Returns whether this shortcut displays the given face.
abstract String getTooltipText()
Returns the current tooltip text.
final ShortcutListener shortcutListener
The ShortcutListener attached to shortcut.
void setShortcut(@Nullable final Shortcut shortcut)
Updates shortcut and registers/de-registers shortcutListener.
Helper functions for keyboard modifiers.
Definition: Modifiers.java:30
Describes a Crossfire spell.
Definition: Spell.java:36
void resetScroll()
Resets the scroll index to the default value.
Interface for listeners interested in FacesManager events.
void setTooltipText(@Nullable final String tooltipText)
Sets the tooltip text to show when the mouse is inside this element.the text to show ornull to disab...
Interface for listeners for Shortcut changes.
static final int CTRL
The mask for "ctrl".
Definition: Modifiers.java:45
Interface for visitors of Shortcut instances.
final Shortcuts shortcuts
The Shortcuts instance for looking up Shortcuts.
Interface for listeners for Shortcut changes.
boolean isCast()
Returns whether the spell should be "cast" or "invoked".
final Image castImage
The overlay image for shortcuts that /cast a spell.
static final int NONE
The mask for "no modifier".
Definition: Modifiers.java:35
static final int MASK
The mask for all used modifiers.
Definition: Modifiers.java:55
Abstract base class for shortcut commands.
Definition: Shortcut.java:32
void updateTooltipText()
Updates the tooltip text to reflect current settings.
static final long serialVersionUID
The serial version UID.
A Shortcut that executes a Crossfire command.
static Dimension getMinimumSizeInt()
Returns the minimal size to display this component.
void scroll(final int distance)
Scrolls the element.the distance to scroll
void removeFacesManagerListener(@NotNull FacesManagerListener facesManagerListener)
Removes a FacesManagerListener to be notified about updated faces.
void addShortcutListener(@NotNull final ShortcutListener listener)
Registers a shortcut listener.
Definition: Shortcut.java:54
final Color castColor
The background color for shortcuts that /cast a spell.
void removeShortcutsListener(@NotNull final ShortcutsListener listener)
Removes a ShortcutsListener.
Definition: Shortcuts.java:225
static final String DEFAULT_TOOLTIP_TEXT
The default tooltip text for empty slots.
Shortcut shortcut
The currently monitored Shortcut instance.
Maintains a mapping of face numbers to face data.