qt/LedOK/player/elescroll.cpp

99 lines
3.6 KiB
C++
Raw Permalink Normal View History

2023-04-18 14:14:46 +08:00
#include "elescroll.h"
2024-02-21 18:08:50 +08:00
#include "playwin.h"
#include <QOpenGLWidget>
2023-04-18 14:14:46 +08:00
#include <QPainter>
2024-02-21 18:08:50 +08:00
#include <QPaintEvent>
2023-04-18 14:14:46 +08:00
2024-02-21 18:08:50 +08:00
EleScroll::EleScroll(QWidget *parent, QString dirPre, const JValue &json, int w, int h) : QWidget{parent}, w(w), h(h) {
img.load(dirPre + json["id"].toString());
auto effStr = json["effect"].toString();
2023-10-23 14:58:29 +08:00
if(effStr.isEmpty() || effStr=="no") return;
2024-02-21 18:08:50 +08:00
auto scrollSpeed = json["scrollSpeed"].toDouble();
if(scrollSpeed==0) {
auto scrollDur = json["effectSpeed"].toDouble();
if(scrollDur==0) return;
scrollSpeed = 1000 / scrollDur;
}
interval = step = 1;
if(scrollSpeed > 60) step = (int) round(scrollSpeed/60);
else if(scrollSpeed < 60) interval = (int) round(60/scrollSpeed);
2023-04-18 14:14:46 +08:00
int idx = effStr.lastIndexOf(' ');
if(idx > -1) {
effect = effStr.at(idx+1).toLatin1();
2023-10-23 14:58:29 +08:00
if(effect=='l') end = -(img.width()-step);
else if(effect=='r') end = img.width()-step;
else if(effect=='t') end = -(img.height()-step);
else if(effect=='b') end = img.height()-step;
2023-04-18 14:14:46 +08:00
}
}
2024-02-21 18:08:50 +08:00
EleScroll::EleScroll(QWidget *parent, QString imgPath, char effect, double scrollSpeed) : QWidget{parent}, effect(effect) {
2023-04-18 14:14:46 +08:00
img.load(imgPath);
if(effect==0) return;
2024-02-21 18:08:50 +08:00
if(scrollSpeed==0) return;
interval = step = 1;
if(scrollSpeed > 60) step = (int) round(scrollSpeed/60);
else if(scrollSpeed < 60) interval = (int) round(60/scrollSpeed);
2023-10-23 14:58:29 +08:00
if(effect=='l') end = -(img.width()-step);
else if(effect=='r') end = img.width()-step;
else if(effect=='t') end = -(img.height()-step);
else if(effect=='b') end = img.height()-step;
2023-04-18 14:14:46 +08:00
}
2024-02-21 18:08:50 +08:00
void EleScroll::paintEvent(QPaintEvent *e) {
2023-04-18 14:14:46 +08:00
if(img.isNull()) return;
2024-02-21 18:08:50 +08:00
auto rect = e->rect();
QPainter painter(this);
2023-04-18 14:14:46 +08:00
painter.setRenderHints(QPainter::Antialiasing|QPainter::SmoothPixmapTransform);
if(effect=='l') {
2024-02-21 18:08:50 +08:00
int x = cur;
do {
if(x > rect.left()-img.width() && x < rect.right()+1) painter.drawPixmap(x, 0, img);
x += img.width();
} while(x < rect.right()+1);
2023-04-18 14:14:46 +08:00
} else if(effect=='r') {
2024-02-21 18:08:50 +08:00
int x = cur + w;
2024-08-13 19:47:06 +08:00
bool con1;
2024-02-21 18:08:50 +08:00
do {
x -= img.width();
con1 = x > rect.left()-img.width();
if(con1 && x < rect.right()+1) painter.drawPixmap(x, 0, img);
} while(con1);
2023-04-18 14:14:46 +08:00
} else if(effect=='t') {
2024-02-21 18:08:50 +08:00
int y = cur;
do {
if(y > rect.top()-img.height() && y < rect.bottom()+1) painter.drawPixmap(0, y, img);
y += img.height();
} while(y < rect.bottom()+1);
2023-04-18 14:14:46 +08:00
} else if(effect=='b') {
2024-02-21 18:08:50 +08:00
int y = cur + h;
2024-08-13 19:47:06 +08:00
bool con1;
2024-02-21 18:08:50 +08:00
do {
y -= img.height();
con1 = y > rect.top()-img.height();
if(con1 && y < rect.bottom()+1) painter.drawPixmap(0, y, img);
} while(con1);
2023-04-18 14:14:46 +08:00
} else painter.drawPixmap(0, 0, img);
2024-02-21 18:08:50 +08:00
if(freshCnt==0 && effect!=0 && interval!=0) connect(PlayWin::self->gl, &QOpenGLWidget::frameSwapped, this, &EleScroll::doFrame, Qt::UniqueConnection);
2023-04-18 14:14:46 +08:00
}
2024-02-21 18:08:50 +08:00
void EleScroll::doFrame() {
if(! isVisible()) {
freshCnt = cur = 0;
disconnect(PlayWin::self->gl, &QOpenGLWidget::frameSwapped, this, &EleScroll::doFrame);
return;
}
if(freshCnt < interval) freshCnt++;
else {
freshCnt = 1;
2023-04-18 14:14:46 +08:00
if(effect=='t' || effect=='l') {
2023-10-23 14:58:29 +08:00
if(cur <= end) cur -= end;
else cur -= step;
2023-04-18 14:14:46 +08:00
} else if(effect=='b' || effect=='r') {
2023-10-23 14:58:29 +08:00
if(cur >= end) cur -= end;
else cur += step;
2023-04-18 14:14:46 +08:00
}
update();
}
}