New version of IMGUI_ONCE_UPON_A_FRAME helper macro

This commit is contained in:
ocornut 2014-11-28 23:08:45 +00:00
parent df2ad5e899
commit 311a2f8328
2 changed files with 30 additions and 22 deletions

View File

@ -111,7 +111,7 @@
Occasionally introducing changes that are breaking the API. The breakage are generally minor and easy to fix. Occasionally introducing changes that are breaking the API. The breakage are generally minor and easy to fix.
Here is a change-log of API breaking changes, if you are using one of the functions listed, expect to have to fix some code. Here is a change-log of API breaking changes, if you are using one of the functions listed, expect to have to fix some code.
- 2014/11/26 (1.17) retired IMGUI_ONCE_UPON_A_FRAME helper macro in favor of ImGuiOnceUponAFrame type that works on all compilers. - 2014/11/26 (1.17) reworked syntax of IMGUI_ONCE_UPON_A_FRAME helper macro to increase compiler compatibility
- 2014/11/07 (1.15) renamed IsHovered() to IsItemHovered() - 2014/11/07 (1.15) renamed IsHovered() to IsItemHovered()
- 2014/10/02 (1.14) renamed IMGUI_INCLUDE_IMGUI_USER_CPP to IMGUI_INCLUDE_IMGUI_USER_INL and imgui_user.cpp to imgui_user.inl (more IDE friendly) - 2014/10/02 (1.14) renamed IMGUI_INCLUDE_IMGUI_USER_CPP to IMGUI_INCLUDE_IMGUI_USER_INL and imgui_user.cpp to imgui_user.inl (more IDE friendly)
- 2014/09/25 (1.13) removed 'text_end' parameter from IO.SetClipboardTextFn (the string is now always zero-terminated for simplicity) - 2014/09/25 (1.13) removed 'text_end' parameter from IO.SetClipboardTextFn (the string is now always zero-terminated for simplicity)
@ -158,7 +158,7 @@
e.g. "##Foobar" display an empty label and uses "##Foobar" as ID e.g. "##Foobar" display an empty label and uses "##Foobar" as ID
- read articles about the imgui principles (see web links) to understand the requirement and use of ID. - read articles about the imgui principles (see web links) to understand the requirement and use of ID.
- tip: the construct 'static ImGuiOnceUponAFrame once; if (once)' will evaluate to 'true' only once a frame, you can use it to quickly add custom UI in the middle of a deep nested inner loop in your code. - tip: the construct 'IMGUI_ONCE_UPON_A_FRAME { ... }' will evaluate to a block of code only once a frame. You can use it to quickly add custom UI in the middle of a deep nested inner loop in your code.
- tip: you can call Render() multiple times (e.g for VR renders), up to you to communicate the extra state to your RenderDrawListFn function. - tip: you can call Render() multiple times (e.g for VR renders), up to you to communicate the extra state to your RenderDrawListFn function.
- tip: you can create widgets without a Begin()/End() block, they will go in an implicit window called "Debug" - tip: you can create widgets without a Begin()/End() block, they will go in an implicit window called "Debug"
- tip: read the ShowTestWindow() code for more example of how to use ImGui! - tip: read the ShowTestWindow() code for more example of how to use ImGui!
@ -6687,12 +6687,16 @@ void ImGui::ShowTestWindow(bool* open)
ImGui::EndTooltip(); ImGui::EndTooltip();
} }
//static ImGuiOnceUponAFrame oaf; // Testing IMGUI_ONCE_UPON_A_FRAME macro
//if (oaf) ImGui::Text("This will be displayed."); //for (int i = 0; i < 5; i++)
//if (oaf) ImGui::Text("This won't be displayed!"); //{
// IMGUI_ONCE_UPON_A_FRAME
// {
// ImGui::Text("This will be displayed only once.");
// }
//}
ImGui::Separator(); ImGui::Separator();
ImGui::Text("^ Horizontal separator");
static int item = 1; static int item = 1;
ImGui::Combo("combo", &item, "aaaa\0bbbb\0cccc\0dddd\0eeee\0\0"); ImGui::Combo("combo", &item, "aaaa\0bbbb\0cccc\0dddd\0eeee\0\0");

12
imgui.h
View File

@ -122,7 +122,7 @@ public:
#endif // #ifndef ImVector #endif // #ifndef ImVector
// Helpers at bottom of the file: // Helpers at bottom of the file:
// - struct ImGuiOnceUponAFrame // Execute a block of code once per frame only (convenient for creating UI within deep-nested code that runs multiple times) // - IMGUI_ONCE_UPON_A_FRAME // Execute a block of code once per frame only (convenient for creating UI within deep-nested code that runs multiple times)
// - struct ImGuiTextFilter // Parse and apply text filters. In format "aaaaa[,bbbb][,ccccc]" // - struct ImGuiTextFilter // Parse and apply text filters. In format "aaaaa[,bbbb][,ccccc]"
// - struct ImGuiTextBuffer // Text buffer for logging/accumulating text // - struct ImGuiTextBuffer // Text buffer for logging/accumulating text
// - struct ImGuiStorage // Custom key value storage (if you need to alter open/close states manually) // - struct ImGuiStorage // Custom key value storage (if you need to alter open/close states manually)
@ -530,8 +530,12 @@ struct ImGuiIO
// Helper: execute a block of code once a frame only // Helper: execute a block of code once a frame only
// Convenient if you want to quickly create an UI within deep-nested code that runs multiple times every frame. // Convenient if you want to quickly create an UI within deep-nested code that runs multiple times every frame.
// Usage: // Usage:
// static ImGuiOnceUponAFrame once; // IMGUI_ONCE_UPON_A_FRAME
// if (once) { ... } // {
// // code block will be executed one per frame
// }
// Attention! the macro expand into 2 statement so make sure you don't use it within e.g. an if() statement without curly braces.
#define IMGUI_ONCE_UPON_A_FRAME static ImGuiOnceUponAFrame imgui_oaf##__LINE__; if (imgui_oaf##__LINE__)
struct ImGuiOnceUponAFrame struct ImGuiOnceUponAFrame
{ {
ImGuiOnceUponAFrame() { RefFrame = -1; } ImGuiOnceUponAFrame() { RefFrame = -1; }
@ -756,7 +760,7 @@ struct ImFont
IMGUI_API bool LoadFromFile(const char* filename); IMGUI_API bool LoadFromFile(const char* filename);
IMGUI_API void Clear(); IMGUI_API void Clear();
IMGUI_API void BuildLookupTable(); IMGUI_API void BuildLookupTable();
IMGUI_API const FntGlyph * FindGlyph(unsigned short c, const FntGlyph* fallback = NULL) const; IMGUI_API const FntGlyph* FindGlyph(unsigned short c, const FntGlyph* fallback = NULL) const;
IMGUI_API float GetFontSize() const { return (float)Info->FontSize; } IMGUI_API float GetFontSize() const { return (float)Info->FontSize; }
IMGUI_API bool IsLoaded() const { return Info != NULL && Common != NULL && Glyphs != NULL; } IMGUI_API bool IsLoaded() const { return Info != NULL && Common != NULL && Glyphs != NULL; }