#include "elescroll.h" #include "playwin.h" #include #include #include 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(); if(effStr.isEmpty() || effStr=="no") return; 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); int idx = effStr.lastIndexOf(' '); if(idx > -1) { effect = effStr.at(idx+1).toLatin1(); 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; } } EleScroll::EleScroll(QWidget *parent, QString imgPath, char effect, double scrollSpeed) : QWidget{parent}, effect(effect) { img.load(imgPath); if(effect==0) return; if(scrollSpeed==0) return; interval = step = 1; if(scrollSpeed > 60) step = (int) round(scrollSpeed/60); else if(scrollSpeed < 60) interval = (int) round(60/scrollSpeed); 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; } void EleScroll::paintEvent(QPaintEvent *e) { if(img.isNull()) return; auto rect = e->rect(); QPainter painter(this); painter.setRenderHints(QPainter::Antialiasing|QPainter::SmoothPixmapTransform); if(effect=='l') { 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); } else if(effect=='r') { int x = cur + w; bool con1; do { x -= img.width(); con1 = x > rect.left()-img.width(); if(con1 && x < rect.right()+1) painter.drawPixmap(x, 0, img); } while(con1); } else if(effect=='t') { 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); } else if(effect=='b') { int y = cur + h; bool con1; do { y -= img.height(); con1 = y > rect.top()-img.height(); if(con1 && y < rect.bottom()+1) painter.drawPixmap(0, y, img); } while(con1); } else painter.drawPixmap(0, 0, img); if(freshCnt==0 && effect!=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(effect=='t' || effect=='l') { if(cur <= end) cur -= end; else cur -= step; } else if(effect=='b' || effect=='r') { if(cur >= end) cur -= end; else cur += step; } update(); } }