2014-05-30 08:53:19 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2014-12-01 10:47:38 +00:00
|
|
|
the official desktop version of Telegram messaging app, see https://telegram.org
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
Telegram Desktop 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.
|
|
|
|
|
|
|
|
It 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.
|
|
|
|
|
2015-10-03 13:16:42 +00:00
|
|
|
In addition, as a special exception, the copyright holders give permission
|
|
|
|
to link the code of portions of this program with the OpenSSL library.
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE
|
2016-02-08 10:56:18 +00:00
|
|
|
Copyright (c) 2014-2016 John Preston, https://desktop.telegram.org
|
2014-05-30 08:53:19 +00:00
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
2016-04-12 21:31:28 +00:00
|
|
|
#include "core/basic_types.h"
|
2016-09-26 12:09:59 +00:00
|
|
|
#include "core/lambda_wrap.h"
|
2014-05-30 08:53:19 +00:00
|
|
|
#include <QtCore/QTimer>
|
|
|
|
#include <QtGui/QColor>
|
|
|
|
|
2016-06-24 10:37:29 +00:00
|
|
|
namespace Media {
|
|
|
|
namespace Clip {
|
|
|
|
|
|
|
|
class Reader;
|
2016-09-28 20:28:53 +00:00
|
|
|
class ReaderPointer {
|
|
|
|
public:
|
2016-09-29 11:37:16 +00:00
|
|
|
ReaderPointer(std_::nullptr_t = nullptr) {
|
2016-09-28 20:28:53 +00:00
|
|
|
}
|
|
|
|
explicit ReaderPointer(Reader *pointer) : _pointer(pointer) {
|
|
|
|
}
|
|
|
|
ReaderPointer(const ReaderPointer &other) = delete;
|
|
|
|
ReaderPointer &operator=(const ReaderPointer &other) = delete;
|
|
|
|
ReaderPointer(ReaderPointer &&other) : _pointer(createAndSwap(other._pointer)) {
|
|
|
|
}
|
|
|
|
ReaderPointer &operator=(ReaderPointer &&other) {
|
|
|
|
swap(other);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
void swap(ReaderPointer &other) {
|
|
|
|
qSwap(_pointer, other._pointer);
|
|
|
|
}
|
|
|
|
Reader *get() const {
|
|
|
|
return valid() ? _pointer : nullptr;
|
|
|
|
}
|
|
|
|
Reader *operator->() const {
|
|
|
|
return get();
|
|
|
|
}
|
|
|
|
void setBad() {
|
|
|
|
reset();
|
|
|
|
_pointer = BadPointer;
|
|
|
|
}
|
|
|
|
void reset() {
|
|
|
|
ReaderPointer temp;
|
|
|
|
swap(temp);
|
|
|
|
}
|
|
|
|
bool isBad() const {
|
|
|
|
return (_pointer == BadPointer);
|
|
|
|
}
|
|
|
|
bool valid() const {
|
|
|
|
return _pointer && !isBad();
|
|
|
|
}
|
|
|
|
explicit operator bool() const {
|
|
|
|
return valid();
|
|
|
|
}
|
|
|
|
~ReaderPointer();
|
|
|
|
|
|
|
|
private:
|
|
|
|
Reader *_pointer = nullptr;
|
|
|
|
static Reader *const BadPointer;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2016-06-24 10:37:29 +00:00
|
|
|
class Manager;
|
|
|
|
|
|
|
|
enum Notification {
|
|
|
|
NotificationReinit,
|
|
|
|
NotificationRepaint,
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Clip
|
|
|
|
} // namespace Media
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
namespace anim {
|
|
|
|
|
|
|
|
typedef float64 (*transition)(const float64 &delta, const float64 &dt);
|
2015-12-31 20:07:41 +00:00
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
float64 linear(const float64 &delta, const float64 &dt);
|
|
|
|
float64 sineInOut(const float64 &delta, const float64 &dt);
|
|
|
|
float64 halfSine(const float64 &delta, const float64 &dt);
|
|
|
|
float64 easeOutBack(const float64 &delta, const float64 &dt);
|
|
|
|
float64 easeInCirc(const float64 &delta, const float64 &dt);
|
|
|
|
float64 easeOutCirc(const float64 &delta, const float64 &dt);
|
|
|
|
float64 easeInCubic(const float64 &delta, const float64 &dt);
|
|
|
|
float64 easeOutCubic(const float64 &delta, const float64 &dt);
|
|
|
|
float64 easeInQuint(const float64 &delta, const float64 &dt);
|
|
|
|
float64 easeOutQuint(const float64 &delta, const float64 &dt);
|
|
|
|
|
|
|
|
class fvalue { // float animated value
|
|
|
|
public:
|
2016-09-26 12:09:59 +00:00
|
|
|
using ValueType = float64;
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-10-05 16:56:27 +00:00
|
|
|
fvalue() = default;
|
|
|
|
fvalue(float64 from) : _cur(from), _from(from) {
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2016-10-05 16:56:27 +00:00
|
|
|
fvalue(float64 from, float64 to) : _cur(from), _from(from), _delta(to - from) {
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2016-10-05 16:56:27 +00:00
|
|
|
void start(float64 to) {
|
2014-05-30 08:53:19 +00:00
|
|
|
_from = _cur;
|
|
|
|
_delta = to - _from;
|
|
|
|
}
|
|
|
|
void restart() {
|
|
|
|
_delta = _from + _delta - _cur;
|
|
|
|
_from = _cur;
|
|
|
|
}
|
2016-10-05 16:56:27 +00:00
|
|
|
|
|
|
|
float64 from() const {
|
|
|
|
return _from;
|
|
|
|
}
|
|
|
|
float64 current() const {
|
2014-05-30 08:53:19 +00:00
|
|
|
return _cur;
|
|
|
|
}
|
2014-10-17 12:57:14 +00:00
|
|
|
float64 to() const {
|
|
|
|
return _from + _delta;
|
|
|
|
}
|
2016-10-05 16:56:27 +00:00
|
|
|
void add(float64 delta) {
|
|
|
|
_from += delta;
|
|
|
|
_cur += delta;
|
|
|
|
}
|
|
|
|
fvalue &update(float64 dt, transition func) {
|
2014-05-30 08:53:19 +00:00
|
|
|
_cur = _from + (*func)(_delta, dt);
|
2014-08-11 09:03:45 +00:00
|
|
|
return *this;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
void finish() {
|
|
|
|
_cur = _from + _delta;
|
|
|
|
_from = _cur;
|
|
|
|
_delta = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-10-05 16:56:27 +00:00
|
|
|
float64 _cur = 0.;
|
|
|
|
float64 _from = 0.;
|
|
|
|
float64 _delta = 0.;
|
2016-09-26 12:09:59 +00:00
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class ivalue { // int animated value
|
|
|
|
public:
|
2016-10-05 16:56:27 +00:00
|
|
|
using ValueType = int;
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-10-05 16:56:27 +00:00
|
|
|
ivalue() = default;
|
|
|
|
ivalue(int from) : _cur(from), _from(float64(from)) {
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2016-10-05 16:56:27 +00:00
|
|
|
ivalue(int from, int to) : _cur(from), _from(float64(from)), _delta(float64(to - from)) {
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
void start(int32 to) {
|
|
|
|
_from = float64(_cur);
|
|
|
|
_delta = float64(to) - _from;
|
|
|
|
}
|
|
|
|
void restart() {
|
|
|
|
_delta = _from + _delta - float64(_cur);
|
|
|
|
_from = float64(_cur);
|
|
|
|
}
|
2016-10-05 16:56:27 +00:00
|
|
|
|
|
|
|
int from() const {
|
|
|
|
return _from;
|
|
|
|
}
|
|
|
|
int current() const {
|
2014-05-30 08:53:19 +00:00
|
|
|
return _cur;
|
|
|
|
}
|
2016-10-05 16:56:27 +00:00
|
|
|
int to() const {
|
|
|
|
return qRound(_from + _delta);
|
|
|
|
}
|
|
|
|
void add(int delta) {
|
|
|
|
_from += delta;
|
|
|
|
_cur += delta;
|
2014-10-17 12:57:14 +00:00
|
|
|
}
|
2016-10-05 16:56:27 +00:00
|
|
|
ivalue &update(float64 dt, transition func) {
|
2014-05-30 08:53:19 +00:00
|
|
|
_cur = qRound(_from + (*func)(_delta, dt));
|
2014-08-11 09:03:45 +00:00
|
|
|
return *this;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
void finish() {
|
|
|
|
_cur = qRound(_from + _delta);
|
|
|
|
_from = _cur;
|
|
|
|
_delta = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-10-05 16:56:27 +00:00
|
|
|
int _cur = 0;
|
|
|
|
float64 _from = 0.;
|
|
|
|
float64 _delta = 0.;
|
2016-09-26 12:09:59 +00:00
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class cvalue { // QColor animated value
|
|
|
|
public:
|
2016-09-26 12:09:59 +00:00
|
|
|
using ValueType = QColor;
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-10-05 16:56:27 +00:00
|
|
|
cvalue() = default;
|
|
|
|
cvalue(const QColor &from)
|
|
|
|
: _cur(from)
|
|
|
|
, _from_r(from.redF())
|
|
|
|
, _from_g(from.greenF())
|
|
|
|
, _from_b(from.blueF())
|
|
|
|
, _from_a(from.alphaF()) {
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
cvalue(const QColor &from, const QColor &to)
|
|
|
|
: _cur(from)
|
2016-10-05 16:56:27 +00:00
|
|
|
, _from_r(from.redF())
|
|
|
|
, _from_g(from.greenF())
|
|
|
|
, _from_b(from.blueF())
|
|
|
|
, _from_a(from.alphaF())
|
|
|
|
, _delta_r(to.redF() - from.redF())
|
|
|
|
, _delta_g(to.greenF() - from.greenF())
|
|
|
|
, _delta_b(to.blueF() - from.blueF())
|
|
|
|
, _delta_a(to.alphaF() - from.alphaF()) {
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
void start(const QColor &to) {
|
|
|
|
_from_r = _cur.redF();
|
|
|
|
_from_g = _cur.greenF();
|
|
|
|
_from_b = _cur.blueF();
|
|
|
|
_from_a = _cur.alphaF();
|
|
|
|
_delta_r = to.redF() - _from_r;
|
|
|
|
_delta_g = to.greenF() - _from_g;
|
|
|
|
_delta_b = to.blueF() - _from_b;
|
|
|
|
_delta_a = to.alphaF() - _from_a;
|
|
|
|
}
|
|
|
|
void restart() {
|
|
|
|
_delta_r = _from_r + _delta_r - _cur.redF();
|
|
|
|
_delta_g = _from_g + _delta_g - _cur.greenF();
|
|
|
|
_delta_b = _from_b + _delta_b - _cur.blueF();
|
|
|
|
_delta_a = _from_a + _delta_a - _cur.alphaF();
|
|
|
|
_from_r = _cur.redF();
|
|
|
|
_from_g = _cur.greenF();
|
|
|
|
_from_b = _cur.blueF();
|
|
|
|
_from_a = _cur.alphaF();
|
|
|
|
}
|
2016-10-05 16:56:27 +00:00
|
|
|
QColor from() const {
|
|
|
|
QColor result;
|
|
|
|
result.setRedF(_from_r);
|
|
|
|
result.setGreenF(_from_g);
|
|
|
|
result.setBlueF(_from_b);
|
|
|
|
result.setAlphaF(_from_a);
|
|
|
|
return result;
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
const QColor ¤t() const {
|
|
|
|
return _cur;
|
|
|
|
}
|
2014-10-17 12:57:14 +00:00
|
|
|
QColor to() const {
|
|
|
|
QColor result;
|
|
|
|
result.setRedF(_from_r + _delta_r);
|
|
|
|
result.setGreenF(_from_g + _delta_g);
|
|
|
|
result.setBlueF(_from_b + _delta_b);
|
|
|
|
result.setAlphaF(_from_a + _delta_a);
|
|
|
|
return result;
|
|
|
|
}
|
2014-08-11 09:03:45 +00:00
|
|
|
cvalue &update(const float64 &dt, transition func) {
|
2014-05-30 08:53:19 +00:00
|
|
|
_cur.setRedF(_from_r + (*func)(_delta_r, dt));
|
|
|
|
_cur.setGreenF(_from_g + (*func)(_delta_g, dt));
|
|
|
|
_cur.setBlueF(_from_b + (*func)(_delta_b, dt));
|
|
|
|
_cur.setAlphaF(_from_a + (*func)(_delta_a, dt));
|
2014-08-11 09:03:45 +00:00
|
|
|
return *this;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
void finish() {
|
|
|
|
_cur.setRedF(_from_r + _delta_r);
|
|
|
|
_cur.setGreenF(_from_g + _delta_g);
|
|
|
|
_cur.setBlueF(_from_b + _delta_b);
|
|
|
|
_cur.setAlphaF(_from_a + _delta_a);
|
|
|
|
_from_r = _cur.redF();
|
|
|
|
_from_g = _cur.greenF();
|
|
|
|
_from_b = _cur.blueF();
|
|
|
|
_from_a = _cur.alphaF();
|
|
|
|
_delta_r = _delta_g = _delta_b = _delta_a = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
QColor _cur;
|
2016-10-05 16:56:27 +00:00
|
|
|
float64 _from_r = 0.;
|
|
|
|
float64 _from_g = 0.;
|
|
|
|
float64 _from_b = 0.;
|
|
|
|
float64 _from_a = 0.;
|
|
|
|
float64 _delta_r = 0.;
|
|
|
|
float64 _delta_g = 0.;
|
|
|
|
float64 _delta_b = 0.;
|
|
|
|
float64 _delta_a = 0.;
|
2016-09-26 12:09:59 +00:00
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void startManager();
|
|
|
|
void stopManager();
|
2016-06-24 10:37:29 +00:00
|
|
|
void registerClipManager(Media::Clip::Manager *manager);
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2015-12-08 12:33:37 +00:00
|
|
|
class Animation;
|
|
|
|
|
2015-12-28 21:20:04 +00:00
|
|
|
class AnimationImplementation {
|
2014-05-30 08:53:19 +00:00
|
|
|
public:
|
2015-12-28 21:20:04 +00:00
|
|
|
virtual void start() {}
|
2015-12-08 12:33:37 +00:00
|
|
|
virtual void step(Animation *a, uint64 ms, bool timer) = 0;
|
2015-12-28 21:20:04 +00:00
|
|
|
virtual ~AnimationImplementation() {}
|
2016-05-12 16:05:20 +00:00
|
|
|
|
2015-12-28 21:20:04 +00:00
|
|
|
};
|
2016-05-12 16:05:20 +00:00
|
|
|
|
2015-12-28 21:20:04 +00:00
|
|
|
class AnimationCallbacks {
|
|
|
|
public:
|
2016-05-12 16:05:20 +00:00
|
|
|
AnimationCallbacks(AnimationImplementation *implementation) : _implementation(implementation) {}
|
|
|
|
AnimationCallbacks(const AnimationCallbacks &other) = delete;
|
|
|
|
AnimationCallbacks &operator=(const AnimationCallbacks &other) = delete;
|
|
|
|
AnimationCallbacks(AnimationCallbacks &&other) : _implementation(other._implementation) {
|
|
|
|
other._implementation = nullptr;
|
|
|
|
}
|
|
|
|
AnimationCallbacks &operator=(AnimationCallbacks &&other) {
|
|
|
|
std::swap(_implementation, other._implementation);
|
2016-06-06 11:35:49 +00:00
|
|
|
return *this;
|
2016-05-12 16:05:20 +00:00
|
|
|
}
|
|
|
|
|
2015-12-28 21:20:04 +00:00
|
|
|
void start() { _implementation->start(); }
|
|
|
|
void step(Animation *a, uint64 ms, bool timer) { _implementation->step(a, ms, timer); }
|
|
|
|
~AnimationCallbacks() { deleteAndMark(_implementation); }
|
2016-05-12 16:05:20 +00:00
|
|
|
|
2015-12-28 21:20:04 +00:00
|
|
|
private:
|
|
|
|
AnimationImplementation *_implementation;
|
2016-05-12 16:05:20 +00:00
|
|
|
|
2015-12-08 12:33:37 +00:00
|
|
|
};
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2015-12-08 12:33:37 +00:00
|
|
|
class Animation {
|
|
|
|
public:
|
2016-05-12 16:05:20 +00:00
|
|
|
Animation(AnimationCallbacks &&callbacks)
|
|
|
|
: _callbacks(std_::move(callbacks))
|
|
|
|
, _animating(false) {
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2015-12-08 12:33:37 +00:00
|
|
|
void start();
|
|
|
|
void stop();
|
|
|
|
|
|
|
|
void step(uint64 ms, bool timer = false) {
|
2016-05-12 16:05:20 +00:00
|
|
|
_callbacks.step(this, ms, timer);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2015-12-08 12:33:37 +00:00
|
|
|
void step() {
|
|
|
|
step(getms(), false);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2015-12-08 12:33:37 +00:00
|
|
|
bool animating() const {
|
|
|
|
return _animating;
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2015-12-08 12:33:37 +00:00
|
|
|
~Animation() {
|
|
|
|
if (_animating) stop();
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2015-12-08 12:33:37 +00:00
|
|
|
private:
|
2016-05-12 16:05:20 +00:00
|
|
|
AnimationCallbacks _callbacks;
|
2015-12-08 12:33:37 +00:00
|
|
|
bool _animating;
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2015-05-29 18:52:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename Type>
|
2015-12-28 21:20:04 +00:00
|
|
|
class AnimationCallbacksRelative : public AnimationImplementation {
|
2015-05-29 18:52:43 +00:00
|
|
|
public:
|
2015-12-08 12:33:37 +00:00
|
|
|
typedef void (Type::*Method)(float64, bool);
|
2015-05-29 18:52:43 +00:00
|
|
|
|
2015-12-08 12:33:37 +00:00
|
|
|
AnimationCallbacksRelative(Type *obj, Method method) : _started(0), _obj(obj), _method(method) {
|
2015-05-29 18:52:43 +00:00
|
|
|
}
|
|
|
|
|
2015-12-08 12:33:37 +00:00
|
|
|
void start() {
|
|
|
|
_started = float64(getms());
|
2015-05-29 18:52:43 +00:00
|
|
|
}
|
2015-12-08 12:33:37 +00:00
|
|
|
|
|
|
|
void step(Animation *a, uint64 ms, bool timer) {
|
|
|
|
(_obj->*_method)(ms - _started, timer);
|
|
|
|
}
|
|
|
|
|
2015-05-29 18:52:43 +00:00
|
|
|
private:
|
2015-12-08 12:33:37 +00:00
|
|
|
float64 _started;
|
2015-05-29 18:52:43 +00:00
|
|
|
Type *_obj;
|
|
|
|
Method _method;
|
|
|
|
|
2015-12-08 12:33:37 +00:00
|
|
|
};
|
2015-05-29 18:52:43 +00:00
|
|
|
template <typename Type>
|
2016-05-12 16:05:20 +00:00
|
|
|
AnimationCallbacks animation(Type *obj, typename AnimationCallbacksRelative<Type>::Method method) {
|
|
|
|
return AnimationCallbacks(new AnimationCallbacksRelative<Type>(obj, method));
|
2015-05-29 18:52:43 +00:00
|
|
|
}
|
|
|
|
|
2015-12-08 12:33:37 +00:00
|
|
|
template <typename Type>
|
2015-12-28 21:20:04 +00:00
|
|
|
class AnimationCallbacksAbsolute : public AnimationImplementation {
|
2015-05-29 18:52:43 +00:00
|
|
|
public:
|
2015-12-08 12:33:37 +00:00
|
|
|
typedef void (Type::*Method)(uint64, bool);
|
2015-05-29 18:52:43 +00:00
|
|
|
|
2015-12-08 12:33:37 +00:00
|
|
|
AnimationCallbacksAbsolute(Type *obj, Method method) : _obj(obj), _method(method) {
|
2015-05-29 18:52:43 +00:00
|
|
|
}
|
|
|
|
|
2015-12-08 12:33:37 +00:00
|
|
|
void step(Animation *a, uint64 ms, bool timer) {
|
|
|
|
(_obj->*_method)(ms, timer);
|
2015-05-29 18:52:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-12-08 12:33:37 +00:00
|
|
|
Type *_obj;
|
|
|
|
Method _method;
|
2015-05-29 18:52:43 +00:00
|
|
|
|
|
|
|
};
|
2015-12-08 12:33:37 +00:00
|
|
|
template <typename Type>
|
2016-05-12 16:05:20 +00:00
|
|
|
AnimationCallbacks animation(Type *obj, typename AnimationCallbacksAbsolute<Type>::Method method) {
|
|
|
|
return AnimationCallbacks(new AnimationCallbacksAbsolute<Type>(obj, method));
|
2015-12-08 12:33:37 +00:00
|
|
|
}
|
2015-05-29 18:52:43 +00:00
|
|
|
|
2015-12-11 18:11:38 +00:00
|
|
|
template <typename Type, typename Param>
|
2015-12-28 21:20:04 +00:00
|
|
|
class AnimationCallbacksRelativeWithParam : public AnimationImplementation {
|
2015-12-11 18:11:38 +00:00
|
|
|
public:
|
|
|
|
typedef void (Type::*Method)(Param, float64, bool);
|
|
|
|
|
|
|
|
AnimationCallbacksRelativeWithParam(Param param, Type *obj, Method method) : _started(0), _param(param), _obj(obj), _method(method) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void start() {
|
|
|
|
_started = float64(getms());
|
|
|
|
}
|
|
|
|
|
|
|
|
void step(Animation *a, uint64 ms, bool timer) {
|
|
|
|
(_obj->*_method)(_param, ms - _started, timer);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
float64 _started;
|
|
|
|
Param _param;
|
|
|
|
Type *_obj;
|
|
|
|
Method _method;
|
|
|
|
|
|
|
|
};
|
|
|
|
template <typename Type, typename Param>
|
2016-05-12 16:05:20 +00:00
|
|
|
AnimationCallbacks animation(Param param, Type *obj, typename AnimationCallbacksRelativeWithParam<Type, Param>::Method method) {
|
|
|
|
return AnimationCallbacks(new AnimationCallbacksRelativeWithParam<Type, Param>(param, obj, method));
|
2015-12-11 18:11:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Type, typename Param>
|
2015-12-28 21:20:04 +00:00
|
|
|
class AnimationCallbacksAbsoluteWithParam : public AnimationImplementation {
|
2015-12-11 18:11:38 +00:00
|
|
|
public:
|
|
|
|
typedef void (Type::*Method)(Param, uint64, bool);
|
|
|
|
|
|
|
|
AnimationCallbacksAbsoluteWithParam(Param param, Type *obj, Method method) : _param(param), _obj(obj), _method(method) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void step(Animation *a, uint64 ms, bool timer) {
|
|
|
|
(_obj->*_method)(_param, ms, timer);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Param _param;
|
|
|
|
Type *_obj;
|
|
|
|
Method _method;
|
|
|
|
|
|
|
|
};
|
|
|
|
template <typename Type, typename Param>
|
2016-05-12 16:05:20 +00:00
|
|
|
AnimationCallbacks animation(Param param, Type *obj, typename AnimationCallbacksAbsoluteWithParam<Type, Param>::Method method) {
|
|
|
|
return AnimationCallbacks(new AnimationCallbacksAbsoluteWithParam<Type, Param>(param, obj, method));
|
2015-12-11 18:11:38 +00:00
|
|
|
}
|
|
|
|
|
2015-12-27 21:37:48 +00:00
|
|
|
template <typename AnimType>
|
|
|
|
class SimpleAnimation {
|
2014-05-30 08:53:19 +00:00
|
|
|
public:
|
2016-09-26 12:09:59 +00:00
|
|
|
using ValueType = typename AnimType::ValueType;
|
|
|
|
using Callback = base::lambda_unique<void()>;
|
2015-12-27 21:37:48 +00:00
|
|
|
|
2016-09-26 12:09:59 +00:00
|
|
|
bool animating() const {
|
|
|
|
if (_data) {
|
|
|
|
if (_data->a_animation.animating()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
_data.reset();
|
|
|
|
}
|
|
|
|
return false;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2015-12-27 21:37:48 +00:00
|
|
|
bool animating(uint64 ms) {
|
2016-09-26 12:09:59 +00:00
|
|
|
if (animating()) {
|
|
|
|
_data->a_animation.step(ms);
|
|
|
|
return animating();
|
2015-04-19 19:01:45 +00:00
|
|
|
}
|
2015-12-27 21:37:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-09-26 12:09:59 +00:00
|
|
|
ValueType current() const {
|
|
|
|
t_assert(_data != nullptr);
|
|
|
|
return _data->value.current();
|
2015-12-27 21:37:48 +00:00
|
|
|
}
|
2016-09-26 12:09:59 +00:00
|
|
|
ValueType current(const ValueType &def) const {
|
|
|
|
return _data ? current() : def;
|
2016-05-19 12:03:51 +00:00
|
|
|
}
|
2016-09-26 12:09:59 +00:00
|
|
|
ValueType current(uint64 ms, const ValueType &def) {
|
2015-12-27 21:37:48 +00:00
|
|
|
return animating(ms) ? current() : def;
|
|
|
|
}
|
|
|
|
|
2016-09-26 12:09:59 +00:00
|
|
|
template <typename Lambda>
|
|
|
|
void start(Lambda &&updateCallback, const ValueType &from, const ValueType &to, float64 duration, anim::transition transition = anim::linear) {
|
2015-12-27 21:37:48 +00:00
|
|
|
if (!_data) {
|
2016-09-26 12:09:59 +00:00
|
|
|
_data = std_::make_unique<Data>(from, std_::move(updateCallback));
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2016-09-26 12:09:59 +00:00
|
|
|
_data->value.start(to);
|
|
|
|
_data->duration = duration;
|
|
|
|
_data->transition = transition;
|
|
|
|
_data->a_animation.start();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-09-26 12:09:59 +00:00
|
|
|
void finish() {
|
2015-12-27 21:37:48 +00:00
|
|
|
if (_data) {
|
2016-09-26 12:09:59 +00:00
|
|
|
_data->value.finish();
|
|
|
|
_data->a_animation.stop();
|
|
|
|
_data.reset();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2015-12-27 21:37:48 +00:00
|
|
|
}
|
2015-12-08 12:33:37 +00:00
|
|
|
|
2015-12-27 21:37:48 +00:00
|
|
|
private:
|
2016-05-12 16:05:20 +00:00
|
|
|
struct Data {
|
2016-09-26 12:09:59 +00:00
|
|
|
Data(const ValueType &from, Callback &&updateCallback)
|
|
|
|
: value(from, from)
|
|
|
|
, a_animation(animation(this, &Data::step))
|
|
|
|
, updateCallback(std_::move(updateCallback)) {
|
|
|
|
}
|
|
|
|
void step(float64 ms, bool timer) {
|
|
|
|
auto dt = (ms >= duration) ? 1. : (ms / duration);
|
|
|
|
if (dt >= 1) {
|
|
|
|
value.finish();
|
|
|
|
a_animation.stop();
|
|
|
|
} else {
|
|
|
|
value.update(dt, transition);
|
|
|
|
}
|
|
|
|
updateCallback();
|
|
|
|
}
|
|
|
|
|
|
|
|
AnimType value;
|
|
|
|
Animation a_animation;
|
|
|
|
Callback updateCallback;
|
|
|
|
float64 duration = 0.;
|
|
|
|
anim::transition transition = anim::linear;
|
2016-05-12 16:05:20 +00:00
|
|
|
};
|
2016-09-26 12:09:59 +00:00
|
|
|
mutable std_::unique_ptr<Data> _data;
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2015-12-27 21:37:48 +00:00
|
|
|
};
|
2015-12-15 14:50:51 +00:00
|
|
|
|
2016-05-19 12:03:51 +00:00
|
|
|
using FloatAnimation = SimpleAnimation<anim::fvalue>;
|
|
|
|
using IntAnimation = SimpleAnimation<anim::ivalue>;
|
|
|
|
using ColorAnimation = SimpleAnimation<anim::cvalue>;
|
2015-12-27 21:37:48 +00:00
|
|
|
|
|
|
|
class AnimationManager : public QObject {
|
2016-09-26 12:09:59 +00:00
|
|
|
Q_OBJECT
|
2015-12-27 21:37:48 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
AnimationManager();
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2015-12-27 21:37:48 +00:00
|
|
|
void start(Animation *obj);
|
|
|
|
void stop(Animation *obj);
|
|
|
|
|
|
|
|
public slots:
|
|
|
|
void timeout();
|
|
|
|
|
2016-06-24 10:37:29 +00:00
|
|
|
void clipCallback(Media::Clip::Reader *reader, qint32 threadIndex, qint32 notification);
|
2015-12-27 21:37:48 +00:00
|
|
|
|
|
|
|
private:
|
2016-09-26 12:09:59 +00:00
|
|
|
using AnimatingObjects = OrderedSet<Animation*>;
|
2015-12-08 12:33:37 +00:00
|
|
|
AnimatingObjects _objects, _starting, _stopping;
|
|
|
|
QTimer _timer;
|
|
|
|
bool _iterating;
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
};
|