Crossfire JXClient, Trunk
GUITextButton.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.button;
24 
37 import java.awt.Color;
38 import java.awt.Dimension;
39 import java.awt.Font;
40 import java.awt.FontMetrics;
41 import java.awt.Graphics;
42 import java.awt.Graphics2D;
43 import java.awt.font.FontRenderContext;
44 import java.awt.geom.RectangularShape;
45 import java.util.Iterator;
46 import org.jetbrains.annotations.NotNull;
47 import org.jetbrains.annotations.Nullable;
48 
57 public class GUITextButton extends AbstractButton implements GUISelectable {
58 
62  private static final long serialVersionUID = 1;
63 
67  @NotNull
68  private final ButtonImages up;
69 
73  @NotNull
74  private final ButtonImages down;
75 
79  @NotNull
80  private final String text;
81 
85  @NotNull
86  private final Font font;
87 
91  @NotNull
92  private final Color color;
93 
97  @NotNull
98  private final Color colorSelected;
99 
104  @NotNull
105  private final Color colorDisabled;
106 
110  @Nullable
111  private final String expectedCommand;
112 
116  @NotNull
118 
122  @NotNull
123  private final Dimension preferredSize;
124 
128  private boolean selected;
129 
150  public GUITextButton(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, @NotNull final ButtonImages up, @NotNull final ButtonImages down, @NotNull final String text, @NotNull final Font font, @NotNull final Color color, @NotNull final Color colorSelected, @NotNull final Color colorDisabled, final boolean autoRepeat, @NotNull final CommandList commandList, @NotNull final GuiFactory guiFactory, @NotNull final NewCharModel newCharModel, @NotNull final KeybindingsManager keybindingsManager) {
152  this.colorSelected = colorSelected;
153  this.colorDisabled = colorDisabled;
154  final GUICommand command = commandList.getSingleCommand();
155  expectedCommand = command instanceof DialogToggleCommand ? "exec command_"+((DialogToggleCommand)command).getDialog() : null;
156  this.keybindingsManager = keybindingsManager;
157  final int preferredHeight = up.getHeight();
158  if (preferredHeight != down.getHeight()) {
159  throw new IllegalArgumentException("'up' state height is "+preferredHeight+" but 'down' state height is "+down.getHeight());
160  }
161  this.up = up;
162  this.down = down;
163  this.text = text;
164  this.font = font;
165  this.color = color;
166  preferredSize = GuiUtils.getTextDimension(text, getFontMetrics(font));
167  if (preferredSize.height < preferredHeight) {
168  preferredSize.height = preferredHeight;
169  }
170  preferredSize.width += 12;
171  }
172 
173  @Override
174  public void activeChanged() {
175  setChanged();
176  }
177 
178  @Override
179  public void paintComponent(@NotNull final Graphics g) {
180  super.paintComponent(g);
181  final Graphics2D g2 = (Graphics2D)g;
182  g2.setFont(font);
183  g2.setColor(isEnabled() ? selected ? colorSelected : color : colorDisabled);
184  final int width = getWidth();
185  (isActive() ? down : up).render(g2, width);
186  final FontRenderContext fontRenderContext = g2.getFontRenderContext();
187  final RectangularShape rectangle = font.getStringBounds(text, fontRenderContext);
188  final int x = (int)Math.round((width-rectangle.getWidth())/2);
189  final FontMetrics fontMetrics = g2.getFontMetrics();
190  final int y = (int)Math.round(preferredSize.height-rectangle.getHeight())/2+fontMetrics.getAscent();
191  g2.drawString(text, x, y);
192  }
193 
194  @NotNull
195  @Override
196  protected Dimension getMinimumSizeInt() {
197  return new Dimension(preferredSize);
198  }
199 
200  @Nullable
201  @Override
202  @SuppressWarnings("MethodDoesntCallSuperMethod")
203  public Dimension getMaximumSize() {
204  return new Dimension(Integer.MAX_VALUE, preferredSize.height);
205  }
206 
207  @Override
208  public void select(final boolean selected) {
209  if (this.selected == selected) {
210  return;
211  }
212 
213  this.selected = selected;
214  setChanged();
215  }
216 
217  @Override
218  public void notifyOpen() {
219  }
220 
221  @Nullable
222  @Override
224  if (expectedCommand == null) {
225  return null;
226  }
227 
228  final Iterator<KeyBinding> bindings = keybindingsManager.getBindings(keyBinding -> {
229  final GUICommand keyBindingCommand = keyBinding.getCommands().getSingleCommand();
230  return keyBindingCommand instanceof ExecuteCommandCommand && ((ExecuteCommandCommand)keyBindingCommand).getCommandString().equals(expectedCommand);
231  }).keySet().iterator();
232  return bindings.hasNext() ? newTooltipText(bindings.next().getBindingDescription()) : null;
233  }
234 
235 }
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement.newTooltipText
TooltipText newTooltipText(@Nullable final String tooltipText)
Definition: AbstractGUIElement.java:247
com.realtime.crossfire.jxclient.gui.button.GUITextButton.text
final String text
Definition: GUITextButton.java:80
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.button.GUITextButton.color
final Color color
Definition: GUITextButton.java:92
com.realtime.crossfire.jxclient.gui.button.GUISelectable
Definition: GUISelectable.java:31
com.realtime.crossfire.jxclient.gui.commandlist.CommandList
Definition: CommandList.java:34
com.realtime.crossfire.jxclient.skin
com.realtime.crossfire.jxclient.gui.textinput.ExecuteCommandCommand
Definition: ExecuteCommandCommand.java:34
com.realtime.crossfire.jxclient.gui.textinput.ExecuteCommandCommand.getCommandString
String getCommandString()
Definition: ExecuteCommandCommand.java:78
com.realtime.crossfire.jxclient.gui.button.GUITextButton.getMaximumSize
Dimension getMaximumSize()
Definition: GUITextButton.java:203
com.realtime.crossfire.jxclient.gui.label
Definition: AbstractLabel.java:23
com.realtime.crossfire.jxclient.skin.skin.GuiFactory
Definition: GuiFactory.java:41
com.realtime.crossfire.jxclient.gui.gui.GuiUtils.getTextDimension
static Dimension getTextDimension(@NotNull final String text, @NotNull final FontMetrics fontMetrics)
Definition: GuiUtils.java:50
com.realtime.crossfire.jxclient.gui.textinput
Definition: ActivateCommandInputCommand.java:23
com.realtime.crossfire.jxclient.gui.commandlist
Definition: CommandList.java:23
com.realtime.crossfire.jxclient.gui.gui.GuiUtils
Definition: GuiUtils.java:34
com.realtime.crossfire.jxclient.gui.keybindings
Definition: InvalidKeyBindingException.java:23
com.realtime.crossfire.jxclient.gui.button.ButtonImages
Definition: ButtonImages.java:35
com.realtime.crossfire.jxclient.gui.button.GUITextButton.paintComponent
void paintComponent(@NotNull final Graphics g)
Definition: GUITextButton.java:179
com.realtime.crossfire.jxclient.gui.button.AbstractButton.newCharModel
final NewCharModel newCharModel
Definition: AbstractButton.java:79
com.realtime.crossfire.jxclient.gui.button.AbstractButton.commandList
final CommandList commandList
Definition: AbstractButton.java:73
com.realtime.crossfire.jxclient.gui.button.ButtonImages.getHeight
int getHeight()
Definition: ButtonImages.java:84
com.realtime.crossfire.jxclient.gui.button.GUITextButton.selected
boolean selected
Definition: GUITextButton.java:128
com.realtime.crossfire.jxclient.gui.button.GUITextButton.getMinimumSizeInt
Dimension getMinimumSizeInt()
Definition: GUITextButton.java:196
com.realtime.crossfire.jxclient.gui.commandlist.CommandList.getSingleCommand
GUICommand getSingleCommand()
Definition: CommandList.java:150
com.realtime.crossfire.jxclient.gui
com.realtime.crossfire.jxclient.gui.gui.ActivatableGUIElement.elementListener
final GUIElementListener elementListener
Definition: ActivatableGUIElement.java:44
com.realtime.crossfire.jxclient.gui.commands.DialogToggleCommand
Definition: DialogToggleCommand.java:33
com.realtime.crossfire.jxclient.gui.keybindings.KeyBinding
Definition: KeyBinding.java:37
com.realtime.crossfire.jxclient.gui.button.GUITextButton.activeChanged
void activeChanged()
Definition: GUITextButton.java:174
com.realtime.crossfire.jxclient.gui.gui.TooltipManager
Definition: TooltipManager.java:33
com.realtime.crossfire.jxclient.gui.button.GUITextButton.select
void select(final boolean selected)
Definition: GUITextButton.java:208
com.realtime.crossfire.jxclient.gui.button.GUITextButton.down
final ButtonImages down
Definition: GUITextButton.java:74
com.realtime.crossfire.jxclient.gui.button.GUITextButton.keybindingsManager
final KeybindingsManager keybindingsManager
Definition: GUITextButton.java:117
com.realtime.crossfire.jxclient.gui.label.NewCharModel
Definition: NewCharModel.java:43
com.realtime.crossfire.jxclient.gui.commands.DialogToggleCommand.getDialog
String getDialog()
Definition: DialogToggleCommand.java:62
com.realtime.crossfire.jxclient.gui.gui
Definition: AbstractGUIElement.java:23
com.realtime.crossfire.jxclient.gui.button.GUITextButton.colorSelected
final Color colorSelected
Definition: GUITextButton.java:98
com.realtime.crossfire.jxclient.gui.button.GUITextButton.GUITextButton
GUITextButton(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, @NotNull final ButtonImages up, @NotNull final ButtonImages down, @NotNull final String text, @NotNull final Font font, @NotNull final Color color, @NotNull final Color colorSelected, @NotNull final Color colorDisabled, final boolean autoRepeat, @NotNull final CommandList commandList, @NotNull final GuiFactory guiFactory, @NotNull final NewCharModel newCharModel, @NotNull final KeybindingsManager keybindingsManager)
Definition: GUITextButton.java:150
com.realtime.crossfire.jxclient.gui.button.GUITextButton.serialVersionUID
static final long serialVersionUID
Definition: GUITextButton.java:62
com.realtime.crossfire.jxclient.gui.gui.TooltipText
Definition: TooltipText.java:31
com.realtime.crossfire
com.realtime
com.realtime.crossfire.jxclient.gui.keybindings.KeybindingsManager.getBindings
Map< KeyBinding, String > getBindings(@NotNull final Predicate< KeyBinding > predicate)
Definition: KeybindingsManager.java:287
com
com.realtime.crossfire.jxclient.gui.button.GUITextButton.up
final ButtonImages up
Definition: GUITextButton.java:68
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement.setChanged
void setChanged()
Definition: AbstractGUIElement.java:223
com.realtime.crossfire.jxclient.gui.button.GUITextButton.notifyOpen
void notifyOpen()
Definition: GUITextButton.java:218
com.realtime.crossfire.jxclient.gui.button.GUITextButton.font
final Font font
Definition: GUITextButton.java:86
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement.guiFactory
final GuiFactory guiFactory
Definition: AbstractGUIElement.java:48
com.realtime.crossfire.jxclient.gui.commands
Definition: AccountCreateCharacterCommand.java:23
com.realtime.crossfire.jxclient.gui.button.AbstractButton
Definition: AbstractButton.java:47
com.realtime.crossfire.jxclient.gui.button.GUITextButton
Definition: GUITextButton.java:57
com.realtime.crossfire.jxclient.gui.button.GUITextButton.preferredSize
final Dimension preferredSize
Definition: GUITextButton.java:123
com.realtime.crossfire.jxclient.gui.button.GUITextButton.colorDisabled
final Color colorDisabled
Definition: GUITextButton.java:105
com.realtime.crossfire.jxclient.gui.button.AbstractButton.autoRepeat
final boolean autoRepeat
Definition: AbstractButton.java:67
com.realtime.crossfire.jxclient.gui.button.GUITextButton.getTooltip
TooltipText getTooltip()
Definition: GUITextButton.java:223
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.gui.button.GUITextButton.expectedCommand
final String expectedCommand
Definition: GUITextButton.java:111
com.realtime.crossfire.jxclient.gui.gui.ActivatableGUIElement.isActive
boolean isActive()
Definition: ActivatableGUIElement.java:124
com.realtime.crossfire.jxclient.gui.commandlist.GUICommand
Definition: GUICommand.java:29
com.realtime.crossfire.jxclient.gui.keybindings.KeybindingsManager
Definition: KeybindingsManager.java:40