| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- /*
- * 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"
- 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;
- }
- static Variant InterpolateValues(const Variant & from, const Variant & to, float alpha)
- {
- auto type = from.GetType();
- auto type_to = to.GetType();
- if (type != type_to)
- {
- Log::Message(Log::LT_WARNING, "Interpolating properties must be of same unit. Got types: '%c' and '%c'.", type, type_to);
- return from;
- }
- switch (type)
- {
- case Variant::FLOAT:
- {
- float f0 = from.Get<float>();
- float f1 = to.Get<float>();
- float f = (1.0f - alpha) * f0 + alpha * f1;
- return Variant(f);
- }
- case Variant::COLOURB:
- {
- Colourf c0 = ColourToLinearSpace(from.Get<Colourb>());
- Colourf c1 = ColourToLinearSpace(to.Get<Colourb>());
- Colourf c = c0 * (1.0f - alpha) + c1 * alpha;
- return Variant(ColourFromLinearSpace(c));
- }
- }
- Log::Message(Log::LT_WARNING, "Currently, only float and color values can be interpolated. Got types of: '%c'.", type);
- return from;
- }
- 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 = 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;
- }
- }
- }
|