Skip to content

Commit cb181c2

Browse files
ocornutIntrets
authored andcommitted
Moved the optional "courtesy maths operators" (#define IMGUI_DEFINE_MATH_OPERATORS) implementation from imgui_internal.h in imgui.h. (ocornut#6164, ocornut#6137, ocornut#5966, ocornut#2832)
1 parent 6f23b17 commit cb181c2

10 files changed

+97
-61
lines changed

docs/CHANGELOG.txt

+12-1
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,20 @@ HOW TO UPDATE?
3737

3838
Breaking Changes:
3939

40+
- Moved the optional "courtesy maths operators" implementation from imgui_internal.h in imgui.h.
41+
Even though we encourage using your own maths types and operators by setting up IM_VEC2_CLASS_EXTRA,
42+
it has been frequently requested by people to use our own. We had an opt-in define which was
43+
previously fulfilled in imgui_internal.h. It is now fulfilled in imgui.h. (#6164, #6137, #5966, #2832)
44+
OK: #define IMGUI_DEFINE_MATH_OPERATORS / #include "imgui.h" / #include "imgui_internal.h"
45+
Error: #include "imgui.h" / #define IMGUI_DEFINE_MATH_OPERATORS / #include "imgui_internal.h"
46+
Added a dedicated compile-time check message to help diagnose this.
47+
4048
Other changes:
4149

4250
- Backends: Win32: Use WM_NCMOUSEMOVE / WM_NCMOUSELEAVE to track mouse positions over
4351
non-client area (e.g. OS decorations) when app is not focused. (#6045, #6162)
4452

4553

46-
4754
-----------------------------------------------------------------------
4855
VERSION 1.89.3 (Released 2023-02-14)
4956
-----------------------------------------------------------------------
@@ -373,6 +380,10 @@ Other Changes:
373380
asserts with 16-bits ImDrawVtx). (#5720)
374381
- Fonts: Added GetGlyphRangesGreek() helper for Greek & Coptic glyph range. (#5676, #5727) [@azonenberg]
375382
- ImDrawList: Not using alloca() anymore, lift single polygon size limits. (#5704, #1811)
383+
- Note: now using a temporary buffer stored in ImDrawListSharedData.
384+
This change made it more visible than you cannot append to multiple ImDrawList from multiple
385+
threads if they share the same ImDrawListSharedData. Previously it was a little more likely
386+
for this to "accidentally" work, but was already incorrect. (#6167)
376387
- Platform IME: [Windows] Removed call to ImmAssociateContextEx() leading to freeze on some setups.
377388
(#2589, #5535, #5264, #4972)
378389
- Misc: better error reporting for PopStyleColor()/PopStyleVar() + easier to recover. (#1651)

docs/FAQ.md

+15-6
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ or view this file with any Markdown viewer.
2525
| **Q&A: Usage** |
2626
| **[About the ID Stack system..<br>Why is my widget not reacting when I click on it?<br>How can I have widgets with an empty label?<br>How can I have multiple widgets with the same label?<br>How can I have multiple windows with the same label?](#q-about-the-id-stack-system)** |
2727
| [How can I display an image? What is ImTextureID, how does it work?](#q-how-can-i-display-an-image-what-is-imtextureid-how-does-it-work)|
28-
| [How can I use my own math types instead of ImVec2/ImVec4?](#q-how-can-i-use-my-own-math-types-instead-of-imvec2imvec4) |
28+
| [How can I use maths operators with ImVec2?](#q-how-can-i-use-maths-operators-with-imvec2) |
29+
| [How can I use my own maths types instead of ImVec2/ImVec4?](#q-how-can-i-use-my-own-maths-types-instead-of-imvec2imvec4) |
2930
| [How can I interact with standard C++ types (such as std::string and std::vector)?](#q-how-can-i-interact-with-standard-c-types-such-as-stdstring-and-stdvector) |
3031
| [How can I display custom shapes? (using low-level ImDrawList API)](#q-how-can-i-display-custom-shapes-using-low-level-imdrawlist-api) |
3132
| **Q&A: Fonts, Text** |
@@ -192,7 +193,7 @@ if (clip_max.x <= clip_min.x || clip_max.y <= clip_min.y)
192193
const D3D11_RECT r = { (LONG)clip_min.x, (LONG)clip_min.y, (LONG)clip_max.x, (LONG)clip_max.y };
193194
ctx->RSSetScissorRects(1, &r);
194195
```
195-
196+
196197
##### [Return to Index](#index)
197198

198199
---
@@ -426,10 +427,18 @@ Finally, you may call `ImGui::ShowMetricsWindow()` to explore/visualize/understa
426427

427428
---
428429

429-
### Q: How can I use my own math types instead of ImVec2/ImVec4?
430+
### Q: How can I use maths operators with ImVec2?
431+
432+
We do not export maths operators by default in imgui.h in order to not conflict with the use of your own maths types and maths operators. As a convenience, you may use `#defne IMGUI_DEFINE_MATH_OPERATORS` + `#include "imgui.h"` to access our basic maths operators.
433+
434+
##### [Return to Index](#index)
435+
436+
---
437+
438+
### Q: How can I use my own maths types instead of ImVec2/ImVec4?
430439

431-
You can edit [imconfig.h](https://github.com/ocornut/imgui/blob/master/imconfig.h) and setup the `IM_VEC2_CLASS_EXTRA`/`IM_VEC4_CLASS_EXTRA` macros to add implicit type conversions.
432-
This way you'll be able to use your own types everywhere, e.g. passing `MyVector2` or `glm::vec2` to ImGui functions instead of `ImVec2`.
440+
You can setup your [imconfig.h](https://github.com/ocornut/imgui/blob/master/imconfig.h) file with `IM_VEC2_CLASS_EXTRA`/`IM_VEC4_CLASS_EXTRA` macros to add implicit type conversions to our own maths types.
441+
This way you will be able to use your own types everywhere, e.g. passing `MyVector2` or `glm::vec2` to ImGui functions instead of `ImVec2`.
433442

434443
##### [Return to Index](#index)
435444

@@ -485,7 +494,7 @@ ImGui::End();
485494
486495
- Refer to "Demo > Examples > Custom Rendering" in the demo window and read the code of `ShowExampleAppCustomRendering()` in `imgui_demo.cpp` from more examples.
487496
- To generate colors: you can use the macro `IM_COL32(255,255,255,255)` to generate them at compile time, or use `ImGui::GetColorU32(IM_COL32(255,255,255,255))` or `ImGui::GetColorU32(ImVec4(1.0f,1.0f,1.0f,1.0f))` to generate a color that is multiplied by the current value of `style.Alpha`.
488-
- 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`.
497+
- 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.h"` to use our math operators, but instead prefer using your own math library and set it up in `imconfig.h`.
489498
- You can use `ImGui::GetBackgroundDrawList()` or `ImGui::GetForegroundDrawList()` to access draw lists which will be displayed behind and over every other Dear ImGui window (one bg/fg drawlist per viewport). This is very convenient if you need to quickly display something on the screen that is not associated with a Dear ImGui window.
490499
- You can also create your own empty window and draw inside it. Call Begin() with the NoBackground | NoDecoration | NoSavedSettings | NoInputs flags (The `ImGuiWindowFlags_NoDecoration` flag itself is a shortcut for NoTitleBar | NoResize | NoScrollbar | NoCollapse). Then you can retrieve the ImDrawList* via `GetWindowDrawList()` and draw to it in any way you like.
491500
- You can create your own ImDrawList instance. You'll need to initialize them with `ImGui::GetDrawListSharedData()`, or create your own instancing `ImDrawListSharedData`, and then call your renderer function with your own ImDrawList or ImDrawData data.

imconfig.h

+2
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ extern thread_local ImGuiContext* MyImGuiTLS;
9797
constexpr ImVec4(const MyVec4& f) : x(f.x), y(f.y), z(f.z), w(f.w) {} \
9898
operator MyVec4() const { return MyVec4(x,y,z,w); }
9999
*/
100+
//---- ...Or use Dear ImGui's own very basic math operators.
101+
//#define IMGUI_DEFINE_MATH_OPERATORS
100102

101103
//---- Use 32-bit vertex indices (default is 16-bit) is one way to allow large meshes with more than 64K vertices.
102104
// Your renderer backend will need to support it (most example renderer backends support both 16/32-bit indices).

imgui.cpp

+24-18
Original file line numberDiff line numberDiff line change
@@ -386,22 +386,28 @@ CODE
386386
When you are not sure about an old symbol or function name, try using the Search/Find function of your IDE to look for comments or references in all imgui files.
387387
You can read releases logs https://github.com/ocornut/imgui/releases for more details.
388388

389+
- 2023/02/15 (1.89.4) - moved the optional "courtesy maths operators" implementation from imgui_internal.h in imgui.h.
390+
Even though we encourage using your own maths types and operators by setting up IM_VEC2_CLASS_EXTRA,
391+
it has been frequently requested by people to use our own. We had an opt-in define which was
392+
previously fulfilled in imgui_internal.h. It is now fulfilled in imgui.h. (#6164)
393+
- OK: #define IMGUI_DEFINE_MATH_OPERATORS / #include "imgui.h" / #include "imgui_internal.h"
394+
- Error: #include "imgui.h" / #define IMGUI_DEFINE_MATH_OPERATORS / #include "imgui_internal.h"
389395
- 2023/02/07 (1.89.3) - backends: renamed "imgui_impl_sdl.cpp" to "imgui_impl_sdl2.cpp" and "imgui_impl_sdl.h" to "imgui_impl_sdl2.h". (#6146) This is in prevision for the future release of SDL3.
390-
- 2022/10/26 (1.89) - commented out redirecting OpenPopupContextItem() which was briefly the name of OpenPopupOnItemClick() from 1.77 to 1.79.
391-
- 2022/10/12 (1.89) - removed runtime patching of invalid "%f"/"%0.f" format strings for DragInt()/SliderInt(). This was obsoleted in 1.61 (May 2018). See 1.61 changelog for details.
392-
- 2022/09/26 (1.89) - renamed and merged keyboard modifiers key enums and flags into a same set. Kept inline redirection enums (will obsolete).
393-
- ImGuiKey_ModCtrl and ImGuiModFlags_Ctrl -> ImGuiMod_Ctrl
394-
- ImGuiKey_ModShift and ImGuiModFlags_Shift -> ImGuiMod_Shift
395-
- ImGuiKey_ModAlt and ImGuiModFlags_Alt -> ImGuiMod_Alt
396-
- ImGuiKey_ModSuper and ImGuiModFlags_Super -> ImGuiMod_Super
397-
the ImGuiKey_ModXXX were introduced in 1.87 and mostly used by backends.
398-
the ImGuiModFlags_XXX have been exposed in imgui.h but not really used by any public api only by third-party extensions.
399-
exceptionally commenting out the older ImGuiKeyModFlags_XXX names ahead of obsolescence schedule to reduce confusion and because they were not meant to be used anyway.
400-
- 2022/09/20 (1.89) - ImGuiKey is now a typed enum, allowing ImGuiKey_XXX symbols to be named in debuggers.
401-
this will require uses of legacy backend-dependent indices to be casted, e.g.
402-
- with imgui_impl_glfw: IsKeyPressed(GLFW_KEY_A) -> IsKeyPressed((ImGuiKey)GLFW_KEY_A);
403-
- with imgui_impl_win32: IsKeyPressed('A') -> IsKeyPressed((ImGuiKey)'A')
404-
- etc. However if you are upgrading code you might well use the better, backend-agnostic IsKeyPressed(ImGuiKey_A) now!
396+
- 2022/10/26 (1.89) - commented out redirecting OpenPopupContextItem() which was briefly the name of OpenPopupOnItemClick() from 1.77 to 1.79.
397+
- 2022/10/12 (1.89) - removed runtime patching of invalid "%f"/"%0.f" format strings for DragInt()/SliderInt(). This was obsoleted in 1.61 (May 2018). See 1.61 changelog for details.
398+
- 2022/09/26 (1.89) - renamed and merged keyboard modifiers key enums and flags into a same set. Kept inline redirection enums (will obsolete).
399+
- ImGuiKey_ModCtrl and ImGuiModFlags_Ctrl -> ImGuiMod_Ctrl
400+
- ImGuiKey_ModShift and ImGuiModFlags_Shift -> ImGuiMod_Shift
401+
- ImGuiKey_ModAlt and ImGuiModFlags_Alt -> ImGuiMod_Alt
402+
- ImGuiKey_ModSuper and ImGuiModFlags_Super -> ImGuiMod_Super
403+
the ImGuiKey_ModXXX were introduced in 1.87 and mostly used by backends.
404+
the ImGuiModFlags_XXX have been exposed in imgui.h but not really used by any public api only by third-party extensions.
405+
exceptionally commenting out the older ImGuiKeyModFlags_XXX names ahead of obsolescence schedule to reduce confusion and because they were not meant to be used anyway.
406+
- 2022/09/20 (1.89) - ImGuiKey is now a typed enum, allowing ImGuiKey_XXX symbols to be named in debuggers.
407+
this will require uses of legacy backend-dependent indices to be casted, e.g.
408+
- with imgui_impl_glfw: IsKeyPressed(GLFW_KEY_A) -> IsKeyPressed((ImGuiKey)GLFW_KEY_A);
409+
- with imgui_impl_win32: IsKeyPressed('A') -> IsKeyPressed((ImGuiKey)'A')
410+
- etc. However if you are upgrading code you might well use the better, backend-agnostic IsKeyPressed(ImGuiKey_A) now!
405411
- 2022/09/12 (1.89) - removed the bizarre legacy default argument for 'TreePush(const void* ptr = NULL)', always pass a pointer value explicitly. NULL/nullptr is ok but require cast, e.g. TreePush((void*)nullptr);
406412
- 2022/09/05 (1.89) - commented out redirecting functions/enums names that were marked obsolete in 1.77 and 1.78 (June 2020):
407413
- DragScalar(), DragScalarN(), DragFloat(), DragFloat2(), DragFloat3(), DragFloat4(): For old signatures ending with (..., const char* format, float power = 1.0f) -> use (..., format ImGuiSliderFlags_Logarithmic) if power != 1.0f.
@@ -860,12 +866,12 @@ CODE
860866
#define _CRT_SECURE_NO_WARNINGS
861867
#endif
862868

863-
#include "imgui.h"
864-
#ifndef IMGUI_DISABLE
865-
866869
#ifndef IMGUI_DEFINE_MATH_OPERATORS
867870
#define IMGUI_DEFINE_MATH_OPERATORS
868871
#endif
872+
873+
#include "imgui.h"
874+
#ifndef IMGUI_DISABLE
869875
#include "imgui_internal.h"
870876

871877
// System includes

imgui.h

+29-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
// Library Version
2424
// (Integer encoded as XYYZZ for use in #if preprocessor conditionals, e.g. '#if IMGUI_VERSION_NUM > 12345')
2525
#define IMGUI_VERSION "1.89.4 WIP"
26-
#define IMGUI_VERSION_NUM 18931
26+
#define IMGUI_VERSION_NUM 18932
2727
#define IMGUI_HAS_TABLE
2828

2929
/*
@@ -37,7 +37,7 @@ Index of this file:
3737
// [SECTION] ImGuiStyle
3838
// [SECTION] ImGuiIO
3939
// [SECTION] Misc data structures (ImGuiInputTextCallbackData, ImGuiSizeCallbackData, ImGuiPayload, ImGuiTableSortSpecs, ImGuiTableColumnSortSpecs)
40-
// [SECTION] Helpers (ImGuiOnceUponAFrame, ImGuiTextFilter, ImGuiTextBuffer, ImGuiStorage, ImGuiListClipper, ImColor)
40+
// [SECTION] Helpers (ImGuiOnceUponAFrame, ImGuiTextFilter, ImGuiTextBuffer, ImGuiStorage, ImGuiListClipper, Math Operators, ImColor)
4141
// [SECTION] Drawing API (ImDrawCallback, ImDrawCmd, ImDrawIdx, ImDrawVert, ImDrawChannel, ImDrawListSplitter, ImDrawFlags, ImDrawListFlags, ImDrawList, ImDrawData)
4242
// [SECTION] Font API (ImFontConfig, ImFontGlyph, ImFontGlyphRangesBuilder, ImFontAtlasFlags, ImFontAtlas, ImFont)
4343
// [SECTION] Viewports (ImGuiViewportFlags, ImGuiViewport)
@@ -2159,7 +2159,7 @@ struct ImGuiTableSortSpecs
21592159
};
21602160

21612161
//-----------------------------------------------------------------------------
2162-
// [SECTION] Helpers (ImGuiOnceUponAFrame, ImGuiTextFilter, ImGuiTextBuffer, ImGuiStorage, ImGuiListClipper, ImColor)
2162+
// [SECTION] Helpers (ImGuiOnceUponAFrame, ImGuiTextFilter, ImGuiTextBuffer, ImGuiStorage, ImGuiListClipper, Math Operators, ImColor)
21632163
//-----------------------------------------------------------------------------
21642164

21652165
// Helper: Unicode defines
@@ -2322,6 +2322,32 @@ struct ImGuiListClipper
23222322
#endif
23232323
};
23242324

2325+
// Helpers: ImVec2/ImVec4 operators
2326+
// - It is important that we are keeping those disabled by default so they don't leak in user space.
2327+
// - This is in order to allow user enabling implicit cast operators between ImVec2/ImVec4 and their own types (using IM_VEC2_CLASS_EXTRA in imconfig.h)
2328+
// - You can use '#define IMGUI_DEFINE_MATH_OPERATORS' to import our operators, provided as a courtesy.
2329+
// - We unfortunately don't have a unary- operator for ImVec2 because this would needs to be defined inside the class itself.
2330+
#ifdef IMGUI_DEFINE_MATH_OPERATORS
2331+
#define IMGUI_DEFINE_MATH_OPERATORS_IMPLEMENTED
2332+
IM_MSVC_RUNTIME_CHECKS_OFF
2333+
static inline ImVec2 operator*(const ImVec2& lhs, const float rhs) { return ImVec2(lhs.x * rhs, lhs.y * rhs); }
2334+
static inline ImVec2 operator/(const ImVec2& lhs, const float rhs) { return ImVec2(lhs.x / rhs, lhs.y / rhs); }
2335+
static inline ImVec2 operator+(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x + rhs.x, lhs.y + rhs.y); }
2336+
static inline ImVec2 operator-(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x - rhs.x, lhs.y - rhs.y); }
2337+
static inline ImVec2 operator*(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x * rhs.x, lhs.y * rhs.y); }
2338+
static inline ImVec2 operator/(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x / rhs.x, lhs.y / rhs.y); }
2339+
static inline ImVec2& operator*=(ImVec2& lhs, const float rhs) { lhs.x *= rhs; lhs.y *= rhs; return lhs; }
2340+
static inline ImVec2& operator/=(ImVec2& lhs, const float rhs) { lhs.x /= rhs; lhs.y /= rhs; return lhs; }
2341+
static inline ImVec2& operator+=(ImVec2& lhs, const ImVec2& rhs) { lhs.x += rhs.x; lhs.y += rhs.y; return lhs; }
2342+
static inline ImVec2& operator-=(ImVec2& lhs, const ImVec2& rhs) { lhs.x -= rhs.x; lhs.y -= rhs.y; return lhs; }
2343+
static inline ImVec2& operator*=(ImVec2& lhs, const ImVec2& rhs) { lhs.x *= rhs.x; lhs.y *= rhs.y; return lhs; }
2344+
static inline ImVec2& operator/=(ImVec2& lhs, const ImVec2& rhs) { lhs.x /= rhs.x; lhs.y /= rhs.y; return lhs; }
2345+
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); }
2346+
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); }
2347+
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); }
2348+
IM_MSVC_RUNTIME_CHECKS_RESTORE
2349+
#endif
2350+
23252351
// Helpers macros to generate 32-bit encoded colors
23262352
// User can declare their own format by #defining the 5 _SHIFT/_MASK macros in their imconfig file.
23272353
#ifndef IM_COL32_R_SHIFT

imgui_demo.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
// - We try to declare static variables in the local scope, as close as possible to the code using them.
3939
// - We never use any of the helpers/facilities used internally by Dear ImGui, unless available in the public API.
4040
// - We never use maths operators on ImVec2/ImVec4. For our other sources files we use them, and they are provided
41-
// by imgui_internal.h using the IMGUI_DEFINE_MATH_OPERATORS define. For your own sources file they are optional
41+
// by imgui.h using the IMGUI_DEFINE_MATH_OPERATORS define. For your own sources file they are optional
4242
// and require you either enable those, either provide your own via IM_VEC2_CLASS_EXTRA in imconfig.h.
4343
// Because we can't assume anything about your support of maths operators, we cannot use them in imgui_demo.cpp.
4444

imgui_draw.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,12 @@ Index of this file:
2626
#define _CRT_SECURE_NO_WARNINGS
2727
#endif
2828

29-
#include "imgui.h"
30-
#ifndef IMGUI_DISABLE
31-
3229
#ifndef IMGUI_DEFINE_MATH_OPERATORS
3330
#define IMGUI_DEFINE_MATH_OPERATORS
3431
#endif
3532

33+
#include "imgui.h"
34+
#ifndef IMGUI_DISABLE
3635
#include "imgui_internal.h"
3736
#ifdef IMGUI_ENABLE_FREETYPE
3837
#include "misc/freetype/imgui_freetype.h"

0 commit comments

Comments
 (0)