PropertyParserTransition.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 "PropertyParserTransition.h"
  29. #include "../../Include/Rocket/Core/StringUtilities.h"
  30. #include "PropertyShorthandDefinition.h"
  31. namespace Rocket {
  32. namespace Core {
  33. struct TransitionSpec {
  34. enum Type { KEYWORD_NONE, KEYWORD_ALL, TWEEN } type;
  35. Tween tween;
  36. TransitionSpec(Tween tween) : type(TWEEN), tween(tween) {}
  37. TransitionSpec(Type type) : type(type) {}
  38. };
  39. static const std::unordered_map<String, TransitionSpec> transition_spec = {
  40. {"none", {TransitionSpec::KEYWORD_NONE} },
  41. {"all", {TransitionSpec::KEYWORD_ALL}},
  42. {"back-in", {Tween{Tween::Back, Tween::In}}},
  43. {"back-out", {Tween{Tween::Back, Tween::Out}}},
  44. {"back-in-out", {Tween{Tween::Back, Tween::InOut}}},
  45. {"bounce-in", {Tween{Tween::Bounce, Tween::In}}},
  46. {"bounce-out", {Tween{Tween::Bounce, Tween::Out}}},
  47. {"bounce-in-out", {Tween{Tween::Bounce, Tween::InOut}}},
  48. {"circular-in", {Tween{Tween::Circular, Tween::In}}},
  49. {"circular-out", {Tween{Tween::Circular, Tween::Out}}},
  50. {"circular-in-out", {Tween{Tween::Circular, Tween::InOut}}},
  51. {"cubic-in", {Tween{Tween::Cubic, Tween::In}}},
  52. {"cubic-out", {Tween{Tween::Cubic, Tween::Out}}},
  53. {"cubic-in-out", {Tween{Tween::Cubic, Tween::InOut}}},
  54. {"elastic-in", {Tween{Tween::Elastic, Tween::In}}},
  55. {"elastic-out", {Tween{Tween::Elastic, Tween::Out}}},
  56. {"elastic-in-out", {Tween{Tween::Elastic, Tween::InOut}}},
  57. {"exponential-in", {Tween{Tween::Exponential, Tween::In}}},
  58. {"exponential-out", {Tween{Tween::Exponential, Tween::Out}}},
  59. {"exponential-in-out", {Tween{Tween::Exponential, Tween::InOut}}},
  60. {"linear-in", {Tween{Tween::Linear, Tween::In}}},
  61. {"linear-out", {Tween{Tween::Linear, Tween::Out}}},
  62. {"linear-in-out", {Tween{Tween::Linear, Tween::InOut}}},
  63. {"quadratic-in", {Tween{Tween::Quadratic, Tween::In}}},
  64. {"quadratic-out", {Tween{Tween::Quadratic, Tween::Out}}},
  65. {"quadratic-in-out", {Tween{Tween::Quadratic, Tween::InOut}}},
  66. {"quartic-in", {Tween{Tween::Quartic, Tween::In}}},
  67. {"quartic-out", {Tween{Tween::Quartic, Tween::Out}}},
  68. {"quartic-in-out", {Tween{Tween::Quartic, Tween::InOut}}},
  69. {"quintic-in", {Tween{Tween::Quintic, Tween::In}}},
  70. {"quintic-out", {Tween{Tween::Quintic, Tween::Out}}},
  71. {"quintic-in-out", {Tween{Tween::Quintic, Tween::InOut}}},
  72. {"sine-in", {Tween{Tween::Sine, Tween::In}}},
  73. {"sine-out", {Tween{Tween::Sine, Tween::Out}}},
  74. {"sine-in-out", {Tween{Tween::Sine, Tween::InOut}}},
  75. };
  76. PropertyParserTransition::PropertyParserTransition()
  77. {
  78. }
  79. bool PropertyParserTransition::ParseValue(Property & property, const String & value, const ParameterMap & parameters) const
  80. {
  81. StringList list_of_properties;
  82. {
  83. auto lowercase_value = value.ToLower();
  84. StringUtilities::ExpandString(list_of_properties, lowercase_value, ',');
  85. }
  86. TransitionList transition_list{ false, false, {} };
  87. for (const String& single_property : list_of_properties)
  88. {
  89. Transition transition;
  90. StringList target_property_names;
  91. StringList arguments;
  92. StringUtilities::ExpandString(arguments, single_property, ' ');
  93. bool duration_found = false;
  94. bool delay_found = false;
  95. bool reverse_adjustment_factor_found = false;
  96. for (auto& argument : arguments)
  97. {
  98. if (argument.Empty())
  99. continue;
  100. // See if we have a <keyword> or <tween> specifier as defined in transition_spec
  101. if (auto it = transition_spec.find(argument); it != transition_spec.end())
  102. {
  103. if (it->second.type == TransitionSpec::KEYWORD_NONE)
  104. {
  105. if (transition_list.transitions.size() > 0) // The none keyword can not be part of multiple definitions
  106. return false;
  107. property = Property{ TransitionList{true, false, {}}, Property::TRANSITION };
  108. return true;
  109. }
  110. else if (it->second.type == TransitionSpec::KEYWORD_ALL)
  111. {
  112. if (transition_list.transitions.size() > 0) // The all keyword can not be part of multiple definitions
  113. return false;
  114. transition_list.all = true;
  115. target_property_names.push_back("all");
  116. }
  117. else if (it->second.type == TransitionSpec::TWEEN)
  118. {
  119. transition.tween = it->second.tween;
  120. }
  121. }
  122. else
  123. {
  124. // Either <duration>, <delay> or a <property name>
  125. float number = 0.0f;
  126. int count = 0;
  127. if (sscanf(argument.CString(), "%fs%n", &number, &count) == 1)
  128. {
  129. // Found a number, if there was an 's' unit, count will be positive
  130. if (count > 0)
  131. {
  132. // Duration or delay was assigned
  133. if (!duration_found)
  134. {
  135. duration_found = true;
  136. transition.duration = number;
  137. }
  138. else if (!delay_found)
  139. {
  140. delay_found = true;
  141. transition.delay = number;
  142. }
  143. else
  144. return false;
  145. }
  146. else
  147. {
  148. // No 's' unit means reverse adjustment factor was found
  149. if (!reverse_adjustment_factor_found)
  150. {
  151. reverse_adjustment_factor_found = true;
  152. transition.reverse_adjustment_factor = number;
  153. }
  154. else
  155. return false;
  156. }
  157. }
  158. else
  159. {
  160. // Must be a property name or shorthand, expand now
  161. if (auto shorthand = StyleSheetSpecification::GetShorthand(argument))
  162. {
  163. // For shorthands, add each underlying property separately
  164. for (const auto& property : shorthand->properties)
  165. target_property_names.push_back(property.first);
  166. }
  167. else if (auto definition = StyleSheetSpecification::GetProperty(argument))
  168. {
  169. // Single property
  170. target_property_names.push_back(argument);
  171. }
  172. else
  173. {
  174. // Unknown property name
  175. return false;
  176. }
  177. }
  178. }
  179. }
  180. // Validate the parsed transition
  181. if (target_property_names.empty() || transition.duration <= 0.0f || transition.reverse_adjustment_factor < 0.0f || transition.reverse_adjustment_factor > 1.0f
  182. || (transition_list.all && target_property_names.size() != 1))
  183. {
  184. return false;
  185. }
  186. for (const auto& property_name : target_property_names)
  187. {
  188. transition.name = property_name;
  189. transition_list.transitions.push_back(transition);
  190. }
  191. }
  192. property = Property{ transition_list, Property::TRANSITION };
  193. return true;
  194. }
  195. void PropertyParserTransition::Release()
  196. {
  197. delete this;
  198. }
  199. }
  200. }