vector2i.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*************************************************************************/
  2. /* vector2i.hpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef GODOT_VECTOR2I_HPP
  31. #define GODOT_VECTOR2I_HPP
  32. #include <godot_cpp/core/error_macros.hpp>
  33. #include <godot_cpp/core/math.hpp>
  34. namespace godot {
  35. class String;
  36. struct Vector2;
  37. struct _NO_DISCARD_ Vector2i {
  38. static const int AXIS_COUNT = 2;
  39. enum Axis {
  40. AXIS_X,
  41. AXIS_Y,
  42. };
  43. union {
  44. struct {
  45. union {
  46. int32_t x;
  47. int32_t width;
  48. };
  49. union {
  50. int32_t y;
  51. int32_t height;
  52. };
  53. };
  54. int32_t coord[2] = { 0 };
  55. };
  56. _FORCE_INLINE_ int32_t &operator[](int p_idx) {
  57. DEV_ASSERT((unsigned int)p_idx < 2);
  58. return coord[p_idx];
  59. }
  60. _FORCE_INLINE_ const int32_t &operator[](int p_idx) const {
  61. DEV_ASSERT((unsigned int)p_idx < 2);
  62. return coord[p_idx];
  63. }
  64. _FORCE_INLINE_ Vector2i::Axis min_axis_index() const {
  65. return x < y ? Vector2i::AXIS_X : Vector2i::AXIS_Y;
  66. }
  67. _FORCE_INLINE_ Vector2i::Axis max_axis_index() const {
  68. return x < y ? Vector2i::AXIS_Y : Vector2i::AXIS_X;
  69. }
  70. Vector2i min(const Vector2i &p_vector2i) const {
  71. return Vector2i(MIN(x, p_vector2i.x), MIN(y, p_vector2i.y));
  72. }
  73. Vector2i max(const Vector2i &p_vector2i) const {
  74. return Vector2i(MAX(x, p_vector2i.x), MAX(y, p_vector2i.y));
  75. }
  76. Vector2i operator+(const Vector2i &p_v) const;
  77. void operator+=(const Vector2i &p_v);
  78. Vector2i operator-(const Vector2i &p_v) const;
  79. void operator-=(const Vector2i &p_v);
  80. Vector2i operator*(const Vector2i &p_v1) const;
  81. Vector2i operator*(const int32_t &rvalue) const;
  82. void operator*=(const int32_t &rvalue);
  83. Vector2i operator/(const Vector2i &p_v1) const;
  84. Vector2i operator/(const int32_t &rvalue) const;
  85. void operator/=(const int32_t &rvalue);
  86. Vector2i operator%(const Vector2i &p_v1) const;
  87. Vector2i operator%(const int32_t &rvalue) const;
  88. void operator%=(const int32_t &rvalue);
  89. Vector2i operator-() const;
  90. bool operator<(const Vector2i &p_vec2) const { return (x == p_vec2.x) ? (y < p_vec2.y) : (x < p_vec2.x); }
  91. bool operator>(const Vector2i &p_vec2) const { return (x == p_vec2.x) ? (y > p_vec2.y) : (x > p_vec2.x); }
  92. bool operator<=(const Vector2i &p_vec2) const { return x == p_vec2.x ? (y <= p_vec2.y) : (x < p_vec2.x); }
  93. bool operator>=(const Vector2i &p_vec2) const { return x == p_vec2.x ? (y >= p_vec2.y) : (x > p_vec2.x); }
  94. bool operator==(const Vector2i &p_vec2) const;
  95. bool operator!=(const Vector2i &p_vec2) const;
  96. int64_t length_squared() const;
  97. double length() const;
  98. real_t aspect() const { return width / (real_t)height; }
  99. Vector2i sign() const { return Vector2i(SIGN(x), SIGN(y)); }
  100. Vector2i abs() const { return Vector2i(Math::abs(x), Math::abs(y)); }
  101. Vector2i clamp(const Vector2i &p_min, const Vector2i &p_max) const;
  102. operator String() const;
  103. operator Vector2() const;
  104. inline Vector2i() {}
  105. inline Vector2i(const int32_t p_x, const int32_t p_y) {
  106. x = p_x;
  107. y = p_y;
  108. }
  109. };
  110. // Multiplication operators required to workaround issues with LLVM using implicit conversion.
  111. _FORCE_INLINE_ Vector2i operator*(const int32_t p_scalar, const Vector2i &p_vector) {
  112. return p_vector * p_scalar;
  113. }
  114. _FORCE_INLINE_ Vector2i operator*(const int64_t p_scalar, const Vector2i &p_vector) {
  115. return p_vector * p_scalar;
  116. }
  117. _FORCE_INLINE_ Vector2i operator*(const float p_scalar, const Vector2i &p_vector) {
  118. return p_vector * p_scalar;
  119. }
  120. _FORCE_INLINE_ Vector2i operator*(const double p_scalar, const Vector2i &p_vector) {
  121. return p_vector * p_scalar;
  122. }
  123. typedef Vector2i Size2i;
  124. typedef Vector2i Point2i;
  125. } // namespace godot
  126. #endif // GODOT_VECTOR2I_HPP