TransformPrimitive.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. if(unit & (Property::PX | Property::NUMBER)) return number;
  51. return Resolve(e, e.GetBox().GetSize().x);
  52. }
  53. float NumericValue::ResolveHeight(Element& e) const throw()
  54. {
  55. if (unit & (Property::PX | Property::NUMBER)) return number;
  56. return Resolve(e, e.GetBox().GetSize().y);
  57. }
  58. float NumericValue::ResolveDepth(Element& e) const throw()
  59. {
  60. if (unit & (Property::PX | Property::NUMBER)) return number;
  61. Vector2f size = e.GetBox().GetSize();
  62. return Resolve(e, Math::Max(size.x, size.y));
  63. }
  64. float NumericValue::ResolveAbsoluteUnit(Property::Unit base_unit) const throw()
  65. {
  66. switch (base_unit)
  67. {
  68. case Property::RAD:
  69. {
  70. switch (unit)
  71. {
  72. case Property::NUMBER:
  73. case Property::DEG:
  74. return Math::DegreesToRadians(number);
  75. case Property::RAD:
  76. return number;
  77. case Property::PERCENT:
  78. return number * 0.01f * 2.0f * Math::ROCKET_PI;
  79. break;
  80. }
  81. }
  82. }
  83. return number;
  84. }
  85. struct ResolveTransformVisitor
  86. {
  87. Matrix4f& m;
  88. Element& e;
  89. bool operator()(const Matrix2D& p)
  90. {
  91. m = Matrix4f::FromRows(
  92. Vector4f(p.values[0], p.values[2], 0, p.values[4]),
  93. Vector4f(p.values[1], p.values[3], 0, p.values[5]),
  94. Vector4f(0, 0, 1, 0),
  95. Vector4f(0, 0, 0, 1)
  96. );
  97. return true;
  98. }
  99. bool operator()(const Matrix3D& p)
  100. {
  101. m = Matrix4f::FromRows(
  102. Vector4f(p.values[0], p.values[1], p.values[2], p.values[3]),
  103. Vector4f(p.values[4], p.values[5], p.values[6], p.values[7]),
  104. Vector4f(p.values[8], p.values[9], p.values[10], p.values[11]),
  105. Vector4f(p.values[12], p.values[13], p.values[14], p.values[15])
  106. );
  107. return true;
  108. }
  109. bool operator()(const TranslateX& p)
  110. {
  111. m = Matrix4f::TranslateX(p.values[0].ResolveWidth(e));
  112. return true;
  113. }
  114. bool operator()(const TranslateY& p)
  115. {
  116. m = Matrix4f::TranslateY(p.values[0].ResolveHeight(e));
  117. return true;
  118. }
  119. bool operator()(const TranslateZ& p)
  120. {
  121. m = Matrix4f::TranslateZ(p.values[0].ResolveDepth(e));
  122. return true;
  123. }
  124. bool operator()(const Translate2D& p)
  125. {
  126. m = Matrix4f::Translate(
  127. p.values[0].ResolveWidth(e),
  128. p.values[1].ResolveHeight(e),
  129. 0
  130. );
  131. return true;
  132. }
  133. bool operator()(const Translate3D& p)
  134. {
  135. m = Matrix4f::Translate(
  136. p.values[0].ResolveWidth(e),
  137. p.values[1].ResolveHeight(e),
  138. p.values[2].ResolveDepth(e)
  139. );
  140. return true;
  141. }
  142. bool operator()(const ScaleX& p)
  143. {
  144. m = Matrix4f::ScaleX(p.values[0]);
  145. return true;
  146. }
  147. bool operator()(const ScaleY& p)
  148. {
  149. m = Matrix4f::ScaleY(p.values[0]);
  150. return true;
  151. }
  152. bool operator()(const ScaleZ& p)
  153. {
  154. m = Matrix4f::ScaleZ(p.values[0]);
  155. return true;
  156. }
  157. bool operator()(const Scale2D& p)
  158. {
  159. m = Matrix4f::Scale(p.values[0], p.values[1], 1);
  160. return true;
  161. }
  162. bool operator()(const Scale3D& p)
  163. {
  164. m = Matrix4f::Scale(p.values[0], p.values[1], p.values[2]);
  165. return true;
  166. }
  167. bool operator()(const RotateX& p)
  168. {
  169. m = Matrix4f::RotateX(p.values[0]);
  170. return true;
  171. }
  172. bool operator()(const RotateY& p)
  173. {
  174. m = Matrix4f::RotateY(p.values[0]);
  175. return true;
  176. }
  177. bool operator()(const RotateZ& p)
  178. {
  179. m = Matrix4f::RotateZ(p.values[0]);
  180. return true;
  181. }
  182. bool operator()(const Rotate2D& p)
  183. {
  184. m = Matrix4f::RotateZ(p.values[0]);
  185. return true;
  186. }
  187. bool operator()(const Rotate3D& p)
  188. {
  189. m = Matrix4f::Rotate(Vector3f(p.values[0], p.values[1], p.values[2]), p.values[3]);
  190. return true;
  191. }
  192. bool operator()(const SkewX& p)
  193. {
  194. m = Matrix4f::SkewX(p.values[0]);
  195. return true;
  196. }
  197. bool operator()(const SkewY& p)
  198. {
  199. m = Matrix4f::SkewY(p.values[0]);
  200. return true;
  201. }
  202. bool operator()(const Skew2D& p)
  203. {
  204. m = Matrix4f::Skew(p.values[0], p.values[1]);
  205. return true;
  206. }
  207. bool operator()(const Perspective& p)
  208. {
  209. return false;
  210. }
  211. };
  212. struct SetIdentityVisitor
  213. {
  214. template <size_t N>
  215. void operator()(ResolvedValuesPrimitive<N>& p)
  216. {
  217. for (auto& value : p.values)
  218. value = 0.0f;
  219. }
  220. template <size_t N>
  221. void operator()(UnresolvedValuesPrimitive<N>& p)
  222. {
  223. for (auto& value : p.values)
  224. value.number = 0.0f;
  225. }
  226. void operator()(Matrix2D& p)
  227. {
  228. for (int i = 0; i < 6; i++)
  229. p.values[i] = ((i == 0 || i == 3) ? 1.0f : 0.0f);
  230. }
  231. void operator()(Matrix3D& p)
  232. {
  233. for (int i = 0; i < 16; i++)
  234. p.values[i] = ((i % 5) == 0 ? 1.0f : 0.0f);
  235. }
  236. void operator()(ScaleX& p)
  237. {
  238. p.values[0] = 1;
  239. }
  240. void operator()(ScaleY& p)
  241. {
  242. p.values[0] = 1;
  243. }
  244. void operator()(ScaleZ& p)
  245. {
  246. p.values[0] = 1;
  247. }
  248. void operator()(Scale2D& p)
  249. {
  250. p.values[0] = p.values[1] = 1;
  251. }
  252. void operator()(Scale3D& p)
  253. {
  254. p.values[0] = p.values[1] = p.values[2] = 1;
  255. }
  256. };
  257. void Primitive::SetIdentity() throw()
  258. {
  259. std::visit(SetIdentityVisitor{}, primitive);
  260. }
  261. bool Primitive::ResolveTransform(Matrix4f & m, Element & e) const throw()
  262. {
  263. ResolveTransformVisitor visitor{ m, e };
  264. bool result = std::visit(visitor, primitive);
  265. return result;
  266. }
  267. bool Primitive::ResolvePerspective(float & p, Element & e) const throw()
  268. {
  269. bool result = false;
  270. if (const Perspective* perspective = std::get_if<Perspective>(&primitive))
  271. {
  272. p = perspective->values[0].ResolveDepth(e);
  273. result = true;
  274. }
  275. return result;
  276. }
  277. struct InterpolateVisitor
  278. {
  279. const PrimitiveVariant& other_variant;
  280. float alpha;
  281. template <size_t N>
  282. void Interpolate(ResolvedValuesPrimitive<N>& p0, const ResolvedValuesPrimitive<N>& p1)
  283. {
  284. for (size_t i = 0; i < N; i++)
  285. p0.values[i] = p0.values[i] * (1.0f - alpha) + p1.values[i] * alpha;
  286. }
  287. template <size_t N>
  288. void Interpolate(UnresolvedValuesPrimitive<N>& p0, const UnresolvedValuesPrimitive<N>& p1)
  289. {
  290. // Assumes that the underlying units have been resolved (e.g. to pixels)
  291. for (size_t i = 0; i < N; i++)
  292. p0.values[i].number = p0.values[i].number*(1.0f - alpha) + p1.values[i].number * alpha;
  293. }
  294. //void Interpolate(Matrix3D& p0, Matrix3D& p1)
  295. //{
  296. // // Special interpolation for full matrices TODO
  297. //}
  298. template <typename T>
  299. void operator()(T& p0)
  300. {
  301. auto& p1 = std::get<T>(other_variant);
  302. Interpolate(p0, p1);
  303. }
  304. };
  305. bool Primitive::InterpolateWith(const Primitive & other, float alpha) throw()
  306. {
  307. if (primitive.index() != other.primitive.index())
  308. return false;
  309. std::visit(InterpolateVisitor{ other.primitive, alpha }, primitive);
  310. return true;
  311. }
  312. struct ResolveUnitsVisitor
  313. {
  314. Element& e;
  315. bool operator()(TranslateX& p)
  316. {
  317. p.values[0] = NumericValue{ p.values[0].ResolveWidth(e), Property::PX };
  318. return true;
  319. }
  320. bool operator()(TranslateY& p)
  321. {
  322. p.values[0] = NumericValue{ p.values[0].ResolveHeight(e), Property::PX };
  323. return true;
  324. }
  325. bool operator()(TranslateZ& p)
  326. {
  327. p.values[0] = NumericValue{ p.values[0].ResolveDepth(e), Property::PX };
  328. return true;
  329. }
  330. bool operator()(Translate2D& p)
  331. {
  332. p.values[0] = NumericValue{ p.values[0].ResolveWidth(e), Property::PX };
  333. p.values[1] = NumericValue{ p.values[1].ResolveHeight(e), Property::PX };
  334. return true;
  335. }
  336. bool operator()(Translate3D& p)
  337. {
  338. p.values[0] = NumericValue{ p.values[0].ResolveWidth(e), Property::PX };
  339. p.values[1] = NumericValue{ p.values[1].ResolveHeight(e), Property::PX };
  340. p.values[2] = NumericValue{ p.values[2].ResolveDepth(e), Property::PX };
  341. return true;
  342. }
  343. template <size_t N>
  344. bool operator()(ResolvedValuesPrimitive<N>& p)
  345. {
  346. // No conversion needed for resolved transforms
  347. return true;
  348. }
  349. bool operator()(Perspective& p)
  350. {
  351. // Perspective is special and not used for transform animations, ignore.
  352. return false;
  353. }
  354. };
  355. bool Primitive::ResolveUnits(Element & e) throw()
  356. {
  357. return std::visit(ResolveUnitsVisitor{ e }, primitive);
  358. }
  359. }
  360. }
  361. }