package com.ts3client.ui; import com.formdev.flatlaf.util.UIScale; import com.ts3client.gfx.IconKeys; import javax.swing.ImageIcon; import java.awt.BasicStroke; import java.awt.Color; import java.awt.Graphics2D; import java.awt.Image; import java.awt.RenderingHints; import java.awt.image.BufferedImage; import java.util.List; /** * The user interface's icons, taken from the active {@link IconTheme} pack and * named by TeamSpeak's icon keys. * *
The icons the client cannot do without are also drawn programmatically here, * so a machine with no icon pack installed still gets a complete (if plain) set. * Everything else — menu and dialog decoration — simply stays blank without a pack, * which is why {@link #of(String)} may return {@code null}. * *
Icons come at the interface's scale; the variants taking a size take it in pixels.
*/
public final class Icons {
/** The box the built-in painters draw in. */
private static final int SZ = IconTheme.SIZE;
private Icons() {
}
private static int small() {
return UIScale.scale(IconTheme.SIZE);
}
private static int large() {
return UIScale.scale(IconTheme.TOOLBAR_SIZE);
}
private interface Painter {
void paint(Graphics2D g);
}
/** @return the pack's icon for a TeamSpeak icon key, or {@code null} when it has none */
public static ImageIcon of(String key) {
return IconTheme.icon(key);
}
/**
* @return the pack's icon for the first key it has, or {@code null} when it has none
* of them — packs differ in which variants they ship
*/
public static ImageIcon ofAny(String... keys) {
for (String key : keys) {
ImageIcon icon = IconTheme.icon(key);
if (icon != null) return icon;
}
return null;
}
/** @return the pack's icon at {@code size} pixels, or {@code null} when it has none */
public static ImageIcon of(String key, int size) {
return IconTheme.icon(key, size);
}
private static ImageIcon themed(String key, Painter fallback) {
return themed(key, small(), fallback);
}
private static ImageIcon themed(String key, int size, Painter fallback) {
ImageIcon icon = IconTheme.icon(key, size);
return icon != null ? icon : make(size, fallback);
}
/** Like {@link #themed(String, Painter)}, but tries the keys in turn. */
private static ImageIcon themed(String[] keys, Painter fallback) {
for (String key : keys) {
ImageIcon icon = IconTheme.icon(key, small());
if (icon != null) return icon;
}
return make(fallback);
}
private static ImageIcon make(Painter p) {
return make(small(), p);
}
/** Runs a painter (which draws in a 16×16 box) scaled to {@code size}. */
private static ImageIcon make(int size, Painter p) {
BufferedImage img = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = img.createGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
g.scale(size / (double) SZ, size / (double) SZ);
p.paint(g);
g.dispose();
return new ImageIcon(img);
}
// ---- tree icons ----
public static ImageIcon server() {
return themed("SERVER_GREEN", Icons::paintServer);
}
public static ImageIcon channel(boolean subscribed) {
return themed(IconKeys.channel(false, false, subscribed),
g -> paintChannel(g, new Color(0x3E7CB1), false, subscribed));
}
/** A channel nobody can join without its password. */
public static ImageIcon channelLocked(boolean subscribed) {
return themed(IconKeys.channel(true, false, subscribed),
g -> paintChannel(g, new Color(0x8A6D3B), true, subscribed));
}
/** A channel that has reached its client limit. */
public static ImageIcon channelFull(boolean subscribed) {
return themed(IconKeys.channel(false, true, subscribed),
g -> paintChannel(g, new Color(0xA53F3F), false, subscribed));
}
/** The chat tab for the current channel. */
public static ImageIcon channelChat() {
ImageIcon icon = of("CHANNEL_CHAT");
return icon != null ? icon : channel(true);
}
/** The chat tab of a private conversation. */
public static ImageIcon privateChat() {
ImageIcon icon = ofAny("PLAYER_CHAT", "NEW_CHAT");
return icon != null ? icon : clientIdle();
}
// ---- client status icons ----
public static ImageIcon clientIdle() {
return clientIdle(small());
}
public static ImageIcon clientIdle(int size) {
return themed("PLAYER_OFF", size, g -> paintPerson(g, Theme.idleClient()));
}
public static ImageIcon clientTalking() {
return clientTalking(small());
}
public static ImageIcon clientTalking(int size) {
return themed("PLAYER_ON", size, g -> paintPerson(g, Theme.talking()));
}
public static ImageIcon clientAway() {
return clientAway(small());
}
public static ImageIcon clientAway(int size) {
return themed("AWAY", size, g -> paintPerson(g, Theme.away()));
}
/** A channel commander that is not talking. */
public static ImageIcon clientCommander() {
return clientCommander(small());
}
/** A channel commander that is talking. */
public static ImageIcon clientCommanderTalking() {
return clientCommanderTalking(small());
}
public static ImageIcon clientQuery() {
return themed("SERVER_QUERY", g -> paintPerson(g, Theme.idleClient()));
}
public static ImageIcon micMuted() {
return micMuted(small());
}
public static ImageIcon micMuted(int size) {
return themed("INPUT_MUTED", size, Icons::paintMicMuted);
}
public static ImageIcon speakerMuted() {
return speakerMuted(small());
}
public static ImageIcon speakerMuted(int size) {
return themed("OUTPUT_MUTED", size, Icons::paintSpeakerMuted);
}
/** The capture device is unavailable (another tab holds it), as opposed to muted. */
public static ImageIcon micDisabled() {
return micDisabled(small());
}
public static ImageIcon micDisabled(int size) {
return themed("HARDWARE_INPUT_MUTED", size, Icons::paintMicDisabled);
}
/** The playback device is unavailable, as opposed to muted/deafened. */
public static ImageIcon speakerDisabled() {
return speakerDisabled(small());
}
public static ImageIcon speakerDisabled(int size) {
return themed("HARDWARE_OUTPUT_MUTED", size, Icons::paintSpeakerDisabled);
}
/** TS3's "Local Mic Mute": silenced, but not reported as muted. */
public static ImageIcon micLocalMuted() {
return micLocalMuted(small());
}
public static ImageIcon micLocalMuted(int size) {
return themed("INPUT_MUTED_LOCAL", size, Icons::paintMicLocalMuted);
}
/** A channel commander that is not talking. */
public static ImageIcon clientCommander(int size) {
return themed("PLAYER_COMMANDER_OFF", size, g -> paintPerson(g, Theme.idleClient()));
}
/** A channel commander that is talking. */
public static ImageIcon clientCommanderTalking(int size) {
return themed("PLAYER_COMMANDER_ON", size, g -> paintPerson(g, Theme.talking()));
}
// ---- toolbar / action icons ----
public static ImageIcon connect() {
return themed("CONNECT", large(), Icons::paintConnect);
}
public static ImageIcon disconnect() {
return themed("DISCONNECT", large(), Icons::paintDisconnect);
}
public static ImageIcon mic() {
return themed("CAPTURE", large(), Icons::paintMic);
}
public static ImageIcon speaker() {
return themed("PLAYBACK", large(), Icons::paintSpeaker);
}
public static ImageIcon micMutedLarge() {
return themed("INPUT_MUTED", large(), Icons::paintMicMuted);
}
public static ImageIcon speakerMutedLarge() {
return themed("OUTPUT_MUTED", large(), Icons::paintSpeakerMuted);
}
/** Marks the server that currently owns the microphone. */
public static ImageIcon micActive() {
return themed("ACTIVATE_MICROPHONE", large(), Icons::paintMicActive);
}
/** The same marker at tab-label size. */
public static ImageIcon micActiveSmall() {
return themed("ACTIVATE_MICROPHONE", Icons::paintMicActive);
}
/**
* A fixed-width "channel icon / edit icon" label icon, used where a tab must not
* change width as the object it shows changes (e.g. the info-panel-in-chat tab).
*/
public static ImageIcon channelClientPair() {
ImageIcon channel = channel(true);
ImageIcon edit = themed("EDIT", Icons::paintEdit);
int gap = UIScale.scale(8);
int width = channel.getIconWidth() + gap + edit.getIconWidth();
int height = Math.max(channel.getIconHeight(), edit.getIconHeight());
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = img.createGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.drawImage(channel.getImage(), 0, (height - channel.getIconHeight()) / 2, null);
g.setColor(Theme.idleClient());
g.setFont(Theme.uiFont());
java.awt.FontMetrics fm = g.getFontMetrics();
int slashX = channel.getIconWidth() + (gap - fm.stringWidth("/")) / 2;
g.drawString("/", slashX, (height + fm.getAscent()) / 2 - 1);
g.drawImage(edit.getImage(), channel.getIconWidth() + gap,
(height - edit.getIconHeight()) / 2, null);
g.dispose();
return new ImageIcon(img);
}
/** The toolbar's away marker. */
public static ImageIcon away() {
return themed("AWAY", large(), g -> paintPerson(g, Theme.away()));
}
public static ImageIcon settings() {
return themed("SETTINGS", large(), Icons::paintSettings);
}
public static ImageIcon app() {
return app(small());
}
public static ImageIcon app(int size) {
return make(size, Icons::paintApp);
}
/** The window icon at the sizes task bars and switchers pick from. */
public static List