2020-05-02 22:25:35 +01:00
|
|
|
#include "overlay.h"
|
2021-11-22 04:14:15 +00:00
|
|
|
#include "log.h"
|
2020-05-02 22:25:35 +01:00
|
|
|
|
|
|
|
void OverlayText::render(QPainter *painter) {
|
|
|
|
QFont font = painter->font();
|
|
|
|
font.setPixelSize(this->fontSize);
|
|
|
|
painter->setFont(font);
|
|
|
|
painter->setPen(this->color);
|
|
|
|
painter->drawText(this->x, this->y, this->text);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OverlayRect::render(QPainter *painter) {
|
|
|
|
if (this->filled) {
|
|
|
|
painter->fillRect(this->x, this->y, this->width, this->height, this->color);
|
|
|
|
} else {
|
|
|
|
painter->setPen(this->color);
|
|
|
|
painter->drawRect(this->x, this->y, this->width, this->height);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OverlayImage::render(QPainter *painter) {
|
|
|
|
painter->drawImage(this->x, this->y, this->image);
|
|
|
|
}
|
|
|
|
|
2021-11-30 17:58:39 +00:00
|
|
|
void Overlay::renderItems(QPainter *painter) {
|
2021-11-30 18:38:06 +00:00
|
|
|
if (this->hidden) return;
|
|
|
|
|
|
|
|
for (auto item : this->items)
|
2021-11-30 17:58:39 +00:00
|
|
|
item->render(painter);
|
|
|
|
}
|
|
|
|
|
2020-05-02 22:25:35 +01:00
|
|
|
void Overlay::clearItems() {
|
|
|
|
for (auto item : this->items) {
|
|
|
|
delete item;
|
|
|
|
}
|
|
|
|
this->items.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<OverlayItem*> Overlay::getItems() {
|
|
|
|
return this->items;
|
|
|
|
}
|
|
|
|
|
2021-11-30 18:38:06 +00:00
|
|
|
void Overlay::setHidden(bool hidden) {
|
|
|
|
this->hidden = hidden;
|
|
|
|
}
|
|
|
|
|
2020-05-02 22:25:35 +01:00
|
|
|
void Overlay::addText(QString text, int x, int y, QString color, int fontSize) {
|
|
|
|
this->items.append(new OverlayText(text, x, y, QColor(color), fontSize));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Overlay::addRect(int x, int y, int width, int height, QString color, bool filled) {
|
|
|
|
this->items.append(new OverlayRect(x, y, width, height, QColor(color), filled));
|
|
|
|
}
|
|
|
|
|
2021-11-28 04:50:44 +00:00
|
|
|
bool Overlay::addImage(int x, int y, QString filepath, int width, int height, unsigned offset, bool hflip, bool vflip, bool setTransparency) {
|
2021-11-22 04:14:15 +00:00
|
|
|
QImage image = QImage(filepath);
|
|
|
|
if (image.isNull()) {
|
|
|
|
logError(QString("Failed to load image '%1'").arg(filepath));
|
|
|
|
return false;
|
|
|
|
}
|
2021-11-28 04:50:44 +00:00
|
|
|
|
|
|
|
int fullWidth = image.width();
|
|
|
|
int fullHeight = image.height();
|
|
|
|
|
|
|
|
if (width <= 0)
|
|
|
|
width = fullWidth;
|
|
|
|
if (height <= 0)
|
|
|
|
height = fullHeight;
|
|
|
|
|
|
|
|
if ((unsigned)(width * height) + offset > (unsigned)(fullWidth * fullHeight)) {
|
|
|
|
logError(QString("%1x%2 image starting at offset %3 exceeds the image size for '%4'")
|
|
|
|
.arg(width)
|
|
|
|
.arg(height)
|
|
|
|
.arg(offset)
|
|
|
|
.arg(filepath));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get specified subset of image
|
|
|
|
if (width != fullWidth || height != fullHeight)
|
|
|
|
image = image.copy(offset % fullWidth, offset / fullWidth, width, height);
|
|
|
|
|
|
|
|
if (hflip || vflip)
|
|
|
|
image = image.transformed(QTransform().scale(hflip ? -1 : 1, vflip ? -1 : 1));
|
|
|
|
|
|
|
|
if (setTransparency)
|
|
|
|
image.setColor(0, qRgba(0, 0, 0, 0));
|
|
|
|
|
2021-11-22 04:14:15 +00:00
|
|
|
this->items.append(new OverlayImage(x, y, image));
|
|
|
|
return true;
|
2020-05-02 22:25:35 +01:00
|
|
|
}
|
2021-12-01 21:06:14 +00:00
|
|
|
|
|
|
|
bool Overlay::addImage(int x, int y, QImage image) {
|
|
|
|
if (image.isNull()) {
|
|
|
|
logError(QString("Failed to load custom image"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
this->items.append(new OverlayImage(x, y, image));
|
|
|
|
return true;
|
|
|
|
}
|