| 1 |
/* |
| 2 |
CheeseCutter v2 (C) Abaddon. Licensed under GNU GPL. |
| 3 |
*/ |
| 4 |
|
| 5 |
module seq.seqtable; |
| 6 |
import main; |
| 7 |
import ui.ui; |
| 8 |
import com.fb; |
| 9 |
import com.util; |
| 10 |
import seq.sequencer; |
| 11 |
import com.session; |
| 12 |
import ct.base; |
| 13 |
import ui.input; |
| 14 |
import derelict.sdl2.sdl; |
| 15 |
import std.string; |
| 16 |
import audio.player; |
| 17 |
|
| 18 |
class SeqVoice : Voice, Undoable { |
| 19 |
InputSeq seqinput; |
| 20 |
|
| 21 |
this(VoiceInitParams v) { |
| 22 |
super(v); |
| 23 |
activeRow = getRowData(0, 0); |
| 24 |
seqinput = new InputSeq(); |
| 25 |
(cast(InputSeq)seqinput).setElement(activeRow.element); |
| 26 |
seqinput.setCoord(area.x + 4, 0); |
| 27 |
(cast(InputSeq)seqinput).setPointer(area.x + 4, 0); |
| 28 |
activeInput = seqinput; |
| 29 |
} |
| 30 |
|
| 31 |
override int keyrelease(Keyinfo key) { |
| 32 |
return seqinput.keyrelease(key); |
| 33 |
} |
| 34 |
|
| 35 |
override int keypress(Keyinfo key) { |
| 36 |
if(key.mods & KMOD_SHIFT) { |
| 37 |
switch(key.raw) |
| 38 |
{ |
| 39 |
case SDLK_RETURN: |
| 40 |
saveState(); |
| 41 |
int r = activeRow.seq.rows; |
| 42 |
int t = 4 * song.highlight; |
| 43 |
activeRow.seq.expand(activeRow.seqOffset, |
| 44 |
(t - (r + t) % t)); |
| 45 |
break; |
| 46 |
case SDLK_INSERT: |
| 47 |
saveState(); |
| 48 |
activeRow.seq.expand(activeRow.seqOffset, 1); |
| 49 |
break; |
| 50 |
case SDLK_DELETE: |
| 51 |
saveState(); |
| 52 |
activeRow.seq.shrink(activeRow.seqOffset, 1, true); |
| 53 |
break; |
| 54 |
default: |
| 55 |
return seqinput.keypress(key); |
| 56 |
} |
| 57 |
} |
| 58 |
else if(key.mods & KMOD_CTRL) { |
| 59 |
switch(key.raw) |
| 60 |
{ |
| 61 |
case SDLK_INSERT: |
| 62 |
saveState(); |
| 63 |
activeRow.seq.expand(0, 1, false); |
| 64 |
break; |
| 65 |
case SDLK_DELETE: |
| 66 |
saveState(); |
| 67 |
if(activeRow.seqOffset < activeRow.seq.rows - 1) |
| 68 |
activeRow.seq.shrink(0, 1, false); |
| 69 |
break; |
| 70 |
case SDLK_q: |
| 71 |
saveState(); |
| 72 |
activeRow.seq.transpose(activeRow.seqOffset, 1); |
| 73 |
break; |
| 74 |
case SDLK_a: |
| 75 |
saveState(); |
| 76 |
activeRow.seq.transpose(activeRow.seqOffset, -1); |
| 77 |
break; |
| 78 |
case SDLK_w: |
| 79 |
saveState(); |
| 80 |
activeRow.seq.transpose(activeRow.seqOffset, 12); |
| 81 |
break; |
| 82 |
case SDLK_s: |
| 83 |
saveState(); |
| 84 |
activeRow.seq.transpose(activeRow.seqOffset, -12); |
| 85 |
break; |
| 86 |
|
| 87 |
default: |
| 88 |
return seqinput.keypress(key); |
| 89 |
} |
| 90 |
} |
| 91 |
else switch(key.raw) |
| 92 |
{ |
| 93 |
case SDLK_LEFT: |
| 94 |
return seqinput.step(-1); |
| 95 |
case SDLK_RIGHT: |
| 96 |
return seqinput.step(1); |
| 97 |
case SDLK_INSERT: |
| 98 |
saveState(); |
| 99 |
activeRow.seq.insert(activeRow.seqOffset); |
| 100 |
break; |
| 101 |
case SDLK_DELETE: |
| 102 |
saveState(); |
| 103 |
activeRow.seq.remove(activeRow.seqOffset); |
| 104 |
break; |
| 105 |
default: |
| 106 |
return seqinput.keypress(key); |
| 107 |
} |
| 108 |
return OK; |
| 109 |
} |
| 110 |
|
| 111 |
override void refreshPointer(int y) { |
| 112 |
assert(seqinput !is null); |
| 113 |
assert(pos !is null); |
| 114 |
activeRow = getRowData(pos.trkOffset, pos.seqOffset + y); |
| 115 |
activeInput.setCoord(0, 1 + area.y + y + anchor); |
| 116 |
(cast(InputSeq)seqinput).setElement(activeRow.element); |
| 117 |
} |
| 118 |
|
| 119 |
override void update() { |
| 120 |
RowData wseq; |
| 121 |
int scry = area.y + area.height; |
| 122 |
int trkofs = pos.trkOffset, seqofs = pos.seqOffset - anchor; |
| 123 |
int lasttrk = tracks.trackLength; |
| 124 |
int hcount = pos.rowCounter - anchor + area.height - 1; |
| 125 |
int row = area.height; |
| 126 |
Sequence seq; |
| 127 |
|
| 128 |
seqofs += area.height;// - pos.delta; |
| 129 |
wseq = getRowData(trkofs, seqofs); |
| 130 |
trkofs = wseq.trkOffset2; |
| 131 |
seqofs = wseq.seqOffset; |
| 132 |
seq = new Sequence(wseq.seq.data.raw[0 .. $], seqofs); |
| 133 |
void printEmpty() { |
| 134 |
import std.array; |
| 135 |
|
| 136 |
screen.cprint(area.x - 1, scry, 1, 0, |
| 137 |
replicate(" ", 16)); |
| 138 |
} |
| 139 |
|
| 140 |
void printTrack() { |
| 141 |
screen.cprint(area.x - 1, scry, 1, 0, |
| 142 |
" " ~ formatTrackValue(wseq.track.smashedValue)); |
| 143 |
if(trkofs == pos.mark) { |
| 144 |
for(int i = 0; i < 13; i++) { |
| 145 |
int xpos = area.x + i; |
| 146 |
if(screen.getbg(xpos, scry) == 0) |
| 147 |
screen.setbg(xpos, scry, playbackBarColor); |
| 148 |
} |
| 149 |
} |
| 150 |
if(trkofs == tracks.wrapOffset) { |
| 151 |
for(int i = 0; i < 15; i++) { |
| 152 |
int xpos = area.x + i - 1; |
| 153 |
if(screen.getbg(xpos, scry) == 0) |
| 154 |
screen.setbg(xpos, scry, wrapBarColor); |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
int rows = seq.rows; |
| 160 |
while(scry >= area.y + 1) { |
| 161 |
if(trkofs < 0) { |
| 162 |
printEmpty(); |
| 163 |
scry--; row--; |
| 164 |
} |
| 165 |
else if(trkofs >= lasttrk+1) { |
| 166 |
printEmpty(); |
| 167 |
if(trkofs == lasttrk+1) { |
| 168 |
wseq = getRowData(trkofs, 0); |
| 169 |
printTrack(); |
| 170 |
} |
| 171 |
hcount--; scry--; trkofs--; |
| 172 |
if(trkofs >= 0) rows = 0; |
| 173 |
continue; |
| 174 |
} |
| 175 |
else { |
| 176 |
for(int i = rows - 1; i >= 0; |
| 177 |
i--, scry--, hcount--, row--) { |
| 178 |
printEmpty(); |
| 179 |
if(scry < area.y + 1) break; |
| 180 |
Element d = seq.data[i]; |
| 181 |
screen.fprint(area.x + 4, scry, d.toString(wseq.element.transpose)); |
| 182 |
if(i == 0) printTrack(); |
| 183 |
else { |
| 184 |
if(.seq.sequencer.displaySequenceRowcounter == true) { |
| 185 |
int c = (hcount - song.highlightOffset) % |
| 186 |
song.highlight ? 11 : 12; |
| 187 |
screen.cprint(area.x, scry, c, 0, format(" %02X ", i)); |
| 188 |
} |
| 189 |
else screen.cprint(area.x, scry, 0, 0, " "); |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
trkofs--; |
| 194 |
if(trkofs >= 0) { |
| 195 |
wseq = getRowData(trkofs, 0); |
| 196 |
seq = wseq.seq; |
| 197 |
rows = seq.rows; |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
} |
| 202 |
|
| 203 |
protected: |
| 204 |
|
| 205 |
override final void undo(UndoValue entry) { |
| 206 |
auto data = entry.array.target; |
| 207 |
auto target = entry.array.source; |
| 208 |
target[] = data; |
| 209 |
assert(parent !is null); |
| 210 |
entry.seq.refresh(); |
| 211 |
parent.step(0); |
| 212 |
} |
| 213 |
|
| 214 |
void saveState() { |
| 215 |
UndoValue v; |
| 216 |
import std.typecons; |
| 217 |
v.array = UndoValue.Array(activeRow.seq.data.raw.dup, |
| 218 |
activeRow.seq.data.raw); |
| 219 |
v.seq = activeRow.seq; |
| 220 |
com.session.insertUndo(this, v); |
| 221 |
} |
| 222 |
|
| 223 |
override final UndoValue createRedoState(UndoValue value) { |
| 224 |
value.array.target = value.array.source.dup; |
| 225 |
return value; |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
class SequenceTable : VoiceTable { |
| 230 |
this(Rectangle a, PosDataTable pi) { |
| 231 |
int x = 5 + com.fb.border + a.x; |
| 232 |
for(int v=0;v<3;v++) { |
| 233 |
Rectangle na = Rectangle(x, a.y, a.height, 13 + com.fb.border); |
| 234 |
x += 13 + com.fb.border; |
| 235 |
voices[v] = new SeqVoice(VoiceInitParams(song.tracks[v], |
| 236 |
na, pi[v], this)); |
| 237 |
} |
| 238 |
super(a, pi); |
| 239 |
} |
| 240 |
|
| 241 |
override void activate() { |
| 242 |
super.activate(); |
| 243 |
// works as scroll(1) would but does not store variables |
| 244 |
int steps = 0; |
| 245 |
foreach(Voice v; voices) { |
| 246 |
with(v.pos) { |
| 247 |
RowData s = v.getRowData(trkOffset); |
| 248 |
if(trkOffset >= v.tracks.trackLength) { |
| 249 |
trkOffset = 0; |
| 250 |
rowCounter = -pointerOffset; |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
} |
| 255 |
|
| 256 |
} |
| 257 |
|
| 258 |
override void update() { |
| 259 |
super.update(); |
| 260 |
if(!audio.player.isPlaying || audio.player.keyjamEnabled) return; |
| 261 |
// trackbar |
| 262 |
for(int i = 0 ; i < 3; i++) { |
| 263 |
PosData fp = fplayPos[i]; |
| 264 |
PosData vp = posTable[i]; |
| 265 |
int tp = fp.rowCounter - vp.rowCounter + anchor; |
| 266 |
if(tp >= 0 && tp < area.height) { |
| 267 |
for(int x = voices[i].area.x; |
| 268 |
x < voices[i].area.x + voices[i].area.width; x++) { |
| 269 |
screen.setColor(x, 1 + area.y + tp, 1,0); |
| 270 |
} |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
} |
| 275 |
|
| 276 |
override void stepVoice(int i) { |
| 277 |
int n = activeVoiceNum + i; |
| 278 |
int c = (n - activeVoiceNum) > 0 ? 0 : 1; |
| 279 |
n = umod(n, 0, 2); |
| 280 |
if(!voices[n].atEnd()) |
| 281 |
super.stepVoice(i); |
| 282 |
|
| 283 |
SeqVoice v = cast(SeqVoice)voices[n]; |
| 284 |
(cast(InputSeq)v.seqinput).columnReset(-c); |
| 285 |
} |
| 286 |
|
| 287 |
// for positioning the cursor using mouse. x is not used |
| 288 |
override void clickedAt(int x, int y, int button) { |
| 289 |
y -= 1; |
| 290 |
step(y-posTable.normalPointerOffset); |
| 291 |
} |
| 292 |
|
| 293 |
override int keypress(Keyinfo key) { |
| 294 |
// globals |
| 295 |
super.keypress(key); |
| 296 |
if(!key.mods) { |
| 297 |
switch(key.raw) |
| 298 |
{ |
| 299 |
case SDLK_HOME: |
| 300 |
SeqVoice v = cast(SeqVoice)activeVoice; |
| 301 |
InputSeq i = cast(InputSeq)v.seqinput; |
| 302 |
if(i.activeColumn > 0) { |
| 303 |
(cast(InputSeq)v.seqinput).columnReset(0); |
| 304 |
break; |
| 305 |
} |
| 306 |
int ofs = activeVoice.activeRow.seqOffset; |
| 307 |
int cy = posTable.normalPointerOffset; |
| 308 |
int m; |
| 309 |
if(cy == 0) m = 1; |
| 310 |
else if(ofs == 0) break; |
| 311 |
else if(ofs > 0 && ofs > cy) { |
| 312 |
m = 0; |
| 313 |
} |
| 314 |
else m = 1; |
| 315 |
|
| 316 |
if(m) { |
| 317 |
toSeqStart(); |
| 318 |
} |
| 319 |
else |
| 320 |
toScreenTop(); |
| 321 |
|
| 322 |
break; |
| 323 |
case SDLK_END: |
| 324 |
SeqVoice v = cast(SeqVoice)activeVoice; |
| 325 |
InputSeq i = cast(InputSeq)v.seqinput; |
| 326 |
if(i.activeColumn < i.columns) { |
| 327 |
(cast(InputSeq)v.seqinput).columnReset(i.columns,0); |
| 328 |
break; |
| 329 |
} |
| 330 |
// something might be wrong here... |
| 331 |
int scrend = tableTop - posTable.pointerOffset - 1; |
| 332 |
assert(scrend >= 0); |
| 333 |
|
| 334 |
int rows = activeVoice.activeRow.seq.rows; |
| 335 |
int seqend = rows - |
| 336 |
activeVoice.activeRow.seqOffset - 1; |
| 337 |
|
| 338 |
int m; |
| 339 |
if(scrend == 0) toSeqEnd(); |
| 340 |
else if(seqend == 0) toScreenBot(); |
| 341 |
else if(seqend >= scrend) { |
| 342 |
toScreenBot(); |
| 343 |
} |
| 344 |
else { |
| 345 |
toSeqEnd(); |
| 346 |
} |
| 347 |
break; |
| 348 |
case SDLK_KP_0: |
| 349 |
audio.player.playRow(voices); |
| 350 |
step(1); |
| 351 |
break; |
| 352 |
default: |
| 353 |
break; |
| 354 |
} |
| 355 |
|
| 356 |
} |
| 357 |
else if(key.mods & KMOD_CTRL) { |
| 358 |
switch(key.raw) { |
| 359 |
case SDLK_p: |
| 360 |
RowData r = activeVoice.activeRow; |
| 361 |
// bad coding because all the rowcountergetters are flawed one way or another |
| 362 |
int rowcount = activeVoice.getRowcounter(r.trkOffset) + r.seqOffset; |
| 363 |
song.splitSequence(r.track.number, r.seqOffset); |
| 364 |
jump(Jump.toBeginning,false); |
| 365 |
step(rowcount); |
| 366 |
centerTo(0); |
| 367 |
break; |
| 368 |
default: |
| 369 |
break; |
| 370 |
} |
| 371 |
} |
| 372 |
int r; |
| 373 |
if((r = activeVoice.keypress(key)) != OK) { |
| 374 |
switch(r) { |
| 375 |
case WRAPL: |
| 376 |
stepVoice(-1); |
| 377 |
break; |
| 378 |
case WRAPR: |
| 379 |
stepVoice(1); |
| 380 |
break; |
| 381 |
default: |
| 382 |
step(stepValue); |
| 383 |
break; |
| 384 |
} |
| 385 |
} |
| 386 |
return OK; |
| 387 |
} |
| 388 |
} |