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