Fix NPE with few songs in category
This commit is contained in:
1 parent
09d36ccf60
commit
ced12ac064
4 files changed
+38
-37
No files matched your search
@@ -284,7 +284,7 @@ namespace Vocaluxe.Screens
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
_Pos += num;
|
_Pos += num;
|
||||||
_Pos = _Pos.Clamp(0, _Scores[_Round].Count - 1);
|
_Pos = _Pos.Clamp(0, _Scores[_Round].Count - 1, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -396,12 +396,8 @@ namespace Vocaluxe.Screens
|
|||||||
|
|
||||||
private void _ChangeRound(int num)
|
private void _ChangeRound(int num)
|
||||||
{
|
{
|
||||||
if (_Points.NumRounds < 1)
|
|
||||||
return;
|
|
||||||
_Round += num;
|
_Round += num;
|
||||||
_Round = _Round.Clamp(-1, _Points.NumRounds - 1);
|
_Round = _Round.Clamp(-1, _Points.NumRounds - 1);
|
||||||
if (_Round >= _Points.NumRounds)
|
|
||||||
_Round = _Points.NumRounds - 1;
|
|
||||||
|
|
||||||
_UpdateRatings();
|
_UpdateRatings();
|
||||||
}
|
}
|
||||||
|
|||||||
+34
-29
@@ -17,6 +17,7 @@
|
|||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics;
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
@@ -63,8 +64,18 @@ namespace VocaluxeLib
|
|||||||
return (int)result;
|
return (int)result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Makes sure val is between min and max
|
||||||
|
/// Asserts that min<=max
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T"></typeparam>
|
||||||
|
/// <param name="val"></param>
|
||||||
|
/// <param name="min"></param>
|
||||||
|
/// <param name="max"></param>
|
||||||
|
/// <returns>Clamped value</returns>
|
||||||
public static T Clamp<T>(this T val, T min, T max) where T : IComparable<T>
|
public static T Clamp<T>(this T val, T min, T max) where T : IComparable<T>
|
||||||
{
|
{
|
||||||
|
Debug.Assert(min.CompareTo(max) <= 0);
|
||||||
if (val.CompareTo(min) < 0)
|
if (val.CompareTo(min) < 0)
|
||||||
return min;
|
return min;
|
||||||
if (val.CompareTo(max) > 0)
|
if (val.CompareTo(max) > 0)
|
||||||
@@ -72,6 +83,27 @@ namespace VocaluxeLib
|
|||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Makes sure val is between min and max but also handles the case where min>max
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T"></typeparam>
|
||||||
|
/// <param name="val"></param>
|
||||||
|
/// <param name="min"></param>
|
||||||
|
/// <param name="max"></param>
|
||||||
|
/// <param name="preferMin"></param>
|
||||||
|
/// <returns>Clamped value</returns>
|
||||||
|
public static T Clamp<T>(this T val, T min, T max, bool preferMin) where T : IComparable<T>
|
||||||
|
{
|
||||||
|
if (min.CompareTo(max) > 0)
|
||||||
|
{
|
||||||
|
if (preferMin)
|
||||||
|
max = min;
|
||||||
|
else
|
||||||
|
min = max;
|
||||||
|
}
|
||||||
|
return Clamp(val, min, max);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Concat strings into one string with ", " as separator.
|
/// Concat strings into one string with ", " as separator.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -88,32 +120,6 @@ namespace VocaluxeLib
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int TryReadInt(StreamReader sr)
|
|
||||||
{
|
|
||||||
string value = String.Empty;
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
int tmp = sr.Peek();
|
|
||||||
//Check for ' ', ?, ?, \n, \r, E
|
|
||||||
while (tmp != 32 && tmp != 19 && tmp != 16 && tmp != 13 && tmp != 10 && tmp != 69)
|
|
||||||
{
|
|
||||||
if (sr.EndOfStream)
|
|
||||||
break;
|
|
||||||
|
|
||||||
var chr = (char)sr.Read();
|
|
||||||
value += chr.ToString();
|
|
||||||
tmp = sr.Peek();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
int result;
|
|
||||||
return Int32.TryParse(value, out result) ? result : 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void SetRect(SRectF bounds, out SRectF rect, float rectAspect, EAspect aspect)
|
public static void SetRect(SRectF bounds, out SRectF rect, float rectAspect, EAspect aspect)
|
||||||
{
|
{
|
||||||
var bounds2 = new RectangleF(bounds.X, bounds.Y, bounds.W, bounds.H);
|
var bounds2 = new RectangleF(bounds.X, bounds.Y, bounds.W, bounds.H);
|
||||||
@@ -279,13 +285,12 @@ namespace VocaluxeLib
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="path">Path in which the file should be stored</param>
|
/// <param name="path">Path in which the file should be stored</param>
|
||||||
/// <param name="filename">filename (including extension)</param>
|
/// <param name="filename">filename (including extension)</param>
|
||||||
|
/// <param name="withPath">Whether the fullpath or only the filename should be returned</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static string GetUniqueFileName(string path, string filename, bool withPath = true)
|
public static string GetUniqueFileName(string path, string filename, bool withPath = true)
|
||||||
{
|
{
|
||||||
string ext = Path.GetExtension(filename);
|
string ext = Path.GetExtension(filename);
|
||||||
filename = Path.GetFileNameWithoutExtension(filename);
|
filename = Path.GetFileNameWithoutExtension(filename) ?? "1";
|
||||||
if (filename == null)
|
|
||||||
filename = "1";
|
|
||||||
if (File.Exists(Path.Combine(path, filename + ext)))
|
if (File.Exists(Path.Combine(path, filename + ext)))
|
||||||
{
|
{
|
||||||
int i = 1;
|
int i = 1;
|
||||||
|
|||||||
@@ -72,7 +72,7 @@ namespace VocaluxeLib.Menu.SongMenu
|
|||||||
set
|
set
|
||||||
{
|
{
|
||||||
int max = CBase.Songs.IsInCategory() ? CBase.Songs.GetNumSongsVisible() : CBase.Songs.GetNumCategories();
|
int max = CBase.Songs.IsInCategory() ? CBase.Songs.GetNumSongsVisible() : CBase.Songs.GetNumCategories();
|
||||||
base._SelectionNr = value.Clamp(-1, max - 1);
|
base._SelectionNr = value.Clamp(-1, max - 1, true);
|
||||||
//Update list in case we scrolled
|
//Update list in case we scrolled
|
||||||
_UpdateList();
|
_UpdateList();
|
||||||
|
|
||||||
@@ -544,7 +544,7 @@ namespace VocaluxeLib.Menu.SongMenu
|
|||||||
|
|
||||||
private int _GetOffsetForPosition(int index, int rowNum = 0)
|
private int _GetOffsetForPosition(int index, int rowNum = 0)
|
||||||
{
|
{
|
||||||
return (index / _NumW) * _NumW - (_NumW * rowNum.Clamp(0, _NumH - 1));
|
return (index / _NumW) * _NumW - (_NumW * rowNum.Clamp(0, _NumH - 1, true));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void _UpdateList(bool force = false)
|
private void _UpdateList(bool force = false)
|
||||||
@@ -564,7 +564,7 @@ namespace VocaluxeLib.Menu.SongMenu
|
|||||||
bool isInCategory = CBase.Songs.IsInCategory();
|
bool isInCategory = CBase.Songs.IsInCategory();
|
||||||
int itemCount = isInCategory ? CBase.Songs.GetNumSongsVisible() : CBase.Songs.GetNumCategories();
|
int itemCount = isInCategory ? CBase.Songs.GetNumSongsVisible() : CBase.Songs.GetNumCategories();
|
||||||
|
|
||||||
offset = offset.Clamp(0, _GetOffsetForPosition(itemCount, _NumH - 1));
|
offset = offset.Clamp(0, _GetOffsetForPosition(itemCount, _NumH - 1), true);
|
||||||
|
|
||||||
if (offset == _Offset && !force)
|
if (offset == _Offset && !force)
|
||||||
return;
|
return;
|
||||||
|
|||||||
Reference in new issue
Block a user