2
0

PropertyParserAnimation.cpp 11 KB

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