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. auto it = keywords.find(argument);
  105. if (it != keywords.end() && it->second.ValidAnimation())
  106. {
  107. switch (it->second.type)
  108. {
  109. case Keyword::NONE:
  110. {
  111. if (animation_list.size() > 0) // The none keyword can not be part of multiple definitions
  112. return false;
  113. property = Property{ AnimationList{}, Property::ANIMATION };
  114. return true;
  115. }
  116. break;
  117. case Keyword::TWEEN:
  118. animation.tween = it->second.tween;
  119. break;
  120. case Keyword::ALTERNATE:
  121. animation.alternate = true;
  122. break;
  123. case Keyword::INFINITE:
  124. if (num_iterations_found)
  125. return false;
  126. animation.num_iterations = -1;
  127. num_iterations_found = true;
  128. break;
  129. case Keyword::PAUSED:
  130. animation.paused = true;
  131. break;
  132. default:
  133. break;
  134. }
  135. }
  136. else
  137. {
  138. // Either <duration>, <delay>, <num_iterations> or a <keyframes-name>
  139. float number = 0.0f;
  140. int count = 0;
  141. if (sscanf(argument.c_str(), "%fs%n", &number, &count) == 1)
  142. {
  143. // Found a number, if there was an 's' unit, count will be positive
  144. if (count > 0)
  145. {
  146. // Duration or delay was assigned
  147. if (!duration_found)
  148. {
  149. duration_found = true;
  150. animation.duration = number;
  151. }
  152. else if (!delay_found)
  153. {
  154. delay_found = true;
  155. animation.delay = number;
  156. }
  157. else
  158. return false;
  159. }
  160. else
  161. {
  162. // No 's' unit means num_iterations was found
  163. if (!num_iterations_found)
  164. {
  165. animation.num_iterations = Math::RoundToInteger(number);
  166. num_iterations_found = true;
  167. }
  168. else
  169. return false;
  170. }
  171. }
  172. else
  173. {
  174. // Must be an animation name
  175. animation.name = argument;
  176. }
  177. }
  178. }
  179. // Validate the parsed transition
  180. if (animation.name.empty() || animation.duration <= 0.0f || (animation.num_iterations < -1 || animation.num_iterations == 0))
  181. {
  182. return false;
  183. }
  184. animation_list.push_back(std::move(animation));
  185. }
  186. property = Property{ animation_list, Property::ANIMATION };
  187. return true;
  188. }
  189. static bool ParseTransition(Property & property, const StringList& transition_values)
  190. {
  191. TransitionList transition_list{ false, false, {} };
  192. for (const String& single_transition_value : transition_values)
  193. {
  194. Transition transition;
  195. PropertyNameList target_property_names;
  196. StringList arguments;
  197. StringUtilities::ExpandString(arguments, single_transition_value, ' ');
  198. bool duration_found = false;
  199. bool delay_found = false;
  200. bool reverse_adjustment_factor_found = false;
  201. for (auto& argument : arguments)
  202. {
  203. if (argument.empty())
  204. continue;
  205. // See if we have a <keyword> or <tween> specifier as defined in keywords
  206. auto it = keywords.find(argument);
  207. if (it != keywords.end() && it->second.ValidTransition())
  208. {
  209. if (it->second.type == Keyword::NONE)
  210. {
  211. if (transition_list.transitions.size() > 0) // The none keyword can not be part of multiple definitions
  212. return false;
  213. property = Property{ TransitionList{true, false, {}}, Property::TRANSITION };
  214. return true;
  215. }
  216. else if (it->second.type == Keyword::ALL)
  217. {
  218. if (transition_list.transitions.size() > 0) // The all keyword can not be part of multiple definitions
  219. return false;
  220. transition_list.all = true;
  221. }
  222. else if (it->second.type == Keyword::TWEEN)
  223. {
  224. transition.tween = it->second.tween;
  225. }
  226. }
  227. else
  228. {
  229. // Either <duration>, <delay> or a <property name>
  230. float number = 0.0f;
  231. int count = 0;
  232. if (sscanf(argument.c_str(), "%fs%n", &number, &count) == 1)
  233. {
  234. // Found a number, if there was an 's' unit, count will be positive
  235. if (count > 0)
  236. {
  237. // Duration or delay was assigned
  238. if (!duration_found)
  239. {
  240. duration_found = true;
  241. transition.duration = number;
  242. }
  243. else if (!delay_found)
  244. {
  245. delay_found = true;
  246. transition.delay = number;
  247. }
  248. else
  249. return false;
  250. }
  251. else
  252. {
  253. // No 's' unit means reverse adjustment factor was found
  254. if (!reverse_adjustment_factor_found)
  255. {
  256. reverse_adjustment_factor_found = true;
  257. transition.reverse_adjustment_factor = number;
  258. }
  259. else
  260. return false;
  261. }
  262. }
  263. else
  264. {
  265. // Must be a property name or shorthand, expand now
  266. if (auto shorthand = StyleSheetSpecification::GetShorthand(argument))
  267. {
  268. auto underlying_properties = StyleSheetSpecification::GetShorthandUnderlyingProperties(shorthand->id);
  269. target_property_names.insert(underlying_properties.begin(), underlying_properties.end());
  270. }
  271. else if (auto definition = StyleSheetSpecification::GetProperty(argument))
  272. {
  273. // Single property
  274. target_property_names.insert(definition->GetId());
  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.id = 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. }