Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arithemic operators added for ImVec2 #5966

Closed
wants to merge 1 commit into from

Conversation

therealjoker4u
Copy link

Instead of doing this:

ImVec2 vec3 = ImVec2(vec1.x + vec2.x, vec1.y + vec2.y);
ImVec2 vec3 = ImVec2(vec1.x - vec2.x, vec1.y - vec2.y);
ImVec2 vec3 = ImVec2(vec1.x * vec2.x, vec1.y * vec2.y);
ImVec2 vec3 = ImVec2(vec1.x / vec2.x, vec1.y / vec2.y);
/// OR ->>>
ImVec2 vec3 = ImVec2(vec1.x + 10, vec1.y + 10);
ImVec2 vec3 = ImVec2(vec1.x - 10, vec1.y - 10);
ImVec2 vec3 = ImVec2(vec1.x * 10, vec1.y * 10);
ImVec2 vec3 = ImVec2(vec1.x / 10, vec1.y / 10);

Use the operators alternative:

ImVec2 vec3 = vec1 + vec2;
ImVec2 vec3 = vec1 - vec2;
ImVec2 vec3 = vec1 * vec2;
ImVec2 vec3 = vec1 / vec2;
/// OR ->>>
ImVec2 vec3 = vec1 + 10;
ImVec2 vec3 = vec1 - 10;
ImVec2 vec3 = vec1 * 10;
ImVec2 vec3 = vec1 / 10;

@inobelar
Copy link

inobelar commented Dec 7, 2022

@alireza98dev Please read FAQ::math_operators - by it's design 'Dear ImGUI'-way of extending 'pretty basic' ImVec2/ImVec4 - by adding extra stuff into your own imconfig.h.

Math operators: if you have setup IM_VEC2_CLASS_EXTRA in imconfig.h to bind your own math types, you can use your own math types and their natural operators instead of ImVec2. ImVec2 by default doesn't export any math operators in the public API. You may use #define IMGUI_DEFINE_MATH_OPERATORS #include "imgui_internal.h" to use the internally defined math operators, but instead prefer using your own math library and set it up in imconfig.h.

Aso look at:

imgui/imgui_internal.h

Lines 375 to 396 in 9d08506

// Helpers: ImVec2/ImVec4 operators
// We are keeping those disabled by default so they don't leak in user space, to allow user enabling implicit cast operators between ImVec2 and their own types (using IM_VEC2_CLASS_EXTRA etc.)
// We unfortunately don't have a unary- operator for ImVec2 because this would needs to be defined inside the class itself.
#ifdef IMGUI_DEFINE_MATH_OPERATORS
IM_MSVC_RUNTIME_CHECKS_OFF
static inline ImVec2 operator*(const ImVec2& lhs, const float rhs) { return ImVec2(lhs.x * rhs, lhs.y * rhs); }
static inline ImVec2 operator/(const ImVec2& lhs, const float rhs) { return ImVec2(lhs.x / rhs, lhs.y / rhs); }
static inline ImVec2 operator+(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x + rhs.x, lhs.y + rhs.y); }
static inline ImVec2 operator-(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x - rhs.x, lhs.y - rhs.y); }
static inline ImVec2 operator*(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x * rhs.x, lhs.y * rhs.y); }
static inline ImVec2 operator/(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x / rhs.x, lhs.y / rhs.y); }
static inline ImVec2& operator*=(ImVec2& lhs, const float rhs) { lhs.x *= rhs; lhs.y *= rhs; return lhs; }
static inline ImVec2& operator/=(ImVec2& lhs, const float rhs) { lhs.x /= rhs; lhs.y /= rhs; return lhs; }
static inline ImVec2& operator+=(ImVec2& lhs, const ImVec2& rhs) { lhs.x += rhs.x; lhs.y += rhs.y; return lhs; }
static inline ImVec2& operator-=(ImVec2& lhs, const ImVec2& rhs) { lhs.x -= rhs.x; lhs.y -= rhs.y; return lhs; }
static inline ImVec2& operator*=(ImVec2& lhs, const ImVec2& rhs) { lhs.x *= rhs.x; lhs.y *= rhs.y; return lhs; }
static inline ImVec2& operator/=(ImVec2& lhs, const ImVec2& rhs) { lhs.x /= rhs.x; lhs.y /= rhs.y; return lhs; }
static inline ImVec4 operator+(const ImVec4& lhs, const ImVec4& rhs) { return ImVec4(lhs.x + rhs.x, lhs.y + rhs.y, lhs.z + rhs.z, lhs.w + rhs.w); }
static inline ImVec4 operator-(const ImVec4& lhs, const ImVec4& rhs) { return ImVec4(lhs.x - rhs.x, lhs.y - rhs.y, lhs.z - rhs.z, lhs.w - rhs.w); }
static inline ImVec4 operator*(const ImVec4& lhs, const ImVec4& rhs) { return ImVec4(lhs.x * rhs.x, lhs.y * rhs.y, lhs.z * rhs.z, lhs.w * rhs.w); }
IM_MSVC_RUNTIME_CHECKS_RESTORE
#endif

@ocornut
Copy link
Owner

ocornut commented Dec 8, 2022

What @inobelar this is very intentional for those reasons. You can use the ones in imgui_internal.h by defining IMGUI_DEFINE_MATH_OPERATORS, or you may include your own in a different file.

@ocornut ocornut closed this Dec 8, 2022
ocornut added a commit that referenced this pull request Feb 15, 2023
…ATH_OPERATORS) implementation from imgui_internal.h in imgui.h. (#6164, #6137, #5966, #2832)
Intrets pushed a commit to Intrets/imgui that referenced this pull request Jun 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants