vector2i.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*************************************************************************/
  2. /* vector2i.hpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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/math.hpp>
  33. #include <godot_cpp/variant/string.hpp>
  34. #include <godot_cpp/variant/vector2.hpp>
  35. namespace godot {
  36. class Vector2i {
  37. public:
  38. _FORCE_INLINE_ GDNativeTypePtr ptr() const { return (void *)this; }
  39. enum Axis {
  40. AXIS_X,
  41. AXIS_Y,
  42. };
  43. union {
  44. int32_t x = 0;
  45. int32_t width;
  46. };
  47. union {
  48. int32_t y = 0;
  49. int32_t height;
  50. };
  51. inline int32_t &operator[](int p_idx) {
  52. return p_idx ? y : x;
  53. }
  54. inline const int32_t &operator[](int p_idx) const {
  55. return p_idx ? y : x;
  56. }
  57. Vector2i operator+(const Vector2i &p_v) const;
  58. void operator+=(const Vector2i &p_v);
  59. Vector2i operator-(const Vector2i &p_v) const;
  60. void operator-=(const Vector2i &p_v);
  61. Vector2i operator*(const Vector2i &p_v1) const;
  62. Vector2i operator*(const int32_t &rvalue) const;
  63. void operator*=(const int32_t &rvalue);
  64. Vector2i operator/(const Vector2i &p_v1) const;
  65. Vector2i operator/(const int32_t &rvalue) const;
  66. void operator/=(const int32_t &rvalue);
  67. Vector2i operator%(const Vector2i &p_v1) const;
  68. Vector2i operator%(const int32_t &rvalue) const;
  69. void operator%=(const int32_t &rvalue);
  70. Vector2i operator-() const;
  71. bool operator<(const Vector2i &p_vec2) const { return (x == p_vec2.x) ? (y < p_vec2.y) : (x < p_vec2.x); }
  72. bool operator>(const Vector2i &p_vec2) const { return (x == p_vec2.x) ? (y > p_vec2.y) : (x > p_vec2.x); }
  73. bool operator<=(const Vector2i &p_vec2) const { return x == p_vec2.x ? (y <= p_vec2.y) : (x < p_vec2.x); }
  74. bool operator>=(const Vector2i &p_vec2) const { return x == p_vec2.x ? (y >= p_vec2.y) : (x > p_vec2.x); }
  75. bool operator==(const Vector2i &p_vec2) const;
  76. bool operator!=(const Vector2i &p_vec2) const;
  77. real_t aspect() const { return width / (real_t)height; }
  78. Vector2i sign() const { return Vector2i(Math::sign(x), Math::sign(y)); }
  79. Vector2i abs() const { return Vector2i(Math::abs(x), Math::abs(y)); }
  80. operator String() const;
  81. operator Vector2() const { return Vector2(x, y); }
  82. inline Vector2i() {}
  83. inline Vector2i(const Vector2 &p_vec2) {
  84. x = (int32_t)p_vec2.x;
  85. y = (int32_t)p_vec2.y;
  86. }
  87. inline Vector2i(int32_t p_x, int32_t p_y) {
  88. x = p_x;
  89. y = p_y;
  90. }
  91. };
  92. inline Vector2i operator*(const int32_t &p_scalar, const Vector2i &p_vector) {
  93. return p_vector * p_scalar;
  94. }
  95. inline Vector2i operator*(const int64_t &p_scalar, const Vector2i &p_vector) {
  96. return p_vector * p_scalar;
  97. }
  98. inline Vector2i operator*(const float &p_scalar, const Vector2i &p_vector) {
  99. return p_vector * p_scalar;
  100. }
  101. inline Vector2i operator*(const double &p_scalar, const Vector2i &p_vector) {
  102. return p_vector * p_scalar;
  103. }
  104. typedef Vector2i Size2i;
  105. typedef Vector2i Point2i;
  106. } // namespace godot
  107. #endif // GODOT_VECTOR2I_HPP