2014-05-30 12:53:19 +04:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2014-12-01 13:47:38 +03:00
|
|
|
the official desktop version of Telegram messaging app, see https://telegram.org
|
2014-05-30 12:53:19 +04: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 16:16:42 +03: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 12:53:19 +04:00
|
|
|
Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE
|
2016-02-08 13:56:18 +03:00
|
|
|
Copyright (c) 2014-2016 John Preston, https://desktop.telegram.org
|
2014-05-30 12:53:19 +04:00
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
2016-04-13 00:31:28 +03:00
|
|
|
#include "core/basic_types.h"
|
2014-05-30 12:53:19 +04:00
|
|
|
#include <QtCore/QTimer>
|
|
|
|
#include <QtGui/QColor>
|
|
|
|
|
2016-06-24 13:37:29 +03:00
|
|
|
namespace Media {
|
|
|
|
namespace Clip {
|
|
|
|
|
|
|
|
class Reader;
|
|
|
|
static Reader * const BadReader = SharedMemoryLocation<Reader, 0>();
|
|
|
|
|
|
|
|
class Manager;
|
|
|
|
|
|
|
|
enum Notification {
|
|
|
|
NotificationReinit,
|
|
|
|
NotificationRepaint,
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Clip
|
|
|
|
} // namespace Media
|
|
|
|
|
2014-05-30 12:53:19 +04:00
|
|
|
namespace anim {
|
|
|
|
|
|
|
|
typedef float64 (*transition)(const float64 &delta, const float64 &dt);
|
2016-01-01 04:07:41 +08:00
|
|
|
|
2014-05-30 12:53:19 +04: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:
|
|
|
|
|
|
|
|
fvalue() {
|
|
|
|
}
|
|
|
|
fvalue(const float64 &from) : _cur(from), _from(from), _delta(0) {
|
|
|
|
}
|
|
|
|
fvalue(const float64 &from, const float64 &to) : _cur(from), _from(from), _delta(to - from) {
|
|
|
|
}
|
|
|
|
void start(const float64 &to) {
|
|
|
|
_from = _cur;
|
|
|
|
_delta = to - _from;
|
|
|
|
}
|
|
|
|
void restart() {
|
|
|
|
_delta = _from + _delta - _cur;
|
|
|
|
_from = _cur;
|
|
|
|
}
|
|
|
|
const float64 ¤t() const {
|
|
|
|
return _cur;
|
|
|
|
}
|
2014-10-17 16:57:14 +04:00
|
|
|
float64 to() const {
|
|
|
|
return _from + _delta;
|
|
|
|
}
|
2014-08-11 13:03:45 +04:00
|
|
|
fvalue &update(const float64 &dt, transition func) {
|
2014-05-30 12:53:19 +04:00
|
|
|
_cur = _from + (*func)(_delta, dt);
|
2014-08-11 13:03:45 +04:00
|
|
|
return *this;
|
2014-05-30 12:53:19 +04:00
|
|
|
}
|
|
|
|
void finish() {
|
|
|
|
_cur = _from + _delta;
|
|
|
|
_from = _cur;
|
|
|
|
_delta = 0;
|
|
|
|
}
|
|
|
|
|
2015-12-28 00:37:48 +03:00
|
|
|
typedef float64 Type;
|
|
|
|
|
2014-05-30 12:53:19 +04:00
|
|
|
private:
|
|
|
|
|
|
|
|
float64 _cur, _from, _delta;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ivalue { // int animated value
|
|
|
|
public:
|
|
|
|
|
|
|
|
ivalue() {
|
|
|
|
}
|
|
|
|
ivalue(int32 from) : _cur(from), _from(float64(from)), _delta(0) {
|
|
|
|
}
|
|
|
|
ivalue(int32 from, int32 to) : _cur(from), _from(float64(from)), _delta(float64(to - from)) {
|
|
|
|
}
|
|
|
|
void start(int32 to) {
|
|
|
|
_from = float64(_cur);
|
|
|
|
_delta = float64(to) - _from;
|
|
|
|
}
|
|
|
|
void restart() {
|
|
|
|
_delta = _from + _delta - float64(_cur);
|
|
|
|
_from = float64(_cur);
|
|
|
|
}
|
|
|
|
int32 current() const {
|
|
|
|
return _cur;
|
|
|
|
}
|
2014-10-17 16:57:14 +04:00
|
|
|
int32 to() const {
|
|
|
|
return _from + _delta;
|
|
|
|
}
|
2014-08-11 13:03:45 +04:00
|
|
|
ivalue &update(const float64 &dt, transition func) {
|
2014-05-30 12:53:19 +04:00
|
|
|
_cur = qRound(_from + (*func)(_delta, dt));
|
2014-08-11 13:03:45 +04:00
|
|
|
return *this;
|
2014-05-30 12:53:19 +04:00
|
|
|
}
|
|
|
|
void finish() {
|
|
|
|
_cur = qRound(_from + _delta);
|
|
|
|
_from = _cur;
|
|
|
|
_delta = 0;
|
|
|
|
}
|
|
|
|
|
2015-12-28 00:37:48 +03:00
|
|
|
typedef int32 Type;
|
|
|
|
|
2014-05-30 12:53:19 +04:00
|
|
|
private:
|
|
|
|
|
|
|
|
int32 _cur;
|
|
|
|
float64 _from, _delta;
|
|
|
|
};
|
|
|
|
|
|
|
|
class cvalue { // QColor animated value
|
|
|
|
public:
|
|
|
|
|
|
|
|
cvalue() {
|
|
|
|
}
|
|
|
|
cvalue(const QColor &from) : _cur(from), _from_r(from.redF()), _from_g(from.greenF()), _from_b(from.blueF()), _from_a(from.alphaF()), _delta_r(0), _delta_g(0), _delta_b(0), _delta_a(0) {
|
|
|
|
}
|
|
|
|
cvalue(const QColor &from, const QColor &to)
|
|
|
|
: _cur(from)
|
|
|
|
, _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())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
const QColor ¤t() const {
|
|
|
|
return _cur;
|
|
|
|
}
|
2014-10-17 16:57:14 +04: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 13:03:45 +04:00
|
|
|
cvalue &update(const float64 &dt, transition func) {
|
2014-05-30 12:53:19 +04: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 13:03:45 +04:00
|
|
|
return *this;
|
2014-05-30 12:53:19 +04: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;
|
|
|
|
}
|
|
|
|
|
2015-12-28 00:37:48 +03:00
|
|
|
typedef QColor Type;
|
|
|
|
|
2014-05-30 12:53:19 +04:00
|
|
|
private:
|
|
|
|
|
|
|
|
QColor _cur;
|
|
|
|
float64 _from_r, _from_g, _from_b, _from_a, _delta_r, _delta_g, _delta_b, _delta_a;
|
|
|
|
};
|
|
|
|
|
|
|
|
void startManager();
|
|
|
|
void stopManager();
|
2016-06-24 13:37:29 +03:00
|
|
|
void registerClipManager(Media::Clip::Manager *manager);
|
2014-05-30 12:53:19 +04:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2015-12-08 15:33:37 +03:00
|
|
|
class Animation;
|
|
|
|
|
2015-12-29 00:20:04 +03:00
|
|
|
class AnimationImplementation {
|
2014-05-30 12:53:19 +04:00
|
|
|
public:
|
2015-12-29 00:20:04 +03:00
|
|
|
virtual void start() {}
|
2015-12-08 15:33:37 +03:00
|
|
|
virtual void step(Animation *a, uint64 ms, bool timer) = 0;
|
2015-12-29 00:20:04 +03:00
|
|
|
virtual ~AnimationImplementation() {}
|
2016-05-12 19:05:20 +03:00
|
|
|
|
2015-12-29 00:20:04 +03:00
|
|
|
};
|
2016-05-12 19:05:20 +03:00
|
|
|
|
2015-12-29 00:20:04 +03:00
|
|
|
class AnimationCallbacks {
|
|
|
|
public:
|
2016-05-12 19:05:20 +03: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 14:35:49 +03:00
|
|
|
return *this;
|
2016-05-12 19:05:20 +03:00
|
|
|
}
|
|
|
|
|
2015-12-29 00:20:04 +03:00
|
|
|
void start() { _implementation->start(); }
|
|
|
|
void step(Animation *a, uint64 ms, bool timer) { _implementation->step(a, ms, timer); }
|
|
|
|
~AnimationCallbacks() { deleteAndMark(_implementation); }
|
2016-05-12 19:05:20 +03:00
|
|
|
|
2015-12-29 00:20:04 +03:00
|
|
|
private:
|
|
|
|
AnimationImplementation *_implementation;
|
2016-05-12 19:05:20 +03:00
|
|
|
|
2015-12-08 15:33:37 +03:00
|
|
|
};
|
2014-05-30 12:53:19 +04:00
|
|
|
|
2015-12-08 15:33:37 +03:00
|
|
|
class Animation {
|
|
|
|
public:
|
2016-05-12 19:05:20 +03:00
|
|
|
Animation(AnimationCallbacks &&callbacks)
|
|
|
|
: _callbacks(std_::move(callbacks))
|
|
|
|
, _animating(false) {
|
2014-05-30 12:53:19 +04:00
|
|
|
}
|
|
|
|
|
2015-12-08 15:33:37 +03:00
|
|
|
void start();
|
|
|
|
void stop();
|
|
|
|
|
|
|
|
void step(uint64 ms, bool timer = false) {
|
2016-05-12 19:05:20 +03:00
|
|
|
_callbacks.step(this, ms, timer);
|
2014-05-30 12:53:19 +04:00
|
|
|
}
|
|
|
|
|
2015-12-08 15:33:37 +03:00
|
|
|
void step() {
|
|
|
|
step(getms(), false);
|
2014-05-30 12:53:19 +04:00
|
|
|
}
|
|
|
|
|
2015-12-08 15:33:37 +03:00
|
|
|
bool animating() const {
|
|
|
|
return _animating;
|
|
|
|
}
|
2014-05-30 12:53:19 +04:00
|
|
|
|
2015-12-08 15:33:37 +03:00
|
|
|
~Animation() {
|
|
|
|
if (_animating) stop();
|
|
|
|
}
|
2014-05-30 12:53:19 +04:00
|
|
|
|
2015-12-08 15:33:37 +03:00
|
|
|
private:
|
2016-05-12 19:05:20 +03:00
|
|
|
AnimationCallbacks _callbacks;
|
2015-12-08 15:33:37 +03:00
|
|
|
bool _animating;
|
2014-05-30 12:53:19 +04:00
|
|
|
|
2015-05-29 21:52:43 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename Type>
|
2015-12-29 00:20:04 +03:00
|
|
|
class AnimationCallbacksRelative : public AnimationImplementation {
|
2015-05-29 21:52:43 +03:00
|
|
|
public:
|
2015-12-08 15:33:37 +03:00
|
|
|
typedef void (Type::*Method)(float64, bool);
|
2015-05-29 21:52:43 +03:00
|
|
|
|
2015-12-08 15:33:37 +03:00
|
|
|
AnimationCallbacksRelative(Type *obj, Method method) : _started(0), _obj(obj), _method(method) {
|
2015-05-29 21:52:43 +03:00
|
|
|
}
|
|
|
|
|
2015-12-08 15:33:37 +03:00
|
|
|
void start() {
|
|
|
|
_started = float64(getms());
|
2015-05-29 21:52:43 +03:00
|
|
|
}
|
2015-12-08 15:33:37 +03:00
|
|
|
|
|
|
|
void step(Animation *a, uint64 ms, bool timer) {
|
|
|
|
(_obj->*_method)(ms - _started, timer);
|
|
|
|
}
|
|
|
|
|
2015-05-29 21:52:43 +03:00
|
|
|
private:
|
2015-12-08 15:33:37 +03:00
|
|
|
float64 _started;
|
2015-05-29 21:52:43 +03:00
|
|
|
Type *_obj;
|
|
|
|
Method _method;
|
|
|
|
|
2015-12-08 15:33:37 +03:00
|
|
|
};
|
2015-05-29 21:52:43 +03:00
|
|
|
template <typename Type>
|
2016-05-12 19:05:20 +03:00
|
|
|
AnimationCallbacks animation(Type *obj, typename AnimationCallbacksRelative<Type>::Method method) {
|
|
|
|
return AnimationCallbacks(new AnimationCallbacksRelative<Type>(obj, method));
|
2015-05-29 21:52:43 +03:00
|
|
|
}
|
|
|
|
|
2015-12-08 15:33:37 +03:00
|
|
|
template <typename Type>
|
2015-12-29 00:20:04 +03:00
|
|
|
class AnimationCallbacksAbsolute : public AnimationImplementation {
|
2015-05-29 21:52:43 +03:00
|
|
|
public:
|
2015-12-08 15:33:37 +03:00
|
|
|
typedef void (Type::*Method)(uint64, bool);
|
2015-05-29 21:52:43 +03:00
|
|
|
|
2015-12-08 15:33:37 +03:00
|
|
|
AnimationCallbacksAbsolute(Type *obj, Method method) : _obj(obj), _method(method) {
|
2015-05-29 21:52:43 +03:00
|
|
|
}
|
|
|
|
|
2015-12-08 15:33:37 +03:00
|
|
|
void step(Animation *a, uint64 ms, bool timer) {
|
|
|
|
(_obj->*_method)(ms, timer);
|
2015-05-29 21:52:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-12-08 15:33:37 +03:00
|
|
|
Type *_obj;
|
|
|
|
Method _method;
|
2015-05-29 21:52:43 +03:00
|
|
|
|
|
|
|
};
|
2015-12-08 15:33:37 +03:00
|
|
|
template <typename Type>
|
2016-05-12 19:05:20 +03:00
|
|
|
AnimationCallbacks animation(Type *obj, typename AnimationCallbacksAbsolute<Type>::Method method) {
|
|
|
|
return AnimationCallbacks(new AnimationCallbacksAbsolute<Type>(obj, method));
|
2015-12-08 15:33:37 +03:00
|
|
|
}
|
2015-05-29 21:52:43 +03:00
|
|
|
|
2015-12-11 21:11:38 +03:00
|
|
|
template <typename Type, typename Param>
|
2015-12-29 00:20:04 +03:00
|
|
|
class AnimationCallbacksRelativeWithParam : public AnimationImplementation {
|
2015-12-11 21:11:38 +03: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 19:05:20 +03: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 21:11:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Type, typename Param>
|
2015-12-29 00:20:04 +03:00
|
|
|
class AnimationCallbacksAbsoluteWithParam : public AnimationImplementation {
|
2015-12-11 21:11:38 +03: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 19:05:20 +03: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 21:11:38 +03:00
|
|
|
}
|
|
|
|
|
2015-12-28 00:37:48 +03:00
|
|
|
template <typename AnimType>
|
|
|
|
class SimpleAnimation {
|
2014-05-30 12:53:19 +04:00
|
|
|
public:
|
2016-05-12 19:05:20 +03:00
|
|
|
using Callback = Function<void>;
|
2015-12-28 00:37:48 +03:00
|
|
|
|
2016-05-19 15:03:51 +03:00
|
|
|
SimpleAnimation() {
|
2014-05-30 12:53:19 +04:00
|
|
|
}
|
|
|
|
|
2015-12-28 00:37:48 +03:00
|
|
|
bool animating(uint64 ms) {
|
|
|
|
if (_data && _data->_a.animating()) {
|
|
|
|
_data->_a.step(ms);
|
|
|
|
return _data && _data->_a.animating();
|
2015-04-19 22:01:45 +03:00
|
|
|
}
|
2015-12-28 00:37:48 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-05-19 15:03:51 +03:00
|
|
|
bool isNull() const {
|
2015-12-28 00:37:48 +03:00
|
|
|
return !_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
typename AnimType::Type current() {
|
2015-12-28 01:49:08 +03:00
|
|
|
return _data ? _data->a.current() : typename AnimType::Type();
|
2015-04-19 22:01:45 +03:00
|
|
|
}
|
|
|
|
|
2016-05-19 15:03:51 +03:00
|
|
|
typename AnimType::Type current(const typename AnimType::Type &def) {
|
|
|
|
return _data ? _data->a.current() : def;
|
|
|
|
}
|
|
|
|
|
2015-12-28 00:37:48 +03:00
|
|
|
typename AnimType::Type current(uint64 ms, const typename AnimType::Type &def) {
|
|
|
|
return animating(ms) ? current() : def;
|
|
|
|
}
|
|
|
|
|
2016-05-12 19:05:20 +03:00
|
|
|
void setup(const typename AnimType::Type &from, Callback &&update) {
|
2015-12-28 00:37:48 +03:00
|
|
|
if (!_data) {
|
2016-05-12 19:05:20 +03:00
|
|
|
_data = new Data(from, std_::move(update), animation(this, &SimpleAnimation<AnimType>::step));
|
2014-05-30 12:53:19 +04:00
|
|
|
} else {
|
2015-12-28 00:37:48 +03:00
|
|
|
_data->a = AnimType(from, from);
|
2014-05-30 12:53:19 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-28 00:37:48 +03:00
|
|
|
void start(const typename AnimType::Type &to, float64 duration, anim::transition transition = anim::linear) {
|
|
|
|
if (_data) {
|
|
|
|
_data->a.start(to);
|
|
|
|
_data->_a.start();
|
|
|
|
_data->duration = duration;
|
|
|
|
_data->transition = transition;
|
2014-05-30 12:53:19 +04:00
|
|
|
}
|
2015-12-28 00:37:48 +03:00
|
|
|
}
|
2015-12-08 15:33:37 +03:00
|
|
|
|
2016-05-19 15:03:51 +03:00
|
|
|
void finish() {
|
|
|
|
if (isNull()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_data->a.finish();
|
|
|
|
_data->_a.stop();
|
|
|
|
delete _data;
|
|
|
|
_data = nullptr;
|
|
|
|
}
|
|
|
|
|
2015-12-28 00:37:48 +03:00
|
|
|
~SimpleAnimation() {
|
2015-12-29 00:20:04 +03:00
|
|
|
deleteAndMark(_data);
|
2015-12-28 00:37:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-05-12 19:05:20 +03:00
|
|
|
struct Data {
|
|
|
|
Data(const typename AnimType::Type &from, Callback &&update, AnimationCallbacks &&acb)
|
2015-12-28 00:37:48 +03:00
|
|
|
: a(from, from)
|
2016-05-12 19:05:20 +03:00
|
|
|
, _a(std_::move(acb))
|
|
|
|
, update(std_::move(update))
|
2015-12-28 00:37:48 +03:00
|
|
|
, duration(0)
|
|
|
|
, transition(anim::linear) {
|
|
|
|
}
|
|
|
|
AnimType a;
|
|
|
|
Animation _a;
|
2015-12-29 00:20:04 +03:00
|
|
|
Callback update;
|
2015-12-28 00:37:48 +03:00
|
|
|
float64 duration;
|
|
|
|
anim::transition transition;
|
2016-05-12 19:05:20 +03:00
|
|
|
};
|
2016-05-19 15:03:51 +03:00
|
|
|
Data *_data = nullptr;
|
2015-12-28 00:37:48 +03:00
|
|
|
|
|
|
|
void step(float64 ms, bool timer) {
|
|
|
|
float64 dt = (ms >= _data->duration) ? 1 : (ms / _data->duration);
|
|
|
|
if (dt >= 1) {
|
|
|
|
_data->a.finish();
|
|
|
|
_data->_a.stop();
|
|
|
|
} else {
|
|
|
|
_data->a.update(dt, _data->transition);
|
2014-05-30 12:53:19 +04:00
|
|
|
}
|
2016-06-17 21:18:01 +03:00
|
|
|
|
|
|
|
Callback callbackCache, *toCall = &_data->update;
|
2015-12-28 00:37:48 +03:00
|
|
|
if (!_data->_a.animating()) {
|
2016-06-17 21:18:01 +03:00
|
|
|
callbackCache = std_::move(_data->update);
|
|
|
|
toCall = &callbackCache;
|
|
|
|
|
2015-12-28 00:37:48 +03:00
|
|
|
delete _data;
|
2016-05-19 15:03:51 +03:00
|
|
|
_data = nullptr;
|
2014-05-30 12:53:19 +04:00
|
|
|
}
|
2016-06-17 21:18:01 +03:00
|
|
|
if (timer) {
|
|
|
|
toCall->call();
|
|
|
|
}
|
2014-05-30 12:53:19 +04:00
|
|
|
}
|
|
|
|
|
2015-12-28 00:37:48 +03:00
|
|
|
};
|
2015-12-15 17:50:51 +03:00
|
|
|
|
2016-05-19 15:03:51 +03:00
|
|
|
using FloatAnimation = SimpleAnimation<anim::fvalue>;
|
|
|
|
using IntAnimation = SimpleAnimation<anim::ivalue>;
|
|
|
|
using ColorAnimation = SimpleAnimation<anim::cvalue>;
|
2015-12-28 00:37:48 +03:00
|
|
|
|
2016-05-27 13:57:11 +03:00
|
|
|
// Macro allows us to lazily create updateCallback.
|
|
|
|
#define ENSURE_ANIMATION(animation, updateCallback, from) \
|
|
|
|
if ((animation).isNull()) { \
|
|
|
|
(animation).setup((from), (updateCallback)); \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define START_ANIMATION(animation, updateCallback, from, to, duration, transition) \
|
|
|
|
ENSURE_ANIMATION(animation, updateCallback, from); \
|
|
|
|
(animation).start((to), (duration), (transition))
|
2015-12-28 00:37:48 +03:00
|
|
|
|
|
|
|
class AnimationManager : public QObject {
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
AnimationManager();
|
2014-05-30 12:53:19 +04:00
|
|
|
|
2015-12-28 00:37:48 +03:00
|
|
|
void start(Animation *obj);
|
|
|
|
void stop(Animation *obj);
|
|
|
|
|
|
|
|
public slots:
|
|
|
|
void timeout();
|
|
|
|
|
2016-06-24 13:37:29 +03:00
|
|
|
void clipCallback(Media::Clip::Reader *reader, qint32 threadIndex, qint32 notification);
|
2015-12-28 00:37:48 +03:00
|
|
|
|
|
|
|
private:
|
2015-12-08 15:33:37 +03:00
|
|
|
typedef QMap<Animation*, NullType> AnimatingObjects;
|
|
|
|
AnimatingObjects _objects, _starting, _stopping;
|
|
|
|
QTimer _timer;
|
|
|
|
bool _iterating;
|
2014-05-30 12:53:19 +04:00
|
|
|
|
|
|
|
};
|