Added MusicControllerLogic, now we can handle audio buttons anywhere in the game.
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2016 The OpenRA Developers (see AUTHORS)
|
||||
* This file is part of OpenRA, which is free software. It is made
|
||||
* available to you under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation, either version 3 of
|
||||
* the License, or (at your option) any later version. For more
|
||||
* information, see COPYING.
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System.Linq;
|
||||
using OpenRA.GameRules;
|
||||
using OpenRA.Graphics;
|
||||
using OpenRA.Mods.Common.Traits;
|
||||
using OpenRA.Widgets;
|
||||
|
||||
namespace OpenRA.Mods.Common.Widgets.Logic.Ingame
|
||||
{
|
||||
public class MusicControllerLogic : ChromeLogic
|
||||
{
|
||||
MusicPlaylist musicPlaylist;
|
||||
|
||||
[ObjectCreator.UseCtor]
|
||||
public MusicControllerLogic(Widget widget, World world, WorldRenderer worldRenderer)
|
||||
{
|
||||
musicPlaylist = world.WorldActor.Trait<MusicPlaylist>();
|
||||
|
||||
var keyhandler = widget.Get<LogicKeyListenerWidget>("MUSICCONTROLLER_KEYHANDLER");
|
||||
keyhandler.OnKeyPress = e =>
|
||||
{
|
||||
if (e.Event == KeyInputEvent.Down)
|
||||
{
|
||||
var key = Hotkey.FromKeyInput(e);
|
||||
|
||||
if (key == Game.Settings.Keys.NextTrack)
|
||||
musicPlaylist.Play(musicPlaylist.GetNextSong());
|
||||
else if (key == Game.Settings.Keys.PreviousTrack)
|
||||
musicPlaylist.Play(musicPlaylist.GetPrevSong());
|
||||
else if (key == Game.Settings.Keys.StopMusic)
|
||||
StopMusic();
|
||||
else if (key == Game.Settings.Keys.PauseMusic)
|
||||
PauseOrResumeMusic();
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
}
|
||||
|
||||
void PauseOrResumeMusic()
|
||||
{
|
||||
if (Game.Sound.MusicPlaying)
|
||||
Game.Sound.PauseMusic();
|
||||
else if (Game.Sound.CurrentMusic != null)
|
||||
Game.Sound.PlayMusic();
|
||||
else
|
||||
{
|
||||
musicPlaylist.Play(musicPlaylist.GetNextSong());
|
||||
}
|
||||
}
|
||||
|
||||
void StopMusic()
|
||||
{
|
||||
if (!musicPlaylist.CurrentSongIsBackground)
|
||||
musicPlaylist.Stop();
|
||||
else
|
||||
musicPlaylist.Play(musicPlaylist.GetNextSong());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -560,6 +560,24 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
BindHotkeyPref(kv, ks, developerTemplate, hotkeyList);
|
||||
}
|
||||
|
||||
// Music
|
||||
{
|
||||
var hotkeys = new Dictionary<string, string>()
|
||||
{
|
||||
{ "NextTrack", "Next" },
|
||||
{ "PreviousTrack", "Previous" },
|
||||
{ "StopMusic", "Stop" },
|
||||
{ "PauseMusic", "Pause or Resume" }
|
||||
};
|
||||
|
||||
var header = ScrollItemWidget.Setup(hotkeyHeader, returnTrue, doNothing);
|
||||
header.Get<LabelWidget>("LABEL").GetText = () => "Music commands";
|
||||
hotkeyList.AddChild(header);
|
||||
|
||||
foreach (var kv in hotkeys)
|
||||
BindHotkeyPref(kv, ks, developerTemplate, hotkeyList);
|
||||
}
|
||||
|
||||
return () => { };
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user