Add true tileset scripting functions
This commit is contained in:
parent
567a45b7e4
commit
8697adf186
11 changed files with 127 additions and 20 deletions
|
@ -85,7 +85,7 @@ public:
|
||||||
void removeEvent(Event*);
|
void removeEvent(Event*);
|
||||||
void addEvent(Event*);
|
void addEvent(Event*);
|
||||||
QPixmap renderConnection(MapConnection, MapLayout *);
|
QPixmap renderConnection(MapConnection, MapLayout *);
|
||||||
QPixmap renderBorder();
|
QPixmap renderBorder(bool ignoreCache = false);
|
||||||
void setDimensions(int newWidth, int newHeight, bool setNewBlockdata = true);
|
void setDimensions(int newWidth, int newHeight, bool setNewBlockdata = true);
|
||||||
void setBorderDimensions(int newWidth, int newHeight, bool setNewBlockdata = true);
|
void setBorderDimensions(int newWidth, int newHeight, bool setNewBlockdata = true);
|
||||||
void cacheBorder();
|
void cacheBorder();
|
||||||
|
|
|
@ -66,6 +66,9 @@ public:
|
||||||
void displayMapGrid();
|
void displayMapGrid();
|
||||||
void displayWildMonTables();
|
void displayWildMonTables();
|
||||||
|
|
||||||
|
void updateMapBorder();
|
||||||
|
void updateMapConnections();
|
||||||
|
|
||||||
void setEditingMap();
|
void setEditingMap();
|
||||||
void setEditingCollision();
|
void setEditingCollision();
|
||||||
void setEditingObjects();
|
void setEditingObjects();
|
||||||
|
|
|
@ -63,6 +63,12 @@ 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 setTilesetPalette(Tileset *tileset, int paletteIndex, QList<QList<int>> colors);
|
||||||
|
Q_INVOKABLE void setPrimaryTilesetPalette(int paletteIndex, QList<QList<int>> colors);
|
||||||
|
Q_INVOKABLE void setSecondaryTilesetPalette(int paletteIndex, QList<QList<int>> colors);
|
||||||
|
QJSValue getTilesetPalette(Tileset *tileset, int paletteIndex);
|
||||||
|
Q_INVOKABLE QJSValue getPrimaryTilesetPalette(int paletteIndex);
|
||||||
|
Q_INVOKABLE QJSValue getSecondaryTilesetPalette(int paletteIndex);
|
||||||
|
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
|
|
|
@ -142,7 +142,7 @@ public:
|
||||||
void saveTilesetMetatileAttributes(Tileset*);
|
void saveTilesetMetatileAttributes(Tileset*);
|
||||||
void saveTilesetMetatiles(Tileset*);
|
void saveTilesetMetatiles(Tileset*);
|
||||||
void saveTilesetTilesImage(Tileset*);
|
void saveTilesetTilesImage(Tileset*);
|
||||||
void saveTilesetPalettes(Tileset*, bool);
|
void saveTilesetPalettes(Tileset*);
|
||||||
|
|
||||||
QString defaultSong;
|
QString defaultSong;
|
||||||
QStringList getSongNames();
|
QStringList getSongNames();
|
||||||
|
|
|
@ -18,6 +18,7 @@ public:
|
||||||
Scripting(MainWindow *mainWindow);
|
Scripting(MainWindow *mainWindow);
|
||||||
static QJSValue fromBlock(Block block);
|
static QJSValue fromBlock(Block block);
|
||||||
static QJSValue dimensions(int width, int height);
|
static QJSValue dimensions(int width, int height);
|
||||||
|
static QJSEngine *getEngine();
|
||||||
static void init(MainWindow *mainWindow);
|
static void init(MainWindow *mainWindow);
|
||||||
static void cb_MetatileChanged(int x, int y, Block prevBlock, Block newBlock);
|
static void cb_MetatileChanged(int x, int y, Block prevBlock, Block newBlock);
|
||||||
static void cb_MapOpened(QString mapName);
|
static void cb_MapOpened(QString mapName);
|
||||||
|
|
|
@ -221,7 +221,7 @@ QPixmap Map::render(bool ignoreCache = false, MapLayout * fromLayout) {
|
||||||
return pixmap;
|
return pixmap;
|
||||||
}
|
}
|
||||||
|
|
||||||
QPixmap Map::renderBorder() {
|
QPixmap Map::renderBorder(bool ignoreCache) {
|
||||||
bool changed_any = false, border_resized = false;
|
bool changed_any = false, border_resized = false;
|
||||||
int width_ = getBorderWidth();
|
int width_ = getBorderWidth();
|
||||||
int height_ = getBorderHeight();
|
int height_ = getBorderHeight();
|
||||||
|
@ -239,7 +239,7 @@ QPixmap Map::renderBorder() {
|
||||||
}
|
}
|
||||||
QPainter painter(&layout->border_image);
|
QPainter painter(&layout->border_image);
|
||||||
for (int i = 0; i < layout->border->blocks->length(); i++) {
|
for (int i = 0; i < layout->border->blocks->length(); i++) {
|
||||||
if (!border_resized && !borderBlockChanged(i, layout->cached_border)) {
|
if (!ignoreCache && (!border_resized && !borderBlockChanged(i, layout->cached_border))) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1440,6 +1440,29 @@ void Editor::displayMapBorder() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Editor::updateMapBorder() {
|
||||||
|
QPixmap pixmap = this->map->renderBorder(true);
|
||||||
|
for (auto item : this->borderItems) {
|
||||||
|
item->setPixmap(pixmap);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Editor::updateMapConnections() {
|
||||||
|
if (connection_items.size() != connection_edit_items.size())
|
||||||
|
return;
|
||||||
|
|
||||||
|
for (int i = 0; i < connection_items.size(); i++) {
|
||||||
|
Map *connected_map = project->getMap(connection_edit_items[i]->connection->map_name);
|
||||||
|
if (!connected_map)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
QPixmap pixmap = connected_map->renderConnection(*(connection_edit_items[i]->connection), map->layout);
|
||||||
|
connection_items[i]->setPixmap(pixmap);
|
||||||
|
connection_edit_items[i]->basePixmap = pixmap;
|
||||||
|
connection_edit_items[i]->setPixmap(pixmap);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int Editor::getBorderDrawDistance(int dimension) {
|
int Editor::getBorderDrawDistance(int dimension) {
|
||||||
// Draw sufficient border blocks to fill the player's view (BORDER_DISTANCE)
|
// Draw sufficient border blocks to fill the player's view (BORDER_DISTANCE)
|
||||||
if (dimension >= BORDER_DISTANCE) {
|
if (dimension >= BORDER_DISTANCE) {
|
||||||
|
|
|
@ -1073,7 +1073,7 @@ void MainWindow::on_actionNew_Tileset_triggered() {
|
||||||
editor->project->saveTilesetTilesImage(newSet);
|
editor->project->saveTilesetTilesImage(newSet);
|
||||||
editor->project->saveTilesetMetatiles(newSet);
|
editor->project->saveTilesetMetatiles(newSet);
|
||||||
editor->project->saveTilesetMetatileAttributes(newSet);
|
editor->project->saveTilesetMetatileAttributes(newSet);
|
||||||
editor->project->saveTilesetPalettes(newSet, !createTilesetDialog->isSecondary);
|
editor->project->saveTilesetPalettes(newSet);
|
||||||
|
|
||||||
//append to tileset specific files
|
//append to tileset specific files
|
||||||
|
|
||||||
|
@ -2746,3 +2746,64 @@ void MainWindow::addImage(int x, int y, QString filepath) {
|
||||||
return;
|
return;
|
||||||
this->ui->graphicsView_Map->overlay.addImage(x, y, filepath);
|
this->ui->graphicsView_Map->overlay.addImage(x, y, filepath);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void MainWindow::setTilesetPalette(Tileset *tileset, int paletteIndex, QList<QList<int>> colors) {
|
||||||
|
if (!this->editor || !this->editor->map || !this->editor->map->layout)
|
||||||
|
return;
|
||||||
|
if (paletteIndex >= tileset->palettes->size())
|
||||||
|
return;
|
||||||
|
if (colors.size() != 16)
|
||||||
|
return;
|
||||||
|
|
||||||
|
for (int i = 0; i < 16; i++) {
|
||||||
|
if (colors[i].size() != 3)
|
||||||
|
continue;
|
||||||
|
auto palettes = tileset->palettes;
|
||||||
|
(*palettes)[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) {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
return;
|
||||||
|
this->setTilesetPalette(this->editor->map->layout->tileset_secondary, paletteIndex, colors);
|
||||||
|
}
|
||||||
|
|
||||||
|
QJSValue MainWindow::getTilesetPalette(Tileset *tileset, int paletteIndex) {
|
||||||
|
if (paletteIndex >= tileset->palettes->size())
|
||||||
|
return QJSValue();
|
||||||
|
|
||||||
|
QList<QList<int>> palette;
|
||||||
|
for (auto color : tileset->palettes->value(paletteIndex)) {
|
||||||
|
palette.append(QList<int>({qRed(color), qGreen(color), qBlue(color)}));
|
||||||
|
}
|
||||||
|
return Scripting::getEngine()->toScriptValue(palette);
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
|
@ -1012,8 +1012,8 @@ void Project::saveTilesets(Tileset *primaryTileset, Tileset *secondaryTileset) {
|
||||||
saveTilesetMetatiles(secondaryTileset);
|
saveTilesetMetatiles(secondaryTileset);
|
||||||
saveTilesetTilesImage(primaryTileset);
|
saveTilesetTilesImage(primaryTileset);
|
||||||
saveTilesetTilesImage(secondaryTileset);
|
saveTilesetTilesImage(secondaryTileset);
|
||||||
saveTilesetPalettes(primaryTileset, true);
|
saveTilesetPalettes(primaryTileset);
|
||||||
saveTilesetPalettes(secondaryTileset, false);
|
saveTilesetPalettes(secondaryTileset);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Project::saveTilesetMetatileLabels(Tileset *primaryTileset, Tileset *secondaryTileset) {
|
void Project::saveTilesetMetatileLabels(Tileset *primaryTileset, Tileset *secondaryTileset) {
|
||||||
|
@ -1150,7 +1150,7 @@ void Project::saveTilesetTilesImage(Tileset *tileset) {
|
||||||
exportIndexed4BPPPng(tileset->tilesImage, tileset->tilesImagePath);
|
exportIndexed4BPPPng(tileset->tilesImage, tileset->tilesImagePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Project::saveTilesetPalettes(Tileset *tileset, bool /*primary*/) {
|
void Project::saveTilesetPalettes(Tileset *tileset) {
|
||||||
PaletteUtil paletteParser;
|
PaletteUtil paletteParser;
|
||||||
for (int i = 0; i < Project::getNumPalettesTotal(); i++) {
|
for (int i = 0; i < Project::getNumPalettesTotal(); i++) {
|
||||||
QString filepath = tileset->palettePaths.at(i);
|
QString filepath = tileset->palettePaths.at(i);
|
||||||
|
|
|
@ -90,3 +90,7 @@ QJSValue Scripting::dimensions(int width, int height) {
|
||||||
obj.setProperty("height", height);
|
obj.setProperty("height", height);
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QJSEngine *Scripting::getEngine() {
|
||||||
|
return instance->engine;
|
||||||
|
}
|
||||||
|
|
|
@ -1,17 +1,26 @@
|
||||||
// Porymap callback when a block is painted.
|
const nightTint = [0.6, 0.55, 1.0];
|
||||||
export function on_block_changed(x, y, prevBlock, newBlock) {
|
|
||||||
map.clearOverlay()
|
function applyTint(palette, tint) {
|
||||||
map.addFilledRect(0, 0, map.getWidth() * 16 - 1, map.getHeight() * 16 - 1, "#80FF0040")
|
for (let i = 0; i < palette.length; i++) {
|
||||||
map.addRect(10, 10, 100, 30, "#FF00FF")
|
const color = palette[i];
|
||||||
map.addImage(80, 80, "D:\\cygwin64\\home\\huder\\scratch\\github-avatar.png")
|
for (let j = 0; j < tint.length; j++) {
|
||||||
map.addText(`coords ${x}, ${y}`, 20, 20, "#00FF00", 24)
|
color[j] = Math.floor(color[j] * tint[j]);
|
||||||
map.addText(`block ${prevBlock.metatileId}`, 20, 60, "#00FFFF", 18)
|
}
|
||||||
console.log("ran", x, y)
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Porymap callback when a map is opened.
|
// Porymap callback when a map is opened.
|
||||||
export function on_map_opened(mapName) {
|
export function on_map_opened(mapName) {
|
||||||
map.clearOverlay()
|
try {
|
||||||
map.addFilledRect(0, 0, map.getWidth() * 16 - 1, map.getHeight() * 16 - 1, "#4000FF00")
|
for (let i = 0; i < 13; i++) {
|
||||||
console.log(`opened ${mapName}`)
|
const primaryPalette = map.getPrimaryTilesetPalette(i)
|
||||||
|
applyTint(primaryPalette, nightTint)
|
||||||
|
map.setPrimaryTilesetPalette(i, primaryPalette)
|
||||||
|
const secondaryPalette = map.getSecondaryTilesetPalette(i)
|
||||||
|
applyTint(secondaryPalette, nightTint)
|
||||||
|
map.setSecondaryTilesetPalette(i, secondaryPalette)
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
console.log(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue