TransformPrimitive.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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-2023 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, Unit base_unit) noexcept
  37. {
  38. RMLUI_ASSERT(base_unit == Unit::RAD || base_unit == Unit::NUMBER);
  39. if (base_unit == Unit::RAD)
  40. {
  41. switch (value.unit)
  42. {
  43. case Unit::RAD: return value.number;
  44. case Unit::DEG: return Math::DegreesToRadians(value.number);
  45. case Unit::PERCENT: return value.number * 0.01f * 2.0f * Math::RMLUI_PI;
  46. default: Log::Message(Log::LT_WARNING, "Trying to pass a non-angle unit to a property expecting an angle.");
  47. }
  48. }
  49. else if (base_unit == Unit::NUMBER && value.unit != Unit::NUMBER)
  50. {
  51. Log::Message(Log::LT_WARNING, "A unit was passed to a property which expected a unit-less number.");
  52. }
  53. return value.number;
  54. }
  55. template <size_t N>
  56. inline ResolvedPrimitive<N>::ResolvedPrimitive(const float* values) noexcept
  57. {
  58. for (size_t i = 0; i < N; ++i)
  59. this->values[i] = values[i];
  60. }
  61. template <size_t N>
  62. inline ResolvedPrimitive<N>::ResolvedPrimitive(const NumericValue* values) noexcept
  63. {
  64. for (size_t i = 0; i < N; ++i)
  65. this->values[i] = values[i].number;
  66. }
  67. template <size_t N>
  68. inline ResolvedPrimitive<N>::ResolvedPrimitive(const NumericValue* values, Array<Unit, N> base_units) noexcept
  69. {
  70. for (size_t i = 0; i < N; ++i)
  71. this->values[i] = ResolvePrimitiveAbsoluteValue(values[i], base_units[i]);
  72. }
  73. template <size_t N>
  74. inline ResolvedPrimitive<N>::ResolvedPrimitive(Array<NumericValue, N> values, Array<Unit, N> base_units) noexcept
  75. {
  76. for (size_t i = 0; i < N; ++i)
  77. this->values[i] = ResolvePrimitiveAbsoluteValue(values[i], base_units[i]);
  78. }
  79. template <size_t N>
  80. inline ResolvedPrimitive<N>::ResolvedPrimitive(Array<float, N> values) noexcept : values(values)
  81. {}
  82. template <size_t N>
  83. inline UnresolvedPrimitive<N>::UnresolvedPrimitive(const NumericValue* values) noexcept
  84. {
  85. for (size_t i = 0; i < N; ++i)
  86. this->values[i] = values[i];
  87. }
  88. template <size_t N>
  89. inline UnresolvedPrimitive<N>::UnresolvedPrimitive(Array<NumericValue, N> values) noexcept : values(values)
  90. {}
  91. Matrix2D::Matrix2D(const NumericValue* values) noexcept : ResolvedPrimitive(values) {}
  92. Matrix3D::Matrix3D(const NumericValue* values) noexcept : ResolvedPrimitive(values) {}
  93. Matrix3D::Matrix3D(const Matrix4f& matrix) noexcept : ResolvedPrimitive(matrix.data()) {}
  94. TranslateX::TranslateX(const NumericValue* values) noexcept : UnresolvedPrimitive(values) {}
  95. TranslateX::TranslateX(float x, Unit unit) noexcept : UnresolvedPrimitive({NumericValue(x, unit)}) {}
  96. TranslateY::TranslateY(const NumericValue* values) noexcept : UnresolvedPrimitive(values) {}
  97. TranslateY::TranslateY(float y, Unit unit) noexcept : UnresolvedPrimitive({NumericValue(y, unit)}) {}
  98. TranslateZ::TranslateZ(const NumericValue* values) noexcept : UnresolvedPrimitive(values) {}
  99. TranslateZ::TranslateZ(float z, Unit unit) noexcept : UnresolvedPrimitive({NumericValue(z, unit)}) {}
  100. Translate2D::Translate2D(const NumericValue* values) noexcept : UnresolvedPrimitive(values) {}
  101. Translate2D::Translate2D(float x, float y, Unit units) noexcept : UnresolvedPrimitive({NumericValue(x, units), NumericValue(y, units)}) {}
  102. Translate3D::Translate3D(const NumericValue* values) noexcept : UnresolvedPrimitive(values) {}
  103. Translate3D::Translate3D(NumericValue x, NumericValue y, NumericValue z) noexcept : UnresolvedPrimitive({x, y, z}) {}
  104. Translate3D::Translate3D(float x, float y, float z, Unit units) noexcept :
  105. UnresolvedPrimitive({NumericValue(x, units), NumericValue(y, units), NumericValue(z, units)})
  106. {}
  107. ScaleX::ScaleX(const NumericValue* values) noexcept : ResolvedPrimitive(values) {}
  108. ScaleX::ScaleX(float value) noexcept : ResolvedPrimitive({value}) {}
  109. ScaleY::ScaleY(const NumericValue* values) noexcept : ResolvedPrimitive(values) {}
  110. ScaleY::ScaleY(float value) noexcept : ResolvedPrimitive({value}) {}
  111. ScaleZ::ScaleZ(const NumericValue* values) noexcept : ResolvedPrimitive(values) {}
  112. ScaleZ::ScaleZ(float value) noexcept : ResolvedPrimitive({value}) {}
  113. Scale2D::Scale2D(const NumericValue* values) noexcept : ResolvedPrimitive(values) {}
  114. Scale2D::Scale2D(float xy) noexcept : ResolvedPrimitive({xy, xy}) {}
  115. Scale2D::Scale2D(float x, float y) noexcept : ResolvedPrimitive({x, y}) {}
  116. Scale3D::Scale3D(const NumericValue* values) noexcept : ResolvedPrimitive(values) {}
  117. Scale3D::Scale3D(float xyz) noexcept : ResolvedPrimitive({xyz, xyz, xyz}) {}
  118. Scale3D::Scale3D(float x, float y, float z) noexcept : ResolvedPrimitive({x, y, z}) {}
  119. RotateX::RotateX(const NumericValue* values) noexcept : ResolvedPrimitive(values, {Unit::RAD}) {}
  120. RotateX::RotateX(float angle, Unit unit) noexcept : ResolvedPrimitive({NumericValue{angle, unit}}, {Unit::RAD}) {}
  121. RotateY::RotateY(const NumericValue* values) noexcept : ResolvedPrimitive(values, {Unit::RAD}) {}
  122. RotateY::RotateY(float angle, Unit unit) noexcept : ResolvedPrimitive({NumericValue{angle, unit}}, {Unit::RAD}) {}
  123. RotateZ::RotateZ(const NumericValue* values) noexcept : ResolvedPrimitive(values, {Unit::RAD}) {}
  124. RotateZ::RotateZ(float angle, Unit unit) noexcept : ResolvedPrimitive({NumericValue{angle, unit}}, {Unit::RAD}) {}
  125. Rotate2D::Rotate2D(const NumericValue* values) noexcept : ResolvedPrimitive(values, {Unit::RAD}) {}
  126. Rotate2D::Rotate2D(float angle, Unit unit) noexcept : ResolvedPrimitive({NumericValue{angle, unit}}, {Unit::RAD}) {}
  127. Rotate3D::Rotate3D(const NumericValue* values) noexcept : ResolvedPrimitive(values, {Unit::NUMBER, Unit::NUMBER, Unit::NUMBER, Unit::RAD}) {}
  128. Rotate3D::Rotate3D(float x, float y, float z, float angle, Unit angle_unit) noexcept :
  129. ResolvedPrimitive(
  130. {NumericValue{x, Unit::NUMBER}, NumericValue{y, Unit::NUMBER}, NumericValue{z, Unit::NUMBER}, NumericValue{angle, angle_unit}},
  131. {Unit::NUMBER, Unit::NUMBER, Unit::NUMBER, Unit::RAD})
  132. {}
  133. SkewX::SkewX(const NumericValue* values) noexcept : ResolvedPrimitive(values, {Unit::RAD}) {}
  134. SkewX::SkewX(float angle, Unit unit) noexcept : ResolvedPrimitive({NumericValue{angle, unit}}, {Unit::RAD}) {}
  135. SkewY::SkewY(const NumericValue* values) noexcept : ResolvedPrimitive(values, {Unit::RAD}) {}
  136. SkewY::SkewY(float angle, Unit unit) noexcept : ResolvedPrimitive({NumericValue{angle, unit}}, {Unit::RAD}) {}
  137. Skew2D::Skew2D(const NumericValue* values) noexcept : ResolvedPrimitive(values, {Unit::RAD, Unit::RAD}) {}
  138. Skew2D::Skew2D(float x, float y, Unit unit) noexcept : ResolvedPrimitive({NumericValue{x, unit}, {NumericValue{y, unit}}}, {Unit::RAD, Unit::RAD})
  139. {}
  140. Perspective::Perspective(const NumericValue* values) noexcept : UnresolvedPrimitive(values) {}
  141. } // namespace Transforms
  142. } // namespace Rml