PropertyParserAnimation.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. }
  132. }
  133. else
  134. {
  135. // Either <duration>, <delay>, <num_iterations> or a <keyframes-name>
  136. float number = 0.0f;
  137. int count = 0;
  138. if (sscanf(argument.c_str(), "%fs%n", &number, &count) == 1)
  139. {
  140. // Found a number, if there was an 's' unit, count will be positive
  141. if (count > 0)
  142. {
  143. // Duration or delay was assigned
  144. if (!duration_found)
  145. {
  146. duration_found = true;
  147. animation.duration = number;
  148. }
  149. else if (!delay_found)
  150. {
  151. delay_found = true;
  152. animation.delay = number;
  153. }
  154. else
  155. return false;
  156. }
  157. else
  158. {
  159. // No 's' unit means num_iterations was found
  160. if (!num_iterations_found)
  161. {
  162. animation.num_iterations = Math::RoundToInteger(number);
  163. num_iterations_found = true;
  164. }
  165. else
  166. return false;
  167. }
  168. }
  169. else
  170. {
  171. // Must be an animation name
  172. animation.name = argument;
  173. }
  174. }
  175. }
  176. // Validate the parsed transition
  177. if (animation.name.empty() || animation.duration <= 0.0f || (animation.num_iterations < -1 || animation.num_iterations == 0))
  178. {
  179. return false;
  180. }
  181. animation_list.push_back(std::move(animation));
  182. }
  183. property = Property{ animation_list, Property::ANIMATION };
  184. return true;
  185. }
  186. static bool ParseTransition(Property & property, const StringList& transition_values)
  187. {
  188. TransitionList transition_list{ false, false, {} };
  189. for (const String& single_transition_value : transition_values)
  190. {
  191. Transition transition;
  192. PropertyIdList target_property_ids;
  193. StringList arguments;
  194. StringUtilities::ExpandString(arguments, single_transition_value, ' ');
  195. bool duration_found = false;
  196. bool delay_found = false;
  197. bool reverse_adjustment_factor_found = false;
  198. for (auto& argument : arguments)
  199. {
  200. if (argument.empty())
  201. continue;
  202. // See if we have a <keyword> or <tween> specifier as defined in keywords
  203. if (auto it = keywords.find(argument); it != keywords.end() && it->second.ValidTransition())
  204. {
  205. if (it->second.type == Keyword::NONE)
  206. {
  207. if (transition_list.transitions.size() > 0) // The none keyword can not be part of multiple definitions
  208. return false;
  209. property = Property{ TransitionList{true, false, {}}, Property::TRANSITION };
  210. return true;
  211. }
  212. else if (it->second.type == Keyword::ALL)
  213. {
  214. if (transition_list.transitions.size() > 0) // The all keyword can not be part of multiple definitions
  215. return false;
  216. transition_list.all = true;
  217. target_property_ids.insert(PropertyId::All);
  218. }
  219. else if (it->second.type == Keyword::TWEEN)
  220. {
  221. transition.tween = it->second.tween;
  222. }
  223. }
  224. else
  225. {
  226. // Either <duration>, <delay> or a <property name>
  227. float number = 0.0f;
  228. int count = 0;
  229. if (sscanf(argument.c_str(), "%fs%n", &number, &count) == 1)
  230. {
  231. // Found a number, if there was an 's' unit, count will be positive
  232. if (count > 0)
  233. {
  234. // Duration or delay was assigned
  235. if (!duration_found)
  236. {
  237. duration_found = true;
  238. transition.duration = number;
  239. }
  240. else if (!delay_found)
  241. {
  242. delay_found = true;
  243. transition.delay = number;
  244. }
  245. else
  246. return false;
  247. }
  248. else
  249. {
  250. // No 's' unit means reverse adjustment factor was found
  251. if (!reverse_adjustment_factor_found)
  252. {
  253. reverse_adjustment_factor_found = true;
  254. transition.reverse_adjustment_factor = number;
  255. }
  256. else
  257. return false;
  258. }
  259. }
  260. else
  261. {
  262. PropertyId id = GetPropertyId(argument);
  263. // Must be a property name or shorthand, expand now
  264. if (auto shorthand = StyleSheetSpecification::GetShorthand(id))
  265. {
  266. // For shorthands, add each underlying property separately
  267. for (const auto& property : shorthand->properties)
  268. target_property_ids.insert(property.first);
  269. }
  270. else if (auto definition = StyleSheetSpecification::GetProperty(id))
  271. {
  272. // Single property
  273. target_property_ids.insert(id);
  274. }
  275. else
  276. {
  277. // Unknown property name
  278. return false;
  279. }
  280. }
  281. }
  282. }
  283. // Validate the parsed transition
  284. if (target_property_ids.empty() || transition.duration <= 0.0f || transition.reverse_adjustment_factor < 0.0f || transition.reverse_adjustment_factor > 1.0f
  285. || (transition_list.all && target_property_ids.size() != 1))
  286. {
  287. return false;
  288. }
  289. for (const auto& id : target_property_ids)
  290. {
  291. if (id != PropertyId::Invalid)
  292. {
  293. transition.property_id = id;
  294. transition_list.transitions.push_back(transition);
  295. }
  296. }
  297. }
  298. property = Property{ transition_list, Property::TRANSITION };
  299. return true;
  300. }
  301. bool PropertyParserAnimation::ParseValue(Property & property, const String & value, const ParameterMap & parameters) const
  302. {
  303. StringList list_of_values;
  304. {
  305. auto lowercase_value = ToLower(value);
  306. StringUtilities::ExpandString(list_of_values, lowercase_value, ',');
  307. }
  308. bool result = false;
  309. if (type == ANIMATION_PARSER)
  310. {
  311. result = ParseAnimation(property, list_of_values);
  312. }
  313. else if (type == TRANSITION_PARSER)
  314. {
  315. result = ParseTransition(property, list_of_values);
  316. }
  317. return result;
  318. }
  319. void PropertyParserAnimation::Release()
  320. {
  321. delete this;
  322. }
  323. }
  324. }