Hi All,
My team has been using the timeline class quite a lot across many of our projects and one thing folks often still stumble over is conversion between ci::Anim<T>
and T
.
One solution we’ve landed on for convenience recently has been to write a big old list of templated operators that take various combinations of ci::Anim<T>
, T
or U
(U
for operations between types like ci::Anim<vec2>
and float
).
Here’s a snapshot of what we currently have: https://gist.github.com/benjaminbojko/72a24102400cc09c812024ad443f9740
With that, for…
ci::Anim<vec2> size;
ci::Anim<vec2> pos;
vec2 otherPos;
… we can write…
vec2 scaledSize = size * 0.5f;
vec2 stretchedSize = size * vec2(2.f, 1.f);
vec2 normalizedSize = size / vec2(1920.f, 1080.f);
vec2 bottomRight = pos + size;
otherPos += size;
…instead of…
vec2 scaledSize = size.value() * 0.5f;
vec2 stretchedSize = size.value() * vec2(2.f, 1.f);
vec2 normalizedSize = size.value() / vec2(1920.f, 1080.f);
vec2 bottomRight = pos.value() + size.value();
otherPos += size.value();
Two caveats are that accessing properties of the value still requires calling value()
or ()
…
float width = size().value().x;
float height = size()().y;
…and assignments only work one way (maybe there’s a way around this?)
// works
vec2 bottomRight = pos;
bottomRight += size;
// breaks
size += vec2(100.f);
Honestly, it still doesn’t really feel quite kosher to me personally, but it does make dealing with animations much more convenient. What do folks here think? Is this risky because it might lead to unexpected implicit conversions? Is it just plain old messy? Is it helpful?
Happy to turn this gist into something more formal/reusable like a PR, but would love to hear some of your thoughts first.