Use CFading where possible

This commit is contained in:
Flamefire committed 2014-03-14 18:15:12 +01:00
1 parent 23364c3bcb
commit 790eda4e15
10 files changed
+119 -202

No files matched your search

+7 -9
View File
@@ -17,7 +17,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using VocaluxeLib;
@@ -39,7 +38,7 @@ namespace Vocaluxe.Base
private static int _Video = -1;
private static CTexture _CurrentVideoTexture;
private static readonly Stopwatch _FadeTimer = new Stopwatch();
private static CFading _VideoFading;
private static bool _VideoEnabled;
private static bool _OwnMusicAdded;
@@ -442,12 +441,12 @@ namespace Vocaluxe.Base
float vtime;
if (CVideo.GetFrame(_Video, ref _CurrentVideoTexture, CSound.GetPosition(_CurrentMusicStream), out vtime))
{
if (_FadeTimer.ElapsedMilliseconds <= 3000L)
_CurrentVideoTexture.Color.A = _FadeTimer.ElapsedMilliseconds / 3000f;
else
if (_VideoFading != null)
{
_CurrentVideoTexture.Color.A = 1f;
_FadeTimer.Stop();
bool finished;
_CurrentVideoTexture.Color.A = _VideoFading.GetValue(out finished);
if (finished)
_VideoFading = null;
}
return _CurrentVideoTexture;
}
@@ -469,8 +468,7 @@ namespace Vocaluxe.Base
CVideo.Skip(_Video, _CurrentPlaylistElement.Start, _CurrentPlaylistElement.VideoGap);
else
CVideo.Skip(_Video, 0f, _CurrentPlaylistElement.VideoGap);
_FadeTimer.Reset();
_FadeTimer.Start();
_VideoFading = new CFading(0f, 1f, 3f);
}
}
+11 -37
View File
@@ -24,10 +24,7 @@ namespace Vocaluxe.Base
{
class CCursor
{
private readonly Stopwatch _CursorFadingTimer = new Stopwatch();
private float _CursorTargetAlpha = 1f;
private float _CursorStartAlpha;
private float _CursorFadingTime = 0.5f;
private CFading _Fading;
private CTexture _Cursor;
private readonly Stopwatch _Movetimer = new Stopwatch();
@@ -52,28 +49,19 @@ namespace Vocaluxe.Base
public void Draw()
{
if (_Movetimer.IsRunning && _Movetimer.ElapsedMilliseconds / 1000f > CSettings.MouseMoveOffTime)
if (_Movetimer.IsRunning && _Movetimer.ElapsedMilliseconds > CSettings.MouseMoveOffTime)
{
_Movetimer.Stop();
_Fade(0f, 0.5f);
}
if (_CursorFadingTimer.IsRunning)
if (_Fading != null)
{
float t = _CursorFadingTimer.ElapsedMilliseconds / 1000f;
if (t < _CursorFadingTime)
{
if (_CursorTargetAlpha >= _CursorStartAlpha)
_Cursor.Color.A = _CursorStartAlpha + (_CursorTargetAlpha - _CursorStartAlpha) * t / _CursorFadingTime;
else
_Cursor.Color.A = (_CursorStartAlpha - _CursorTargetAlpha) * (1f - t / _CursorFadingTime);
}
else
{
_CursorFadingTimer.Stop();
_Cursor.Color.A = _CursorTargetAlpha;
}
bool finished;
_Cursor.Color.A = _Fading.GetValue(out finished);
if (finished)
_Fading = null;
}
if (Visible && (CSettings.GameState == EGameState.EditTheme || ShowCursor))
@@ -85,11 +73,10 @@ namespace Vocaluxe.Base
if (Math.Abs(_Cursor.Rect.X - x) > CSettings.MouseMoveDiffMin ||
Math.Abs(_Cursor.Rect.Y - y) > CSettings.MouseMoveDiffMin)
{
if (_CursorTargetAlpha < 0.01)
if (!IsActive)
_Fade(1f, 0.2f);
_Movetimer.Reset();
_Movetimer.Start();
_Movetimer.Restart();
CSettings.MouseActive();
}
@@ -130,26 +117,13 @@ namespace Vocaluxe.Base
public void FadeIn()
{
_Movetimer.Reset();
_Movetimer.Start();
_Movetimer.Restart();
_Fade(1f, 0.2f);
}
private void _Fade(float targetAlpha, float time)
{
_CursorFadingTimer.Stop();
_CursorFadingTimer.Reset();
if (targetAlpha >= 0f && targetAlpha <= 1f)
_CursorTargetAlpha = targetAlpha;
else
_CursorTargetAlpha = 1f;
if (time >= 0f)
_CursorFadingTime = time;
_CursorStartAlpha = _Cursor.Color.A;
_CursorFadingTimer.Start();
_Fading = new CFading(_Cursor.Color.A, targetAlpha, time);
}
}
}
+35 -66
View File
@@ -30,13 +30,11 @@ namespace Vocaluxe.Base
{
static class CGraphics
{
private static bool _Fading;
private static Stopwatch _FadingTimer;
private static CFading _Fading;
private static readonly CCursor _Cursor = new CCursor();
private static float _GlobalAlpha;
private static float _ZOffset;
private static IMenu _OldScreen;
private static EScreens _CurrentScreen;
private static EScreens _NextScreen;
private static EPopupScreens _CurrentPopupScreen;
@@ -98,7 +96,6 @@ namespace Vocaluxe.Base
_CurrentScreen = EScreens.ScreenLoad;
_NextScreen = EScreens.ScreenNull;
_CurrentPopupScreen = EPopupScreens.NoPopup;
_FadingTimer = new Stopwatch();
_VolumePopupTimer = new Stopwatch();
_GlobalAlpha = 1f;
@@ -198,11 +195,9 @@ namespace Vocaluxe.Base
public static bool Draw()
{
if ((_NextScreen != EScreens.ScreenNull) && !_Fading)
if (_NextScreen != EScreens.ScreenNull && _Fading == null)
{
_Fading = true;
_FadingTimer.Reset();
_FadingTimer.Start();
_Fading = new CFading(0f, 1f, CConfig.FadeTime);
if (_NextScreen == EScreens.ScreenPartyDummy)
{
@@ -216,49 +211,29 @@ namespace Vocaluxe.Base
HidePopup(EPopupScreens.PopupPlayerControl);
}
if (_Fading)
if (_Fading != null)
{
long fadeTime = (long)(CConfig.FadeTime * 1000);
bool finished;
float newAlpha = _Fading.GetValue(out finished);
if ((_FadingTimer.ElapsedMilliseconds < fadeTime) && (CConfig.FadeTime > 0))
if (!finished)
{
long ms = 1;
if (_FadingTimer.ElapsedMilliseconds > 0)
ms = _FadingTimer.ElapsedMilliseconds;
float factor = (float)ms / fadeTime;
_GlobalAlpha = 1f; // -factor / 100f;
_GlobalAlpha = 1f; // -newAlpha / 100f;
_ZOffset = CSettings.ZFar / 2;
if (_CurrentScreen == EScreens.ScreenPartyDummy)
{
CFonts.PartyModeID = CParty.CurrentPartyModeID;
_OldScreen.Draw();
CFonts.PartyModeID = -1;
}
else
_OldScreen.Draw();
_DrawScreen(_CurrentScreen);
_GlobalAlpha = factor;
_GlobalAlpha = newAlpha;
_ZOffset = 0f;
if (_NextScreen == EScreens.ScreenPartyDummy)
{
CFonts.PartyModeID = CParty.CurrentPartyModeID;
_Screens[(int)_NextScreen].Draw();
CFonts.PartyModeID = -1;
}
else
_Screens[(int)_NextScreen].Draw();
_DrawScreen(_NextScreen);
_GlobalAlpha = 1f;
}
else
{
_Screens[(int)_CurrentScreen].OnClose();
GC.Collect();
_CurrentScreen = _NextScreen;
_NextScreen = EScreens.ScreenNull;
if (CBackgroundMusic.IsPlaying)
@@ -266,32 +241,13 @@ namespace Vocaluxe.Base
_Screens[(int)_CurrentScreen].OnShowFinish();
_Screens[(int)_CurrentScreen].ProcessMouseMove(_Cursor.X, _Cursor.Y);
if (_CurrentScreen == EScreens.ScreenPartyDummy)
{
CFonts.PartyModeID = CParty.CurrentPartyModeID;
_Screens[(int)_CurrentScreen].Draw();
CFonts.PartyModeID = -1;
}
else
_Screens[(int)_CurrentScreen].Draw();
_DrawScreen(_CurrentScreen);
_Fading = false;
_FadingTimer.Stop();
_Fading = null;
}
}
else
{
if (_CurrentScreen == EScreens.ScreenPartyDummy)
{
CFonts.PartyModeID = CParty.CurrentPartyModeID;
_Screens[(int)_CurrentScreen].Draw();
CFonts.PartyModeID = -1;
}
else
_Screens[(int)_CurrentScreen].Draw();
_OldScreen = _Screens[(int)_CurrentScreen];
}
_DrawScreen(_CurrentScreen);
foreach (IMenu popup in _PopupScreens)
popup.Draw();
@@ -302,6 +258,18 @@ namespace Vocaluxe.Base
return true;
}
private static void _DrawScreen(EScreens screen)
{
if (screen == EScreens.ScreenPartyDummy)
{
CFonts.PartyModeID = CParty.CurrentPartyModeID;
_Screens[(int)screen].Draw();
CFonts.PartyModeID = -1;
}
else
_Screens[(int)screen].Draw();
}
public static void FadeTo(EScreens screen)
{
if (screen == EScreens.ScreenPartyDummy)
@@ -483,8 +451,7 @@ namespace Vocaluxe.Base
if (_CurrentPopupScreen == EPopupScreens.NoPopup && diff != 0)
{
ShowPopup(EPopupScreens.PopupVolumeControl);
_VolumePopupTimer.Reset();
_VolumePopupTimer.Start();
_VolumePopupTimer.Restart();
}
CBackgroundMusic.ApplyVolume();
@@ -498,7 +465,7 @@ namespace Vocaluxe.Base
CDraw.MakeScreenShot();
else
{
if (!_Fading)
if (_Fading == null)
{
bool handled = false;
if (_CurrentPopupScreen != EPopupScreens.NoPopup)
@@ -562,12 +529,14 @@ namespace Vocaluxe.Base
bool handled = false;
if (_CurrentPopupScreen != EPopupScreens.NoPopup)
{
handled = _PopupScreens[(int)_CurrentPopupScreen].HandleMouse(mouseEvent);
if (handled && _CurrentPopupScreen == EPopupScreens.PopupVolumeControl)
_Screens[(int)_CurrentScreen].ApplyVolume();
if (handled && _CurrentPopupScreen == EPopupScreens.PopupVolumeControl)
_Screens[(int)_CurrentScreen].ApplyVolume();
}
if (!handled && !_Fading && (_Cursor.IsActive || mouseEvent.LB || mouseEvent.RB || mouseEvent.MB))
if (!handled && _Fading == null && (_Cursor.IsActive || mouseEvent.LB || mouseEvent.RB || mouseEvent.MB))
resume &= _Screens[(int)_CurrentScreen].HandleMouse(mouseEvent);
if (!eventsAvailable)
@@ -594,14 +563,14 @@ namespace Vocaluxe.Base
CDraw.MakeScreenShot();
else
{
if (!_Fading)
if (_Fading == null)
_Screens[(int)_CurrentScreen].HandleInputThemeEditor(keyEvent);
}
}
while (mouse.PollEvent(ref mouseEvent))
{
if (!_Fading)
if (_Fading == null)
_Screens[(int)_CurrentScreen].HandleMouseThemeEditor(mouseEvent);
_UpdateMousePosition(mouseEvent.X, mouseEvent.Y);
+1 -1
View File
@@ -129,7 +129,7 @@ namespace Vocaluxe.Base
public const int NumNoteLines = 11;
public static int MouseMoveDiffMin = 2;
public const float MouseMoveOffTime = 3f;
public const int MouseMoveOffTime = 3000; //in ms
public const int MaxNumPlayer = 6;
public const int MaxScore = 10000;
@@ -213,14 +213,6 @@ namespace Vocaluxe.Lib.Sound
private class CGstreamerSharpAudioStream
{
private enum EStreamAction
{
Nothing,
Pause,
Stop,
Close
}
private Element _Element;
private bool _Loop;
public volatile bool Closed = true;
+23 -31
View File
@@ -24,6 +24,7 @@ using System.IO;
using System.Threading;
using Vocaluxe.Base;
using Vocaluxe.Lib.Sound.Decoder;
using VocaluxeLib;
namespace Vocaluxe.Lib.Sound
{
@@ -370,14 +371,8 @@ namespace Vocaluxe.Lib.Sound
private float _Volume = 1f;
private float _VolumeMax = 1f;
private readonly Stopwatch _FadeTimer = new Stopwatch();
private float _FadeTime;
private float _TargetVolume = 1f;
private float _StartVolume = 1f;
private bool _CloseStreamAfterFade;
private bool _PauseStreamAfterFade;
private bool _Fading;
private EStreamAction _AfterFadeAction;
private CFading _Fading;
private readonly Stopwatch _Timer = new Stopwatch();
@@ -519,33 +514,28 @@ namespace Vocaluxe.Lib.Sound
public void Fade(float targetVolume, float fadeTime)
{
_Fading = true;
_FadeTimer.Stop();
_FadeTimer.Reset();
_StartVolume = _Volume;
_TargetVolume = targetVolume / 100f;
_FadeTime = fadeTime;
_FadeTimer.Start();
_Fading = new CFading(_Volume, targetVolume, fadeTime);
_AfterFadeAction = EStreamAction.Nothing;
}
public void FadeAndPause(float targetVolume, float fadeTime)
{
_PauseStreamAfterFade = true;
Fade(targetVolume, fadeTime);
_AfterFadeAction = EStreamAction.Pause;
}
public void FadeAndStop(float targetVolume, float fadeTime, Closeproc closeProc, int streamID)
{
_Closeproc = closeProc;
_StreamID = streamID;
_CloseStreamAfterFade = true;
Fade(targetVolume, fadeTime);
_AfterFadeAction = EStreamAction.Stop;
}
public void Play()
{
Paused = false;
_PauseStreamAfterFade = false;
_AfterFadeAction = EStreamAction.Nothing;
AL.SourcePlay(_Source);
}
@@ -745,21 +735,23 @@ namespace Vocaluxe.Lib.Sound
private void _Update()
{
if (_Fading)
if (_Fading != null)
{
if (_FadeTimer.ElapsedMilliseconds / 1000f < _FadeTime)
_Volume = _StartVolume + (_TargetVolume - _StartVolume) * ((_FadeTimer.ElapsedMilliseconds / 1000f) / _FadeTime);
else
bool finished;
_Volume = _Fading.GetValue(out finished);
if (finished)
{
_Volume = _TargetVolume;
_FadeTimer.Stop();
_Fading = false;
if (_CloseStreamAfterFade)
_Terminated = true;
if (_PauseStreamAfterFade)
Paused = true;
switch (_AfterFadeAction)
{
case EStreamAction.Close:
case EStreamAction.Stop:
_Terminated = true;
break;
case EStreamAction.Pause:
Paused = true;
break;
}
_Fading = null;
}
}
}
+25 -40
View File
@@ -17,7 +17,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
@@ -25,6 +24,7 @@ using System.Threading;
using Vocaluxe.Base;
using Vocaluxe.Lib.Sound.Decoder;
using Vocaluxe.Lib.Sound.PortAudio;
using VocaluxeLib;
namespace Vocaluxe.Lib.Sound
{
@@ -351,15 +351,8 @@ namespace Vocaluxe.Lib.Sound
private float _Volume = 1f;
private float _VolumeMax = 1f;
private readonly Stopwatch _FadeTimer = new Stopwatch();
private float _FadeTime;
private float _TargetVolume = 1f;
private float _StartVolume = 1f;
private bool _CloseStreamAfterFade;
private bool _PauseStreamAfterFade;
private bool _StopStreamAfterFade;
private bool _Fading;
private EStreamAction _AfterFadeAction;
private CFading _Fading;
private static CPortAudio.SPaHostApiInfo _ApiInfo;
private static CPortAudio.SPaDeviceInfo _OutputDeviceInfo;
@@ -507,19 +500,12 @@ namespace Vocaluxe.Lib.Sound
public void Fade(float targetVolume, float fadeTime)
{
_Fading = true;
_FadeTimer.Stop();
_FadeTimer.Reset();
_StartVolume = _Volume;
_TargetVolume = targetVolume / 100f;
_FadeTime = fadeTime;
_FadeTimer.Start();
_Fading = new CFading(_Volume, targetVolume, fadeTime);
_AfterFadeAction = EStreamAction.Nothing;
}
public void FadeAndPause(float targetVolume, float fadeTime)
{
_PauseStreamAfterFade = true;
Fade(targetVolume, fadeTime);
}
@@ -527,25 +513,22 @@ namespace Vocaluxe.Lib.Sound
{
_Closeproc = closeProc;
_StreamID = streamID;
_CloseStreamAfterFade = true;
Fade(targetVolume, fadeTime);
_AfterFadeAction = EStreamAction.Close;
}
public void FadeAndStop(float targetVolume, float fadeTime, Closeproc closeProc, int streamID)
{
_Closeproc = closeProc;
_StreamID = streamID;
_StopStreamAfterFade = true;
Fade(targetVolume, fadeTime);
_AfterFadeAction = EStreamAction.Stop;
}
public void Play()
{
Paused = false;
_PauseStreamAfterFade = false;
_StopStreamAfterFade = false;
_AfterFadeAction = EStreamAction.Nothing;
lock (_Mutex)
{
_ErrorCheck("StartStream", CPortAudio.Pa_StartStream(_Ptr));
@@ -927,23 +910,25 @@ namespace Vocaluxe.Lib.Sound
private void _Update()
{
if (_Fading)
if (_Fading != null)
{
if (_FadeTimer.ElapsedMilliseconds / 1000f < _FadeTime)
_Volume = _StartVolume + (_TargetVolume - _StartVolume) * ((_FadeTimer.ElapsedMilliseconds / 1000f) / _FadeTime);
else
bool finished;
_Volume = _Fading.GetValue(out finished);
if (finished)
{
_Volume = _TargetVolume;
_FadeTimer.Stop();
_Fading = false;
if (_CloseStreamAfterFade)
_Terminated = true;
if (_PauseStreamAfterFade)
Paused = true;
if (_StopStreamAfterFade)
Stop();
switch (_AfterFadeAction)
{
case EStreamAction.Close:
_Terminated = true;
break;
case EStreamAction.Stop:
Stop();
break;
case EStreamAction.Pause:
Paused = true;
break;
}
_Fading = null;
}
}
}
+8
View File
@@ -17,6 +17,14 @@
namespace Vocaluxe.Lib.Sound
{
public enum EStreamAction
{
Nothing,
Pause,
Stop,
Close
}
struct SAudioStreams
{
public int Handle;
+2
View File
@@ -34,6 +34,8 @@ namespace VocaluxeLib
/// <param name="duration">Duration of the fading in seconds</param>
public CFading(float fromValue, float toValue, float duration)
{
if (duration < 0)
duration = 0;
_FromValue = fromValue;
_ToValue = toValue;
_Duration = duration * 1000f;
@@ -17,7 +17,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Xml;
using VocaluxeLib.Draw;
@@ -99,7 +98,7 @@ namespace VocaluxeLib.Menu.SongMenu
protected SThemeSongMenu _Theme;
private bool _ThemeLoaded;
private readonly Stopwatch _VideoFadeTimer = new Stopwatch();
private CFading _VideoFading;
private readonly List<int> _Streams = new List<int>();
protected CTexture _Vidtex;
@@ -388,12 +387,12 @@ namespace VocaluxeLib.Menu.SongMenu
{
if (_Vidtex != null)
{
if (_VideoFadeTimer.ElapsedMilliseconds <= 3000L)
_Vidtex.Color.A = _VideoFadeTimer.ElapsedMilliseconds / 3000f;
else
if (_VideoFading != null)
{
_Vidtex.Color.A = 1f;
_VideoFadeTimer.Stop();
bool finished;
_Vidtex.Color.A = _VideoFading.GetValue(out finished);
if (finished)
_VideoFading = null;
}
}
}
@@ -563,9 +562,7 @@ namespace VocaluxeLib.Menu.SongMenu
if (_PreviewVideoStream == -1)
return;
CBase.Video.Skip(_PreviewVideoStream, startposition, song.VideoGap);
_VideoFadeTimer.Stop();
_VideoFadeTimer.Reset();
_VideoFadeTimer.Start();
_VideoFading = new CFading(0f, 1f, 3f);
}
}
}