PropertyParserAnimation.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. struct PropertyParserAnimationData {
  49. const UnorderedMap<String, Keyword> keywords = {
  50. {"none", {KeywordType::None}},
  51. {"all", {KeywordType::All}},
  52. {"alternate", {KeywordType::Alternate}},
  53. {"infinite", {KeywordType::Infinite}},
  54. {"paused", {KeywordType::Paused}},
  55. {"back-in", {Tween{Tween::Back, Tween::In}}},
  56. {"back-out", {Tween{Tween::Back, Tween::Out}}},
  57. {"back-in-out", {Tween{Tween::Back, Tween::InOut}}},
  58. {"bounce-in", {Tween{Tween::Bounce, Tween::In}}},
  59. {"bounce-out", {Tween{Tween::Bounce, Tween::Out}}},
  60. {"bounce-in-out", {Tween{Tween::Bounce, Tween::InOut}}},
  61. {"circular-in", {Tween{Tween::Circular, Tween::In}}},
  62. {"circular-out", {Tween{Tween::Circular, Tween::Out}}},
  63. {"circular-in-out", {Tween{Tween::Circular, Tween::InOut}}},
  64. {"cubic-in", {Tween{Tween::Cubic, Tween::In}}},
  65. {"cubic-out", {Tween{Tween::Cubic, Tween::Out}}},
  66. {"cubic-in-out", {Tween{Tween::Cubic, Tween::InOut}}},
  67. {"elastic-in", {Tween{Tween::Elastic, Tween::In}}},
  68. {"elastic-out", {Tween{Tween::Elastic, Tween::Out}}},
  69. {"elastic-in-out", {Tween{Tween::Elastic, Tween::InOut}}},
  70. {"exponential-in", {Tween{Tween::Exponential, Tween::In}}},
  71. {"exponential-out", {Tween{Tween::Exponential, Tween::Out}}},
  72. {"exponential-in-out", {Tween{Tween::Exponential, Tween::InOut}}},
  73. {"linear-in", {Tween{Tween::Linear, Tween::In}}},
  74. {"linear-out", {Tween{Tween::Linear, Tween::Out}}},
  75. {"linear-in-out", {Tween{Tween::Linear, Tween::InOut}}},
  76. {"quadratic-in", {Tween{Tween::Quadratic, Tween::In}}},
  77. {"quadratic-out", {Tween{Tween::Quadratic, Tween::Out}}},
  78. {"quadratic-in-out", {Tween{Tween::Quadratic, Tween::InOut}}},
  79. {"quartic-in", {Tween{Tween::Quartic, Tween::In}}},
  80. {"quartic-out", {Tween{Tween::Quartic, Tween::Out}}},
  81. {"quartic-in-out", {Tween{Tween::Quartic, Tween::InOut}}},
  82. {"quintic-in", {Tween{Tween::Quintic, Tween::In}}},
  83. {"quintic-out", {Tween{Tween::Quintic, Tween::Out}}},
  84. {"quintic-in-out", {Tween{Tween::Quintic, Tween::InOut}}},
  85. {"sine-in", {Tween{Tween::Sine, Tween::In}}},
  86. {"sine-out", {Tween{Tween::Sine, Tween::Out}}},
  87. {"sine-in-out", {Tween{Tween::Sine, Tween::InOut}}},
  88. };
  89. };
  90. ControlledLifetimeResource<PropertyParserAnimationData> PropertyParserAnimation::parser_data;
  91. void PropertyParserAnimation::Initialize()
  92. {
  93. parser_data.Initialize();
  94. }
  95. void PropertyParserAnimation::Shutdown()
  96. {
  97. parser_data.Shutdown();
  98. }
  99. PropertyParserAnimation::PropertyParserAnimation(Type type) : type(type) {}
  100. bool PropertyParserAnimation::ParseValue(Property& property, const String& value, const ParameterMap& /*parameters*/) const
  101. {
  102. StringList list_of_values;
  103. StringUtilities::ExpandString(list_of_values, value, ',');
  104. bool result = false;
  105. if (type == ANIMATION_PARSER)
  106. {
  107. result = ParseAnimation(property, list_of_values);
  108. }
  109. else if (type == TRANSITION_PARSER)
  110. {
  111. result = ParseTransition(property, list_of_values);
  112. }
  113. return result;
  114. }
  115. bool PropertyParserAnimation::ParseAnimation(Property& property, const StringList& animation_values)
  116. {
  117. AnimationList animation_list;
  118. for (const String& single_animation_value : animation_values)
  119. {
  120. Animation animation;
  121. StringList arguments;
  122. StringUtilities::ExpandString(arguments, single_animation_value, ' ');
  123. bool duration_found = false;
  124. bool delay_found = false;
  125. bool num_iterations_found = false;
  126. for (auto& argument : arguments)
  127. {
  128. if (argument.empty())
  129. continue;
  130. // See if we have a <keyword> or <tween> specifier as defined in keywords
  131. auto it = parser_data->keywords.find(StringUtilities::ToLower(argument));
  132. if (it != parser_data->keywords.end() && it->second.ValidAnimation())
  133. {
  134. switch (it->second.type)
  135. {
  136. case KeywordType::None:
  137. {
  138. if (animation_list.size() > 0) // The none keyword can not be part of multiple definitions
  139. return false;
  140. property = Property{AnimationList{}, Unit::ANIMATION};
  141. return true;
  142. }
  143. break;
  144. case KeywordType::Tween: animation.tween = it->second.tween; break;
  145. case KeywordType::Alternate: animation.alternate = true; break;
  146. case KeywordType::Infinite:
  147. if (num_iterations_found)
  148. return false;
  149. animation.num_iterations = -1;
  150. num_iterations_found = true;
  151. break;
  152. case KeywordType::Paused: animation.paused = true; break;
  153. default: break;
  154. }
  155. }
  156. else
  157. {
  158. // Either <duration>, <delay>, <num_iterations> or a <keyframes-name>
  159. float number = 0.0f;
  160. int count = 0;
  161. if (sscanf(argument.c_str(), "%fs%n", &number, &count) == 1)
  162. {
  163. // Found a number, if there was an 's' unit, count will be positive
  164. if (count > 0)
  165. {
  166. // Duration or delay was assigned
  167. if (!duration_found)
  168. {
  169. duration_found = true;
  170. animation.duration = number;
  171. }
  172. else if (!delay_found)
  173. {
  174. delay_found = true;
  175. animation.delay = number;
  176. }
  177. else
  178. return false;
  179. }
  180. else
  181. {
  182. // No 's' unit means num_iterations was found
  183. if (!num_iterations_found)
  184. {
  185. animation.num_iterations = Math::RoundToInteger(number);
  186. num_iterations_found = true;
  187. }
  188. else
  189. return false;
  190. }
  191. }
  192. else
  193. {
  194. // Must be an animation name
  195. animation.name = argument;
  196. }
  197. }
  198. }
  199. // Validate the parsed transition
  200. if (animation.name.empty() || animation.duration <= 0.0f || (animation.num_iterations < -1 || animation.num_iterations == 0))
  201. {
  202. return false;
  203. }
  204. animation_list.push_back(std::move(animation));
  205. }
  206. property.value = std::move(animation_list);
  207. property.unit = Unit::ANIMATION;
  208. return true;
  209. }
  210. bool PropertyParserAnimation::ParseTransition(Property& property, const StringList& transition_values)
  211. {
  212. TransitionList transition_list{false, false, {}};
  213. for (const String& single_transition_value : transition_values)
  214. {
  215. Transition transition;
  216. PropertyIdSet target_property_ids;
  217. StringList arguments;
  218. StringUtilities::ExpandString(arguments, single_transition_value, ' ');
  219. bool duration_found = false;
  220. bool delay_found = false;
  221. bool reverse_adjustment_factor_found = false;
  222. for (auto& argument : arguments)
  223. {
  224. if (argument.empty())
  225. continue;
  226. // See if we have a <keyword> or <tween> specifier as defined in keywords
  227. auto it = parser_data->keywords.find(argument);
  228. if (it != parser_data->keywords.end() && it->second.ValidTransition())
  229. {
  230. if (it->second.type == KeywordType::None)
  231. {
  232. if (transition_list.transitions.size() > 0) // The none keyword can not be part of multiple definitions
  233. return false;
  234. property = Property{TransitionList{true, false, {}}, Unit::TRANSITION};
  235. return true;
  236. }
  237. else if (it->second.type == KeywordType::All)
  238. {
  239. if (transition_list.transitions.size() > 0) // The all keyword can not be part of multiple definitions
  240. return false;
  241. transition_list.all = true;
  242. }
  243. else if (it->second.type == KeywordType::Tween)
  244. {
  245. transition.tween = it->second.tween;
  246. }
  247. }
  248. else
  249. {
  250. // Either <duration>, <delay> or a <property name>
  251. float number = 0.0f;
  252. int count = 0;
  253. if (sscanf(argument.c_str(), "%fs%n", &number, &count) == 1)
  254. {
  255. // Found a number, if there was an 's' unit, count will be positive
  256. if (count > 0)
  257. {
  258. // Duration or delay was assigned
  259. if (!duration_found)
  260. {
  261. duration_found = true;
  262. transition.duration = number;
  263. }
  264. else if (!delay_found)
  265. {
  266. delay_found = true;
  267. transition.delay = number;
  268. }
  269. else
  270. return false;
  271. }
  272. else
  273. {
  274. // No 's' unit means reverse adjustment factor was found
  275. if (!reverse_adjustment_factor_found)
  276. {
  277. reverse_adjustment_factor_found = true;
  278. transition.reverse_adjustment_factor = number;
  279. }
  280. else
  281. return false;
  282. }
  283. }
  284. else
  285. {
  286. // Must be a property name or shorthand, expand now
  287. if (auto shorthand = StyleSheetSpecification::GetShorthand(argument))
  288. {
  289. PropertyIdSet underlying_properties = StyleSheetSpecification::GetShorthandUnderlyingProperties(shorthand->id);
  290. target_property_ids |= underlying_properties;
  291. }
  292. else if (auto definition = StyleSheetSpecification::GetProperty(argument))
  293. {
  294. // Single property
  295. target_property_ids.Insert(definition->GetId());
  296. }
  297. else
  298. {
  299. // Unknown property name
  300. return false;
  301. }
  302. }
  303. }
  304. }
  305. // Validate the parsed transition
  306. if ((transition_list.all && !target_property_ids.Empty()) //
  307. || (!transition_list.all && target_property_ids.Empty()) //
  308. || transition.duration <= 0.0f //
  309. || transition.reverse_adjustment_factor < 0.0f //
  310. || transition.reverse_adjustment_factor > 1.0f //
  311. )
  312. {
  313. return false;
  314. }
  315. if (transition_list.all)
  316. {
  317. transition.id = PropertyId::Invalid;
  318. transition_list.transitions.push_back(transition);
  319. }
  320. else
  321. {
  322. for (const PropertyId id : target_property_ids)
  323. {
  324. transition.id = id;
  325. transition_list.transitions.push_back(transition);
  326. }
  327. }
  328. }
  329. property.value = std::move(transition_list);
  330. property.unit = Unit::TRANSITION;
  331. return true;
  332. }
  333. } // namespace Rml