TransformPrimitive.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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) 2014 Markus Schöngart
  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 "../../Include/Rocket/Core/TransformPrimitive.h"
  29. #include <iostream>
  30. namespace Rocket {
  31. namespace Core {
  32. namespace Transforms {
  33. NumericValue::NumericValue() throw()
  34. : number(), unit(Property::UNKNOWN)
  35. {
  36. }
  37. NumericValue::NumericValue(float number, Property::Unit unit) throw()
  38. : number(number), unit(unit)
  39. {
  40. }
  41. float NumericValue::Resolve(Element& e, float base) const throw()
  42. {
  43. Property prop;
  44. prop.value = Variant(number);
  45. prop.unit = unit;
  46. return e.ResolveProperty(&prop, base);
  47. }
  48. float NumericValue::ResolveWidth(Element& e) const throw()
  49. {
  50. return Resolve(e, e.GetBox().GetSize().x);
  51. }
  52. float NumericValue::ResolveHeight(Element& e) const throw()
  53. {
  54. return Resolve(e, e.GetBox().GetSize().y);
  55. }
  56. float NumericValue::ResolveDepth(Element& e) const throw()
  57. {
  58. Vector2f size = e.GetBox().GetSize();
  59. return Resolve(e, Math::Max(size.x, size.y));
  60. }
  61. struct ResolveTransformVisitor
  62. {
  63. Matrix4f& m;
  64. Element& e;
  65. bool operator()(const Matrix2D& p)
  66. {
  67. m = Matrix4f::FromRows(
  68. Vector4f(p.values[0], p.values[2], 0, p.values[4]),
  69. Vector4f(p.values[1], p.values[3], 0, p.values[5]),
  70. Vector4f(0, 0, 1, 0),
  71. Vector4f(0, 0, 0, 1)
  72. );
  73. return true;
  74. }
  75. bool operator()(const Matrix3D& p)
  76. {
  77. m = Matrix4f::FromRows(
  78. Vector4f(p.values[0], p.values[1], p.values[2], p.values[3]),
  79. Vector4f(p.values[4], p.values[5], p.values[6], p.values[7]),
  80. Vector4f(p.values[8], p.values[9], p.values[10], p.values[11]),
  81. Vector4f(p.values[12], p.values[13], p.values[14], p.values[15])
  82. );
  83. return true;
  84. }
  85. bool operator()(const TranslateX& p)
  86. {
  87. m = Matrix4f::TranslateX(p.values[0].ResolveWidth(e));
  88. return true;
  89. }
  90. bool operator()(const TranslateY& p)
  91. {
  92. m = Matrix4f::TranslateY(p.values[0].ResolveHeight(e));
  93. return true;
  94. }
  95. bool operator()(const TranslateZ& p)
  96. {
  97. m = Matrix4f::TranslateZ(p.values[0].ResolveDepth(e));
  98. return true;
  99. }
  100. bool operator()(const Translate2D& p)
  101. {
  102. m = Matrix4f::Translate(
  103. p.values[0].ResolveWidth(e),
  104. p.values[1].ResolveHeight(e),
  105. 0
  106. );
  107. return true;
  108. }
  109. bool operator()(const Translate3D& p)
  110. {
  111. m = Matrix4f::Translate(
  112. p.values[0].ResolveWidth(e),
  113. p.values[1].ResolveHeight(e),
  114. p.values[2].ResolveDepth(e)
  115. );
  116. return true;
  117. }
  118. bool operator()(const ScaleX& p)
  119. {
  120. m = Matrix4f::ScaleX(p.values[0]);
  121. return true;
  122. }
  123. bool operator()(const ScaleY& p)
  124. {
  125. m = Matrix4f::ScaleY(p.values[0]);
  126. return true;
  127. }
  128. bool operator()(const ScaleZ& p)
  129. {
  130. m = Matrix4f::ScaleZ(p.values[0]);
  131. return true;
  132. }
  133. bool operator()(const Scale2D& p)
  134. {
  135. m = Matrix4f::Scale(p.values[0], p.values[1], 1);
  136. return true;
  137. }
  138. bool operator()(const Scale3D& p)
  139. {
  140. m = Matrix4f::Scale(p.values[0], p.values[1], p.values[2]);
  141. return true;
  142. }
  143. bool operator()(const RotateX& p)
  144. {
  145. m = Matrix4f::RotateX(p.values[0]);
  146. return true;
  147. }
  148. bool operator()(const RotateY& p)
  149. {
  150. m = Matrix4f::RotateY(p.values[0]);
  151. return true;
  152. }
  153. bool operator()(const RotateZ& p)
  154. {
  155. m = Matrix4f::RotateZ(p.values[0]);
  156. return true;
  157. }
  158. bool operator()(const Rotate2D& p)
  159. {
  160. m = Matrix4f::RotateZ(p.values[0]);
  161. return true;
  162. }
  163. bool operator()(const Rotate3D& p)
  164. {
  165. m = Matrix4f::Rotate(Vector3f(p.values[0], p.values[1], p.values[2]), p.values[3]);
  166. return true;
  167. }
  168. bool operator()(const SkewX& p)
  169. {
  170. m = Matrix4f::SkewX(p.values[0]);
  171. return true;
  172. }
  173. bool operator()(const SkewY& p)
  174. {
  175. m = Matrix4f::SkewY(p.values[0]);
  176. return true;
  177. }
  178. bool operator()(const Skew2D& p)
  179. {
  180. m = Matrix4f::Skew(p.values[0], p.values[1]);
  181. return true;
  182. }
  183. bool operator()(const Perspective& p)
  184. {
  185. return false;
  186. }
  187. };
  188. struct SetIdentityVisitor
  189. {
  190. template <size_t N>
  191. void operator()(ResolvedValuesPrimitive<N>& p)
  192. {
  193. for (auto& value : p.values)
  194. value = 0.0f;
  195. }
  196. template <size_t N>
  197. void operator()(UnresolvedValuesPrimitive<N>& p)
  198. {
  199. for (auto& value : p.values)
  200. value.number = 0.0f;
  201. }
  202. void operator()(Matrix2D& p)
  203. {
  204. for (int i = 0; i < 6; i++)
  205. p.values[i] = ((i == 0 || i == 3) ? 1.0f : 0.0f);
  206. }
  207. void operator()(Matrix3D& p)
  208. {
  209. for (int i = 0; i < 16; i++)
  210. p.values[i] = ((i % 5) == 0 ? 1.0f : 0.0f);
  211. }
  212. void operator()(ScaleX& p)
  213. {
  214. p.values[0] = 1;
  215. }
  216. void operator()(ScaleY& p)
  217. {
  218. p.values[0] = 1;
  219. }
  220. void operator()(ScaleZ& p)
  221. {
  222. p.values[0] = 1;
  223. }
  224. void operator()(Scale2D& p)
  225. {
  226. p.values[0] = p.values[1] = 1;
  227. }
  228. void operator()(Scale3D& p)
  229. {
  230. p.values[0] = p.values[1] = p.values[2] = 1;
  231. }
  232. };
  233. void Primitive::SetIdentity() throw()
  234. {
  235. std::visit(SetIdentityVisitor{}, primitive);
  236. }
  237. bool Primitive::ResolveTransform(Matrix4f & m, Element & e) const throw()
  238. {
  239. ResolveTransformVisitor visitor{ m, e };
  240. bool result = std::visit(visitor, primitive);
  241. return result;
  242. }
  243. bool Primitive::ResolvePerspective(float & p, Element & e) const throw()
  244. {
  245. bool result = false;
  246. if (const Perspective* perspective = std::get_if<Perspective>(&primitive))
  247. {
  248. p = perspective->values[0].ResolveDepth(e);
  249. result = true;
  250. }
  251. return result;
  252. }
  253. struct InterpolateVisitor
  254. {
  255. const PrimitiveVariant& other_variant;
  256. float alpha;
  257. template <size_t N>
  258. void Interpolate(ResolvedValuesPrimitive<N>& p0, const ResolvedValuesPrimitive<N>& p1)
  259. {
  260. for (size_t i = 0; i < N; i++)
  261. p0.values[i] = p0.values[i] * (1.0f - alpha) + p1.values[i] * alpha;
  262. }
  263. template <size_t N>
  264. void Interpolate(UnresolvedValuesPrimitive<N>& p0, const UnresolvedValuesPrimitive<N>& p1)
  265. {
  266. // TODO: While we have the same type and dimension, we may have different units. Should convert?
  267. for (size_t i = 0; i < N; i++)
  268. p0.values[i].number = p0.values[i].number*(1.0f - alpha) + p1.values[i].number * alpha;
  269. }
  270. //void Interpolate(Matrix3D& p0, Matrix3D& p1)
  271. //{
  272. // // Special interpolation for full matrices TODO
  273. //}
  274. template <typename T>
  275. void operator()(T& p0)
  276. {
  277. auto& p1 = std::get<T>(other_variant);
  278. Interpolate(p0, p1);
  279. }
  280. };
  281. bool Primitive::InterpolateWith(const Primitive & other, float alpha) throw()
  282. {
  283. if (primitive.index() != other.primitive.index())
  284. return false;
  285. std::visit(InterpolateVisitor{ other.primitive, alpha }, primitive);
  286. return true;
  287. }
  288. }
  289. }
  290. }