Add a timer to get the animations going.

This commit is contained in:
King_DuckZ 2020-03-28 18:53:04 +01:00
parent 78e72d0bcb
commit 88be1a1247
3 changed files with 20 additions and 6 deletions

View file

@ -100,7 +100,7 @@ void MemoservWin::open_file_from_dialog() {
m_memcards.push_back(mc::psx::make_memory_card(file.toStdString())); m_memcards.push_back(mc::psx::make_memory_card(file.toStdString()));
auto& mc = m_memcards.back(); auto& mc = m_memcards.back();
auto grid = std::make_unique<widget::BlockGrid>(this, 3); auto grid = std::make_unique<widget::BlockGrid>(this, 3, g_icon_fps);
for (const auto& block : mc) { for (const auto& block : mc) {
for (std::size_t n = 0; block.has_magic() and n < block.block_count(); ++n) { for (std::size_t n = 0; block.has_magic() and n < block.block_count(); ++n) {
grid->emplace_back(make_qt_animation(block, g_icon_size, g_icon_size, g_icon_fps)); grid->emplace_back(make_qt_animation(block, g_icon_size, g_icon_size, g_icon_fps));

View file

@ -16,9 +16,8 @@
*/ */
#include "block_grid.hpp" #include "block_grid.hpp"
#include <QGridLayout>
#include <QHBoxLayout>
#include <QPainter> #include <QPainter>
#include <QTimer>
#include <algorithm> #include <algorithm>
namespace duck::widget { namespace duck::widget {
@ -26,10 +25,14 @@ namespace {
const constexpr int g_icon_spacing = 3; const constexpr int g_icon_spacing = 3;
} //unnamed namespace } //unnamed namespace
BlockGrid::BlockGrid (QWidget* parent, unsigned int columns) : BlockGrid::BlockGrid (QWidget* parent, unsigned int columns, float fps) :
QWidget(parent), QWidget(parent),
m_anim_timer(new QTimer(this)),
m_columns(columns) m_columns(columns)
{ {
fps = std::max(fps, 1.0f);
m_anim_timer->start(1000.0f / fps);
connect(m_anim_timer, &QTimer::timeout, this, &BlockGrid::advance_frame);
} }
BlockGrid::~BlockGrid() noexcept = default; BlockGrid::~BlockGrid() noexcept = default;
@ -96,4 +99,11 @@ void BlockGrid::paintEvent (QPaintEvent* event) {
++count; ++count;
} }
} }
void BlockGrid::advance_frame() {
for (auto& ico : m_icons) {
ico.advance_frame();
}
this->update();
}
} //namespace duck::widget } //namespace duck::widget

View file

@ -22,13 +22,13 @@
#include <memory> #include <memory>
#include <vector> #include <vector>
class QGridLayout; class QTimer;
namespace duck::widget { namespace duck::widget {
class BlockGrid : public QWidget { class BlockGrid : public QWidget {
Q_OBJECT Q_OBJECT
public: public:
explicit BlockGrid (QWidget* parent=nullptr, unsigned int columns=0); explicit BlockGrid (QWidget* parent=nullptr, unsigned int columns=0, float fps=24.0f);
~BlockGrid() noexcept; ~BlockGrid() noexcept;
QSize minimumSizeHint() const override; QSize minimumSizeHint() const override;
@ -43,10 +43,14 @@ public:
protected: protected:
void paintEvent (QPaintEvent* event) override; void paintEvent (QPaintEvent* event) override;
private slots:
void advance_frame();
private: private:
unsigned int columns() const; unsigned int columns() const;
std::vector<AnimatedPixmap> m_icons; std::vector<AnimatedPixmap> m_icons;
QTimer* m_anim_timer;
unsigned int m_columns; unsigned int m_columns;
}; };
} //namespace duck::widget } //namespace duck::widget