TransformPrimitive.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. bool Matrix2D::ResolveTransform(Matrix4f& m, Element& e) const throw()
  62. {
  63. m = Matrix4f::FromRows(
  64. Vector4f(values[0], values[1], 0, values[2]),
  65. Vector4f(values[3], values[4], 0, values[5]),
  66. Vector4f( 0, 0, 1, 0),
  67. Vector4f( 0, 0, 0, 1)
  68. );
  69. return true;
  70. }
  71. bool Matrix3D::ResolveTransform(Matrix4f& m, Element& e) const throw()
  72. {
  73. m = Matrix4f::FromRows(
  74. Vector4f(values[ 0], values[ 1], values[ 2], values[ 3]),
  75. Vector4f(values[ 4], values[ 5], values[ 6], values[ 7]),
  76. Vector4f(values[ 8], values[ 9], values[10], values[11]),
  77. Vector4f(values[12], values[13], values[14], values[15])
  78. );
  79. return true;
  80. }
  81. bool TranslateX::ResolveTransform(Matrix4f& m, Element& e) const throw()
  82. {
  83. m = Matrix4f::TranslateX(values[0].ResolveWidth(e));
  84. return true;
  85. }
  86. bool TranslateY::ResolveTransform(Matrix4f& m, Element& e) const throw()
  87. {
  88. m = Matrix4f::TranslateY(values[0].ResolveHeight(e));
  89. return true;
  90. }
  91. bool TranslateZ::ResolveTransform(Matrix4f& m, Element& e) const throw()
  92. {
  93. m = Matrix4f::TranslateZ(values[0].ResolveDepth(e));
  94. return true;
  95. }
  96. bool Translate2D::ResolveTransform(Matrix4f& m, Element& e) const throw()
  97. {
  98. m = Matrix4f::Translate(
  99. values[0].ResolveWidth(e),
  100. values[1].ResolveHeight(e),
  101. 0
  102. );
  103. return true;
  104. }
  105. bool Translate3D::ResolveTransform(Matrix4f& m, Element& e) const throw()
  106. {
  107. m = Matrix4f::Translate(
  108. values[0].ResolveWidth(e),
  109. values[1].ResolveHeight(e),
  110. values[2].ResolveDepth(e)
  111. );
  112. return true;
  113. }
  114. bool ScaleX::ResolveTransform(Matrix4f& m, Element& e) const throw()
  115. {
  116. m = Matrix4f::ScaleX(values[0]);
  117. return true;
  118. }
  119. bool ScaleY::ResolveTransform(Matrix4f& m, Element& e) const throw()
  120. {
  121. m = Matrix4f::ScaleY(values[0]);
  122. return true;
  123. }
  124. bool ScaleZ::ResolveTransform(Matrix4f& m, Element& e) const throw()
  125. {
  126. m = Matrix4f::ScaleZ(values[0]);
  127. return true;
  128. }
  129. bool Scale2D::ResolveTransform(Matrix4f& m, Element& e) const throw()
  130. {
  131. m = Matrix4f::Scale(values[0], values[1], 1);
  132. return true;
  133. }
  134. bool Scale3D::ResolveTransform(Matrix4f& m, Element& e) const throw()
  135. {
  136. m = Matrix4f::Scale(values[0], values[1], values[2]);
  137. return true;
  138. }
  139. bool RotateX::ResolveTransform(Matrix4f& m, Element& e) const throw()
  140. {
  141. m = Matrix4f::RotateX(values[0]);
  142. return true;
  143. }
  144. bool RotateY::ResolveTransform(Matrix4f& m, Element& e) const throw()
  145. {
  146. m = Matrix4f::RotateY(values[0]);
  147. return true;
  148. }
  149. bool RotateZ::ResolveTransform(Matrix4f& m, Element& e) const throw()
  150. {
  151. m = Matrix4f::RotateZ(values[0]);
  152. return true;
  153. }
  154. bool Rotate2D::ResolveTransform(Matrix4f& m, Element& e) const throw()
  155. {
  156. m = Matrix4f::RotateZ(values[0]);
  157. return true;
  158. }
  159. bool Rotate3D::ResolveTransform(Matrix4f& m, Element& e) const throw()
  160. {
  161. m = Matrix4f::Rotate(Vector3f(values[0], values[1], values[2]), values[3]);
  162. return true;
  163. }
  164. bool SkewX::ResolveTransform(Matrix4f& m, Element& e) const throw()
  165. {
  166. m = Matrix4f::SkewX(values[0]);
  167. return true;
  168. }
  169. bool SkewY::ResolveTransform(Matrix4f& m, Element& e) const throw()
  170. {
  171. m = Matrix4f::SkewY(values[0]);
  172. return true;
  173. }
  174. bool Skew2D::ResolveTransform(Matrix4f& m, Element& e) const throw()
  175. {
  176. m = Matrix4f::Skew(values[0], values[1]);
  177. return true;
  178. }
  179. bool Perspective::ResolvePerspective(float& p, Element& e) const throw()
  180. {
  181. p = values[0].ResolveDepth(e);
  182. return true;
  183. }
  184. }
  185. }
  186. }