Crossfire JXClient, Trunk  R20561
GUILabelStats.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 
29 import java.awt.Color;
30 import java.awt.Font;
31 import org.jetbrains.annotations.NotNull;
32 import org.jetbrains.annotations.Nullable;
33 
40 public class GUILabelStats extends GUIOneLineLabel {
41 
45  private static final long serialVersionUID = 1;
46 
50  @NotNull
51  private final Stats stats;
52 
56  private final int stat;
57 
61  @NotNull
62  private final StatsListener statsListener = new StatsListener() {
63 
64  @Override
65  public void reset() {
66  // ignore
67  }
68 
69  @Override
70  public void statChanged(final int statNo, final int value) {
71  switch (stat) {
72  case Stats.CS_STAT_SPEED:
73  if (stat == statNo) {
74  setText(Formatter.formatFloat(stats.getFloatStat(stat), 2));
75  }
76  break;
77 
79  if (stat == statNo) {
81  }
82  break;
83 
85  case Stats.C_STAT_WEIGHT:
86  if (stat == statNo) {
87  //noinspection IntegerDivisionInFloatingPointContext
88  setText(Formatter.formatFloat(((value+50)/100)/10.0, 1));
89  setTooltipText(Formatter.formatFloat(value/1000.0, 3)+"kg");
90  }
91  break;
92 
93  default:
94  if (stat == statNo) {
95  setText(String.valueOf(value));
96  }
97  break;
98  }
99  }
100 
101  @Override
102  public void simpleWeaponSpeedChanged(final boolean simpleWeaponSpeed) {
103  // ignore
104  }
105 
106  @Override
107  public void titleChanged(@NotNull final String title) {
108  if (stat == Stats.CS_STAT_TITLE) {
109  setText(title);
110  }
111  }
112 
113  @Override
114  @SuppressWarnings("IfMayBeConditional")
115  public void rangeChanged(@NotNull final String range) {
116  if (stat != Stats.CS_STAT_RANGE) {
117  return;
118  }
119 
120  final String text;
121  if (range.startsWith("Range: spell ")) {
122  text = range.substring(13);
123  } else if (range.startsWith("Range: ") || range.startsWith("Skill: ")) {
124  text = range.substring(7);
125  } else {
126  text = range;
127  }
128  setText(text);
129  }
130 
131  @Override
132  public void activeSkillChanged(@NotNull final String activeSkill) {
133  // ignore
134  }
135 
136  @Override
137  public void experienceChanged(final long exp) {
138  if (stat == Stats.CS_STAT_EXP || stat == Stats.CS_STAT_EXP64) {
139  setText(String.valueOf(exp));
140  }
141  }
142 
143  @Override
144  public void experienceNextLevelChanged(final long expNextLevel) {
145  if (stat == Stats.C_STAT_EXP_NEXT_LEVEL) {
146  setText(String.valueOf(expNextLevel));
147  }
148  }
149 
150  };
151 
164  public GUILabelStats(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, @NotNull final Font font, @NotNull final Color color, @Nullable final Color backgroundColor, final int stat, @NotNull final Alignment alignment, @NotNull final Stats stats) {
165  super(tooltipManager, elementListener, name, null, font, color, backgroundColor, alignment, "");
166  this.stats = stats;
167  this.stat = stat;
168  this.stats.addCrossfireStatsListener(statsListener);
169  }
170 
174  @Override
175  public void dispose() {
176  super.dispose();
177  stats.removeCrossfireStatsListener(statsListener);
178  }
179 
180 }
A AbstractLabel that renders the text as a plain string.
void setText(@NotNull final String text)
The label text.
final TooltipManager tooltipManager
The TooltipManager to update.
static final int CS_STAT_TITLE
The Title stat.
Definition: Stats.java:159
static final int CS_STAT_RANGE
The Range stat - this is what is currently readied by the player to fire.
Definition: Stats.java:154
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.
final Color backgroundColor
If set, the opaque background color.
static final int CS_STAT_WEIGHT_LIM
The Weight Limit stat.
Definition: Stats.java:184
double getWeaponSpeed()
Returns the weapon speed stat.
Definition: Stats.java:786
final Stats stats
The Stats instance to use.
Utility class for formatting values into strings.
Definition: Formatter.java:30
static final int C_STAT_EXP_NEXT_LEVEL
The global experience needed to reach next level stat.
Definition: Stats.java:103
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...
A GUILabel that displays a value of the last received "stats" command.
Interface for listeners interested in changes of Stats instances.
double getFloatStat(final int statNo)
Returns the numerical value of the given statistic.
Definition: Stats.java:624
static final int CS_STAT_EXP64
The Global Experience (64bit encoding) stat.
Definition: Stats.java:194
static final int CS_STAT_EXP
The Global Experience (32bit encoding) stat.
Definition: Stats.java:98
static final int C_STAT_WEIGHT
The character's weight.
Definition: Stats.java:189
GUILabelStats(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, @NotNull final Font font, @NotNull final Color color, @Nullable final Color backgroundColor, final int stat, @NotNull final Alignment alignment, @NotNull final Stats stats)
Creates a new instance.
final StatsListener statsListener
The StatsListener registered to be notified about stat changes.
static String formatFloat(final double value, final int digits)
Formats a float value for display.
Definition: Formatter.java:76
This is the representation of all the statistics of a player, like its speed or its experience...
Definition: Stats.java:43
static final int CS_STAT_WEAP_SP
The Weapon Speed stat.
Definition: Stats.java:148
static final long serialVersionUID
The serial version UID.
static final int CS_STAT_SPEED
The Speed stat.
Definition: Stats.java:133
void dispose()
Releases all allocated resources.