#include "elemultipng.h" #include #include #include #include const QChar effTypes[] = {'l', 't', 'r', 'b'}; EleMultiPng::EleMultiPng(QString dirPre, QJsonArray maps, QWidget *parent) : QWidget{parent} { QJsonObject map = maps[0].toObject(); EffDur = map["effectSpeed"].toInt()*1000; if(EffDur==0) return; picDur = map["picDuration"].toInt()*1000; if(picDur==0) picDur = EffDur; foreach(auto map, maps) imgs.append(QPixmap(dirPre+map["id"].toString())); 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(); } else { if(timerId!=0) { killTimer(timerId); timerId = 0; } if(moveTimerId!=0) { killTimer(moveTimerId); moveTimerId = 0; } } }