diff --git a/Vocaluxe/Screens/CScreenHighscore.cs b/Vocaluxe/Screens/CScreenHighscore.cs index 01848911..010a3cce 100644 --- a/Vocaluxe/Screens/CScreenHighscore.cs +++ b/Vocaluxe/Screens/CScreenHighscore.cs @@ -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); } } diff --git a/Vocaluxe/Screens/CScreenScore.cs b/Vocaluxe/Screens/CScreenScore.cs index db70f56e..39754156 100644 --- a/Vocaluxe/Screens/CScreenScore.cs +++ b/Vocaluxe/Screens/CScreenScore.cs @@ -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(); } diff --git a/VocaluxeLib/CHelper.cs b/VocaluxeLib/CHelper.cs index 2edec3ce..411d39ec 100644 --- a/VocaluxeLib/CHelper.cs +++ b/VocaluxeLib/CHelper.cs @@ -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; } + /// + /// Makes sure val is between min and max + /// Asserts that min<=max + /// + /// + /// + /// + /// + /// Clamped value public static T Clamp(this T val, T min, T max) where T : IComparable { + 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; } + /// + /// Makes sure val is between min and max but also handles the case where min>max + /// + /// + /// + /// + /// + /// + /// Clamped value + public static T Clamp(this T val, T min, T max, bool preferMin) where T : IComparable + { + if (min.CompareTo(max) > 0) + { + if (preferMin) + max = min; + else + min = max; + } + return Clamp(val, min, max); + } + /// /// Concat strings into one string with ", " as separator. /// @@ -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 /// /// Path in which the file should be stored /// filename (including extension) + /// Whether the fullpath or only the filename should be returned /// 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; diff --git a/VocaluxeLib/Menu/SongMenu/CSongMenuTileBoard.cs b/VocaluxeLib/Menu/SongMenu/CSongMenuTileBoard.cs index cc675587..e6c0b94e 100644 --- a/VocaluxeLib/Menu/SongMenu/CSongMenuTileBoard.cs +++ b/VocaluxeLib/Menu/SongMenu/CSongMenuTileBoard.cs @@ -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;