| 1 |
/* |
| 2 |
CheeseCutter v2 (C) Abaddon. Licensed under GNU GPL. |
| 3 |
*/ |
| 4 |
|
| 5 |
module ui.input; |
| 6 |
import derelict.sdl2.sdl; |
| 7 |
import com.fb; |
| 8 |
import com.session; |
| 9 |
import com.util; |
| 10 |
import ct.base; |
| 11 |
import seq.sequencer; |
| 12 |
import audio.player; |
| 13 |
import ui.ui; |
| 14 |
import std.string; |
| 15 |
import std.utf; |
| 16 |
import std.stdio : stderr; |
| 17 |
import std.conv; |
| 18 |
|
| 19 |
enum { RETURN = -1, CANCEL = -2, OK = 0, WRAP = 1, WRAPR, WRAPL, EXIT, IllegalValue } |
| 20 |
|
| 21 |
alias ValueChangedCallback = void delegate(); |
| 22 |
|
| 23 |
mixin template ValueChangedHandler() { |
| 24 |
private ValueChangedCallback valueChangedCallback = null; |
| 25 |
private void valueChanged() { |
| 26 |
if(valueChangedCallback !is null) |
| 27 |
valueChangedCallback(); |
| 28 |
} |
| 29 |
|
| 30 |
void setValueChangedCallback(ValueChangedCallback cb) { |
| 31 |
this.valueChangedCallback = cb; |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
struct Keyinfo { |
| 37 |
int key, mods, unicode; |
| 38 |
alias key raw; |
| 39 |
} |
| 40 |
|
| 41 |
class Cursor { |
| 42 |
enum BLINK_VAL = 8; |
| 43 |
int x = -1, y = -1; |
| 44 |
private { |
| 45 |
int counter; |
| 46 |
int bg2, fg2; |
| 47 |
int bg, fg; |
| 48 |
} |
| 49 |
|
| 50 |
void set() { set(x,y); } |
| 51 |
alias set refresh; |
| 52 |
|
| 53 |
void set(int nx, int ny) { |
| 54 |
if(nx < 0 || ny < 0) return; |
| 55 |
if(x != nx || y != ny) { |
| 56 |
x = nx; y = ny; |
| 57 |
ushort col = screen.getChar(x, y); |
| 58 |
counter = BLINK_VAL; |
| 59 |
bg = fg2 = (col >> 8) & 15; |
| 60 |
fg = bg2 = (col >> 12) & 15; |
| 61 |
} |
| 62 |
screen.setColor(x,y,bg2,fg2); |
| 63 |
} |
| 64 |
|
| 65 |
void reset() { |
| 66 |
counter = BLINK_VAL; |
| 67 |
bg2 = fg; |
| 68 |
fg2 = bg; |
| 69 |
} |
| 70 |
|
| 71 |
void blink() { |
| 72 |
if(--counter < 0) { |
| 73 |
int t; |
| 74 |
counter = BLINK_VAL; |
| 75 |
t = bg2; bg2 = fg2; fg2 = t; |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
class Input { |
| 81 |
mixin ValueChangedHandler; |
| 82 |
|
| 83 |
Cursor cursor; |
| 84 |
const int width; |
| 85 |
int x, y, nibble; |
| 86 |
alias x pointerX; |
| 87 |
alias y pointerY; |
| 88 |
alias width inputLength; |
| 89 |
ubyte[] inarray, outarray; |
| 90 |
|
| 91 |
this(ubyte[] p, int len) { |
| 92 |
this(len); |
| 93 |
setOutput(p); |
| 94 |
} |
| 95 |
this(int len) { |
| 96 |
cursor = new Cursor(); |
| 97 |
width = len; |
| 98 |
inarray.length = len; |
| 99 |
} |
| 100 |
|
| 101 |
void setOutput(ubyte[] p) { |
| 102 |
outarray = p; |
| 103 |
} |
| 104 |
|
| 105 |
void setCoord(int nx, int ny) { |
| 106 |
if(nx) x = nx; |
| 107 |
if(ny) y = ny; |
| 108 |
} |
| 109 |
alias setCoord set; |
| 110 |
|
| 111 |
int keypress(Keyinfo key) { return 0 ;} |
| 112 |
|
| 113 |
int keyrelease(Keyinfo key) { return 0; } |
| 114 |
|
| 115 |
int setValue(int v) { assert(0); } |
| 116 |
|
| 117 |
int step(int st) { |
| 118 |
nibble += st; |
| 119 |
if(nibble < 0) { |
| 120 |
nibble = inputLength - 1; |
| 121 |
return WRAP; // should return WRAPL |
| 122 |
} |
| 123 |
else if(nibble >= inputLength) { |
| 124 |
nibble = 0; |
| 125 |
return WRAP; |
| 126 |
} |
| 127 |
return OK; |
| 128 |
} |
| 129 |
|
| 130 |
void update() { assert(0); } |
| 131 |
|
| 132 |
void refresh() { assert(0); } |
| 133 |
|
| 134 |
int toInt() { |
| 135 |
return toInt(inarray); |
| 136 |
} |
| 137 |
|
| 138 |
@property int value() { |
| 139 |
return toInt(inarray); |
| 140 |
} |
| 141 |
|
| 142 |
int toInt(ubyte[] ar) { |
| 143 |
int v; |
| 144 |
for(int i = cast(int)(ar.length-1), sh; i >= 0; i--) { |
| 145 |
v |= ar[i] << sh; |
| 146 |
sh += 4; |
| 147 |
} |
| 148 |
return v; |
| 149 |
} |
| 150 |
|
| 151 |
int toIntRange(int b, int e) { |
| 152 |
return toInt(inarray[b..e]); |
| 153 |
} |
| 154 |
|
| 155 |
private: |
| 156 |
|
| 157 |
int valueKeyReader(Keyinfo key, char[] keytab) { |
| 158 |
foreach(i, k; keytab) { |
| 159 |
if(key.raw == k) |
| 160 |
return cast(int)i; |
| 161 |
} |
| 162 |
return -1; |
| 163 |
} |
| 164 |
|
| 165 |
int keypressStepHandler(Keyinfo key, char[] keytab) { |
| 166 |
int v = valueKeyReader(key, keytab); |
| 167 |
if(v >= 0) { |
| 168 |
int r = setValue(v); |
| 169 |
// if 'setValue' returns > 0, don't step |
| 170 |
if(r) return r; |
| 171 |
// if shift is pressed, don't step |
| 172 |
if(key.mods & KMOD_SHIFT) return OK; |
| 173 |
return step(1); |
| 174 |
} |
| 175 |
return OK; |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
class InputValue : Input { |
| 180 |
this(ubyte[] p, int len) { |
| 181 |
super(p, len); |
| 182 |
} |
| 183 |
|
| 184 |
override void setOutput(ubyte[] p) { |
| 185 |
super.setOutput(p); |
| 186 |
// initialize value |
| 187 |
for(int i=0; i < inputLength; i++) { |
| 188 |
int j = i / 2; |
| 189 |
int sh = (i & 1) ? 0 : 4; |
| 190 |
inarray[i] = (p[j] >> sh) & 15; |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
override int keypress(Keyinfo key) { |
| 195 |
int v; |
| 196 |
if(key.mods & KMOD_CTRL) return 0; |
| 197 |
if(key.raw == SDLK_RETURN) |
| 198 |
return RETURN; |
| 199 |
else if(key.raw == SDLK_ESCAPE) |
| 200 |
return CANCEL; |
| 201 |
return keypressStepHandler(key, cast(char[])"0123456789abcdef"); |
| 202 |
} |
| 203 |
|
| 204 |
override int setValue(int v) { |
| 205 |
valueChanged(); |
| 206 |
inarray[nibble] = cast(ubyte)v; |
| 207 |
int c = toInt(); |
| 208 |
for(int i = cast(int)(inputLength/2-1); i >= 0; i--) { |
| 209 |
outarray[i] = c & 255; |
| 210 |
c >>= 8; |
| 211 |
} |
| 212 |
return OK; |
| 213 |
} |
| 214 |
|
| 215 |
override void update() { |
| 216 |
string fmt = std.string.format("0%dX",inputLength); |
| 217 |
screen.cprint(x, y, 1, -1, format("%" ~ fmt,toInt())); |
| 218 |
cursor.set(x + nibble, y); |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
class InputByte : InputValue { |
| 223 |
this(ubyte[] p) { |
| 224 |
super(p, 2); |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
class InputBoundedByte : InputValue { |
| 229 |
this(ubyte[] p) { |
| 230 |
super(p, 2); |
| 231 |
} |
| 232 |
|
| 233 |
override int keypress(Keyinfo key) { |
| 234 |
int v; |
| 235 |
if(key.mods & KMOD_CTRL) return 0; |
| 236 |
if(key.raw == SDLK_RETURN) |
| 237 |
return RETURN; |
| 238 |
else if(key.raw == SDLK_ESCAPE) |
| 239 |
return CANCEL; |
| 240 |
switch(key.raw) |
| 241 |
{ |
| 242 |
case SDLK_LEFT: |
| 243 |
return step(-1); |
| 244 |
case SDLK_BACKSPACE: |
| 245 |
step(-1); |
| 246 |
setValue(0); |
| 247 |
return OK; |
| 248 |
case SDLK_RIGHT: |
| 249 |
return step(1); |
| 250 |
default: |
| 251 |
if(nibble < 2) |
| 252 |
return keypressStepHandler(key, cast(char[])"0123456789abcdef"); |
| 253 |
return OK; |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
override int step(int st) { |
| 258 |
nibble += st; |
| 259 |
if(nibble < 0) |
| 260 |
nibble = 0; |
| 261 |
else if(nibble > 2) |
| 262 |
nibble = 2; |
| 263 |
return OK; |
| 264 |
} |
| 265 |
|
| 266 |
override void update() { |
| 267 |
screen.cprint(x, y, 1, -1, format("%02X ", toInt())); |
| 268 |
cursor.set(x + nibble, y); |
| 269 |
} |
| 270 |
|
| 271 |
} |
| 272 |
|
| 273 |
class InputSingleChar : InputValue { |
| 274 |
string keys; |
| 275 |
int defaultKey; |
| 276 |
this(ubyte[] p, string keys, int defaultKey) { |
| 277 |
super(p, 2); |
| 278 |
this.keys = keys; |
| 279 |
setValue(0); |
| 280 |
this.defaultKey = defaultKey; |
| 281 |
} |
| 282 |
|
| 283 |
override int keypress(Keyinfo key) { |
| 284 |
auto v = keys.indexOf(key.raw); |
| 285 |
if(key.mods & KMOD_CTRL) return 0; |
| 286 |
else if(key.raw == SDLK_ESCAPE) { |
| 287 |
return CANCEL; |
| 288 |
} |
| 289 |
else if(key.raw == SDLK_RETURN) { |
| 290 |
setValue(cast(ubyte)defaultKey); |
| 291 |
return RETURN; |
| 292 |
} |
| 293 |
else if(v >= 0) { |
| 294 |
setValue(cast(ubyte)(v)); |
| 295 |
return RETURN; |
| 296 |
} |
| 297 |
return IllegalValue; |
| 298 |
} |
| 299 |
|
| 300 |
override int step(int st) { |
| 301 |
return OK; |
| 302 |
} |
| 303 |
|
| 304 |
override void update() { |
| 305 |
//screen.cprint(x, y, 1, -1, format("%02X ", toInt())); |
| 306 |
screen.cprint(x + nibble, y, 1, -1, std.conv.to!string(keys[defaultKey])); |
| 307 |
cursor.set(x + nibble, y); |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
class InputWord : InputValue { |
| 313 |
this(ubyte[] p) { |
| 314 |
super(p, 4); |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
class InputTrack : InputWord { |
| 319 |
Track trk; |
| 320 |
ubyte[2] buf; |
| 321 |
this(RowData s, ValueChangedCallback cb) { |
| 322 |
super(buf); |
| 323 |
init(s); |
| 324 |
trk.setValue(buf[0], buf[1]); |
| 325 |
valueChangedCallback = cb; |
| 326 |
} |
| 327 |
|
| 328 |
void init(RowData s) { |
| 329 |
trk = s.trk; |
| 330 |
buf[] = valueCheck(trk.trans, trk.number); |
| 331 |
super.setOutput(buf); |
| 332 |
} |
| 333 |
|
| 334 |
alias init refresh; |
| 335 |
|
| 336 |
void flush() { |
| 337 |
if(trk.number != buf[1] || |
| 338 |
trk.trans != buf[0]) |
| 339 |
valueChanged(); |
| 340 |
trk.setValue(buf[0], buf[1]); |
| 341 |
} |
| 342 |
|
| 343 |
override int setValue(int v) { |
| 344 |
super.setValue(v); |
| 345 |
buf[] = valueCheckNoWrap(buf[0], buf[1]); |
| 346 |
super.setOutput(buf); |
| 347 |
return OK; |
| 348 |
} |
| 349 |
|
| 350 |
override int keypress(Keyinfo key) { |
| 351 |
if(key.mods & KMOD_ALT) return OK; |
| 352 |
switch(key.unicode) |
| 353 |
{ |
| 354 |
case 6: // ctrl-f |
| 355 |
int s = song.getFreeSequence(buf[1] + 1); |
| 356 |
if(s > 0) |
| 357 |
buf[] = valueCheck(buf[0], s); |
| 358 |
setOutput(buf); |
| 359 |
flush(); |
| 360 |
break; |
| 361 |
case SDLK_LESS: |
| 362 |
int i = buf[1] - 1; |
| 363 |
buf[] = valueCheck(buf[0], i); |
| 364 |
setOutput(buf); |
| 365 |
flush(); |
| 366 |
break; |
| 367 |
case SDLK_GREATER: |
| 368 |
int i = buf[1] + 1; |
| 369 |
buf[] = valueCheck(buf[0], i); |
| 370 |
setOutput(buf); |
| 371 |
flush(); |
| 372 |
break; |
| 373 |
default: |
| 374 |
break; |
| 375 |
} |
| 376 |
switch(key.raw) |
| 377 |
{ |
| 378 |
case SDLK_LEFT: |
| 379 |
if(--nibble < 0) { |
| 380 |
nibble = inputLength - 1; |
| 381 |
return WRAPL; |
| 382 |
} |
| 383 |
return OK; |
| 384 |
case SDLK_RIGHT: |
| 385 |
if(++nibble >= inputLength) { |
| 386 |
nibble = 0; |
| 387 |
return WRAPR; |
| 388 |
} |
| 389 |
return OK; |
| 390 |
case SDLK_SPACE: |
| 391 |
flush(); |
| 392 |
return OK; |
| 393 |
default: |
| 394 |
break; |
| 395 |
} |
| 396 |
if(buf[0] < 0xc0) { |
| 397 |
int r = super.keypress(key); |
| 398 |
return r; |
| 399 |
} |
| 400 |
return OK; |
| 401 |
} |
| 402 |
|
| 403 |
private: |
| 404 |
|
| 405 |
ubyte[] valueCheck(int tr, int no) { |
| 406 |
if(tr < 0x80) tr = 0x80; |
| 407 |
if(no < 0) no = 0; |
| 408 |
if(no > 0x80) no = 0x00; |
| 409 |
if(no >= MAX_SEQ_NUM) no = MAX_SEQ_NUM-1; |
| 410 |
return cast(ubyte[])[tr,no]; |
| 411 |
} |
| 412 |
|
| 413 |
// don't allow wrapmark |
| 414 |
ubyte[] valueCheckNoWrap(int tr, int no) { |
| 415 |
ubyte[] b = valueCheck(tr, no); |
| 416 |
if(b[0] > 0xbf) b[0] = 0xbf; |
| 417 |
return b; |
| 418 |
} |
| 419 |
} |
| 420 |
|
| 421 |
class InputString : Input { |
| 422 |
char[] instring; |
| 423 |
this(string s) { this(s, 80); } |
| 424 |
this(string s, int len) { |
| 425 |
super(len); |
| 426 |
setOutput(s); |
| 427 |
} |
| 428 |
|
| 429 |
override void setOutput(ubyte[] p) { |
| 430 |
assert(0); |
| 431 |
} |
| 432 |
|
| 433 |
void setOutput(string s) { |
| 434 |
int tl = cast(int)s.length; |
| 435 |
char[] str2 = std.utf.toUTF8(s.dup).dup; |
| 436 |
str2.length = inputLength; |
| 437 |
if(tl >= inputLength) tl = inputLength; |
| 438 |
str2[tl .. $] = ' '; |
| 439 |
instring = str2; |
| 440 |
nibble = stringLength; |
| 441 |
if(nibble >= inputLength) |
| 442 |
nibble = inputLength - 1; |
| 443 |
} |
| 444 |
|
| 445 |
override string toString() { |
| 446 |
return toString(false); |
| 447 |
} |
| 448 |
|
| 449 |
string toString(bool pad) { |
| 450 |
if(!pad) |
| 451 |
return cast(string)(instring[0..stringLength].dup); |
| 452 |
return cast(string)(instring); |
| 453 |
} |
| 454 |
|
| 455 |
override void update() { |
| 456 |
screen.cprint(x, y, 1, 0, toString(true)); |
| 457 |
cursor.set(x + nibble, y); |
| 458 |
} |
| 459 |
|
| 460 |
void setChar(dchar value) { |
| 461 |
instring[nibble] = cast(char)value; |
| 462 |
} |
| 463 |
|
| 464 |
override int keypress(Keyinfo key) { |
| 465 |
int i; |
| 466 |
|
| 467 |
switch(key.raw) |
| 468 |
{ |
| 469 |
case SDLK_LEFT: |
| 470 |
if(step(-1)) |
| 471 |
nibble = 0; |
| 472 |
break; |
| 473 |
case SDLK_RIGHT: |
| 474 |
if(step(1)) |
| 475 |
nibble = inputLength-1; |
| 476 |
break; |
| 477 |
case SDLK_BACKSPACE: |
| 478 |
if(step(-1)) { |
| 479 |
nibble = 0; |
| 480 |
break; |
| 481 |
} |
| 482 |
goto case SDLK_DELETE; |
| 483 |
case SDLK_DELETE: |
| 484 |
instring[nibble .. $-1] = instring[nibble+1 .. $].dup; |
| 485 |
instring[$-1] = ' '; |
| 486 |
break; |
| 487 |
// slightly bugs when str.length == inputLength |
| 488 |
case SDLK_HOME: |
| 489 |
nibble = 0; |
| 490 |
break; |
| 491 |
case SDLK_END: |
| 492 |
nibble = stringLength; |
| 493 |
if(nibble >= inputLength) |
| 494 |
nibble = inputLength - 1; |
| 495 |
break; |
| 496 |
case SDLK_RETURN: |
| 497 |
return RETURN; |
| 498 |
case SDLK_ESCAPE: |
| 499 |
return CANCEL; |
| 500 |
default: |
| 501 |
void insert() { |
| 502 |
instring[nibble+1 .. $] = instring[nibble .. $-1].dup; |
| 503 |
} |
| 504 |
if(key.raw == SDLK_INSERT) { |
| 505 |
insert(); |
| 506 |
setChar(' '); |
| 507 |
} |
| 508 |
else if(key.unicode && key.unicode != '`') { |
| 509 |
string old = cast(string)(instring.dup); |
| 510 |
insert(); |
| 511 |
setChar(key.unicode); |
| 512 |
try { |
| 513 |
validate(instring); |
| 514 |
} |
| 515 |
catch(UTFException e) { |
| 516 |
stderr.writeln(e.toString); |
| 517 |
instring = old.dup; |
| 518 |
break; |
| 519 |
} |
| 520 |
if(step(1) == WRAP) nibble = inputLength - 1; |
| 521 |
} |
| 522 |
break; |
| 523 |
} |
| 524 |
return OK; |
| 525 |
} |
| 526 |
|
| 527 |
@property int stringLength() { |
| 528 |
for(int i = inputLength - 1; i >= 0; i--) { |
| 529 |
if(instring[i] != ' ') |
| 530 |
return i+1; |
| 531 |
} |
| 532 |
return 0; |
| 533 |
} |
| 534 |
} |
| 535 |
|
| 536 |
abstract class ExtendedInput : Input { |
| 537 |
protected { |
| 538 |
int nibble, memvalue; |
| 539 |
Element element; |
| 540 |
} |
| 541 |
int invalue; |
| 542 |
bool changed; |
| 543 |
//bool valueChanged() { return changed; } |
| 544 |
|
| 545 |
/+ |
| 546 |
protected this() { |
| 547 |
super(1); |
| 548 |
} |
| 549 |
|
| 550 |
protected this(int w) { |
| 551 |
super(w); |
| 552 |
} |
| 553 |
+/ |
| 554 |
|
| 555 |
this(ValueChangedCallback cb) { |
| 556 |
this(1, cb); |
| 557 |
} |
| 558 |
|
| 559 |
this(int w, ValueChangedCallback cb) { |
| 560 |
this.valueChangedCallback = cb; |
| 561 |
super(w); |
| 562 |
} |
| 563 |
|
| 564 |
|
| 565 |
override int step(int st) { |
| 566 |
nibble += st; |
| 567 |
if(nibble >= width) { |
| 568 |
nibble = width - 1; |
| 569 |
return WRAPR; |
| 570 |
} |
| 571 |
else if(nibble < 0) { |
| 572 |
nibble = 0; |
| 573 |
return WRAPL; |
| 574 |
} |
| 575 |
return OK; |
| 576 |
} |
| 577 |
|
| 578 |
override int keypress(Keyinfo key) { |
| 579 |
return keypress(key, "0123456789abcdef"); |
| 580 |
} |
| 581 |
|
| 582 |
int keypress(Keyinfo key, string keytab) { |
| 583 |
if(key.mods & KMOD_CTRL || key.mods & KMOD_ALT) |
| 584 |
return OK; |
| 585 |
switch(key.unicode) { |
| 586 |
case ' ': |
| 587 |
if(memvalue >= 0) { |
| 588 |
changed = (invalue != memvalue); |
| 589 |
invalue = memvalue; |
| 590 |
valueChanged(); |
| 591 |
setRowValue(memvalue); |
| 592 |
return WRAP; |
| 593 |
} |
| 594 |
goto case '.'; |
| 595 |
case '.': |
| 596 |
valueChanged(); |
| 597 |
clearRow(); |
| 598 |
return WRAP; |
| 599 |
default: |
| 600 |
if(keytab == null) return WRAP; |
| 601 |
int value = valueKeyReader(key, keytab); |
| 602 |
if(value < 0) { |
| 603 |
changed = false; |
| 604 |
return OK; |
| 605 |
} |
| 606 |
return valuekeyHandler(value); |
| 607 |
} |
| 608 |
//not reached |
| 609 |
} |
| 610 |
|
| 611 |
protected: |
| 612 |
|
| 613 |
int valuekeyHandler(int value) { |
| 614 |
if(width == 1) invalue = value; |
| 615 |
else { |
| 616 |
if(nibble == 0) { |
| 617 |
invalue &= 0x0f; |
| 618 |
invalue |= value << 4; |
| 619 |
} |
| 620 |
else { |
| 621 |
invalue &= 0xf0; |
| 622 |
invalue |= value & 255; |
| 623 |
} |
| 624 |
} |
| 625 |
|
| 626 |
valueChanged(); |
| 627 |
|
| 628 |
setRowValue(invalue); |
| 629 |
|
| 630 |
memvalue = invalue; |
| 631 |
if(++nibble >= width) { |
| 632 |
nibble = 0; |
| 633 |
invalue = 0; |
| 634 |
return WRAP; |
| 635 |
} |
| 636 |
|
| 637 |
return OK; |
| 638 |
} |
| 639 |
|
| 640 |
void clearRow() { |
| 641 |
memvalue = -1; |
| 642 |
} |
| 643 |
|
| 644 |
void setRowValue(int value) { |
| 645 |
} |
| 646 |
|
| 647 |
void setElement(Element e) { |
| 648 |
element = e; |
| 649 |
} |
| 650 |
|
| 651 |
override void update() { |
| 652 |
assert(0); |
| 653 |
} |
| 654 |
|
| 655 |
static int valueKeyReader(Keyinfo key, const char[] keytab) { |
| 656 |
foreach(i, k; keytab) { |
| 657 |
if(key.raw == k) { |
| 658 |
if(key.mods & KMOD_SHIFT) |
| 659 |
return cast(int)(i | 0x80); |
| 660 |
return cast(int)i; |
| 661 |
} |
| 662 |
} |
| 663 |
return -1; |
| 664 |
} |
| 665 |
} |
| 666 |
|
| 667 |
class InputOctave : ExtendedInput { |
| 668 |
this(ValueChangedCallback cb) { |
| 669 |
super(1, cb); |
| 670 |
} |
| 671 |
|
| 672 |
override int keypress(Keyinfo key) { |
| 673 |
return super.keypress(key,"012345678"); |
| 674 |
} |
| 675 |
|
| 676 |
override void setRowValue(int value) { |
| 677 |
if(element.note.value >= 3) { |
| 678 |
int note = ((element.note.value + element.transpose) % 12) |
| 679 |
+ value * 12 - element.transpose; |
| 680 |
if(note >= 3 && note < 0x5f) |
| 681 |
element.note = cast(ubyte)note; |
| 682 |
} |
| 683 |
} |
| 684 |
} |
| 685 |
|
| 686 |
class InputInstrument : ExtendedInput { |
| 687 |
this(ValueChangedCallback cb) { |
| 688 |
super(2, cb); |
| 689 |
} |
| 690 |
|
| 691 |
override void clearRow() { |
| 692 |
super.clearRow(); |
| 693 |
element.instr = 0xc0; |
| 694 |
invalue = 0x30; |
| 695 |
} |
| 696 |
|
| 697 |
override void setElement(Element e) { |
| 698 |
super.setElement(e); |
| 699 |
if(e.instr.value < 0x30) |
| 700 |
invalue = e.instr.value; |
| 701 |
else invalue = 0; |
| 702 |
} |
| 703 |
|
| 704 |
override int keypress(Keyinfo key) { |
| 705 |
switch(key.unicode) { |
| 706 |
case SDLK_RETURN: |
| 707 |
if(element.instr.value < 0x30) |
| 708 |
mainui.activateInstrumentTable(element.instr.value); |
| 709 |
break; |
| 710 |
default: |
| 711 |
break; |
| 712 |
} |
| 713 |
return super.keypress(key); |
| 714 |
} |
| 715 |
|
| 716 |
override void setRowValue(int v) { |
| 717 |
element.instr = cast(ubyte)v; |
| 718 |
UI.activateInstrument(v); |
| 719 |
} |
| 720 |
} |
| 721 |
|
| 722 |
class InputCmd : ExtendedInput { |
| 723 |
this(ValueChangedCallback cb) { |
| 724 |
super(2, cb); |
| 725 |
} |
| 726 |
|
| 727 |
override void clearRow() { |
| 728 |
super.clearRow(); |
| 729 |
element.cmd = 0; |
| 730 |
} |
| 731 |
|
| 732 |
override void setElement(Element e) { |
| 733 |
super.setElement(e); |
| 734 |
invalue = e.cmd.rawValue; |
| 735 |
} |
| 736 |
|
| 737 |
override void setRowValue(int v) { |
| 738 |
element.cmd = cast(ubyte)v; |
| 739 |
} |
| 740 |
} |
| 741 |
|
| 742 |
class InputNote : ExtendedInput { |
| 743 |
InputKeyjam keyjam; |
| 744 |
private bool noteStarted = false; |
| 745 |
|
| 746 |
this(ValueChangedCallback cb) { |
| 747 |
super(1, cb); |
| 748 |
keyjam = new InputKeyjam(); |
| 749 |
} |
| 750 |
|
| 751 |
override int keyrelease(Keyinfo key) { |
| 752 |
import audio.player; |
| 753 |
if(!noteStarted || audio.player.getPlaystatus() == Status.Play) |
| 754 |
return OK; |
| 755 |
noteStarted = false; |
| 756 |
Element emt = Element([0x00,cast(ubyte)(0xc0+state.activeInstrument),1]); |
| 757 |
audio.player.playNote(emt); |
| 758 |
return OK; |
| 759 |
} |
| 760 |
|
| 761 |
override int keypress(Keyinfo key) { |
| 762 |
if(key.mods & KMOD_CTRL || key.mods & KMOD_ALT) { |
| 763 |
switch(key.raw) { |
| 764 |
case SDLK_g: |
| 765 |
if(element.instr.value < 0x30) |
| 766 |
UI.activateInstrument(element.instr.value); |
| 767 |
break; |
| 768 |
default: |
| 769 |
break; |
| 770 |
} |
| 771 |
return OK; |
| 772 |
} |
| 773 |
|
| 774 |
switch(key.unicode) { |
| 775 |
case SDLK_RETURN: |
| 776 |
if(element.instr.value < 0x30) |
| 777 |
UI.activateInstrument(element.instr.value); |
| 778 |
break; |
| 779 |
case SDLK_COMMA: |
| 780 |
if(element.note.value >= 3 && element.note.value < 0x5f) { |
| 781 |
valueChanged(); |
| 782 |
element.note.setTied(element.note.isTied() ? |
| 783 |
false : true); |
| 784 |
} |
| 785 |
return WRAP; |
| 786 |
case ' ', '.': |
| 787 |
valueChanged(); |
| 788 |
clearRow(); |
| 789 |
return WRAP; |
| 790 |
/+ |
| 791 |
case SDLK_SEMICOLON: |
| 792 |
if(element.note.rawValue >= 3 && element.note.rawValue < 0x5f) |
| 793 |
element.note.setTied(false); |
| 794 |
return WRAP; |
| 795 |
+/ |
| 796 |
default: |
| 797 |
break; |
| 798 |
} |
| 799 |
|
| 800 |
int r = super.keypress(key,"1!azsxdcvgbhnjmq2w3er5t6y7ui9o0p"); |
| 801 |
// r will be > 0 (in 'wrap') if valid data was entered |
| 802 |
if(r) { |
| 803 |
keyjam.element.transpose = element.transpose; |
| 804 |
keyjam.keypress(key); |
| 805 |
|
| 806 |
noteStarted = true; |
| 807 |
} |
| 808 |
// no cache for notecolumn |
| 809 |
memvalue = -1; |
| 810 |
return r; |
| 811 |
} |
| 812 |
|
| 813 |
override void clearRow() { |
| 814 |
super.clearRow(); |
| 815 |
element.note = 0; |
| 816 |
element.note.setTied(false); |
| 817 |
element.instr = 0x80; |
| 818 |
element.cmd = 0; |
| 819 |
} |
| 820 |
|
| 821 |
override void setRowValue(int value) { |
| 822 |
if(value < 0) return; |
| 823 |
switch(value) { |
| 824 |
case 0: |
| 825 |
element.note = NOTE_KEYOFF; |
| 826 |
element.note.setTied(false); |
| 827 |
element.instr = 0x80; |
| 828 |
break; |
| 829 |
case 2: |
| 830 |
case 0x80: |
| 831 |
element.note = NOTE_KEYON; |
| 832 |
element.note.setTied(false); |
| 833 |
element.instr = 0x80; |
| 834 |
break; |
| 835 |
default: |
| 836 |
int note = ((value - 3) & 0x7f) + 12 * state.octave - element.transpose; |
| 837 |
if(note > 0x5e) break; |
| 838 |
element.note = cast(ubyte)note; |
| 839 |
if(state.autoinsertInstrument && value < 0x80) { |
| 840 |
if(state.activeInstrument >= 0) |
| 841 |
element.instr = cast(ubyte)(state.activeInstrument); |
| 842 |
else element.instr = 0x80; |
| 843 |
} |
| 844 |
if(value >= 0x80) { |
| 845 |
element.note.setTied(true); |
| 846 |
} |
| 847 |
else element.note.setTied(false); |
| 848 |
break; |
| 849 |
} |
| 850 |
} |
| 851 |
|
| 852 |
override int step(int st) { |
| 853 |
if(st >= 0) return WRAPR; |
| 854 |
return WRAPL; |
| 855 |
} |
| 856 |
} |
| 857 |
|
| 858 |
class InputKeyjam : ExtendedInput { |
| 859 |
ubyte[4] dummy; |
| 860 |
|
| 861 |
this() { |
| 862 |
element = Element(dummy); |
| 863 |
super(1, null); |
| 864 |
} |
| 865 |
|
| 866 |
override void setRowValue(int value) { |
| 867 |
if(value < 0) return; |
| 868 |
switch(value) { |
| 869 |
case 0: |
| 870 |
element.note = NOTE_KEYOFF; |
| 871 |
element.note.setTied(false); |
| 872 |
// element.instr = 0x80; |
| 873 |
break; |
| 874 |
case 2: |
| 875 |
case 0x80: |
| 876 |
element.note = NOTE_KEYON; |
| 877 |
break; |
| 878 |
default: |
| 879 |
int note = ((value - 3) & 0x7f) + 12 * state.octave; |
| 880 |
if(note > 0x5e) return; |
| 881 |
element.note = cast(ubyte)note; |
| 882 |
if(value >= 0x80) { |
| 883 |
element.note.setTied(true); |
| 884 |
} |
| 885 |
else element.note.setTied(false); |
| 886 |
|
| 887 |
break; |
| 888 |
} |
| 889 |
if(state.activeInstrument >= 0) |
| 890 |
element.instr = cast(ubyte)(state.activeInstrument); |
| 891 |
audio.player.playNote(element); |
| 892 |
} |
| 893 |
|
| 894 |
override int keypress(Keyinfo key) { |
| 895 |
return super.keypress(key,"1!azsxdcvgbhnjmq2w3er5t6y7ui9o0p"); |
| 896 |
} |
| 897 |
|
| 898 |
override int keyrelease(Keyinfo key) { |
| 899 |
return OK; |
| 900 |
} |
| 901 |
} |
| 902 |
|
| 903 |
final class InputSeq : ExtendedInput, Undoable { |
| 904 |
Element element; |
| 905 |
private { |
| 906 |
ExtendedInput inputNote, inputInstrument, inputCmd, inputOctave; |
| 907 |
} |
| 908 |
ExtendedInput[] inputters; |
| 909 |
ExtendedInput activeInput; |
| 910 |
int activeInputNo; |
| 911 |
alias activeInputNo activeColumn; |
| 912 |
enum columns = 3; |
| 913 |
|
| 914 |
this() { |
| 915 |
super(1, null); |
| 916 |
inputNote = new InputNote(&valueChanged); |
| 917 |
inputInstrument = new InputInstrument(&valueChanged); |
| 918 |
inputCmd = new InputCmd(&valueChanged); |
| 919 |
inputOctave = new InputOctave(&valueChanged); |
| 920 |
activeInput = inputNote; |
| 921 |
inputters = [inputNote, inputOctave, inputInstrument, inputCmd]; |
| 922 |
} |
| 923 |
|
| 924 |
void setPointer(int x, int y) { |
| 925 |
if(x >0) |
| 926 |
pointerX = x; |
| 927 |
if(y > 0) |
| 928 |
pointerY = y; |
| 929 |
} |
| 930 |
|
| 931 |
override void setCoord(int x, int y) { |
| 932 |
setPointer(x, y); |
| 933 |
} |
| 934 |
|
| 935 |
override void setElement(Element e) { |
| 936 |
element = e; |
| 937 |
activeInput.setElement(e); |
| 938 |
} |
| 939 |
|
| 940 |
override int keyrelease(Keyinfo key) { |
| 941 |
return activeInput.keyrelease(key); |
| 942 |
} |
| 943 |
|
| 944 |
override int keypress(Keyinfo key) { |
| 945 |
switch(key.unicode) { |
| 946 |
case SDLK_SEMICOLON: |
| 947 |
state.autoinsertInstrument ^= 1; |
| 948 |
UI.statusline.display(format("Instrument autoinsert mode %s", |
| 949 |
state.autoinsertInstrument ? "enabled." : "disabled.")); |
| 950 |
return OK; |
| 951 |
case SDLK_LESS: |
| 952 |
state.octave = clamp(--state.octave, 0, 6); |
| 953 |
break; |
| 954 |
case SDLK_GREATER: |
| 955 |
state.octave = clamp(++state.octave, 0, 6); |
| 956 |
break; |
| 957 |
default: |
| 958 |
break; |
| 959 |
} |
| 960 |
|
| 961 |
int r = activeInput.keypress(key); |
| 962 |
|
| 963 |
return r; |
| 964 |
} |
| 965 |
|
| 966 |
void valueChanged() { |
| 967 |
UndoValue v; |
| 968 |
import std.typecons; |
| 969 |
v.array = UndoValue.Array(activeInput.element.data.dup, |
| 970 |
element.data); |
| 971 |
|
| 972 |
com.session.insertUndo(this, v); |
| 973 |
} |
| 974 |
|
| 975 |
override protected final void undo(UndoValue entry) { |
| 976 |
ubyte[] data = entry.array.target; |
| 977 |
ubyte[] target = entry.array.source; |
| 978 |
target[] = data; |
| 979 |
} |
| 980 |
|
| 981 |
override protected final UndoValue createRedoState(UndoValue value) { |
| 982 |
value.array.target = value.array.source.dup; |
| 983 |
return value; |
| 984 |
} |
| 985 |
|
| 986 |
void columnReset(int foo) { |
| 987 |
if(foo == 0) { |
| 988 |
inputInstrument.nibble = 0; |
| 989 |
inputCmd.nibble = 0; |
| 990 |
activeInputNo = 0; |
| 991 |
activeInput = inputNote; |
| 992 |
} |
| 993 |
else { |
| 994 |
inputInstrument.nibble = 1; |
| 995 |
inputCmd.nibble = 1; |
| 996 |
activeInputNo = 3; |
| 997 |
activeInput = inputCmd; |
| 998 |
} |
| 999 |
} |
| 1000 |
|
| 1001 |
// nibble arg is for END key |
| 1002 |
void columnReset(int foo, int nibble) { |
| 1003 |
if(foo == 0) { |
| 1004 |
inputInstrument.nibble = 0; |
| 1005 |
inputCmd.nibble = 0; |
| 1006 |
activeInputNo = 0; |
| 1007 |
activeInput = inputNote; |
| 1008 |
} |
| 1009 |
else { |
| 1010 |
inputInstrument.nibble = 1; |
| 1011 |
inputCmd.nibble = nibble; |
| 1012 |
activeInputNo = 3; |
| 1013 |
activeInput = inputCmd; |
| 1014 |
} |
| 1015 |
} |
| 1016 |
|
| 1017 |
override int step(int st) { |
| 1018 |
int r = activeInput.step(st); |
| 1019 |
if(r == WRAPR) { |
| 1020 |
//activeInput.nibble = 0; |
| 1021 |
foreach(inp; inputters) { |
| 1022 |
inp.nibble = 0; |
| 1023 |
} |
| 1024 |
|
| 1025 |
activeInputNo++; |
| 1026 |
if(activeInputNo >= inputters.length) { |
| 1027 |
activeInputNo = 0; |
| 1028 |
activeInput = inputters[activeInputNo]; |
| 1029 |
return WRAPR; |
| 1030 |
} |
| 1031 |
activeInput = inputters[activeInputNo]; |
| 1032 |
activeInput.nibble = 0; |
| 1033 |
|
| 1034 |
} |
| 1035 |
else if(r == WRAPL) { |
| 1036 |
foreach(inp; inputters) { |
| 1037 |
inp.nibble = inp.width - 1; |
| 1038 |
} |
| 1039 |
activeInputNo--; |
| 1040 |
if(activeInputNo < 0) { |
| 1041 |
activeInputNo = cast(int)(inputters.length - 1); |
| 1042 |
activeInput = inputters[activeInputNo]; |
| 1043 |
|
| 1044 |
return WRAPL; |
| 1045 |
} |
| 1046 |
activeInput.nibble = activeInput.width - 1; |
| 1047 |
activeInput = inputters[activeInputNo]; |
| 1048 |
} |
| 1049 |
return OK; |
| 1050 |
} |
| 1051 |
|
| 1052 |
override void update() { |
| 1053 |
screen.cprint(pointerX, pointerY, 1, -1, element.toPlainString()); |
| 1054 |
|
| 1055 |
assert(activeInput == inputters[activeInputNo]); |
| 1056 |
int xofs = [0, 2, 4, 7][activeInputNo]; |
| 1057 |
cursor.set(pointerX + xofs + activeInput.nibble, pointerY); |
| 1058 |
} |
| 1059 |
} |
| 1060 |
|
| 1061 |
class InputSpecial : InputValue { |
| 1062 |
this(ubyte[] p) { |
| 1063 |
super(p, 5); |
| 1064 |
} |
| 1065 |
|
| 1066 |
override void setOutput(ubyte[] p) { |
| 1067 |
} |
| 1068 |
|
| 1069 |
override int setValue(int v) { |
| 1070 |
inarray[nibble] = v & 15; |
| 1071 |
return OK; |
| 1072 |
} |
| 1073 |
|
| 1074 |
override void update() { |
| 1075 |
static immutable offsets = [0, 2, 3, 5, 6]; |
| 1076 |
screen.cprint(x, y, 1, -1, format("%01X-%02X %02X",inarray[0],toInt(inarray[1..3]),toInt(inarray[3..5]))); |
| 1077 |
cursor.set(pointerX + offsets[nibble], pointerY); |
| 1078 |
} |
| 1079 |
} |