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
175
176
177
178
179
180
181
182
183
184
|
#pragma once
#include <array>
#include <cstddef>
#include "../../helpers/AnimatedVariable.hpp"
namespace Desktop::Types {
template <typename T>
struct SAddOperation {
static constexpr T identity() {
return T{};
}
static T apply(const T& lhs, const T& rhs) {
return lhs + rhs;
}
};
template <typename T>
struct SMultiplyOperation {
static constexpr T identity() {
return T{1};
}
static T apply(const T& lhs, const T& rhs) {
return lhs * rhs;
}
};
template <Animable VarType, typename Key, size_t Count, typename Operation = SMultiplyOperation<VarType>>
class CMultiAnimatedVariableContainer {
public:
using value_type = VarType;
using key_type = Key;
using operation_type = Operation;
static constexpr size_t size() {
return Count;
}
PHLANIMVAR<VarType>& get(const Key key) {
return m_vars.at(index(key));
}
const PHLANIMVAR<VarType>& get(const Key key) const {
return m_vars.at(index(key));
}
PHLANIMVAR<VarType>& operator[](const Key key) {
return get(key);
}
const PHLANIMVAR<VarType>& operator[](const Key key) const {
return get(key);
}
PHLANIMVAR<VarType>& raw(const size_t i) {
return m_vars.at(i);
}
const PHLANIMVAR<VarType>& raw(const size_t i) const {
return m_vars.at(i);
}
std::array<PHLANIMVAR<VarType>, Count>& all() {
return m_vars;
}
const std::array<PHLANIMVAR<VarType>, Count>& all() const {
return m_vars;
}
template <typename Fn>
void forEach(Fn&& fn) {
for (size_t i = 0; i < Count; ++i)
fn(static_cast<Key>(i), m_vars.at(i));
}
template <typename Fn>
void forEach(Fn&& fn) const {
for (size_t i = 0; i < Count; ++i)
fn(static_cast<Key>(i), m_vars.at(i));
}
VarType getTotal() const {
return accumulate([](const auto& var) { return var->value(); });
}
VarType getTotalGoal() const {
return accumulate([](const auto& var) { return var->goal(); });
}
VarType getTotalBegun() const {
return accumulate([](const auto& var) { return var->begun(); });
}
VarType getTotalWithout(const Key key) const {
return accumulateExcept(key, [](const auto& var) { return var->value(); });
}
VarType getTotalGoalWithout(const Key key) const {
return accumulateExcept(key, [](const auto& var) { return var->goal(); });
}
VarType getTotalBegunWithout(const Key key) const {
return accumulateExcept(key, [](const auto& var) { return var->begun(); });
}
VarType value() const {
return getTotal();
}
VarType goal() const {
return getTotalGoal();
}
bool isBeingAnimated() const {
for (const auto& var : m_vars) {
if (var && var->isBeingAnimated())
return true;
}
return false;
}
bool initialized() const {
for (const auto& var : m_vars) {
if (!var)
return false;
}
return true;
}
void warp(const bool endCallback = true) {
for (auto& var : m_vars) {
if (var)
var->warp(endCallback);
}
}
private:
static constexpr size_t index(const Key key) {
return static_cast<size_t>(key);
}
template <typename Getter>
VarType accumulate(Getter&& getter) const {
VarType result = Operation::identity();
for (const auto& var : m_vars) {
if (!var)
continue;
result = Operation::apply(result, getter(var));
}
return result;
}
template <typename Getter>
VarType accumulateExcept(const Key key, Getter&& getter) const {
VarType result = Operation::identity();
const size_t except = index(key);
for (size_t i = 0; i < Count; ++i) {
if (i == except || !m_vars.at(i))
continue;
result = Operation::apply(result, getter(m_vars.at(i)));
}
return result;
}
std::array<PHLANIMVAR<VarType>, Count> m_vars;
};
template <Animable VarType, typename Key, size_t Count, typename Operation = SMultiplyOperation<VarType>>
using CMultiAVarContainer = CMultiAnimatedVariableContainer<VarType, Key, Count, Operation>;
}
|