2022-01-20 10:08:17 +08:00
|
|
|
#include "elemultipng.h"
|
|
|
|
#include <QJsonObject>
|
|
|
|
#include <QJsonArray>
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QTimerEvent>
|
2022-08-25 18:37:24 +08:00
|
|
|
const QChar effTypes[] = {'l', 't', 'r', 'b'};
|
2022-01-20 10:08:17 +08:00
|
|
|
|
|
|
|
EleMultiPng::EleMultiPng(QString dirPre, QJsonArray maps, QWidget *parent) : QWidget{parent} {
|
|
|
|
QJsonObject map = maps[0].toObject();
|
|
|
|
EffDur = map["effectSpeed"].toInt()*1000;
|
|
|
|
if(EffDur==0) return;
|
2022-08-25 18:37:24 +08:00
|
|
|
picDur = map["picDuration"].toInt()*1000;
|
|
|
|
if(picDur==0) picDur = EffDur;
|
|
|
|
foreach(auto map, maps) imgs.append(QPixmap(dirPre+map["id"].toString()));
|
2022-01-20 10:08:17 +08:00
|
|
|
QString effStr = map["effect"].toString();
|
|
|
|
if(effStr.isEmpty() || effStr=="no") EffDur = 0;
|
|
|
|
else if(effStr.endsWith("left")) effType = 'l';
|
|
|
|
else if(effStr.endsWith("top")) effType = 't';
|
|
|
|
else if(effStr.endsWith("right")) effType = 'r';
|
|
|
|
else if(effStr.endsWith("bottom")) effType = 'b';
|
|
|
|
else if(effStr == "random") needRand = true;
|
|
|
|
else EffDur = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EleMultiPng::startMove() {
|
|
|
|
if(EffDur==0) return;
|
|
|
|
if(needRand) effType = effTypes[rand.generate() % 4];
|
|
|
|
double effDurD = EffDur;
|
|
|
|
if(effType=='l') {
|
|
|
|
imgx = width();
|
|
|
|
imgy = 0;
|
|
|
|
effDurD /= width();
|
|
|
|
} else if(effType=='r') {
|
|
|
|
imgx = -width();
|
|
|
|
imgy = 0;
|
|
|
|
effDurD /= width();
|
|
|
|
} else if(effType=='t') {
|
|
|
|
imgx = 0;
|
|
|
|
imgy = height();
|
|
|
|
effDurD /= height();
|
|
|
|
} else if(effType=='b') {
|
|
|
|
imgx = 0;
|
|
|
|
imgy = -height();
|
|
|
|
effDurD /= height();
|
|
|
|
} else return;
|
2022-08-25 18:37:24 +08:00
|
|
|
movePx = ceil(17/effDurD);
|
|
|
|
moveInter = effDurD*movePx;
|
2022-01-20 10:08:17 +08:00
|
|
|
moveTimerId = startTimer(moveInter, Qt::PreciseTimer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EleMultiPng::paintEvent(QPaintEvent *) {
|
|
|
|
if(timerId==0 && (EffDur!=0 || imgs.size()>1)) {
|
|
|
|
timerId = startTimer(picDur, Qt::PreciseTimer);
|
|
|
|
startMove();
|
|
|
|
}
|
|
|
|
QPainter painter(this);
|
|
|
|
painter.setRenderHints(QPainter::Antialiasing|QPainter::SmoothPixmapTransform);
|
|
|
|
painter.drawPixmap(imgx, imgy, imgs[imgc]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EleMultiPng::timerEvent(QTimerEvent *e) {
|
|
|
|
if(isVisible()) {
|
|
|
|
int id = e->timerId();
|
|
|
|
if(id==timerId){
|
|
|
|
if(imgc+2 > imgs.size()) imgc = 0;
|
|
|
|
else imgc++;
|
|
|
|
startMove();
|
|
|
|
} else if(id==moveTimerId){
|
|
|
|
if(effType=='l') {
|
|
|
|
imgx -= movePx;
|
|
|
|
if(imgx < 0) imgx = 0;
|
|
|
|
} else if(effType=='t') {
|
|
|
|
imgy -= movePx;
|
|
|
|
if(imgy < 0) imgy = 0;
|
|
|
|
} else if(effType=='r') {
|
|
|
|
imgx += movePx;
|
|
|
|
if(imgx > 0) imgx = 0;
|
|
|
|
} else if(effType=='b') {
|
|
|
|
imgy += movePx;
|
|
|
|
if(imgy > 0) imgy = 0;
|
|
|
|
}
|
|
|
|
if(imgx==0 && imgy==0) {
|
|
|
|
killTimer(moveTimerId);
|
|
|
|
moveTimerId = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
update();
|
|
|
|
} else {
|
|
|
|
if(timerId!=0) {
|
|
|
|
killTimer(timerId);
|
|
|
|
timerId = 0;
|
|
|
|
}
|
|
|
|
if(moveTimerId!=0) {
|
|
|
|
killTimer(moveTimerId);
|
|
|
|
moveTimerId = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|