ElementAnimation.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2018 Michael Ragazzon
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "precompiled.h"
  28. #include "ElementAnimation.h"
  29. #include "../../Include/Rocket/Core/Element.h"
  30. namespace Rocket {
  31. namespace Core {
  32. static Colourf ColourToLinearSpace(Colourb c)
  33. {
  34. Colourf result;
  35. // Approximate inverse sRGB function
  36. result.red = Math::SquareRoot((float)c.red / 255.f);
  37. result.green = Math::SquareRoot((float)c.green / 255.f);
  38. result.blue = Math::SquareRoot((float)c.blue / 255.f);
  39. result.alpha = (float)c.alpha / 255.f;
  40. return result;
  41. }
  42. static Colourb ColourFromLinearSpace(Colourf c)
  43. {
  44. Colourb result;
  45. result.red = (Rocket::Core::byte)Math::Clamp(c.red*c.red*255.f, 0.0f, 255.f);
  46. result.green = (Rocket::Core::byte)Math::Clamp(c.green*c.green*255.f, 0.0f, 255.f);
  47. result.blue = (Rocket::Core::byte)Math::Clamp(c.blue*c.blue*255.f, 0.0f, 255.f);
  48. result.alpha = (Rocket::Core::byte)Math::Clamp(c.alpha*255.f, 0.0f, 255.f);
  49. return result;
  50. }
  51. static Variant InterpolateValues(const Variant & from, const Variant & to, float alpha)
  52. {
  53. auto type = from.GetType();
  54. auto type_to = to.GetType();
  55. if (type != type_to)
  56. {
  57. Log::Message(Log::LT_WARNING, "Interpolating properties must be of same unit. Got types: '%c' and '%c'.", type, type_to);
  58. return from;
  59. }
  60. switch (type)
  61. {
  62. case Variant::FLOAT:
  63. {
  64. float f0 = from.Get<float>();
  65. float f1 = to.Get<float>();
  66. float f = (1.0f - alpha) * f0 + alpha * f1;
  67. return Variant(f);
  68. }
  69. case Variant::COLOURB:
  70. {
  71. Colourf c0 = ColourToLinearSpace(from.Get<Colourb>());
  72. Colourf c1 = ColourToLinearSpace(to.Get<Colourb>());
  73. Colourf c = c0 * (1.0f - alpha) + c1 * alpha;
  74. return Variant(ColourFromLinearSpace(c));
  75. }
  76. }
  77. Log::Message(Log::LT_WARNING, "Currently, only float and color values can be interpolated. Got types of: '%c'.", type);
  78. return from;
  79. }
  80. bool ElementAnimation::AddKey(float time, const Property & property)
  81. {
  82. if (property.unit != property_unit)
  83. return false;
  84. keys.push_back({ time, property.value });
  85. return true;
  86. }
  87. Property ElementAnimation::UpdateAndGetProperty(float time)
  88. {
  89. Property result;
  90. //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);
  91. if (animation_complete || time - last_update_time <= 0.0f)
  92. return result;
  93. const float dt = time - last_update_time;
  94. last_update_time = time;
  95. time_since_iteration_start += dt;
  96. if (time_since_iteration_start >= duration)
  97. {
  98. // Next iteration
  99. current_iteration += 1;
  100. if (current_iteration < num_iterations || num_iterations == -1)
  101. {
  102. time_since_iteration_start = 0.0f;
  103. if (alternate_direction)
  104. reverse_direction = !reverse_direction;
  105. }
  106. else
  107. {
  108. animation_complete = true;
  109. time_since_iteration_start = duration;
  110. }
  111. }
  112. float t = time_since_iteration_start;
  113. if (reverse_direction)
  114. t = duration - t;
  115. int key0 = -1;
  116. int key1 = -1;
  117. {
  118. for (int i = 0; i < (int)keys.size(); i++)
  119. {
  120. if (keys[i].time >= t)
  121. {
  122. key1 = i;
  123. break;
  124. }
  125. }
  126. if (key1 < 0) key1 = (int)keys.size() - 1;
  127. key0 = (key1 == 0 ? 0 : key1 - 1 );
  128. }
  129. ROCKET_ASSERT(key0 >= 0 && key0 < (int)keys.size() && key1 >= 0 && key1 < (int)keys.size());
  130. float alpha = 0.0f;
  131. {
  132. const float t0 = keys[key0].time;
  133. const float t1 = keys[key1].time;
  134. const float eps = 1e-3f;
  135. if (t1 - t0 > eps)
  136. alpha = (t - t0) / (t1 - t0);
  137. alpha = Math::Clamp(alpha, 0.0f, 1.0f);
  138. }
  139. result.unit = property_unit;
  140. result.specificity = property_specificity;
  141. result.value = InterpolateValues(keys[key0].value, keys[key1].value, alpha);
  142. return result;
  143. }
  144. }
  145. }