vector2i.hpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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_axis) {
  56. DEV_ASSERT((unsigned int)p_axis < 2);
  57. return coord[p_axis];
  58. }
  59. _FORCE_INLINE_ const int32_t &operator[](int p_axis) const {
  60. DEV_ASSERT((unsigned int)p_axis < 2);
  61. return coord[p_axis];
  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. double distance_to(const Vector2i &p_to) const {
  82. return (p_to - *this).length();
  83. }
  84. int64_t distance_squared_to(const Vector2i &p_to) const {
  85. return (p_to - *this).length_squared();
  86. }
  87. Vector2i operator+(const Vector2i &p_v) const;
  88. void operator+=(const Vector2i &p_v);
  89. Vector2i operator-(const Vector2i &p_v) const;
  90. void operator-=(const Vector2i &p_v);
  91. Vector2i operator*(const Vector2i &p_v1) const;
  92. Vector2i operator*(int32_t p_rvalue) const;
  93. void operator*=(int32_t p_rvalue);
  94. Vector2i operator/(const Vector2i &p_v1) const;
  95. Vector2i operator/(int32_t p_rvalue) const;
  96. void operator/=(int32_t p_rvalue);
  97. Vector2i operator%(const Vector2i &p_v1) const;
  98. Vector2i operator%(int32_t p_rvalue) const;
  99. void operator%=(int32_t p_rvalue);
  100. Vector2i operator-() const;
  101. bool operator<(const Vector2i &p_vec2) const { return (x == p_vec2.x) ? (y < p_vec2.y) : (x < p_vec2.x); }
  102. bool operator>(const Vector2i &p_vec2) const { return (x == p_vec2.x) ? (y > p_vec2.y) : (x > p_vec2.x); }
  103. bool operator<=(const Vector2i &p_vec2) const { return x == p_vec2.x ? (y <= p_vec2.y) : (x < p_vec2.x); }
  104. bool operator>=(const Vector2i &p_vec2) const { return x == p_vec2.x ? (y >= p_vec2.y) : (x > p_vec2.x); }
  105. bool operator==(const Vector2i &p_vec2) const;
  106. bool operator!=(const Vector2i &p_vec2) const;
  107. int64_t length_squared() const;
  108. double length() const;
  109. real_t aspect() const { return width / (real_t)height; }
  110. Vector2i sign() const { return Vector2i(SIGN(x), SIGN(y)); }
  111. Vector2i abs() const { return Vector2i(Math::abs(x), Math::abs(y)); }
  112. Vector2i clamp(const Vector2i &p_min, const Vector2i &p_max) const;
  113. Vector2i clampi(int32_t p_min, int32_t p_max) const;
  114. Vector2i snapped(const Vector2i &p_step) const;
  115. Vector2i snappedi(int32_t p_step) const;
  116. operator String() const;
  117. operator Vector2() const;
  118. inline Vector2i() {}
  119. inline Vector2i(int32_t p_x, int32_t p_y) {
  120. x = p_x;
  121. y = p_y;
  122. }
  123. };
  124. // Multiplication operators required to workaround issues with LLVM using implicit conversion.
  125. _FORCE_INLINE_ Vector2i operator*(int32_t p_scalar, const Vector2i &p_vector) {
  126. return p_vector * p_scalar;
  127. }
  128. _FORCE_INLINE_ Vector2i operator*(int64_t p_scalar, const Vector2i &p_vector) {
  129. return p_vector * p_scalar;
  130. }
  131. _FORCE_INLINE_ Vector2i operator*(float p_scalar, const Vector2i &p_vector) {
  132. return p_vector * p_scalar;
  133. }
  134. _FORCE_INLINE_ Vector2i operator*(double p_scalar, const Vector2i &p_vector) {
  135. return p_vector * p_scalar;
  136. }
  137. typedef Vector2i Size2i;
  138. typedef Vector2i Point2i;
  139. } // namespace godot