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 addEvent(Event*);
|
||||
QPixmap renderConnection(MapConnection, MapLayout *);
|
||||
QPixmap renderBorder();
|
||||
QPixmap renderBorder(bool ignoreCache = false);
|
||||
void setDimensions(int newWidth, int newHeight, bool setNewBlockdata = true);
|
||||
void setBorderDimensions(int newWidth, int newHeight, bool setNewBlockdata = true);
|
||||
void cacheBorder();
|
||||
|
|
|
@ -66,6 +66,9 @@ public:
|
|||
void displayMapGrid();
|
||||
void displayWildMonTables();
|
||||
|
||||
void updateMapBorder();
|
||||
void updateMapConnections();
|
||||
|
||||
void setEditingMap();
|
||||
void setEditingCollision();
|
||||
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 addFilledRect(int x, int y, int width, int height, QString color = "#000000");
|
||||
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:
|
||||
|
|
|
@ -142,7 +142,7 @@ public:
|
|||
void saveTilesetMetatileAttributes(Tileset*);
|
||||
void saveTilesetMetatiles(Tileset*);
|
||||
void saveTilesetTilesImage(Tileset*);
|
||||
void saveTilesetPalettes(Tileset*, bool);
|
||||
void saveTilesetPalettes(Tileset*);
|
||||
|
||||
QString defaultSong;
|
||||
QStringList getSongNames();
|
||||
|
|
|
@ -18,6 +18,7 @@ public:
|
|||
Scripting(MainWindow *mainWindow);
|
||||
static QJSValue fromBlock(Block block);
|
||||
static QJSValue dimensions(int width, int height);
|
||||
static QJSEngine *getEngine();
|
||||
static void init(MainWindow *mainWindow);
|
||||
static void cb_MetatileChanged(int x, int y, Block prevBlock, Block newBlock);
|
||||
static void cb_MapOpened(QString mapName);
|
||||
|
|
|
@ -221,7 +221,7 @@ QPixmap Map::render(bool ignoreCache = false, MapLayout * fromLayout) {
|
|||
return pixmap;
|
||||
}
|
||||
|
||||
QPixmap Map::renderBorder() {
|
||||
QPixmap Map::renderBorder(bool ignoreCache) {
|
||||
bool changed_any = false, border_resized = false;
|
||||
int width_ = getBorderWidth();
|
||||
int height_ = getBorderHeight();
|
||||
|
@ -239,7 +239,7 @@ QPixmap Map::renderBorder() {
|
|||
}
|
||||
QPainter painter(&layout->border_image);
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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) {
|
||||
// Draw sufficient border blocks to fill the player's view (BORDER_DISTANCE)
|
||||
if (dimension >= BORDER_DISTANCE) {
|
||||
|
|
|
@ -1073,7 +1073,7 @@ void MainWindow::on_actionNew_Tileset_triggered() {
|
|||
editor->project->saveTilesetTilesImage(newSet);
|
||||
editor->project->saveTilesetMetatiles(newSet);
|
||||
editor->project->saveTilesetMetatileAttributes(newSet);
|
||||
editor->project->saveTilesetPalettes(newSet, !createTilesetDialog->isSecondary);
|
||||
editor->project->saveTilesetPalettes(newSet);
|
||||
|
||||
//append to tileset specific files
|
||||
|
||||
|
@ -2746,3 +2746,64 @@ void MainWindow::addImage(int x, int y, QString filepath) {
|
|||
return;
|
||||
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);
|
||||
saveTilesetTilesImage(primaryTileset);
|
||||
saveTilesetTilesImage(secondaryTileset);
|
||||
saveTilesetPalettes(primaryTileset, true);
|
||||
saveTilesetPalettes(secondaryTileset, false);
|
||||
saveTilesetPalettes(primaryTileset);
|
||||
saveTilesetPalettes(secondaryTileset);
|
||||
}
|
||||
|
||||
void Project::saveTilesetMetatileLabels(Tileset *primaryTileset, Tileset *secondaryTileset) {
|
||||
|
@ -1150,7 +1150,7 @@ void Project::saveTilesetTilesImage(Tileset *tileset) {
|
|||
exportIndexed4BPPPng(tileset->tilesImage, tileset->tilesImagePath);
|
||||
}
|
||||
|
||||
void Project::saveTilesetPalettes(Tileset *tileset, bool /*primary*/) {
|
||||
void Project::saveTilesetPalettes(Tileset *tileset) {
|
||||
PaletteUtil paletteParser;
|
||||
for (int i = 0; i < Project::getNumPalettesTotal(); i++) {
|
||||
QString filepath = tileset->palettePaths.at(i);
|
||||
|
|
|
@ -90,3 +90,7 @@ QJSValue Scripting::dimensions(int width, int height) {
|
|||
obj.setProperty("height", height);
|
||||
return obj;
|
||||
}
|
||||
|
||||
QJSEngine *Scripting::getEngine() {
|
||||
return instance->engine;
|
||||
}
|
||||
|
|
|
@ -1,17 +1,26 @@
|
|||
// Porymap callback when a block is painted.
|
||||
export function on_block_changed(x, y, prevBlock, newBlock) {
|
||||
map.clearOverlay()
|
||||
map.addFilledRect(0, 0, map.getWidth() * 16 - 1, map.getHeight() * 16 - 1, "#80FF0040")
|
||||
map.addRect(10, 10, 100, 30, "#FF00FF")
|
||||
map.addImage(80, 80, "D:\\cygwin64\\home\\huder\\scratch\\github-avatar.png")
|
||||
map.addText(`coords ${x}, ${y}`, 20, 20, "#00FF00", 24)
|
||||
map.addText(`block ${prevBlock.metatileId}`, 20, 60, "#00FFFF", 18)
|
||||
console.log("ran", x, y)
|
||||
const nightTint = [0.6, 0.55, 1.0];
|
||||
|
||||
function applyTint(palette, tint) {
|
||||
for (let i = 0; i < palette.length; i++) {
|
||||
const color = palette[i];
|
||||
for (let j = 0; j < tint.length; j++) {
|
||||
color[j] = Math.floor(color[j] * tint[j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Porymap callback when a map is opened.
|
||||
export function on_map_opened(mapName) {
|
||||
map.clearOverlay()
|
||||
map.addFilledRect(0, 0, map.getWidth() * 16 - 1, map.getHeight() * 16 - 1, "#4000FF00")
|
||||
console.log(`opened ${mapName}`)
|
||||
try {
|
||||
for (let i = 0; i < 13; i++) {
|
||||
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