Crossfire JXClient, Trunk  R20561
GUIItemFloor.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 
35 import java.awt.Image;
36 import javax.swing.SwingUtilities;
37 import org.jetbrains.annotations.NotNull;
38 
43 public class GUIItemFloor extends GUIItemItem {
44 
48  private static final long serialVersionUID = 1;
49 
53  @NotNull
54  private final CommandQueue commandQueue;
55 
59  @NotNull
61 
65  @NotNull
66  private final ItemView floorView;
67 
71  @NotNull
72  private final ItemSet itemSet;
73 
77  @NotNull
78  private final FacesManager facesManager;
79 
83  @NotNull
84  private final Image nextGroupFace;
85 
89  @NotNull
90  private final Image prevGroupFace;
91 
95  private final int defaultIndex;
96 
101  private int index = -1;
102 
107  @NotNull
109 
110  @Override
111  public void locationChanged() {
112  SwingUtilities.invokeLater(locationListenerEdt);
113  }
114 
115  };
116 
120  @NotNull
121  private final Runnable locationListenerEdt = () -> setIndex(index, true);
122 
139  public GUIItemFloor(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final CommandQueue commandQueue, @NotNull final String name, @NotNull final ItemPainter itemPainter, final int index, @NotNull final CrossfireServerConnection crossfireServerConnection, @NotNull final ItemView floorView, @NotNull final ItemSet itemSet, @NotNull final FacesManager facesManager, @NotNull final Image nextGroupFace, @NotNull final Image prevGroupFace, final int size) {
140  super(tooltipManager, elementListener, name, itemPainter, facesManager);
141  this.commandQueue = commandQueue;
142  this.crossfireServerConnection = crossfireServerConnection;
143  this.floorView = floorView;
144  this.itemSet = itemSet;
145  this.facesManager = facesManager;
146  this.nextGroupFace = nextGroupFace;
147  this.prevGroupFace = prevGroupFace;
148  defaultIndex = index;
149  setIndex(index, false);
150  if (size != 0) {
151  setSize(size, size);
152  }
153  }
154 
158  @Override
159  public void dispose() {
160  super.dispose();
161  setIndex(-1, false);
162  }
163 
167  @Override
168  public boolean canScroll(final int distance) {
169  if (distance < 0) {
170  return index >= -distance;
171  }
172  //noinspection SimplifiableIfStatement
173  if (distance > 0) {
174  return index+distance < floorView.getSize();
175  }
176  return false;
177  }
178 
182  @Override
183  public void scroll(final int distance) {
184  setIndex(index+distance, false);
185  }
186 
190  @Override
191  public void resetScroll() {
192  setIndex(defaultIndex, false);
193  }
194 
198  @Override
199  public void button1Clicked(final int modifiers) {
200  final CfItem item = getItem();
201  if (item == null) {
202  return;
203  }
204 
205  switch (modifiers&Modifiers.MASK) {
206  case Modifiers.NONE:
207  if (item.isItemGroupButton()) {
208  crossfireServerConnection.sendApply(item.getTag());
209  } else {
210  crossfireServerConnection.sendExamine(item.getTag());
211  }
212  break;
213 
214  case Modifiers.CTRL:
215  crossfireServerConnection.sendApply(item.getTag());
216  break;
217  }
218  }
219 
223  @Override
224  public void button2Clicked(final int modifiers) {
225  final CfItem item = getItem();
226  if (item == null) {
227  return;
228  }
229 
230  switch (modifiers&Modifiers.MASK) {
231  case Modifiers.NONE:
232  crossfireServerConnection.sendApply(item.getTag());
233  break;
234  }
235  }
236 
240  @Override
241  public void button3Clicked(final int modifiers) {
242  final CfItem item = getItem();
243  if (item == null) {
244  return;
245  }
246 
247  final CfItem player = itemSet.getPlayer();
248  if (player == null) {
249  return;
250  }
251 
252  switch (modifiers&Modifiers.MASK) {
253  case Modifiers.NONE:
254  commandQueue.sendMove(player.getTag(), item.getTag());
255  break;
256 
257  case Modifiers.SHIFT:
258  crossfireServerConnection.sendApply(item.getTag());
259  break;
260  }
261  }
262 
266  @Override
267  public int getIndex() {
268  return index;
269  }
270 
277  private void setIndex(final int index, final boolean forced) {
278  if (this.index != index) {
279  if (this.index >= 0) {
280  floorView.removeLocationListener(this.index, locationListener);
281  }
282  this.index = index;
283  if (this.index >= 0) {
284  floorView.addLocationListener(this.index, locationListener);
285  }
286  } else if (!forced) {
287  return;
288  }
289 
290  setItem(floorView.getItem(index));
291  }
292 
296  @Override
297  public void setIndexNoListeners(final int index) {
298  this.index = index;
299 
300  setItemNoListeners(floorView.getItem(index));
301  }
302 
306  @NotNull
307  @Override
308  protected Image getFace(@NotNull final CfItem item) {
309  if (!item.isItemGroupButton()) {
310  return facesManager.getOriginalImageIcon(item.getFace().getFaceNum(), null).getImage();
311  }
312 
313  /*
314  * replace empty.111 with arrows for "Click here for next/previous
315  * group of items". When a container is opened, the first item
316  * will be the 'prev' arrow, so take that into account.
317  */
318  final int min = itemSet.getOpenContainer() == 0 ? 0 : 1;
319  return index > min ? nextGroupFace : prevGroupFace;
320  }
321 
325  @Override
326  public void setSelected(final boolean selected) {
327  // ignore: floor objects are never selected
328  }
329 
333  @Override
334  protected boolean isSelected() {
335  return false;
336  }
337 
338 }
final int defaultIndex
The default scroll index.
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 ...
void setIndex(final int index, final boolean forced)
Set the floor tile to display.
void sendMove(final int to, final int tag)
Sends a "move" command to the server.
final TooltipManager tooltipManager
The TooltipManager to update.
void resetScroll()
Resets the scroll index to the default value.
Interface for listeners interested in changed items.
final GUIElementListener elementListener
The GUIElementListener to notify.
A GUIElement representing an in-game object in the ground view.
static final int SHIFT
The mask for "shift".
Definition: Modifiers.java:40
final FacesManager facesManager
The FacesManager instance to use.
final LocationListener locationListener
The LocationListener used to detect items added to or removed from this floor tile.
void setItemNoListeners(@Nullable final CfItem item)
Sets the current item instance without registering listeners for updates.
Manages image information ("faces") needed to display the map view, items, and spell icons...
A GUIElement instance representing an in-game item.
Interface defining an abstract GUI element.
Definition: GUIElement.java:32
final ItemView floorView
The floor view to watch.
int getOpenContainer()
Returns the currently opened container.
Definition: ItemSet.java:436
Helper functions for keyboard modifiers.
Definition: Modifiers.java:30
final Runnable locationListenerEdt
Called from locationListener but runs on the EDT.
CfItem getItem()
Returns the current item instance.
boolean isItemGroupButton()
Returns whether this object is a fake object for selecting object groups in the ground view...
Definition: CfItem.java:564
void scroll(final int distance)
Scrolls the element.the distance to scroll
final Image prevGroupFace
The face to substitute into "Click here for previous group of items".
final Image nextGroupFace
The face to substitute into "Click here for next group of items".
final CommandQueue commandQueue
The command queue for sending commands.
int getSize()
Returns the number of items.
void addLocationListener(int index, @NotNull LocationListener locationListener)
Adds a LocationListener to be notified when the item displayed in a floor slot has changed...
static final int CTRL
The mask for "ctrl".
Definition: Modifiers.java:45
void removeLocationListener(int index, @NotNull LocationListener locationListener)
Removes a LocationListener to be notified when the item displayed in a floor slot has changed...
static final long serialVersionUID
The serial version UID.
final CrossfireServerConnection crossfireServerConnection
The connection instance.
CfItem getPlayer()
Returns the player object this client controls.
Definition: ItemSet.java:284
static final int NONE
The mask for "no modifier".
Definition: Modifiers.java:35
GUIItemFloor(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final CommandQueue commandQueue, @NotNull final String name, @NotNull final ItemPainter itemPainter, final int index, @NotNull final CrossfireServerConnection crossfireServerConnection, @NotNull final ItemView floorView, @NotNull final ItemSet itemSet, @NotNull final FacesManager facesManager, @NotNull final Image nextGroupFace, @NotNull final Image prevGroupFace, final int size)
Creates a new instance.
void setItem(@Nullable final CfItem item)
Sets the current item instance.
static final int MASK
The mask for all used modifiers.
Definition: Modifiers.java:55
final ItemPainter itemPainter
The ItemPainter for painting the icon.
void dispose()
Releases all allocated resources.
Adds encoding/decoding of crossfire protocol packets to a ServerConnection.
CfItem getItem(int index)
Returns the CfItem in a given slot.
Model class maintaining the CfItems known to the player.
Definition: ItemSet.java:43
Maintains the pending (ncom) commands sent to the server.
void sendApply(int tag)
Sends an "apply" command to the server.
void sendExamine(int tag)
Sends an "examine" command to the server.
The representation of a Crossfire Item, client-side.
Definition: CfItem.java:36
Maintains a mapping of face numbers to face data.