qt/LedOK/player/elescroll.cpp
2025-12-30 17:22:43 +08:00

89 lines
3.2 KiB
C++

#include "elescroll.h"
#include "playwin.h"
#include <QOpenGLWidget>
#include <QPainter>
#include <QPaintEvent>
#include <QDebug>
EleScroll::EleScroll(QWidget *parent, QList<QPixmap> imgs, char direct, double speed) : QWidget{parent}, imgs(imgs), direct(direct) {
auto img0 = imgs[0];
qDebug().nospace()<<" SrcScroll img cnt "<<imgs.size()<<", "<<img0.width()<<"x"<<img0.height()<<" direct "<<direct<<" speed "<<speed;
if(speed==0) return;
int width = 0, height = 0;
for(auto &img : imgs) {
width += img.width();
height += img.height();
}
if(direct=='l') end = -(width-step);
else if(direct=='r') end = width-step;
else if(direct=='t') end = -(height-step);
else if(direct=='b') end = height-step;
else direct = 0;
if(direct==0) return;
interval = step = 1;
if(speed > 60) step = (int) round(speed/60);
else if(speed < 60) interval = (int) round(60/speed);
}
void EleScroll::paintEvent(QPaintEvent *e) {
if(imgs.isEmpty()) return;
auto rect = e->rect();
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing|QPainter::SmoothPixmapTransform);
if(direct=='l') {
int ii = 0, x = cur;
do {
if(x > rect.left()-imgs[ii].width() && x < rect.right()+1) painter.drawPixmap(x, 0, imgs[ii]);
x += imgs[ii].width();
if(++ii >= imgs.size()) ii = 0;
} while(x < rect.right()+1);
} else if(direct=='r') {
int ii = imgs.size()-1, x = cur + width();
bool con1;
do {
x -= imgs[ii].width();
con1 = x > rect.left()-imgs[ii].width();
if(con1 && x < rect.right()+1) painter.drawPixmap(x, 0, imgs[ii]);
if(--ii < 0) ii = imgs.size()-1;
} while(con1);
} else if(direct=='t') {
int ii = 0, y = cur;
do {
if(y > rect.top()-imgs[ii].height() && y < rect.bottom()+1) painter.drawPixmap(0, y, imgs[ii]);
y += imgs[ii].height();
if(++ii >= imgs.size()) ii = 0;
} while(y < rect.bottom()+1);
} else if(direct=='b') {
int ii = imgs.size()-1, y = cur + height();
bool con1;
do {
y -= imgs[ii].height();
con1 = y > rect.top()-imgs[ii].height();
if(con1 && y < rect.bottom()+1) painter.drawPixmap(0, y, imgs[ii]);
if(--ii < 0) ii = imgs.size()-1;
} while(con1);
} else painter.drawPixmap(0, 0, imgs[0]);
if(freshCnt==0 && direct!=0 && interval!=0) connect(PlayWin::self->gl, &QOpenGLWidget::frameSwapped, this, &EleScroll::doFrame, Qt::UniqueConnection);
}
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;
if(direct=='t' || direct=='l') {
if(cur <= end) cur -= end;
else cur -= step;
} else if(direct=='b' || direct=='r') {
if(cur >= end) cur -= end;
else cur += step;
}
update();
}
}