幾年前就一直考慮過寫這個控件了,在9年前用C#的時候,就看到過別人用C#寫了個水波效果的控件,挺好玩的,當時看了下代碼用的二維數組來存儲變換的圖像像素數據,自從學了Qt之後,有過幾回想要用Qt寫一個版本,當時功力尚淺,嘗試過了沒寫成功,我記得還有個用匯編寫的dll提供調用,那個效率賊高,用CPU繪製的話效率相對來講低不少。前陣子一個好友-離心泵(QQ:33522)恰巧寫了個,我在他的基礎上改進了一些功能,增長了一些接口設置,好比提供參數能夠控制水波的消失速度,擴散的速度,水波的面積大小以及水波的深度等。linux
#ifndef WAVEWATER_H #define WAVEWATER_H /** * 水波效果控件 做者:離心泵(QQ:33522) 整理:feiyangqingyun(QQ:517216493) 2019-10-03 * 1:可設置顯示的圖像 * 2:可設置衰減係數,控制消失速度,值越小水波消失越快 * 3:可設置折射係數,控制擴散速度,值越大水波擴散越快 * 4:可設置石頭大小,控制水波面積,值越大水波面積越大 * 5:可設置石頭重量,控制水波深度,值越大水波深度越浪 * 6:目前採用的是cpu運算和繪製,圖片越小越流暢 */ #include <QWidget> #include <QMap> #ifdef quc #if (QT_VERSION < QT_VERSION_CHECK(5,7,0)) #include <QtDesigner/QDesignerExportWidget> #else #include <QtUiPlugin/QDesignerExportWidget> #endif class QDESIGNER_WIDGET_EXPORT WaveWater : public QWidget #else class WaveWater : public QWidget #endif { Q_OBJECT Q_PROPERTY(int damping READ getDamping WRITE setDamping) Q_PROPERTY(int refractive READ getRefractive WRITE setRefractive) Q_PROPERTY(int stoneSize READ getStoneSize WRITE setStoneSize) Q_PROPERTY(int stoneWeight READ getStoneWeight WRITE setStoneWeight) Q_PROPERTY(QPixmap image READ getImage WRITE setImage) public: explicit WaveWater(QWidget *parent = 0); ~WaveWater(); protected: void mousePressEvent(QMouseEvent *); void mouseMoveEvent(QMouseEvent *); void mouseReleaseEvent(QMouseEvent *); void paintEvent(QPaintEvent *); private: int damping; //波能衰減係數 int refractive; //波能折射係數 int stoneSize; //石頭大小 int stoneWeight; //石頭重量 QPixmap image; //渲染圖片 QImage imageRender; //渲染像素圖片 QImage imageOrigin; //渲染源 QPixmap pixmap; //渲染圖片 int imageWidth; //圖片寬度 int imageHeight; //圖片高度 int **waveBuffer1; //波能緩衝區1 int **waveBuffer2; //波能緩衝區2 bool isStoped; //水波是否結束波動 bool isPressed; //鼠標是否按下 QPoint lastPoint; //鼠標按下出的座標 QTimer *timer; //定時器繪製 private slots: //執行計算位圖進行繪製 void drawImage(); //水波步進擴散 void waveSpread(); //渲染水波位圖 void waveRender(); //扔石子 void dropStone(int x, int y, int stoneSize, int stoneWeight); public: int getDamping() const; int getRefractive() const; int getStoneSize() const; int getStoneWeight() const; QPixmap getImage() const; QSize sizeHint() const; QSize minimumSizeHint() const; public Q_SLOTS: //設置衰減率係數 void setDamping(int damping); //設置折射率係數 void setRefractive(int refractive); //設置石頭大小 void setStoneSize(int stoneSize); //設置石頭重量 void setStoneWeight(int stoneWeight); //設置背景圖片 void setImage(const QPixmap &pixmap); }; #endif // WAVEWATER_H
void WaveWater::paintEvent(QPaintEvent *) { if(image.isNull()) { return; } QPainter painter(this); painter.setRenderHints(QPainter::SmoothPixmapTransform); //若是中止階段則繪製原圖,不然繪製渲染圖 if (isStoped) { painter.drawPixmap(0, 0, image); } else { painter.drawPixmap(0, 0, pixmap); } } void WaveWater::drawImage() { if(isStoped) { timer->stop(); } else { this->waveRender(); this->update(); } } void WaveWater::waveSpread() { quint32 counter = 0; // 波能漸變 for(int h = 1; h < imageHeight - 1; h++) { for(int w = 1; w < imageWidth - 1; w++) { //波能擴散 waveBuffer2[w][h] = ((waveBuffer1[w - 1][h] + waveBuffer1[w + 1][h] + waveBuffer1[w][h - 1] + waveBuffer1[w][ h + 1]) >> 1) - waveBuffer2[w][h]; //波能衰減 waveBuffer2[w][h] -= (waveBuffer2[w][h] >> damping); //判斷波動釋放結束 if(waveBuffer1[w][h] - waveBuffer2[w][h]) { counter++; } } } //交換緩衝區 int **temp = waveBuffer1; waveBuffer1 = waveBuffer2; waveBuffer2 = temp; //設置釋放完成 isStoped = counter ? false : true; } void WaveWater::waveRender() { //先改變波紋振幅 waveSpread(); if(isStoped) { return; } int offsetX = 0; int offsetY = 0; //掃描位圖 for(int y = 1; y < imageHeight - 1; y++) { for(int x = 1; x < imageWidth - 1; x++) { //根據波幅計算位圖數據偏移值,渲染點(x,y)對應原始圖片(offsetX,offsetY) offsetX = x + ((waveBuffer1[x - 1][y] - waveBuffer1[x + 1][y]) >> refractive); offsetY = y + ((waveBuffer1[x][y - 1] - waveBuffer1[x][y + 1]) >> refractive); //複製象素 if(0 <= offsetX && offsetX < imageWidth - 1 && 0 <= offsetY && offsetY < imageHeight - 1) { QRgb color = imageOrigin.pixel(offsetX, offsetY); imageRender.setPixel(x, y, color); } } } pixmap.convertFromImage(imageRender); } void WaveWater::dropStone(int x, int y, int stoneSize, int stoneWeight) { isStoped = false; int posX = 0; int posY = 0; for(int h = -stoneSize; h < stoneSize; h++) { for(int w = -stoneSize; w < stoneSize; w++) { posX = x + w; posY = y + h; //控制範圍,不能超出圖片 if(posX < 0 || posX >= imageWidth || posY < 0 || posY >= imageHeight) { continue; } //點(w,h)在一個圓形區域內,初始化波能緩衝區1 if(h * h + w * w <= stoneSize * stoneSize) { waveBuffer1[posX][posY] = -stoneWeight; } } } }