| 1 | #include "TileViewer.h" |
| 2 | #include "ui_TileViewer.h" |
| 3 | #include "VramTiledView.h" |
| 4 | #include "VDPDataStore.h" |
| 5 | #include "PaletteDialog.h" |
| 6 | #include "Convert.h" |
| 7 | #include <QMessageBox> |
| 8 | |
| 9 | |
| 10 | static uint8_t currentPalette[32] = { 0 }; |
| 11 | |
| 12 | TileViewer::TileViewer(QWidget* parent) |
| 13 | : QDialog(parent), image4label(32, 32, QImage::Format_RGB32) |
| 14 | { |
| 15 | setupUi(this); |
| 16 | // No QMetaObject::connectSlotsByName available now since we dropped the slots: in the header; |
| 17 | connect(cb_tilemapsource, qOverload<int>(&QComboBox::currentIndexChanged), this, &TileViewer::on_cb_tilemapsource_currentIndexChanged); |
| 18 | connect(cb_screen, qOverload<int>(&QComboBox::currentIndexChanged) ,this ,&TileViewer::on_cb_screen_currentIndexChanged); |
| 19 | connect(le_nametable, &QLineEdit::textChanged, this, &TileViewer::on_le_nametable_textChanged); |
| 20 | connect(le_colortable, &QLineEdit::textChanged, this, &TileViewer::on_le_colortable_textChanged); |
| 21 | connect(le_patterntable, &QLineEdit::textChanged, this, &TileViewer::on_le_patterntable_textChanged); |
| 22 | |
| 23 | connect(cb_color0, &QCheckBox::stateChanged, this, &TileViewer::on_cb_color0_stateChanged); |
| 24 | connect(useVDPRegisters, &QCheckBox::stateChanged, this, &TileViewer::on_useVDPRegisters_stateChanged); |
| 25 | |
| 26 | connect(editPaletteButton, &QPushButton::clicked, this, &TileViewer::on_editPaletteButton_clicked); |
| 27 | connect(useVDPPalette, &QCheckBox::stateChanged, this, &TileViewer::on_useVDPPalette_stateChanged); |
| 28 | connect(zoomLevel, qOverload<double>(&QDoubleSpinBox::valueChanged), this, &TileViewer::on_zoomLevel_valueChanged); |
| 29 | |
| 30 | connect(cb_drawgrid, &QCheckBox::stateChanged, this, &TileViewer::on_cb_drawgrid_stateChanged); |
| 31 | connect(cb_highlight, &QCheckBox::stateChanged, this, &TileViewer::on_cb_highlight_stateChanged); |
| 32 | connect(sp_highlight, qOverload<int>(&QSpinBox::valueChanged), this, &TileViewer::on_sp_highlight_valueChanged); |
| 33 | connect(sp_bordercolor, qOverload<int>(&QSpinBox::valueChanged), this, &TileViewer::on_sp_bordercolor_valueChanged); |
| 34 | |
| 35 | connect(cb_blinkcolors, &QCheckBox::stateChanged, this, &TileViewer::on_cb_blinkcolors_stateChanged); |
| 36 | connect(cb_screenrows, qOverload<int>(&QComboBox::currentIndexChanged), this, &TileViewer::on_cb_screenrows_currentIndexChanged); |
| 37 | |
| 38 | |
| 39 | |
| 40 | const QFont fixedFont = QFontDatabase::systemFont(QFontDatabase::FixedFont); |
| 41 | plainTextEdit->setFont(fixedFont); |
| 42 | label_charpat->setFont(fixedFont); |
| 43 | label_charadr->setFont(fixedFont); |
| 44 | |
| 45 | // Now hook up some signals and slots. |
| 46 | // This way we have created the VDPDataStore::instance before our imagewidget. |
| 47 | // This allows the VDPDatastore to start asking for data as quickly as possible. |
| 48 | connect(refreshButton, &QPushButton::clicked, |
| 49 | &VDPDataStore::instance(), &VDPDataStore::refresh); |
| 50 | connect(&VDPDataStore::instance(), &VDPDataStore::dataRefreshed, |
| 51 | this, &TileViewer::VDPDataStoreDataRefreshed); |
| 52 | |
| 53 | imageWidget = new VramTiledView(); |
| 54 | QSizePolicy sizePolicy1(QSizePolicy::Fixed, QSizePolicy::Fixed); |
| 55 | sizePolicy1.setHorizontalStretch(0); |
| 56 | sizePolicy1.setVerticalStretch(0); |
| 57 | sizePolicy1.setHeightForWidth(imageWidget->sizePolicy().hasHeightForWidth()); |
| 58 | imageWidget->setSizePolicy(sizePolicy1); |
| 59 | imageWidget->setMinimumSize(QSize(256, 212)); |
| 60 | connect(&VDPDataStore::instance(), &VDPDataStore::dataRefreshed, |
| 61 | imageWidget, &VramTiledView::refresh); |
| 62 | |
| 63 | scrollArea->setWidget(imageWidget); |
| 64 | |
| 65 | imageWidget->setPaletteSource(VDPDataStore::instance().getDefaultPalettePointer()); |
| 66 | imageWidget->setUseBlink(cb_blinkcolors->isChecked()); |
| 67 | imageWidget->setDrawGrid(cb_drawgrid->isChecked()); |
| 68 | |
| 69 | connect(imageWidget, &VramTiledView::highlightCount, |
| 70 | this, &TileViewer::highlightInfo); |
| 71 | connect(imageWidget, &VramTiledView::imageHovered, |
| 72 | this, &TileViewer::imageMouseOver); |
| 73 | connect(imageWidget, &VramTiledView::imageClicked, |
| 74 | this, &TileViewer::displayCharInfo); |
| 75 | |
| 76 | VDPDataStore::instance().refresh(); |
| 77 | } |
| 78 | |
| 79 | void TileViewer::VDPDataStoreDataRefreshed() |
| 80 | { |
| 81 | const auto* vram = VDPDataStore::instance().getVramPointer(); |
| 82 | imageWidget->setVramSource(vram); |
| 83 | |
| 84 | if (useVDPPalette->isChecked()) { |
| 85 | imageWidget->setPaletteSource(VDPDataStore::instance().getPalettePointer()); |
| 86 | } |
| 87 | decodeVDPregs(); |
| 88 | } |
| 89 | |
| 90 | void TileViewer::highlightInfo(uint8_t character, int count) |
| 91 | { |
| 92 | label_highligtinfo->setText( |
| 93 | (count == 0) ? QString("%1 (not in nametabel)").arg(hexValue(character, 2)) |
| 94 | : QString("%1 (%2 in nametabel)").arg(hexValue(character, 2)).arg(count)); |
| 95 | } |
| 96 | |
| 97 | void TileViewer::update_label_characterimage() |
| 98 | { |
| 99 | imageWidget->drawCharAtImage(mouseOverChar, mouseOverX, mouseOverY, image4label); |
| 100 | label_characterimage->setPixmap(QPixmap::fromImage(image4label)); |
| 101 | } |
| 102 | |
| 103 | void TileViewer::imageMouseOver(int screenX, int screenY, int character) |
| 104 | { |
| 105 | mouseOverX = screenX; |
| 106 | mouseOverY = screenY; |
| 107 | mouseOverChar = character; |
| 108 | update_label_characterimage(); |
| 109 | |
| 110 | label_mouseover->setText(QString("col %1 row %2").arg(screenX).arg(screenY)); |
| 111 | int screenMode = imageWidget->getScreenMode(); |
| 112 | |
| 113 | // Screen 2 and 4 have 3 pattern/colortables so add 256/512 to get to 2nd and 3th bank depending on y |
| 114 | //printf("screenmode %i screen y %i character %i \n", screenMode, screenY, character); |
| 115 | label_charpat->setText( |
| 116 | (screenMode == 2 || screenMode == 4) |
| 117 | ? QString("%1/%2 (%3/%4)").arg(hexValue(character & 255, 2)).arg(character >> 8).arg(character & 255).arg(character >> 8) |
| 118 | : QString("%1 (%2)").arg(hexValue(character, 2)).arg(character)); |
| 119 | imageWidget->drawCharAtImage(character, screenX, screenY, image4label); |
| 120 | label_characterimage->setPixmap(QPixmap::fromImage(image4label)); |
| 121 | |
| 122 | auto nametable = imageWidget->getNameTableAddress(); |
| 123 | auto patterntable = imageWidget->getPatternTableAddress(); |
| 124 | auto colortable = imageWidget->getColorTableAddress(); |
| 125 | |
| 126 | QString pgt = hexValue(patterntable + 8 * character, 4); |
| 127 | QString ct("-"); |
| 128 | //printf("Tileviewer screenmode %i", screenMode); |
| 129 | switch (screenMode) { |
| 130 | case 0: |
| 131 | case 3: |
| 132 | //no colortable used in these modes |
| 133 | break; |
| 134 | case 1: |
| 135 | ct = hexValue(colortable + (character >> 3), 4); |
| 136 | break; |
| 137 | case 2: |
| 138 | case 4: |
| 139 | //regular 8 bytes per char colortable, show adr of row 0 |
| 140 | ct = hexValue(colortable + 8 * character, 4); |
| 141 | break; |
| 142 | case 80: |
| 143 | //show the coloraddres for the blink info |
| 144 | ct = hexValue(colortable + (screenX >> 3) + 10 * screenY, 4); |
| 145 | break; |
| 146 | } |
| 147 | QString nt(""); |
| 148 | if (cb_tilemapsource->currentIndex() > 0) { |
| 149 | int scrWidth = screenMode == 80 ? 80 |
| 150 | : screenMode == 0 ? 40 |
| 151 | : 32; |
| 152 | nt = QString("\nNT: %1 (base+%2) )") |
| 153 | .arg(hexValue(nametable + scrWidth * screenY + screenX, 4)) |
| 154 | .arg(hexValue(scrWidth * screenY + screenX, 3)); |
| 155 | } |
| 156 | label_charadr->setText(QString("PGT: %1, CT: %2%3").arg(pgt).arg(ct).arg(nt)); |
| 157 | } |
| 158 | |
| 159 | void TileViewer::decodeVDPregs() |
| 160 | { |
| 161 | const uint8_t* regs = VDPDataStore::instance().getRegsPointer(); |
| 162 | |
| 163 | // // Get current screenmode |
| 164 | // static const int bits_modetxt[32] = { |
| 165 | // 1, 3, 0, 255, 2, 255, 255, 255, |
| 166 | // 4, 255, 80, 255, 5, 255, 255, 255, |
| 167 | // 6, 255, 255, 255, 7, 255, 255, 255, |
| 168 | // 255, 255, 255, 255, 8, 255, 255, 255, |
| 169 | // }; |
| 170 | |
| 171 | //int v = ((regs[0] & 0x0E) << 1) | ((regs[1] & 0x10) >> 4) | ((regs[1] & 0x08) >> 2); |
| 172 | int v = ((regs[0] & 0x0E) << 1) | ((regs[1] & 0x18) >> 3); |
| 173 | |
| 174 | label_M1->setEnabled(v & 0x02); |
| 175 | label_M2->setEnabled(v & 0x01); |
| 176 | label_M3->setEnabled(v & 0x04); |
| 177 | label_M4->setEnabled(v & 0x08); |
| 178 | label_M5->setEnabled(v & 0x10); |
| 179 | |
| 180 | QString modeText; |
| 181 | int cbIndex = 6; // "not tile based" |
| 182 | int reg2mask = 255; // mask for nametable (aka pattern layout table) |
| 183 | int reg3mask = 255; // mask for colortable |
| 184 | int reg4mask = 255; // mask for patterntable |
| 185 | bool usesBlink = false; |
| 186 | switch (v) { |
| 187 | case 0: modeText = "Graphic1"; cbIndex = 2; break; |
| 188 | case 1: modeText = "MultiColor"; cbIndex = 4; break; |
| 189 | case 2: modeText = "Text1"; cbIndex = 0; reg3mask = 0x80; break; |
| 190 | case 4: modeText = "Graphic2"; cbIndex = 3; reg3mask = 0x80; reg4mask = 0x3c; break; |
| 191 | case 8: modeText = "Graphic3"; cbIndex = 5; reg3mask = 0x80; reg4mask = 0x3c; break; |
| 192 | case 10: modeText = "Text2"; cbIndex = 1; reg3mask = 0xf8; reg2mask = 0xfc; usesBlink = true; break; |
| 193 | case 12: modeText = "Graphic4"; break; |
| 194 | case 16: modeText = "Graphic5"; break; |
| 195 | case 20: modeText = "Graphic6"; break; |
| 196 | case 28: modeText = "Graphic7"; break; |
| 197 | default: modeText = "unknown"; break; |
| 198 | } |
| 199 | label_screenmode->setText(modeText); |
| 200 | |
| 201 | if (useVDPRegisters->isChecked()) { |
| 202 | cb_screen->setCurrentIndex(cbIndex); |
| 203 | int nameTable = (regs[2] & reg2mask) << 10; |
| 204 | int patternTable = (regs[4] & reg4mask) << 11; |
| 205 | int colorTable = ((regs[3] & reg3mask) << 6) | (regs[10] << 14); |
| 206 | le_nametable->setText(hexValue(nameTable, 4)); |
| 207 | le_patterntable->setText(hexValue(patternTable, 4)); |
| 208 | le_colortable->setText(hexValue(colorTable, 4)); |
| 209 | cb_blinkcolors->setChecked(usesBlink ? regs[13] & 0xf0 : false); |
| 210 | cb_screenrows->setCurrentIndex((regs[9] & 128) ? 1 : 0 ); |
| 211 | sp_bordercolor->setValue(regs[7] & 15); |
| 212 | cb_color0->setChecked(regs[8] & 32); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | void TileViewer::refresh() |
| 217 | { |
| 218 | // All of the code is in the VDPDataStore; |
| 219 | VDPDataStore::instance().refresh(); |
| 220 | } |
| 221 | |
| 222 | void TileViewer::displayCharInfo(int screenX, int screenY, int character, const QString& textInfo) |
| 223 | { |
| 224 | label_charpat->setText(QString::number(character)); |
| 225 | plainTextEdit->setPlainText(QString("info for character %1 (%2)\n%3") |
| 226 | .arg(character) |
| 227 | .arg(hexValue(character, 2)) |
| 228 | .arg(textInfo)); |
| 229 | imageWidget->drawCharAtImage(character, screenX, screenY, image4label); |
| 230 | label_clickedcharimage->setPixmap(QPixmap::fromImage(image4label)); |
| 231 | } |
| 232 | |
| 233 | void TileViewer::on_cb_tilemapsource_currentIndexChanged(int index) |
| 234 | { |
| 235 | imageWidget->setTabletoShow(index); |
| 236 | } |
| 237 | |
| 238 | void TileViewer::on_cb_screen_currentIndexChanged(int index) |
| 239 | { |
| 240 | int i = [&] { |
| 241 | switch (index) { |
| 242 | case 0: return 0; |
| 243 | case 1: return 80; |
| 244 | case 2: return 1; |
| 245 | case 3: return 2; |
| 246 | case 4: return 3; |
| 247 | case 5: return 4; |
| 248 | case 6: return 255; |
| 249 | default: return 0; |
| 250 | } |
| 251 | }(); |
| 252 | imageWidget->setScreenMode(i); |
| 253 | // blink checkbox depends on screenMode and manuel usage |
| 254 | cb_blinkcolors->setEnabled(i == 80 && !useVDPRegisters->isChecked()); |
| 255 | } |
| 256 | |
| 257 | void TileViewer::on_le_nametable_textChanged(const QString& text) |
| 258 | { |
| 259 | auto font = le_nametable->font(); |
| 260 | if (auto i = stringToValue<int>(text)) { |
| 261 | imageWidget->setNameTableAddress(*i); |
| 262 | font.setItalic(false); |
| 263 | } else { |
| 264 | font.setItalic(true); |
| 265 | } |
| 266 | le_nametable->setFont(font); |
| 267 | } |
| 268 | |
| 269 | void TileViewer::on_le_colortable_textChanged(const QString& text) |
| 270 | { |
| 271 | auto font = le_colortable->font(); |
| 272 | if (auto i = stringToValue<int>(text)) { |
| 273 | imageWidget->setColorTableAddress(*i); |
| 274 | font.setItalic(false); |
| 275 | } else { |
| 276 | font.setItalic(true); |
| 277 | } |
| 278 | le_colortable->setFont(font); |
| 279 | } |
| 280 | |
| 281 | void TileViewer::on_le_patterntable_textChanged(const QString& text) |
| 282 | { |
| 283 | auto font = le_patterntable->font(); |
| 284 | if (auto i = stringToValue<int>(text)) { |
| 285 | imageWidget->setPatternTableAddress(*i); |
| 286 | font.setItalic(false); |
| 287 | } else { |
| 288 | font.setItalic(true); |
| 289 | } |
| 290 | le_patterntable->setFont(font); |
| 291 | } |
| 292 | |
| 293 | void TileViewer::on_cb_color0_stateChanged(int state) |
| 294 | { |
| 295 | imageWidget->setTpBit(state != Qt::Unchecked); |
| 296 | } |
| 297 | |
| 298 | void TileViewer::on_useVDPRegisters_stateChanged(int state) |
| 299 | { |
| 300 | bool useManual = state == Qt::Unchecked; |
| 301 | const uint8_t* regs = VDPDataStore::instance().getRegsPointer(); |
| 302 | decodeVDPregs(); |
| 303 | cb_screen->setEnabled(useManual); |
| 304 | le_colortable->setEnabled(useManual); |
| 305 | le_nametable->setEnabled(useManual); |
| 306 | le_patterntable->setEnabled(useManual); |
| 307 | cb_screenrows->setEnabled(useManual); |
| 308 | cb_screenrows->setCurrentIndex(regs[9] & 128 ? 1 : 0); |
| 309 | sp_bordercolor->setEnabled(useManual); |
| 310 | int v = ((regs[0] & 0x0E) << 1) | ((regs[1] & 0x18) >> 3); |
| 311 | cb_blinkcolors->setEnabled(useManual && v == 10); // screenMode == 80 |
| 312 | cb_color0->setEnabled(useManual); |
| 313 | } |
| 314 | |
| 315 | void TileViewer::on_editPaletteButton_clicked(bool /*checked*/) |
| 316 | { |
| 317 | auto* p = new PaletteDialog(); |
| 318 | p->setPalette(currentPalette); |
| 319 | p->setAutoSync(true); |
| 320 | connect(p, &PaletteDialog::paletteSynced, imageWidget, &VramTiledView::refresh); |
| 321 | connect(p, &PaletteDialog::paletteSynced, this, &TileViewer::update_label_characterimage); |
| 322 | p->show(); |
| 323 | //useVDPPalette->setChecked(false); |
| 324 | //QMessageBox::information( |
| 325 | // this, |
| 326 | // "Not yet implemented", |
| 327 | // "Sorry, the palette editor is not yet implemented, " |
| 328 | // "only disabling 'Use VDP palette registers' for now"); |
| 329 | } |
| 330 | |
| 331 | void TileViewer::on_useVDPPalette_stateChanged(int state) |
| 332 | { |
| 333 | const uint8_t* palette = VDPDataStore::instance().getPalettePointer(); |
| 334 | if (state) { |
| 335 | imageWidget->setPaletteSource(palette); |
| 336 | } else { |
| 337 | if (palette) memcpy(currentPalette, palette, 32); |
| 338 | imageWidget->setPaletteSource(currentPalette); |
| 339 | } |
| 340 | imageWidget->refresh(); |
| 341 | editPaletteButton->setEnabled(!state); |
| 342 | } |
| 343 | |
| 344 | void TileViewer::on_zoomLevel_valueChanged(double d) |
| 345 | { |
| 346 | imageWidget->setZoom(float(d)); |
| 347 | } |
| 348 | |
| 349 | void TileViewer::on_cb_drawgrid_stateChanged(int state) |
| 350 | { |
| 351 | imageWidget->setDrawGrid(state == Qt::Checked); |
| 352 | } |
| 353 | |
| 354 | void TileViewer::on_cb_highlight_stateChanged(int state) |
| 355 | { |
| 356 | imageWidget->setHighlightChar(state == Qt::Unchecked ? -1 : sp_highlight->value()); |
| 357 | } |
| 358 | |
| 359 | void TileViewer::on_sp_highlight_valueChanged(int i) |
| 360 | { |
| 361 | if (cb_highlight->isChecked()) { |
| 362 | imageWidget->setHighlightChar(i); |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | void TileViewer::on_sp_bordercolor_valueChanged(int i) |
| 367 | { |
| 368 | imageWidget->setBorderColor(i); |
| 369 | } |
| 370 | |
| 371 | void TileViewer::on_cb_blinkcolors_stateChanged(int arg1) |
| 372 | { |
| 373 | imageWidget->setUseBlink(arg1 == Qt::Checked); |
| 374 | } |
| 375 | |
| 376 | void TileViewer::on_cb_screenrows_currentIndexChanged(int index) |
| 377 | { |
| 378 | auto rows = cb_screenrows->isEnabled() ? (index == 0 ? 24.0f : |
| 379 | index == 1 ? 26.5f |
| 380 | : 32.0f) |
| 381 | : 0.0f; |
| 382 | imageWidget->setForcedScreenRows(rows); |
| 383 | } |