PropertyParserAnimation.cpp 11 KB

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