00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 package com.realtime.crossfire.jxclient.sound;
00023
00024 import com.realtime.crossfire.jxclient.guistate.ClientSocketState;
00025 import com.realtime.crossfire.jxclient.guistate.GuiStateListener;
00026 import com.realtime.crossfire.jxclient.guistate.GuiStateManager;
00027 import com.realtime.crossfire.jxclient.util.DebugWriter;
00028 import java.util.Collection;
00029 import java.util.EnumSet;
00030 import org.jetbrains.annotations.NotNull;
00031 import org.jetbrains.annotations.Nullable;
00032
00039 public class SoundManager {
00040
00044 @NotNull
00045 private final ClipManager clipManager;
00046
00051 @Nullable
00052 private final DebugWriter debugSound;
00053
00057 @NotNull
00058 private final MusicManager musicManager;
00059
00063 private boolean enabled;
00064
00068 @NotNull
00069 private final Collection<Sounds> mutedSounds = EnumSet.allOf(Sounds.class);
00070
00075 @NotNull
00076 private final GuiStateListener guiStateListener = new GuiStateListener() {
00077
00078 @Override
00079 public void start() {
00080 muteMusic(true);
00081 mute(Sounds.CHARACTER, true);
00082 }
00083
00084 @Override
00085 public void metaserver() {
00086 muteMusic(true);
00087 mute(Sounds.CHARACTER, true);
00088 }
00089
00090 @Override
00091 public void preConnecting(@NotNull final String serverInfo) {
00092
00093 }
00094
00095 @Override
00096 public void connecting(@NotNull final String serverInfo) {
00097 muteMusic(true);
00098 mute(Sounds.CHARACTER, true);
00099 }
00100
00101 @Override
00102 public void connecting(@NotNull final ClientSocketState clientSocketState) {
00103
00104 }
00105
00106 @Override
00107 public void connected() {
00108 muteMusic(false);
00109 mute(Sounds.CHARACTER, false);
00110 }
00111
00112 @Override
00113 public void connectFailed(@NotNull final String reason) {
00114
00115 }
00116
00117 };
00118
00125 public SoundManager(@NotNull final GuiStateManager guiStateManager, @Nullable final DebugWriter debugSound) {
00126 final AudioFileLoader audioFileLoader = new AudioFileLoader(debugSound);
00127 clipManager = new ClipManager(audioFileLoader, debugSound);
00128 musicManager = new MusicManager(audioFileLoader, debugSound);
00129 this.debugSound = debugSound;
00130 guiStateManager.addGuiStateListener(guiStateListener);
00131 }
00132
00137 public void setEnabled(final boolean enabled) {
00138 if (this.enabled == enabled) {
00139 return;
00140 }
00141
00142 this.enabled = enabled;
00143 musicManager.setEnabled(enabled);
00144 }
00145
00152 public void playClip(@NotNull final Sounds type, @Nullable final String name, @NotNull final String action) {
00153 if (!enabled) {
00154 if (debugSound != null) {
00155 debugSound.debugProtocolWrite("playClip(type="+type+", name="+name+", action="+action+"): sound is muted");
00156 }
00157 return;
00158 }
00159
00160 if (mutedSounds.contains(type)) {
00161 if (debugSound != null) {
00162 debugSound.debugProtocolWrite("playClip(type="+type+", name="+name+", action="+action+"): sound type is muted");
00163 }
00164 return;
00165 }
00166
00167 if (debugSound != null) {
00168 debugSound.debugProtocolWrite("playClip(type="+type+", name="+name+", action="+action+")");
00169 }
00170 clipManager.play(name, action);
00171 }
00172
00179 private void mute(@NotNull final Sounds type, final boolean mute) {
00180 if (mute) {
00181 mutedSounds.add(type);
00182 } else {
00183 mutedSounds.remove(type);
00184
00185 }
00186 }
00187
00193 public void playMusic(@Nullable final String name) {
00194 musicManager.play(name);
00195 }
00196
00202 private void muteMusic(final boolean muted) {
00203 musicManager.setMuted(muted);
00204 }
00205
00209 public void shutdown() {
00210 if (debugSound != null) {
00211 debugSound.debugProtocolWrite("shutdown");
00212 }
00213 musicManager.shutdown();
00214 clipManager.shutdown();
00215 }
00216
00217 }