vector2i.hpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /**************************************************************************/
  2. /* vector2i.hpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. #pragma once
  31. #include <godot_cpp/core/error_macros.hpp>
  32. #include <godot_cpp/core/math.hpp>
  33. namespace godot {
  34. class String;
  35. struct Vector2;
  36. struct [[nodiscard]] Vector2i {
  37. static const int AXIS_COUNT = 2;
  38. enum Axis {
  39. AXIS_X,
  40. AXIS_Y,
  41. };
  42. union {
  43. struct {
  44. union {
  45. int32_t x;
  46. int32_t width;
  47. };
  48. union {
  49. int32_t y;
  50. int32_t height;
  51. };
  52. };
  53. int32_t coord[2] = { 0 };
  54. };
  55. _FORCE_INLINE_ int32_t &operator[](int p_idx) {
  56. DEV_ASSERT((unsigned int)p_idx < 2);
  57. return coord[p_idx];
  58. }
  59. _FORCE_INLINE_ const int32_t &operator[](int p_idx) const {
  60. DEV_ASSERT((unsigned int)p_idx < 2);
  61. return coord[p_idx];
  62. }
  63. _FORCE_INLINE_ Vector2i::Axis min_axis_index() const {
  64. return x < y ? Vector2i::AXIS_X : Vector2i::AXIS_Y;
  65. }
  66. _FORCE_INLINE_ Vector2i::Axis max_axis_index() const {
  67. return x < y ? Vector2i::AXIS_Y : Vector2i::AXIS_X;
  68. }
  69. Vector2i min(const Vector2i &p_vector2i) const {
  70. return Vector2i(MIN(x, p_vector2i.x), MIN(y, p_vector2i.y));
  71. }
  72. Vector2i mini(int32_t p_scalar) const {
  73. return Vector2i(MIN(x, p_scalar), MIN(y, p_scalar));
  74. }
  75. Vector2i max(const Vector2i &p_vector2i) const {
  76. return Vector2i(MAX(x, p_vector2i.x), MAX(y, p_vector2i.y));
  77. }
  78. Vector2i maxi(int32_t p_scalar) const {
  79. return Vector2i(MAX(x, p_scalar), MAX(y, p_scalar));
  80. }
  81. Vector2i operator+(const Vector2i &p_v) const;
  82. void operator+=(const Vector2i &p_v);
  83. Vector2i operator-(const Vector2i &p_v) const;
  84. void operator-=(const Vector2i &p_v);
  85. Vector2i operator*(const Vector2i &p_v1) const;
  86. Vector2i operator*(const int32_t &rvalue) const;
  87. void operator*=(const int32_t &rvalue);
  88. Vector2i operator/(const Vector2i &p_v1) const;
  89. Vector2i operator/(const int32_t &rvalue) const;
  90. void operator/=(const int32_t &rvalue);
  91. Vector2i operator%(const Vector2i &p_v1) const;
  92. Vector2i operator%(const int32_t &rvalue) const;
  93. void operator%=(const int32_t &rvalue);
  94. Vector2i operator-() const;
  95. bool operator<(const Vector2i &p_vec2) const { return (x == p_vec2.x) ? (y < p_vec2.y) : (x < p_vec2.x); }
  96. bool operator>(const Vector2i &p_vec2) const { return (x == p_vec2.x) ? (y > p_vec2.y) : (x > p_vec2.x); }
  97. bool operator<=(const Vector2i &p_vec2) const { return x == p_vec2.x ? (y <= p_vec2.y) : (x < p_vec2.x); }
  98. bool operator>=(const Vector2i &p_vec2) const { return x == p_vec2.x ? (y >= p_vec2.y) : (x > p_vec2.x); }
  99. bool operator==(const Vector2i &p_vec2) const;
  100. bool operator!=(const Vector2i &p_vec2) const;
  101. int64_t length_squared() const;
  102. double length() const;
  103. int64_t distance_squared_to(const Vector2i &p_to) const;
  104. double distance_to(const Vector2i &p_to) const;
  105. real_t aspect() const { return width / (real_t)height; }
  106. Vector2i sign() const { return Vector2i(SIGN(x), SIGN(y)); }
  107. Vector2i abs() const { return Vector2i(Math::abs(x), Math::abs(y)); }
  108. Vector2i snapped(const Vector2i &p_step) const;
  109. Vector2i snappedi(int32_t p_step) const;
  110. Vector2i clamp(const Vector2i &p_min, const Vector2i &p_max) const;
  111. Vector2i clampi(int32_t p_min, int32_t p_max) const;
  112. operator String() const;
  113. operator Vector2() const;
  114. inline Vector2i() {}
  115. inline Vector2i(const int32_t p_x, const int32_t p_y) {
  116. x = p_x;
  117. y = p_y;
  118. }
  119. };
  120. // Multiplication operators required to workaround issues with LLVM using implicit conversion.
  121. _FORCE_INLINE_ Vector2i operator*(const int32_t p_scalar, const Vector2i &p_vector) {
  122. return p_vector * p_scalar;
  123. }
  124. _FORCE_INLINE_ Vector2i operator*(const int64_t p_scalar, const Vector2i &p_vector) {
  125. return p_vector * p_scalar;
  126. }
  127. _FORCE_INLINE_ Vector2i operator*(const float p_scalar, const Vector2i &p_vector) {
  128. return p_vector * p_scalar;
  129. }
  130. _FORCE_INLINE_ Vector2i operator*(const double p_scalar, const Vector2i &p_vector) {
  131. return p_vector * p_scalar;
  132. }
  133. typedef Vector2i Size2i;
  134. typedef Vector2i Point2i;
  135. } // namespace godot