qt/LedOK/player/elescroll.cpp

89 lines
3.2 KiB
C++
Raw 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>
2025-12-30 17:22:43 +08:00
#include <QDebug>
2023-04-18 14:14:46 +08:00
2025-12-30 17:22:43 +08:00
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();
2023-04-18 14:14:46 +08:00
}
2025-12-30 17:22:43 +08:00
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;
2024-02-21 18:08:50 +08:00
interval = step = 1;
2025-12-30 17:22:43 +08:00
if(speed > 60) step = (int) round(speed/60);
else if(speed < 60) interval = (int) round(60/speed);
2023-04-18 14:14:46 +08:00
}
2024-02-21 18:08:50 +08:00
void EleScroll::paintEvent(QPaintEvent *e) {
2025-12-30 17:22:43 +08:00
if(imgs.isEmpty()) 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);
2025-12-30 17:22:43 +08:00
if(direct=='l') {
int ii = 0, x = cur;
2024-02-21 18:08:50 +08:00
do {
2025-12-30 17:22:43 +08:00
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;
2024-02-21 18:08:50 +08:00
} while(x < rect.right()+1);
2025-12-30 17:22:43 +08:00
} else if(direct=='r') {
int ii = imgs.size()-1, x = cur + width();
2024-08-13 19:47:06 +08:00
bool con1;
2024-02-21 18:08:50 +08:00
do {
2025-12-30 17:22:43 +08:00
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;
2024-02-21 18:08:50 +08:00
} while(con1);
2025-12-30 17:22:43 +08:00
} else if(direct=='t') {
int ii = 0, y = cur;
2024-02-21 18:08:50 +08:00
do {
2025-12-30 17:22:43 +08:00
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;
2024-02-21 18:08:50 +08:00
} while(y < rect.bottom()+1);
2025-12-30 17:22:43 +08:00
} else if(direct=='b') {
int ii = imgs.size()-1, y = cur + height();
2024-08-13 19:47:06 +08:00
bool con1;
2024-02-21 18:08:50 +08:00
do {
2025-12-30 17:22:43 +08:00
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;
2024-02-21 18:08:50 +08:00
} while(con1);
2025-12-30 17:22:43 +08:00
} 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);
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;
2025-12-30 17:22:43 +08:00
if(direct=='t' || direct=='l') {
2023-10-23 14:58:29 +08:00
if(cur <= end) cur -= end;
else cur -= step;
2025-12-30 17:22:43 +08:00
} else if(direct=='b' || direct=='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();
}
}