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:
parent
4527caff52
commit
78e72d0bcb
8 changed files with 132 additions and 152 deletions
56
src/qt/animated_pixmap.cpp
Normal file
56
src/qt/animated_pixmap.cpp
Normal 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
|
|
@ -17,43 +17,32 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <QWidget>
|
||||
#include <QPixmap>
|
||||
#include <vector>
|
||||
|
||||
namespace duck::widget {
|
||||
class AnimatedIcon : public QWidget {
|
||||
Q_OBJECT
|
||||
namespace duck {
|
||||
class AnimatedPixmap {
|
||||
public:
|
||||
static const constexpr unsigned int EmptyIconWidth = 50;
|
||||
static const constexpr unsigned int EmptyIconHeight = EmptyIconWidth;
|
||||
static const constexpr int EmptyPixmapWidth = -1;
|
||||
static const constexpr int EmptyPixmapHeight = EmptyPixmapWidth;
|
||||
|
||||
AnimatedIcon (AnimatedIcon&&) = default;
|
||||
AnimatedIcon (const AnimatedIcon&) = delete;
|
||||
explicit AnimatedIcon (QWidget* parent=nullptr, float fps=24.0f, bool loop=true);
|
||||
~AnimatedIcon() noexcept;
|
||||
AnimatedPixmap (AnimatedPixmap&&) = default;
|
||||
AnimatedPixmap (const AnimatedPixmap&) = delete;
|
||||
AnimatedPixmap (bool loop);
|
||||
~AnimatedPixmap() noexcept;
|
||||
|
||||
AnimatedIcon& operator= (AnimatedIcon&&) = default;
|
||||
|
||||
QSize minimumSizeHint() const override;
|
||||
QSize sizeHint() const override;
|
||||
AnimatedPixmap& operator= (AnimatedPixmap&&) = default;
|
||||
|
||||
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();
|
||||
|
||||
protected:
|
||||
void paintEvent (QPaintEvent* event) override;
|
||||
const QPixmap& current_frame() const;
|
||||
|
||||
private:
|
||||
std::vector<QPixmap> m_frames;
|
||||
std::size_t m_frame;
|
||||
float m_fps;
|
||||
bool m_loop;
|
||||
};
|
||||
} //namespace duck::widget
|
||||
} //namespace duck
|
|
@ -21,15 +21,15 @@
|
|||
#include <QPixmap>
|
||||
|
||||
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;
|
||||
|
||||
auto retval = std::make_unique<widget::AnimatedIcon>();
|
||||
AnimatedPixmap retval(true);
|
||||
|
||||
for (const auto& frame : icon_fetch(block, width, height)) {
|
||||
QPixmap pix(width, height);
|
||||
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;
|
||||
|
|
|
@ -17,8 +17,7 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include "widgets/animated_icon.hpp"
|
||||
#include <memory>
|
||||
#include "animated_pixmap.hpp"
|
||||
|
||||
namespace mc::psx {
|
||||
template <bool> class BasicBlock;
|
||||
|
@ -26,10 +25,9 @@ using ConstBlock = BasicBlock<true>;
|
|||
} //namespace mc::psx
|
||||
|
||||
namespace duck {
|
||||
std::unique_ptr<widget::AnimatedIcon> make_qt_animation (
|
||||
AnimatedPixmap make_qt_animation (
|
||||
const mc::psx::ConstBlock& block,
|
||||
int width,
|
||||
int height,
|
||||
float fps
|
||||
int height
|
||||
);
|
||||
} //namespace duck
|
||||
|
|
|
@ -9,7 +9,6 @@ moc_files = qt5.preprocess(
|
|||
'memoserv_win.hpp',
|
||||
'version_info_win.hpp',
|
||||
'widgets/block_grid.hpp',
|
||||
'widgets/animated_icon.hpp',
|
||||
],
|
||||
#ui_files: 'main_window.ui',
|
||||
moc_extra_arguments: ['-DMAKES_MY_MOC_HEADER_COMPILE'],
|
||||
|
@ -36,8 +35,8 @@ executable(app_name,
|
|||
project_config_file,
|
||||
gitrev_config_file,
|
||||
'widgets/block_grid.cpp',
|
||||
'widgets/animated_icon.cpp',
|
||||
'make_qt_animation.cpp',
|
||||
'animated_pixmap.cpp',
|
||||
include_directories: inc,
|
||||
dependencies: [qt5_dep, fslib_dep, memcard_dep],
|
||||
install: true,
|
||||
|
|
|
@ -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
|
|
@ -17,28 +17,19 @@
|
|||
|
||||
#include "block_grid.hpp"
|
||||
#include <QGridLayout>
|
||||
#include <iostream>
|
||||
#include <QHBoxLayout>
|
||||
#include <QPainter>
|
||||
#include <algorithm>
|
||||
|
||||
namespace duck::widget {
|
||||
namespace {
|
||||
const constexpr int g_icon_spacing = 3;
|
||||
} //unnamed namespace
|
||||
|
||||
BlockGrid::BlockGrid (QWidget* parent) :
|
||||
BlockGrid(parent, 3)
|
||||
{
|
||||
}
|
||||
|
||||
BlockGrid::BlockGrid (QWidget* parent, unsigned int columns) :
|
||||
QWidget(parent),
|
||||
m_columns(columns),
|
||||
m_block_lay(nullptr)
|
||||
m_columns(columns)
|
||||
{
|
||||
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;
|
||||
|
@ -48,39 +39,61 @@ QSize BlockGrid::minimumSizeHint() const {
|
|||
}
|
||||
|
||||
QSize BlockGrid::sizeHint() const {
|
||||
const auto w = icon_width() * m_columns + g_icon_spacing * (1 + m_columns);
|
||||
const unsigned int rows = size() / m_columns;
|
||||
const auto h = icon_height() * rows + g_icon_spacing * (rows + 1);
|
||||
if (m_columns) {
|
||||
const auto w = icon_width() * m_columns + g_icon_spacing * (1 + m_columns);
|
||||
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 {
|
||||
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 {
|
||||
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) {
|
||||
const auto& columns = m_columns;
|
||||
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);
|
||||
void BlockGrid::push_back (AnimatedPixmap&& anim) {
|
||||
m_icons.push_back(std::move(anim));
|
||||
}
|
||||
|
||||
std::size_t BlockGrid::size() const {
|
||||
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
|
||||
|
|
|
@ -17,7 +17,7 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include "animated_icon.hpp"
|
||||
#include "../animated_pixmap.hpp"
|
||||
#include <QWidget>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
@ -28,8 +28,7 @@ namespace duck::widget {
|
|||
class BlockGrid : public QWidget {
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit BlockGrid (QWidget* parent=nullptr);
|
||||
BlockGrid (QWidget* parent, unsigned int columns);
|
||||
explicit BlockGrid (QWidget* parent=nullptr, unsigned int columns=0);
|
||||
~BlockGrid() noexcept;
|
||||
|
||||
QSize minimumSizeHint() const override;
|
||||
|
@ -38,12 +37,16 @@ public:
|
|||
unsigned int icon_width() const;
|
||||
unsigned int icon_height() const;
|
||||
|
||||
void emplace_back (std::unique_ptr<AnimatedIcon>&& anim);
|
||||
void push_back (AnimatedPixmap&& anim);
|
||||
std::size_t size() const;
|
||||
|
||||
protected:
|
||||
void paintEvent (QPaintEvent* event) override;
|
||||
|
||||
private:
|
||||
std::vector<AnimatedIcon*> m_icons;
|
||||
unsigned int columns() const;
|
||||
|
||||
std::vector<AnimatedPixmap> m_icons;
|
||||
unsigned int m_columns;
|
||||
QGridLayout* m_block_lay;
|
||||
};
|
||||
} //namespace duck::widget
|
||||
|
|
Loading…
Add table
Reference in a new issue