-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConsoleRenderer.cs
More file actions
257 lines (236 loc) · 11.2 KB
/
Copy pathConsoleRenderer.cs
File metadata and controls
257 lines (236 loc) · 11.2 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
using SharpMod;
using static PrettyConsole.Color;
namespace SharpModConsolePlayer.Renderer {
internal static class ConsoleRenderer {
private const int ChannelWidth = 20;
private const uint PreviousPatternRowThreshold = 1;
internal enum ViewMode {
Any,
Patterns,
Samples
}
internal static void InitializeConsole() {
Console.OutputEncoding = System.Text.Encoding.UTF8;
Console.CursorVisible = false;
Console.Clear();
}
internal static void RestoreConsole() {
Console.Clear();
Console.CursorVisible = true;
}
internal static async Task RenderLoop(Func<SoundFile?> getSoundFile) {
int fromChannel = 0;
int fromSample = 0;
uint lastRow = uint.MaxValue;
uint lastCurrentPattern = uint.MaxValue;
int lastWidth = Console.WindowWidth;
int lastHeight = Console.WindowHeight;
ViewMode mode = ViewMode.Patterns;
bool forceRedraw = true;
SoundFile? lastSoundFile = null;
while(true) {
await Task.Delay(16);
SoundFile? sf = getSoundFile();
if(sf == null) continue;
if(!ReferenceEquals(sf, lastSoundFile)) {
lastSoundFile = sf;
fromChannel = Math.Min(fromChannel, MaxFromChannel(sf));
fromSample = 0;
lastRow = uint.MaxValue;
lastCurrentPattern = uint.MaxValue;
Channel.ResetVuMeters();
Console.Clear();
forceRedraw = true;
}
int width = Console.WindowWidth;
int height = Console.WindowHeight;
if(width != lastWidth || height != lastHeight) {
lastWidth = width;
lastHeight = height;
fromChannel = Math.Min(fromChannel, MaxFromChannel(sf));
Console.Clear();
forceRedraw = true;
}
if(width <= 0 || height <= 0) continue;
if(!HandleInput(sf, ref fromChannel, ref fromSample, ref mode, ref forceRedraw)) return;
try {
if(mode == ViewMode.Samples) {
Info.Render(sf);
Samples.Render(sf, fromSample, forceRedraw);
forceRedraw = false;
} else {
RenderHeaderAndVuMeters(sf, fromChannel);
uint currentRow = sf.Row;
uint currentPattern = sf.CurrentPattern;
if(forceRedraw || currentRow != lastRow || currentPattern != lastCurrentPattern) {
lastRow = currentRow;
lastCurrentPattern = currentPattern;
forceRedraw = false;
RenderPatterns(sf, fromChannel);
}
}
SongProgress.Render(sf);
} catch(ArgumentOutOfRangeException) {
// Window resized mid-frame; next iteration will detect the new size and redraw.
forceRedraw = true;
}
if(Dialog.IsOpen) Dialog.ShowMessage();
}
}
private static int MaxFromChannel(SoundFile sf) {
int width = Console.WindowWidth;
int fitFromRight = Math.Max(0, (width - Channel.VisibleWidth) / ChannelWidth);
return Math.Max(0, (int)sf.ActiveChannels - 1 - fitFromRight);
}
private static bool HandleInput(SoundFile sf, ref int fromChannel, ref int fromSample, ref ViewMode mode, ref bool forceRedraw) {
var NextTrack = () => {
if(OpenAlStreamPlayer.PlaylistIndex < OpenAlStreamPlayer.PlaylistCount - 1) {
OpenAlStreamPlayer.request = PlaybackRequest.Next;
OpenAlStreamPlayer.IsPlaying = false;
}
};
var PreviousTrack = () => {
if(OpenAlStreamPlayer.PlaylistIndex > 0) {
OpenAlStreamPlayer.request = PlaybackRequest.Previous;
OpenAlStreamPlayer.IsPlaying = false;
}
};
while(Console.KeyAvailable) {
ConsoleKeyInfo info = Console.ReadKey(intercept: true);
ConsoleKey key = info.Key;
int previousFromChannel = fromChannel;
if(key >= ConsoleKey.D1 && key <= ConsoleKey.D9) {
int bank = (info.Modifiers & ConsoleModifiers.Control) != 0 ? 2
: (info.Modifiers & ConsoleModifiers.Shift) != 0 ? 1
: 0;
uint channel = (uint)(bank * 12 + (key - ConsoleKey.D1));
sf.ToggleMute(channel);
forceRedraw = true;
continue;
}
switch(key) {
case ConsoleKey.Spacebar:
OpenAlStreamPlayer.IsPaused = !OpenAlStreamPlayer.IsPaused;
break;
case ConsoleKey.LeftArrow:
fromChannel = Math.Max(0, fromChannel - 1);
break;
case ConsoleKey.RightArrow:
fromChannel = Math.Min(MaxFromChannel(sf), fromChannel + 1);
break;
case ConsoleKey.Tab:
if(Dialog.IsOpen) Dialog.Close();
mode = mode == ViewMode.Patterns ? ViewMode.Samples : ViewMode.Patterns;
Console.Clear();
forceRedraw = true;
break;
case ConsoleKey.UpArrow:
if(mode == ViewMode.Samples) {
fromSample = Math.Max(0, fromSample - 1);
}
break;
case ConsoleKey.DownArrow:
if(mode == ViewMode.Samples) {
int rps = Math.Max(1, Math.Clamp(Samples.RowsPerSample, 0, 3));
int visibleSamples = Math.Max(1, (Console.WindowHeight - 1 - Samples.FirstSampleRow) / rps);
int maxFromSample = Math.Max(0, sf.Instruments.Length - 1 - visibleSamples);
fromSample = Math.Min(maxFromSample, fromSample + 1);
}
break;
case ConsoleKey.PageUp: {
// If we are well into the current pattern (row >= threshold), snap to its row 0.
// Otherwise step back one order entry. The threshold is comfortably above the
// number of rows the engine can advance between two keypresses, so a second
// PageUp after the first reliably falls into the "step back" branch.
uint orderIndex = sf.Position / 64;
uint row = sf.Position % 64;
if(row < PreviousPatternRowThreshold) sf.Position = (orderIndex - 1) * 64;
else sf.Position = orderIndex * 64;
break;
}
case ConsoleKey.PageDown: {
// OpenMPT "Next Pattern": step forward one order entry to row 0.
uint orderIndex = sf.Position / 64;
uint maxOrder = sf.PositionCount / 64;
if(maxOrder > 0 && orderIndex + 1 < maxOrder) sf.Position = (orderIndex + 1) * 64;
break;
}
case ConsoleKey.Home:
PreviousTrack();
break;
case ConsoleKey.End:
NextTrack();
break;
case ConsoleKey.Escape:
if(Dialog.IsOpen) {
Dialog.Close();
Console.Clear();
forceRedraw = true;
break;
} else {
OpenAlStreamPlayer.request = PlaybackRequest.Quit;
OpenAlStreamPlayer.IsPlaying = false;
return false;
}
case ConsoleKey.Q:
OpenAlStreamPlayer.request = PlaybackRequest.Quit;
OpenAlStreamPlayer.IsPlaying = false;
return false;
case ConsoleKey.F1:
if(Dialog.IsOpen) {
Dialog.Close();
Console.Clear();
forceRedraw = true;
break;
}
ViewMode dialogMode = mode;
Dialog.SetMessage(" Shortcuts ", 74, 12, () => Cli.PrintKeyBindings($"│ ", $" │", dialogMode));
Dialog.ShowMessage();
break;
case ConsoleKey.H:
Samples.RowsPerSample = (Samples.RowsPerSample + 1) % 4;
break;
case ConsoleKey.M:
Samples.ShowMetadata = !Samples.ShowMetadata;
break;
default:
if(info.Modifiers.HasFlag(ConsoleModifiers.Control)) {
switch(info.KeyChar) {
case '\u0001':
PreviousTrack();
break;
case '\u0005':
NextTrack();
break;
}
}
break;
}
}
return true;
}
private static void RenderHeaderAndVuMeters(SoundFile sf, int fromChannel) {
Info.Render(sf);
int width = Console.WindowWidth;
for(int i = 0; fromChannel + i < sf.ActiveChannels; i++) {
int x = i * ChannelWidth;
if(x >= width) break;
int cellWidth = Math.Min(Channel.VisibleWidth, width - x);
Channel.RenderVuMeter(sf, fromChannel + i, x, cellWidth);
}
}
private static void RenderPatterns(SoundFile sf, int fromChannel) {
uint patternIndex = sf.Pattern;
if(patternIndex == 0xFF) {
patternIndex = sf.Order.Last((o) => o != 0xFF);
}
int width = Console.WindowWidth;
for(int i = 0; fromChannel + i < sf.ActiveChannels; i++) {
int x = i * ChannelWidth;
if(x >= width) break;
int cellWidth = Math.Min(Channel.VisibleWidth, width - x);
Channel.Render(sf, fromChannel + i, patternIndex, x, cellWidth);
}
}
}
}