Implement the rest of the palette api

This commit is contained in:
Marcus Huderle 2020-05-07 20:00:14 -05:00
parent 09a892f525
commit bf64764103
3 changed files with 163 additions and 31 deletions

View file

@ -63,15 +63,32 @@ public:
Q_INVOKABLE void addRect(int x, int y, int width, int height, QString color = "#000000"); 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 addFilledRect(int x, int y, int width, int height, QString color = "#000000");
Q_INVOKABLE void addImage(int x, int y, QString filepath); Q_INVOKABLE void addImage(int x, int y, QString filepath);
void refreshAfterPaletteChange(Tileset *tileset);
void setTilesetPalette(Tileset *tileset, int paletteIndex, QList<QList<int>> colors); void setTilesetPalette(Tileset *tileset, int paletteIndex, QList<QList<int>> colors);
Q_INVOKABLE void setPrimaryTilesetPalette(int paletteIndex, QList<QList<int>> colors); Q_INVOKABLE void setPrimaryTilesetPalette(int paletteIndex, QList<QList<int>> colors);
Q_INVOKABLE void setPrimaryTilesetPalettes(QList<QList<QList<int>>> palettes);
Q_INVOKABLE void setSecondaryTilesetPalette(int paletteIndex, QList<QList<int>> colors); Q_INVOKABLE void setSecondaryTilesetPalette(int paletteIndex, QList<QList<int>> colors);
QJSValue getTilesetPalette(Tileset *tileset, int paletteIndex); Q_INVOKABLE void setSecondaryTilesetPalettes(QList<QList<QList<int>>> palettes);
QJSValue getTilesetPalette(QList<QList<QRgb>> *palettes, int paletteIndex);
QJSValue getTilesetPalettes(QList<QList<QRgb>> *palettes);
Q_INVOKABLE QJSValue getPrimaryTilesetPalette(int paletteIndex); Q_INVOKABLE QJSValue getPrimaryTilesetPalette(int paletteIndex);
Q_INVOKABLE QJSValue getPrimaryTilesetPalettes();
Q_INVOKABLE QJSValue getSecondaryTilesetPalette(int paletteIndex); Q_INVOKABLE QJSValue getSecondaryTilesetPalette(int paletteIndex);
Q_INVOKABLE QJSValue getSecondaryTilesetPalettes();
void refreshAfterPalettePreviewChange();
void setTilesetPalettePreview(Tileset *tileset, int paletteIndex, QList<QList<int>> colors); void setTilesetPalettePreview(Tileset *tileset, int paletteIndex, QList<QList<int>> colors);
Q_INVOKABLE void setPrimaryTilesetPalettePreview(int paletteIndex, QList<QList<int>> colors); Q_INVOKABLE void setPrimaryTilesetPalettePreview(int paletteIndex, QList<QList<int>> colors);
Q_INVOKABLE void setPrimaryTilesetPalettesPreview(QList<QList<QList<int>>> palettes);
Q_INVOKABLE void setSecondaryTilesetPalettePreview(int paletteIndex, QList<QList<int>> colors); Q_INVOKABLE void setSecondaryTilesetPalettePreview(int paletteIndex, QList<QList<int>> colors);
Q_INVOKABLE void setSecondaryTilesetPalettesPreview(QList<QList<QList<int>>> 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); Q_INVOKABLE void registerAction(QString functionName, QString actionName);

View file

@ -2754,6 +2754,18 @@ void MainWindow::addImage(int x, int y, QString filepath) {
this->ui->graphicsView_Map->overlay.addImage(x, y, 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<QList<int>> colors) { void MainWindow::setTilesetPalette(Tileset *tileset, int paletteIndex, QList<QList<int>> colors) {
if (!this->editor || !this->editor->map || !this->editor->map->layout) if (!this->editor || !this->editor->map || !this->editor->map->layout)
return; return;
@ -2768,51 +2780,93 @@ void MainWindow::setTilesetPalette(Tileset *tileset, int paletteIndex, QList<QLi
(*tileset->palettes)[paletteIndex][i] = qRgb(colors[i][0], colors[i][1], colors[i][2]); (*tileset->palettes)[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]); (*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<QList<int>> colors) { void MainWindow::setPrimaryTilesetPalette(int paletteIndex, QList<QList<int>> colors) {
if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_primary) if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_primary)
return; return;
this->setTilesetPalette(this->editor->map->layout->tileset_primary, paletteIndex, colors); this->setTilesetPalette(this->editor->map->layout->tileset_primary, paletteIndex, colors);
this->refreshAfterPaletteChange(this->editor->map->layout->tileset_primary);
}
void MainWindow::setPrimaryTilesetPalettes(QList<QList<QList<int>>> 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<QList<int>> colors) { void MainWindow::setSecondaryTilesetPalette(int paletteIndex, QList<QList<int>> colors) {
if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_secondary) if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_secondary)
return; return;
this->setTilesetPalette(this->editor->map->layout->tileset_secondary, paletteIndex, colors); 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) { void MainWindow::setSecondaryTilesetPalettes(QList<QList<QList<int>>> palettes) {
if (paletteIndex >= tileset->palettes->size()) 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<QList<QRgb>> *palettes, int paletteIndex) {
if (paletteIndex >= palettes->size())
return QJSValue(); return QJSValue();
QList<QList<int>> palette; QList<QList<int>> palette;
for (auto color : tileset->palettes->value(paletteIndex)) { for (auto color : palettes->value(paletteIndex)) {
palette.append(QList<int>({qRed(color), qGreen(color), qBlue(color)})); palette.append(QList<int>({qRed(color), qGreen(color), qBlue(color)}));
} }
return Scripting::getEngine()->toScriptValue(palette); return Scripting::getEngine()->toScriptValue(palette);
} }
QJSValue MainWindow::getTilesetPalettes(QList<QList<QRgb>> *palettes) {
QList<QList<QList<int>>> outPalettes;
for (int i = 0; i < palettes->size(); i++) {
QList<QList<int>> colors;
for (auto color : palettes->value(i)) {
colors.append(QList<int>({qRed(color), qGreen(color), qBlue(color)}));
}
outPalettes.append(colors);
}
return Scripting::getEngine()->toScriptValue(outPalettes);
}
QJSValue MainWindow::getPrimaryTilesetPalette(int paletteIndex) { QJSValue MainWindow::getPrimaryTilesetPalette(int paletteIndex) {
if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_primary) if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_primary)
return QJSValue(); 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) { QJSValue MainWindow::getSecondaryTilesetPalette(int paletteIndex) {
if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_secondary) if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_secondary)
return QJSValue(); 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<QList<int>> colors) { void MainWindow::setTilesetPalettePreview(Tileset *tileset, int paletteIndex, QList<QList<int>> colors) {
@ -2829,24 +2883,82 @@ void MainWindow::setTilesetPalettePreview(Tileset *tileset, int paletteIndex, QL
auto palettes = tileset->palettePreviews; auto palettes = tileset->palettePreviews;
(*palettes)[paletteIndex][i] = qRgb(colors[i][0], colors[i][1], colors[i][2]); (*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<QList<int>> colors) { void MainWindow::setPrimaryTilesetPalettePreview(int paletteIndex, QList<QList<int>> colors) {
if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_primary) if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_primary)
return; return;
this->setTilesetPalettePreview(this->editor->map->layout->tileset_primary, paletteIndex, colors); this->setTilesetPalettePreview(this->editor->map->layout->tileset_primary, paletteIndex, colors);
this->refreshAfterPalettePreviewChange();
}
void MainWindow::setPrimaryTilesetPalettesPreview(QList<QList<QList<int>>> 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<QList<int>> colors) { void MainWindow::setSecondaryTilesetPalettePreview(int paletteIndex, QList<QList<int>> colors) {
if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_secondary) if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_secondary)
return; return;
this->setTilesetPalettePreview(this->editor->map->layout->tileset_secondary, paletteIndex, colors); this->setTilesetPalettePreview(this->editor->map->layout->tileset_secondary, paletteIndex, colors);
this->refreshAfterPalettePreviewChange();
}
void MainWindow::setSecondaryTilesetPalettesPreview(QList<QList<QList<int>>> 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) { void MainWindow::registerAction(QString functionName, QString actionName) {
@ -2855,7 +2967,7 @@ void MainWindow::registerAction(QString functionName, QString actionName) {
Scripting::registerAction(functionName, actionName); Scripting::registerAction(functionName, actionName);
if (Scripting::numRegisteredActions() == 1) { if (Scripting::numRegisteredActions() == 1) {
this->ui->menuTools->addSeparator(); this->ui->menuTools->addSection("Custom Actions");
} }
this->ui->menuTools->addAction(actionName, [actionName](){ this->ui->menuTools->addAction(actionName, [actionName](){
Scripting::invokeAction(actionName); Scripting::invokeAction(actionName);

View file

@ -13,14 +13,17 @@ function applyTint(palette, tint) {
function applyTintToPalettes(tint) { function applyTintToPalettes(tint) {
try { try {
for (let i = 0; i < 13; i++) { // const primaryPalettes = map.getPrimaryTilesetPalettes()
const primaryPalette = map.getPrimaryTilesetPalette(i) // for (let i = 0; i < primaryPalettes.length; i++)
applyTint(primaryPalette, tint) // applyTint(primaryPalettes[i], tint)
map.setPrimaryTilesetPalettePreview(i, primaryPalette)
const secondaryPalette = map.getSecondaryTilesetPalette(i) // const secondaryPalettes = map.getSecondaryTilesetPalettes()
applyTint(secondaryPalette, tint) // for (let i = 0; i < secondaryPalettes.length; i++)
map.setSecondaryTilesetPalettePreview(i, secondaryPalette) // applyTint(secondaryPalettes[i], tint)
}
// map.setPrimaryTilesetPalettesPreview(primaryPalettes)
// map.setSecondaryTilesetPalettesPreview(secondaryPalettes)
map.setSecondaryTileset("gTileset_Rustboro")
} catch(err) { } catch(err) {
console.log(err) console.log(err)
} }