Crossfire JXClient, Trunk
GUIGauge.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.gauge;
24 
32 import java.awt.AlphaComposite;
33 import java.awt.Dimension;
34 import java.awt.Graphics;
35 import java.awt.Graphics2D;
36 import java.awt.Image;
37 import java.awt.event.MouseEvent;
38 import org.jetbrains.annotations.NotNull;
39 import org.jetbrains.annotations.Nullable;
40 
47 public class GUIGauge extends AbstractGUIElement implements GUIGaugeListener {
48 
52  private static final long serialVersionUID = 1;
53 
58  @Nullable
59  private final String tooltipFormat;
60 
64  @Nullable
65  private final CommandList commandList;
66 
70  @NotNull
71  private final Stats stats;
72 
76  @NotNull
77  private String tooltipText = "-"; // make sure the following setValues() does not short-cut
78 
82  @Nullable
83  private final Image emptyImage;
84 
88  @NotNull
89  private final Orientation orientation;
90 
94  @NotNull
95  private final GaugeState gaugeState;
96 
100  private final float alpha;
101 
105  private boolean hidden;
106 
126  public GUIGauge(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, @Nullable final Image fullImage, @Nullable final Image lowImage, @Nullable final Image negativeImage, @Nullable final Image emptyImage, @NotNull final Orientation orientation, @Nullable final String tooltipFormat, final float alpha, @Nullable final CommandList commandList, @NotNull final GuiFactory guiFactory, @NotNull final Stats stats) {
128  this.emptyImage = emptyImage;
129  this.orientation = orientation;
130  this.tooltipFormat = tooltipFormat;
131  this.commandList = commandList;
132  this.stats = stats;
133  gaugeState = new GaugeState(fullImage, lowImage, negativeImage, 0, 0);
134  this.alpha = alpha;
135  orientation.setExtends(getWidth(), getHeight());
136  orientation.setHasNegativeImage(negativeImage != null);
137  orientation.setValues(0, 0, 0);
139  }
140 
141  @Override
142  public void setBounds(final int x, final int y, final int width, final int height) {
143  super.setBounds(x, y, width, height);
144  if (orientation.setExtends(width, height)) {
146  setChanged();
147  }
148  }
149  }
150 
151  @Override
152  public void paintComponent(@NotNull final Graphics g) {
153  if (hidden) {
154  return;
155  }
156 
157  final Graphics paint;
158  if (alpha < 1.0F) {
159  final Graphics2D g2d = (Graphics2D)g.create();
160  g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
161  paint = g2d;
162  } else {
163  paint = g;
164  }
165 
166  super.paintComponent(paint);
167  if (emptyImage != null) {
168  paint.drawImage(emptyImage, 0, 0, null);
169  }
170  gaugeState.draw(paint);
171  if (paint != g) {
172  paint.dispose();
173  }
174  }
175 
176  @Override
177  public void setValues(final int curValue, final int minValue, final int maxValue, @NotNull final String labelText, @NotNull final String tooltipText) {
178  if (!orientation.setValues(curValue, minValue, maxValue) && this.tooltipText.equals(tooltipText)) {
179  return;
180  }
181 
182  this.tooltipText = tooltipText;
183 
185  setChanged();
186  }
187 
188  tooltipChanged();
189  }
190 
191  @Nullable
192  @Override
193  @SuppressWarnings("MethodDoesntCallSuperMethod")
194  public Dimension getPreferredSize() {
195  return gaugeState.getPreferredSize();
196  }
197 
198  @Nullable
199  @Override
200  @SuppressWarnings("MethodDoesntCallSuperMethod")
201  public Dimension getMinimumSize() {
202  return gaugeState.getPreferredSize();
203  }
204 
205  @Nullable
206  @Override
207  @SuppressWarnings("MethodDoesntCallSuperMethod")
208  public Dimension getMaximumSize() {
209  return gaugeState.getPreferredSize();
210  }
211 
212  @Override
213  public void setHidden(final boolean hidden) {
214  this.hidden = hidden;
215  }
216 
217  @Override
218  public void mouseClicked(@NotNull final MouseEvent e) {
219  super.mouseClicked(e);
220 
221  if (!isEnabled()) {
222  return;
223  }
224 
225  switch (e.getButton()) {
226  case MouseEvent.BUTTON1:
227  break;
228 
229  case MouseEvent.BUTTON2:
230  if (commandList != null) {
232  }
233  break;
234 
235  case MouseEvent.BUTTON3:
236  break;
237  }
238  }
239 
240  @Nullable
241  @Override
243  return newTooltipText(formatTooltip());
244  }
245 
246  @Override
247  public void notifyOpen() {
248  }
249 
260  @Nullable
261  private String formatTooltip() {
262  if (tooltipFormat == null) {
263  return null;
264  }
265  final StringBuilder sb = new StringBuilder();
266  final char[] formatChars = tooltipFormat.toCharArray();
267  int i = 0;
268  while (i < formatChars.length) {
269  final char ch = formatChars[i++];
270  if (ch != '%' || i >= formatChars.length) {
271  sb.append(ch);
272  } else {
273  switch (formatChars[i++]) {
274  case '%':
275  sb.append('%');
276  break;
277 
278  case 'S':
279  sb.append(stats.getActiveSkill());
280  break;
281 
282  case 'T':
283  sb.append(tooltipText);
284  break;
285 
286  default:
287  sb.append('%');
288  sb.append(formatChars[i-1]);
289  break;
290  }
291  }
292  }
293  return sb.toString();
294  }
295 
296 }
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement.newTooltipText
TooltipText newTooltipText(@Nullable final String tooltipText)
Definition: AbstractGUIElement.java:247
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement.name
final String name
Definition: AbstractGUIElement.java:77
com.realtime.crossfire.jxclient
com.realtime.crossfire.jxclient.skin.skin
Definition: DefaultJXCSkin.java:23
com.realtime.crossfire.jxclient.gui.gauge.GUIGauge.commandList
final CommandList commandList
Definition: GUIGauge.java:65
com.realtime.crossfire.jxclient.gui.gauge.GUIGauge.getPreferredSize
Dimension getPreferredSize()
Definition: GUIGauge.java:194
com.realtime.crossfire.jxclient.gui.gauge.Orientation.setExtends
boolean setExtends(int width, int height)
com.realtime.crossfire.jxclient.gui.commandlist.CommandList
Definition: CommandList.java:34
com.realtime.crossfire.jxclient.skin
com.realtime.crossfire.jxclient.gui.gauge.GUIGauge.paintComponent
void paintComponent(@NotNull final Graphics g)
Definition: GUIGauge.java:152
com.realtime.crossfire.jxclient.gui.gauge.GUIGauge.stats
final Stats stats
Definition: GUIGauge.java:71
com.realtime.crossfire.jxclient.gui.gauge.GUIGauge.serialVersionUID
static final long serialVersionUID
Definition: GUIGauge.java:52
com.realtime.crossfire.jxclient.skin.skin.GuiFactory
Definition: GuiFactory.java:41
com.realtime.crossfire.jxclient.gui.commandlist
Definition: CommandList.java:23
com.realtime.crossfire.jxclient.gui.gauge.GUIGauge
Definition: GUIGauge.java:47
com.realtime.crossfire.jxclient.gui.gauge.GaugeState.draw
void draw(@NotNull final Graphics g)
Definition: GaugeState.java:162
com.realtime.crossfire.jxclient.stats.Stats
Definition: Stats.java:44
com.realtime.crossfire.jxclient.gui.gauge.GUIGauge.tooltipFormat
final String tooltipFormat
Definition: GUIGauge.java:59
com.realtime.crossfire.jxclient.gui.gauge.GUIGauge.setHidden
void setHidden(final boolean hidden)
Definition: GUIGauge.java:213
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement.tooltipChanged
void tooltipChanged()
Definition: AbstractGUIElement.java:265
com.realtime.crossfire.jxclient.gui.gauge.Orientation.setHasNegativeImage
void setHasNegativeImage(boolean hasNegativeImage)
com.realtime.crossfire.jxclient.gui.gauge.GUIGauge.mouseClicked
void mouseClicked(@NotNull final MouseEvent e)
Definition: GUIGauge.java:218
com.realtime.crossfire.jxclient.gui.gauge.GUIGauge.tooltipText
String tooltipText
Definition: GUIGauge.java:77
com.realtime.crossfire.jxclient.gui.gauge.GaugeState.getPreferredSize
Dimension getPreferredSize()
Definition: GaugeState.java:173
com.realtime.crossfire.jxclient.gui.gauge.GUIGauge.notifyOpen
void notifyOpen()
Definition: GUIGauge.java:247
com.realtime.crossfire.jxclient.gui.gauge.GaugeState
Definition: GaugeState.java:35
com.realtime.crossfire.jxclient.gui.gauge.GUIGauge.emptyImage
final Image emptyImage
Definition: GUIGauge.java:83
com.realtime.crossfire.jxclient.stats.Stats.getActiveSkill
String getActiveSkill()
Definition: Stats.java:846
com.realtime.crossfire.jxclient.gui.gauge.GaugeState.setValues
boolean setValues(@NotNull final Orientation orientation)
Definition: GaugeState.java:130
com.realtime.crossfire.jxclient.gui.gauge.GUIGauge.GUIGauge
GUIGauge(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, @Nullable final Image fullImage, @Nullable final Image lowImage, @Nullable final Image negativeImage, @Nullable final Image emptyImage, @NotNull final Orientation orientation, @Nullable final String tooltipFormat, final float alpha, @Nullable final CommandList commandList, @NotNull final GuiFactory guiFactory, @NotNull final Stats stats)
Definition: GUIGauge.java:126
com.realtime.crossfire.jxclient.gui
com.realtime.crossfire.jxclient.gui.gauge.GUIGauge.formatTooltip
String formatTooltip()
Definition: GUIGauge.java:261
com.realtime.crossfire.jxclient.gui.gauge.GUIGauge.gaugeState
final GaugeState gaugeState
Definition: GUIGauge.java:95
com.realtime.crossfire.jxclient.gui.gauge.GUIGauge.alpha
final float alpha
Definition: GUIGauge.java:100
com.realtime.crossfire.jxclient.gui.commandlist.CommandList.execute
void execute()
Definition: CommandList.java:99
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement.elementListener
final GUIElementListener elementListener
Definition: AbstractGUIElement.java:89
com.realtime.crossfire.jxclient.gui.gui.TooltipManager
Definition: TooltipManager.java:33
com.realtime.crossfire.jxclient.gui.gauge.GUIGauge.getMaximumSize
Dimension getMaximumSize()
Definition: GUIGauge.java:208
com.realtime.crossfire.jxclient.gui.gauge.GUIGauge.hidden
boolean hidden
Definition: GUIGauge.java:105
com.realtime.crossfire.jxclient.gui.gauge.GUIGauge.orientation
final Orientation orientation
Definition: GUIGauge.java:89
com.realtime.crossfire.jxclient.gui.gui
Definition: AbstractGUIElement.java:23
com.realtime.crossfire.jxclient.gui.gauge.Orientation.setValues
boolean setValues(int cur, int min, int max)
com.realtime.crossfire.jxclient.gui.gui.TooltipText
Definition: TooltipText.java:31
com.realtime.crossfire
com.realtime.crossfire.jxclient.gui.gauge.GUIGauge.getMinimumSize
Dimension getMinimumSize()
Definition: GUIGauge.java:201
com.realtime
com.realtime.crossfire.jxclient.gui.gauge.GUIGauge.setValues
void setValues(final int curValue, final int minValue, final int maxValue, @NotNull final String labelText, @NotNull final String tooltipText)
Definition: GUIGauge.java:177
com
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement.setChanged
void setChanged()
Definition: AbstractGUIElement.java:223
com.realtime.crossfire.jxclient.gui.gauge.GUIGauge.setBounds
void setBounds(final int x, final int y, final int width, final int height)
Definition: GUIGauge.java:142
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement
Definition: AbstractGUIElement.java:37
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement.guiFactory
final GuiFactory guiFactory
Definition: AbstractGUIElement.java:48
com.realtime.crossfire.jxclient.gui.gauge.Orientation
Definition: Orientation.java:29
com.realtime.crossfire.jxclient.gui.gauge.GUIGaugeListener
Definition: GUIGaugeListener.java:31
com.realtime.crossfire.jxclient.gui.gui.GUIElementListener
Definition: GUIElementListener.java:32
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement.tooltipManager
final TooltipManager tooltipManager
Definition: AbstractGUIElement.java:83
com.realtime.crossfire.jxclient.stats
Definition: ActiveSkillWatcher.java:23
com.realtime.crossfire.jxclient.gui.gauge.GUIGauge.getTooltip
TooltipText getTooltip()
Definition: GUIGauge.java:242