Converted SSkinElement to a class (no pointless copies)

This commit is contained in:
Flamefire committed 2013-05-22 00:44:13 +02:00
1 parent ac8b7dc940
commit 284b46819f
1 file changed
+59 -92
+59 -92
View File
@@ -44,13 +44,23 @@ namespace Vocaluxe.Base
public int PartyModeID; public int PartyModeID;
} }
struct SSkinElement class CSkinElement
{ {
public string Name; public readonly string Name;
public string Value; public string Value;
public CTexture Texture; public CTexture Texture;
public int VideoIndex;
public CSkinElement(string name)
{
Name = name;
}
}
class CVideoSkinElement : CSkinElement
{
public int VideoIndex = -1;
public CVideoSkinElement(string name) : base(name) {}
} }
struct SSkin struct SSkin
@@ -64,8 +74,8 @@ namespace Vocaluxe.Base
public string FileName; public string FileName;
public int PartyModeID; public int PartyModeID;
public Dictionary<string, SSkinElement> SkinList; public Dictionary<string, CSkinElement> SkinList;
public List<SSkinElement> VideoList; public List<CVideoSkinElement> VideoList;
public SColors ThemeColors; public SColors ThemeColors;
} }
@@ -172,45 +182,33 @@ namespace Vocaluxe.Base
if (xmlReader == null) if (xmlReader == null)
return false; return false;
string value;
// load skins/textures // load skins/textures
List<string> keys = new List<string>(_Skins[skinIndex].SkinList.Keys); foreach (CSkinElement skinElement in _Skins[skinIndex].SkinList.Values)
foreach (string name in keys)
{ {
try try
{ {
xmlReader.GetValue("//root/Skins/" + name, out value, String.Empty); xmlReader.GetValue("//root/Skins/" + skinElement.Name, out skinElement.Value, String.Empty);
SSkinElement sk = _Skins[skinIndex].SkinList[name]; skinElement.Texture = CDraw.AddTexture(Path.Combine(_Skins[skinIndex].Path, skinElement.Value));
sk.Value = value;
sk.VideoIndex = -1;
sk.Texture = CDraw.AddTexture(Path.Combine(_Skins[skinIndex].Path, sk.Value));
_Skins[skinIndex].SkinList[name] = sk;
} }
catch (Exception e) catch (Exception e)
{ {
MessageBox.Show("Error on loading texture \"" + name + "\": " + e.Message + e.StackTrace); MessageBox.Show("Error on loading texture \"" + skinElement.Name + "\": " + e.Message + e.StackTrace);
CLog.LogError("Error on loading texture \"" + name + "\": " + e.Message + e.StackTrace); CLog.LogError("Error on loading texture \"" + skinElement.Name + "\": " + e.Message + e.StackTrace);
} }
} }
// load videos // load videos
for (int i = 0; i < _Skins[skinIndex].VideoList.Count; i++) foreach (CVideoSkinElement videoSkinElement in _Skins[skinIndex].VideoList)
{ {
try try
{ {
xmlReader.GetValue("//root/Videos/" + _Skins[skinIndex].VideoList[i].Name, out value, String.Empty); xmlReader.GetValue("//root/Videos/" + videoSkinElement.Name, out videoSkinElement.Value, String.Empty);
//Do not load video yet
SSkinElement sk = new SSkinElement {Name = _Skins[skinIndex].VideoList[i].Name, Value = value, VideoIndex = -1, Texture = null};
_Skins[skinIndex].VideoList[i] = sk;
} }
catch (Exception e) catch (Exception e)
{ {
MessageBox.Show("Error on loading video \"" + _Skins[skinIndex].VideoList[i].Name + "\": " + e.Message + e.StackTrace); MessageBox.Show("Error on loading video \"" + videoSkinElement.Name + "\": " + e.Message + e.StackTrace);
CLog.LogError("Error on loading video \"" + _Skins[skinIndex].VideoList[i].Name + "\": " + e.Message + e.StackTrace); CLog.LogError("Error on loading video \"" + videoSkinElement.Name + "\": " + e.Message + e.StackTrace);
} }
} }
@@ -224,16 +222,16 @@ namespace Vocaluxe.Base
int numPartyModeSkins = 0; int numPartyModeSkins = 0;
for (int i = 0; i < _Skins.Count; i++) for (int i = 0; i < _Skins.Count; i++)
{ {
foreach (SSkinElement sk in _Skins[i].SkinList.Values) foreach (CSkinElement sk in _Skins[i].SkinList.Values)
{ {
CTexture texture = sk.Texture; CTexture texture = sk.Texture;
CDraw.RemoveTexture(ref texture); CDraw.RemoveTexture(ref texture);
} }
for (int j = 0; j < _Skins[i].VideoList.Count; j++) foreach (CVideoSkinElement vsk in _Skins[i].VideoList)
{ {
CVideo.Close(_Skins[i].VideoList[j].VideoIndex); CVideo.Close(vsk.VideoIndex);
CTexture videoTexture = _Skins[i].VideoList[j].Texture; CTexture videoTexture = vsk.Texture;
CDraw.RemoveTexture(ref videoTexture); CDraw.RemoveTexture(ref videoTexture);
} }
@@ -380,7 +378,7 @@ namespace Vocaluxe.Base
#region Skins #region Skins
writer.WriteStartElement("Skins"); writer.WriteStartElement("Skins");
foreach (SSkinElement element in _Skins[skinIndex].SkinList.Values) foreach (CSkinElement element in _Skins[skinIndex].SkinList.Values)
writer.WriteElementString(element.Name, element.Value); writer.WriteElementString(element.Name, element.Value);
writer.WriteEndElement(); writer.WriteEndElement();
#endregion Skins #endregion Skins
@@ -388,7 +386,7 @@ namespace Vocaluxe.Base
#region Videos #region Videos
writer.WriteStartElement("Videos"); writer.WriteStartElement("Videos");
foreach (SSkinElement element in _Skins[skinIndex].VideoList) foreach (CVideoSkinElement element in _Skins[skinIndex].VideoList)
writer.WriteElementString(element.Name, element.Value); writer.WriteElementString(element.Name, element.Value);
writer.WriteEndElement(); writer.WriteEndElement();
#endregion Videos #endregion Videos
@@ -505,21 +503,15 @@ namespace Vocaluxe.Base
skin.FileName = file; skin.FileName = file;
skin.PartyModeID = theme.PartyModeID; skin.PartyModeID = theme.PartyModeID;
skin.SkinList = new Dictionary<string, SSkinElement>(); skin.SkinList = new Dictionary<string, CSkinElement>();
List<string> names = xmlReader.GetValues("Skins"); List<string> names = xmlReader.GetValues("Skins");
foreach (string str in names) foreach (string str in names)
{ skin.SkinList[str] = new CSkinElement(str);
SSkinElement sk = new SSkinElement {Name = str, Value = String.Empty};
skin.SkinList[str] = sk;
}
skin.VideoList = new List<SSkinElement>(); skin.VideoList = new List<CVideoSkinElement>();
names = xmlReader.GetValues("Videos"); names = xmlReader.GetValues("Videos");
foreach (string str in names) foreach (string str in names)
{ skin.VideoList.Add(new CVideoSkinElement(str));
SSkinElement sk = new SSkinElement {Name = str, Value = String.Empty};
skin.VideoList.Add(sk);
}
_Skins.Add(skin); _Skins.Add(skin);
} }
} }
@@ -584,8 +576,9 @@ namespace Vocaluxe.Base
public static CTexture GetSkinTexture(string textureName, int partyModeID) public static CTexture GetSkinTexture(string textureName, int partyModeID)
{ {
int skinIndex = GetSkinIndex(partyModeID); int skinIndex = GetSkinIndex(partyModeID);
if (skinIndex != -1 && textureName != null && _Skins[skinIndex].SkinList.ContainsKey(textureName)) CSkinElement sk;
return _Skins[skinIndex].SkinList[textureName].Texture; if (skinIndex != -1 && textureName != null && _Skins[skinIndex].SkinList.TryGetValue(textureName, out sk))
return sk.Texture;
return null; return null;
} }
@@ -596,85 +589,59 @@ namespace Vocaluxe.Base
private static string _GetSkinFileName(string skinName, int skinIndex, bool returnPath = false) private static string _GetSkinFileName(string skinName, int skinIndex, bool returnPath = false)
{ {
foreach (SSkinElement sk in _Skins[skinIndex].SkinList.Values) CSkinElement sk;
{ if (_Skins[skinIndex].SkinList.TryGetValue(skinName, out sk))
if (sk.Name == skinName)
return !returnPath ? sk.Value : Path.Combine(_Skins[skinIndex].Path, sk.Value); return !returnPath ? sk.Value : Path.Combine(_Skins[skinIndex].Path, sk.Value);
}
CLog.LogError("Can't find Skin Element \"" + skinName); CLog.LogError("Can't find Skin Element \"" + skinName);
return skinName; return skinName;
} }
public static CTexture GetSkinVideoTexture(string videoName, int partyModeID) public static CVideoSkinElement GetSkinVideo(string videoName, int partyModeID)
{ {
int skinIndex = GetSkinIndex(partyModeID); int skinIndex = GetSkinIndex(partyModeID);
if (skinIndex != -1) if (skinIndex != -1)
{ {
for (int i = 0; i < _Skins[skinIndex].VideoList.Count; i++) foreach (CVideoSkinElement sk in _Skins[skinIndex].VideoList)
{ {
SSkinElement sk = _Skins[skinIndex].VideoList[i];
if (sk.Name == videoName) if (sk.Name == videoName)
{ {
float time = 0f;
if (sk.VideoIndex == -1) if (sk.VideoIndex == -1)
{ {
sk.VideoIndex = CVideo.Load(GetVideoFilePath(sk.Name, partyModeID)); sk.VideoIndex = CVideo.Load(GetVideoFilePath(sk.Name, partyModeID));
CVideo.SetLoop(sk.VideoIndex, true); CVideo.SetLoop(sk.VideoIndex, true);
} }
CVideo.GetFrame(sk.VideoIndex, ref sk.Texture, time, out time); return sk;
_Skins[skinIndex].VideoList[i] = sk;
return sk.Texture;
} }
} }
} }
return null; return null;
} }
public static CTexture GetSkinVideoTexture(string videoName, int partyModeID)
{
CVideoSkinElement sk = GetSkinVideo(videoName, partyModeID);
if (sk == null)
return null;
float time = 0f;
CVideo.GetFrame(sk.VideoIndex, ref sk.Texture, time, out time);
return sk.Texture;
}
public static void SkinVideoPause(string videoName, int partyModeID) public static void SkinVideoPause(string videoName, int partyModeID)
{ {
int skinIndex = GetSkinIndex(partyModeID); CVideoSkinElement sk = GetSkinVideo(videoName, partyModeID);
if (skinIndex != -1) if (sk == null)
{
for (int i = 0; i < _Skins[skinIndex].VideoList.Count; i++)
{
SSkinElement sk = _Skins[skinIndex].VideoList[i];
if (sk.Name == videoName)
{
if (sk.VideoIndex == -1)
{
sk.VideoIndex = CVideo.Load(GetVideoFilePath(sk.Name, partyModeID));
CVideo.SetLoop(sk.VideoIndex, true);
}
CVideo.Pause(sk.VideoIndex);
_Skins[skinIndex].VideoList[i] = sk;
return; return;
} CVideo.Pause(sk.VideoIndex);
}
}
} }
public static void SkinVideoResume(string videoName, int partyModeID) public static void SkinVideoResume(string videoName, int partyModeID)
{ {
int skinIndex = GetSkinIndex(partyModeID); CVideoSkinElement sk = GetSkinVideo(videoName, partyModeID);
if (skinIndex != -1) if (sk == null)
{
for (int i = 0; i < _Skins[skinIndex].VideoList.Count; i++)
{
SSkinElement sk = _Skins[skinIndex].VideoList[i];
if (sk.Name == videoName)
{
if (sk.VideoIndex == -1)
{
sk.VideoIndex = CVideo.Load(GetVideoFilePath(sk.Name, partyModeID));
CVideo.SetLoop(sk.VideoIndex, true);
}
CVideo.Resume(sk.VideoIndex);
_Skins[skinIndex].VideoList[i] = sk;
return; return;
} CVideo.Resume(sk.VideoIndex);
}
}
} }
public static string GetVideoFilePath(string videoName, int partyModeID) public static string GetVideoFilePath(string videoName, int partyModeID)
@@ -684,7 +651,7 @@ namespace Vocaluxe.Base
private static string _GetVideoFileName(string videoName, int skinIndex, bool returnPath = false) private static string _GetVideoFileName(string videoName, int skinIndex, bool returnPath = false)
{ {
foreach (SSkinElement sk in _Skins[skinIndex].VideoList) foreach (CVideoSkinElement sk in _Skins[skinIndex].VideoList)
{ {
if (sk.Name == videoName) if (sk.Name == videoName)
return !returnPath ? sk.Value : Path.Combine(_Skins[skinIndex].Path, sk.Value); return !returnPath ? sk.Value : Path.Combine(_Skins[skinIndex].Path, sk.Value);