PropertyParserAnimation.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2018 Michael R. P. Ragazzon
  7. * Copyright (c) 2019-2023 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "PropertyParserAnimation.h"
  29. #include "../../Include/RmlUi/Core/PropertyDefinition.h"
  30. #include "../../Include/RmlUi/Core/PropertyIdSet.h"
  31. #include "../../Include/RmlUi/Core/StringUtilities.h"
  32. #include "../../Include/RmlUi/Core/StyleSheetSpecification.h"
  33. #include "PropertyShorthandDefinition.h"
  34. namespace Rml {
  35. enum class KeywordType { None, Tween, All, Alternate, Infinite, Paused };
  36. struct Keyword {
  37. KeywordType type;
  38. Tween tween;
  39. Keyword(Tween tween) : type(KeywordType::Tween), tween(tween) {}
  40. Keyword(KeywordType type) : type(type) {}
  41. bool ValidTransition() const { return type == KeywordType::None || type == KeywordType::Tween || type == KeywordType::All; }
  42. bool ValidAnimation() const
  43. {
  44. return type == KeywordType::None || type == KeywordType::Tween || type == KeywordType::Alternate || type == KeywordType::Infinite ||
  45. type == KeywordType::Paused;
  46. }
  47. };
  48. static const UnorderedMap<String, Keyword> keywords = {
  49. {"none", {KeywordType::None}},
  50. {"all", {KeywordType::All}},
  51. {"alternate", {KeywordType::Alternate}},
  52. {"infinite", {KeywordType::Infinite}},
  53. {"paused", {KeywordType::Paused}},
  54. {"back-in", {Tween{Tween::Back, Tween::In}}},
  55. {"back-out", {Tween{Tween::Back, Tween::Out}}},
  56. {"back-in-out", {Tween{Tween::Back, Tween::InOut}}},
  57. {"bounce-in", {Tween{Tween::Bounce, Tween::In}}},
  58. {"bounce-out", {Tween{Tween::Bounce, Tween::Out}}},
  59. {"bounce-in-out", {Tween{Tween::Bounce, Tween::InOut}}},
  60. {"circular-in", {Tween{Tween::Circular, Tween::In}}},
  61. {"circular-out", {Tween{Tween::Circular, Tween::Out}}},
  62. {"circular-in-out", {Tween{Tween::Circular, Tween::InOut}}},
  63. {"cubic-in", {Tween{Tween::Cubic, Tween::In}}},
  64. {"cubic-out", {Tween{Tween::Cubic, Tween::Out}}},
  65. {"cubic-in-out", {Tween{Tween::Cubic, Tween::InOut}}},
  66. {"elastic-in", {Tween{Tween::Elastic, Tween::In}}},
  67. {"elastic-out", {Tween{Tween::Elastic, Tween::Out}}},
  68. {"elastic-in-out", {Tween{Tween::Elastic, Tween::InOut}}},
  69. {"exponential-in", {Tween{Tween::Exponential, Tween::In}}},
  70. {"exponential-out", {Tween{Tween::Exponential, Tween::Out}}},
  71. {"exponential-in-out", {Tween{Tween::Exponential, Tween::InOut}}},
  72. {"linear-in", {Tween{Tween::Linear, Tween::In}}},
  73. {"linear-out", {Tween{Tween::Linear, Tween::Out}}},
  74. {"linear-in-out", {Tween{Tween::Linear, Tween::InOut}}},
  75. {"quadratic-in", {Tween{Tween::Quadratic, Tween::In}}},
  76. {"quadratic-out", {Tween{Tween::Quadratic, Tween::Out}}},
  77. {"quadratic-in-out", {Tween{Tween::Quadratic, Tween::InOut}}},
  78. {"quartic-in", {Tween{Tween::Quartic, Tween::In}}},
  79. {"quartic-out", {Tween{Tween::Quartic, Tween::Out}}},
  80. {"quartic-in-out", {Tween{Tween::Quartic, Tween::InOut}}},
  81. {"quintic-in", {Tween{Tween::Quintic, Tween::In}}},
  82. {"quintic-out", {Tween{Tween::Quintic, Tween::Out}}},
  83. {"quintic-in-out", {Tween{Tween::Quintic, Tween::InOut}}},
  84. {"sine-in", {Tween{Tween::Sine, Tween::In}}},
  85. {"sine-out", {Tween{Tween::Sine, Tween::Out}}},
  86. {"sine-in-out", {Tween{Tween::Sine, Tween::InOut}}},
  87. };
  88. PropertyParserAnimation::PropertyParserAnimation(Type type) : type(type) {}
  89. static bool ParseAnimation(Property& property, const StringList& animation_values)
  90. {
  91. AnimationList animation_list;
  92. for (const String& single_animation_value : animation_values)
  93. {
  94. Animation animation;
  95. StringList arguments;
  96. StringUtilities::ExpandString(arguments, single_animation_value, ' ');
  97. bool duration_found = false;
  98. bool delay_found = false;
  99. bool num_iterations_found = false;
  100. for (auto& argument : arguments)
  101. {
  102. if (argument.empty())
  103. continue;
  104. // See if we have a <keyword> or <tween> specifier as defined in keywords
  105. auto it = keywords.find(argument);
  106. if (it != keywords.end() && it->second.ValidAnimation())
  107. {
  108. switch (it->second.type)
  109. {
  110. case KeywordType::None:
  111. {
  112. if (animation_list.size() > 0) // The none keyword can not be part of multiple definitions
  113. return false;
  114. property = Property{AnimationList{}, Unit::ANIMATION};
  115. return true;
  116. }
  117. break;
  118. case KeywordType::Tween: animation.tween = it->second.tween; break;
  119. case KeywordType::Alternate: animation.alternate = true; break;
  120. case KeywordType::Infinite:
  121. if (num_iterations_found)
  122. return false;
  123. animation.num_iterations = -1;
  124. num_iterations_found = true;
  125. break;
  126. case KeywordType::Paused: animation.paused = true; break;
  127. default: break;
  128. }
  129. }
  130. else
  131. {
  132. // Either <duration>, <delay>, <num_iterations> or a <keyframes-name>
  133. float number = 0.0f;
  134. int count = 0;
  135. if (sscanf(argument.c_str(), "%fs%n", &number, &count) == 1)
  136. {
  137. // Found a number, if there was an 's' unit, count will be positive
  138. if (count > 0)
  139. {
  140. // Duration or delay was assigned
  141. if (!duration_found)
  142. {
  143. duration_found = true;
  144. animation.duration = number;
  145. }
  146. else if (!delay_found)
  147. {
  148. delay_found = true;
  149. animation.delay = number;
  150. }
  151. else
  152. return false;
  153. }
  154. else
  155. {
  156. // No 's' unit means num_iterations was found
  157. if (!num_iterations_found)
  158. {
  159. animation.num_iterations = Math::RoundToInteger(number);
  160. num_iterations_found = true;
  161. }
  162. else
  163. return false;
  164. }
  165. }
  166. else
  167. {
  168. // Must be an animation name
  169. animation.name = argument;
  170. }
  171. }
  172. }
  173. // Validate the parsed transition
  174. if (animation.name.empty() || animation.duration <= 0.0f || (animation.num_iterations < -1 || animation.num_iterations == 0))
  175. {
  176. return false;
  177. }
  178. animation_list.push_back(std::move(animation));
  179. }
  180. property.value = std::move(animation_list);
  181. property.unit = Unit::ANIMATION;
  182. return true;
  183. }
  184. static bool ParseTransition(Property& property, const StringList& transition_values)
  185. {
  186. TransitionList transition_list{false, false, {}};
  187. for (const String& single_transition_value : transition_values)
  188. {
  189. Transition transition;
  190. PropertyIdSet target_property_ids;
  191. StringList arguments;
  192. StringUtilities::ExpandString(arguments, single_transition_value, ' ');
  193. bool duration_found = false;
  194. bool delay_found = false;
  195. bool reverse_adjustment_factor_found = false;
  196. for (auto& argument : arguments)
  197. {
  198. if (argument.empty())
  199. continue;
  200. // See if we have a <keyword> or <tween> specifier as defined in keywords
  201. auto it = keywords.find(argument);
  202. if (it != keywords.end() && it->second.ValidTransition())
  203. {
  204. if (it->second.type == KeywordType::None)
  205. {
  206. if (transition_list.transitions.size() > 0) // The none keyword can not be part of multiple definitions
  207. return false;
  208. property = Property{TransitionList{true, false, {}}, Unit::TRANSITION};
  209. return true;
  210. }
  211. else if (it->second.type == KeywordType::All)
  212. {
  213. if (transition_list.transitions.size() > 0) // The all keyword can not be part of multiple definitions
  214. return false;
  215. transition_list.all = true;
  216. }
  217. else if (it->second.type == KeywordType::Tween)
  218. {
  219. transition.tween = it->second.tween;
  220. }
  221. }
  222. else
  223. {
  224. // Either <duration>, <delay> or a <property name>
  225. float number = 0.0f;
  226. int count = 0;
  227. if (sscanf(argument.c_str(), "%fs%n", &number, &count) == 1)
  228. {
  229. // Found a number, if there was an 's' unit, count will be positive
  230. if (count > 0)
  231. {
  232. // Duration or delay was assigned
  233. if (!duration_found)
  234. {
  235. duration_found = true;
  236. transition.duration = number;
  237. }
  238. else if (!delay_found)
  239. {
  240. delay_found = true;
  241. transition.delay = number;
  242. }
  243. else
  244. return false;
  245. }
  246. else
  247. {
  248. // No 's' unit means reverse adjustment factor was found
  249. if (!reverse_adjustment_factor_found)
  250. {
  251. reverse_adjustment_factor_found = true;
  252. transition.reverse_adjustment_factor = number;
  253. }
  254. else
  255. return false;
  256. }
  257. }
  258. else
  259. {
  260. // Must be a property name or shorthand, expand now
  261. if (auto shorthand = StyleSheetSpecification::GetShorthand(argument))
  262. {
  263. PropertyIdSet underlying_properties = StyleSheetSpecification::GetShorthandUnderlyingProperties(shorthand->id);
  264. target_property_ids |= underlying_properties;
  265. }
  266. else if (auto definition = StyleSheetSpecification::GetProperty(argument))
  267. {
  268. // Single property
  269. target_property_ids.Insert(definition->GetId());
  270. }
  271. else
  272. {
  273. // Unknown property name
  274. return false;
  275. }
  276. }
  277. }
  278. }
  279. // Validate the parsed transition
  280. if ((transition_list.all && !target_property_ids.Empty()) //
  281. || (!transition_list.all && target_property_ids.Empty()) //
  282. || transition.duration <= 0.0f //
  283. || transition.reverse_adjustment_factor < 0.0f //
  284. || transition.reverse_adjustment_factor > 1.0f //
  285. )
  286. {
  287. return false;
  288. }
  289. if (transition_list.all)
  290. {
  291. transition.id = PropertyId::Invalid;
  292. transition_list.transitions.push_back(transition);
  293. }
  294. else
  295. {
  296. for (const PropertyId id : target_property_ids)
  297. {
  298. transition.id = id;
  299. transition_list.transitions.push_back(transition);
  300. }
  301. }
  302. }
  303. property.value = std::move(transition_list);
  304. property.unit = Unit::TRANSITION;
  305. return true;
  306. }
  307. bool PropertyParserAnimation::ParseValue(Property& property, const String& value, const ParameterMap& /*parameters*/) const
  308. {
  309. StringList list_of_values;
  310. {
  311. auto lowercase_value = StringUtilities::ToLower(value);
  312. StringUtilities::ExpandString(list_of_values, lowercase_value, ',');
  313. }
  314. bool result = false;
  315. if (type == ANIMATION_PARSER)
  316. {
  317. result = ParseAnimation(property, list_of_values);
  318. }
  319. else if (type == TRANSITION_PARSER)
  320. {
  321. result = ParseTransition(property, list_of_values);
  322. }
  323. return result;
  324. }
  325. } // namespace Rml