PropertyParserAnimation.cpp 9.9 KB

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