1 | /**************************************************************************/ |
2 | /* vector2i.h */ |
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 | |
31 | #ifndef VECTOR2I_H |
32 | #define VECTOR2I_H |
33 | |
34 | #include "core/error/error_macros.h" |
35 | #include "core/math/math_funcs.h" |
36 | |
37 | class String; |
38 | struct Vector2; |
39 | |
40 | struct _NO_DISCARD_ Vector2i { |
41 | static const int AXIS_COUNT = 2; |
42 | |
43 | enum Axis { |
44 | AXIS_X, |
45 | AXIS_Y, |
46 | }; |
47 | |
48 | union { |
49 | struct { |
50 | union { |
51 | int32_t x; |
52 | int32_t width; |
53 | }; |
54 | union { |
55 | int32_t y; |
56 | int32_t height; |
57 | }; |
58 | }; |
59 | |
60 | int32_t coord[2] = { 0 }; |
61 | }; |
62 | |
63 | _FORCE_INLINE_ int32_t &operator[](int p_idx) { |
64 | DEV_ASSERT((unsigned int)p_idx < 2); |
65 | return coord[p_idx]; |
66 | } |
67 | _FORCE_INLINE_ const int32_t &operator[](int p_idx) const { |
68 | DEV_ASSERT((unsigned int)p_idx < 2); |
69 | return coord[p_idx]; |
70 | } |
71 | |
72 | _FORCE_INLINE_ Vector2i::Axis min_axis_index() const { |
73 | return x < y ? Vector2i::AXIS_X : Vector2i::AXIS_Y; |
74 | } |
75 | |
76 | _FORCE_INLINE_ Vector2i::Axis max_axis_index() const { |
77 | return x < y ? Vector2i::AXIS_Y : Vector2i::AXIS_X; |
78 | } |
79 | |
80 | Vector2i min(const Vector2i &p_vector2i) const { |
81 | return Vector2i(MIN(x, p_vector2i.x), MIN(y, p_vector2i.y)); |
82 | } |
83 | |
84 | Vector2i max(const Vector2i &p_vector2i) const { |
85 | return Vector2i(MAX(x, p_vector2i.x), MAX(y, p_vector2i.y)); |
86 | } |
87 | |
88 | Vector2i operator+(const Vector2i &p_v) const; |
89 | void operator+=(const Vector2i &p_v); |
90 | Vector2i operator-(const Vector2i &p_v) const; |
91 | void operator-=(const Vector2i &p_v); |
92 | Vector2i operator*(const Vector2i &p_v1) const; |
93 | |
94 | Vector2i operator*(const int32_t &rvalue) const; |
95 | void operator*=(const int32_t &rvalue); |
96 | |
97 | Vector2i operator/(const Vector2i &p_v1) const; |
98 | Vector2i operator/(const int32_t &rvalue) const; |
99 | void operator/=(const int32_t &rvalue); |
100 | |
101 | Vector2i operator%(const Vector2i &p_v1) const; |
102 | Vector2i operator%(const int32_t &rvalue) const; |
103 | void operator%=(const int32_t &rvalue); |
104 | |
105 | Vector2i operator-() const; |
106 | bool operator<(const Vector2i &p_vec2) const { return (x == p_vec2.x) ? (y < p_vec2.y) : (x < p_vec2.x); } |
107 | bool operator>(const Vector2i &p_vec2) const { return (x == p_vec2.x) ? (y > p_vec2.y) : (x > p_vec2.x); } |
108 | |
109 | bool operator<=(const Vector2i &p_vec2) const { return x == p_vec2.x ? (y <= p_vec2.y) : (x < p_vec2.x); } |
110 | bool operator>=(const Vector2i &p_vec2) const { return x == p_vec2.x ? (y >= p_vec2.y) : (x > p_vec2.x); } |
111 | |
112 | bool operator==(const Vector2i &p_vec2) const; |
113 | bool operator!=(const Vector2i &p_vec2) const; |
114 | |
115 | int64_t length_squared() const; |
116 | double length() const; |
117 | |
118 | real_t aspect() const { return width / (real_t)height; } |
119 | Vector2i sign() const { return Vector2i(SIGN(x), SIGN(y)); } |
120 | Vector2i abs() const { return Vector2i(Math::abs(x), Math::abs(y)); } |
121 | Vector2i clamp(const Vector2i &p_min, const Vector2i &p_max) const; |
122 | Vector2i snapped(const Vector2i &p_step) const; |
123 | |
124 | operator String() const; |
125 | operator Vector2() const; |
126 | |
127 | inline Vector2i() {} |
128 | inline Vector2i(const int32_t p_x, const int32_t p_y) { |
129 | x = p_x; |
130 | y = p_y; |
131 | } |
132 | }; |
133 | |
134 | // Multiplication operators required to workaround issues with LLVM using implicit conversion. |
135 | |
136 | _FORCE_INLINE_ Vector2i operator*(const int32_t p_scalar, const Vector2i &p_vector) { |
137 | return p_vector * p_scalar; |
138 | } |
139 | |
140 | _FORCE_INLINE_ Vector2i operator*(const int64_t p_scalar, const Vector2i &p_vector) { |
141 | return p_vector * p_scalar; |
142 | } |
143 | |
144 | _FORCE_INLINE_ Vector2i operator*(const float p_scalar, const Vector2i &p_vector) { |
145 | return p_vector * p_scalar; |
146 | } |
147 | |
148 | _FORCE_INLINE_ Vector2i operator*(const double p_scalar, const Vector2i &p_vector) { |
149 | return p_vector * p_scalar; |
150 | } |
151 | |
152 | typedef Vector2i Size2i; |
153 | typedef Vector2i Point2i; |
154 | |
155 | #endif // VECTOR2I_H |
156 | |