Added implementation of blobs painter.
This commit is contained in:
parent
419989760c
commit
df8eed9576
4 changed files with 161 additions and 0 deletions
|
|
@ -68,6 +68,8 @@ PRIVATE
|
|||
ui/layers/layer_widget.h
|
||||
ui/paint/blob.cpp
|
||||
ui/paint/blob.h
|
||||
ui/paint/blobs.cpp
|
||||
ui/paint/blobs.h
|
||||
ui/platform/linux/ui_window_linux.cpp
|
||||
ui/platform/linux/ui_window_linux.h
|
||||
ui/platform/linux/ui_utility_linux.cpp
|
||||
|
|
|
|||
|
|
@ -404,4 +404,50 @@ private:
|
|||
|
||||
};
|
||||
|
||||
class continuous_value {
|
||||
public:
|
||||
continuous_value() = default;
|
||||
continuous_value(float64 duration) : _duration(duration) {
|
||||
}
|
||||
void start(float64 to, float64 duration) {
|
||||
_to = to;
|
||||
_delta = (_to - _cur) / duration;
|
||||
}
|
||||
void start(float64 to) {
|
||||
start(to, _duration);
|
||||
}
|
||||
void reset() {
|
||||
_to = _cur = _delta = 0.;
|
||||
}
|
||||
|
||||
float64 current() const {
|
||||
return _cur;
|
||||
}
|
||||
float64 to() const {
|
||||
return _to;
|
||||
}
|
||||
float64 delta() const {
|
||||
return _delta;
|
||||
}
|
||||
void update(crl::time dt, Fn<void(float64 &)> &&callback = nullptr) {
|
||||
if (_to != _cur) {
|
||||
_cur += _delta * dt;
|
||||
if ((_to != _cur) && ((_delta > 0) == (_cur > _to))) {
|
||||
_cur = _to;
|
||||
}
|
||||
if (callback) {
|
||||
callback(_cur);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
float64 _duration = 0.;
|
||||
float64 _to = 0.;
|
||||
|
||||
float64 _cur = 0.;
|
||||
float64 _delta = 0.;
|
||||
|
||||
};
|
||||
|
||||
} // namespace anim
|
||||
|
|
|
|||
63
ui/paint/blobs.cpp
Normal file
63
ui/paint/blobs.cpp
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
// This file is part of Desktop App Toolkit,
|
||||
// a set of libraries for developing nice desktop applications.
|
||||
//
|
||||
// For license and copyright information please follow this link:
|
||||
// https://github.com/desktop-app/legal/blob/master/LEGAL
|
||||
//
|
||||
#include "ui/paint/blobs.h"
|
||||
|
||||
#include "ui/painter.h"
|
||||
|
||||
namespace Ui::Paint {
|
||||
|
||||
Blobs::Blobs(
|
||||
std::vector<BlobData> blobDatas,
|
||||
float levelDuration,
|
||||
float maxLevel)
|
||||
: _maxLevel(maxLevel)
|
||||
, _blobDatas(blobDatas)
|
||||
, _levelValue(levelDuration) {
|
||||
init();
|
||||
}
|
||||
|
||||
void Blobs::init() {
|
||||
for (const auto &data : _blobDatas) {
|
||||
auto blob = Paint::BlobBezier(data.segmentsCount, data.minScale);
|
||||
blob.setRadius(data.minRadius, data.maxRadius);
|
||||
_blobs.push_back(std::move(blob));
|
||||
}
|
||||
}
|
||||
|
||||
float Blobs::maxRadius() const {
|
||||
const auto maxOfRadiuses = [](const BlobData data) {
|
||||
return std::max(data.maxRadius, data.minRadius);
|
||||
};
|
||||
const auto max = *ranges::max_element(
|
||||
_blobDatas,
|
||||
std::less<>(),
|
||||
maxOfRadiuses);
|
||||
return maxOfRadiuses(max);
|
||||
}
|
||||
|
||||
void Blobs::setLevel(float value) {
|
||||
const auto to = std::min(_maxLevel, value) / _maxLevel;
|
||||
_levelValue.start(to);
|
||||
}
|
||||
|
||||
void Blobs::paint(Painter &p, const QBrush &brush) {
|
||||
const auto dt = crl::now() - _lastUpdateTime;
|
||||
_levelValue.update((dt > 20) ? 17 : dt);
|
||||
|
||||
const auto opacity = p.opacity();
|
||||
for (auto i = 0; i < _blobs.size(); i++) {
|
||||
_blobs[i].update(_levelValue.current(), _blobDatas[i].speedScale);
|
||||
const auto alpha = _blobDatas[i].alpha;
|
||||
if (alpha != 1.) {
|
||||
p.setOpacity(opacity * alpha);
|
||||
}
|
||||
_blobs[i].paint(p, brush);
|
||||
}
|
||||
_lastUpdateTime = crl::now();
|
||||
}
|
||||
|
||||
} // namespace Ui::Paint
|
||||
50
ui/paint/blobs.h
Normal file
50
ui/paint/blobs.h
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
// This file is part of Desktop App Toolkit,
|
||||
// a set of libraries for developing nice desktop applications.
|
||||
//
|
||||
// For license and copyright information please follow this link:
|
||||
// https://github.com/desktop-app/legal/blob/master/LEGAL
|
||||
//
|
||||
#pragma once
|
||||
|
||||
#include "ui/effects/animation_value.h"
|
||||
#include "ui/paint/blob.h"
|
||||
|
||||
class Painter;
|
||||
|
||||
namespace Ui::Paint {
|
||||
|
||||
class Blobs final {
|
||||
public:
|
||||
struct BlobData {
|
||||
int segmentsCount = 0;
|
||||
float minScale = 0;
|
||||
float minRadius = 0;
|
||||
float maxRadius = 0;
|
||||
float speedScale = 0;
|
||||
float alpha = 0;
|
||||
};
|
||||
|
||||
Blobs(
|
||||
std::vector<BlobData> blobDatas,
|
||||
float levelDuration,
|
||||
float maxLevel);
|
||||
|
||||
void setLevel(float value);
|
||||
void paint(Painter &p, const QBrush &brush);
|
||||
|
||||
[[nodiscard]] float maxRadius() const;
|
||||
|
||||
private:
|
||||
void init();
|
||||
|
||||
const float _maxLevel;
|
||||
|
||||
std::vector<BlobData> _blobDatas;
|
||||
std::vector<BlobBezier> _blobs;
|
||||
|
||||
crl::time _lastUpdateTime = 0;
|
||||
anim::continuous_value _levelValue;
|
||||
|
||||
};
|
||||
|
||||
} // namespace Ui::Paint
|
||||
Loading…
Add table
Reference in a new issue