From d3c1276271ccd895985575bac95d4b9ee4019276 Mon Sep 17 00:00:00 2001 From: "BADIM-PC\\Vadim" Date: Mon, 12 Feb 2018 22:05:30 +0300 Subject: [PATCH] music editor keybaord --- src/music.c | 514 +++++++++++++++++++++++++--------------------------- 1 file changed, 244 insertions(+), 270 deletions(-) diff --git a/src/music.c b/src/music.c index 9737315..2d28bee 100644 --- a/src/music.c +++ b/src/music.c @@ -738,257 +738,253 @@ static void updateSelection(Music* music) resetSelection(music); } -// static void processTrackerKeydown(Music* music, SDL_Keysym* keysum) -// { -// tic_keycode keycode = keysum->sym; -// SDL_Scancode scancode = keysum->scancode; +static void processTrackerKeyboard(Music* music) +{ + tic_mem* tic = music->tic; -// bool shift = SDL_GetModState() & KMOD_SHIFT; + if(tic->ram.input.keyboard.data == 0) + { + music->tracker.note = -1; + s32 channel = music->tracker.col / CHANNEL_COLS; + music->tic->api.sfx_stop(music->tic, channel); + return; + } -// if(shift) -// { -// switch (keycode) -// { -// case SDLK_UP: -// case SDLK_DOWN: -// case SDLK_LEFT: -// case SDLK_RIGHT: -// case SDLK_HOME: -// case SDLK_END: -// case SDLK_PAGEUP: -// case SDLK_PAGEDOWN: -// case SDLK_TAB: -// checkSelection(music); -// } -// } + bool shift = tic->api.key(tic, tic_key_shift); -// switch (keycode) -// { -// case SDLK_UP: upRow(music); break; -// case SDLK_DOWN: downRow(music); break; -// case SDLK_LEFT: leftCol(music); break; -// case SDLK_RIGHT: rightCol(music); break; -// case SDLK_HOME: goHome(music); break; -// case SDLK_END: goEnd(music); break; -// case SDLK_PAGEUP: pageUp(music); break; -// case SDLK_PAGEDOWN: pageDown(music); break; -// case SDLK_TAB: doTab(music); break; -// case SDLK_DELETE: -// deleteSelection(music); -// history_add(music->history); -// downRow(music); -// break; -// case SDLK_SPACE: playNote(music); break; -// case SDLK_RETURN: -// case SDLK_KP_ENTER: -// { -// const tic_music_pos* pos = getMusicPos(music); -// pos->track < 0 -// ? (shift ? playFrameRow(music) : playFrame(music)) -// : stopTrack(music); -// } -// break; -// } + if(shift) + { + if(isKeyBeenPressed(tic_key_up) + || isKeyBeenPressed(tic_key_down) + || isKeyBeenPressed(tic_key_left) + || isKeyBeenPressed(tic_key_right) + || isKeyBeenPressed(tic_key_home) + || isKeyBeenPressed(tic_key_end) + || isKeyBeenPressed(tic_key_pageup) + || isKeyBeenPressed(tic_key_pagedown) + || isKeyBeenPressed(tic_key_tab)) + { + checkSelection(music); + } + } -// if(shift) -// { -// switch (keycode) -// { -// case SDLK_UP: -// case SDLK_DOWN: -// case SDLK_LEFT: -// case SDLK_RIGHT: -// case SDLK_HOME: -// case SDLK_END: -// case SDLK_PAGEUP: -// case SDLK_PAGEDOWN: -// case SDLK_TAB: -// updateSelection(music); -// } -// } -// else resetSelection(music); + if(isKeyBeenPressed(tic_key_up)) upRow(music); + else if(isKeyBeenPressed(tic_key_down)) downRow(music); + else if(isKeyBeenPressed(tic_key_left)) leftCol(music); + else if(isKeyBeenPressed(tic_key_right)) rightCol(music); + else if(isKeyBeenPressed(tic_key_home)) goHome(music); + else if(isKeyBeenPressed(tic_key_end)) goEnd(music); + else if(isKeyBeenPressed(tic_key_pageup)) pageUp(music); + else if(isKeyBeenPressed(tic_key_pagedown)) pageDown(music); + else if(isKeyBeenPressed(tic_key_tab)) doTab(music); + else if(isKeyBeenPressed(tic_key_delete)) + { + deleteSelection(music); + history_add(music->history); + downRow(music); + } + else if(isKeyBeenPressed(tic_key_space)) playNote(music); + else if(isKeyBeenPressed(tic_key_return)) + { + const tic_music_pos* pos = getMusicPos(music); + pos->track < 0 + ? (shift ? playFrameRow(music) : playFrame(music)) + : stopTrack(music); + } -// static const SDL_Scancode Piano[] = -// { -// SDL_SCANCODE_Z, -// SDL_SCANCODE_S, -// SDL_SCANCODE_X, -// SDL_SCANCODE_D, -// SDL_SCANCODE_C, -// SDL_SCANCODE_V, -// SDL_SCANCODE_G, -// SDL_SCANCODE_B, -// SDL_SCANCODE_H, -// SDL_SCANCODE_N, -// SDL_SCANCODE_J, -// SDL_SCANCODE_M, + if(shift) + { + if(isKeyBeenPressed(tic_key_up) + || isKeyBeenPressed(tic_key_down) + || isKeyBeenPressed(tic_key_left) + || isKeyBeenPressed(tic_key_right) + || isKeyBeenPressed(tic_key_home) + || isKeyBeenPressed(tic_key_end) + || isKeyBeenPressed(tic_key_pageup) + || isKeyBeenPressed(tic_key_pagedown) + || isKeyBeenPressed(tic_key_tab)) + { + updateSelection(music); + } + } + else resetSelection(music); -// // octave +1 -// SDL_SCANCODE_Q, -// SDL_SCANCODE_2, -// SDL_SCANCODE_W, -// SDL_SCANCODE_3, -// SDL_SCANCODE_E, -// SDL_SCANCODE_R, -// SDL_SCANCODE_5, -// SDL_SCANCODE_T, -// SDL_SCANCODE_6, -// SDL_SCANCODE_Y, -// SDL_SCANCODE_7, -// SDL_SCANCODE_U, -// }; + static const tic_keycode Piano[] = + { + tic_key_z, + tic_key_s, + tic_key_x, + tic_key_d, + tic_key_c, + tic_key_v, + tic_key_g, + tic_key_b, + tic_key_h, + tic_key_n, + tic_key_j, + tic_key_m, -// if (getChannelPattern(music)) -// { -// s32 col = music->tracker.col % CHANNEL_COLS; + // octave +1 + tic_key_q, + tic_key_2, + tic_key_w, + tic_key_3, + tic_key_e, + tic_key_r, + tic_key_5, + tic_key_t, + tic_key_6, + tic_key_y, + tic_key_7, + tic_key_u, + }; -// switch (col) -// { -// case ColumnNote: -// case ColumnSemitone: -// if (scancode == SDL_SCANCODE_1 || scancode == SDL_SCANCODE_A) -// { -// setStopNote(music); -// downRow(music); -// } -// else -// { -// tic_track_pattern* pattern = getChannelPattern(music); + if (getChannelPattern(music)) + { + s32 col = music->tracker.col % CHANNEL_COLS; -// for (s32 i = 0; i < COUNT_OF(Piano); i++) -// { -// if (scancode == Piano[i]) -// { -// s32 note = i % NOTES; + switch (col) + { + case ColumnNote: + case ColumnSemitone: + if (isKeyBeenPressed(tic_key_1) || isKeyBeenPressed(tic_key_a)) + { + setStopNote(music); + downRow(music); + } + else + { + tic_track_pattern* pattern = getChannelPattern(music); -// if(pattern->rows[music->tracker.row].note > NoteNone) -// { -// pattern->rows[music->tracker.row].note = note + NoteStart; -// playNote(music); -// } -// else -// { -// s32 octave = i / NOTES + music->tracker.last.octave; -// s32 volume = music->tracker.last.volume; -// s32 sfx = music->tracker.last.sfx; -// setNote(music, note, octave, volume, sfx); -// } + for (s32 i = 0; i < COUNT_OF(Piano); i++) + { + if (isKeyBeenPressed(Piano[i])) + { + s32 note = i % NOTES; -// downRow(music); + if(pattern->rows[music->tracker.row].note > NoteNone) + { + pattern->rows[music->tracker.row].note = note + NoteStart; + playNote(music); + } + else + { + s32 octave = i / NOTES + music->tracker.last.octave; + s32 volume = music->tracker.last.volume; + s32 sfx = music->tracker.last.sfx; + setNote(music, note, octave, volume, sfx); + } -// break; -// } - -// } -// } -// break; -// case ColumnOctave: -// if(getNote(music) >= 0) -// { -// s32 octave = -1; -// if (keycode >= SDLK_1 && keycode <= SDLK_8) octave = keycode - SDLK_1; -// if (keycode >= SDLK_KP_1 && keycode <= SDLK_KP_8) octave = keycode - SDLK_KP_1; + downRow(music); -// if(octave >= 0) -// { -// setOctave(music, octave); -// downRow(music); -// } -// } -// break; -// case ColumnSfxHi: -// case ColumnSfxLow: -// if(getNote(music) >= 0) -// { -// s32 val = -1; + break; + } + } + } + break; + case ColumnOctave: + if(getNote(music) >= 0) + { + s32 octave = -1; + + char sym = getKeyboardText(); + + if(sym >= '1' && sym <= '8') octave = sym - '1'; + + if(octave >= 0) + { + setOctave(music, octave); + downRow(music); + } + } + break; + case ColumnSfxHi: + case ColumnSfxLow: + if(getNote(music) >= 0) + { + s32 val = -1; + + char sym = getKeyboardText(); -// if (keycode >= SDLK_0 && keycode <= SDLK_9) val = keycode - SDLK_0; -// if (keycode >= SDLK_KP_1 && keycode <= SDLK_KP_9) val = keycode - SDLK_KP_1 + 1; -// if (keycode == SDLK_KP_0) val = 0; + if (sym >= '0' && sym <= '9') val = sym - '0'; -// if(val >= 0) -// { -// enum {Base = 10}; -// s32 sfx = getSfx(music); + if(val >= 0) + { + enum {Base = 10}; + s32 sfx = getSfx(music); -// sfx = col == 3 -// ? val * Base + sfx % Base -// : sfx / Base * Base + val % Base; + sfx = col == 3 + ? val * Base + sfx % Base + : sfx / Base * Base + val % Base; -// setSfx(music, sfx); + setSfx(music, sfx); -// if(col == 3) rightCol(music); -// else downRow(music), leftCol(music); -// } -// } -// break; -// case ColumnVolume: -// if (getNote(music) >= 0) -// { -// s32 val = -1; + if(col == 3) rightCol(music); + else downRow(music), leftCol(music); + } + } + break; + case ColumnVolume: + if (getNote(music) >= 0) + { + s32 val = -1; -// if(keycode >= SDLK_0 && keycode <= SDLK_9) val = keycode - SDLK_0; -// if(keycode >= SDLK_a && keycode <= SDLK_f) val = keycode - SDLK_a + 10; -// if(keycode >= SDLK_KP_1 && keycode <= SDLK_KP_9) val = keycode - SDLK_KP_1 + 1; -// if(keycode == SDLK_KP_0) val = 0; + char sym = getKeyboardText(); -// if(val >= 0) -// { -// setVolume(music, MAX_VOLUME - val); -// downRow(music); -// } -// } -// break; -// } + if(sym >= '0' && sym <= '9') val = sym - '0'; + if(sym >= 'a' && sym <= 'f') val = sym - 'a' + 10; -// history_add(music->history); -// } -// } + if(val >= 0) + { + setVolume(music, MAX_VOLUME - val); + downRow(music); + } + } + break; + } -// static void processPatternKeydown(Music* music, SDL_Keysym* keysum) -// { -// tic_keycode keycode = keysum->sym; + history_add(music->history); + } +} -// s32 channel = music->tracker.col / CHANNEL_COLS; +static void processPatternKeyboard(Music* music) +{ + s32 channel = music->tracker.col / CHANNEL_COLS; -// switch (keycode) -// { -// case SDLK_DELETE: setChannelPatternValue(music, 0, channel); break; -// case SDLK_TAB: nextPattern(music); break; -// case SDLK_DOWN: -// case SDLK_KP_ENTER: -// case SDLK_RETURN: music->tracker.row = music->tracker.scroll; break; -// case SDLK_LEFT: patternColLeft(music); break; -// case SDLK_RIGHT: patternColRight(music); break; -// default: -// { -// s32 val = -1; + if(isKeyBeenPressed(tic_key_delete)) setChannelPatternValue(music, 0, channel); + else if(isKeyBeenPressed(tic_key_tab)) nextPattern(music); + else if(isKeyBeenPressed(tic_key_left)) patternColLeft(music); + else if(isKeyBeenPressed(tic_key_right)) patternColRight(music); + else if(isKeyBeenPressed(tic_key_down) + || isKeyBeenPressed(tic_key_return)) + music->tracker.row = music->tracker.scroll; + else + { + s32 val = -1; -// if(keycode >= SDLK_0 && keycode <= SDLK_9) val = keycode - SDLK_0; -// if(keycode >= SDLK_KP_1 && keycode <= SDLK_KP_9) val = keycode - SDLK_KP_1 + 1; -// if(keycode == SDLK_KP_0) val = 0; + char sym = getKeyboardText(); + if(sym >= '0' && sym <= '9') val = sym - '0'; -// if(val >= 0) -// { -// enum {Base = 10}; -// s32 patternId = tic_tool_get_pattern_id(getTrack(music), music->tracker.frame, channel); + if(val >= 0) + { + enum {Base = 10}; + s32 patternId = tic_tool_get_pattern_id(getTrack(music), music->tracker.frame, channel); -// patternId = music->tracker.patternCol == 0 -// ? val * Base + patternId % Base -// : patternId / Base * Base + val % Base; + patternId = music->tracker.patternCol == 0 + ? val * Base + patternId % Base + : patternId / Base * Base + val % Base; -// if(patternId <= MUSIC_PATTERNS) -// { -// setChannelPatternValue(music, patternId, channel); + if(patternId <= MUSIC_PATTERNS) + { + setChannelPatternValue(music, patternId, channel); -// if(music->tracker.patternCol == 0) -// patternColRight(music); -// } -// } -// } -// } -// } + if(music->tracker.patternCol == 0) + patternColRight(music); + } + } + } +} static void selectAll(Music* music) { @@ -1003,45 +999,35 @@ static void selectAll(Music* music) updateSelection(music); } -// static void processKeydown(Music* music, SDL_Keysym* keysum) -// { -// tic_keycode keycode = keysum->sym; +static void processKeyboard(Music* music) +{ + tic_mem* tic = music->tic; -// switch(getClipboardEvent()) -// { -// case TIC_CLIPBOARD_CUT: copyToClipboard(music, true); break; -// case TIC_CLIPBOARD_COPY: copyToClipboard(music, false); break; -// case TIC_CLIPBOARD_PASTE: copyFromClipboard(music); break; -// default: break; -// } + switch(getClipboardEvent()) + { + case TIC_CLIPBOARD_CUT: copyToClipboard(music, true); break; + case TIC_CLIPBOARD_COPY: copyToClipboard(music, false); break; + case TIC_CLIPBOARD_PASTE: copyFromClipboard(music); break; + default: break; + } -// SDL_Keymod keymod = SDL_GetModState(); + bool ctrl = tic->api.key(tic, tic_key_ctrl); -// if (keymod & TIC_MOD_CTRL) -// { -// switch (keycode) -// { -// case SDLK_a: selectAll(music); break; -// case SDLK_z: undo(music); break; -// case SDLK_y: redo(music); break; -// case SDLK_UP: upFrame(music); break; -// case SDLK_DOWN: downFrame(music); break; -// } -// } -// else -// { -// music->tracker.row >= 0 -// ? processTrackerKeydown(music, keysum) -// : processPatternKeydown(music, keysum); -// } -// } - -// static void processKeyup(Music* music, SDL_Keysym* keysum) -// { -// music->tracker.note = -1; -// s32 channel = music->tracker.col / CHANNEL_COLS; -// music->tic->api.sfx_stop(music->tic, channel); -// } + if (ctrl) + { + if(isKeyBeenPressed(tic_key_a)) selectAll(music); + else if(isKeyBeenPressed(tic_key_z)) undo(music); + else if(isKeyBeenPressed(tic_key_y)) redo(music); + else if(isKeyBeenPressed(tic_key_up)) upFrame(music); + else if(isKeyBeenPressed(tic_key_down)) downFrame(music); + } + else + { + music->tracker.row >= 0 + ? processTrackerKeyboard(music) + : processPatternKeyboard(music); + } +} static void setIndex(Music* music, s32 delta, void* data) { @@ -1595,19 +1581,7 @@ static void drawTrackerLayout(Music* music) } } - // SDL_Event* event = NULL; - // while ((event = pollEvent())) - // { - // switch (event->type) - // { - // case SDL_KEYDOWN: - // processKeydown(music, &event->key.keysym); - // break; - // case SDL_KEYUP: - // processKeyup(music, &event->key.keysym); - // break; - // } - // } + processKeyboard(music); if(music->tracker.follow) {