1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
|
#include "duration.hpp"
#include "obs-module-helper.hpp"
#include <sstream>
#include <iomanip>
namespace advss {
Duration::Duration(double initialValueInSeconds) : _value(initialValueInSeconds)
{
}
void Duration::Save(obs_data_t *obj, const char *name) const
{
auto data = obs_data_create();
_value.Save(data, "value");
obs_data_set_int(data, "unit", static_cast<int>(_unit));
obs_data_set_int(data, "version", 1);
obs_data_set_obj(obj, name, data);
obs_data_release(data);
}
static int durationUnitToMultiplier(Duration::Unit u);
void Duration::Load(obs_data_t *obj, const char *name)
{
auto data = obs_data_get_obj(obj, name);
// TODO: remove this fallback
if (!data || !obs_data_has_user_value(data, "version") ||
obs_data_get_int(data, "version") != 1) {
_value = 0.;
bool usingDefaultArgs = strcmp("duration", name) == 0;
if (usingDefaultArgs) {
_value = obs_data_get_double(obj, "seconds");
}
if (_value.GetValue() == 0.0) {
_value = obs_data_get_double(obj, name);
}
if (usingDefaultArgs) {
_unit = static_cast<Duration::Unit>(
obs_data_get_int(obj, "displayUnit"));
} else if (_value.GetValue() >= 86400) {
_unit = (_value.GetValue() / 60 >= 86400)
? Duration::Unit::HOURS
: Duration::Unit::MINUTES;
} else {
_unit = Duration::Unit::SECONDS;
}
_value = _value / durationUnitToMultiplier(_unit);
obs_data_release(data);
return;
}
_value.Load(data, "value");
_unit = static_cast<Duration::Unit>(obs_data_get_int(data, "unit"));
obs_data_release(data);
}
bool Duration::DurationReached()
{
if (IsReset()) {
_startTime = std::chrono::high_resolution_clock::now();
}
auto runTime = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::high_resolution_clock::now() - _startTime);
return runTime.count() >= Milliseconds();
}
bool Duration::IsReset() const
{
return _startTime.time_since_epoch().count() == 0;
}
double Duration::Seconds() const
{
return _value.GetValue() * durationUnitToMultiplier(_unit);
}
double Duration::Milliseconds() const
{
return Seconds() * 1000.0;
}
double Duration::TimeRemaining() const
{
if (IsReset()) {
return Seconds();
}
auto runTime = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::high_resolution_clock::now() - _startTime);
if (runTime.count() >= Milliseconds()) {
return 0;
}
return (Milliseconds() - runTime.count()) / 1000.0;
}
void Duration::SetTimeRemaining(double remaining)
{
long long msPassed = (long long)((Seconds() - remaining) * 1000);
_startTime = std::chrono::high_resolution_clock::now() -
std::chrono::milliseconds(msPassed);
}
void Duration::Reset()
{
_startTime = {};
}
static std::string durationUnitToString(Duration::Unit u)
{
switch (u) {
case Duration::Unit::SECONDS:
return obs_module_text("AdvSceneSwitcher.unit.seconds");
case Duration::Unit::MINUTES:
return obs_module_text("AdvSceneSwitcher.unit.minutes");
case Duration::Unit::HOURS:
return obs_module_text("AdvSceneSwitcher.unit.hours");
default:
break;
}
return "";
}
std::string Duration::ToString() const
{
std::ostringstream ss;
ss << std::fixed << std::setprecision(2) << _value.GetValue() << " "
<< durationUnitToString(_unit);
if (!_value.IsFixedType()) {
ss << " [" << GetWeakVariableName(_value.GetVariable()) << "]";
}
return ss.str();
}
// TODO: Remove the code below
// Only used for backwards compatibility
static int durationUnitToMultiplier(Duration::Unit u)
{
switch (u) {
case Duration::Unit::SECONDS:
return 1;
case Duration::Unit::MINUTES:
return 60;
case Duration::Unit::HOURS:
return 3600;
default:
break;
}
return 0;
}
void Duration::SetUnit(Unit u)
{
double prevMultiplier = durationUnitToMultiplier(_unit);
double newMultiplier = durationUnitToMultiplier(u);
_unit = u;
_value = _value * (prevMultiplier / newMultiplier);
}
void Duration::ResolveVariables()
{
_value.ResolveVariables();
}
} // namespace advss
|