Fix NPE with few songs in category

This commit is contained in:
Flamefire committed 2014-08-09 15:27:05 +02:00
1 parent 09d36ccf60
commit ced12ac064
4 files changed
+38 -37

No files matched your search

+1 -1
View File
@@ -284,7 +284,7 @@ namespace Vocaluxe.Screens
else
{
_Pos += num;
_Pos = _Pos.Clamp(0, _Scores[_Round].Count - 1);
_Pos = _Pos.Clamp(0, _Scores[_Round].Count - 1, true);
}
}
-4
View File
@@ -396,12 +396,8 @@ namespace Vocaluxe.Screens
private void _ChangeRound(int num)
{
if (_Points.NumRounds < 1)
return;
_Round += num;
_Round = _Round.Clamp(-1, _Points.NumRounds - 1);
if (_Round >= _Points.NumRounds)
_Round = _Points.NumRounds - 1;
_UpdateRatings();
}
+34 -29
View File
@@ -17,6 +17,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.IO;
@@ -63,8 +64,18 @@ namespace VocaluxeLib
return (int)result;
}
/// <summary>
/// Makes sure val is between min and max
/// Asserts that min&lt;=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>
{
Debug.Assert(min.CompareTo(max) <= 0);
if (val.CompareTo(min) < 0)
return min;
if (val.CompareTo(max) > 0)
@@ -72,6 +83,27 @@ namespace VocaluxeLib
return val;
}
/// <summary>
/// Makes sure val is between min and max but also handles the case where min&gt;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>
/// Concat strings into one string with ", " as separator.
/// </summary>
@@ -88,32 +120,6 @@ namespace VocaluxeLib
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)
{
var bounds2 = new RectangleF(bounds.X, bounds.Y, bounds.W, bounds.H);
@@ -279,13 +285,12 @@ namespace VocaluxeLib
/// </summary>
/// <param name="path">Path in which the file should be stored</param>
/// <param name="filename">filename (including extension)</param>
/// <param name="withPath">Whether the fullpath or only the filename should be returned</param>
/// <returns></returns>
public static string GetUniqueFileName(string path, string filename, bool withPath = true)
{
string ext = Path.GetExtension(filename);
filename = Path.GetFileNameWithoutExtension(filename);
if (filename == null)
filename = "1";
filename = Path.GetFileNameWithoutExtension(filename) ?? "1";
if (File.Exists(Path.Combine(path, filename + ext)))
{
int i = 1;
@@ -72,7 +72,7 @@ namespace VocaluxeLib.Menu.SongMenu
set
{
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
_UpdateList();
@@ -544,7 +544,7 @@ namespace VocaluxeLib.Menu.SongMenu
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)
@@ -564,7 +564,7 @@ namespace VocaluxeLib.Menu.SongMenu
bool isInCategory = CBase.Songs.IsInCategory();
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)
return;