1
0
Fork 0
mirror of https://github.com/polybar/polybar.git synced 2024-11-11 13:50:56 -05:00
polybar/src/drawtypes/animation.cpp

70 lines
1.6 KiB
C++
Raw Normal View History

2016-11-02 15:22:45 -04:00
#include "drawtypes/animation.hpp"
#include "drawtypes/label.hpp"
2016-12-09 03:02:47 -05:00
#include "utils/factory.hpp"
2016-11-02 15:22:45 -04:00
2016-11-19 00:22:44 -05:00
POLYBAR_NS
2016-11-02 15:22:45 -04:00
namespace drawtypes {
void animation::add(icon_t&& frame) {
m_frames.emplace_back(forward<decltype(frame)>(frame));
m_framecount = m_frames.size();
}
icon_t animation::get() {
tick();
return m_frames[m_frame];
}
int animation::framerate() {
return m_framerate_ms;
}
animation::operator bool() {
return !m_frames.empty();
}
void animation::tick() {
auto now = chrono::system_clock::now();
auto diff = chrono::duration_cast<chrono::milliseconds>(now - m_lastupdate);
2016-11-25 07:55:15 -05:00
if (diff.count() < m_framerate_ms) {
2016-11-02 15:22:45 -04:00
return;
2016-11-25 07:55:15 -05:00
}
if (++m_frame >= m_framecount) {
2016-11-02 15:22:45 -04:00
m_frame = 0;
2016-11-25 07:55:15 -05:00
}
2016-11-02 15:22:45 -04:00
m_lastupdate = now;
}
/**
* Create an animation by loading values
* from the configuration
*/
2016-11-25 07:55:15 -05:00
animation_t load_animation(const config& conf, const string& section, string name, bool required) {
2016-11-02 15:22:45 -04:00
vector<icon_t> vec;
vector<string> frames;
name = string_util::ltrim(string_util::rtrim(name, '>'), '<');
auto anim_defaults = load_optional_icon(conf, section, name);
2016-11-25 07:55:15 -05:00
if (required) {
2016-11-02 15:22:45 -04:00
frames = conf.get_list<string>(section, name);
2016-11-25 07:55:15 -05:00
} else {
2016-11-02 15:22:45 -04:00
frames = conf.get_list<string>(section, name, {});
2016-11-25 07:55:15 -05:00
}
2016-11-02 15:22:45 -04:00
for (size_t i = 0; i < frames.size(); i++) {
vec.emplace_back(forward<icon_t>(load_optional_icon(conf, section, name + "-" + to_string(i), frames[i])));
vec.back()->copy_undefined(anim_defaults);
}
2016-11-02 15:22:45 -04:00
auto framerate = conf.get<int>(section, name + "-framerate", 1000);
return factory_util::shared<animation>(move(vec), framerate);
2016-11-02 15:22:45 -04:00
}
}
2016-11-19 00:22:44 -05:00
POLYBAR_NS_END