porymap/src/scriptapi/apioverlay.cpp

192 lines
6.4 KiB
C++
Raw Normal View History

#include "mapview.h"
2022-09-05 01:24:11 +01:00
#include "scripting.h"
#include "imageproviders.h"
void MapView::clear(int layer) {
this->getOverlay(layer)->clearItems();
this->scene()->update();
2022-09-05 01:24:11 +01:00
}
// Overload. No layer provided, clear all layers
void MapView::clear() {
this->clearOverlayMap();
this->scene()->update();
2022-09-05 01:24:11 +01:00
}
void MapView::hide(int layer) {
this->setVisibility(false, layer);
2022-09-05 01:24:11 +01:00
}
// Overload. No layer provided, hide all layers
void MapView::hide() {
this->setVisibility(false);
2022-09-05 01:24:11 +01:00
}
void MapView::show(int layer) {
this->setVisibility(true, layer);
2022-09-05 01:24:11 +01:00
}
// Overload. No layer provided, show all layers
void MapView::show() {
this->setVisibility(true);
2022-09-05 01:24:11 +01:00
}
bool MapView::getVisibility(int layer) {
return !(this->getOverlay(layer)->getHidden());
2022-09-05 01:24:11 +01:00
}
void MapView::setVisibility(bool visible, int layer) {
this->getOverlay(layer)->setHidden(!visible);
this->scene()->update();
2022-09-05 01:24:11 +01:00
}
// Overload. No layer provided, set visibility of all layers
void MapView::setVisibility(bool visible) {
foreach (Overlay * layer, this->overlayMap)
layer->setHidden(!visible);
this->scene()->update();
2022-09-05 01:24:11 +01:00
}
int MapView::getX(int layer) {
return this->getOverlay(layer)->getX();
2022-09-05 01:24:11 +01:00
}
int MapView::getY(int layer) {
return this->getOverlay(layer)->getY();
2022-09-05 01:24:11 +01:00
}
void MapView::setX(int x, int layer) {
this->getOverlay(layer)->setX(x);
this->scene()->update();
2022-09-05 01:24:11 +01:00
}
// Overload. No layer provided, set x of all layers
void MapView::setX(int x) {
foreach (Overlay * layer, this->overlayMap)
layer->setX(x);
this->scene()->update();
2022-09-05 01:24:11 +01:00
}
void MapView::setY(int y, int layer) {
this->getOverlay(layer)->setY(y);
this->scene()->update();
2022-09-05 01:24:11 +01:00
}
// Overload. No layer provided, set y of all layers
void MapView::setY(int y) {
foreach (Overlay * layer, this->overlayMap)
layer->setY(y);
this->scene()->update();
2022-09-05 01:24:11 +01:00
}
QJSValue MapView::getPosition(int layer) {
Overlay * overlay = this->getOverlay(layer);
2022-09-05 01:24:11 +01:00
return Scripting::position(overlay->getX(), overlay->getY());
}
void MapView::setPosition(int x, int y, int layer) {
this->getOverlay(layer)->setPosition(x, y);
this->scene()->update();
2022-09-05 01:24:11 +01:00
}
// Overload. No layer provided, set position of all layers
void MapView::setPosition(int x, int y) {
foreach (Overlay * layer, this->overlayMap)
layer->setPosition(x, y);
this->scene()->update();
2022-09-05 01:24:11 +01:00
}
void MapView::move(int deltaX, int deltaY, int layer) {
this->getOverlay(layer)->move(deltaX, deltaY);
this->scene()->update();
2022-09-05 01:24:11 +01:00
}
// Overload. No layer provided, move all layers
void MapView::move(int deltaX, int deltaY) {
foreach (Overlay * layer, this->overlayMap)
layer->move(deltaX, deltaY);
this->scene()->update();
2022-09-05 01:24:11 +01:00
}
int MapView::getOpacity(int layer) {
return this->getOverlay(layer)->getOpacity();
2022-09-05 01:24:11 +01:00
}
void MapView::setOpacity(int opacity, int layer) {
this->getOverlay(layer)->setOpacity(opacity);
this->scene()->update();
2022-09-05 01:24:11 +01:00
}
// Overload. No layer provided, set opacity of all layers
void MapView::setOpacity(int opacity) {
foreach (Overlay * layer, this->overlayMap)
layer->setOpacity(opacity);
this->scene()->update();
2022-09-05 01:24:11 +01:00
}
void MapView::addText(QString text, int x, int y, QString color, int fontSize, int layer) {
this->getOverlay(layer)->addText(text, x, y, color, fontSize);
this->scene()->update();
2022-09-05 01:24:11 +01:00
}
void MapView::addRect(int x, int y, int width, int height, QString color, int layer) {
this->getOverlay(layer)->addRect(x, y, width, height, color, false);
this->scene()->update();
2022-09-05 01:24:11 +01:00
}
void MapView::addFilledRect(int x, int y, int width, int height, QString color, int layer) {
this->getOverlay(layer)->addRect(x, y, width, height, color, true);
this->scene()->update();
2022-09-05 01:24:11 +01:00
}
void MapView::addImage(int x, int y, QString filepath, int layer, bool useCache) {
if (this->getOverlay(layer)->addImage(x, y, filepath, useCache))
this->scene()->update();
2022-09-05 01:24:11 +01:00
}
void MapView::createImage(int x, int y, QString filepath, int width, int height, unsigned offset, qreal hScale, qreal vScale, int paletteId, bool setTransparency, int layer, bool useCache) {
if (!this->editor || !this->editor->map || !this->editor->map->layout
2022-09-05 01:24:11 +01:00
|| !this->editor->map->layout->tileset_primary || !this->editor->map->layout->tileset_secondary)
return;
QList<QRgb> palette;
if (paletteId != -1)
palette = Tileset::getPalette(paletteId, this->editor->map->layout->tileset_primary, this->editor->map->layout->tileset_secondary);
if (this->getOverlay(layer)->addImage(x, y, filepath, useCache, width, height, offset, hScale, vScale, palette, setTransparency))
this->scene()->update();
2022-09-05 01:24:11 +01:00
}
void MapView::addTileImage(int x, int y, int tileId, bool xflip, bool yflip, int paletteId, bool setTransparency, int layer) {
if (!this->editor || !this->editor->map || !this->editor->map->layout
2022-09-05 01:24:11 +01:00
|| !this->editor->map->layout->tileset_primary || !this->editor->map->layout->tileset_secondary)
return;
QImage image = getPalettedTileImage(tileId,
this->editor->map->layout->tileset_primary,
this->editor->map->layout->tileset_secondary,
paletteId)
.mirrored(xflip, yflip);
if (setTransparency)
image.setColor(0, qRgba(0, 0, 0, 0));
if (this->getOverlay(layer)->addImage(x, y, image))
this->scene()->update();
2022-09-05 01:24:11 +01:00
}
void MapView::addTileImage(int x, int y, QJSValue tileObj, bool setTransparency, int layer) {
2022-09-05 01:24:11 +01:00
Tile tile = Scripting::toTile(tileObj);
this->addTileImage(x, y, tile.tileId, tile.xflip, tile.yflip, tile.palette, setTransparency, layer);
}
void MapView::addMetatileImage(int x, int y, int metatileId, bool setTransparency, int layer) {
if (!this->editor || !this->editor->map || !this->editor->map->layout
2022-09-05 01:24:11 +01:00
|| !this->editor->map->layout->tileset_primary || !this->editor->map->layout->tileset_secondary)
return;
QImage image = getMetatileImage(static_cast<uint16_t>(metatileId),
this->editor->map->layout->tileset_primary,
this->editor->map->layout->tileset_secondary,
this->editor->map->metatileLayerOrder,
this->editor->map->metatileLayerOpacity);
if (setTransparency)
image.setColor(0, qRgba(0, 0, 0, 0));
if (this->getOverlay(layer)->addImage(x, y, image))
this->scene()->update();
2022-09-05 01:24:11 +01:00
}