PropertyParserAnimation.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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.value = std::move(animation_list);
  188. property.unit = Property::ANIMATION;
  189. return true;
  190. }
  191. static bool ParseTransition(Property & property, const StringList& transition_values)
  192. {
  193. TransitionList transition_list{ false, false, {} };
  194. for (const String& single_transition_value : transition_values)
  195. {
  196. Transition transition;
  197. PropertyIdSet target_property_names;
  198. StringList arguments;
  199. StringUtilities::ExpandString(arguments, single_transition_value, ' ');
  200. bool duration_found = false;
  201. bool delay_found = false;
  202. bool reverse_adjustment_factor_found = false;
  203. for (auto& argument : arguments)
  204. {
  205. if (argument.empty())
  206. continue;
  207. // See if we have a <keyword> or <tween> specifier as defined in keywords
  208. auto it = keywords.find(argument);
  209. if (it != keywords.end() && it->second.ValidTransition())
  210. {
  211. if (it->second.type == Keyword::NONE)
  212. {
  213. if (transition_list.transitions.size() > 0) // The none keyword can not be part of multiple definitions
  214. return false;
  215. property = Property{ TransitionList{true, false, {}}, Property::TRANSITION };
  216. return true;
  217. }
  218. else if (it->second.type == Keyword::ALL)
  219. {
  220. if (transition_list.transitions.size() > 0) // The all keyword can not be part of multiple definitions
  221. return false;
  222. transition_list.all = true;
  223. }
  224. else if (it->second.type == Keyword::TWEEN)
  225. {
  226. transition.tween = it->second.tween;
  227. }
  228. }
  229. else
  230. {
  231. // Either <duration>, <delay> or a <property name>
  232. float number = 0.0f;
  233. int count = 0;
  234. if (sscanf(argument.c_str(), "%fs%n", &number, &count) == 1)
  235. {
  236. // Found a number, if there was an 's' unit, count will be positive
  237. if (count > 0)
  238. {
  239. // Duration or delay was assigned
  240. if (!duration_found)
  241. {
  242. duration_found = true;
  243. transition.duration = number;
  244. }
  245. else if (!delay_found)
  246. {
  247. delay_found = true;
  248. transition.delay = number;
  249. }
  250. else
  251. return false;
  252. }
  253. else
  254. {
  255. // No 's' unit means reverse adjustment factor was found
  256. if (!reverse_adjustment_factor_found)
  257. {
  258. reverse_adjustment_factor_found = true;
  259. transition.reverse_adjustment_factor = number;
  260. }
  261. else
  262. return false;
  263. }
  264. }
  265. else
  266. {
  267. // Must be a property name or shorthand, expand now
  268. if (auto shorthand = StyleSheetSpecification::GetShorthand(argument))
  269. {
  270. PropertyIdSet underlying_properties = StyleSheetSpecification::GetShorthandUnderlyingProperties(shorthand->id);
  271. target_property_names |= underlying_properties;
  272. }
  273. else if (auto definition = StyleSheetSpecification::GetProperty(argument))
  274. {
  275. // Single property
  276. target_property_names.Insert(definition->GetId());
  277. }
  278. else
  279. {
  280. // Unknown property name
  281. return false;
  282. }
  283. }
  284. }
  285. }
  286. // Validate the parsed transition
  287. if (target_property_names.Empty() || transition.duration <= 0.0f || transition.reverse_adjustment_factor < 0.0f || transition.reverse_adjustment_factor > 1.0f
  288. || (transition_list.all && target_property_names.Size() != 1))
  289. {
  290. return false;
  291. }
  292. for (const auto& property_name : target_property_names)
  293. {
  294. transition.id = property_name;
  295. transition_list.transitions.push_back(transition);
  296. }
  297. }
  298. property.value = std::move(transition_list);
  299. property.unit = Property::TRANSITION;
  300. return true;
  301. }
  302. bool PropertyParserAnimation::ParseValue(Property & property, const String & value, const ParameterMap & parameters) const
  303. {
  304. StringList list_of_values;
  305. {
  306. auto lowercase_value = StringUtilities::ToLower(value);
  307. StringUtilities::ExpandString(list_of_values, lowercase_value, ',');
  308. }
  309. bool result = false;
  310. if (type == ANIMATION_PARSER)
  311. {
  312. result = ParseAnimation(property, list_of_values);
  313. }
  314. else if (type == TRANSITION_PARSER)
  315. {
  316. result = ParseTransition(property, list_of_values);
  317. }
  318. return result;
  319. }
  320. }
  321. }