Backend changes for new music player
This commit is contained in:
@@ -13,38 +13,16 @@ using OpenRA.FileFormats;
|
||||
namespace OpenRA.GameRules
|
||||
{
|
||||
public class MusicInfo
|
||||
{
|
||||
public readonly MusicPool Pool;
|
||||
public readonly string[] Music = { };
|
||||
{
|
||||
public readonly string Filename = null;
|
||||
public readonly string Title = null;
|
||||
public readonly float Length = 0; // seconds
|
||||
|
||||
public MusicInfo( MiniYaml y )
|
||||
public MusicInfo( string key, MiniYaml value )
|
||||
{
|
||||
FieldLoader.Load(this, y);
|
||||
Pool = new MusicPool(Music);
|
||||
FieldLoader.Load(this, value);
|
||||
if (Filename == null)
|
||||
Filename = key+".aud";
|
||||
}
|
||||
}
|
||||
|
||||
public class MusicPool
|
||||
{
|
||||
readonly string[] clips;
|
||||
int playing = 0;
|
||||
|
||||
public MusicPool(params string[] clips)
|
||||
{
|
||||
this.clips = clips;
|
||||
}
|
||||
|
||||
public string GetNext()
|
||||
{
|
||||
playing = (playing + 1) % clips.Length;
|
||||
return clips[playing];
|
||||
}
|
||||
public string GetPrev()
|
||||
{
|
||||
playing = (playing + clips.Length - 1) % clips.Length;
|
||||
return clips[playing];
|
||||
}
|
||||
public string GetCurrent(){ return clips[playing];}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ namespace OpenRA
|
||||
Info = LoadYamlRules(m.Rules, map.Rules, (k, y) => new ActorInfo(k.Key.ToLowerInvariant(), k.Value, y));
|
||||
Weapons = LoadYamlRules(m.Weapons, map.Weapons, (k, _) => new WeaponInfo(k.Key.ToLowerInvariant(), k.Value));
|
||||
Voices = LoadYamlRules(m.Voices, map.Voices, (k, _) => new VoiceInfo(k.Value));
|
||||
Music = LoadYamlRules(m.Music, map.Music, (k, _) => new MusicInfo(k.Value));
|
||||
Music = LoadYamlRules(m.Music, map.Music, (k, _) => new MusicInfo(k.Key, k.Value));
|
||||
Movies = LoadYamlRules(m.Movies, new Dictionary<string,MiniYaml>(), (k, v) => k.Value.Value);
|
||||
|
||||
TileSets = new Dictionary<string, TileSet>();
|
||||
|
||||
@@ -21,11 +21,10 @@ namespace OpenRA
|
||||
{
|
||||
static ISoundEngine soundEngine;
|
||||
static Cache<string, ISoundSource> sounds;
|
||||
static ISoundSource rawSource;
|
||||
static ISound music;
|
||||
static ISound video;
|
||||
|
||||
static bool paused;
|
||||
static bool stopped;
|
||||
static string currentMusic;
|
||||
|
||||
static ISoundSource LoadSound(string filename)
|
||||
{
|
||||
@@ -43,26 +42,12 @@ namespace OpenRA
|
||||
soundEngine = new OpenAlSoundEngine();
|
||||
sounds = new Cache<string, ISoundSource>(LoadSound);
|
||||
music = null;
|
||||
currentMusic = null;
|
||||
video = null;
|
||||
paused = false;
|
||||
stopped = false;
|
||||
}
|
||||
|
||||
public static void SetListenerPosition(float2 position) { soundEngine.SetListenerPosition(position); }
|
||||
|
||||
static ISoundSource rawSource;
|
||||
public static void PlayVideoSoundtrack(byte[] raw)
|
||||
{
|
||||
rawSource = LoadSoundRaw(raw);
|
||||
video = soundEngine.Play2D(rawSource, false, true, float2.Zero, SoundVolume);
|
||||
}
|
||||
|
||||
public static void StopVideoSoundtrack()
|
||||
{
|
||||
if (video != null)
|
||||
soundEngine.StopSound(video);
|
||||
}
|
||||
|
||||
public static void Play(string name)
|
||||
{
|
||||
if (name == "" || name == null)
|
||||
@@ -93,36 +78,47 @@ namespace OpenRA
|
||||
Play(name, pos);
|
||||
}
|
||||
|
||||
public static void PlayVideoSoundtrack(byte[] raw)
|
||||
{
|
||||
rawSource = LoadSoundRaw(raw);
|
||||
video = soundEngine.Play2D(rawSource, false, true, float2.Zero, SoundVolume);
|
||||
}
|
||||
|
||||
public static void StopVideoSoundtrack()
|
||||
{
|
||||
if (video != null)
|
||||
soundEngine.StopSound(video);
|
||||
}
|
||||
|
||||
public static void PlayMusic(string name)
|
||||
{
|
||||
if (name == "" || name == null)
|
||||
return;
|
||||
|
||||
if (music != null)
|
||||
soundEngine.StopSound(music);
|
||||
if (name == currentMusic && music != null)
|
||||
{
|
||||
soundEngine.PauseSound(music, false);
|
||||
return;
|
||||
}
|
||||
StopMusic();
|
||||
|
||||
currentMusic = name;
|
||||
var sound = sounds[name];
|
||||
music = soundEngine.Play2D(sound, true, true, float2.Zero, MusicVolume);
|
||||
}
|
||||
|
||||
public static bool MusicPaused
|
||||
public static void StopMusic()
|
||||
{
|
||||
get { return paused; }
|
||||
set {
|
||||
paused = value;
|
||||
if (music != null)
|
||||
soundEngine.PauseSound(music, paused);
|
||||
}
|
||||
if (music != null)
|
||||
soundEngine.StopSound(music);
|
||||
|
||||
currentMusic = null;
|
||||
}
|
||||
|
||||
public static bool MusicStopped
|
||||
public static void PauseMusic()
|
||||
{
|
||||
get { return stopped; }
|
||||
set {
|
||||
stopped = value;
|
||||
if (music != null && stopped)
|
||||
soundEngine.StopSound(music);
|
||||
}
|
||||
if (music != null)
|
||||
soundEngine.PauseSound(music, true);
|
||||
}
|
||||
|
||||
public static float GlobalVolume
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
using System.Linq;
|
||||
using OpenRA.FileFormats;
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2010 The OpenRA Developers (see AUTHORS)
|
||||
@@ -12,85 +14,86 @@ namespace OpenRA.Widgets.Delegates
|
||||
{
|
||||
public class MusicPlayerDelegate : IWidgetDelegate
|
||||
{
|
||||
string CurrentSong = null;
|
||||
public MusicPlayerDelegate()
|
||||
{
|
||||
var bg = Widget.RootWidget.GetWidget("MUSIC_BG");
|
||||
bg.Visible = Game.Settings.MusicPlayer;
|
||||
CurrentSong = GetNextSong();
|
||||
|
||||
bg.GetWidget("BUTTON_PLAY").OnMouseUp = mi =>
|
||||
{
|
||||
var foo = Widget.RootWidget.GetWidget<VqaPlayerWidget>("VIDEOPLAYER");
|
||||
foo.Load("intro2.vqa");
|
||||
foo.Play();
|
||||
/*
|
||||
if (Sound.MusicStopped)
|
||||
Sound.PlayMusic(GetSong());
|
||||
Sound.MusicStopped = false;
|
||||
Sound.MusicPaused = false;
|
||||
if (CurrentSong == null)
|
||||
return true;
|
||||
|
||||
Sound.PlayMusic(Rules.Music[CurrentSong].Filename);
|
||||
bg.GetWidget("BUTTON_PLAY").Visible = false;
|
||||
bg.GetWidget("BUTTON_PAUSE").Visible = true;
|
||||
*/
|
||||
|
||||
return true;
|
||||
};
|
||||
/*
|
||||
|
||||
bg.GetWidget("BUTTON_PAUSE").OnMouseUp = mi =>
|
||||
{
|
||||
Sound.MusicPaused = true;
|
||||
{
|
||||
Sound.PauseMusic();
|
||||
bg.GetWidget("BUTTON_PAUSE").Visible = false;
|
||||
bg.GetWidget("BUTTON_PLAY").Visible = true;
|
||||
return true;
|
||||
};
|
||||
*/
|
||||
|
||||
bg.GetWidget("BUTTON_STOP").OnMouseUp = mi =>
|
||||
{
|
||||
var foo = Widget.RootWidget.GetWidget<VqaPlayerWidget>("VIDEOPLAYER");
|
||||
foo.Stop();
|
||||
/*
|
||||
Sound.MusicStopped = true;
|
||||
Sound.StopMusic();
|
||||
bg.GetWidget("BUTTON_PAUSE").Visible = false;
|
||||
bg.GetWidget("BUTTON_PLAY").Visible = true;
|
||||
*/
|
||||
|
||||
return true;
|
||||
};
|
||||
/*
|
||||
|
||||
bg.GetWidget("BUTTON_NEXT").OnMouseUp = mi =>
|
||||
{
|
||||
Sound.PlayMusic(GetNextSong());
|
||||
Sound.MusicStopped = false;
|
||||
Sound.MusicPaused = false;
|
||||
bg.GetWidget("BUTTON_PLAY").Visible = false;
|
||||
bg.GetWidget("BUTTON_PAUSE").Visible = true;
|
||||
return true;
|
||||
CurrentSong = GetNextSong();
|
||||
return bg.GetWidget("BUTTON_PLAY").OnMouseUp(mi);
|
||||
};
|
||||
|
||||
bg.GetWidget("BUTTON_PREV").OnMouseUp = mi =>
|
||||
{
|
||||
Sound.PlayMusic(GetPrevSong());
|
||||
Sound.MusicStopped = false;
|
||||
Sound.MusicPaused = false;
|
||||
bg.GetWidget("BUTTON_PLAY").Visible = false;
|
||||
bg.GetWidget("BUTTON_PAUSE").Visible = true;
|
||||
return true;
|
||||
CurrentSong = GetPrevSong();
|
||||
return bg.GetWidget("BUTTON_PLAY").OnMouseUp(mi);
|
||||
};
|
||||
*/
|
||||
}
|
||||
|
||||
|
||||
string GetNextSong()
|
||||
{
|
||||
if (!Rules.Music.ContainsKey("allmusic")) return null;
|
||||
return Rules.Music["allmusic"].Pool.GetNext();
|
||||
var songs = Rules.Music.Select(a => a.Key)
|
||||
.Where(a => FileSystem.Exists(Rules.Music[a].Filename));
|
||||
|
||||
var nextSong = songs
|
||||
.SkipWhile(m => m != CurrentSong)
|
||||
.Skip(1)
|
||||
.FirstOrDefault();
|
||||
|
||||
if (nextSong == null)
|
||||
nextSong = songs.FirstOrDefault();
|
||||
|
||||
return nextSong;
|
||||
}
|
||||
|
||||
string GetPrevSong()
|
||||
{
|
||||
if (!Rules.Music.ContainsKey("allmusic")) return null;
|
||||
return Rules.Music["allmusic"].Pool.GetPrev();
|
||||
}
|
||||
|
||||
string GetSong()
|
||||
{
|
||||
if (!Rules.Music.ContainsKey("allmusic")) return null;
|
||||
return Rules.Music["allmusic"].Pool.GetCurrent();
|
||||
var songs = Rules.Music.Select(a => a.Key)
|
||||
.Where(a => FileSystem.Exists(Rules.Music[a].Filename))
|
||||
.Reverse();
|
||||
|
||||
var nextSong = songs
|
||||
.SkipWhile(m => m != CurrentSong)
|
||||
.Skip(1)
|
||||
.FirstOrDefault();
|
||||
|
||||
if (nextSong == null)
|
||||
nextSong = songs.FirstOrDefault();
|
||||
|
||||
return nextSong;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user