tcr/java/src/client/audio/Volume.java

107 lines
2.1 KiB
Java
Raw Normal View History

2025-05-04 20:27:55 +02:00
package client.audio;
2025-03-11 00:23:54 +01:00
2025-05-04 20:27:55 +02:00
import client.Game;
import client.gui.element.Slider;
2025-03-11 00:23:54 +01:00
import game.color.TextColor;
2025-05-04 20:27:55 +02:00
import game.sound.EventType;
import game.vars.CVar;
import game.vars.CVarCategory;
2025-03-11 00:23:54 +01:00
public enum Volume implements CVar {
MASTER("master", "Gesamt"),
MUSIC("music", "Musik"),
2025-05-04 20:27:55 +02:00
SFX("sfx", "Geräusche", EventType.SOUND_EFFECT),
GUI("gui", "Oberfläche", EventType.UI_INTERFACE);
private static final Volume[] LOOKUP = new Volume[EventType.values().length];
2025-03-11 00:23:54 +01:00
public final String id;
public final String name;
2025-05-04 20:27:55 +02:00
public final EventType type;
2025-03-11 00:23:54 +01:00
private int value = 100;
2025-05-04 20:27:55 +02:00
private Volume(String id, String name, EventType type) {
2025-03-11 00:23:54 +01:00
this.id = id;
this.name = name;
2025-05-04 20:27:55 +02:00
this.type = type;
}
private Volume(String id, String name) {
this(id, name, null);
}
static {
for(Volume volume : values()) {
if(volume.type != null)
LOOKUP[volume.type.ordinal()] = volume;
}
}
public static Volume getByType(EventType type) {
return LOOKUP[type.ordinal()];
2025-03-11 00:23:54 +01:00
}
public String getDisplay() {
return this.name;
}
public String getCVarName() {
return "snd_volume_" + this.id;
}
public String getType() {
return TextColor.DGREEN + "volume";
}
public CVarCategory getCategory() {
return CVarCategory.SOUND;
}
public boolean parse(String str) {
int value;
try {
value = Integer.parseInt(str);
}
catch(NumberFormatException e) {
return false;
}
if(value < 0 || value > 100)
return false;
this.value = value;
this.apply();
return true;
}
public String format() {
return "" + this.value;
}
public String getDefault() {
return "100";
}
public void setDefault() {
this.value = 100;
this.apply();
}
public String getValues() {
return "0..100";
}
public void apply() {
if(Game.getGame().getAudioInterface() != null)
Game.getGame().getAudioInterface().alSetVolume(this, (short)(((float)this.value) / 100.0f * 32767.0f));
}
public Slider selector(int x, int y, int w, int h) {
return new Slider(x, y, w, h, 0, 0, 100, 100, this.value, new Slider.Callback() {
public void use(Slider elem, int value) {
Volume.this.value = value;
Volume.this.apply();
}
}, this.name, "%");
}
}