qt/LedOK/player/elemultipng.cpp

105 lines
3.3 KiB
C++
Raw Normal View History

2023-04-18 14:14:46 +08:00
#include "elemultipng.h"
#include <QJsonObject>
#include <QJsonArray>
#include <QPainter>
#include <QTimerEvent>
const QChar effTypes[] = {'l', 't', 'r', 'b'};
EleMultiPng::EleMultiPng(QString dirPre, QJsonArray maps, QWidget *parent) : QWidget{parent} {
QJsonObject map = maps[0].toObject();
picDur = map["picDuration"].toInt()*1000;
if(picDur==0) return;
EffDur = map["effectSpeed"].toInt()*1000;
2023-04-19 14:42:06 +08:00
foreach(QJsonValue map, maps) imgs.append(QPixmap(dirPre+map["id"].toString()));
2023-04-18 14:14:46 +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;
movePx = ceil(17/effDurD);
moveInter = effDurD*movePx;
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();
foreach(auto split, splits) split->update();
} else {
if(timerId!=0) {
killTimer(timerId);
timerId = 0;
}
if(moveTimerId!=0) {
killTimer(moveTimerId);
moveTimerId = 0;
}
}
}
void EleSplitPng::paintEvent(QPaintEvent *) {
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing|QPainter::SmoothPixmapTransform);
painter.drawPixmap(multiPng->imgx, multiPng->imgy, multiPng->imgs[multiPng->imgc]);
}