diff --git a/Vocaluxe/Base/CConfig.cs b/Vocaluxe/Base/CConfig.cs index 9bc011eb..7b6e35e2 100644 --- a/Vocaluxe/Base/CConfig.cs +++ b/Vocaluxe/Base/CConfig.cs @@ -806,12 +806,14 @@ namespace Vocaluxe.Base /// public static void UsePlayers() { + CProfile[] profiles = CProfiles.GetProfiles(); + for (int j = 0; j < CSettings.MaxNumPlayer; j++) { CGame.Players[j].ProfileID = -1; - if (Players[j] == "") + if (Players[j] == "" || profiles == null) continue; - CProfile[] profiles = CProfiles.GetProfiles(); + for (int i = 0; i < profiles.Length; i++) { if (Path.GetFileName(profiles[i].FileName) == Players[j] && profiles[i].Active == EOffOn.TR_CONFIG_ON) diff --git a/Vocaluxe/Base/CGame.cs b/Vocaluxe/Base/CGame.cs index c6b58e0e..40942314 100644 --- a/Vocaluxe/Base/CGame.cs +++ b/Vocaluxe/Base/CGame.cs @@ -68,8 +68,6 @@ namespace Vocaluxe.Base _SongQueue.Init(); Players = new SPlayer[CSettings.MaxNumPlayer]; ResetPlayer(); - - CConfig.UsePlayers(); } public static EGameMode GameMode diff --git a/Vocaluxe/Base/CGraphics.cs b/Vocaluxe/Base/CGraphics.cs index b08f6373..5e7001fb 100644 --- a/Vocaluxe/Base/CGraphics.cs +++ b/Vocaluxe/Base/CGraphics.cs @@ -322,6 +322,7 @@ namespace Vocaluxe.Base CSound.Update(); CBackgroundMusic.Update(); CController.Update(); + CProfiles.Update(); if (CConfig.CoverLoading == ECoverLoading.TR_CONFIG_COVERLOADING_DYNAMIC && _CurrentScreen != EScreens.ScreenSing) CSongs.LoadCover(); diff --git a/Vocaluxe/Base/CProfiles.cs b/Vocaluxe/Base/CProfiles.cs index aed33efc..47efb51d 100644 --- a/Vocaluxe/Base/CProfiles.cs +++ b/Vocaluxe/Base/CProfiles.cs @@ -28,21 +28,55 @@ using VocaluxeLib.Profile; namespace Vocaluxe.Base { - + [Flags] + public enum EProfileChangedFlags + { + None = 1, + Avatar = 2, + Profile = 4 + } + + public delegate void ProfileChangedCallback(EProfileChangedFlags TypeChanged); static class CProfiles { + #region enums and structs + enum EAction + { + LoadProfiles, + LoadAvatars, + AddProfile, + EditProfile, + DeleteProfile, + AddAvatar, + EditAvatar + } + + struct SChange + { + public CProfile Profile; + public CAvatar Avatar; + public EAction Action; + public int ProfileID; + } + #endregion enums and structs + + #region private vars private static Dictionary _Profiles; private static Queue _ProfileIDs; - private static Object _ProfileMutex = new Object(); private static Dictionary _Avatars; private static Queue _AvatarIDs; - private static Object _AvatarMutex = new Object(); - public delegate void ProfilesChangedNotification(); - private static List _Callbacks; + private static Queue _Queue = new Queue(); + private static Object _QueueMutex = new Object(); + private static List _ProfileChangedCallbacks = new List(); + private static bool _ProfilesChanged = false; + private static bool _AvatarsChanged = false; + #endregion private vars + + #region properties public static int NumProfiles { get { return _Profiles.Count; } @@ -52,7 +86,9 @@ namespace Vocaluxe.Base { get { return _Avatars.Count; } } + #endregion properties + #region public methods public static void Init() { _Avatars = new Dictionary(); @@ -69,56 +105,293 @@ namespace Vocaluxe.Base _ProfileIDs.Enqueue(i); } - _Callbacks = new List(); - LoadProfiles(); } - public static void AddNotificationCallback(ProfilesChangedNotification Callback) + public static void Update() { - if (Callback == null) + lock (_QueueMutex) + { + while (_Queue.Count > 0) + { + SChange change = _Queue.Dequeue(); + switch (change.Action) + { + case EAction.LoadProfiles: + _LoadProfiles(); + _ProfilesChanged = true; + _AvatarsChanged = true; + break; + + case EAction.LoadAvatars: + _LoadAvatars(); + _AvatarsChanged = true; + break; + + case EAction.AddProfile: + if (change.Profile == null) + break; + + change.Profile.ID = _ProfileIDs.Dequeue(); + _Profiles.Add(change.Profile.ID, change.Profile); + _ProfilesChanged = true; + break; + + case EAction.EditProfile: + if (change.Profile == null) + break; + + if (!ICProfileIDValid(change.Profile.ID)) + return; + + _Profiles[change.Profile.ID] = change.Profile; + _ProfilesChanged = true; + break; + + case EAction.DeleteProfile: + if (!ICProfileIDValid(change.ProfileID)) + break; + + _DeleteProfile(change.ProfileID); + _ProfilesChanged = true; + break; + + case EAction.AddAvatar: + if (change.Avatar == null) + break; + + change.Avatar.ID = _AvatarIDs.Dequeue(); + _Avatars.Add(change.Avatar.ID, change.Avatar); + _AvatarsChanged = true; + break; + + case EAction.EditAvatar: + if (change.Avatar == null) + break; + + if (!ICAvatarIDValid(change.Avatar.ID)) + return; + + _Avatars[change.Avatar.ID] = change.Avatar; + _AvatarsChanged = true; + break; + + default: + break; + } + } + } + + if (_ProfileChangedCallbacks.Count == 0) return; - lock (_Callbacks) + EProfileChangedFlags flags = EProfileChangedFlags.None; + + if (_AvatarsChanged) + flags = EProfileChangedFlags.Avatar; + + if (_ProfilesChanged) + flags |= EProfileChangedFlags.Profile; + + if (flags != EProfileChangedFlags.None) { - _Callbacks.Add(Callback); + int index = 0; + while (index < _ProfileChangedCallbacks.Count) + { + try + { + _ProfileChangedCallbacks[index](flags); + } + catch (Exception) + { + _ProfileChangedCallbacks.RemoveAt(index); + } + index++; + } + } + _AvatarsChanged = false; + _ProfilesChanged = false; + } + + public static void AddProfileChangedCallback(ProfileChangedCallback Notification) + { + _ProfileChangedCallbacks.Add(Notification); + } + + public static void LoadProfiles() + { + SChange change = new SChange(); + change.Action = EAction.LoadProfiles; + + lock (_QueueMutex) + { + _Queue.Enqueue(change); } } + public static void LoadAvatars() + { + SChange change = new SChange(); + change.Action = EAction.LoadAvatars; + + lock (_QueueMutex) + { + _Queue.Enqueue(change); + } + } + + public static void AddProfile(CProfile NewProfile) + { + if (NewProfile == null) + return; + + SChange change = new SChange(); + change.Action = EAction.AddProfile; + change.Profile = NewProfile; + + lock (_QueueMutex) + { + _Queue.Enqueue(change); + } + } + + public static void EditProfile(CProfile EditProfile) + { + if (EditProfile == null) + return; + + SChange change = new SChange(); + change.Action = EAction.EditProfile; + change.Profile = EditProfile; + + lock (_QueueMutex) + { + _Queue.Enqueue(change); + } + } + + public static void DeleteProfile(int profileID) + { + if (!ICProfileIDValid(profileID)) + return; + + SChange change = new SChange(); + change.Action = EAction.DeleteProfile; + change.ProfileID = profileID; + + lock (_QueueMutex) + { + _Queue.Enqueue(change); + } + } + + public static void AddAvatar(CAvatar NewAvatar) + { + if (NewAvatar == null) + return; + + SChange change = new SChange(); + change.Action = EAction.AddAvatar; + change.Avatar = NewAvatar; + + lock (_QueueMutex) + { + _Queue.Enqueue(change); + } + } + + public static void EditAvatar(CAvatar EditAvatar) + { + if (EditAvatar == null) + return; + + SChange change = new SChange(); + change.Action = EAction.EditAvatar; + change.Avatar = EditAvatar; + + lock (_QueueMutex) + { + _Queue.Enqueue(change); + } + } + public static CProfile[] GetProfiles() { List list = new List(); - lock (_ProfileMutex) - { - if (_Profiles.Count == 0) - return null; + if (_Profiles.Count == 0) + return null; - CProfile[] result = new CProfile[_Profiles.Count]; - _Profiles.Values.CopyTo(result, 0); - for (int i = 0; i < result.Length; i++) - { - list.Add(result[i]); - } - list.Sort(_CompareByPlayerName); + CProfile[] result = new CProfile[_Profiles.Count]; + _Profiles.Values.CopyTo(result, 0); + for (int i = 0; i < result.Length; i++) + { + list.Add(result[i]); } + list.Sort(_CompareByPlayerName); return list.ToArray(); } public static CAvatar[] GetAvatars() { CAvatar[] result = null; - lock (_AvatarMutex) - { - if (_Avatars.Count == 0) - return null; + if (_Avatars.Count == 0) + return null; - result = new CAvatar[_Avatars.Count]; - _Avatars.Values.CopyTo(result, 0); - } + result = new CAvatar[_Avatars.Count]; + _Avatars.Values.CopyTo(result, 0); + return result; } + + public static int NewProfile(string fileName = "") + { + CProfile profile = new CProfile + { + FileName = fileName != "" ? Path.Combine(CSettings.FolderProfiles, fileName) : String.Empty + }; + if (File.Exists(profile.FileName)) + return -1; + + profile.ID = _ProfileIDs.Dequeue(); + _Profiles.Add(profile.ID, profile); + _ProfilesChanged = true; + return profile.ID; + } + + public static int NewAvatar(string fileName) + { + CAvatar avatar = new CAvatar(-1); + if (!avatar.LoadFromFile(fileName)) + return -1; + + avatar.ID = _AvatarIDs.Dequeue(); + _Avatars.Add(avatar.ID, avatar); + _AvatarsChanged = true; + return avatar.ID; + } + + public static void SaveProfiles() + { + foreach (int id in _Profiles.Keys) + { + _Profiles[id].SaveProfile(); + } + } + + public static bool ICProfileIDValid(int profileID) + { + return _Profiles.ContainsKey(profileID); + } + + public static bool ICAvatarIDValid(int avatarID) + { + return _Avatars.ContainsKey(avatarID); + } + #endregion public methods + + #region profile properties public static string GetPlayerName(int profileID, int playerNum = 0) { if (ICProfileIDValid(profileID)) @@ -130,134 +403,6 @@ namespace Vocaluxe.Base return playerName; } - public static string GetProfileFileName(int profileID) - { - if (!ICProfileIDValid(profileID)) - return String.Empty; - - return Path.GetFileName(_Profiles[profileID].FileName); - } - - public static int NewProfile(string fileName = "") - { - CProfile profile = new CProfile - { - FileName = fileName != "" ? Path.Combine(CSettings.FolderProfiles, fileName) : String.Empty - }; - - if (File.Exists(profile.FileName)) - return -1; - - lock (_ProfileMutex) - { - profile.ID = _ProfileIDs.Dequeue(); - _Profiles.Add(profile.ID, profile); - } - _Notification(); - return profile.ID; - } - - public static void SaveProfiles() - { - foreach (int id in _Profiles.Keys) - { - _Profiles[id].SaveProfile(); - } - - LoadProfiles(); - } - - public static void LoadProfiles() - { - LoadAvatars(); - - lock (_ProfileMutex) - { - List knownFiles = new List(); - if (_Profiles.Count > 0) - { - int[] ids = new int[_Profiles.Keys.Count]; - _Profiles.Keys.CopyTo(ids, 0); - for (int i = 0; i < ids.Length; i++) - { - if (_Profiles[ids[i]].LoadProfile()) - { - _Profiles[ids[i]].Avatar = _GetAvatar(_Profiles[ids[i]].AvatarFileName); - knownFiles.Add(_Profiles[ids[i]].FileName); - } - else - _Profiles.Remove(ids[i]); - } - } - - List files = new List(); - files.AddRange(CHelper.ListFiles(CSettings.FolderProfiles, "*.xml", true, true)); - - foreach (string file in knownFiles) - { - files.Remove(file); - } - - foreach (string file in files) - { - CProfile profile = new CProfile - { - FileName = Path.Combine(CSettings.FolderProfiles, file) - }; - - if (profile.LoadProfile()) - { - profile.Avatar = _GetAvatar(profile.AvatarFileName); - profile.ID = _ProfileIDs.Dequeue(); - _Profiles.Add(profile.ID, profile); - } - } - } - _Notification(); - } - - public static void LoadAvatars() - { - lock (_AvatarMutex) - { - List knownFiles = new List(); - if (_Avatars.Count > 0) - { - int[] ids = new int[_Avatars.Keys.Count]; - _Avatars.Keys.CopyTo(ids, 0); - for (int i = 0; i < ids.Length; i++) - { - if (_Avatars[ids[i]].Reload()) - knownFiles.Add(_Avatars[ids[i]].FileName); - else - _Avatars.Remove(ids[i]); - } - } - - List files = new List(); - files.AddRange(CHelper.ListFiles(CSettings.FolderProfiles, "*.png", true, true)); - files.AddRange(CHelper.ListFiles(CSettings.FolderProfiles, "*.jpg", true, true)); - files.AddRange(CHelper.ListFiles(CSettings.FolderProfiles, "*.jpeg", true, true)); - files.AddRange(CHelper.ListFiles(CSettings.FolderProfiles, "*.bmp", true, true)); - - foreach (string file in knownFiles) - { - files.Remove(file); - } - - foreach (string file in files) - { - CAvatar avatar = new CAvatar(-1); - if (avatar.LoadFromFile(file)) - { - avatar.ID = _AvatarIDs.Dequeue(); - _Avatars.Add(avatar.ID, avatar); - } - } - } - _Notification(); - } - public static void SetPlayerName(int profileID, string PlayerName) { if (!ICProfileIDValid(profileID)) @@ -266,6 +411,14 @@ namespace Vocaluxe.Base _Profiles[profileID].PlayerName = PlayerName; } + public static string GetProfileFileName(int profileID) + { + if (!ICProfileIDValid(profileID)) + return String.Empty; + + return Path.GetFileName(_Profiles[profileID].FileName); + } + public static string AddGetPlayerName(int profileID, char chr) { if (!ICProfileIDValid(profileID)) @@ -287,16 +440,6 @@ namespace Vocaluxe.Base return profile.PlayerName; } - public static bool ICProfileIDValid(int profileID) - { - return _Profiles.ContainsKey(profileID); - } - - public static bool ICAvatarIDValid(int avatarID) - { - return _Avatars.ContainsKey(avatarID); - } - public static EGameDifficulty GetDifficulty(int profileID) { return ICProfileIDValid(profileID) ? _Profiles[profileID].Difficulty : EGameDifficulty.TR_CONFIG_NORMAL; @@ -315,11 +458,6 @@ namespace Vocaluxe.Base return ICProfileIDValid(profileID) ? _Profiles[profileID].GuestProfile : EOffOn.TR_CONFIG_OFF; } - public static EOffOn GetActive(int profileID) - { - return ICProfileIDValid(profileID) ? _Profiles[profileID].Active : EOffOn.TR_CONFIG_OFF; - } - public static void SetGuestProfile(int profileID, EOffOn option) { if (!ICProfileIDValid(profileID)) @@ -328,10 +466,16 @@ namespace Vocaluxe.Base _Profiles[profileID].GuestProfile = option; } + public static EOffOn GetActive(int profileID) + { + return ICProfileIDValid(profileID) ? _Profiles[profileID].Active : EOffOn.TR_CONFIG_OFF; + } + public static void SetActive(int profileID, EOffOn option) { if (!ICProfileIDValid(profileID)) return; + _Profiles[profileID].Active = option; } @@ -365,7 +509,9 @@ namespace Vocaluxe.Base return _Profiles[profileID].Avatar.ID; } + #endregion profile properties + #region avatar texture public static STexture GetAvatarTexture(int avatarID) { if (!ICAvatarIDValid(avatarID)) @@ -381,68 +527,138 @@ namespace Vocaluxe.Base return _Profiles[profileID].Avatar.Texture; } - - public static void DeleteProfile(int profileID) - { - lock (_ProfileMutex) - { - if (!ICProfileIDValid(profileID)) - return; - - if (_Profiles[profileID].FileName == "") - { - _RemoveProfile(profileID); - return; - } - - try - { - //Check if profile saved in config - for (int i = 0; i < CConfig.Players.Length; i++) - { - if (CConfig.Players[i] == _Profiles[profileID].FileName) - { - CConfig.Players[i] = string.Empty; - CConfig.SaveConfig(); - } - } - File.Delete(_Profiles[profileID].FileName); - _RemoveProfile(profileID); - - //Check if profile is selected in game - for (int i = 0; i < CGame.Players.Length; i++) - { - if (CGame.Players[i].ProfileID > profileID) - CGame.Players[i].ProfileID--; - else if (CGame.Players[i].ProfileID == profileID) - CGame.Players[i].ProfileID = -1; - } - } - catch (Exception) - { - CLog.LogError("Can't delete Profile File " + _Profiles[profileID].FileName + ".xml"); - } - } - } + #endregion avatar texture #region private methods - private static void _Notification() + private static void _LoadProfiles() { - lock (_Callbacks) + _LoadAvatars(); + + List knownFiles = new List(); + if (_Profiles.Count > 0) { - for (int i = 0; i < _Callbacks.Count; i++) + int[] ids = new int[_Profiles.Keys.Count]; + _Profiles.Keys.CopyTo(ids, 0); + for (int i = 0; i < ids.Length; i++) { - try + if (_Profiles[ids[i]].LoadProfile()) { - _Callbacks[i](); + _Profiles[ids[i]].Avatar = _GetAvatar(_Profiles[ids[i]].AvatarFileName); + knownFiles.Add(Path.GetFileName(_Profiles[ids[i]].FileName)); } - catch (Exception) + else + _Profiles.Remove(ids[i]); + } + } + + List files = new List(); + files.AddRange(CHelper.ListFiles(CSettings.FolderProfiles, "*.xml", true, true)); + + foreach (string file in files) + { + if (knownFiles.Contains(Path.GetFileName(file))) + continue; + + CProfile profile = new CProfile + { + FileName = Path.Combine(CSettings.FolderProfiles, file) + }; + + if (profile.LoadProfile()) + { + profile.Avatar = _GetAvatar(profile.AvatarFileName); + profile.ID = _ProfileIDs.Dequeue(); + _Profiles.Add(profile.ID, profile); + } + } + _ProfilesChanged = true; + } + + private static void _LoadAvatars() + { + List knownFiles = new List(); + if (_Avatars.Count > 0) + { + int[] ids = new int[_Avatars.Keys.Count]; + _Avatars.Keys.CopyTo(ids, 0); + for (int i = 0; i < ids.Length; i++) + { + if (_Avatars[ids[i]].Reload()) + knownFiles.Add(Path.GetFileName(_Avatars[ids[i]].FileName)); + else + _Avatars.Remove(ids[i]); + } + } + + List files = new List(); + files.AddRange(CHelper.ListFiles(CSettings.FolderProfiles, "*.png", true, true)); + files.AddRange(CHelper.ListFiles(CSettings.FolderProfiles, "*.jpg", true, true)); + files.AddRange(CHelper.ListFiles(CSettings.FolderProfiles, "*.jpeg", true, true)); + files.AddRange(CHelper.ListFiles(CSettings.FolderProfiles, "*.bmp", true, true)); + + foreach (string file in files) + { + if (knownFiles.Contains(Path.GetFileName(file))) + continue; + + CAvatar avatar = new CAvatar(-1); + if (avatar.LoadFromFile(file)) + { + avatar.ID = _AvatarIDs.Dequeue(); + _Avatars.Add(avatar.ID, avatar); + } + } + _ProfilesChanged = true; + } + + private static void _DeleteProfile(int profileID) + { + if (!ICProfileIDValid(profileID)) + return; + + if (_Profiles[profileID].FileName == "") + { + _RemoveProfile(profileID); + return; + } + + try + { + //Check if profile saved in config + for (int i = 0; i < CConfig.Players.Length; i++) + { + if (CConfig.Players[i] == _Profiles[profileID].FileName) { - _Callbacks.RemoveAt(i); + CConfig.Players[i] = string.Empty; + CConfig.SaveConfig(); } } - + File.Delete(_Profiles[profileID].FileName); + _RemoveProfile(profileID); + + //Check if profile is selected in game + for (int i = 0; i < CGame.Players.Length; i++) + { + if (CGame.Players[i].ProfileID > profileID) + CGame.Players[i].ProfileID--; + else if (CGame.Players[i].ProfileID == profileID) + CGame.Players[i].ProfileID = -1; + } } + catch (Exception) + { + CLog.LogError("Can't delete Profile File " + _Profiles[profileID].FileName + ".xml"); + } + _ProfilesChanged = true; + } + + private static void _RemoveProfile(int profileID) + { + if (!ICProfileIDValid(profileID)) + return; + + _Profiles.Remove(profileID); + _ProfilesChanged = true; } private static int _CompareByPlayerName(CProfile a, CProfile b) @@ -452,25 +668,13 @@ namespace Vocaluxe.Base private static CAvatar _GetAvatar(string fileName) { - lock (_AvatarMutex) + foreach (int id in _Avatars.Keys) { - foreach (int id in _Avatars.Keys) - { - if (Path.GetFileName(_Avatars[id].FileName) == fileName) - return _Avatars[id]; - } + if (Path.GetFileName(_Avatars[id].FileName) == fileName) + return _Avatars[id]; } return new CAvatar(-1); } - - private static void _RemoveProfile(int profileID) - { - if (!ICProfileIDValid(profileID)) - return; - - _Profiles.Remove(profileID); - _Notification(); - } #endregion private methods } } \ No newline at end of file diff --git a/Vocaluxe/Base/CWebcam.cs b/Vocaluxe/Base/CWebcam.cs index bfe48e78..c30787f1 100644 --- a/Vocaluxe/Base/CWebcam.cs +++ b/Vocaluxe/Base/CWebcam.cs @@ -91,5 +91,10 @@ namespace Vocaluxe.Base { return _Webcam.IsDeviceAvailable(); } + + public static bool IsCapturing() + { + return _Webcam.IsCapturing(); + } } } \ No newline at end of file diff --git a/Vocaluxe/Base/Server/CVocaluxeServer.cs b/Vocaluxe/Base/Server/CVocaluxeServer.cs index a20201cf..b9bc7b99 100644 --- a/Vocaluxe/Base/Server/CVocaluxeServer.cs +++ b/Vocaluxe/Base/Server/CVocaluxeServer.cs @@ -12,6 +12,7 @@ using ClientServerLib; using Vocaluxe.Base; using Vocaluxe.Lib.Input; using VocaluxeLib; +using VocaluxeLib.Profile; namespace Vocaluxe.Base.Server { @@ -156,12 +157,20 @@ namespace Vocaluxe.Base.Server while (File.Exists(filename + i + ".jpg")) i++; - FileStream fs = File.Create(filename + i + ".jpg"); + filename = filename + i + ".jpg"; + FileStream fs = File.Create(filename); fs.Write(avatarPictureJpg.data, 0, avatarPictureJpg.data.Length); fs.Flush(); fs.Close(); - success = true; - CProfiles.LoadProfiles(); + + CAvatar avatar = new CAvatar(-1); + success = avatar.LoadFromFile(filename); + + if (success) + { + CProfiles.AddAvatar(avatar); + } + } catch {} diff --git a/Vocaluxe/Lib/Webcam/CAForgeNet.cs b/Vocaluxe/Lib/Webcam/CAForgeNet.cs index 614a90c7..78d54321 100644 --- a/Vocaluxe/Lib/Webcam/CAForgeNet.cs +++ b/Vocaluxe/Lib/Webcam/CAForgeNet.cs @@ -164,6 +164,11 @@ namespace Vocaluxe.Lib.Webcam return _Devices.Count > 0; } + public bool IsCapturing() + { + return _Webcam.IsRunning; + } + public bool Select(SWebcamConfig config) { //Close old camera connection diff --git a/Vocaluxe/Lib/Webcam/IWebcam.cs b/Vocaluxe/Lib/Webcam/IWebcam.cs index 75eb3fe4..75f33716 100644 --- a/Vocaluxe/Lib/Webcam/IWebcam.cs +++ b/Vocaluxe/Lib/Webcam/IWebcam.cs @@ -59,6 +59,7 @@ namespace Vocaluxe.Lib.Webcam SWebcamConfig GetConfig(); SWebcamDevice[] GetDevices(); bool IsDeviceAvailable(); + bool IsCapturing(); bool Select(SWebcamConfig webcamConfig); } diff --git a/Vocaluxe/Program.cs b/Vocaluxe/Program.cs index ea384764..d00b8d67 100644 --- a/Vocaluxe/Program.cs +++ b/Vocaluxe/Program.cs @@ -197,15 +197,23 @@ namespace Vocaluxe CController.Connect(); CLog.StopBenchmark(0, "Init Input"); + Application.DoEvents(); + // Init Game; CLog.StartBenchmark(0, "Init Game"); CGame.Init(); + CProfiles.Update(); + CConfig.UsePlayers(); CLog.StopBenchmark(0, "Init Game"); + Application.DoEvents(); + // Init Party Modes; CLog.StartBenchmark(0, "Init Party Modes"); CParty.Init(); CLog.StopBenchmark(0, "Init Party Modes"); + + Application.DoEvents(); } catch (Exception e) { diff --git a/Vocaluxe/Screens/CScreenProfiles.cs b/Vocaluxe/Screens/CScreenProfiles.cs index 8eea3b29..26b43d12 100644 --- a/Vocaluxe/Screens/CScreenProfiles.cs +++ b/Vocaluxe/Screens/CScreenProfiles.cs @@ -60,9 +60,10 @@ namespace Vocaluxe.Screens private const string _ButtonTakeSnapshot = "ButtonTakeSnapshot"; private const string _StaticAvatar = "StaticAvatar"; + private bool _ProfilesChanged; + private bool _AvatarsChanged; private EEditMode _EditMode; - private bool _ProfilesChanged; private STexture _WebcamTexture = new STexture(-1); private Bitmap _Snapshot; @@ -77,7 +78,9 @@ namespace Vocaluxe.Screens _ThemeStatics = new string[] {_StaticAvatar}; _EditMode = EEditMode.None; - CProfiles.AddNotificationCallback(_OnProfilesChanged); + _ProfilesChanged = false; + _AvatarsChanged = false; + CProfiles.AddProfileChangedCallback(new ProfileChangedCallback(_OnProfileChanged)); } public override void LoadTheme(string xmlPath) @@ -108,6 +111,7 @@ namespace Vocaluxe.Screens case EEditMode.PlayerName: _SelectSlides[_SelectSlideProfiles].RenameValue( CProfiles.AddGetPlayerName(_SelectSlides[_SelectSlideProfiles].ValueIndex, keyEvent.Unicode)); + _ProfilesChanged = true; break; } } @@ -116,7 +120,10 @@ namespace Vocaluxe.Screens switch (keyEvent.Key) { case Keys.Escape: - CGraphics.FadeTo(EScreens.ScreenMain); + if (_EditMode == EEditMode.PlayerName) + _EditMode = EEditMode.None; + else + CGraphics.FadeTo(EScreens.ScreenMain); break; case Keys.Enter: @@ -150,6 +157,7 @@ namespace Vocaluxe.Screens { _SelectSlides[_SelectSlideProfiles].RenameValue( CProfiles.GetDeleteCharInPlayerName(_SelectSlides[_SelectSlideProfiles].ValueIndex)); + _ProfilesChanged = true; } else CGraphics.FadeTo(EScreens.ScreenMain); @@ -243,6 +251,64 @@ namespace Vocaluxe.Screens return true; } + public override bool UpdateGame() + { + if (_AvatarsChanged) + _LoadAvatars(true); + + if (_ProfilesChanged) + _LoadProfiles(true); + + if (_SelectSlides[_SelectSlideProfiles].Selection > -1) + { + _Buttons[_ButtonPlayerName].Text.Text = CProfiles.GetPlayerName(_SelectSlides[_SelectSlideProfiles].ValueIndex); + if (_EditMode == EEditMode.PlayerName) + _Buttons[_ButtonPlayerName].Text.Text += "|"; + + _SelectSlides[_SelectSlideDifficulty].Selection = (int)CProfiles.GetDifficulty(_SelectSlides[_SelectSlideProfiles].ValueIndex); + _SelectSlides[_SelectSlideGuestProfile].Selection = (int)CProfiles.GetGuestProfile(_SelectSlides[_SelectSlideProfiles].ValueIndex); + _SelectSlides[_SelectSlideActive].Selection = (int)CProfiles.GetActive(_SelectSlides[_SelectSlideProfiles].ValueIndex); + + int avatarID = CProfiles.GetAvatarID(_SelectSlides[_SelectSlideProfiles].ValueIndex); + _SelectSlides[_SelectSlideAvatars].SetSelectionByValueIndex(avatarID); + if (CWebcam.IsDeviceAvailable() && CWebcam.IsCapturing()) + { + if (_Snapshot == null) + CWebcam.GetFrame(ref _WebcamTexture); + + _Statics[_StaticAvatar].Texture = _WebcamTexture; + } + else + _Statics[_StaticAvatar].Texture = CProfiles.GetAvatarTexture(avatarID); + } + + return true; + } + + public override void OnShow() + { + base.OnShow(); + _LoadAvatars(false); + _LoadProfiles(false); + UpdateGame(); + } + + public override void OnClose() + { + base.OnClose(); + _EditMode = EEditMode.None; + _OnDiscardSnapshot(); + } + + private void _OnProfileChanged(EProfileChangedFlags Flags) + { + if (EProfileChangedFlags.Avatar == (EProfileChangedFlags.Avatar & Flags)) + _AvatarsChanged = true; + + if (EProfileChangedFlags.Profile == (EProfileChangedFlags.Profile & Flags)) + _ProfilesChanged = true; + } + private void _OnTakeSnapshot() { _Buttons[_ButtonSaveSnapshot].Visible = true; @@ -265,31 +331,26 @@ namespace Vocaluxe.Screens private void _OnSaveSnapshot() { - const string filename = "snapshot"; + string filename = Path.Combine(CSettings.FolderProfiles, "snapshot"); int i = 0; - while (File.Exists(Path.Combine(CSettings.FolderProfiles, filename + i + ".png"))) + while (File.Exists(filename + i + ".png")) i++; - _Snapshot.Save(Path.Combine(CSettings.FolderProfiles, filename + i + ".png"), ImageFormat.Png); - CProfiles.LoadAvatars(); - _LoadAvatars(false); + + filename = filename + i + ".png"; + _Snapshot.Save(filename, ImageFormat.Png); + _Snapshot = null; CWebcam.Stop(); CDraw.RemoveTexture(ref _WebcamTexture); - CAvatar[] avatars = CProfiles.GetAvatars(); - for (int j = 0; j < avatars.Length; j++) - { - if (Path.GetFileName(avatars[j].FileName) == (filename + i + ".png")) - { - CProfiles.SetAvatar(_SelectSlides[_SelectSlideProfiles].ValueIndex, avatars[j].ID); - break; - } - } - _Buttons[_ButtonSaveSnapshot].Visible = false; _Buttons[_ButtonDiscardSnapshot].Visible = false; _Buttons[_ButtonTakeSnapshot].Visible = false; _Buttons[_ButtonWebcam].Visible = true; + + int id = CProfiles.NewAvatar(filename); + CProfiles.SetAvatar(_SelectSlides[_SelectSlideProfiles].ValueIndex, id); + _LoadAvatars(false); } private void _OnWebcam() @@ -303,83 +364,37 @@ namespace Vocaluxe.Screens _Buttons[_ButtonWebcam].Visible = false; } - public override bool UpdateGame() + private void _NewProfile() { - if (_ProfilesChanged) - { - _LoadAvatars(true); - _LoadProfiles(true); - _ProfilesChanged = false; - } - - if (_SelectSlides[_SelectSlideProfiles].Selection > -1) - { - _Buttons[_ButtonPlayerName].Text.Text = CProfiles.GetPlayerName(_SelectSlides[_SelectSlideProfiles].ValueIndex); - if (_EditMode == EEditMode.PlayerName) - _Buttons[_ButtonPlayerName].Text.Text += "|"; - - _SelectSlides[_SelectSlideDifficulty].Selection = (int)CProfiles.GetDifficulty(_SelectSlides[_SelectSlideProfiles].ValueIndex); - _SelectSlides[_SelectSlideGuestProfile].Selection = (int)CProfiles.GetGuestProfile(_SelectSlides[_SelectSlideProfiles].ValueIndex); - _SelectSlides[_SelectSlideActive].Selection = (int)CProfiles.GetActive(_SelectSlides[_SelectSlideProfiles].ValueIndex); - - int avatarID = CProfiles.GetAvatarID(_SelectSlides[_SelectSlideProfiles].ValueIndex); - _SelectSlides[_SelectSlideAvatars].SetSelectionByValueIndex(avatarID); - if (CWebcam.IsDeviceAvailable() && _WebcamTexture.Index != -1) - { - if (_Snapshot == null) - CWebcam.GetFrame(ref _WebcamTexture); - - _Statics[_StaticAvatar].Texture = _WebcamTexture; - } - else - _Statics[_StaticAvatar].Texture = CProfiles.GetAvatarTexture(avatarID); - } - - return true; - } - - public override void OnShow() - { - base.OnShow(); - - CProfiles.LoadProfiles(); - _LoadAvatars(false); + _EditMode = EEditMode.None; + int id = CProfiles.NewProfile(); _LoadProfiles(false); - UpdateGame(); - } + _SelectSlides[_SelectSlideProfiles].SetSelectionByValueIndex(id); - public override void OnClose() - { - base.OnClose(); + CProfiles.SetAvatar(_SelectSlides[_SelectSlideProfiles].ValueIndex, + _SelectSlides[_SelectSlideAvatars].ValueIndex); - _OnDiscardSnapshot(); - } - - private void _OnProfilesChanged() - { - _ProfilesChanged = true; + _SetInteractionToButton(_Buttons[_ButtonPlayerName]); + _EditMode = EEditMode.PlayerName; } private void _SaveProfiles() { _EditMode = EEditMode.None; CProfiles.SaveProfiles(); - _LoadProfiles(true); - UpdateGame(); } private void _DeleteProfile() { _EditMode = EEditMode.None; - int selection = _SelectSlides[_SelectSlideProfiles].Selection; + CProfiles.DeleteProfile(_SelectSlides[_SelectSlideProfiles].ValueIndex); - _LoadProfiles(false); - if (_SelectSlides[_SelectSlideProfiles].NumValues > selection) - _SelectSlides[_SelectSlideProfiles].Selection = selection; + int selection = _SelectSlides[_SelectSlideProfiles].Selection; + if (_SelectSlides[_SelectSlideProfiles].NumValues - 1 > selection) + _SelectSlides[_SelectSlideProfiles].Selection = selection + 1; else _SelectSlides[_SelectSlideProfiles].Selection = selection - 1; - UpdateGame(); } private void _LoadProfiles(bool Keep) @@ -401,7 +416,13 @@ namespace Vocaluxe.Screens if (CProfiles.NumProfiles > 0 && CProfiles.NumAvatars > 0) { - _SelectSlides[_SelectSlideProfiles].SetSelectionByValueIndex(selectedProfileID); + if (selectedProfileID != -1) + _SelectSlides[_SelectSlideProfiles].SetSelectionByValueIndex(selectedProfileID); + else + { + _SelectSlides[_SelectSlideProfiles].Selection = 0; + selectedProfileID = _SelectSlides[_SelectSlideProfiles].ValueIndex; + } if (!Keep) { @@ -414,6 +435,7 @@ namespace Vocaluxe.Screens if (_EditMode == EEditMode.PlayerName) CProfiles.SetPlayerName(_SelectSlides[_SelectSlideProfiles].ValueIndex, name); } + _ProfilesChanged = false; } private void _LoadAvatars(bool Keep) @@ -435,20 +457,8 @@ namespace Vocaluxe.Screens } else _SelectSlides[_SelectSlideAvatars].SetSelectionByValueIndex(CProfiles.GetAvatarID(_SelectSlides[_SelectSlideProfiles].ValueIndex)); - } - private void _NewProfile() - { - _EditMode = EEditMode.None; - int id = CProfiles.NewProfile(); - _LoadProfiles(false); - _SelectSlides[_SelectSlideProfiles].SetSelectionByValueIndex(id); - - CProfiles.SetAvatar(_SelectSlides[_SelectSlideProfiles].ValueIndex, - _SelectSlides[_SelectSlideAvatars].ValueIndex); - - _SetInteractionToButton(_Buttons[_ButtonPlayerName]); - _EditMode = EEditMode.PlayerName; - } + _AvatarsChanged = false; + } } } \ No newline at end of file diff --git a/VocaluxeLib/Profile/CProfile.cs b/VocaluxeLib/Profile/CProfile.cs index f4ed3efd..c4b9b0ac 100644 --- a/VocaluxeLib/Profile/CProfile.cs +++ b/VocaluxeLib/Profile/CProfile.cs @@ -23,7 +23,17 @@ namespace VocaluxeLib.Profile public string AvatarFileName; public EGameDifficulty Difficulty; - public CAvatar Avatar; + + private CAvatar _Avatar; + public CAvatar Avatar + { + get { return _Avatar; } + set + { + _Avatar = value; + AvatarFileName = _Avatar.FileName; + } + } public EOffOn GuestProfile; public EOffOn Active;