Crossfire JXClient, Trunk  R20561
GUILabelStats2.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.label;
23 
28 import java.awt.Color;
29 import java.awt.Font;
30 import org.jetbrains.annotations.NotNull;
31 import org.jetbrains.annotations.Nullable;
32 
39 public class GUILabelStats2 extends GUIOneLineLabel {
40 
44  private static final long serialVersionUID = 1;
45 
49  @NotNull
50  private final Color colorUpgradable;
51 
55  @NotNull
56  private final Color colorDepleted;
57 
61  @NotNull
62  private final Color colorBoosted;
63 
67  @NotNull
68  private final Color colorBoostedUpgradable;
69 
73  private final int statCurrent;
74 
78  private final int statBase;
79 
83  private final int statRace;
84 
88  private final int statApplied;
89 
93  @NotNull
94  private final Stats stats;
95 
99  @NotNull
100  private Color color;
101 
105  @NotNull
106  private final StatsListener statsListener = new StatsListener() {
107 
108  @Override
109  public void reset() {
110  // ignore
111  }
112 
113  @Override
114  public void statChanged(final int statNo, final int value) {
115  if (statNo == statCurrent || statNo == statBase || statNo == statRace || statNo == statApplied) {
116  updateStat();
117  }
118  }
119 
120  @Override
121  public void simpleWeaponSpeedChanged(final boolean simpleWeaponSpeed) {
122  // ignore
123  }
124 
125  @Override
126  public void titleChanged(@NotNull final String title) {
127  // ignore
128  }
129 
130  @Override
131  public void rangeChanged(@NotNull final String range) {
132  // ignore
133  }
134 
135  @Override
136  public void activeSkillChanged(@NotNull final String activeSkill) {
137  // ignore
138  }
139 
140  @Override
141  public void experienceChanged(final long exp) {
142  // ignore
143  }
144 
145  @Override
146  public void experienceNextLevelChanged(final long expNextLevel) {
147  // ignore
148  }
149 
150  };
151 
171  public GUILabelStats2(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, @NotNull final Font font, @NotNull final Color colorNormal, @NotNull final Color colorUpgradable, @NotNull final Color colorDepleted, @NotNull final Color colorBoosted, @NotNull final Color colorBoostedUpgradable, @Nullable final Color backgroundColor, final int statCurrent, final int statBase, final int statRace, final int statApplied, @NotNull final Alignment alignment, @NotNull final Stats stats) {
172  super(tooltipManager, elementListener, name, null, font, colorNormal, backgroundColor, alignment, "");
173  this.colorUpgradable = colorUpgradable;
174  this.colorDepleted = colorDepleted;
175  this.colorBoosted = colorBoosted;
176  this.colorBoostedUpgradable = colorBoostedUpgradable;
177  this.statCurrent = statCurrent;
178  this.statBase = statBase;
179  this.statRace = statRace;
180  this.statApplied = statApplied;
181  this.stats = stats;
182  this.stats.addCrossfireStatsListener(statsListener);
183  color = colorNormal;
184  updateStat();
185  }
186 
190  private void updateStat() {
191  final int baseValue = stats.getStat(statBase);
192  final int raceValue = stats.getStat(statRace);
193  final int appliedValue = stats.getStat(statApplied);
194  final int currValue = stats.getStat(statCurrent);
195  final int currValueWithoutGear = currValue-appliedValue;
196  if (baseValue == 0 && raceValue == 0) {
197  // no server support
198  color = GUILabelStats2.super.getTextColor();
199  setText(String.valueOf(currValue));
200  setTooltipText("Current: "+currValue);
201  return;
202  }
203 
204  final Color newColor;
205  if (currValueWithoutGear < baseValue) {
206  newColor = colorDepleted;
207  } else if (currValueWithoutGear == baseValue) {
208  newColor = baseValue < raceValue ? colorUpgradable : GUILabelStats2.super.getTextColor();
209  } else {
210  newColor = baseValue < raceValue ? colorBoostedUpgradable : colorBoosted;
211  }
212  if (color != newColor) {
213  color = newColor;
214  setChanged();
215  }
216  setText(String.valueOf(currValue));
217 
218  final StringBuilder sb = new StringBuilder();
219  sb.append("<html>Current: ").append(currValue);
220  if (currValueWithoutGear < baseValue) {
221  sb.append("<br>Depleted by ").append(baseValue-currValueWithoutGear).append(" from ").append(baseValue).append(".");
222  } else if (currValueWithoutGear > baseValue) {
223  sb.append("<br>Increased by ").append(currValueWithoutGear-baseValue).append(" from ").append(baseValue).append(".");
224  }
225  if (appliedValue > 0) {
226  sb.append("<br>Boosted by ").append(appliedValue).append(" by gear or skills.");
227  } else if (appliedValue < 0) {
228  sb.append("<br>Reduced by ").append(-appliedValue).append(" by gear or skills.");
229  }
230  if (baseValue < raceValue) {
231  sb.append("<br>Upgradable to ").append(raceValue).append(" by drinking stat potions.");
232  }
233  setTooltipText(sb.toString());
234  }
235 
239  @Override
240  public void dispose() {
241  super.dispose();
242  stats.removeCrossfireStatsListener(statsListener);
243  }
244 
248  @NotNull
249  @Override
250  protected Color getTextColor() {
251  return color;
252  }
253 
254 }
final int statBase
The base stat without applied boosts or depletions.
A AbstractLabel that renders the text as a plain string.
final Color colorDepleted
The color for depleted stats.
void setText(@NotNull final String text)
The label text.
final StatsListener statsListener
The StatsListener registered to be notified about stat changes.
final int statApplied
The stat change due to gear or skills.
final TooltipManager tooltipManager
The TooltipManager to update.
final Color colorBoosted
The color for boosted stats.
void setChanged()
Records that the contents have changed and must be repainted.
void removeCrossfireStatsListener(@NotNull final StatsListener statsListener)
Removes a StatsListener to be notified about stat changes.
Definition: Stats.java:778
final GUIElementListener elementListener
The GUIElementListener to notify.
GUILabelStats2(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, @NotNull final Font font, @NotNull final Color colorNormal, @NotNull final Color colorUpgradable, @NotNull final Color colorDepleted, @NotNull final Color colorBoosted, @NotNull final Color colorBoostedUpgradable, @Nullable final Color backgroundColor, final int statCurrent, final int statBase, final int statRace, final int statApplied, @NotNull final Alignment alignment, @NotNull final Stats stats)
Creates a new instance.
void updateStat()
Updates the values to reflect the current stat value.
final Color backgroundColor
If set, the opaque background color.
void addCrossfireStatsListener(@NotNull final StatsListener statsListener)
Adds a StatsListener to be notified about stat changes.
Definition: Stats.java:770
static final long serialVersionUID
The serial version UID.
A GUILabel that displays a value of the last received "stats" command.
final Color colorUpgradable
The color for upgradable stats.
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 interested in changes of Stats instances.
void dispose()
Releases all allocated resources.
final int statRace
The race&#39;s maximum stat.
final Stats stats
The Stats instance to use.
This is the representation of all the statistics of a player, like its speed or its experience...
Definition: Stats.java:43
final Color colorBoostedUpgradable
The color for boosted and upgradable stats.
int getStat(final int statNo)
Returns the numerical value of the given statistic.
Definition: Stats.java:615