diff --git a/include/mainwindow.h b/include/mainwindow.h index 751c1e90..bd7f4408 100644 --- a/include/mainwindow.h +++ b/include/mainwindow.h @@ -63,15 +63,32 @@ public: Q_INVOKABLE void addRect(int x, int y, int width, int height, QString color = "#000000"); Q_INVOKABLE void addFilledRect(int x, int y, int width, int height, QString color = "#000000"); Q_INVOKABLE void addImage(int x, int y, QString filepath); + void refreshAfterPaletteChange(Tileset *tileset); void setTilesetPalette(Tileset *tileset, int paletteIndex, QList> colors); Q_INVOKABLE void setPrimaryTilesetPalette(int paletteIndex, QList> colors); + Q_INVOKABLE void setPrimaryTilesetPalettes(QList>> palettes); Q_INVOKABLE void setSecondaryTilesetPalette(int paletteIndex, QList> colors); - QJSValue getTilesetPalette(Tileset *tileset, int paletteIndex); + Q_INVOKABLE void setSecondaryTilesetPalettes(QList>> palettes); + QJSValue getTilesetPalette(QList> *palettes, int paletteIndex); + QJSValue getTilesetPalettes(QList> *palettes); Q_INVOKABLE QJSValue getPrimaryTilesetPalette(int paletteIndex); + Q_INVOKABLE QJSValue getPrimaryTilesetPalettes(); Q_INVOKABLE QJSValue getSecondaryTilesetPalette(int paletteIndex); + Q_INVOKABLE QJSValue getSecondaryTilesetPalettes(); + void refreshAfterPalettePreviewChange(); void setTilesetPalettePreview(Tileset *tileset, int paletteIndex, QList> colors); Q_INVOKABLE void setPrimaryTilesetPalettePreview(int paletteIndex, QList> colors); + Q_INVOKABLE void setPrimaryTilesetPalettesPreview(QList>> palettes); Q_INVOKABLE void setSecondaryTilesetPalettePreview(int paletteIndex, QList> colors); + Q_INVOKABLE void setSecondaryTilesetPalettesPreview(QList>> palettes); + Q_INVOKABLE QJSValue getPrimaryTilesetPalettePreview(int paletteIndex); + Q_INVOKABLE QJSValue getPrimaryTilesetPalettesPreview(); + Q_INVOKABLE QJSValue getSecondaryTilesetPalettePreview(int paletteIndex); + Q_INVOKABLE QJSValue getSecondaryTilesetPalettesPreview(); + Q_INVOKABLE QString getPrimaryTileset(); + Q_INVOKABLE QString getSecondaryTileset(); + Q_INVOKABLE void setPrimaryTileset(QString tileset); + Q_INVOKABLE void setSecondaryTileset(QString tileset); Q_INVOKABLE void registerAction(QString functionName, QString actionName); diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 3ba56dde..27e29645 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -2754,6 +2754,18 @@ void MainWindow::addImage(int x, int y, QString filepath) { this->ui->graphicsView_Map->overlay.addImage(x, y, filepath); } +void MainWindow::refreshAfterPaletteChange(Tileset *tileset) { + if (this->tilesetEditor) { + this->tilesetEditor->setTilesets(this->editor->map->layout->tileset_primary_label, this->editor->map->layout->tileset_secondary_label); + } + this->editor->metatile_selector_item->draw(); + this->editor->selected_border_metatiles_item->draw(); + this->editor->map_item->draw(true); + this->editor->updateMapBorder(); + this->editor->updateMapConnections(); + this->editor->project->saveTilesetPalettes(tileset); +} + void MainWindow::setTilesetPalette(Tileset *tileset, int paletteIndex, QList> colors) { if (!this->editor || !this->editor->map || !this->editor->map->layout) return; @@ -2768,51 +2780,93 @@ void MainWindow::setTilesetPalette(Tileset *tileset, int paletteIndex, QListpalettes)[paletteIndex][i] = qRgb(colors[i][0], colors[i][1], colors[i][2]); (*tileset->palettePreviews)[paletteIndex][i] = qRgb(colors[i][0], colors[i][1], colors[i][2]); } - - if (this->tilesetEditor) { - this->tilesetEditor->setTilesets(this->editor->map->layout->tileset_primary_label, this->editor->map->layout->tileset_secondary_label); - } - this->editor->metatile_selector_item->draw(); - this->editor->selected_border_metatiles_item->draw(); - this->editor->map_item->draw(true); - this->editor->updateMapBorder(); - this->editor->updateMapConnections(); - this->editor->project->saveTilesetPalettes(tileset); } void MainWindow::setPrimaryTilesetPalette(int paletteIndex, QList> colors) { if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_primary) return; this->setTilesetPalette(this->editor->map->layout->tileset_primary, paletteIndex, colors); + this->refreshAfterPaletteChange(this->editor->map->layout->tileset_primary); +} + +void MainWindow::setPrimaryTilesetPalettes(QList>> palettes) { + if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_primary) + return; + for (int i = 0; i < palettes.size(); i++) { + this->setTilesetPalette(this->editor->map->layout->tileset_primary, i, palettes[i]); + } + this->refreshAfterPaletteChange(this->editor->map->layout->tileset_primary); } void MainWindow::setSecondaryTilesetPalette(int paletteIndex, QList> colors) { if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_secondary) return; this->setTilesetPalette(this->editor->map->layout->tileset_secondary, paletteIndex, colors); + this->refreshAfterPaletteChange(this->editor->map->layout->tileset_secondary); } -QJSValue MainWindow::getTilesetPalette(Tileset *tileset, int paletteIndex) { - if (paletteIndex >= tileset->palettes->size()) +void MainWindow::setSecondaryTilesetPalettes(QList>> palettes) { + if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_secondary) + return; + for (int i = 0; i < palettes.size(); i++) { + this->setTilesetPalette(this->editor->map->layout->tileset_secondary, i, palettes[i]); + } + this->refreshAfterPaletteChange(this->editor->map->layout->tileset_secondary); +} + +QJSValue MainWindow::getTilesetPalette(QList> *palettes, int paletteIndex) { + if (paletteIndex >= palettes->size()) return QJSValue(); QList> palette; - for (auto color : tileset->palettes->value(paletteIndex)) { + for (auto color : palettes->value(paletteIndex)) { palette.append(QList({qRed(color), qGreen(color), qBlue(color)})); } return Scripting::getEngine()->toScriptValue(palette); } +QJSValue MainWindow::getTilesetPalettes(QList> *palettes) { + QList>> outPalettes; + for (int i = 0; i < palettes->size(); i++) { + QList> colors; + for (auto color : palettes->value(i)) { + colors.append(QList({qRed(color), qGreen(color), qBlue(color)})); + } + outPalettes.append(colors); + } + return Scripting::getEngine()->toScriptValue(outPalettes); +} + QJSValue MainWindow::getPrimaryTilesetPalette(int paletteIndex) { if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_primary) return QJSValue(); - return this->getTilesetPalette(this->editor->map->layout->tileset_primary, paletteIndex); + return this->getTilesetPalette(this->editor->map->layout->tileset_primary->palettes, paletteIndex); +} + +QJSValue MainWindow::getPrimaryTilesetPalettes() { + if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_primary) + return QJSValue(); + return this->getTilesetPalettes(this->editor->map->layout->tileset_primary->palettes); } QJSValue MainWindow::getSecondaryTilesetPalette(int paletteIndex) { if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_secondary) return QJSValue(); - return this->getTilesetPalette(this->editor->map->layout->tileset_secondary, paletteIndex); + return this->getTilesetPalette(this->editor->map->layout->tileset_secondary->palettes, paletteIndex); +} + +QJSValue MainWindow::getSecondaryTilesetPalettes() { + if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_secondary) + return QJSValue(); + return this->getTilesetPalettes(this->editor->map->layout->tileset_secondary->palettes); +} + +void MainWindow::refreshAfterPalettePreviewChange() { + this->editor->metatile_selector_item->draw(); + this->editor->selected_border_metatiles_item->draw(); + this->editor->map_item->draw(true); + this->editor->updateMapBorder(); + this->editor->updateMapConnections(); } void MainWindow::setTilesetPalettePreview(Tileset *tileset, int paletteIndex, QList> colors) { @@ -2829,24 +2883,82 @@ void MainWindow::setTilesetPalettePreview(Tileset *tileset, int paletteIndex, QL auto palettes = tileset->palettePreviews; (*palettes)[paletteIndex][i] = qRgb(colors[i][0], colors[i][1], colors[i][2]); } - - this->editor->metatile_selector_item->draw(); - this->editor->selected_border_metatiles_item->draw(); - this->editor->map_item->draw(true); - this->editor->updateMapBorder(); - this->editor->updateMapConnections(); } void MainWindow::setPrimaryTilesetPalettePreview(int paletteIndex, QList> colors) { if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_primary) return; this->setTilesetPalettePreview(this->editor->map->layout->tileset_primary, paletteIndex, colors); + this->refreshAfterPalettePreviewChange(); +} + +void MainWindow::setPrimaryTilesetPalettesPreview(QList>> palettes) { + if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_primary) + return; + for (int i = 0; i < palettes.size(); i++) { + this->setTilesetPalettePreview(this->editor->map->layout->tileset_primary, i, palettes[i]); + } + this->refreshAfterPalettePreviewChange(); } void MainWindow::setSecondaryTilesetPalettePreview(int paletteIndex, QList> colors) { if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_secondary) return; this->setTilesetPalettePreview(this->editor->map->layout->tileset_secondary, paletteIndex, colors); + this->refreshAfterPalettePreviewChange(); +} + +void MainWindow::setSecondaryTilesetPalettesPreview(QList>> palettes) { + if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_secondary) + return; + for (int i = 0; i < palettes.size(); i++) { + this->setTilesetPalettePreview(this->editor->map->layout->tileset_secondary, i, palettes[i]); + } + this->refreshAfterPalettePreviewChange(); +} + +QJSValue MainWindow::getPrimaryTilesetPalettePreview(int paletteIndex) { + if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_primary) + return QJSValue(); + return this->getTilesetPalette(this->editor->map->layout->tileset_primary->palettePreviews, paletteIndex); +} + +QJSValue MainWindow::getPrimaryTilesetPalettesPreview() { + if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_primary) + return QJSValue(); + return this->getTilesetPalettes(this->editor->map->layout->tileset_primary->palettePreviews); +} + +QJSValue MainWindow::getSecondaryTilesetPalettePreview(int paletteIndex) { + if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_secondary) + return QJSValue(); + return this->getTilesetPalette(this->editor->map->layout->tileset_secondary->palettePreviews, paletteIndex); +} + +QJSValue MainWindow::getSecondaryTilesetPalettesPreview() { + if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_secondary) + return QJSValue(); + return this->getTilesetPalettes(this->editor->map->layout->tileset_secondary->palettePreviews); +} + +QString MainWindow::getPrimaryTileset() { + if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_primary) + return QString(); + return this->editor->map->layout->tileset_primary->name; +} + +QString MainWindow::getSecondaryTileset() { + if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_secondary) + return QString(); + return this->editor->map->layout->tileset_secondary->name; +} + +void MainWindow::setPrimaryTileset(QString tileset) { + this->on_comboBox_PrimaryTileset_currentTextChanged(tileset); +} + +void MainWindow::setSecondaryTileset(QString tileset) { + this->on_comboBox_SecondaryTileset_currentTextChanged(tileset); } void MainWindow::registerAction(QString functionName, QString actionName) { @@ -2855,7 +2967,7 @@ void MainWindow::registerAction(QString functionName, QString actionName) { Scripting::registerAction(functionName, actionName); if (Scripting::numRegisteredActions() == 1) { - this->ui->menuTools->addSeparator(); + this->ui->menuTools->addSection("Custom Actions"); } this->ui->menuTools->addAction(actionName, [actionName](){ Scripting::invokeAction(actionName); diff --git a/test_script.js b/test_script.js index 85b3e72c..c807898a 100644 --- a/test_script.js +++ b/test_script.js @@ -13,14 +13,17 @@ function applyTint(palette, tint) { function applyTintToPalettes(tint) { try { - for (let i = 0; i < 13; i++) { - const primaryPalette = map.getPrimaryTilesetPalette(i) - applyTint(primaryPalette, tint) - map.setPrimaryTilesetPalettePreview(i, primaryPalette) - const secondaryPalette = map.getSecondaryTilesetPalette(i) - applyTint(secondaryPalette, tint) - map.setSecondaryTilesetPalettePreview(i, secondaryPalette) - } + // const primaryPalettes = map.getPrimaryTilesetPalettes() + // for (let i = 0; i < primaryPalettes.length; i++) + // applyTint(primaryPalettes[i], tint) + + // const secondaryPalettes = map.getSecondaryTilesetPalettes() + // for (let i = 0; i < secondaryPalettes.length; i++) + // applyTint(secondaryPalettes[i], tint) + + // map.setPrimaryTilesetPalettesPreview(primaryPalettes) + // map.setSecondaryTilesetPalettesPreview(secondaryPalettes) + map.setSecondaryTileset("gTileset_Rustboro") } catch(err) { console.log(err) }