PropertyParserTransition.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 "PropertyParserTransition.h"
  29. #include "../../Include/Rocket/Core/StringUtilities.h"
  30. namespace Rocket {
  31. namespace Core {
  32. struct TransitionSpec {
  33. enum Type { KEYWORD_NONE, KEYWORD_ALL, TWEEN } type;
  34. Tween tween;
  35. TransitionSpec(Tween tween) : type(TWEEN), tween(tween) {}
  36. TransitionSpec(Type type) : type(type) {}
  37. };
  38. static const std::unordered_map<String, TransitionSpec> transition_spec = {
  39. {"none", {TransitionSpec::KEYWORD_NONE} },
  40. {"all", {TransitionSpec::KEYWORD_ALL}},
  41. {"back-in", {Tween{Tween::Back, Tween::In}}},
  42. {"back-out", {Tween{Tween::Back, Tween::Out}}},
  43. {"back-in-out", {Tween{Tween::Back, Tween::InOut}}},
  44. {"bounce-in", {Tween{Tween::Bounce, Tween::In}}},
  45. {"bounce-out", {Tween{Tween::Bounce, Tween::Out}}},
  46. {"bounce-in-out", {Tween{Tween::Bounce, Tween::InOut}}},
  47. {"circular-in", {Tween{Tween::Circular, Tween::In}}},
  48. {"circular-out", {Tween{Tween::Circular, Tween::Out}}},
  49. {"circular-in-out", {Tween{Tween::Circular, Tween::InOut}}},
  50. {"cubic-in", {Tween{Tween::Cubic, Tween::In}}},
  51. {"cubic-out", {Tween{Tween::Cubic, Tween::Out}}},
  52. {"cubic-in-out", {Tween{Tween::Cubic, Tween::InOut}}},
  53. {"elastic-in", {Tween{Tween::Elastic, Tween::In}}},
  54. {"elastic-out", {Tween{Tween::Elastic, Tween::Out}}},
  55. {"elastic-in-out", {Tween{Tween::Elastic, Tween::InOut}}},
  56. {"exponential-in", {Tween{Tween::Exponential, Tween::In}}},
  57. {"exponential-out", {Tween{Tween::Exponential, Tween::Out}}},
  58. {"exponential-in-out", {Tween{Tween::Exponential, Tween::InOut}}},
  59. {"linear-in", {Tween{Tween::Linear, Tween::In}}},
  60. {"linear-out", {Tween{Tween::Linear, Tween::Out}}},
  61. {"linear-in-out", {Tween{Tween::Linear, Tween::InOut}}},
  62. {"quadratic-in", {Tween{Tween::Quadratic, Tween::In}}},
  63. {"quadratic-out", {Tween{Tween::Quadratic, Tween::Out}}},
  64. {"quadratic-in-out", {Tween{Tween::Quadratic, Tween::InOut}}},
  65. {"quartic-in", {Tween{Tween::Quartic, Tween::In}}},
  66. {"quartic-out", {Tween{Tween::Quartic, Tween::Out}}},
  67. {"quartic-in-out", {Tween{Tween::Quartic, Tween::InOut}}},
  68. {"quintic-in", {Tween{Tween::Quintic, Tween::In}}},
  69. {"quintic-out", {Tween{Tween::Quintic, Tween::Out}}},
  70. {"quintic-in-out", {Tween{Tween::Quintic, Tween::InOut}}},
  71. {"sine-in", {Tween{Tween::Sine, Tween::In}}},
  72. {"sine-out", {Tween{Tween::Sine, Tween::Out}}},
  73. {"sine-in-out", {Tween{Tween::Sine, Tween::InOut}}},
  74. };
  75. PropertyParserTransition::PropertyParserTransition()
  76. {
  77. }
  78. bool PropertyParserTransition::ParseValue(Property & property, const String & value, const ParameterMap & parameters) const
  79. {
  80. StringList list_of_properties;
  81. {
  82. auto lowercase_value = value.ToLower();
  83. StringUtilities::ExpandString(list_of_properties, lowercase_value, ',');
  84. }
  85. TransitionList transition_list{ false, false, {} };
  86. for (const String& single_property : list_of_properties)
  87. {
  88. Transition transition;
  89. StringList arguments;
  90. StringUtilities::ExpandString(arguments, single_property, ' ');
  91. bool duration_found = false;
  92. bool delay_found = false;
  93. for (auto& argument : arguments)
  94. {
  95. if (argument.Empty())
  96. continue;
  97. // See if we have a <keyword> or <tween> specifier as defined in transition_spec
  98. if (auto it = transition_spec.find(argument); it != transition_spec.end())
  99. {
  100. if (it->second.type == TransitionSpec::KEYWORD_NONE)
  101. {
  102. if (transition_list.transitions.size() > 0) // The none keyword can not be part of multiple definitions
  103. return false;
  104. property = Property{ TransitionList{true, false, {}}, Property::TRANSITION };
  105. return true;
  106. }
  107. else if (it->second.type == TransitionSpec::KEYWORD_ALL)
  108. {
  109. if (transition_list.transitions.size() > 0) // The all keyword can not be part of multiple definitions
  110. return false;
  111. transition_list.all = true;
  112. transition.name = "all";
  113. }
  114. else if (it->second.type == TransitionSpec::TWEEN)
  115. {
  116. transition.tween = it->second.tween;
  117. }
  118. }
  119. else
  120. {
  121. // Either <duration>, <delay> or a <property name>
  122. float* time_target = (duration_found ? &transition.delay : &transition.duration);
  123. if (sscanf(argument.CString(), "%fs", time_target) == 1)
  124. {
  125. // Duration or delay was assigned
  126. if (!duration_found)
  127. duration_found = true;
  128. else if (!delay_found)
  129. delay_found = true;
  130. else
  131. return false;
  132. }
  133. else
  134. {
  135. // Must be a property name (unless its invalid, we don't actually validate it)
  136. if (!transition.name.Empty())
  137. return false;
  138. transition.name = argument;
  139. }
  140. }
  141. }
  142. // Validate the parsed transition
  143. if (transition.name.Length() == 0 || transition.duration <= 0.0f)
  144. {
  145. return false;
  146. }
  147. transition_list.transitions.push_back(transition);
  148. }
  149. property = Property{ transition_list, Property::TRANSITION };
  150. return true;
  151. }
  152. void PropertyParserTransition::Release()
  153. {
  154. delete this;
  155. }
  156. }
  157. }