-
Notifications
You must be signed in to change notification settings - Fork 0
/
QueueSystem.h
199 lines (157 loc) · 4.7 KB
/
QueueSystem.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include "vector"
#include "import.h"
#include "iostream"
#include "SDL3_mixer/SDL_mixer.h"
#include <unordered_map>
#include <algorithm>
#include <ranges>
#include <random>
#pragma once
class Queue
{
private:
bool loop = false;
bool queueloop = false;
using UUIDFromMusicBlock = MusicPlayer::UUID;
using UUIDFromQueue = MusicPlayer::UUID;
std::unordered_map<UUIDFromQueue, UUIDFromMusicBlock> mappedIds;
std::vector<UUIDFromQueue> mainqueue;
UUIDFromQueue currentMusic{0};
public:
std::vector<std::pair<MusicPlayer::UUID, MusicPlayer::UUID>> getMainqueue() const
{
std::vector<std::pair<MusicPlayer::UUID, MusicPlayer::UUID>> result(mainqueue.size());
std::transform(mainqueue.begin(), mainqueue.end(), result.begin(),
[this](auto internalId) {
return std::make_pair(internalId, InternalIDToGlobalID(internalId));
});
return result;
}
bool isLoop() const
{
return loop;
}
bool isQueueLoop() const
{
return queueloop;
}
Queue(const std::vector<MusicPlayer::UUID>& imported)
{
for(auto& uuid : imported)
{
AddToEnd(uuid);
}
}
Queue() = default;
MusicPlayer::UUID InternalIDToGlobalID(MusicPlayer::UUID internalId) const
{
return mappedIds.at(internalId);
}
MusicPlayer::UUID GetCurrentMusic()
{
return currentMusic == 0 ? MusicPlayer::UUID{0} : mappedIds[currentMusic];
}
void New_Crt_Mus(MusicPlayer::UUID idFromQueue)
{
currentMusic = idFromQueue;
}
void Next_Song()
{
if (mainqueue.empty())
{
currentMusic = {0};
return;
}
if ((currentMusic == mainqueue[mainqueue.size() - 1]) and queueloop)
{
currentMusic = mainqueue[0];
return;
}
if (currentMusic == mainqueue[mainqueue.size() - 1])
{
std::cout << "End";
currentMusic = {0};
return;
}
auto it = std::find(mainqueue.begin(), mainqueue.end(), currentMusic);
size_t i = std::distance(mainqueue.begin(), it);
currentMusic = mainqueue[i + 1];
}
void Prev_Song()
{
if ((currentMusic == 0) or (currentMusic == mainqueue[0]))
{
return;
}
size_t i = 0;
while (currentMusic != mainqueue[i])
{
i++;
}
currentMusic = mainqueue[i - 1];
}
void Randomize()
{
auto rng = std::default_random_engine {};
std::shuffle(std::begin(mainqueue), std::end(mainqueue), rng);
}
bool IsFirst(MusicPlayer::UUID id)
{
assert(!mainqueue.empty() && "Queue must be not empty");
return id == mainqueue[0];
}
bool IsLast(MusicPlayer::UUID id)
{
assert(!mainqueue.empty() && "Queue must be not empty");
return id == mainqueue.back();
}
void SwapUp(MusicPlayer::UUID id)
{
assert(!mainqueue.empty() && "Queue must be not empty");
auto it = std::find(mainqueue.begin(), mainqueue.end(), id);
if (it == mainqueue.end())
throw std::runtime_error("Swap not successful: element not found");
if (it == mainqueue.begin())
throw std::runtime_error("Swap not successful: cannot swap up first element");
std::iter_swap(it, std::prev(it));
}
void SwapDown(MusicPlayer::UUID id)
{
assert(!mainqueue.empty() && "Queue must be not empty");
auto it = std::find(mainqueue.begin(), mainqueue.end(), id);
if (it == mainqueue.end())
throw std::runtime_error("Swap not successful: element not found");
if (std::next(it) == mainqueue.end())
throw std::runtime_error("Swap not successful: cannot swap down last element");
std::iter_swap(it, std::next(it));
}
void ChangeLoop()
{
loop = !loop;
}
void ChangeQueueLoop()
{
queueloop = !queueloop;
}
void Delete(MusicPlayer::UUID internalId)
{
assert(!mainqueue.empty() && "Queue must be not empty");
if(internalId == currentMusic)
currentMusic = 0;
mainqueue.erase(std::ranges::find(mainqueue, internalId));
mappedIds.erase(internalId);
}
void CleanALL()
{
mainqueue.clear();
return;
}
void AddToEnd(const MusicPlayer::UUID uuidFromMusicBlock)
{
MusicPlayer::UUID newId{};
mappedIds[newId] = uuidFromMusicBlock;
mainqueue.push_back(newId);
//LogInfo("Added {0} to queue as {1}", uuidFromMusicBlock, newId);
std::cout << std::format("Added {0} to queue as {1}", static_cast<uint64_t>(uuidFromMusicBlock), static_cast<uint64_t>(newId)) << '\n';
}
};