TransformPrimitive.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2014 Markus Schöngart
  7. * Copyright (c) 2019 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "../../Include/RmlUi/Core/TransformPrimitive.h"
  29. #include "../../Include/RmlUi/Core/Element.h"
  30. #include "../../Include/RmlUi/Core/TypeConverter.h"
  31. namespace Rml {
  32. namespace Transforms {
  33. /// Returns the numeric value converted to 'base_unit'. Only accepts base units of 'Number' or 'Rad':
  34. /// 'Number' will pass-through the provided value.
  35. /// 'Rad' will convert {Rad, Deg, %} -> Rad.
  36. static float ResolvePrimitiveAbsoluteValue(NumericValue value, Property::Unit base_unit) noexcept
  37. {
  38. RMLUI_ASSERT(base_unit == Property::RAD || base_unit == Property::NUMBER);
  39. if (base_unit == Property::RAD)
  40. {
  41. switch (value.unit)
  42. {
  43. case Property::RAD:
  44. return value.number;
  45. case Property::DEG:
  46. return Math::DegreesToRadians(value.number);
  47. case Property::PERCENT:
  48. return value.number * 0.01f * 2.0f * Math::RMLUI_PI;
  49. default:
  50. Log::Message(Log::LT_WARNING, "Trying to pass a non-angle unit to a property expecting an angle.");
  51. }
  52. }
  53. else if (base_unit == Property::NUMBER && value.unit != Property::NUMBER)
  54. {
  55. Log::Message(Log::LT_WARNING, "A unit was passed to a property which expected a unit-less number.");
  56. }
  57. return value.number;
  58. }
  59. template<size_t N>
  60. inline ResolvedPrimitive<N>::ResolvedPrimitive(const float* values) noexcept
  61. {
  62. for (size_t i = 0; i < N; ++i)
  63. this->values[i] = values[i];
  64. }
  65. template<size_t N>
  66. inline ResolvedPrimitive<N>::ResolvedPrimitive(const NumericValue* values) noexcept
  67. {
  68. for (size_t i = 0; i < N; ++i)
  69. this->values[i] = values[i].number;
  70. }
  71. template<size_t N>
  72. inline ResolvedPrimitive<N>::ResolvedPrimitive(const NumericValue* values, Array<Property::Unit, N> base_units) noexcept
  73. {
  74. for (size_t i = 0; i < N; ++i)
  75. this->values[i] = ResolvePrimitiveAbsoluteValue(values[i], base_units[i]);
  76. }
  77. template<size_t N>
  78. inline ResolvedPrimitive<N>::ResolvedPrimitive(Array<NumericValue, N> values, Array<Property::Unit, N> base_units) noexcept
  79. {
  80. for (size_t i = 0; i < N; ++i)
  81. this->values[i] = ResolvePrimitiveAbsoluteValue(values[i], base_units[i]);
  82. }
  83. template<size_t N>
  84. inline ResolvedPrimitive<N>::ResolvedPrimitive(Array<float, N> values) noexcept : values(values) { }
  85. template<size_t N>
  86. inline UnresolvedPrimitive<N>::UnresolvedPrimitive(const NumericValue* values) noexcept
  87. {
  88. for (size_t i = 0; i < N; ++i)
  89. this->values[i] = values[i];
  90. }
  91. template<size_t N>
  92. inline UnresolvedPrimitive<N>::UnresolvedPrimitive(Array<NumericValue, N> values) noexcept : values(values) { }
  93. Matrix2D::Matrix2D(const NumericValue* values) noexcept : ResolvedPrimitive(values) { }
  94. Matrix3D::Matrix3D(const NumericValue* values) noexcept : ResolvedPrimitive(values) { }
  95. Matrix3D::Matrix3D(const Matrix4f& matrix) noexcept : ResolvedPrimitive(matrix.data()) { }
  96. TranslateX::TranslateX(const NumericValue* values) noexcept : UnresolvedPrimitive(values) { }
  97. TranslateX::TranslateX(float x, Property::Unit unit) noexcept : UnresolvedPrimitive({ NumericValue(x, unit) }) { }
  98. TranslateY::TranslateY(const NumericValue* values) noexcept : UnresolvedPrimitive(values) { }
  99. TranslateY::TranslateY(float y, Property::Unit unit) noexcept : UnresolvedPrimitive({ NumericValue(y, unit) }) { }
  100. TranslateZ::TranslateZ(const NumericValue* values) noexcept : UnresolvedPrimitive(values) { }
  101. TranslateZ::TranslateZ(float z, Property::Unit unit) noexcept : UnresolvedPrimitive({ NumericValue(z, unit) }) { }
  102. Translate2D::Translate2D(const NumericValue* values) noexcept : UnresolvedPrimitive(values) { }
  103. Translate2D::Translate2D(float x, float y, Property::Unit units) noexcept : UnresolvedPrimitive({ NumericValue(x, units), NumericValue(y, units) }) { }
  104. Translate3D::Translate3D(const NumericValue* values) noexcept : UnresolvedPrimitive(values) { }
  105. Translate3D::Translate3D(NumericValue x, NumericValue y, NumericValue z) noexcept : UnresolvedPrimitive({ x, y, z }) { }
  106. Translate3D::Translate3D(float x, float y, float z, Property::Unit units) noexcept
  107. : UnresolvedPrimitive({ NumericValue(x, units), NumericValue(y, units), NumericValue(z, units) }) { }
  108. ScaleX::ScaleX(const NumericValue* values) noexcept : ResolvedPrimitive(values) { }
  109. ScaleX::ScaleX(float value) noexcept : ResolvedPrimitive({ value }) { }
  110. ScaleY::ScaleY(const NumericValue* values) noexcept : ResolvedPrimitive(values) { }
  111. ScaleY::ScaleY(float value) noexcept : ResolvedPrimitive({ value }) { }
  112. ScaleZ::ScaleZ(const NumericValue* values) noexcept : ResolvedPrimitive(values) { }
  113. ScaleZ::ScaleZ(float value) noexcept : ResolvedPrimitive({ value }) { }
  114. Scale2D::Scale2D(const NumericValue* values) noexcept : ResolvedPrimitive(values) { }
  115. Scale2D::Scale2D(float xy) noexcept : ResolvedPrimitive({ xy, xy }) { }
  116. Scale2D::Scale2D(float x, float y) noexcept : ResolvedPrimitive({ x, y }) { }
  117. Scale3D::Scale3D(const NumericValue* values) noexcept : ResolvedPrimitive(values) { }
  118. Scale3D::Scale3D(float xyz) noexcept : ResolvedPrimitive({ xyz, xyz, xyz }) { }
  119. Scale3D::Scale3D(float x, float y, float z) noexcept : ResolvedPrimitive({ x, y, z }) { }
  120. RotateX::RotateX(const NumericValue* values) noexcept : ResolvedPrimitive(values, { Property::RAD }) { }
  121. RotateX::RotateX(float angle, Property::Unit unit) noexcept : ResolvedPrimitive({ NumericValue{ angle, unit } }, { Property::RAD }) { }
  122. RotateY::RotateY(const NumericValue* values) noexcept : ResolvedPrimitive(values, { Property::RAD }) {}
  123. RotateY::RotateY(float angle, Property::Unit unit) noexcept : ResolvedPrimitive({ NumericValue{ angle, unit } }, { Property::RAD }) { }
  124. RotateZ::RotateZ(const NumericValue* values) noexcept : ResolvedPrimitive(values, { Property::RAD }) { }
  125. RotateZ::RotateZ(float angle, Property::Unit unit) noexcept : ResolvedPrimitive({ NumericValue{ angle, unit } }, { Property::RAD }) { }
  126. Rotate2D::Rotate2D(const NumericValue* values) noexcept : ResolvedPrimitive(values, { Property::RAD }) { }
  127. Rotate2D::Rotate2D(float angle, Property::Unit unit) noexcept : ResolvedPrimitive({ NumericValue{ angle, unit } }, { Property::RAD }) { }
  128. Rotate3D::Rotate3D(const NumericValue* values) noexcept : ResolvedPrimitive(values, { Property::NUMBER, Property::NUMBER, Property::NUMBER, Property::RAD }) { }
  129. Rotate3D::Rotate3D(float x, float y, float z, float angle, Property::Unit angle_unit) noexcept
  130. : ResolvedPrimitive(
  131. { NumericValue{x, Property::NUMBER}, NumericValue{y, Property::NUMBER}, NumericValue{z, Property::NUMBER}, NumericValue{angle, angle_unit} },
  132. { Property::NUMBER, Property::NUMBER, Property::NUMBER, Property::RAD }
  133. )
  134. { }
  135. SkewX::SkewX(const NumericValue* values) noexcept : ResolvedPrimitive(values, { Property::RAD }) { }
  136. SkewX::SkewX(float angle, Property::Unit unit) noexcept : ResolvedPrimitive({ NumericValue{ angle, unit } }, { Property::RAD }) { }
  137. SkewY::SkewY(const NumericValue* values) noexcept : ResolvedPrimitive(values, { Property::RAD }) { }
  138. SkewY::SkewY(float angle, Property::Unit unit) noexcept : ResolvedPrimitive({ NumericValue{ angle, unit } }, { Property::RAD }) { }
  139. Skew2D::Skew2D(const NumericValue* values) noexcept : ResolvedPrimitive(values, { Property::RAD, Property::RAD }) { }
  140. Skew2D::Skew2D(float x, float y, Property::Unit unit) noexcept
  141. : ResolvedPrimitive({ NumericValue{ x, unit }, { NumericValue{ y, unit }} }, { Property::RAD, Property::RAD }) { }
  142. Perspective::Perspective(const NumericValue* values) noexcept : UnresolvedPrimitive(values) { }
  143. }
  144. } // namespace Rml