Skip to content

Commit

Permalink
Merge pull request #851 from DeinAlptraum/random-list
Browse files Browse the repository at this point in the history
Do not repeat songs when randomizing
  • Loading branch information
barbeque-squared authored Jul 14, 2024
2 parents f2214ab + acaca08 commit f6d812c
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
51 changes: 49 additions & 2 deletions src/screens/UScreenSong.pas
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ interface

type
TVisArr = array of integer;
CardinalArray = array of cardinal;

TScreenSong = class(TMenu)
private
Expand All @@ -77,6 +78,10 @@ TScreenSong = class(TMenu)
LastSelectMouse: integer;
LastSelectTime: integer;

RandomSongOrder: CardinalArray;
NextRandomSongIdx: cardinal;
RandomSearchOrder: CardinalArray;

procedure StartMusicPreview();
procedure StartVideoPreview();
public
Expand Down Expand Up @@ -220,6 +225,8 @@ TScreenSong = class(TMenu)

SongIndex: integer; //Index of Song that is playing since UScreenScore...

NextRandomSearchIdx: cardinal;

constructor Create; override;
procedure SetScroll;
procedure SetScrollRefresh;
Expand Down Expand Up @@ -645,6 +652,24 @@ function TScreenSong.ParseInput(PressedKey: cardinal; CharCode: UCS4Char; Presse
VerifySong, WebList: string;
Fix: boolean;
VS: integer;

function RandomPermute(Num: integer): CardinalArray;
var
Ordered: array of cardinal;
Idx, i: cardinal;
begin
SetLength(Ordered, Num);
SetLength(Result, Num);
for i := 0 to Num-1 do Ordered[i] := i;
for i := 0 to Num-1 do
begin
Idx := Random(Num);
Result[i] := Ordered[Idx];
Delete(Ordered, Idx, 1);
Dec(Num);
end;
end;

begin
Result := true;

Expand Down Expand Up @@ -919,7 +944,6 @@ function TScreenSong.ParseInput(PressedKey: cardinal; CharCode: UCS4Char; Presse

SDLK_R:
begin
Randomize;
if (Songs.SongList.Count > 0) and
(FreeListMode) then
begin
Expand Down Expand Up @@ -987,7 +1011,26 @@ function TScreenSong.ParseInput(PressedKey: cardinal; CharCode: UCS4Char; Presse
end
else // random in one category
begin
SkipTo(Random(CatSongs.VisibleSongs));
if CatSongs.CatNumShow = -2 then
begin
if NextRandomSearchIdx >= CatSongs.VisibleSongs then
begin
NextRandomSearchIdx := 0;
RandomSearchOrder := RandomPermute(CatSongs.VisibleSongs);
end;
SkipTo(RandomSearchOrder[NextRandomSearchIdx]);
Inc(NextRandomSearchIdx);
end
else
begin
if NextRandomSongIdx >= CatSongs.VisibleSongs then
begin
NextRandomSongIdx := 0;
RandomSongOrder := RandomPermute(CatSongs.VisibleSongs);
end;
SkipTo(RandomSongOrder[NextRandomSongIdx]);
Inc(NextRandomSongIdx);
end
end;
AudioPlayback.PlaySound(SoundLib.Change);

Expand Down Expand Up @@ -1095,6 +1138,7 @@ function TScreenSong.ParseInput(PressedKey: cardinal; CharCode: UCS4Char; Presse
begin
//Atm: Set Empty Filter
CatSongs.SetFilter('', fltAll);
NextRandomSearchIdx := CatSongs.VisibleSongs;

//Show Cat in Top Left Mod
HideCatTL;
Expand Down Expand Up @@ -1815,6 +1859,9 @@ constructor TScreenSong.Create;
LastSelectMouse := 0;
LastSelectTime := 0;

NextRandomSongIdx := CatSongs.VisibleSongs;
NextRandomSearchIdx := CatSongs.VisibleSongs;

end;

procedure TScreenSong.ColorDuetNameSingers();
Expand Down
9 changes: 9 additions & 0 deletions src/screens/UScreenSongJumpto.pas
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ function TScreenSongJumpto.ParseInput(PressedKey: cardinal; CharCode: UCS4Char;

Button[0].Text[0].Text := Button[0].Text[0].Text + UCS4ToUTF8String(CharCode);
SetTextFound(CatSongs.SetFilter(Button[0].Text[0].Text, fSelectType));
ScreenSong.NextRandomSearchIdx := CatSongs.VisibleSongs;
end;
end;

Expand All @@ -112,6 +113,7 @@ function TScreenSongJumpto.ParseInput(PressedKey: cardinal; CharCode: UCS4Char;
begin
Button[0].Text[0].DeleteLastLetter();
SetTextFound(CatSongs.SetFilter(Button[0].Text[0].Text, fSelectType));
ScreenSong.NextRandomSearchIdx := CatSongs.VisibleSongs;
end;
end;

Expand All @@ -130,6 +132,7 @@ function TScreenSongJumpto.ParseInput(PressedKey: cardinal; CharCode: UCS4Char;
//ScreenSong.UnLoadDetailedCover;
Button[0].Text[0].Text := '';
CatSongs.SetFilter('', fltAll);
ScreenSong.NextRandomSearchIdx := CatSongs.VisibleSongs;
SetTextFound(0);
end;
end;
Expand All @@ -151,15 +154,21 @@ function TScreenSongJumpto.ParseInput(PressedKey: cardinal; CharCode: UCS4Char;
Interaction := 1;
InteractInc;
if (Length(Button[0].Text[0].Text) > 0) then
begin
SetTextFound(CatSongs.SetFilter(Button[0].Text[0].Text, fSelectType));
ScreenSong.NextRandomSearchIdx := CatSongs.VisibleSongs;
end;
Interaction := 0;
end;
SDLK_LEFT:
begin
Interaction := 1;
InteractDec;
if (Length(Button[0].Text[0].Text) > 0) then
begin
SetTextFound(CatSongs.SetFilter(Button[0].Text[0].Text, fSelectType));
ScreenSong.NextRandomSearchIdx := CatSongs.VisibleSongs;
end;
Interaction := 0;
end;
end;
Expand Down

0 comments on commit f6d812c

Please sign in to comment.