| 1 | #include <stdint.h> |
| 2 | #include <stdbool.h> |
| 3 | #include <string.h> |
| 4 | #include "pt2_textout.h" |
| 5 | #include "pt2_visuals.h" |
| 6 | #include "pt2_helpers.h" |
| 7 | #include "pt2_replayer.h" |
| 8 | #include "pt2_bmp.h" |
| 9 | #include "pt2_mouse.h" |
| 10 | #include "pt2_edit.h" |
| 11 | #include "pt2_textedit.h" |
| 12 | #include "pt2_posed.h" |
| 13 | |
| 14 | static char posEdNames[MAX_PATTERNS][16]; |
| 15 | |
| 16 | void posEdScrollDown(void) |
| 17 | { |
| 18 | const uint16_t scrollAmount = mouse.rightButtonPressed ? 10 : 1; |
| 19 | |
| 20 | if (song->currPos < song->header.songLength-1) |
| 21 | { |
| 22 | int16_t pos = song->currPos + scrollAmount; |
| 23 | if (pos > song->header.songLength-1) |
| 24 | pos = song->header.songLength-1; |
| 25 | |
| 26 | modSetPos(pos, DONT_SET_ROW); |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | void posEdScrollUp(void) |
| 31 | { |
| 32 | const uint16_t scrollAmount = mouse.rightButtonPressed ? 10 : 1; |
| 33 | |
| 34 | if (song->currPos > 0) |
| 35 | { |
| 36 | int16_t pos = song->currPos - scrollAmount; |
| 37 | if (pos < 0) |
| 38 | pos = 0; |
| 39 | |
| 40 | modSetPos(pos, DONT_SET_ROW); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | void posEdPageUp(void) |
| 45 | { |
| 46 | const uint16_t scrollAmount = POSED_LIST_SIZE; |
| 47 | |
| 48 | if (song->currPos > 0) |
| 49 | { |
| 50 | int16_t pos = song->currPos - scrollAmount; |
| 51 | if (pos < 0) |
| 52 | pos = 0; |
| 53 | |
| 54 | modSetPos(pos, DONT_SET_ROW); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | void posEdPageDown(void) |
| 59 | { |
| 60 | const uint16_t scrollAmount = POSED_LIST_SIZE; |
| 61 | |
| 62 | if (song->currPos < song->header.songLength-1) |
| 63 | { |
| 64 | int16_t pos = song->currPos + scrollAmount; |
| 65 | if (pos > song->header.songLength-1) |
| 66 | pos = song->header.songLength-1; |
| 67 | |
| 68 | modSetPos(pos, DONT_SET_ROW); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | void posEdScrollToTop(void) |
| 73 | { |
| 74 | if (song->currPos > 0) |
| 75 | modSetPos(0, DONT_SET_ROW); |
| 76 | } |
| 77 | |
| 78 | void posEdScrollToBottom(void) |
| 79 | { |
| 80 | if (song->currPos < song->header.songLength-1) |
| 81 | modSetPos(song->header.songLength-1, DONT_SET_ROW); |
| 82 | } |
| 83 | |
| 84 | void posEdClearNames(void) |
| 85 | { |
| 86 | memset(posEdNames, 0, sizeof (posEdNames)); |
| 87 | |
| 88 | if (ui.posEdScreenShown) |
| 89 | ui.updatePosEd = true; |
| 90 | } |
| 91 | |
| 92 | void posEdEditName(void) |
| 93 | { |
| 94 | if (editor.currMode == MODE_IDLE || editor.currMode == MODE_EDIT) |
| 95 | { |
| 96 | const uint8_t pattern = (uint8_t)song->header.patternTable[song->currPos & 0x7F]; |
| 97 | |
| 98 | textEdit.textStartPtr = posEdNames[pattern]; |
| 99 | textEdit.textEndPtr = posEdNames[pattern] + 15; |
| 100 | textEdit.numBlocks = 15; |
| 101 | textEdit.cursorStartX = 184; |
| 102 | textEdit.cursorStartY = 58; |
| 103 | textEdit.scrollable = true; |
| 104 | enterTextEditMode(PTB_PE_PATTNAME); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | void posEdInsert(void) |
| 109 | { |
| 110 | if ((editor.currMode == MODE_IDLE || editor.currMode == MODE_EDIT) && song->header.songLength < 128) |
| 111 | { |
| 112 | for (int32_t i = 0; i < 127-song->currPos; i++) |
| 113 | song->header.patternTable[127-i] = song->header.patternTable[(127-i)-1]; |
| 114 | song->header.patternTable[song->currPos] = 0; |
| 115 | |
| 116 | song->header.songLength++; |
| 117 | if (song->currPos > song->header.songLength-1) |
| 118 | editor.currPosEdPattDisp = &song->header.patternTable[song->header.songLength-1]; |
| 119 | |
| 120 | updateWindowTitle(MOD_IS_MODIFIED); |
| 121 | |
| 122 | ui.updateSongSize = true; |
| 123 | ui.updateSongLength = true; |
| 124 | ui.updateSongPattern = true; |
| 125 | |
| 126 | if (ui.posEdScreenShown) |
| 127 | ui.updatePosEd = true; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | void posEdDelete(void) |
| 132 | { |
| 133 | if ((editor.currMode == MODE_IDLE || editor.currMode == MODE_EDIT) && song->header.songLength > 1) |
| 134 | { |
| 135 | for (int32_t i = 0; i < 128-song->currPos; i++) |
| 136 | song->header.patternTable[song->currPos+i] = song->header.patternTable[song->currPos+i+1]; |
| 137 | song->header.patternTable[127] = 0; |
| 138 | |
| 139 | song->header.songLength--; |
| 140 | if (song->currPos > song->header.songLength-1) |
| 141 | editor.currPosEdPattDisp = &song->header.patternTable[song->header.songLength-1]; |
| 142 | |
| 143 | updateWindowTitle(MOD_IS_MODIFIED); |
| 144 | |
| 145 | ui.updateSongSize = true; |
| 146 | ui.updateSongLength = true; |
| 147 | ui.updateSongPattern = true; |
| 148 | |
| 149 | if (ui.posEdScreenShown) |
| 150 | ui.updatePosEd = true; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | void posEdToggle(void) |
| 155 | { |
| 156 | ui.posEdScreenShown ^= 1; |
| 157 | if (ui.posEdScreenShown) |
| 158 | { |
| 159 | renderPosEdScreen(); |
| 160 | ui.updatePosEd = true; |
| 161 | } |
| 162 | else |
| 163 | { |
| 164 | displayMainScreen(); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | void renderPosEdScreen(void) |
| 169 | { |
| 170 | blit32(120, 0, 200, 99, posEdBMP); |
| 171 | ui.updatePosEd = true; |
| 172 | } |
| 173 | |
| 174 | static void drawPosEdName(int32_t entry, int32_t y, bool selected) |
| 175 | { |
| 176 | const uint8_t pattern = (uint8_t)song->header.patternTable[entry & 0x7F]; |
| 177 | for (int32_t i = 0; i < 15; i++) |
| 178 | { |
| 179 | const int32_t x = 184 + (i * FONT_CHAR_W); |
| 180 | |
| 181 | char ch; |
| 182 | |
| 183 | if (ui.editTextFlag) |
| 184 | ch = posEdNames[pattern][textEdit.scrollOffset + i]; |
| 185 | else |
| 186 | ch = posEdNames[pattern][i]; |
| 187 | |
| 188 | if (ch == '\0') |
| 189 | break; |
| 190 | |
| 191 | if (selected) |
| 192 | charOut(x, y, ch, video.palette[PAL_GENTXT]); |
| 193 | else |
| 194 | charOut(x, y, ch, video.palette[PAL_QADSCP]); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | void updatePosEd(void) |
| 199 | { |
| 200 | if (!ui.updatePosEd || !ui.posEdScreenShown || ui.askBoxShown) |
| 201 | return; |
| 202 | |
| 203 | ui.updatePosEd = false; |
| 204 | |
| 205 | // clear entries first |
| 206 | fillRect(128, 23, (22*FONT_CHAR_W)-1, ((FONT_CHAR_H+1)*5)-1, video.palette[PAL_BACKGRD]); |
| 207 | fillRect(128, 53, (22*FONT_CHAR_W)-1, FONT_CHAR_H, video.palette[PAL_GENBKG]); |
| 208 | fillRect(128, 59, (22*FONT_CHAR_W)-1, ((FONT_CHAR_H+1)*6)-1, video.palette[PAL_BACKGRD]); |
| 209 | |
| 210 | for (int32_t i = 0; i < POSED_LIST_SIZE; i++) |
| 211 | { |
| 212 | const int32_t entry = song->currPos - (5 - i); |
| 213 | if (entry < 0 || entry >= song->header.songLength) |
| 214 | continue; |
| 215 | |
| 216 | const int32_t pattern = song->header.patternTable[entry]; |
| 217 | const int32_t yPos = 23 + (i * (FONT_CHAR_H+1)); |
| 218 | const bool selected = (i == 5); |
| 219 | const uint32_t textColor = selected ? video.palette[PAL_GENTXT] : video.palette[PAL_QADSCP]; |
| 220 | |
| 221 | printThreeDecimals(128, yPos, entry, textColor); |
| 222 | printTwoDecimals(160, yPos, selected ? *editor.currPosEdPattDisp : pattern, textColor); |
| 223 | drawPosEdName(entry, yPos, selected); |
| 224 | } |
| 225 | |
| 226 | // kludge to fix bottom part of text edit marker |
| 227 | if (ui.editTextFlag && (textEdit.object == PTB_PE_PATT || textEdit.object == PTB_PE_PATTNAME)) |
| 228 | renderTextEditCursor(); |
| 229 | } |