| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- /*
- * This source file is part of libRocket, the HTML/CSS Interface Middleware
- *
- * For the latest information, see http://www.librocket.com
- *
- * Copyright (c) 2018 Michael Ragazzon
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- *
- */
- #include "precompiled.h"
- #include "ElementAnimation.h"
- #include "../../Include/Rocket/Core/Element.h"
- #include "../../Include/Rocket/Core/TransformPrimitive.h"
- namespace Rocket {
- namespace Core {
- static Colourf ColourToLinearSpace(Colourb c)
- {
- Colourf result;
- // Approximate inverse sRGB function
- result.red = Math::SquareRoot((float)c.red / 255.f);
- result.green = Math::SquareRoot((float)c.green / 255.f);
- result.blue = Math::SquareRoot((float)c.blue / 255.f);
- result.alpha = (float)c.alpha / 255.f;
- return result;
- }
- static Colourb ColourFromLinearSpace(Colourf c)
- {
- Colourb result;
- result.red = (Rocket::Core::byte)Math::Clamp(c.red*c.red*255.f, 0.0f, 255.f);
- result.green = (Rocket::Core::byte)Math::Clamp(c.green*c.green*255.f, 0.0f, 255.f);
- result.blue = (Rocket::Core::byte)Math::Clamp(c.blue*c.blue*255.f, 0.0f, 255.f);
- result.alpha = (Rocket::Core::byte)Math::Clamp(c.alpha*255.f, 0.0f, 255.f);
- return result;
- }
- template<size_t N>
- static bool TryInterpolatePrimitive(Rocket::Core::Transforms::Primitive* p_inout, const Rocket::Core::Transforms::Primitive* p1, float alpha)
- {
- using namespace Rocket::Core::Transforms;
- bool result = false;
- if (auto values0 = dynamic_cast<UnresolvedValuesPrimitive< N >*>(p_inout))
- {
- auto values1 = dynamic_cast<const UnresolvedValuesPrimitive< N >*>(p1);
- if (values1) {
- values0->InterpolateValues(*values1, alpha);
- result = true;
- }
- }
- return result;
- }
- template<size_t N>
- static bool TryInterpolateResolvedPrimitive(Rocket::Core::Transforms::Primitive* p_inout, const Rocket::Core::Transforms::Primitive* p1, float alpha)
- {
- using namespace Rocket::Core::Transforms;
- bool result = false;
- if (auto values0 = dynamic_cast<ResolvedValuesPrimitive< N >*>(p_inout))
- {
- auto values1 = dynamic_cast<const ResolvedValuesPrimitive< N >*>(p1);
- if (values1) {
- values0->InterpolateValues(*values1, alpha);
- result = true;
- }
- }
- return result;
- }
- static Variant InterpolateValues(const Variant & v0, const Variant & v1, float alpha)
- {
- auto type0 = v0.GetType();
- auto type1 = v1.GetType();
- if (type0 != type1)
- {
- Log::Message(Log::LT_WARNING, "Interpolating properties must be of same unit. Got types: '%c' and '%c'.", type0, type1);
- return v0;
- }
- switch (type0)
- {
- case Variant::FLOAT:
- {
- float f0 = v0.Get<float>();
- float f1 = v1.Get<float>();
- float f = (1.0f - alpha) * f0 + alpha * f1;
- return Variant(f);
- }
- case Variant::COLOURB:
- {
- Colourf c0 = ColourToLinearSpace(v0.Get<Colourb>());
- Colourf c1 = ColourToLinearSpace(v1.Get<Colourb>());
- Colourf c = c0 * (1.0f - alpha) + c1 * alpha;
- return Variant(ColourFromLinearSpace(c));
- }
- case Variant::TRANSFORMREF:
- {
- using namespace Rocket::Core::Transforms;
- auto t0 = v0.Get<TransformRef>();
- auto t1 = v1.Get<TransformRef>();
- Primitive* p0 = t0->GetPrimitive(0).Clone();
- const Primitive* p1 = &t1->GetPrimitive(0);
- bool success = false;
- // Todo: Check carefully for memory leaks
- // Todo: Lots of dynamic dispatch, not good!
- if (TryInterpolateResolvedPrimitive<1>(p0, p1, alpha))
- success = true;
- else if (TryInterpolateResolvedPrimitive<2>(p0, p1, alpha))
- success = true;
- else if (TryInterpolateResolvedPrimitive<3>(p0, p1, alpha))
- success = true;
- else if (TryInterpolateResolvedPrimitive<4>(p0, p1, alpha))
- success = true;
- else if (TryInterpolateResolvedPrimitive<6>(p0, p1, alpha))
- success = true;
- else if (TryInterpolateResolvedPrimitive<16>(p0, p1, alpha))
- success = true;
- else if (TryInterpolatePrimitive<1>(p0, p1, alpha))
- success = true;
- else if (TryInterpolatePrimitive<2>(p0, p1, alpha))
- success = true;
- else if (TryInterpolatePrimitive<3>(p0, p1, alpha))
- success = true;
- if(success)
- {
- auto transform = new Transform;
- transform->AddPrimitive(*p0);
- TransformRef tref{ transform };
- delete p0;
- return Variant(tref);
- }
- else
- {
- delete p0;
- }
- Log::Message(Log::LT_WARNING, "Could not decode transform for interpolation.");
- }
- }
- Log::Message(Log::LT_WARNING, "Currently, only float and color values can be interpolated. Got types of: '%c'.", type0);
- return v0;
- }
- bool ElementAnimation::AddKey(float time, const Property & property)
- {
- if (property.unit != property_unit)
- return false;
- keys.push_back({ time, property.value });
- return true;
- }
- Property ElementAnimation::UpdateAndGetProperty(float time)
- {
- Property result;
- //Log::Message(Log::LT_INFO, "Animation it = %d, t_it = %f, rev = %d, dt = %f", current_iteration, time_since_iteration_start, (int)reverse_direction, time - last_update_time);
- if (animation_complete || time - last_update_time <= 0.0f)
- return result;
- const float dt = 0.01f;// time - last_update_time;
- last_update_time = time;
- time_since_iteration_start += dt;
- if (time_since_iteration_start >= duration)
- {
- // Next iteration
- current_iteration += 1;
- if (current_iteration < num_iterations || num_iterations == -1)
- {
- time_since_iteration_start = 0.0f;
- if (alternate_direction)
- reverse_direction = !reverse_direction;
- }
- else
- {
- animation_complete = true;
- time_since_iteration_start = duration;
- }
- }
- float t = time_since_iteration_start;
- if (reverse_direction)
- t = duration - t;
- int key0 = -1;
- int key1 = -1;
- {
- for (int i = 0; i < (int)keys.size(); i++)
- {
- if (keys[i].time >= t)
- {
- key1 = i;
- break;
- }
- }
- if (key1 < 0) key1 = (int)keys.size() - 1;
- key0 = (key1 == 0 ? 0 : key1 - 1 );
- }
- ROCKET_ASSERT(key0 >= 0 && key0 < (int)keys.size() && key1 >= 0 && key1 < (int)keys.size());
- float alpha = 0.0f;
- {
- const float t0 = keys[key0].time;
- const float t1 = keys[key1].time;
- const float eps = 1e-3f;
- if (t1 - t0 > eps)
- alpha = (t - t0) / (t1 - t0);
-
- alpha = Math::Clamp(alpha, 0.0f, 1.0f);
- }
- result.unit = property_unit;
- result.specificity = property_specificity;
- result.value = InterpolateValues(keys[key0].value, keys[key1].value, alpha);
-
- return result;
- }
- }
- }
|