PropertyParserAnimation.cpp 10 KB

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