Icons don't need to be in a dedicated widget, just painting is enough.

Now BlockGrid holds the animations and does the drawing by
itself.
This commit is contained in:
King_DuckZ 2020-03-28 18:38:42 +01:00
parent 4527caff52
commit 78e72d0bcb
8 changed files with 132 additions and 152 deletions

View file

@ -0,0 +1,56 @@
/* Copyright 2020, Michele Santullo
* This file is part of memoserv.
*
* Memoserv is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Memoserv is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Memoserv. If not, see <http://www.gnu.org/licenses/>.
*/
#include "animated_pixmap.hpp"
#include <algorithm>
namespace duck {
AnimatedPixmap::AnimatedPixmap (bool loop) :
m_frame(0),
m_loop(loop)
{
}
AnimatedPixmap::~AnimatedPixmap() noexcept = default;
void AnimatedPixmap::push_back (QPixmap&& frame) {
m_frames.push_back(std::move(frame));
}
std::size_t AnimatedPixmap::frame_count() const {
return m_frames.size();
}
QSize AnimatedPixmap::size() const {
return {
(m_frames.empty() ? EmptyPixmapWidth : m_frames.front().width()),
(m_frames.empty() ? EmptyPixmapHeight : m_frames.front().height())
};
}
void AnimatedPixmap::advance_frame() {
++m_frame;
if (m_loop)
m_frame = (not m_frames.empty() ? m_frame % m_frames.size() : 0);
else
m_frame = std::min(m_frame, m_frames.size());
}
const QPixmap& AnimatedPixmap::current_frame() const {
return m_frames[m_frame];
}
} //namespace duck

View file

@ -17,43 +17,32 @@
#pragma once #pragma once
#include <QWidget>
#include <QPixmap> #include <QPixmap>
#include <vector> #include <vector>
namespace duck::widget { namespace duck {
class AnimatedIcon : public QWidget { class AnimatedPixmap {
Q_OBJECT
public: public:
static const constexpr unsigned int EmptyIconWidth = 50; static const constexpr int EmptyPixmapWidth = -1;
static const constexpr unsigned int EmptyIconHeight = EmptyIconWidth; static const constexpr int EmptyPixmapHeight = EmptyPixmapWidth;
AnimatedIcon (AnimatedIcon&&) = default; AnimatedPixmap (AnimatedPixmap&&) = default;
AnimatedIcon (const AnimatedIcon&) = delete; AnimatedPixmap (const AnimatedPixmap&) = delete;
explicit AnimatedIcon (QWidget* parent=nullptr, float fps=24.0f, bool loop=true); AnimatedPixmap (bool loop);
~AnimatedIcon() noexcept; ~AnimatedPixmap() noexcept;
AnimatedIcon& operator= (AnimatedIcon&&) = default; AnimatedPixmap& operator= (AnimatedPixmap&&) = default;
QSize minimumSizeHint() const override;
QSize sizeHint() const override;
void push_back (QPixmap&& frame); void push_back (QPixmap&& frame);
std::size_t size() const; std::size_t frame_count() const;
QSize size() const;
unsigned int width() const;
unsigned int height() const;
public slots:
void advance_frame(); void advance_frame();
const QPixmap& current_frame() const;
protected:
void paintEvent (QPaintEvent* event) override;
private: private:
std::vector<QPixmap> m_frames; std::vector<QPixmap> m_frames;
std::size_t m_frame; std::size_t m_frame;
float m_fps;
bool m_loop; bool m_loop;
}; };
} //namespace duck::widget } //namespace duck

View file

@ -21,15 +21,15 @@
#include <QPixmap> #include <QPixmap>
namespace duck { namespace duck {
std::unique_ptr<widget::AnimatedIcon> make_qt_animation (const mc::psx::ConstBlock& block, int width, int height, float fps) { AnimatedPixmap make_qt_animation (const mc::psx::ConstBlock& block, int width, int height) {
using mc::icon_fetch; using mc::icon_fetch;
auto retval = std::make_unique<widget::AnimatedIcon>(); AnimatedPixmap retval(true);
for (const auto& frame : icon_fetch(block, width, height)) { for (const auto& frame : icon_fetch(block, width, height)) {
QPixmap pix(width, height); QPixmap pix(width, height);
pix.loadFromData(reinterpret_cast<const uchar*>(frame.data()), frame.size(), "BMP"); pix.loadFromData(reinterpret_cast<const uchar*>(frame.data()), frame.size(), "BMP");
retval->push_back(std::move(pix)); retval.push_back(std::move(pix));
} }
return retval; return retval;

View file

@ -17,8 +17,7 @@
#pragma once #pragma once
#include "widgets/animated_icon.hpp" #include "animated_pixmap.hpp"
#include <memory>
namespace mc::psx { namespace mc::psx {
template <bool> class BasicBlock; template <bool> class BasicBlock;
@ -26,10 +25,9 @@ using ConstBlock = BasicBlock<true>;
} //namespace mc::psx } //namespace mc::psx
namespace duck { namespace duck {
std::unique_ptr<widget::AnimatedIcon> make_qt_animation ( AnimatedPixmap make_qt_animation (
const mc::psx::ConstBlock& block, const mc::psx::ConstBlock& block,
int width, int width,
int height, int height
float fps
); );
} //namespace duck } //namespace duck

View file

@ -9,7 +9,6 @@ moc_files = qt5.preprocess(
'memoserv_win.hpp', 'memoserv_win.hpp',
'version_info_win.hpp', 'version_info_win.hpp',
'widgets/block_grid.hpp', 'widgets/block_grid.hpp',
'widgets/animated_icon.hpp',
], ],
#ui_files: 'main_window.ui', #ui_files: 'main_window.ui',
moc_extra_arguments: ['-DMAKES_MY_MOC_HEADER_COMPILE'], moc_extra_arguments: ['-DMAKES_MY_MOC_HEADER_COMPILE'],
@ -36,8 +35,8 @@ executable(app_name,
project_config_file, project_config_file,
gitrev_config_file, gitrev_config_file,
'widgets/block_grid.cpp', 'widgets/block_grid.cpp',
'widgets/animated_icon.cpp',
'make_qt_animation.cpp', 'make_qt_animation.cpp',
'animated_pixmap.cpp',
include_directories: inc, include_directories: inc,
dependencies: [qt5_dep, fslib_dep, memcard_dep], dependencies: [qt5_dep, fslib_dep, memcard_dep],
install: true, install: true,

View file

@ -1,78 +0,0 @@
/* Copyright 2020, Michele Santullo
* This file is part of memoserv.
*
* Memoserv is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Memoserv is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Memoserv. If not, see <http://www.gnu.org/licenses/>.
*/
#include "animated_icon.hpp"
#include <QSizePolicy>
#include <QPainter>
#include <algorithm>
namespace duck::widget {
namespace {
} //unnamed namespace
AnimatedIcon::AnimatedIcon (QWidget* parent, float fps, bool loop) :
QWidget(parent),
m_frame(0),
m_fps(fps),
m_loop(loop)
{
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
}
AnimatedIcon::~AnimatedIcon() noexcept = default;
QSize AnimatedIcon::minimumSizeHint() const {
return {static_cast<int>(width()), static_cast<int>(height())};
}
QSize AnimatedIcon::sizeHint() const {
return {static_cast<int>(width()), static_cast<int>(height())};
}
void AnimatedIcon::push_back (QPixmap&& frame) {
m_frames.push_back(std::move(frame));
}
std::size_t AnimatedIcon::size() const {
return m_frames.size();
}
unsigned int AnimatedIcon::width() const {
return (m_frames.empty() ? EmptyIconWidth : m_frames.front().width());
}
unsigned int AnimatedIcon::height() const {
return (m_frames.empty() ? EmptyIconHeight : m_frames.front().height());
}
void AnimatedIcon::advance_frame() {
++m_frame;
if (m_loop)
m_frame = (not m_frames.empty() ? m_frame % m_frames.size() : 0);
else
m_frame = std::min(m_frame, m_frames.size());
update();
}
void AnimatedIcon::paintEvent (QPaintEvent* event) {
if (m_frame < m_frames.size()) {
QPainter painter(this);
painter.drawPixmap(0, 0, m_frames[m_frame]);
}
}
} //namespace duck::widget

View file

@ -17,28 +17,19 @@
#include "block_grid.hpp" #include "block_grid.hpp"
#include <QGridLayout> #include <QGridLayout>
#include <iostream> #include <QHBoxLayout>
#include <QPainter>
#include <algorithm>
namespace duck::widget { namespace duck::widget {
namespace { namespace {
const constexpr int g_icon_spacing = 3; const constexpr int g_icon_spacing = 3;
} //unnamed namespace } //unnamed namespace
BlockGrid::BlockGrid (QWidget* parent) :
BlockGrid(parent, 3)
{
}
BlockGrid::BlockGrid (QWidget* parent, unsigned int columns) : BlockGrid::BlockGrid (QWidget* parent, unsigned int columns) :
QWidget(parent), QWidget(parent),
m_columns(columns), m_columns(columns)
m_block_lay(nullptr)
{ {
auto block_lay = std::make_unique<QGridLayout>();
block_lay->setSpacing(g_icon_spacing);
m_block_lay = block_lay.get();
m_block_lay->setColumnStretch(m_columns, 1);
this->setLayout(block_lay.release());
} }
BlockGrid::~BlockGrid() noexcept = default; BlockGrid::~BlockGrid() noexcept = default;
@ -48,39 +39,61 @@ QSize BlockGrid::minimumSizeHint() const {
} }
QSize BlockGrid::sizeHint() const { QSize BlockGrid::sizeHint() const {
const auto w = icon_width() * m_columns + g_icon_spacing * (1 + m_columns); if (m_columns) {
const unsigned int rows = size() / m_columns; const auto w = icon_width() * m_columns + g_icon_spacing * (1 + m_columns);
const auto h = icon_height() * rows + g_icon_spacing * (rows + 1); const unsigned int rows = size() / m_columns;
const auto h = icon_height() * rows + g_icon_spacing * (rows + 1);
return {static_cast<int>(w), static_cast<int>(h)}; return {static_cast<int>(w), static_cast<int>(h)};
}
else {
return {-1, -1};
}
} }
unsigned int BlockGrid::icon_width() const { unsigned int BlockGrid::icon_width() const {
return m_icons.empty() ? AnimatedIcon::EmptyIconWidth : m_icons.front()->width(); return m_icons.empty() ? static_cast<unsigned int>(g_icon_spacing) : m_icons.front().size().width();
} }
unsigned int BlockGrid::icon_height() const { unsigned int BlockGrid::icon_height() const {
return m_icons.empty() ? AnimatedIcon::EmptyIconHeight : m_icons.front()->height(); return m_icons.empty() ? static_cast<unsigned int>(g_icon_spacing) : m_icons.front().size().height();
} }
void BlockGrid::emplace_back (std::unique_ptr<AnimatedIcon>&& anim) { void BlockGrid::push_back (AnimatedPixmap&& anim) {
const auto& columns = m_columns; m_icons.push_back(std::move(anim));
const auto count = this->size();
const int x = count % columns;
const int y = count / columns;
{
const auto raw_ptr = anim.release();
m_block_lay->addWidget(raw_ptr, y, x);
m_icons.push_back(raw_ptr);
}
m_block_lay->setRowStretch(y, 0);
m_block_lay->setRowStretch(y + 1, 1);
} }
std::size_t BlockGrid::size() const { std::size_t BlockGrid::size() const {
return m_icons.size(); return m_icons.size();
} }
unsigned int BlockGrid::columns() const {
if (m_columns) {
return m_columns;
}
else {
const auto widget_w = static_cast<unsigned int>(std::max(this->width(), g_icon_spacing));
const auto ico_spacing_uns = static_cast<unsigned int>(g_icon_spacing);
return (widget_w - ico_spacing_uns) / (icon_width() + ico_spacing_uns);
}
}
void BlockGrid::paintEvent (QPaintEvent* event) {
QPainter painter(this);
const int max_col = static_cast<int>(this->columns());
int x = 0;
int y = 0;
int count = 0;
for (auto& ico : m_icons) {
x = g_icon_spacing + (count % max_col) * (g_icon_spacing + ico.size().width());
y = g_icon_spacing + (count / max_col) * (g_icon_spacing + ico.size().height());
if (ico.frame_count()) {
painter.drawPixmap(x, y, ico.current_frame());
}
++count;
}
}
} //namespace duck::widget } //namespace duck::widget

View file

@ -17,7 +17,7 @@
#pragma once #pragma once
#include "animated_icon.hpp" #include "../animated_pixmap.hpp"
#include <QWidget> #include <QWidget>
#include <memory> #include <memory>
#include <vector> #include <vector>
@ -28,8 +28,7 @@ namespace duck::widget {
class BlockGrid : public QWidget { class BlockGrid : public QWidget {
Q_OBJECT Q_OBJECT
public: public:
explicit BlockGrid (QWidget* parent=nullptr); explicit BlockGrid (QWidget* parent=nullptr, unsigned int columns=0);
BlockGrid (QWidget* parent, unsigned int columns);
~BlockGrid() noexcept; ~BlockGrid() noexcept;
QSize minimumSizeHint() const override; QSize minimumSizeHint() const override;
@ -38,12 +37,16 @@ public:
unsigned int icon_width() const; unsigned int icon_width() const;
unsigned int icon_height() const; unsigned int icon_height() const;
void emplace_back (std::unique_ptr<AnimatedIcon>&& anim); void push_back (AnimatedPixmap&& anim);
std::size_t size() const; std::size_t size() const;
protected:
void paintEvent (QPaintEvent* event) override;
private: private:
std::vector<AnimatedIcon*> m_icons; unsigned int columns() const;
std::vector<AnimatedPixmap> m_icons;
unsigned int m_columns; unsigned int m_columns;
QGridLayout* m_block_lay;
}; };
} //namespace duck::widget } //namespace duck::widget